Whamcloud - gitweb
8ecb298773ba9a282ba5a6b5b6463b2bdbd61c71
[fs/lustre-release.git] / lustre / mdt / mdt_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/mdt/mdt_reint.c
39  *
40  * Lustre Metadata Target (mdt) reintegration routines
41  *
42  * Author: Peter Braam <braam@clusterfs.com>
43  * Author: Andreas Dilger <adilger@clusterfs.com>
44  * Author: Phil Schwan <phil@clusterfs.com>
45  * Author: Huang Hua <huanghua@clusterfs.com>
46  * Author: Yury Umanets <umka@clusterfs.com>
47  */
48
49 #ifndef EXPORT_SYMTAB
50 # define EXPORT_SYMTAB
51 #endif
52 #define DEBUG_SUBSYSTEM S_MDS
53
54 #include "mdt_internal.h"
55 #include "lu_time.h"
56
57 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
58                                      struct md_attr *ma)
59 {
60         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
61         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
62                                                &RMF_MDT_MD, RCL_SERVER);
63
64         ma->ma_cookie = req_capsule_server_get(info->mti_pill,
65                                                &RMF_LOGCOOKIES);
66         ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
67                                                   &RMF_LOGCOOKIES,
68                                                   RCL_SERVER);
69
70         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
71         ma->ma_valid = 0;
72 }
73
74 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
75                                 struct mdt_object *object,
76                                 struct mdt_body *repbody)
77 {
78         ENTRY;
79
80         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
81          * we won't go to below*/
82         if (repbody->valid & OBD_MD_FLMDSCAPA)
83                 RETURN(rc);
84
85         if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
86             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
87                 struct lustre_capa *capa;
88
89                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
90                 LASSERT(capa);
91                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
92                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
93                                  0);
94                 if (rc == 0)
95                         repbody->valid |= OBD_MD_FLMDSCAPA;
96         }
97
98         RETURN(rc);
99 }
100
101 /**
102  * Get version of object by fid.
103  *
104  * Return real version or ENOENT_VERSION if object doesn't exist
105  */
106 static void mdt_obj_version_get(struct mdt_thread_info *info,
107                                 struct mdt_object *o, __u64 *version)
108 {
109         LASSERT(o);
110         LASSERT(mdt_object_exists(o) >= 0);
111         if (mdt_object_exists(o) > 0)
112                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
113         else
114                 *version = ENOENT_VERSION;
115         CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
116                PFID(mdt_object_fid(o)), *version);
117 }
118
119 /**
120  * Check version is correct.
121  *
122  * Should be called only during replay.
123  */
124 static int mdt_version_check(struct ptlrpc_request *req,
125                              __u64 version, int idx)
126 {
127         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
128         ENTRY;
129
130         if (!exp_connect_vbr(req->rq_export))
131                 RETURN(0);
132
133         LASSERT(req_is_replay(req));
134         /** VBR: version is checked always because costs nothing */
135         LASSERT(idx < PTLRPC_NUM_VERSIONS);
136         /** Sanity check for malformed buffers */
137         if (pre_ver == NULL) {
138                 CERROR("No versions in request buffer\n");
139                 cfs_spin_lock(&req->rq_export->exp_lock);
140                 req->rq_export->exp_vbr_failed = 1;
141                 cfs_spin_unlock(&req->rq_export->exp_lock);
142                 RETURN(-EOVERFLOW);
143         } else if (pre_ver[idx] != version) {
144                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
145                        pre_ver[idx], version);
146                 cfs_spin_lock(&req->rq_export->exp_lock);
147                 req->rq_export->exp_vbr_failed = 1;
148                 cfs_spin_unlock(&req->rq_export->exp_lock);
149                 RETURN(-EOVERFLOW);
150         }
151         RETURN(0);
152 }
153
154 /**
155  * Save pre-versions in reply.
156  */
157 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
158                              int idx)
159 {
160         __u64 *reply_ver;
161
162         if (!exp_connect_vbr(req->rq_export))
163                 return;
164
165         LASSERT(!req_is_replay(req));
166         LASSERT(req->rq_repmsg != NULL);
167         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
168         if (reply_ver)
169                 reply_ver[idx] = version;
170 }
171
172 /**
173  * Save enoent version, it is needed when it is obvious that object doesn't
174  * exist, e.g. child during create.
175  */
176 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
177 {
178         /* save version of file name for replay, it must be ENOENT here */
179         if (!req_is_replay(mdt_info_req(info))) {
180                 info->mti_ver[idx] = ENOENT_VERSION;
181                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
182         }
183 }
184
185 /**
186  * Get version from disk and save in reply buffer.
187  *
188  * Versions are saved in reply only during normal operations not replays.
189  */
190 void mdt_version_get_save(struct mdt_thread_info *info,
191                           struct mdt_object *mto, int idx)
192 {
193         /* don't save versions during replay */
194         if (!req_is_replay(mdt_info_req(info))) {
195                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
196                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
197         }
198 }
199
200 /**
201  * Get version from disk and check it, no save in reply.
202  */
203 int mdt_version_get_check(struct mdt_thread_info *info,
204                           struct mdt_object *mto, int idx)
205 {
206         /* only check versions during replay */
207         if (!req_is_replay(mdt_info_req(info)))
208                 return 0;
209
210         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
211         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
212 }
213
214 /**
215  * Get version from disk and check if recovery or just save.
216  */
217 int mdt_version_get_check_save(struct mdt_thread_info *info,
218                                struct mdt_object *mto, int idx)
219 {
220         int rc = 0;
221
222         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
223         if (req_is_replay(mdt_info_req(info)))
224                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
225                                        idx);
226         else
227                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
228         return rc;
229 }
230
231 /**
232  * Lookup with version checking.
233  *
234  * This checks version of 'name'. Many reint functions uses 'name' for child not
235  * FID, therefore we need to get object by name and check its version.
236  */
237 int mdt_lookup_version_check(struct mdt_thread_info *info,
238                              struct mdt_object *p, struct lu_name *lname,
239                              struct lu_fid *fid, int idx)
240 {
241         int rc, vbrc;
242
243         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
244                         &info->mti_spec);
245         /* Check version only during replay */
246         if (!req_is_replay(mdt_info_req(info)))
247                 return rc;
248
249         info->mti_ver[idx] = ENOENT_VERSION;
250         if (rc == 0) {
251                 struct mdt_object *child;
252                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
253                 if (likely(!IS_ERR(child))) {
254                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
255                         mdt_object_put(info->mti_env, child);
256                 }
257         }
258         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
259         return vbrc ? vbrc : rc;
260
261 }
262
263 /*
264  * VBR: we save three versions in reply:
265  * 0 - parent. Check that parent version is the same during replay.
266  * 1 - name. Version of 'name' if file exists with the same name or
267  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
268  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
269  * check.
270  */
271 static int mdt_md_create(struct mdt_thread_info *info)
272 {
273         struct mdt_device       *mdt = info->mti_mdt;
274         struct mdt_object       *parent;
275         struct mdt_object       *child;
276         struct mdt_lock_handle  *lh;
277         struct mdt_body         *repbody;
278         struct md_attr          *ma = &info->mti_attr;
279         struct mdt_reint_record *rr = &info->mti_rr;
280         struct lu_name          *lname;
281         int rc;
282         ENTRY;
283
284         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  (%s->"DFID") in "DFID,
285                   rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
286
287         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
288
289         lh = &info->mti_lh[MDT_LH_PARENT];
290         mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
291
292         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
293                                       MDS_INODELOCK_UPDATE);
294         if (IS_ERR(parent))
295                 RETURN(PTR_ERR(parent));
296
297         rc = mdt_version_get_check_save(info, parent, 0);
298         if (rc)
299                 GOTO(out_put_parent, rc);
300
301         /*
302          * Check child name version during replay.
303          * During create replay a file may exist with same name.
304          */
305         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
306         rc = mdt_lookup_version_check(info, parent, lname,
307                                       &info->mti_tmp_fid1, 1);
308         /* -ENOENT is expected here */
309         if (rc != 0 && rc != -ENOENT)
310                 GOTO(out_put_parent, rc);
311
312         /* save version of file name for replay, it must be ENOENT here */
313         mdt_enoent_version_save(info, 1);
314
315         child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
316         if (likely(!IS_ERR(child))) {
317                 struct md_object *next = mdt_object_child(parent);
318
319                 ma->ma_need = MA_INODE;
320                 ma->ma_valid = 0;
321                 /* capa for cross-ref will be stored here */
322                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
323                                                      &RMF_CAPA1);
324                 LASSERT(ma->ma_capa);
325
326                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
327                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
328
329                 /* Version of child will be updated on disk. */
330                 info->mti_mos = child;
331                 rc = mdt_version_get_check_save(info, child, 2);
332                 if (rc)
333                         GOTO(out_put_child, rc);
334
335                 /* Let lower layer know current lock mode. */
336                 info->mti_spec.sp_cr_mode =
337                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
338
339                 /*
340                  * Do perform lookup sanity check. We do not know if name exists
341                  * or not.
342                  */
343                 info->mti_spec.sp_cr_lookup = 1;
344                 info->mti_spec.sp_feat = &dt_directory_features;
345
346                 rc = mdo_create(info->mti_env, next, lname,
347                                 mdt_object_child(child),
348                                 &info->mti_spec, ma);
349                 if (rc == 0) {
350                         /* Return fid & attr to client. */
351                         if (ma->ma_valid & MA_INODE)
352                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
353                                                    mdt_object_fid(child));
354                 }
355 out_put_child:
356                 mdt_object_put(info->mti_env, child);
357         } else {
358                 rc = PTR_ERR(child);
359         }
360         mdt_create_pack_capa(info, rc, child, repbody);
361 out_put_parent:
362         mdt_object_unlock_put(info, parent, lh, rc);
363         RETURN(rc);
364 }
365
366 /* Partial request to create object only */
367 static int mdt_md_mkobj(struct mdt_thread_info *info)
368 {
369         struct mdt_device      *mdt = info->mti_mdt;
370         struct mdt_object      *o;
371         struct mdt_body        *repbody;
372         struct md_attr         *ma = &info->mti_attr;
373         int rc;
374         ENTRY;
375
376         DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
377                   PFID(info->mti_rr.rr_fid2));
378
379         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
380
381         o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
382         if (!IS_ERR(o)) {
383                 struct md_object *next = mdt_object_child(o);
384
385                 ma->ma_need = MA_INODE;
386                 ma->ma_valid = 0;
387
388                 /*
389                  * Cross-ref create can encounter already created obj in case of
390                  * recovery, just get attr in that case.
391                  */
392                 if (mdt_object_exists(o) == 1) {
393                         rc = mo_attr_get(info->mti_env, next, ma);
394                 } else {
395                         /*
396                          * Here, NO permission check for object_create,
397                          * such check has been done on the original MDS.
398                          */
399                         rc = mo_object_create(info->mti_env, next,
400                                               &info->mti_spec, ma);
401                 }
402                 if (rc == 0) {
403                         /* Return fid & attr to client. */
404                         if (ma->ma_valid & MA_INODE)
405                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
406                                                    mdt_object_fid(o));
407                 }
408                 mdt_object_put(info->mti_env, o);
409         } else
410                 rc = PTR_ERR(o);
411
412         mdt_create_pack_capa(info, rc, o, repbody);
413         RETURN(rc);
414 }
415
416 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
417                  struct md_attr *ma, int flags)
418 {
419         struct mdt_lock_handle  *lh;
420         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
421         __u64 lockpart = MDS_INODELOCK_UPDATE;
422         int rc;
423         ENTRY;
424
425         /* attr shouldn't be set on remote object */
426         LASSERT(mdt_object_exists(mo) >= 0);
427
428         lh = &info->mti_lh[MDT_LH_PARENT];
429         mdt_lock_reg_init(lh, LCK_PW);
430
431         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
432                 lockpart |= MDS_INODELOCK_LOOKUP;
433
434         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
435         if (rc != 0)
436                 RETURN(rc);
437
438         if (mdt_object_exists(mo) == 0)
439                 GOTO(out_unlock, rc = -ENOENT);
440
441         /* all attrs are packed into mti_attr in unpack_setattr */
442         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
443                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
444
445         /* This is only for set ctime when rename's source is on remote MDS. */
446         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
447                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
448
449         /* VBR: update version if attr changed are important for recovery */
450         if (do_vbr) {
451                 /* update on-disk version of changed object */
452                 info->mti_mos = mo;
453                 rc = mdt_version_get_check_save(info, mo, 0);
454                 if (rc)
455                         GOTO(out_unlock, rc);
456         }
457
458         /* all attrs are packed into mti_attr in unpack_setattr */
459         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
460         if (rc != 0)
461                 GOTO(out_unlock, rc);
462
463         EXIT;
464 out_unlock:
465         mdt_object_unlock(info, mo, lh, rc);
466         return rc;
467 }
468
469 static int mdt_reint_setattr(struct mdt_thread_info *info,
470                              struct mdt_lock_handle *lhc)
471 {
472         struct md_attr          *ma = &info->mti_attr;
473         struct mdt_reint_record *rr = &info->mti_rr;
474         struct ptlrpc_request   *req = mdt_info_req(info);
475         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
476         struct mdt_file_data    *mfd;
477         struct mdt_object       *mo;
478         struct md_object        *next;
479         struct mdt_body         *repbody;
480         int                      som_au, rc;
481         ENTRY;
482
483         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
484                   (unsigned int)ma->ma_attr.la_valid);
485
486         if (info->mti_dlm_req)
487                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
488
489         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
490         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
491         if (IS_ERR(mo))
492                 GOTO(out, rc = PTR_ERR(mo));
493
494         /* start a log jounal handle if needed */
495         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
496                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
497                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
498                         /* Check write access for the O_TRUNC case */
499                         if (mdt_write_read(mo) < 0)
500                                 GOTO(out_put, rc = -ETXTBSY);
501                 }
502         } else if (info->mti_ioepoch &&
503                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
504                 /* Truncate case. IOEpoch is opened. */
505                 rc = mdt_write_get(mo);
506                 if (rc)
507                         GOTO(out_put, rc);
508
509                 mfd = mdt_mfd_new();
510                 if (mfd == NULL) {
511                         mdt_write_put(mo);
512                         GOTO(out_put, rc = -ENOMEM);
513                 }
514
515                 mdt_ioepoch_open(info, mo, 0);
516                 repbody->ioepoch = mo->mot_ioepoch;
517
518                 mdt_object_get(info->mti_env, mo);
519                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
520                 mfd->mfd_object = mo;
521                 mfd->mfd_xid = req->rq_xid;
522
523                 cfs_spin_lock(&med->med_open_lock);
524                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
525                 cfs_spin_unlock(&med->med_open_lock);
526                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
527         }
528
529         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
530         if (som_au) {
531                 /* SOM Attribute update case. Find the proper mfd and update
532                  * SOM attributes on the proper object. */
533                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
534                 LASSERT(info->mti_ioepoch);
535
536                 cfs_spin_lock(&med->med_open_lock);
537                 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
538                 if (mfd == NULL) {
539                         cfs_spin_unlock(&med->med_open_lock);
540                         CDEBUG(D_INODE, "no handle for file close: "
541                                "fid = "DFID": cookie = "LPX64"\n",
542                                PFID(info->mti_rr.rr_fid1),
543                                info->mti_ioepoch->handle.cookie);
544                         GOTO(out_put, rc = -ESTALE);
545                 }
546                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
547                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
548
549                 class_handle_unhash(&mfd->mfd_handle);
550                 cfs_list_del_init(&mfd->mfd_list);
551                 cfs_spin_unlock(&med->med_open_lock);
552
553                 /* Close the found mfd, update attributes. */
554                 ma->ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
555                 OBD_ALLOC_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
556                 if (ma->ma_lmm == NULL)
557                         GOTO(out_put, rc = -ENOMEM);
558
559                 mdt_mfd_close(info, mfd);
560
561                 OBD_FREE_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
562         } else {
563                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
564                 if (rc)
565                         GOTO(out_put, rc);
566         }
567
568         ma->ma_need = MA_INODE;
569         ma->ma_valid = 0;
570         next = mdt_object_child(mo);
571         rc = mo_attr_get(info->mti_env, next, ma);
572         if (rc != 0)
573                 GOTO(out_put, rc);
574
575         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
576
577         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
578             info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
579             S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
580             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
581                 struct lustre_capa *capa;
582
583                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
584                 LASSERT(capa);
585                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
586                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
587                 if (rc)
588                         GOTO(out_put, rc);
589                 repbody->valid |= OBD_MD_FLOSSCAPA;
590         }
591
592         EXIT;
593 out_put:
594         mdt_object_put(info->mti_env, mo);
595 out:
596         if (rc == 0)
597                 mdt_counter_incr(req->rq_export, LPROC_MDT_SETATTR);
598
599         mdt_client_compatibility(info);
600         mdt_shrink_reply(info);
601         return rc;
602 }
603
604 static int mdt_reint_create(struct mdt_thread_info *info,
605                             struct mdt_lock_handle *lhc)
606 {
607         struct ptlrpc_request   *req = mdt_info_req(info);
608         int                     rc;
609         ENTRY;
610
611         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
612                 RETURN(err_serious(-ESTALE));
613
614         if (info->mti_dlm_req)
615                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
616
617         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
618         case S_IFDIR:{
619                 /* Cross-ref case. */
620                 /* TODO: we can add LPROC_MDT_CROSS for cross-ref stats */
621                 if (info->mti_cross_ref) {
622                         rc = mdt_md_mkobj(info);
623                 } else {
624                         LASSERT(info->mti_rr.rr_namelen > 0);
625                         mdt_counter_incr(req->rq_export, LPROC_MDT_MKDIR);
626                         rc = mdt_md_create(info);
627                 }
628                 break;
629         }
630         case S_IFREG:
631         case S_IFLNK:
632         case S_IFCHR:
633         case S_IFBLK:
634         case S_IFIFO:
635         case S_IFSOCK:{
636                 /* Special file should stay on the same node as parent. */
637                 LASSERT(info->mti_rr.rr_namelen > 0);
638                 mdt_counter_incr(req->rq_export, LPROC_MDT_MKNOD);
639                 rc = mdt_md_create(info);
640                 break;
641         }
642         default:
643                 rc = err_serious(-EOPNOTSUPP);
644         }
645         RETURN(rc);
646 }
647
648 /*
649  * VBR: save parent version in reply and child version getting by its name.
650  * Version of child is getting and checking during its lookup. If
651  */
652 static int mdt_reint_unlink(struct mdt_thread_info *info,
653                             struct mdt_lock_handle *lhc)
654 {
655         struct mdt_reint_record *rr = &info->mti_rr;
656         struct ptlrpc_request   *req = mdt_info_req(info);
657         struct md_attr          *ma = &info->mti_attr;
658         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
659         struct mdt_object       *mp;
660         struct mdt_object       *mc;
661         struct mdt_lock_handle  *parent_lh;
662         struct mdt_lock_handle  *child_lh;
663         struct lu_name          *lname;
664         int                      rc;
665         ENTRY;
666
667         DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
668                   rr->rr_name);
669
670         if (info->mti_dlm_req)
671                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
672
673         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
674                 RETURN(err_serious(-ENOENT));
675
676         /*
677          * step 1: lock the parent. Note, this may be child in case of
678          * remote operation denoted by ->mti_cross_ref flag.
679          */
680         parent_lh = &info->mti_lh[MDT_LH_PARENT];
681         if (info->mti_cross_ref) {
682                 /*
683                  * Init reg lock for cross ref case when we need to do only
684                  * ref del locally.
685                  */
686                 mdt_lock_reg_init(parent_lh, LCK_PW);
687         } else {
688                 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
689                                   rr->rr_namelen);
690         }
691         mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
692                                   MDS_INODELOCK_UPDATE);
693         if (IS_ERR(mp)) {
694                 rc = PTR_ERR(mp);
695                 /* errors are possible here in cross-ref cases, see below */
696                 if (info->mti_cross_ref)
697                         rc = 0;
698                 GOTO(out, rc);
699         }
700
701         rc = mdt_version_get_check_save(info, mp, 0);
702         if (rc)
703                 GOTO(out_unlock_parent, rc);
704
705         mdt_reint_init_ma(info, ma);
706         if (!ma->ma_lmm || !ma->ma_cookie)
707                 GOTO(out_unlock_parent, rc = -EINVAL);
708
709         if (info->mti_cross_ref) {
710                 /*
711                  * Remote partial operation. It is possible that replay may
712                  * happen on parent MDT and this operation will be repeated.
713                  * Therefore the object absense is allowed case and nothing
714                  * should be done here.
715                  */
716                 if (mdt_object_exists(mp) > 0) {
717                         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
718                         rc = mo_ref_del(info->mti_env,
719                                         mdt_object_child(mp), ma);
720                         if (rc == 0)
721                                 mdt_handle_last_unlink(info, mp, ma);
722                 } else
723                         rc = 0;
724                 GOTO(out_unlock_parent, rc);
725         }
726
727         /* step 2: find & lock the child */
728         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
729         /* lookup child object along with version checking */
730         rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
731         if (rc != 0)
732                  GOTO(out_unlock_parent, rc);
733
734         /* We will lock the child regardless it is local or remote. No harm. */
735         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
736         if (IS_ERR(mc))
737                 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
738         child_lh = &info->mti_lh[MDT_LH_CHILD];
739         mdt_lock_reg_init(child_lh, LCK_EX);
740         rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
741                              MDT_CROSS_LOCK);
742         if (rc != 0) {
743                 mdt_object_put(info->mti_env, mc);
744                 GOTO(out_unlock_parent, rc);
745         }
746
747         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
748                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
749         /* save version when object is locked */
750         mdt_version_get_save(info, mc, 1);
751         /*
752          * Now we can only make sure we need MA_INODE, in mdd layer, will check
753          * whether need MA_LOV and MA_COOKIE.
754          */
755         ma->ma_need = MA_INODE;
756         ma->ma_valid = 0;
757         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
758         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
759                         mdt_object_child(mc), lname, ma);
760         if (rc == 0)
761                 mdt_handle_last_unlink(info, mc, ma);
762
763         if (ma->ma_valid & MA_INODE) {
764                 switch (ma->ma_attr.la_mode & S_IFMT) {
765                 case S_IFDIR:
766                         mdt_counter_incr(req->rq_export, LPROC_MDT_RMDIR);
767                         break;
768                 case S_IFREG:
769                 case S_IFLNK:
770                 case S_IFCHR:
771                 case S_IFBLK:
772                 case S_IFIFO:
773                 case S_IFSOCK:
774                         mdt_counter_incr(req->rq_export, LPROC_MDT_UNLINK);
775                         break;
776                 default:
777                         LASSERTF(0, "bad file type %o unlinking\n",
778                                  ma->ma_attr.la_mode);
779                 }
780         }
781
782         EXIT;
783
784         mdt_object_unlock_put(info, mc, child_lh, rc);
785 out_unlock_parent:
786         mdt_object_unlock_put(info, mp, parent_lh, rc);
787 out:
788         return rc;
789 }
790
791 /*
792  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
793  * name.
794  */
795 static int mdt_reint_link(struct mdt_thread_info *info,
796                           struct mdt_lock_handle *lhc)
797 {
798         struct mdt_reint_record *rr = &info->mti_rr;
799         struct ptlrpc_request   *req = mdt_info_req(info);
800         struct md_attr          *ma = &info->mti_attr;
801         struct mdt_object       *ms;
802         struct mdt_object       *mp;
803         struct mdt_lock_handle  *lhs;
804         struct mdt_lock_handle  *lhp;
805         struct lu_name          *lname;
806         int rc;
807         ENTRY;
808
809         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
810                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
811
812         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
813                 RETURN(err_serious(-ENOENT));
814
815         if (info->mti_dlm_req)
816                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
817
818         if (info->mti_cross_ref) {
819                 /* MDT holding name ask us to add ref. */
820                 lhs = &info->mti_lh[MDT_LH_CHILD];
821                 mdt_lock_reg_init(lhs, LCK_EX);
822                 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
823                                           MDS_INODELOCK_UPDATE);
824                 if (IS_ERR(ms))
825                         RETURN(PTR_ERR(ms));
826
827                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
828                 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
829                 mdt_object_unlock_put(info, ms, lhs, rc);
830                 RETURN(rc);
831         }
832
833         /* Invalid case so return error immediately instead of
834          * processing it */
835         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
836                 RETURN(-EPERM);
837
838         /* step 1: find & lock the target parent dir */
839         lhp = &info->mti_lh[MDT_LH_PARENT];
840         mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
841                           rr->rr_namelen);
842         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
843                                   MDS_INODELOCK_UPDATE);
844         if (IS_ERR(mp))
845                 RETURN(PTR_ERR(mp));
846
847         rc = mdt_version_get_check_save(info, mp, 0);
848         if (rc)
849                 GOTO(out_unlock_parent, rc);
850
851         /* step 2: find & lock the source */
852         lhs = &info->mti_lh[MDT_LH_CHILD];
853         mdt_lock_reg_init(lhs, LCK_EX);
854
855         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
856         if (IS_ERR(ms))
857                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
858
859         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
860                             MDT_CROSS_LOCK);
861         if (rc != 0) {
862                 mdt_object_put(info->mti_env, ms);
863                 GOTO(out_unlock_parent, rc);
864         }
865
866         /* step 3: link it */
867         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
868                        OBD_FAIL_MDS_REINT_LINK_WRITE);
869
870         info->mti_mos = ms;
871         rc = mdt_version_get_check_save(info, ms, 1);
872         if (rc)
873                 GOTO(out_unlock_child, rc);
874
875         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
876         /** check target version by name during replay */
877         rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
878         if (rc != 0 && rc != -ENOENT)
879                 GOTO(out_unlock_child, rc);
880         /* save version of file name for replay, it must be ENOENT here */
881         if (!req_is_replay(mdt_info_req(info))) {
882                 info->mti_ver[2] = ENOENT_VERSION;
883                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
884         }
885
886         rc = mdo_link(info->mti_env, mdt_object_child(mp),
887                       mdt_object_child(ms), lname, ma);
888
889         if (rc == 0)
890                 mdt_counter_incr(req->rq_export, LPROC_MDT_LINK);
891
892         EXIT;
893 out_unlock_child:
894         mdt_object_unlock_put(info, ms, lhs, rc);
895 out_unlock_parent:
896         mdt_object_unlock_put(info, mp, lhp, rc);
897         return rc;
898 }
899 /**
900  * lock the part of the directory according to the hash of the name
901  * (lh->mlh_pdo_hash) in parallel directory lock.
902  */
903 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
904                        struct mdt_lock_handle *lh,
905                        struct mdt_object *obj, __u64 ibits)
906 {
907         struct ldlm_res_id *res_id = &info->mti_res_id;
908         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
909         ldlm_policy_data_t *policy = &info->mti_policy;
910         int rc;
911
912         /*
913          * Finish res_id initializing by name hash marking part of
914          * directory which is taking modification.
915          */
916         LASSERT(lh->mlh_pdo_hash != 0);
917         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
918         memset(policy, 0, sizeof(*policy));
919         policy->l_inodebits.bits = ibits;
920         /*
921          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
922          * going to be sent to client. If it is - mdt_intent_policy() path will
923          * fix it up and turn FL_LOCAL flag off.
924          */
925         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
926                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
927                           &info->mti_exp->exp_handle.h_cookie);
928         return rc;
929 }
930
931 /* partial operation for rename */
932 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
933 {
934         struct mdt_reint_record *rr = &info->mti_rr;
935         struct ptlrpc_request   *req = mdt_info_req(info);
936         struct md_attr          *ma = &info->mti_attr;
937         struct mdt_object       *mtgtdir;
938         struct mdt_object       *mtgt = NULL;
939         struct mdt_lock_handle  *lh_tgtdir;
940         struct mdt_lock_handle  *lh_tgt = NULL;
941         struct lu_fid           *tgt_fid = &info->mti_tmp_fid1;
942         struct lu_name          *lname;
943         int                      rc;
944         ENTRY;
945
946         DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
947                   rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
948
949         /* step 1: lookup & lock the tgt dir. */
950         lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
951         mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
952                           rr->rr_tgtlen);
953         mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
954                                        MDS_INODELOCK_UPDATE);
955         if (IS_ERR(mtgtdir))
956                 RETURN(PTR_ERR(mtgtdir));
957
958         /* step 2: find & lock the target object if exists. */
959         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
960         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
961         rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
962                         lname, tgt_fid, &info->mti_spec);
963         if (rc != 0 && rc != -ENOENT) {
964                 GOTO(out_unlock_tgtdir, rc);
965         } else if (rc == 0) {
966                 /*
967                  * In case of replay that name can be already inserted, check
968                  * that and do nothing if so.
969                  */
970                 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
971                         GOTO(out_unlock_tgtdir, rc);
972
973                 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
974                 mdt_lock_reg_init(lh_tgt, LCK_EX);
975
976                 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
977                                             MDS_INODELOCK_LOOKUP);
978                 if (IS_ERR(mtgt))
979                         GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
980
981                 mdt_reint_init_ma(info, ma);
982                 if (!ma->ma_lmm || !ma->ma_cookie)
983                         GOTO(out_unlock_tgt, rc = -EINVAL);
984
985                 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
986                                     mdt_object_child(mtgt), rr->rr_fid2,
987                                     lname, ma);
988         } else /* -ENOENT */ {
989                 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
990                                      lname, rr->rr_fid2, ma);
991         }
992
993         /* handle last link of tgt object */
994         if (rc == 0 && mtgt)
995                 mdt_handle_last_unlink(info, mtgt, ma);
996
997         EXIT;
998 out_unlock_tgt:
999         if (mtgt)
1000                 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
1001 out_unlock_tgtdir:
1002         mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
1003         return rc;
1004 }
1005
1006 static int mdt_rename_lock(struct mdt_thread_info *info,
1007                            struct lustre_handle *lh)
1008 {
1009         struct ldlm_namespace *ns     = info->mti_mdt->mdt_namespace;
1010         ldlm_policy_data_t    *policy = &info->mti_policy;
1011         struct ldlm_res_id    *res_id = &info->mti_res_id;
1012         struct md_site        *ms;
1013         int rc;
1014         ENTRY;
1015
1016         ms = mdt_md_site(info->mti_mdt);
1017         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1018
1019         memset(policy, 0, sizeof *policy);
1020         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1021
1022         if (ms->ms_control_exp == NULL) {
1023                 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1024
1025                 /*
1026                  * Current node is controller, that is mdt0, where we should
1027                  * take BFL lock.
1028                  */
1029                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1030                                             LCK_EX, &flags, ldlm_blocking_ast,
1031                                             ldlm_completion_ast, NULL, NULL, 0,
1032                                             &info->mti_exp->exp_handle.h_cookie,
1033                                             lh);
1034         } else {
1035                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
1036                      ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1037                 int flags = 0;
1038
1039                 /*
1040                  * This is the case mdt0 is remote node, issue DLM lock like
1041                  * other clients.
1042                  */
1043                 rc = ldlm_cli_enqueue(ms->ms_control_exp, NULL, &einfo, res_id,
1044                                       policy, &flags, NULL, 0, lh, 0);
1045         }
1046
1047         RETURN(rc);
1048 }
1049
1050 static void mdt_rename_unlock(struct lustre_handle *lh)
1051 {
1052         ENTRY;
1053         LASSERT(lustre_handle_is_used(lh));
1054         ldlm_lock_decref(lh, LCK_EX);
1055         EXIT;
1056 }
1057
1058 /*
1059  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1060  * target. Source should not be ancestor of target dir. May be other rename
1061  * checks can be moved here later.
1062  */
1063 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1064 {
1065         struct mdt_reint_record *rr = &info->mti_rr;
1066         struct lu_fid dst_fid = *rr->rr_fid2;
1067         struct mdt_object *dst;
1068         int rc = 0;
1069         ENTRY;
1070
1071         do {
1072                 LASSERT(fid_is_sane(&dst_fid));
1073                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1074                 if (!IS_ERR(dst)) {
1075                         rc = mdo_is_subdir(info->mti_env,
1076                                            mdt_object_child(dst), fid,
1077                                            &dst_fid);
1078                         mdt_object_put(info->mti_env, dst);
1079                         if (rc != -EREMOTE && rc < 0) {
1080                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1081                         } else {
1082                                 /* check the found fid */
1083                                 if (lu_fid_eq(&dst_fid, fid))
1084                                         rc = -EINVAL;
1085                         }
1086                 } else {
1087                         rc = PTR_ERR(dst);
1088                 }
1089         } while (rc == -EREMOTE);
1090
1091         RETURN(rc);
1092 }
1093
1094 /*
1095  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1096  * 2 - src child; 3 - tgt child.
1097  * Update on disk version of src child.
1098  */
1099 static int mdt_reint_rename(struct mdt_thread_info *info,
1100                             struct mdt_lock_handle *lhc)
1101 {
1102         struct mdt_reint_record *rr = &info->mti_rr;
1103         struct md_attr          *ma = &info->mti_attr;
1104         struct ptlrpc_request   *req = mdt_info_req(info);
1105         struct mdt_object       *msrcdir;
1106         struct mdt_object       *mtgtdir;
1107         struct mdt_object       *mold;
1108         struct mdt_object       *mnew = NULL;
1109         struct mdt_lock_handle  *lh_srcdirp;
1110         struct mdt_lock_handle  *lh_tgtdirp;
1111         struct mdt_lock_handle  *lh_oldp;
1112         struct mdt_lock_handle  *lh_newp;
1113         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1114         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1115         struct lustre_handle     rename_lh = { 0 };
1116         struct lu_name           slname = { 0 };
1117         struct lu_name          *lname;
1118         int                      rc;
1119         ENTRY;
1120
1121         if (info->mti_dlm_req)
1122                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1123
1124         if (info->mti_cross_ref) {
1125                 rc = mdt_reint_rename_tgt(info);
1126                 RETURN(rc);
1127         }
1128
1129         DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1130                   PFID(rr->rr_fid1), rr->rr_name,
1131                   PFID(rr->rr_fid2), rr->rr_tgt);
1132
1133         rc = mdt_rename_lock(info, &rename_lh);
1134         if (rc) {
1135                 CERROR("Can't lock FS for rename, rc %d\n", rc);
1136                 RETURN(rc);
1137         }
1138
1139         lh_newp = &info->mti_lh[MDT_LH_NEW];
1140
1141         /* step 1: lock the source dir. */
1142         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1143         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1144                           rr->rr_namelen);
1145         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1146                                        MDS_INODELOCK_UPDATE);
1147         if (IS_ERR(msrcdir))
1148                 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1149
1150         rc = mdt_version_get_check_save(info, msrcdir, 0);
1151         if (rc)
1152                 GOTO(out_unlock_source, rc);
1153
1154         /* step 2: find & lock the target dir. */
1155         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1156         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1157                           rr->rr_tgtlen);
1158         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1159                 mdt_object_get(info->mti_env, msrcdir);
1160                 mtgtdir = msrcdir;
1161                 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1162                          rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1163                                                  MDS_INODELOCK_UPDATE);
1164                          if (rc)
1165                                  GOTO(out_unlock_source, rc);
1166                          OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1167                 }
1168         } else {
1169                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1170                                           rr->rr_fid2);
1171                 if (IS_ERR(mtgtdir))
1172                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1173
1174                 /* check early, the real version will be saved after locking */
1175                 rc = mdt_version_get_check(info, mtgtdir, 1);
1176                 if (rc)
1177                         GOTO(out_put_target, rc);
1178
1179                 rc = mdt_object_exists(mtgtdir);
1180                 if (rc == 0) {
1181                         GOTO(out_put_target, rc = -ESTALE);
1182                 } else if (rc > 0) {
1183                         /* we lock the target dir if it is local */
1184                         rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1185                                              MDS_INODELOCK_UPDATE,
1186                                              MDT_LOCAL_LOCK);
1187                         if (rc != 0)
1188                                 GOTO(out_put_target, rc);
1189                         /* get and save correct version after locking */
1190                         mdt_version_get_save(info, mtgtdir, 1);
1191                 }
1192         }
1193
1194         /* step 3: find & lock the old object. */
1195         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1196         mdt_name_copy(&slname, lname);
1197         rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1198         if (rc != 0)
1199                 GOTO(out_unlock_target, rc);
1200
1201         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1202                 GOTO(out_unlock_target, rc = -EINVAL);
1203
1204         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1205         if (IS_ERR(mold))
1206                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1207
1208         lh_oldp = &info->mti_lh[MDT_LH_OLD];
1209         mdt_lock_reg_init(lh_oldp, LCK_EX);
1210         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1211                              MDT_CROSS_LOCK);
1212         if (rc != 0) {
1213                 mdt_object_put(info->mti_env, mold);
1214                 GOTO(out_unlock_target, rc);
1215         }
1216
1217         info->mti_mos = mold;
1218         /* save version after locking */
1219         mdt_version_get_save(info, mold, 2);
1220         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1221
1222         /* step 4: find & lock the new object. */
1223         /* new target object may not exist now */
1224         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1225         /* lookup with version checking */
1226         rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1227         if (rc == 0) {
1228                 /* the new_fid should have been filled at this moment */
1229                 if (lu_fid_eq(old_fid, new_fid))
1230                        GOTO(out_unlock_old, rc);
1231
1232                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1233                     lu_fid_eq(new_fid, rr->rr_fid2))
1234                         GOTO(out_unlock_old, rc = -EINVAL);
1235
1236                 mdt_lock_reg_init(lh_newp, LCK_EX);
1237                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1238                 if (IS_ERR(mnew))
1239                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1240
1241                 rc = mdt_object_lock(info, mnew, lh_newp,
1242                                      MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1243                 if (rc != 0) {
1244                         mdt_object_put(info->mti_env, mnew);
1245                         GOTO(out_unlock_old, rc);
1246                 }
1247                 /* get and save version after locking */
1248                 mdt_version_get_save(info, mnew, 3);
1249                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1250         } else if (rc != -EREMOTE && rc != -ENOENT) {
1251                 GOTO(out_unlock_old, rc);
1252         } else {
1253                 mdt_enoent_version_save(info, 3);
1254         }
1255
1256         /* step 5: rename it */
1257         mdt_reint_init_ma(info, ma);
1258         if (!ma->ma_lmm || !ma->ma_cookie)
1259                 GOTO(out_unlock_new, rc = -EINVAL);
1260
1261         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1262                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1263
1264
1265         /* Check if @dst is subdir of @src. */
1266         rc = mdt_rename_sanity(info, old_fid);
1267         if (rc)
1268                 GOTO(out_unlock_new, rc);
1269
1270         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1271                         mdt_object_child(mtgtdir), old_fid, &slname,
1272                         (mnew ? mdt_object_child(mnew) : NULL),
1273                         lname, ma);
1274
1275         /* handle last link of tgt object */
1276         if (rc == 0) {
1277                 mdt_counter_incr(req->rq_export, LPROC_MDT_RENAME);
1278                 if (mnew)
1279                         mdt_handle_last_unlink(info, mnew, ma);
1280
1281                 mdt_rename_counter_tally(info, info->mti_mdt, req->rq_export,
1282                                          msrcdir, mtgtdir);
1283         }
1284
1285         EXIT;
1286 out_unlock_new:
1287         if (mnew)
1288                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1289 out_unlock_old:
1290         mdt_object_unlock_put(info, mold, lh_oldp, rc);
1291 out_unlock_target:
1292         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1293 out_put_target:
1294         mdt_object_put(info->mti_env, mtgtdir);
1295 out_unlock_source:
1296         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1297 out_rename_lock:
1298         mdt_rename_unlock(&rename_lh);
1299         return rc;
1300 }
1301
1302 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1303                            struct mdt_lock_handle *lhc);
1304
1305 static mdt_reinter reinters[REINT_MAX] = {
1306         [REINT_SETATTR]  = mdt_reint_setattr,
1307         [REINT_CREATE]   = mdt_reint_create,
1308         [REINT_LINK]     = mdt_reint_link,
1309         [REINT_UNLINK]   = mdt_reint_unlink,
1310         [REINT_RENAME]   = mdt_reint_rename,
1311         [REINT_OPEN]     = mdt_reint_open,
1312         [REINT_SETXATTR] = mdt_reint_setxattr
1313 };
1314
1315 int mdt_reint_rec(struct mdt_thread_info *info,
1316                   struct mdt_lock_handle *lhc)
1317 {
1318         int rc;
1319         ENTRY;
1320
1321         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1322
1323         RETURN(rc);
1324 }