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