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