Whamcloud - gitweb
LU-1187 out: add resend check for update.
[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, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_reint.c
37  *
38  * Lustre Metadata Target (mdt) reintegration routines
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  * Author: Huang Hua <huanghua@clusterfs.com>
44  * Author: Yury Umanets <umka@clusterfs.com>
45  */
46
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include "mdt_internal.h"
50
51 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
52                                      struct md_attr *ma)
53 {
54         ma->ma_need = MA_INODE;
55         ma->ma_valid = 0;
56 }
57
58 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
59                                 struct mdt_object *object,
60                                 struct mdt_body *repbody)
61 {
62         ENTRY;
63
64         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
65          * we won't go to below*/
66         if (repbody->valid & OBD_MD_FLMDSCAPA)
67                 RETURN(rc);
68
69         if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
70             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
71                 struct lustre_capa *capa;
72
73                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
74                 LASSERT(capa);
75                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
76                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
77                                  0);
78                 if (rc == 0)
79                         repbody->valid |= OBD_MD_FLMDSCAPA;
80         }
81
82         RETURN(rc);
83 }
84
85 /**
86  * Get version of object by fid.
87  *
88  * Return real version or ENOENT_VERSION if object doesn't exist
89  */
90 static void mdt_obj_version_get(struct mdt_thread_info *info,
91                                 struct mdt_object *o, __u64 *version)
92 {
93         LASSERT(o);
94         if (mdt_object_exists(o) && !mdt_object_remote(o) &&
95             !fid_is_obf(mdt_object_fid(o)))
96                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
97         else
98                 *version = ENOENT_VERSION;
99         CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
100                PFID(mdt_object_fid(o)), *version);
101 }
102
103 /**
104  * Check version is correct.
105  *
106  * Should be called only during replay.
107  */
108 static int mdt_version_check(struct ptlrpc_request *req,
109                              __u64 version, int idx)
110 {
111         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
112         ENTRY;
113
114         if (!exp_connect_vbr(req->rq_export))
115                 RETURN(0);
116
117         LASSERT(req_is_replay(req));
118         /** VBR: version is checked always because costs nothing */
119         LASSERT(idx < PTLRPC_NUM_VERSIONS);
120         /** Sanity check for malformed buffers */
121         if (pre_ver == NULL) {
122                 CERROR("No versions in request buffer\n");
123                 spin_lock(&req->rq_export->exp_lock);
124                 req->rq_export->exp_vbr_failed = 1;
125                 spin_unlock(&req->rq_export->exp_lock);
126                 RETURN(-EOVERFLOW);
127         } else if (pre_ver[idx] != version) {
128                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
129                        pre_ver[idx], version);
130                 spin_lock(&req->rq_export->exp_lock);
131                 req->rq_export->exp_vbr_failed = 1;
132                 spin_unlock(&req->rq_export->exp_lock);
133                 RETURN(-EOVERFLOW);
134         }
135         RETURN(0);
136 }
137
138 /**
139  * Save pre-versions in reply.
140  */
141 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
142                              int idx)
143 {
144         __u64 *reply_ver;
145
146         if (!exp_connect_vbr(req->rq_export))
147                 return;
148
149         LASSERT(!req_is_replay(req));
150         LASSERT(req->rq_repmsg != NULL);
151         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
152         if (reply_ver)
153                 reply_ver[idx] = version;
154 }
155
156 /**
157  * Save enoent version, it is needed when it is obvious that object doesn't
158  * exist, e.g. child during create.
159  */
160 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
161 {
162         /* save version of file name for replay, it must be ENOENT here */
163         if (!req_is_replay(mdt_info_req(info))) {
164                 info->mti_ver[idx] = ENOENT_VERSION;
165                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
166         }
167 }
168
169 /**
170  * Get version from disk and save in reply buffer.
171  *
172  * Versions are saved in reply only during normal operations not replays.
173  */
174 void mdt_version_get_save(struct mdt_thread_info *info,
175                           struct mdt_object *mto, int idx)
176 {
177         /* don't save versions during replay */
178         if (!req_is_replay(mdt_info_req(info))) {
179                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
180                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
181         }
182 }
183
184 /**
185  * Get version from disk and check it, no save in reply.
186  */
187 int mdt_version_get_check(struct mdt_thread_info *info,
188                           struct mdt_object *mto, int idx)
189 {
190         /* only check versions during replay */
191         if (!req_is_replay(mdt_info_req(info)))
192                 return 0;
193
194         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
195         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
196 }
197
198 /**
199  * Get version from disk and check if recovery or just save.
200  */
201 int mdt_version_get_check_save(struct mdt_thread_info *info,
202                                struct mdt_object *mto, int idx)
203 {
204         int rc = 0;
205
206         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
207         if (req_is_replay(mdt_info_req(info)))
208                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
209                                        idx);
210         else
211                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
212         return rc;
213 }
214
215 /**
216  * Lookup with version checking.
217  *
218  * This checks version of 'name'. Many reint functions uses 'name' for child not
219  * FID, therefore we need to get object by name and check its version.
220  */
221 int mdt_lookup_version_check(struct mdt_thread_info *info,
222                              struct mdt_object *p, struct lu_name *lname,
223                              struct lu_fid *fid, int idx)
224 {
225         int rc, vbrc;
226
227         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
228                         &info->mti_spec);
229         /* Check version only during replay */
230         if (!req_is_replay(mdt_info_req(info)))
231                 return rc;
232
233         info->mti_ver[idx] = ENOENT_VERSION;
234         if (rc == 0) {
235                 struct mdt_object *child;
236                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
237                 if (likely(!IS_ERR(child))) {
238                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
239                         mdt_object_put(info->mti_env, child);
240                 }
241         }
242         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
243         return vbrc ? vbrc : rc;
244
245 }
246
247 /*
248  * VBR: we save three versions in reply:
249  * 0 - parent. Check that parent version is the same during replay.
250  * 1 - name. Version of 'name' if file exists with the same name or
251  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
252  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
253  * check.
254  */
255 static int mdt_md_create(struct mdt_thread_info *info)
256 {
257         struct mdt_device       *mdt = info->mti_mdt;
258         struct mdt_object       *parent;
259         struct mdt_object       *child;
260         struct mdt_lock_handle  *lh;
261         struct mdt_body         *repbody;
262         struct md_attr          *ma = &info->mti_attr;
263         struct mdt_reint_record *rr = &info->mti_rr;
264         struct lu_name          *lname;
265         int rc;
266         ENTRY;
267
268         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  (%s->"DFID") in "DFID,
269                   rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
270
271         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
272                 RETURN(-EPERM);
273
274         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
275
276         lh = &info->mti_lh[MDT_LH_PARENT];
277         mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
278
279         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
280                                       MDS_INODELOCK_UPDATE);
281         if (IS_ERR(parent))
282                 RETURN(PTR_ERR(parent));
283
284         rc = mdt_version_get_check_save(info, parent, 0);
285         if (rc)
286                 GOTO(out_put_parent, rc);
287
288         /*
289          * Check child name version during replay.
290          * During create replay a file may exist with same name.
291          */
292         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
293         rc = mdt_lookup_version_check(info, parent, lname,
294                                       &info->mti_tmp_fid1, 1);
295         if (rc == 0)
296                 GOTO(out_put_parent, rc = -EEXIST);
297
298         /* -ENOENT is expected here */
299         if (rc != -ENOENT)
300                 GOTO(out_put_parent, rc);
301
302         /* save version of file name for replay, it must be ENOENT here */
303         mdt_enoent_version_save(info, 1);
304
305         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
306         if (likely(!IS_ERR(child))) {
307                 struct md_object *next = mdt_object_child(parent);
308
309                 if (mdt_object_remote(child)) {
310                         struct seq_server_site *ss;
311                         struct lu_ucred *uc  = mdt_ucred(info);
312
313                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
314                                 if (uc->uc_gid !=
315                                     mdt->mdt_enable_remote_dir_gid &&
316                                     mdt->mdt_enable_remote_dir_gid != -1) {
317                                         CERROR("%s: Creating remote dir is only"
318                                                " permitted for administrator or"
319                                                " set mdt_enable_remote_dir_gid:"
320                                                " rc = %d\n",
321                                                 mdt2obd_dev(mdt)->obd_name,
322                                                 -EPERM);
323                                         GOTO(out_put_child, rc = -EPERM);
324                                 }
325                         }
326
327                         ss = mdt_seq_site(mdt);
328                         if (ss->ss_node_id != 0 &&
329                             mdt->mdt_enable_remote_dir == 0) {
330                                 CERROR("%s: remote dir is only permitted on"
331                                        " MDT0 or set_param"
332                                        " mdt.*.enable_remote_dir=1\n",
333                                        mdt2obd_dev(mdt)->obd_name);
334                                 GOTO(out_put_child, rc = -EPERM);
335                         }
336                         if (!mdt_is_dne_client(mdt_info_req(info)->rq_export)) {
337                                 /* Return -EIO for old client */
338                                 GOTO(out_put_child, rc = -EIO);
339                         }
340
341                 }
342                 ma->ma_need = MA_INODE;
343                 ma->ma_valid = 0;
344                 /* capa for cross-ref will be stored here */
345                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
346                                                      &RMF_CAPA1);
347                 LASSERT(ma->ma_capa);
348
349                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
350                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
351
352                 /* Version of child will be updated on disk. */
353                 info->mti_mos = child;
354                 rc = mdt_version_get_check_save(info, child, 2);
355                 if (rc)
356                         GOTO(out_put_child, rc);
357
358                 /* Let lower layer know current lock mode. */
359                 info->mti_spec.sp_cr_mode =
360                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
361
362                 /*
363                  * Do not perform lookup sanity check. We know that name does
364                  * not exist.
365                  */
366                 info->mti_spec.sp_cr_lookup = 0;
367                 info->mti_spec.sp_feat = &dt_directory_features;
368
369                 rc = mdo_create(info->mti_env, next, lname,
370                                 mdt_object_child(child),
371                                 &info->mti_spec, ma);
372                 if (rc == 0)
373                         rc = mdt_attr_get_complex(info, child, ma);
374
375                 if (rc == 0) {
376                         /* Return fid & attr to client. */
377                         if (ma->ma_valid & MA_INODE)
378                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
379                                                    mdt_object_fid(child));
380                 }
381 out_put_child:
382                 mdt_object_put(info->mti_env, child);
383         } else {
384                 rc = PTR_ERR(child);
385         }
386         mdt_create_pack_capa(info, rc, child, repbody);
387 out_put_parent:
388         mdt_object_unlock_put(info, parent, lh, rc);
389         RETURN(rc);
390 }
391
392 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
393                  struct md_attr *ma, int flags)
394 {
395         struct mdt_lock_handle  *lh;
396         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
397         __u64 lockpart = MDS_INODELOCK_UPDATE;
398         int rc;
399         ENTRY;
400
401         /* attr shouldn't be set on remote object */
402         LASSERT(!mdt_object_remote(mo));
403
404         lh = &info->mti_lh[MDT_LH_PARENT];
405         mdt_lock_reg_init(lh, LCK_PW);
406
407         /* Even though the new MDT will grant PERM lock to the old
408          * client, but the old client will almost ignore that during
409          * So it needs to revoke both LOOKUP and PERM lock here, so
410          * both new and old client can cancel the dcache */
411         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
412                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
413
414         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
415         if (rc != 0)
416                 RETURN(rc);
417
418         if (mdt_object_exists(mo) == 0)
419                 GOTO(out_unlock, rc = -ENOENT);
420
421         /* all attrs are packed into mti_attr in unpack_setattr */
422         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
423                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
424
425         /* This is only for set ctime when rename's source is on remote MDS. */
426         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
427                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
428
429         /* VBR: update version if attr changed are important for recovery */
430         if (do_vbr) {
431                 /* update on-disk version of changed object */
432                 info->mti_mos = mo;
433                 rc = mdt_version_get_check_save(info, mo, 0);
434                 if (rc)
435                         GOTO(out_unlock, rc);
436         }
437
438         /* all attrs are packed into mti_attr in unpack_setattr */
439         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
440         if (rc != 0)
441                 GOTO(out_unlock, rc);
442
443         EXIT;
444 out_unlock:
445         mdt_object_unlock(info, mo, lh, rc);
446         return rc;
447 }
448
449 /**
450  * Check HSM flags and add HS_DIRTY flag if relevant.
451  *
452  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
453  * and is not RELEASED.
454  */
455 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
456                         struct md_attr *ma)
457 {
458         int rc;
459         ENTRY;
460
461         /* If the file was modified, add the dirty flag */
462         ma->ma_need = MA_HSM;
463         rc = mdt_attr_get_complex(info, mo, ma);
464         if (rc) {
465                 CERROR("file attribute read error for "DFID": %d.\n",
466                         PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
467                 RETURN(rc);
468         }
469
470         /* If an up2date copy exists in the backend, add dirty flag */
471         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
472             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
473
474                 ma->ma_hsm.mh_flags |= HS_DIRTY;
475                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
476                 if (rc) {
477                         CERROR("file attribute change error for "DFID": %d\n",
478                                 PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
479                         RETURN(rc);
480                 }
481         }
482
483         RETURN(rc);
484 }
485
486 static int mdt_reint_setattr(struct mdt_thread_info *info,
487                              struct mdt_lock_handle *lhc)
488 {
489         struct md_attr          *ma = &info->mti_attr;
490         struct mdt_reint_record *rr = &info->mti_rr;
491         struct ptlrpc_request   *req = mdt_info_req(info);
492         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
493         struct mdt_file_data    *mfd;
494         struct mdt_object       *mo;
495         struct mdt_body         *repbody;
496         int                      som_au, rc, rc2;
497         ENTRY;
498
499         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
500                   (unsigned int)ma->ma_attr.la_valid);
501
502         if (info->mti_dlm_req)
503                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
504
505         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
506                 RETURN(-EPERM);
507
508         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
509         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
510         if (IS_ERR(mo))
511                 GOTO(out, rc = PTR_ERR(mo));
512
513         /* start a log jounal handle if needed */
514         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
515                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
516                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
517                         /* Check write access for the O_TRUNC case */
518                         if (mdt_write_read(mo) < 0)
519                                 GOTO(out_put, rc = -ETXTBSY);
520                 }
521         } else if (info->mti_ioepoch &&
522                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
523                 /* Truncate case. IOEpoch is opened. */
524                 rc = mdt_write_get(mo);
525                 if (rc)
526                         GOTO(out_put, rc);
527
528                 mfd = mdt_mfd_new();
529                 if (mfd == NULL) {
530                         mdt_write_put(mo);
531                         GOTO(out_put, rc = -ENOMEM);
532                 }
533
534                 mdt_ioepoch_open(info, mo, 0);
535                 repbody->ioepoch = mo->mot_ioepoch;
536
537                 mdt_object_get(info->mti_env, mo);
538                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
539                 mfd->mfd_object = mo;
540                 mfd->mfd_xid = req->rq_xid;
541
542                 spin_lock(&med->med_open_lock);
543                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
544                 spin_unlock(&med->med_open_lock);
545                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
546         }
547
548         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
549         if (som_au) {
550                 /* SOM Attribute update case. Find the proper mfd and update
551                  * SOM attributes on the proper object. */
552                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
553                 LASSERT(info->mti_ioepoch);
554
555                 spin_lock(&med->med_open_lock);
556                 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
557                 if (mfd == NULL) {
558                         spin_unlock(&med->med_open_lock);
559                         CDEBUG(D_INODE, "no handle for file close: "
560                                "fid = "DFID": cookie = "LPX64"\n",
561                                PFID(info->mti_rr.rr_fid1),
562                                info->mti_ioepoch->handle.cookie);
563                         GOTO(out_put, rc = -ESTALE);
564                 }
565                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
566                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
567
568                 class_handle_unhash(&mfd->mfd_handle);
569                 cfs_list_del_init(&mfd->mfd_list);
570                 spin_unlock(&med->med_open_lock);
571
572                 mdt_mfd_close(info, mfd);
573         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
574                 LASSERT((ma->ma_valid & MA_LOV) == 0);
575                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
576                 if (rc)
577                         GOTO(out_put, rc);
578         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
579                 struct lu_buf *buf  = &info->mti_buf;
580                 LASSERT(ma->ma_attr.la_valid == 0);
581                 buf->lb_buf = ma->ma_lmm;
582                 buf->lb_len = ma->ma_lmm_size;
583                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
584                                   buf, XATTR_NAME_LOV, 0);
585                 if (rc)
586                         GOTO(out_put, rc);
587         } else
588                 LBUG();
589
590         /* If file data is modified, add the dirty flag */
591         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
592                 rc = mdt_add_dirty_flag(info, mo, ma);
593
594         ma->ma_need = MA_INODE;
595         ma->ma_valid = 0;
596         rc = mdt_attr_get_complex(info, mo, ma);
597         if (rc != 0)
598                 GOTO(out_put, rc);
599
600         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
601
602         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
603             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
604             S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
605             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
606                 struct lustre_capa *capa;
607
608                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
609                 LASSERT(capa);
610                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
611                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
612                 if (rc)
613                         GOTO(out_put, rc);
614                 repbody->valid |= OBD_MD_FLOSSCAPA;
615         }
616
617         EXIT;
618 out_put:
619         mdt_object_put(info->mti_env, mo);
620 out:
621         if (rc == 0)
622                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
623
624         mdt_client_compatibility(info);
625         rc2 = mdt_fix_reply(info);
626         if (rc == 0)
627                 rc = rc2;
628         return rc;
629 }
630
631 static int mdt_reint_create(struct mdt_thread_info *info,
632                             struct mdt_lock_handle *lhc)
633 {
634         struct ptlrpc_request   *req = mdt_info_req(info);
635         int                     rc;
636         ENTRY;
637
638         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
639                 RETURN(err_serious(-ESTALE));
640
641         if (info->mti_dlm_req)
642                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
643
644         LASSERT(info->mti_rr.rr_namelen > 0);
645         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
646         case S_IFDIR:
647                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
648                 break;
649         case S_IFREG:
650         case S_IFLNK:
651         case S_IFCHR:
652         case S_IFBLK:
653         case S_IFIFO:
654         case S_IFSOCK:
655                 /* Special file should stay on the same node as parent. */
656                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
657                 break;
658         default:
659                 CERROR("%s: Unsupported mode %o\n",
660                        mdt2obd_dev(info->mti_mdt)->obd_name,
661                        info->mti_attr.ma_attr.la_mode);
662                 RETURN(err_serious(-EOPNOTSUPP));
663         }
664
665         rc = mdt_md_create(info);
666         RETURN(rc);
667 }
668
669 /*
670  * VBR: save parent version in reply and child version getting by its name.
671  * Version of child is getting and checking during its lookup. If
672  */
673 static int mdt_reint_unlink(struct mdt_thread_info *info,
674                             struct mdt_lock_handle *lhc)
675 {
676         struct mdt_reint_record *rr = &info->mti_rr;
677         struct ptlrpc_request   *req = mdt_info_req(info);
678         struct md_attr          *ma = &info->mti_attr;
679         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
680         struct mdt_object       *mp;
681         struct mdt_object       *mc;
682         struct mdt_lock_handle  *parent_lh;
683         struct mdt_lock_handle  *child_lh;
684         struct lu_name          *lname;
685         int                      rc;
686         int                      no_name = 0;
687         ENTRY;
688
689         DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
690                   rr->rr_name);
691
692         if (info->mti_dlm_req)
693                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
694
695         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
696                 RETURN(err_serious(-ENOENT));
697
698         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
699                 RETURN(-EPERM);
700         /*
701          * step 1: Found the parent.
702          */
703         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
704         if (IS_ERR(mp)) {
705                 rc = PTR_ERR(mp);
706                 GOTO(out, rc);
707         }
708
709         parent_lh = &info->mti_lh[MDT_LH_PARENT];
710         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
711         if (mdt_object_remote(mp)) {
712                 mdt_lock_reg_init(parent_lh, LCK_EX);
713                 rc = mdt_remote_object_lock(info, mp, &parent_lh->mlh_rreg_lh,
714                                             parent_lh->mlh_rreg_mode,
715                                             MDS_INODELOCK_UPDATE);
716                 if (rc != ELDLM_OK)
717                         GOTO(put_parent, rc);
718
719         } else {
720                 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
721                                   rr->rr_namelen);
722                 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
723                                      MDT_LOCAL_LOCK);
724                 if (rc)
725                         GOTO(put_parent, rc);
726
727                 rc = mdt_version_get_check_save(info, mp, 0);
728                 if (rc)
729                         GOTO(unlock_parent, rc);
730         }
731
732         /* step 2: find & lock the child */
733         /* lookup child object along with version checking */
734         fid_zero(child_fid);
735         rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
736         if (rc != 0) {
737                 /* Name might not be able to find during resend of
738                  * remote unlink, considering following case.
739                  * dir_A is a remote directory, the name entry of
740                  * dir_A is on MDT0, the directory is on MDT1,
741                  *
742                  * 1. client sends unlink req to MDT1.
743                  * 2. MDT1 sends name delete update to MDT0.
744                  * 3. name entry is being deleted in MDT0 synchronously.
745                  * 4. MDT1 is restarted.
746                  * 5. client resends unlink req to MDT1. So it can not
747                  *    find the name entry on MDT0 anymore.
748                  * In this case, MDT1 only needs to destory the local
749                  * directory.
750                  * */
751                 if (mdt_object_remote(mp) && rc == -ENOENT &&
752                     !fid_is_zero(rr->rr_fid2) &&
753                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
754                         no_name = 1;
755                         *child_fid = *rr->rr_fid2;
756                  } else {
757                         GOTO(unlock_parent, rc);
758                  }
759         }
760
761         if (fid_is_obf(child_fid) || fid_is_dot_lustre(child_fid))
762                 GOTO(unlock_parent, rc = -EPERM);
763
764         mdt_reint_init_ma(info, ma);
765
766         /* We will lock the child regardless it is local or remote. No harm. */
767         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
768         if (IS_ERR(mc))
769                 GOTO(unlock_parent, rc = PTR_ERR(mc));
770
771         child_lh = &info->mti_lh[MDT_LH_CHILD];
772         mdt_lock_reg_init(child_lh, LCK_EX);
773         if (mdt_object_remote(mc)) {
774                 struct mdt_body  *repbody;
775
776                 if (!fid_is_zero(rr->rr_fid2)) {
777                         CDEBUG(D_INFO, "%s: name %s can not find "DFID"\n",
778                                mdt2obd_dev(info->mti_mdt)->obd_name,
779                                (char *)rr->rr_name, PFID(mdt_object_fid(mc)));
780                         GOTO(put_child, rc = -ENOENT);
781                 }
782                 CDEBUG(D_INFO, "%s: name %s: "DFID" is another MDT\n",
783                        mdt2obd_dev(info->mti_mdt)->obd_name,
784                        (char *)rr->rr_name, PFID(mdt_object_fid(mc)));
785
786                 if (!mdt_is_dne_client(req->rq_export))
787                         /* Return -EIO for old client */
788                         GOTO(put_child, rc = -EIO);
789
790                 if (info->mti_spec.sp_rm_entry) {
791                         struct lu_ucred *uc  = mdt_ucred(info);
792
793                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
794                                 CERROR("%s: unlink remote entry is only "
795                                        "permitted for administrator: rc = %d\n",
796                                         mdt2obd_dev(info->mti_mdt)->obd_name,
797                                         -EPERM);
798                                 GOTO(put_child, rc = -EPERM);
799                         }
800
801                         ma->ma_need = MA_INODE;
802                         ma->ma_valid = 0;
803                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
804                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
805                                         NULL, lname, ma, no_name);
806                         GOTO(put_child, rc);
807                 }
808                 /* Revoke the LOOKUP lock of the remote object granted by
809                  * this MDT. Since the unlink will happen on another MDT,
810                  * it will release the LOOKUP lock right away. Then What
811                  * would happen if another client try to grab the LOOKUP
812                  * lock at the same time with unlink XXX */
813                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
814                                 MDT_CROSS_LOCK);
815                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
816                 LASSERT(repbody != NULL);
817                 repbody->fid1 = *mdt_object_fid(mc);
818                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
819                 GOTO(unlock_child, rc = -EREMOTE);
820         } else if (info->mti_spec.sp_rm_entry) {
821                 CERROR("%s: lfs rmdir should not be used on local dir %s\n",
822                        mdt2obd_dev(info->mti_mdt)->obd_name,
823                        (char *)rr->rr_name);
824                 GOTO(put_child, rc = -EPERM);
825         }
826
827         rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
828                              MDT_CROSS_LOCK);
829         if (rc != 0) {
830                 GOTO(put_child, rc);
831         }
832
833         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
834                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
835         /* save version when object is locked */
836         mdt_version_get_save(info, mc, 1);
837         /*
838          * Now we can only make sure we need MA_INODE, in mdd layer, will check
839          * whether need MA_LOV and MA_COOKIE.
840          */
841         ma->ma_need = MA_INODE;
842         ma->ma_valid = 0;
843         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
844
845         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
846                         mdt_object_child(mc), lname, ma, no_name);
847         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
848                 rc = mdt_attr_get_complex(info, mc, ma);
849         if (rc == 0)
850                 mdt_handle_last_unlink(info, mc, ma);
851
852         if (ma->ma_valid & MA_INODE) {
853                 switch (ma->ma_attr.la_mode & S_IFMT) {
854                 case S_IFDIR:
855                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
856                         break;
857                 case S_IFREG:
858                 case S_IFLNK:
859                 case S_IFCHR:
860                 case S_IFBLK:
861                 case S_IFIFO:
862                 case S_IFSOCK:
863                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
864                         break;
865                 default:
866                         LASSERTF(0, "bad file type %o unlinking\n",
867                                  ma->ma_attr.la_mode);
868                 }
869         }
870
871         EXIT;
872 unlock_child:
873         mdt_object_unlock(info, mc, child_lh, rc);
874 put_child:
875         mdt_object_put(info->mti_env, mc);
876 unlock_parent:
877         mdt_object_unlock(info, mp, parent_lh, rc);
878 put_parent:
879         mdt_object_put(info->mti_env, mp);
880 out:
881         return rc;
882 }
883
884 /*
885  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
886  * name.
887  */
888 static int mdt_reint_link(struct mdt_thread_info *info,
889                           struct mdt_lock_handle *lhc)
890 {
891         struct mdt_reint_record *rr = &info->mti_rr;
892         struct ptlrpc_request   *req = mdt_info_req(info);
893         struct md_attr          *ma = &info->mti_attr;
894         struct mdt_object       *ms;
895         struct mdt_object       *mp;
896         struct mdt_lock_handle  *lhs;
897         struct mdt_lock_handle  *lhp;
898         struct lu_name          *lname;
899         int rc;
900         ENTRY;
901
902         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
903                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
904
905         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
906                 RETURN(err_serious(-ENOENT));
907
908         if (info->mti_dlm_req)
909                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
910
911         /* Invalid case so return error immediately instead of
912          * processing it */
913         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
914                 RETURN(-EPERM);
915
916         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1) ||
917             fid_is_obf(rr->rr_fid2) || fid_is_dot_lustre(rr->rr_fid2))
918                 RETURN(-EPERM);
919
920         /* step 1: find & lock the target parent dir */
921         lhp = &info->mti_lh[MDT_LH_PARENT];
922         mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
923                           rr->rr_namelen);
924         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
925                                   MDS_INODELOCK_UPDATE);
926         if (IS_ERR(mp))
927                 RETURN(PTR_ERR(mp));
928
929         rc = mdt_version_get_check_save(info, mp, 0);
930         if (rc)
931                 GOTO(out_unlock_parent, rc);
932
933         /* step 2: find & lock the source */
934         lhs = &info->mti_lh[MDT_LH_CHILD];
935         mdt_lock_reg_init(lhs, LCK_EX);
936
937         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
938         if (IS_ERR(ms))
939                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
940
941         if (mdt_object_remote(ms)) {
942                 mdt_object_put(info->mti_env, ms);
943                 CERROR("Target directory "DFID" is on another MDT\n",
944                         PFID(rr->rr_fid1));
945                 GOTO(out_unlock_parent, rc = -EXDEV);
946         }
947
948         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
949                             MDT_CROSS_LOCK);
950         if (rc != 0) {
951                 mdt_object_put(info->mti_env, ms);
952                 GOTO(out_unlock_parent, rc);
953         }
954
955         /* step 3: link it */
956         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
957                        OBD_FAIL_MDS_REINT_LINK_WRITE);
958
959         info->mti_mos = ms;
960         rc = mdt_version_get_check_save(info, ms, 1);
961         if (rc)
962                 GOTO(out_unlock_child, rc);
963
964         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
965         /** check target version by name during replay */
966         rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
967         if (rc != 0 && rc != -ENOENT)
968                 GOTO(out_unlock_child, rc);
969         /* save version of file name for replay, it must be ENOENT here */
970         if (!req_is_replay(mdt_info_req(info))) {
971                 info->mti_ver[2] = ENOENT_VERSION;
972                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
973         }
974
975         rc = mdo_link(info->mti_env, mdt_object_child(mp),
976                       mdt_object_child(ms), lname, ma);
977
978         if (rc == 0)
979                 mdt_counter_incr(req, LPROC_MDT_LINK);
980
981         EXIT;
982 out_unlock_child:
983         mdt_object_unlock_put(info, ms, lhs, rc);
984 out_unlock_parent:
985         mdt_object_unlock_put(info, mp, lhp, rc);
986         return rc;
987 }
988 /**
989  * lock the part of the directory according to the hash of the name
990  * (lh->mlh_pdo_hash) in parallel directory lock.
991  */
992 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
993                        struct mdt_lock_handle *lh,
994                        struct mdt_object *obj, __u64 ibits)
995 {
996         struct ldlm_res_id *res_id = &info->mti_res_id;
997         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
998         ldlm_policy_data_t *policy = &info->mti_policy;
999         int rc;
1000
1001         /*
1002          * Finish res_id initializing by name hash marking part of
1003          * directory which is taking modification.
1004          */
1005         LASSERT(lh->mlh_pdo_hash != 0);
1006         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
1007         memset(policy, 0, sizeof(*policy));
1008         policy->l_inodebits.bits = ibits;
1009         /*
1010          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1011          * going to be sent to client. If it is - mdt_intent_policy() path will
1012          * fix it up and turn FL_LOCAL flag off.
1013          */
1014         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1015                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1016                           &info->mti_exp->exp_handle.h_cookie);
1017         return rc;
1018 }
1019
1020 static int mdt_rename_lock(struct mdt_thread_info *info,
1021                            struct lustre_handle *lh)
1022 {
1023         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
1024         ldlm_policy_data_t      *policy = &info->mti_policy;
1025         struct ldlm_res_id      *res_id = &info->mti_res_id;
1026         __u64                   flags = 0;
1027         int rc;
1028         ENTRY;
1029
1030         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1031
1032         memset(policy, 0, sizeof *policy);
1033         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1034 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 4, 53, 0)
1035         /* In phase I, we will not do cross-rename, so local BFL lock would
1036          * be enough
1037          */
1038         flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1039         /*
1040          * Current node is controller, that is mdt0, where we should
1041          * take BFL lock.
1042          */
1043         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1044                                     LCK_EX, &flags, ldlm_blocking_ast,
1045                                     ldlm_completion_ast, NULL, NULL, 0,
1046                                     LVB_T_NONE,
1047                                     &info->mti_exp->exp_handle.h_cookie,
1048                                     lh);
1049 #else
1050 #warning "Local rename lock is invalid for DNE phase II."
1051 #endif
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         DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1130                   PFID(rr->rr_fid1), rr->rr_name,
1131                   PFID(rr->rr_fid2), rr->rr_tgt);
1132
1133         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1) ||
1134             fid_is_obf(rr->rr_fid2) || fid_is_dot_lustre(rr->rr_fid2))
1135                 RETURN(-EPERM);
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                 if (unlikely(mdt_object_remote(mtgtdir))) {
1184                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1185                                "on different MDTs\n", PFID(rr->rr_fid1),
1186                                PFID(rr->rr_fid2));
1187                         GOTO(out_put_target, rc = -EXDEV);
1188                 } else {
1189                         if (likely(mdt_object_exists(mtgtdir))) {
1190                                 /* we lock the target dir if it is local */
1191                                 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1192                                                      MDS_INODELOCK_UPDATE,
1193                                                      MDT_LOCAL_LOCK);
1194                                 if (rc != 0)
1195                                         GOTO(out_put_target, rc);
1196                                 /* get and save correct version after locking */
1197                                 mdt_version_get_save(info, mtgtdir, 1);
1198                         } else {
1199                                 GOTO(out_put_target, rc = -ESTALE);
1200                         }
1201                 }
1202         }
1203
1204         /* step 3: find & lock the old object. */
1205         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1206         mdt_name_copy(&slname, lname);
1207         fid_zero(old_fid);
1208         rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1209         if (rc != 0)
1210                 GOTO(out_unlock_target, rc);
1211
1212         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1213                 GOTO(out_unlock_target, rc = -EINVAL);
1214
1215         if (fid_is_obf(old_fid) || fid_is_dot_lustre(old_fid))
1216                 GOTO(out_unlock_target, rc = -EPERM);
1217
1218         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1219         if (IS_ERR(mold))
1220                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1221         if (mdt_object_remote(mold)) {
1222                 mdt_object_put(info->mti_env, mold);
1223                 CDEBUG(D_INFO, "Source child "DFID" is on another MDT\n",
1224                        PFID(old_fid));
1225                 GOTO(out_unlock_target, rc = -EXDEV);
1226         }
1227
1228         lh_oldp = &info->mti_lh[MDT_LH_OLD];
1229         mdt_lock_reg_init(lh_oldp, LCK_EX);
1230         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1231                              MDT_CROSS_LOCK);
1232         if (rc != 0) {
1233                 mdt_object_put(info->mti_env, mold);
1234                 GOTO(out_unlock_target, rc);
1235         }
1236
1237         info->mti_mos = mold;
1238         /* save version after locking */
1239         mdt_version_get_save(info, mold, 2);
1240         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1241
1242         /* step 4: find & lock the new object. */
1243         /* new target object may not exist now */
1244         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1245         /* lookup with version checking */
1246         fid_zero(new_fid);
1247         rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1248         if (rc == 0) {
1249                 /* the new_fid should have been filled at this moment */
1250                 if (lu_fid_eq(old_fid, new_fid))
1251                        GOTO(out_unlock_old, rc);
1252
1253                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1254                     lu_fid_eq(new_fid, rr->rr_fid2))
1255                         GOTO(out_unlock_old, rc = -EINVAL);
1256
1257                 if (fid_is_obf(new_fid) || fid_is_dot_lustre(new_fid))
1258                         GOTO(out_unlock_old, rc = -EPERM);
1259
1260                 mdt_lock_reg_init(lh_newp, LCK_EX);
1261                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1262                 if (IS_ERR(mnew))
1263                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1264
1265                 if (mdt_object_remote(mnew)) {
1266                         mdt_object_put(info->mti_env, mnew);
1267                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1268                                PFID(new_fid));
1269                         GOTO(out_unlock_old, rc = -EXDEV);
1270                 }
1271
1272                 rc = mdt_object_lock(info, mnew, lh_newp,
1273                                      MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1274                 if (rc != 0) {
1275                         mdt_object_put(info->mti_env, mnew);
1276                         GOTO(out_unlock_old, rc);
1277                 }
1278                 /* get and save version after locking */
1279                 mdt_version_get_save(info, mnew, 3);
1280                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1281         } else if (rc != -EREMOTE && rc != -ENOENT) {
1282                 GOTO(out_unlock_old, rc);
1283         } else {
1284                 mdt_enoent_version_save(info, 3);
1285         }
1286
1287         /* step 5: rename it */
1288         mdt_reint_init_ma(info, ma);
1289
1290         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1291                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1292
1293
1294         /* Check if @dst is subdir of @src. */
1295         rc = mdt_rename_sanity(info, old_fid);
1296         if (rc)
1297                 GOTO(out_unlock_new, rc);
1298
1299         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1300                         mdt_object_child(mtgtdir), old_fid, &slname,
1301                         (mnew ? mdt_object_child(mnew) : NULL),
1302                         lname, ma);
1303
1304         /* handle last link of tgt object */
1305         if (rc == 0) {
1306                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1307                 if (mnew)
1308                         mdt_handle_last_unlink(info, mnew, ma);
1309
1310                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1311                                          msrcdir, mtgtdir);
1312         }
1313
1314         EXIT;
1315 out_unlock_new:
1316         if (mnew)
1317                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1318 out_unlock_old:
1319         mdt_object_unlock_put(info, mold, lh_oldp, rc);
1320 out_unlock_target:
1321         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1322 out_put_target:
1323         mdt_object_put(info->mti_env, mtgtdir);
1324 out_unlock_source:
1325         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1326 out_rename_lock:
1327         if (lustre_handle_is_used(&rename_lh))
1328                 mdt_rename_unlock(&rename_lh);
1329         return rc;
1330 }
1331
1332 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1333                            struct mdt_lock_handle *lhc);
1334
1335 static mdt_reinter reinters[REINT_MAX] = {
1336         [REINT_SETATTR]  = mdt_reint_setattr,
1337         [REINT_CREATE]   = mdt_reint_create,
1338         [REINT_LINK]     = mdt_reint_link,
1339         [REINT_UNLINK]   = mdt_reint_unlink,
1340         [REINT_RENAME]   = mdt_reint_rename,
1341         [REINT_OPEN]     = mdt_reint_open,
1342         [REINT_SETXATTR] = mdt_reint_setxattr,
1343         [REINT_RMENTRY]  = mdt_reint_unlink
1344 };
1345
1346 int mdt_reint_rec(struct mdt_thread_info *info,
1347                   struct mdt_lock_handle *lhc)
1348 {
1349         int rc;
1350         ENTRY;
1351
1352         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1353
1354         RETURN(rc);
1355 }