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