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