Whamcloud - gitweb
5b458cee4e6d2f67ea07fcd9ac73a9ed0b2a896d
[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             !mdt_object_obf(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         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
272
273         lh = &info->mti_lh[MDT_LH_PARENT];
274         mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
275
276         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
277                                       MDS_INODELOCK_UPDATE);
278         if (IS_ERR(parent))
279                 RETURN(PTR_ERR(parent));
280
281         if (mdt_object_obf(parent))
282                 GOTO(out_put_parent, rc = -EPERM);
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                                 CERROR("%s: Creating remote dir is only "
315                                        "permitted for administrator: rc = %d\n",
316                                         mdt2obd_dev(mdt)->obd_name, -EPERM);
317                                 GOTO(out_put_child, rc = -EPERM);
318                         }
319
320                         ss = mdt_seq_site(mdt);
321                         if (ss->ss_node_id != 0 &&
322                             mdt->mdt_enable_remote_dir == 0) {
323                                 CERROR("%s: remote dir is only permitted on"
324                                        " MDT0 or set_param"
325                                        " mdt.*.enable_remote_dir=1\n",
326                                        mdt2obd_dev(mdt)->obd_name);
327                                 GOTO(out_put_child, rc = -EPERM);
328                         }
329                 }
330                 ma->ma_need = MA_INODE;
331                 ma->ma_valid = 0;
332                 /* capa for cross-ref will be stored here */
333                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
334                                                      &RMF_CAPA1);
335                 LASSERT(ma->ma_capa);
336
337                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
338                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
339
340                 /* Version of child will be updated on disk. */
341                 info->mti_mos = child;
342                 rc = mdt_version_get_check_save(info, child, 2);
343                 if (rc)
344                         GOTO(out_put_child, rc);
345
346                 /* Let lower layer know current lock mode. */
347                 info->mti_spec.sp_cr_mode =
348                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
349
350                 /*
351                  * Do not perform lookup sanity check. We know that name does
352                  * not exist.
353                  */
354                 info->mti_spec.sp_cr_lookup = 0;
355                 info->mti_spec.sp_feat = &dt_directory_features;
356
357                 rc = mdo_create(info->mti_env, next, lname,
358                                 mdt_object_child(child),
359                                 &info->mti_spec, ma);
360                 if (rc == 0)
361                         rc = mdt_attr_get_complex(info, child, ma);
362
363                 if (rc == 0) {
364                         /* Return fid & attr to client. */
365                         if (ma->ma_valid & MA_INODE)
366                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
367                                                    mdt_object_fid(child));
368                 }
369 out_put_child:
370                 mdt_object_put(info->mti_env, child);
371         } else {
372                 rc = PTR_ERR(child);
373         }
374         mdt_create_pack_capa(info, rc, child, repbody);
375 out_put_parent:
376         mdt_object_unlock_put(info, parent, lh, rc);
377         RETURN(rc);
378 }
379
380 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
381                  struct md_attr *ma, int flags)
382 {
383         struct mdt_lock_handle  *lh;
384         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
385         __u64 lockpart = MDS_INODELOCK_UPDATE;
386         int rc;
387         ENTRY;
388
389         /* attr shouldn't be set on remote object */
390         LASSERT(!mdt_object_remote(mo));
391
392         lh = &info->mti_lh[MDT_LH_PARENT];
393         mdt_lock_reg_init(lh, LCK_PW);
394
395         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
396                 lockpart |= MDS_INODELOCK_LOOKUP;
397
398         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
399         if (rc != 0)
400                 RETURN(rc);
401
402         if (mdt_object_exists(mo) == 0)
403                 GOTO(out_unlock, rc = -ENOENT);
404
405         /* all attrs are packed into mti_attr in unpack_setattr */
406         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
407                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
408
409         /* This is only for set ctime when rename's source is on remote MDS. */
410         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
411                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
412
413         /* VBR: update version if attr changed are important for recovery */
414         if (do_vbr) {
415                 /* update on-disk version of changed object */
416                 info->mti_mos = mo;
417                 rc = mdt_version_get_check_save(info, mo, 0);
418                 if (rc)
419                         GOTO(out_unlock, rc);
420         }
421
422         /* all attrs are packed into mti_attr in unpack_setattr */
423         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
424         if (rc != 0)
425                 GOTO(out_unlock, rc);
426
427         EXIT;
428 out_unlock:
429         mdt_object_unlock(info, mo, lh, rc);
430         return rc;
431 }
432
433 /**
434  * Check HSM flags and add HS_DIRTY flag if relevant.
435  *
436  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
437  * and is not RELEASED.
438  */
439 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
440                         struct md_attr *ma)
441 {
442         int rc;
443         ENTRY;
444
445         /* If the file was modified, add the dirty flag */
446         ma->ma_need = MA_HSM;
447         rc = mdt_attr_get_complex(info, mo, ma);
448         if (rc) {
449                 CERROR("file attribute read error for "DFID": %d.\n",
450                         PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
451                 RETURN(rc);
452         }
453
454         /* If an up2date copy exists in the backend, add dirty flag */
455         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
456             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
457
458                 ma->ma_hsm.mh_flags |= HS_DIRTY;
459                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
460                 if (rc) {
461                         CERROR("file attribute change error for "DFID": %d\n",
462                                 PFID(lu_object_fid(&mo->mot_obj.mo_lu)), rc);
463                         RETURN(rc);
464                 }
465         }
466
467         RETURN(rc);
468 }
469
470 static int mdt_reint_setattr(struct mdt_thread_info *info,
471                              struct mdt_lock_handle *lhc)
472 {
473         struct md_attr          *ma = &info->mti_attr;
474         struct mdt_reint_record *rr = &info->mti_rr;
475         struct ptlrpc_request   *req = mdt_info_req(info);
476         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
477         struct mdt_file_data    *mfd;
478         struct mdt_object       *mo;
479         struct mdt_body         *repbody;
480         int                      som_au, rc, rc2;
481         ENTRY;
482
483         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
484                   (unsigned int)ma->ma_attr.la_valid);
485
486         if (info->mti_dlm_req)
487                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
488
489         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
490         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
491         if (IS_ERR(mo))
492                 GOTO(out, rc = PTR_ERR(mo));
493
494         if (mdt_object_obf(mo))
495                 GOTO(out_put, rc = -EPERM);
496
497         /* start a log jounal handle if needed */
498         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
499                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
500                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
501                         /* Check write access for the O_TRUNC case */
502                         if (mdt_write_read(mo) < 0)
503                                 GOTO(out_put, rc = -ETXTBSY);
504                 }
505         } else if (info->mti_ioepoch &&
506                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
507                 /* Truncate case. IOEpoch is opened. */
508                 rc = mdt_write_get(mo);
509                 if (rc)
510                         GOTO(out_put, rc);
511
512                 mfd = mdt_mfd_new();
513                 if (mfd == NULL) {
514                         mdt_write_put(mo);
515                         GOTO(out_put, rc = -ENOMEM);
516                 }
517
518                 mdt_ioepoch_open(info, mo, 0);
519                 repbody->ioepoch = mo->mot_ioepoch;
520
521                 mdt_object_get(info->mti_env, mo);
522                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
523                 mfd->mfd_object = mo;
524                 mfd->mfd_xid = req->rq_xid;
525
526                 spin_lock(&med->med_open_lock);
527                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
528                 spin_unlock(&med->med_open_lock);
529                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
530         }
531
532         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
533         if (som_au) {
534                 /* SOM Attribute update case. Find the proper mfd and update
535                  * SOM attributes on the proper object. */
536                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
537                 LASSERT(info->mti_ioepoch);
538
539                 spin_lock(&med->med_open_lock);
540                 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
541                 if (mfd == NULL) {
542                         spin_unlock(&med->med_open_lock);
543                         CDEBUG(D_INODE, "no handle for file close: "
544                                "fid = "DFID": cookie = "LPX64"\n",
545                                PFID(info->mti_rr.rr_fid1),
546                                info->mti_ioepoch->handle.cookie);
547                         GOTO(out_put, rc = -ESTALE);
548                 }
549                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
550                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
551
552                 class_handle_unhash(&mfd->mfd_handle);
553                 cfs_list_del_init(&mfd->mfd_list);
554                 spin_unlock(&med->med_open_lock);
555
556                 mdt_mfd_close(info, mfd);
557         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
558                 LASSERT((ma->ma_valid & MA_LOV) == 0);
559                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
560                 if (rc)
561                         GOTO(out_put, rc);
562         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
563                 struct lu_buf *buf  = &info->mti_buf;
564                 LASSERT(ma->ma_attr.la_valid == 0);
565                 buf->lb_buf = ma->ma_lmm;
566                 buf->lb_len = ma->ma_lmm_size;
567                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
568                                   buf, XATTR_NAME_LOV, 0);
569                 if (rc)
570                         GOTO(out_put, rc);
571         } else
572                 LBUG();
573
574         /* If file data is modified, add the dirty flag */
575         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
576                 rc = mdt_add_dirty_flag(info, mo, ma);
577
578         ma->ma_need = MA_INODE;
579         ma->ma_valid = 0;
580         rc = mdt_attr_get_complex(info, mo, ma);
581         if (rc != 0)
582                 GOTO(out_put, rc);
583
584         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
585
586         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
587             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
588             S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
589             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
590                 struct lustre_capa *capa;
591
592                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
593                 LASSERT(capa);
594                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
595                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
596                 if (rc)
597                         GOTO(out_put, rc);
598                 repbody->valid |= OBD_MD_FLOSSCAPA;
599         }
600
601         EXIT;
602 out_put:
603         mdt_object_put(info->mti_env, mo);
604 out:
605         if (rc == 0)
606                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
607
608         mdt_client_compatibility(info);
609         rc2 = mdt_fix_reply(info);
610         if (rc == 0)
611                 rc = rc2;
612         return rc;
613 }
614
615 static int mdt_reint_create(struct mdt_thread_info *info,
616                             struct mdt_lock_handle *lhc)
617 {
618         struct ptlrpc_request   *req = mdt_info_req(info);
619         int                     rc;
620         ENTRY;
621
622         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
623                 RETURN(err_serious(-ESTALE));
624
625         if (info->mti_dlm_req)
626                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
627
628         LASSERT(info->mti_rr.rr_namelen > 0);
629         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
630         case S_IFDIR:
631                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
632                 break;
633         case S_IFREG:
634         case S_IFLNK:
635         case S_IFCHR:
636         case S_IFBLK:
637         case S_IFIFO:
638         case S_IFSOCK:
639                 /* Special file should stay on the same node as parent. */
640                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
641                 break;
642         default:
643                 CERROR("%s: Unsupported mode %o\n",
644                        mdt2obd_dev(info->mti_mdt)->obd_name,
645                        info->mti_attr.ma_attr.la_mode);
646                 RETURN(err_serious(-EOPNOTSUPP));
647         }
648
649         rc = mdt_md_create(info);
650         RETURN(rc);
651 }
652
653 /*
654  * VBR: save parent version in reply and child version getting by its name.
655  * Version of child is getting and checking during its lookup. If
656  */
657 static int mdt_reint_unlink(struct mdt_thread_info *info,
658                             struct mdt_lock_handle *lhc)
659 {
660         struct mdt_reint_record *rr = &info->mti_rr;
661         struct ptlrpc_request   *req = mdt_info_req(info);
662         struct md_attr          *ma = &info->mti_attr;
663         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
664         struct mdt_object       *mp;
665         struct mdt_object       *mc;
666         struct mdt_lock_handle  *parent_lh;
667         struct mdt_lock_handle  *child_lh;
668         struct lu_name          *lname;
669         int                      rc;
670         ENTRY;
671
672         DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
673                   rr->rr_name);
674
675         if (info->mti_dlm_req)
676                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
677
678         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
679                 RETURN(err_serious(-ENOENT));
680
681         /*
682          * step 1: Found the parent.
683          */
684         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
685         if (IS_ERR(mp)) {
686                 rc = PTR_ERR(mp);
687                 GOTO(out, rc);
688         }
689
690         if (mdt_object_obf(mp))
691                 GOTO(put_parent, rc = -EPERM);
692
693         parent_lh = &info->mti_lh[MDT_LH_PARENT];
694         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
695         if (mdt_object_remote(mp)) {
696                 mdt_lock_reg_init(parent_lh, LCK_EX);
697                 rc = mdt_remote_object_lock(info, mp, &parent_lh->mlh_rreg_lh,
698                                             parent_lh->mlh_rreg_mode,
699                                             MDS_INODELOCK_UPDATE);
700                 if (rc != ELDLM_OK)
701                         GOTO(put_parent, rc);
702
703         } else {
704                 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
705                                   rr->rr_namelen);
706                 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
707                                      MDT_LOCAL_LOCK);
708                 if (rc)
709                         GOTO(put_parent, rc);
710
711                 rc = mdt_version_get_check_save(info, mp, 0);
712                 if (rc)
713                         GOTO(unlock_parent, rc);
714         }
715
716         /* step 2: find & lock the child */
717         /* lookup child object along with version checking */
718         fid_zero(child_fid);
719         rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
720         if (rc != 0)
721                 GOTO(unlock_parent, rc);
722
723         mdt_reint_init_ma(info, ma);
724
725         /* We will lock the child regardless it is local or remote. No harm. */
726         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
727         if (IS_ERR(mc))
728                 GOTO(unlock_parent, rc = PTR_ERR(mc));
729
730         child_lh = &info->mti_lh[MDT_LH_CHILD];
731         mdt_lock_reg_init(child_lh, LCK_EX);
732         if (mdt_object_remote(mc)) {
733                 struct mdt_body  *repbody;
734
735                 if (!fid_is_zero(rr->rr_fid2)) {
736                         CDEBUG(D_INFO, "%s: name %s can not find "DFID"\n",
737                                mdt2obd_dev(info->mti_mdt)->obd_name,
738                                (char *)rr->rr_name, PFID(mdt_object_fid(mc)));
739                         GOTO(unlock_parent, rc = -ENOENT);
740                 }
741                 CDEBUG(D_INFO, "%s: name %s: "DFID" is another MDT\n",
742                        mdt2obd_dev(info->mti_mdt)->obd_name,
743                        (char *)rr->rr_name, PFID(mdt_object_fid(mc)));
744
745                 if (info->mti_spec.sp_rm_entry) {
746                         struct lu_ucred *uc  = mdt_ucred(info);
747
748                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
749                                 CERROR("%s: unlink remote entry is only "
750                                        "permitted for administrator: rc = %d\n",
751                                         mdt2obd_dev(info->mti_mdt)->obd_name,
752                                         -EPERM);
753                                 GOTO(unlock_parent, rc = -EPERM);
754                         }
755
756                         ma->ma_need = MA_INODE;
757                         ma->ma_valid = 0;
758                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
759                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
760                                         NULL, lname, ma);
761                         mdt_object_put(info->mti_env, mc);
762                         GOTO(unlock_parent, rc);
763                 }
764                 /* Revoke the LOOKUP lock of the remote object granted by
765                  * this MDT. Since the unlink will happen on another MDT,
766                  * it will release the LOOKUP lock right away. Then What
767                  * would happen if another client try to grab the LOOKUP
768                  * lock at the same time with unlink XXX */
769                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
770                                 MDT_CROSS_LOCK);
771                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
772                 LASSERT(repbody != NULL);
773                 repbody->fid1 = *mdt_object_fid(mc);
774                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
775                 mdt_object_unlock_put(info, mc, child_lh, rc);
776                 GOTO(unlock_parent, rc = -EREMOTE);
777         } else if (info->mti_spec.sp_rm_entry) {
778                 CERROR("%s: lfs rmdir should not be used on local dir %s\n",
779                        mdt2obd_dev(info->mti_mdt)->obd_name,
780                        (char *)rr->rr_name);
781                 mdt_object_put(info->mti_env, mc);
782                 GOTO(unlock_parent, rc = -EPERM);
783         }
784
785         rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
786                              MDT_CROSS_LOCK);
787         if (rc != 0) {
788                 mdt_object_put(info->mti_env, mc);
789                 GOTO(unlock_parent, rc);
790         }
791
792         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
793                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
794         /* save version when object is locked */
795         mdt_version_get_save(info, mc, 1);
796         /*
797          * Now we can only make sure we need MA_INODE, in mdd layer, will check
798          * whether need MA_LOV and MA_COOKIE.
799          */
800         ma->ma_need = MA_INODE;
801         ma->ma_valid = 0;
802         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
803         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
804                         mdt_object_child(mc), lname, ma);
805         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
806                 rc = mdt_attr_get_complex(info, mc, ma);
807         if (rc == 0)
808                 mdt_handle_last_unlink(info, mc, ma);
809
810         if (ma->ma_valid & MA_INODE) {
811                 switch (ma->ma_attr.la_mode & S_IFMT) {
812                 case S_IFDIR:
813                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
814                         break;
815                 case S_IFREG:
816                 case S_IFLNK:
817                 case S_IFCHR:
818                 case S_IFBLK:
819                 case S_IFIFO:
820                 case S_IFSOCK:
821                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
822                         break;
823                 default:
824                         LASSERTF(0, "bad file type %o unlinking\n",
825                                  ma->ma_attr.la_mode);
826                 }
827         }
828
829         EXIT;
830
831         mdt_object_unlock_put(info, mc, child_lh, rc);
832 unlock_parent:
833         mdt_object_unlock(info, mp, parent_lh, rc);
834 put_parent:
835         mdt_object_put(info->mti_env, mp);
836 out:
837         return rc;
838 }
839
840 /*
841  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
842  * name.
843  */
844 static int mdt_reint_link(struct mdt_thread_info *info,
845                           struct mdt_lock_handle *lhc)
846 {
847         struct mdt_reint_record *rr = &info->mti_rr;
848         struct ptlrpc_request   *req = mdt_info_req(info);
849         struct md_attr          *ma = &info->mti_attr;
850         struct mdt_object       *ms;
851         struct mdt_object       *mp;
852         struct mdt_lock_handle  *lhs;
853         struct mdt_lock_handle  *lhp;
854         struct lu_name          *lname;
855         int rc;
856         ENTRY;
857
858         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
859                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
860
861         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
862                 RETURN(err_serious(-ENOENT));
863
864         if (info->mti_dlm_req)
865                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
866
867         /* Invalid case so return error immediately instead of
868          * processing it */
869         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
870                 RETURN(-EPERM);
871
872         /* step 1: find & lock the target parent dir */
873         lhp = &info->mti_lh[MDT_LH_PARENT];
874         mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
875                           rr->rr_namelen);
876         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
877                                   MDS_INODELOCK_UPDATE);
878         if (IS_ERR(mp))
879                 RETURN(PTR_ERR(mp));
880
881         if (mdt_object_obf(mp))
882                 GOTO(out_unlock_parent, rc = -EPERM);
883
884         rc = mdt_version_get_check_save(info, mp, 0);
885         if (rc)
886                 GOTO(out_unlock_parent, rc);
887
888         /* step 2: find & lock the source */
889         lhs = &info->mti_lh[MDT_LH_CHILD];
890         mdt_lock_reg_init(lhs, LCK_EX);
891
892         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
893         if (IS_ERR(ms))
894                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
895
896         if (mdt_object_remote(ms)) {
897                 mdt_object_put(info->mti_env, ms);
898                 CERROR("Target directory "DFID" is on another MDT\n",
899                         PFID(rr->rr_fid1));
900                 GOTO(out_unlock_parent, rc = -EXDEV);
901         }
902
903         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
904                             MDT_CROSS_LOCK);
905         if (rc != 0) {
906                 mdt_object_put(info->mti_env, ms);
907                 GOTO(out_unlock_parent, rc);
908         }
909
910         /* step 3: link it */
911         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
912                        OBD_FAIL_MDS_REINT_LINK_WRITE);
913
914         info->mti_mos = ms;
915         rc = mdt_version_get_check_save(info, ms, 1);
916         if (rc)
917                 GOTO(out_unlock_child, rc);
918
919         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
920         /** check target version by name during replay */
921         rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
922         if (rc != 0 && rc != -ENOENT)
923                 GOTO(out_unlock_child, rc);
924         /* save version of file name for replay, it must be ENOENT here */
925         if (!req_is_replay(mdt_info_req(info))) {
926                 info->mti_ver[2] = ENOENT_VERSION;
927                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
928         }
929
930         rc = mdo_link(info->mti_env, mdt_object_child(mp),
931                       mdt_object_child(ms), lname, ma);
932
933         if (rc == 0)
934                 mdt_counter_incr(req, LPROC_MDT_LINK);
935
936         EXIT;
937 out_unlock_child:
938         mdt_object_unlock_put(info, ms, lhs, rc);
939 out_unlock_parent:
940         mdt_object_unlock_put(info, mp, lhp, rc);
941         return rc;
942 }
943 /**
944  * lock the part of the directory according to the hash of the name
945  * (lh->mlh_pdo_hash) in parallel directory lock.
946  */
947 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
948                        struct mdt_lock_handle *lh,
949                        struct mdt_object *obj, __u64 ibits)
950 {
951         struct ldlm_res_id *res_id = &info->mti_res_id;
952         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
953         ldlm_policy_data_t *policy = &info->mti_policy;
954         int rc;
955
956         /*
957          * Finish res_id initializing by name hash marking part of
958          * directory which is taking modification.
959          */
960         LASSERT(lh->mlh_pdo_hash != 0);
961         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
962         memset(policy, 0, sizeof(*policy));
963         policy->l_inodebits.bits = ibits;
964         /*
965          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
966          * going to be sent to client. If it is - mdt_intent_policy() path will
967          * fix it up and turn FL_LOCAL flag off.
968          */
969         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
970                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
971                           &info->mti_exp->exp_handle.h_cookie);
972         return rc;
973 }
974
975 static int mdt_rename_lock(struct mdt_thread_info *info,
976                            struct lustre_handle *lh)
977 {
978         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
979         ldlm_policy_data_t      *policy = &info->mti_policy;
980         struct ldlm_res_id      *res_id = &info->mti_res_id;
981         __u64                   flags = 0;
982         int rc;
983         ENTRY;
984
985         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
986
987         memset(policy, 0, sizeof *policy);
988         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
989 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 4, 53, 0)
990         /* In phase I, we will not do cross-rename, so local BFL lock would
991          * be enough
992          */
993         flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
994         /*
995          * Current node is controller, that is mdt0, where we should
996          * take BFL lock.
997          */
998         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
999                                     LCK_EX, &flags, ldlm_blocking_ast,
1000                                     ldlm_completion_ast, NULL, NULL, 0,
1001                                     LVB_T_NONE,
1002                                     &info->mti_exp->exp_handle.h_cookie,
1003                                     lh);
1004 #else
1005 #warning "Local rename lock is invalid for DNE phase II."
1006 #endif
1007         RETURN(rc);
1008 }
1009
1010 static void mdt_rename_unlock(struct lustre_handle *lh)
1011 {
1012         ENTRY;
1013         LASSERT(lustre_handle_is_used(lh));
1014         ldlm_lock_decref(lh, LCK_EX);
1015         EXIT;
1016 }
1017
1018 /*
1019  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1020  * target. Source should not be ancestor of target dir. May be other rename
1021  * checks can be moved here later.
1022  */
1023 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1024 {
1025         struct mdt_reint_record *rr = &info->mti_rr;
1026         struct lu_fid dst_fid = *rr->rr_fid2;
1027         struct mdt_object *dst;
1028         int rc = 0;
1029         ENTRY;
1030
1031         do {
1032                 LASSERT(fid_is_sane(&dst_fid));
1033                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1034                 if (!IS_ERR(dst)) {
1035                         rc = mdo_is_subdir(info->mti_env,
1036                                            mdt_object_child(dst), fid,
1037                                            &dst_fid);
1038                         mdt_object_put(info->mti_env, dst);
1039                         if (rc != -EREMOTE && rc < 0) {
1040                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1041                         } else {
1042                                 /* check the found fid */
1043                                 if (lu_fid_eq(&dst_fid, fid))
1044                                         rc = -EINVAL;
1045                         }
1046                 } else {
1047                         rc = PTR_ERR(dst);
1048                 }
1049         } while (rc == -EREMOTE);
1050
1051         RETURN(rc);
1052 }
1053
1054 /*
1055  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1056  * 2 - src child; 3 - tgt child.
1057  * Update on disk version of src child.
1058  */
1059 static int mdt_reint_rename(struct mdt_thread_info *info,
1060                             struct mdt_lock_handle *lhc)
1061 {
1062         struct mdt_reint_record *rr = &info->mti_rr;
1063         struct md_attr          *ma = &info->mti_attr;
1064         struct ptlrpc_request   *req = mdt_info_req(info);
1065         struct mdt_object       *msrcdir;
1066         struct mdt_object       *mtgtdir;
1067         struct mdt_object       *mold;
1068         struct mdt_object       *mnew = NULL;
1069         struct mdt_lock_handle  *lh_srcdirp;
1070         struct mdt_lock_handle  *lh_tgtdirp;
1071         struct mdt_lock_handle  *lh_oldp;
1072         struct mdt_lock_handle  *lh_newp;
1073         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1074         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1075         struct lustre_handle     rename_lh = { 0 };
1076         struct lu_name           slname = { 0 };
1077         struct lu_name          *lname;
1078         int                      rc;
1079         ENTRY;
1080
1081         if (info->mti_dlm_req)
1082                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1083
1084         DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1085                   PFID(rr->rr_fid1), rr->rr_name,
1086                   PFID(rr->rr_fid2), rr->rr_tgt);
1087
1088         rc = mdt_rename_lock(info, &rename_lh);
1089         if (rc) {
1090                 CERROR("Can't lock FS for rename, rc %d\n", rc);
1091                 RETURN(rc);
1092         }
1093
1094         lh_newp = &info->mti_lh[MDT_LH_NEW];
1095
1096         /* step 1: lock the source dir. */
1097         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1098         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1099                           rr->rr_namelen);
1100         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1101                                        MDS_INODELOCK_UPDATE);
1102         if (IS_ERR(msrcdir))
1103                 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1104
1105         if (mdt_object_obf(msrcdir))
1106                 GOTO(out_unlock_source, rc = -EPERM);
1107
1108         rc = mdt_version_get_check_save(info, msrcdir, 0);
1109         if (rc)
1110                 GOTO(out_unlock_source, rc);
1111
1112         /* step 2: find & lock the target dir. */
1113         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1114         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1115                           rr->rr_tgtlen);
1116         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1117                 mdt_object_get(info->mti_env, msrcdir);
1118                 mtgtdir = msrcdir;
1119                 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1120                          rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1121                                                  MDS_INODELOCK_UPDATE);
1122                          if (rc)
1123                                  GOTO(out_unlock_source, rc);
1124                          OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1125                 }
1126         } else {
1127                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1128                                           rr->rr_fid2);
1129                 if (IS_ERR(mtgtdir))
1130                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1131
1132                 if (mdt_object_obf(mtgtdir))
1133                         GOTO(out_put_target, rc = -EPERM);
1134
1135                 /* check early, the real version will be saved after locking */
1136                 rc = mdt_version_get_check(info, mtgtdir, 1);
1137                 if (rc)
1138                         GOTO(out_put_target, rc);
1139
1140                 if (unlikely(mdt_object_remote(mtgtdir))) {
1141                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1142                                "on different MDTs\n", PFID(rr->rr_fid1),
1143                                PFID(rr->rr_fid2));
1144                         GOTO(out_put_target, rc = -EXDEV);
1145                 } else {
1146                         if (likely(mdt_object_exists(mtgtdir))) {
1147                                 /* we lock the target dir if it is local */
1148                                 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1149                                                      MDS_INODELOCK_UPDATE,
1150                                                      MDT_LOCAL_LOCK);
1151                                 if (rc != 0)
1152                                         GOTO(out_put_target, rc);
1153                                 /* get and save correct version after locking */
1154                                 mdt_version_get_save(info, mtgtdir, 1);
1155                         } else {
1156                                 GOTO(out_put_target, rc = -ESTALE);
1157                         }
1158                 }
1159         }
1160
1161         /* step 3: find & lock the old object. */
1162         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1163         mdt_name_copy(&slname, lname);
1164         fid_zero(old_fid);
1165         rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1166         if (rc != 0)
1167                 GOTO(out_unlock_target, rc);
1168
1169         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1170                 GOTO(out_unlock_target, rc = -EINVAL);
1171
1172         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1173         if (IS_ERR(mold))
1174                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1175         if (mdt_object_remote(mold)) {
1176                 mdt_object_put(info->mti_env, mold);
1177                 CDEBUG(D_INFO, "Source child "DFID" is on another MDT\n",
1178                        PFID(old_fid));
1179                 GOTO(out_unlock_target, rc = -EXDEV);
1180         }
1181
1182         if (mdt_object_obf(mold)) {
1183                 mdt_object_put(info->mti_env, mold);
1184                 GOTO(out_unlock_target, rc = -EPERM);
1185         }
1186
1187         lh_oldp = &info->mti_lh[MDT_LH_OLD];
1188         mdt_lock_reg_init(lh_oldp, LCK_EX);
1189         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1190                              MDT_CROSS_LOCK);
1191         if (rc != 0) {
1192                 mdt_object_put(info->mti_env, mold);
1193                 GOTO(out_unlock_target, rc);
1194         }
1195
1196         info->mti_mos = mold;
1197         /* save version after locking */
1198         mdt_version_get_save(info, mold, 2);
1199         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1200
1201         /* step 4: find & lock the new object. */
1202         /* new target object may not exist now */
1203         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1204         /* lookup with version checking */
1205         fid_zero(new_fid);
1206         rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1207         if (rc == 0) {
1208                 /* the new_fid should have been filled at this moment */
1209                 if (lu_fid_eq(old_fid, new_fid))
1210                        GOTO(out_unlock_old, rc);
1211
1212                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1213                     lu_fid_eq(new_fid, rr->rr_fid2))
1214                         GOTO(out_unlock_old, rc = -EINVAL);
1215
1216                 mdt_lock_reg_init(lh_newp, LCK_EX);
1217                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1218                 if (IS_ERR(mnew))
1219                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1220
1221                 if (mdt_object_obf(mnew)) {
1222                         mdt_object_put(info->mti_env, mnew);
1223                         GOTO(out_unlock_old, rc = -EPERM);
1224                 }
1225
1226                 if (mdt_object_remote(mnew)) {
1227                         mdt_object_put(info->mti_env, mnew);
1228                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1229                                PFID(new_fid));
1230                         GOTO(out_unlock_old, rc = -EXDEV);
1231                 }
1232
1233                 rc = mdt_object_lock(info, mnew, lh_newp,
1234                                      MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1235                 if (rc != 0) {
1236                         mdt_object_put(info->mti_env, mnew);
1237                         GOTO(out_unlock_old, rc);
1238                 }
1239                 /* get and save version after locking */
1240                 mdt_version_get_save(info, mnew, 3);
1241                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1242         } else if (rc != -EREMOTE && rc != -ENOENT) {
1243                 GOTO(out_unlock_old, rc);
1244         } else {
1245                 mdt_enoent_version_save(info, 3);
1246         }
1247
1248         /* step 5: rename it */
1249         mdt_reint_init_ma(info, ma);
1250
1251         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1252                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1253
1254
1255         /* Check if @dst is subdir of @src. */
1256         rc = mdt_rename_sanity(info, old_fid);
1257         if (rc)
1258                 GOTO(out_unlock_new, rc);
1259
1260         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1261                         mdt_object_child(mtgtdir), old_fid, &slname,
1262                         (mnew ? mdt_object_child(mnew) : NULL),
1263                         lname, ma);
1264
1265         /* handle last link of tgt object */
1266         if (rc == 0) {
1267                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1268                 if (mnew)
1269                         mdt_handle_last_unlink(info, mnew, ma);
1270
1271                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1272                                          msrcdir, mtgtdir);
1273         }
1274
1275         EXIT;
1276 out_unlock_new:
1277         if (mnew)
1278                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1279 out_unlock_old:
1280         mdt_object_unlock_put(info, mold, lh_oldp, rc);
1281 out_unlock_target:
1282         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1283 out_put_target:
1284         mdt_object_put(info->mti_env, mtgtdir);
1285 out_unlock_source:
1286         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1287 out_rename_lock:
1288         if (lustre_handle_is_used(&rename_lh))
1289                 mdt_rename_unlock(&rename_lh);
1290         return rc;
1291 }
1292
1293 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1294                            struct mdt_lock_handle *lhc);
1295
1296 static mdt_reinter reinters[REINT_MAX] = {
1297         [REINT_SETATTR]  = mdt_reint_setattr,
1298         [REINT_CREATE]   = mdt_reint_create,
1299         [REINT_LINK]     = mdt_reint_link,
1300         [REINT_UNLINK]   = mdt_reint_unlink,
1301         [REINT_RENAME]   = mdt_reint_rename,
1302         [REINT_OPEN]     = mdt_reint_open,
1303         [REINT_SETXATTR] = mdt_reint_setxattr,
1304         [REINT_RMENTRY]  = mdt_reint_unlink
1305 };
1306
1307 int mdt_reint_rec(struct mdt_thread_info *info,
1308                   struct mdt_lock_handle *lhc)
1309 {
1310         int rc;
1311         ENTRY;
1312
1313         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1314
1315         RETURN(rc);
1316 }