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