Whamcloud - gitweb
LU-4371 gss: Generalize testing of GSSAPI code
[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_md_operative(rr->rr_fid1))
273                 RETURN(-EPERM);
274
275         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
276
277         parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
278         if (IS_ERR(parent))
279                 RETURN(PTR_ERR(parent));
280
281         if (!mdt_object_exists(parent))
282                 GOTO(put_parent, rc = -ENOENT);
283
284         lh = &info->mti_lh[MDT_LH_PARENT];
285         if (mdt_object_remote(parent)) {
286                 mdt_lock_reg_init(lh, LCK_EX);
287                 rc = mdt_remote_object_lock(info, parent, &lh->mlh_rreg_lh,
288                                             lh->mlh_rreg_mode,
289                                             MDS_INODELOCK_UPDATE);
290                 if (rc != ELDLM_OK)
291                         GOTO(put_parent, rc);
292
293         } else {
294                 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
295                 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE,
296                                      MDT_LOCAL_LOCK);
297                 if (rc)
298                         GOTO(put_parent, rc);
299
300                 rc = mdt_version_get_check_save(info, parent, 0);
301                 if (rc)
302                         GOTO(unlock_parent, rc);
303         }
304
305         /*
306          * Check child name version during replay.
307          * During create replay a file may exist with same name.
308          */
309         rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
310                                       &info->mti_tmp_fid1, 1);
311         if (rc == 0)
312                 GOTO(unlock_parent, rc = -EEXIST);
313
314         /* -ENOENT is expected here */
315         if (rc != -ENOENT)
316                 GOTO(unlock_parent, rc);
317
318         /* save version of file name for replay, it must be ENOENT here */
319         mdt_enoent_version_save(info, 1);
320
321         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
322         if (likely(!IS_ERR(child))) {
323                 struct md_object *next = mdt_object_child(parent);
324
325                 if (mdt_object_remote(child)) {
326                         struct seq_server_site *ss;
327                         struct lu_ucred *uc  = mdt_ucred(info);
328
329                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
330                                 if (uc->uc_gid !=
331                                     mdt->mdt_enable_remote_dir_gid &&
332                                     mdt->mdt_enable_remote_dir_gid != -1) {
333                                         CERROR("%s: Creating remote dir is only"
334                                                " permitted for administrator or"
335                                                " set mdt_enable_remote_dir_gid:"
336                                                " rc = %d\n",
337                                                 mdt_obd_name(mdt), -EPERM);
338                                         GOTO(out_put_child, rc = -EPERM);
339                                 }
340                         }
341
342                         ss = mdt_seq_site(mdt);
343                         if (ss->ss_node_id != 0 &&
344                             mdt->mdt_enable_remote_dir == 0) {
345                                 CERROR("%s: remote dir is only permitted on"
346                                        " MDT0 or set_param"
347                                        " mdt.*.enable_remote_dir=1\n",
348                                        mdt_obd_name(mdt));
349                                 GOTO(out_put_child, rc = -EPERM);
350                         }
351                         if (!mdt_is_dne_client(mdt_info_req(info)->rq_export)) {
352                                 /* Return -EIO for old client */
353                                 GOTO(out_put_child, rc = -EIO);
354                         }
355
356                 }
357                 ma->ma_need = MA_INODE;
358                 ma->ma_valid = 0;
359                 /* capa for cross-ref will be stored here */
360                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
361                                                      &RMF_CAPA1);
362                 LASSERT(ma->ma_capa);
363
364                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
365                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
366
367                 /* Version of child will be updated on disk. */
368                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
369                 rc = mdt_version_get_check_save(info, child, 2);
370                 if (rc)
371                         GOTO(out_put_child, rc);
372
373                 /* Let lower layer know current lock mode. */
374                 info->mti_spec.sp_cr_mode =
375                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
376
377                 /*
378                  * Do not perform lookup sanity check. We know that name does
379                  * not exist.
380                  */
381                 info->mti_spec.sp_cr_lookup = 0;
382                 info->mti_spec.sp_feat = &dt_directory_features;
383
384                 rc = mdo_create(info->mti_env, next, &rr->rr_name,
385                                 mdt_object_child(child), &info->mti_spec, ma);
386                 if (rc == 0)
387                         rc = mdt_attr_get_complex(info, child, ma);
388
389                 if (rc == 0) {
390                         /* Return fid & attr to client. */
391                         if (ma->ma_valid & MA_INODE)
392                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
393                                                    mdt_object_fid(child));
394                 }
395 out_put_child:
396                 mdt_object_put(info->mti_env, child);
397         } else {
398                 rc = PTR_ERR(child);
399         }
400         mdt_create_pack_capa(info, rc, child, repbody);
401 unlock_parent:
402         mdt_object_unlock(info, parent, lh, rc);
403 put_parent:
404         mdt_object_put(info->mti_env, parent);
405         RETURN(rc);
406 }
407
408 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
409                              struct mdt_object *obj, __u64 ibits,
410                              struct ldlm_enqueue_info *einfo)
411 {
412         ldlm_policy_data_t      *policy = &mti->mti_policy;
413         int                     rc;
414         ENTRY;
415
416         if (!S_ISDIR(obj->mot_header.loh_attr))
417                 RETURN(0);
418
419         memset(policy, 0, sizeof(*policy));
420         policy->l_inodebits.bits = ibits;
421
422         rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
423                               policy);
424         RETURN(rc);
425 }
426
427 /**
428  * Lock slave stripes if necessary, the lock handles of slave stripes
429  * will be stored in einfo->ei_cbdata.
430  **/
431 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
432                            ldlm_mode_t mode, __u64 ibits,
433                            struct ldlm_enqueue_info *einfo)
434 {
435         ldlm_policy_data_t      *policy = &mti->mti_policy;
436         int                     rc;
437         ENTRY;
438
439         if (!S_ISDIR(obj->mot_header.loh_attr))
440                 RETURN(0);
441
442         memset(einfo, 0, sizeof(*einfo));
443         einfo->ei_type = LDLM_IBITS;
444         einfo->ei_mode = mode;
445         einfo->ei_cb_bl = mdt_remote_blocking_ast;
446         einfo->ei_cb_cp = ldlm_completion_ast;
447         einfo->ei_enq_slave = 1;
448         memset(policy, 0, sizeof(*policy));
449         policy->l_inodebits.bits = ibits;
450
451         rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
452                             policy);
453         RETURN(rc);
454 }
455
456 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
457                  struct md_attr *ma, int flags)
458 {
459         struct mdt_lock_handle  *lh;
460         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
461         __u64 lockpart = MDS_INODELOCK_UPDATE;
462         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
463         int rc;
464         ENTRY;
465
466         /* attr shouldn't be set on remote object */
467         LASSERT(!mdt_object_remote(mo));
468
469         lh = &info->mti_lh[MDT_LH_PARENT];
470         mdt_lock_reg_init(lh, LCK_PW);
471
472         /* Even though the new MDT will grant PERM lock to the old
473          * client, but the old client will almost ignore that during
474          * So it needs to revoke both LOOKUP and PERM lock here, so
475          * both new and old client can cancel the dcache */
476         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
477                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
478
479         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
480         if (rc != 0)
481                 RETURN(rc);
482
483         rc = mdt_lock_slaves(info, mo, LCK_EX, lockpart, einfo);
484         if (rc != 0)
485                 GOTO(out_unlock, rc);
486
487         if (mdt_object_exists(mo) == 0)
488                 GOTO(out_unlock, rc = -ENOENT);
489
490         /* all attrs are packed into mti_attr in unpack_setattr */
491         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
492                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
493
494         /* This is only for set ctime when rename's source is on remote MDS. */
495         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
496                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
497
498         /* VBR: update version if attr changed are important for recovery */
499         if (do_vbr) {
500                 /* update on-disk version of changed object */
501                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
502                 rc = mdt_version_get_check_save(info, mo, 0);
503                 if (rc)
504                         GOTO(out_unlock, rc);
505         }
506
507         /* Ensure constant striping during chown(). See LU-2789. */
508         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
509                 mutex_lock(&mo->mot_lov_mutex);
510
511         /* all attrs are packed into mti_attr in unpack_setattr */
512         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
513
514         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
515                 mutex_unlock(&mo->mot_lov_mutex);
516
517         if (rc != 0)
518                 GOTO(out_unlock, rc);
519
520         EXIT;
521 out_unlock:
522         mdt_unlock_slaves(info, mo, lockpart, einfo);
523         mdt_object_unlock(info, mo, lh, rc);
524         return rc;
525 }
526
527 /**
528  * Check HSM flags and add HS_DIRTY flag if relevant.
529  *
530  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
531  * and is not RELEASED.
532  */
533 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
534                         struct md_attr *ma)
535 {
536         int rc;
537         ENTRY;
538
539         /* If the file was modified, add the dirty flag */
540         ma->ma_need = MA_HSM;
541         rc = mdt_attr_get_complex(info, mo, ma);
542         if (rc) {
543                 CERROR("file attribute read error for "DFID": %d.\n",
544                         PFID(mdt_object_fid(mo)), rc);
545                 RETURN(rc);
546         }
547
548         /* If an up2date copy exists in the backend, add dirty flag */
549         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
550             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
551                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
552
553                 ma->ma_hsm.mh_flags |= HS_DIRTY;
554
555                 mdt_lock_reg_init(lh, LCK_PW);
556                 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
557                                      MDT_LOCAL_LOCK);
558                 if (rc != 0)
559                         RETURN(rc);
560
561                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
562                 if (rc)
563                         CERROR("file attribute change error for "DFID": %d\n",
564                                 PFID(mdt_object_fid(mo)), rc);
565                 mdt_object_unlock(info, mo, lh, rc);
566         }
567
568         RETURN(rc);
569 }
570
571 static int mdt_reint_setattr(struct mdt_thread_info *info,
572                              struct mdt_lock_handle *lhc)
573 {
574         struct md_attr          *ma = &info->mti_attr;
575         struct mdt_reint_record *rr = &info->mti_rr;
576         struct ptlrpc_request   *req = mdt_info_req(info);
577         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
578         struct mdt_file_data    *mfd;
579         struct mdt_object       *mo;
580         struct mdt_body         *repbody;
581         int                      som_au, rc, rc2;
582         ENTRY;
583
584         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
585                   (unsigned int)ma->ma_attr.la_valid);
586
587         if (info->mti_dlm_req)
588                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
589
590         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
591         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
592         if (IS_ERR(mo))
593                 GOTO(out, rc = PTR_ERR(mo));
594
595         /* start a log jounal handle if needed */
596         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
597                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
598                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
599                         /* Check write access for the O_TRUNC case */
600                         if (mdt_write_read(mo) < 0)
601                                 GOTO(out_put, rc = -ETXTBSY);
602                 }
603         } else if (info->mti_ioepoch &&
604                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
605                 /* Truncate case. IOEpoch is opened. */
606                 rc = mdt_write_get(mo);
607                 if (rc)
608                         GOTO(out_put, rc);
609
610                 mfd = mdt_mfd_new(med);
611                 if (mfd == NULL) {
612                         mdt_write_put(mo);
613                         GOTO(out_put, rc = -ENOMEM);
614                 }
615
616                 mdt_ioepoch_open(info, mo, 0);
617                 repbody->ioepoch = mo->mot_ioepoch;
618
619                 mdt_object_get(info->mti_env, mo);
620                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
621                 mfd->mfd_object = mo;
622                 mfd->mfd_xid = req->rq_xid;
623
624                 spin_lock(&med->med_open_lock);
625                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
626                 spin_unlock(&med->med_open_lock);
627                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
628         }
629
630         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
631         if (som_au) {
632                 /* SOM Attribute update case. Find the proper mfd and update
633                  * SOM attributes on the proper object. */
634                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
635                 LASSERT(info->mti_ioepoch);
636
637                 spin_lock(&med->med_open_lock);
638                 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
639                                      req_is_replay(req));
640                 if (mfd == NULL) {
641                         spin_unlock(&med->med_open_lock);
642                         CDEBUG(D_INODE, "no handle for file close: "
643                                "fid = "DFID": cookie = "LPX64"\n",
644                                PFID(info->mti_rr.rr_fid1),
645                                info->mti_ioepoch->handle.cookie);
646                         GOTO(out_put, rc = -ESTALE);
647                 }
648                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
649                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
650
651                 class_handle_unhash(&mfd->mfd_handle);
652                 cfs_list_del_init(&mfd->mfd_list);
653                 spin_unlock(&med->med_open_lock);
654
655                 mdt_mfd_close(info, mfd);
656         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
657                 LASSERT((ma->ma_valid & MA_LOV) == 0);
658                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
659                 if (rc)
660                         GOTO(out_put, rc);
661         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
662                 struct lu_buf *buf  = &info->mti_buf;
663                 LASSERT(ma->ma_attr.la_valid == 0);
664                 buf->lb_buf = ma->ma_lmm;
665                 buf->lb_len = ma->ma_lmm_size;
666                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
667                                   buf, XATTR_NAME_LOV, 0);
668                 if (rc)
669                         GOTO(out_put, rc);
670         } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
671                 struct lu_buf *buf  = &info->mti_buf;
672
673                 LASSERT(ma->ma_attr.la_valid == 0);
674                 buf->lb_buf = ma->ma_lmv;
675                 buf->lb_len = ma->ma_lmv_size;
676                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
677                                   buf, XATTR_NAME_DEFAULT_LMV, 0);
678                 if (rc)
679                         GOTO(out_put, rc);
680         } else
681                 LBUG();
682
683         /* If file data is modified, add the dirty flag */
684         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
685                 rc = mdt_add_dirty_flag(info, mo, ma);
686
687         ma->ma_need = MA_INODE;
688         ma->ma_valid = 0;
689         rc = mdt_attr_get_complex(info, mo, ma);
690         if (rc != 0)
691                 GOTO(out_put, rc);
692
693         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
694
695         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
696             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
697             S_ISREG(lu_object_attr(&mo->mot_obj)) &&
698             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
699                 struct lustre_capa *capa;
700
701                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
702                 LASSERT(capa);
703                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
704                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
705                 if (rc)
706                         GOTO(out_put, rc);
707                 repbody->valid |= OBD_MD_FLOSSCAPA;
708         }
709
710         EXIT;
711 out_put:
712         mdt_object_put(info->mti_env, mo);
713 out:
714         if (rc == 0)
715                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
716
717         mdt_client_compatibility(info);
718         rc2 = mdt_fix_reply(info);
719         if (rc == 0)
720                 rc = rc2;
721         return rc;
722 }
723
724 static int mdt_reint_create(struct mdt_thread_info *info,
725                             struct mdt_lock_handle *lhc)
726 {
727         struct ptlrpc_request   *req = mdt_info_req(info);
728         int                     rc;
729         ENTRY;
730
731         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
732                 RETURN(err_serious(-ESTALE));
733
734         if (info->mti_dlm_req)
735                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
736
737         if (!lu_name_is_valid(&info->mti_rr.rr_name))
738                 RETURN(-EPROTO);
739
740         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
741         case S_IFDIR:
742                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
743                 break;
744         case S_IFREG:
745         case S_IFLNK:
746         case S_IFCHR:
747         case S_IFBLK:
748         case S_IFIFO:
749         case S_IFSOCK:
750                 /* Special file should stay on the same node as parent. */
751                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
752                 break;
753         default:
754                 CERROR("%s: Unsupported mode %o\n",
755                        mdt_obd_name(info->mti_mdt),
756                        info->mti_attr.ma_attr.la_mode);
757                 RETURN(err_serious(-EOPNOTSUPP));
758         }
759
760         rc = mdt_md_create(info);
761         RETURN(rc);
762 }
763
764 /*
765  * VBR: save parent version in reply and child version getting by its name.
766  * Version of child is getting and checking during its lookup. If
767  */
768 static int mdt_reint_unlink(struct mdt_thread_info *info,
769                             struct mdt_lock_handle *lhc)
770 {
771         struct mdt_reint_record *rr = &info->mti_rr;
772         struct ptlrpc_request   *req = mdt_info_req(info);
773         struct md_attr          *ma = &info->mti_attr;
774         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
775         struct mdt_object       *mp;
776         struct mdt_object       *mc;
777         struct mdt_lock_handle  *parent_lh;
778         struct mdt_lock_handle  *child_lh;
779         struct ldlm_enqueue_info *einfo = &info->mti_einfo;
780         int                     rc;
781         int                     no_name = 0;
782         ENTRY;
783
784         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
785                   PNAME(&rr->rr_name));
786
787         if (info->mti_dlm_req)
788                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
789
790         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
791                 RETURN(err_serious(-ENOENT));
792
793         if (!fid_is_md_operative(rr->rr_fid1))
794                 RETURN(-EPERM);
795
796         /*
797          * step 1: Found the parent.
798          */
799         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
800         if (IS_ERR(mp)) {
801                 rc = PTR_ERR(mp);
802                 GOTO(out, rc);
803         }
804
805         parent_lh = &info->mti_lh[MDT_LH_PARENT];
806
807         if (mdt_object_remote(mp)) {
808                 mdt_lock_reg_init(parent_lh, LCK_EX);
809                 rc = mdt_remote_object_lock(info, mp, &parent_lh->mlh_rreg_lh,
810                                             parent_lh->mlh_rreg_mode,
811                                             MDS_INODELOCK_UPDATE);
812                 if (rc != ELDLM_OK)
813                         GOTO(put_parent, rc);
814
815         } else {
816                 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
817                 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
818                                      MDT_LOCAL_LOCK);
819                 if (rc)
820                         GOTO(put_parent, rc);
821
822                 rc = mdt_version_get_check_save(info, mp, 0);
823                 if (rc)
824                         GOTO(unlock_parent, rc);
825         }
826
827         /* step 2: find & lock the child */
828         /* lookup child object along with version checking */
829         fid_zero(child_fid);
830         rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
831         if (rc != 0) {
832                 /* Name might not be able to find during resend of
833                  * remote unlink, considering following case.
834                  * dir_A is a remote directory, the name entry of
835                  * dir_A is on MDT0, the directory is on MDT1,
836                  *
837                  * 1. client sends unlink req to MDT1.
838                  * 2. MDT1 sends name delete update to MDT0.
839                  * 3. name entry is being deleted in MDT0 synchronously.
840                  * 4. MDT1 is restarted.
841                  * 5. client resends unlink req to MDT1. So it can not
842                  *    find the name entry on MDT0 anymore.
843                  * In this case, MDT1 only needs to destory the local
844                  * directory.
845                  * */
846                 if (mdt_object_remote(mp) && rc == -ENOENT &&
847                     !fid_is_zero(rr->rr_fid2) &&
848                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
849                         no_name = 1;
850                         *child_fid = *rr->rr_fid2;
851                  } else {
852                         GOTO(unlock_parent, rc);
853                  }
854         }
855
856         if (!fid_is_md_operative(child_fid))
857                 GOTO(unlock_parent, rc = -EPERM);
858
859         /* We will lock the child regardless it is local or remote. No harm. */
860         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
861         if (IS_ERR(mc))
862                 GOTO(unlock_parent, rc = PTR_ERR(mc));
863
864         child_lh = &info->mti_lh[MDT_LH_CHILD];
865         mdt_lock_reg_init(child_lh, LCK_EX);
866         if (mdt_object_remote(mc)) {
867                 struct mdt_body  *repbody;
868
869                 if (!fid_is_zero(rr->rr_fid2)) {
870                         CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
871                                mdt_obd_name(info->mti_mdt),
872                                PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
873                         GOTO(put_child, rc = -ENOENT);
874                 }
875                 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
876                        mdt_obd_name(info->mti_mdt),
877                        PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
878
879                 if (!mdt_is_dne_client(req->rq_export))
880                         /* Return -EIO for old client */
881                         GOTO(put_child, rc = -EIO);
882
883                 if (info->mti_spec.sp_rm_entry) {
884                         struct lu_ucred *uc  = mdt_ucred(info);
885
886                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
887                                 CERROR("%s: unlink remote entry is only "
888                                        "permitted for administrator: rc = %d\n",
889                                         mdt_obd_name(info->mti_mdt),
890                                         -EPERM);
891                                 GOTO(put_child, rc = -EPERM);
892                         }
893
894                         ma->ma_need = MA_INODE;
895                         ma->ma_valid = 0;
896                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
897                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
898                                         NULL, &rr->rr_name, ma, no_name);
899                         GOTO(put_child, rc);
900                 }
901                 /* Revoke the LOOKUP lock of the remote object granted by
902                  * this MDT. Since the unlink will happen on another MDT,
903                  * it will release the LOOKUP lock right away. Then What
904                  * would happen if another client try to grab the LOOKUP
905                  * lock at the same time with unlink XXX */
906                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
907                                 MDT_CROSS_LOCK);
908                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
909                 LASSERT(repbody != NULL);
910                 repbody->fid1 = *mdt_object_fid(mc);
911                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
912                 GOTO(unlock_child, rc = -EREMOTE);
913         } else if (info->mti_spec.sp_rm_entry) {
914                 rc = -EPERM;
915                 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
916                        "rc = %d\n",
917                        mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
918                 GOTO(put_child, rc);
919         }
920
921         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
922          * this now because a running HSM restore on the child (unlink
923          * victim) will hold the layout lock. See LU-4002. */
924         rc = mdt_object_lock(info, mc, child_lh,
925                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
926                              MDT_CROSS_LOCK);
927         if (rc != 0)
928                 GOTO(put_child, rc);
929         /*
930          * Now we can only make sure we need MA_INODE, in mdd layer, will check
931          * whether need MA_LOV and MA_COOKIE.
932          */
933         ma->ma_need = MA_INODE;
934         ma->ma_valid = 0;
935
936         rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, einfo);
937         if (rc != 0)
938                 GOTO(unlock_child, rc);
939
940         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
941                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
942         /* save version when object is locked */
943         mdt_version_get_save(info, mc, 1);
944
945         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
946
947         mutex_lock(&mc->mot_lov_mutex);
948
949         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
950                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
951
952         mutex_unlock(&mc->mot_lov_mutex);
953
954         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
955                 rc = mdt_attr_get_complex(info, mc, ma);
956         if (rc == 0)
957                 mdt_handle_last_unlink(info, mc, ma);
958
959         if (ma->ma_valid & MA_INODE) {
960                 switch (ma->ma_attr.la_mode & S_IFMT) {
961                 case S_IFDIR:
962                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
963                         break;
964                 case S_IFREG:
965                 case S_IFLNK:
966                 case S_IFCHR:
967                 case S_IFBLK:
968                 case S_IFIFO:
969                 case S_IFSOCK:
970                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
971                         break;
972                 default:
973                         LASSERTF(0, "bad file type %o unlinking\n",
974                                  ma->ma_attr.la_mode);
975                 }
976         }
977
978         EXIT;
979
980 unlock_child:
981         mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, einfo);
982         mdt_object_unlock(info, mc, child_lh, rc);
983
984         /* Since we do not need reply md striped dir info to client, so
985          * reset mti_big_lmm_used to avoid confusing mdt_fix_reply */
986         if (info->mti_big_lmm_used)
987                 info->mti_big_lmm_used = 0;
988 put_child:
989         mdt_object_put(info->mti_env, mc);
990 unlock_parent:
991         mdt_object_unlock(info, mp, parent_lh, rc);
992 put_parent:
993         mdt_object_put(info->mti_env, mp);
994 out:
995         return rc;
996 }
997
998 /*
999  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1000  * name.
1001  */
1002 static int mdt_reint_link(struct mdt_thread_info *info,
1003                           struct mdt_lock_handle *lhc)
1004 {
1005         struct mdt_reint_record *rr = &info->mti_rr;
1006         struct ptlrpc_request   *req = mdt_info_req(info);
1007         struct md_attr          *ma = &info->mti_attr;
1008         struct mdt_object       *ms;
1009         struct mdt_object       *mp;
1010         struct mdt_lock_handle  *lhs;
1011         struct mdt_lock_handle  *lhp;
1012         int rc;
1013         ENTRY;
1014
1015         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1016                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1017
1018         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1019                 RETURN(err_serious(-ENOENT));
1020
1021         if (info->mti_dlm_req)
1022                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1023
1024         /* Invalid case so return error immediately instead of
1025          * processing it */
1026         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1027                 RETURN(-EPERM);
1028
1029         if (!fid_is_md_operative(rr->rr_fid1) ||
1030             !fid_is_md_operative(rr->rr_fid2))
1031                 RETURN(-EPERM);
1032
1033         /* step 1: find & lock the target parent dir */
1034         lhp = &info->mti_lh[MDT_LH_PARENT];
1035         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1036         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1037                                   MDS_INODELOCK_UPDATE);
1038         if (IS_ERR(mp))
1039                 RETURN(PTR_ERR(mp));
1040
1041         rc = mdt_version_get_check_save(info, mp, 0);
1042         if (rc)
1043                 GOTO(out_unlock_parent, rc);
1044
1045         /* step 2: find & lock the source */
1046         lhs = &info->mti_lh[MDT_LH_CHILD];
1047         mdt_lock_reg_init(lhs, LCK_EX);
1048
1049         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1050         if (IS_ERR(ms))
1051                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1052
1053         if (!mdt_object_exists(ms)) {
1054                 mdt_object_put(info->mti_env, ms);
1055                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1056                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1057                 GOTO(out_unlock_parent, rc = -ENOENT);
1058         }
1059
1060         if (mdt_object_remote(ms)) {
1061                 mdt_object_put(info->mti_env, ms);
1062                 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1063                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1064                        PFID(rr->rr_fid2));
1065                 GOTO(out_unlock_parent, rc = -EXDEV);
1066         }
1067
1068         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1069                              MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1070         if (rc != 0) {
1071                 mdt_object_put(info->mti_env, ms);
1072                 GOTO(out_unlock_parent, rc);
1073         }
1074
1075         /* step 3: link it */
1076         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1077                        OBD_FAIL_MDS_REINT_LINK_WRITE);
1078
1079         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1080         rc = mdt_version_get_check_save(info, ms, 1);
1081         if (rc)
1082                 GOTO(out_unlock_child, rc);
1083
1084         /** check target version by name during replay */
1085         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1086                                       &info->mti_tmp_fid1, 2);
1087         if (rc != 0 && rc != -ENOENT)
1088                 GOTO(out_unlock_child, rc);
1089         /* save version of file name for replay, it must be ENOENT here */
1090         if (!req_is_replay(mdt_info_req(info))) {
1091                 if (rc != -ENOENT) {
1092                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1093                                PNAME(&rr->rr_name));
1094                         GOTO(out_unlock_child, rc = -EEXIST);
1095                 }
1096                 info->mti_ver[2] = ENOENT_VERSION;
1097                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1098         }
1099
1100         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1101               mdt_object_child(ms), &rr->rr_name, ma);
1102
1103         if (rc == 0)
1104                 mdt_counter_incr(req, LPROC_MDT_LINK);
1105
1106         EXIT;
1107 out_unlock_child:
1108         mdt_object_unlock_put(info, ms, lhs, rc);
1109 out_unlock_parent:
1110         mdt_object_unlock_put(info, mp, lhp, rc);
1111         return rc;
1112 }
1113 /**
1114  * lock the part of the directory according to the hash of the name
1115  * (lh->mlh_pdo_hash) in parallel directory lock.
1116  */
1117 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1118                               struct mdt_lock_handle *lh,
1119                               struct mdt_object *obj, __u64 ibits)
1120 {
1121         struct ldlm_res_id *res = &info->mti_res_id;
1122         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1123         ldlm_policy_data_t *policy = &info->mti_policy;
1124         int rc;
1125
1126         /*
1127          * Finish res_id initializing by name hash marking part of
1128          * directory which is taking modification.
1129          */
1130         LASSERT(lh->mlh_pdo_hash != 0);
1131         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1132         memset(policy, 0, sizeof(*policy));
1133         policy->l_inodebits.bits = ibits;
1134         /*
1135          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1136          * going to be sent to client. If it is - mdt_intent_policy() path will
1137          * fix it up and turn FL_LOCAL flag off.
1138          */
1139         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1140                           res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1141                           &info->mti_exp->exp_handle.h_cookie);
1142         return rc;
1143 }
1144
1145 static int mdt_rename_lock(struct mdt_thread_info *info,
1146                            struct lustre_handle *lh)
1147 {
1148         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
1149         ldlm_policy_data_t      *policy = &info->mti_policy;
1150         struct ldlm_res_id      *res_id = &info->mti_res_id;
1151         __u64                   flags = 0;
1152         int rc;
1153         ENTRY;
1154
1155         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1156
1157         memset(policy, 0, sizeof *policy);
1158         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1159 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 53, 0)
1160         /* In phase I, we will not do cross-rename, so local BFL lock would
1161          * be enough
1162          */
1163         flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1164         /*
1165          * Current node is controller, that is mdt0, where we should
1166          * take BFL lock.
1167          */
1168         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1169                                     LCK_EX, &flags, ldlm_blocking_ast,
1170                                     ldlm_completion_ast, NULL, NULL, 0,
1171                                     LVB_T_NONE,
1172                                     &info->mti_exp->exp_handle.h_cookie,
1173                                     lh);
1174 #else
1175 #warning "Local rename lock is invalid for DNE phase II."
1176 #endif
1177         RETURN(rc);
1178 }
1179
1180 static void mdt_rename_unlock(struct lustre_handle *lh)
1181 {
1182         ENTRY;
1183         LASSERT(lustre_handle_is_used(lh));
1184         ldlm_lock_decref(lh, LCK_EX);
1185         EXIT;
1186 }
1187
1188 /*
1189  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1190  * target. Source should not be ancestor of target dir. May be other rename
1191  * checks can be moved here later.
1192  */
1193 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1194 {
1195         struct mdt_reint_record *rr = &info->mti_rr;
1196         struct lu_fid dst_fid = *rr->rr_fid2;
1197         struct mdt_object *dst;
1198         int rc = 0;
1199         ENTRY;
1200
1201         /* If the source and target are in the same directory, they can not
1202          * be parent/child relationship, so subdir check is not needed */
1203         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1204                 return 0;
1205
1206         do {
1207                 LASSERT(fid_is_sane(&dst_fid));
1208                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1209                 if (!IS_ERR(dst)) {
1210                         rc = mdo_is_subdir(info->mti_env,
1211                                            mdt_object_child(dst), fid,
1212                                            &dst_fid);
1213                         mdt_object_put(info->mti_env, dst);
1214                         if (rc != -EREMOTE && rc < 0) {
1215                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1216                         } else {
1217                                 /* check the found fid */
1218                                 if (lu_fid_eq(&dst_fid, fid))
1219                                         rc = -EINVAL;
1220                         }
1221                 } else {
1222                         rc = PTR_ERR(dst);
1223                 }
1224         } while (rc == -EREMOTE);
1225
1226         RETURN(rc);
1227 }
1228
1229 /* Update object linkEA */
1230 struct mdt_lock_list {
1231         struct mdt_object       *mll_obj;
1232         struct mdt_lock_handle  mll_lh;
1233         struct list_head        mll_list;
1234 };
1235
1236 static void mdt_unlock_list(struct mdt_thread_info *info,
1237                             struct list_head *list, int rc)
1238 {
1239         struct mdt_lock_list *mll;
1240         struct mdt_lock_list *mll2;
1241
1242         list_for_each_entry_safe(mll, mll2, list, mll_list) {
1243                 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1244                 list_del(&mll->mll_list);
1245                 OBD_FREE_PTR(mll);
1246         }
1247 }
1248
1249 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1250                                       struct mdt_object *obj,
1251                                       struct mdt_object *pobj,
1252                                       struct list_head *lock_list)
1253 {
1254         struct lu_buf           *buf = &info->mti_big_buf;
1255         struct linkea_data      ldata = { 0 };
1256         int                     count;
1257         int                     rc;
1258         ENTRY;
1259
1260         if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1261                 RETURN(0);
1262
1263         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1264         if (buf->lb_buf == NULL)
1265                 RETURN(-ENOMEM);
1266
1267         ldata.ld_buf = buf;
1268         rc = mdt_links_read(info, obj, &ldata);
1269         if (rc != 0) {
1270                 if (rc == -ENOENT || rc == -ENODATA)
1271                         rc = 0;
1272                 RETURN(rc);
1273         }
1274
1275         LASSERT(ldata.ld_leh != NULL);
1276         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1277         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1278                 struct mdt_device *mdt = info->mti_mdt;
1279                 struct mdt_object *mdt_pobj;
1280                 struct mdt_lock_list *mll;
1281                 struct lu_name name;
1282                 struct lu_fid  fid;
1283
1284                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1285                                     &name, &fid);
1286                 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1287                                                          ldata.ld_reclen);
1288                 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1289                 if (IS_ERR(mdt_pobj)) {
1290                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1291                               mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1292                         continue;
1293                 }
1294
1295                 if (!mdt_object_exists(mdt_pobj)) {
1296                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1297                               mdt_obd_name(mdt), PFID(&fid));
1298                         mdt_object_put(info->mti_env, mdt_pobj);
1299                         continue;
1300                 }
1301
1302                 if (mdt_pobj == pobj) {
1303                         CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1304                                mdt_obd_name(mdt), PFID(&fid));
1305                         mdt_object_put(info->mti_env, mdt_pobj);
1306                         continue;
1307                 }
1308
1309                 OBD_ALLOC_PTR(mll);
1310                 if (mll == NULL) {
1311                         mdt_object_put(info->mti_env, mdt_pobj);
1312                         GOTO(out, rc = -ENOMEM);
1313                 }
1314
1315                 if (mdt_object_remote(mdt_pobj)) {
1316                         mdt_lock_reg_init(&mll->mll_lh, LCK_EX);
1317                         rc = mdt_remote_object_lock(info, mdt_pobj,
1318                                                     &mll->mll_lh.mlh_rreg_lh,
1319                                                     mll->mll_lh.mlh_rreg_mode,
1320                                                     MDS_INODELOCK_UPDATE);
1321                 } else {
1322                         mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1323                         rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1324                                              MDS_INODELOCK_UPDATE,
1325                                              MDT_LOCAL_LOCK);
1326                 }
1327                 if (rc != 0) {
1328                         CERROR("%s: cannot lock "DFID": rc =%d\n",
1329                                mdt_obd_name(mdt), PFID(&fid), rc);
1330                         mdt_object_put(info->mti_env, mdt_pobj);
1331                         OBD_FREE_PTR(mll);
1332                         GOTO(out, rc);
1333                 }
1334
1335                 CFS_INIT_LIST_HEAD(&mll->mll_list);
1336                 mll->mll_obj = mdt_pobj;
1337                 list_add_tail(&mll->mll_list, lock_list);
1338         }
1339 out:
1340         if (rc != 0)
1341                 mdt_unlock_list(info, lock_list, rc);
1342         RETURN(rc);
1343 }
1344
1345 /* migrate files from one MDT to another MDT */
1346 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1347                                       struct mdt_lock_handle *lhc)
1348 {
1349         struct mdt_reint_record *rr = &info->mti_rr;
1350         struct md_attr          *ma = &info->mti_attr;
1351         struct mdt_object       *msrcdir;
1352         struct mdt_object       *mold;
1353         struct mdt_object       *mnew = NULL;
1354         struct mdt_lock_handle  *lh_dirp;
1355         struct mdt_lock_handle  *lh_childp;
1356         struct mdt_lock_handle  *lh_tgtp = NULL;
1357         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1358         struct list_head        lock_list;
1359         int                     rc;
1360         ENTRY;
1361
1362         CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1363                PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1364         /* 1: lock the source dir. */
1365         msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1366         if (IS_ERR(msrcdir)) {
1367                 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1368                         mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1369                         (int)PTR_ERR(msrcdir));
1370                 RETURN(PTR_ERR(msrcdir));
1371         }
1372
1373         lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1374         if (mdt_object_remote(msrcdir)) {
1375                 mdt_lock_reg_init(lh_dirp, LCK_EX);
1376                 rc = mdt_remote_object_lock(info, msrcdir,
1377                                             &lh_dirp->mlh_rreg_lh,
1378                                             lh_dirp->mlh_rreg_mode,
1379                                             MDS_INODELOCK_UPDATE);
1380                 if (rc != ELDLM_OK)
1381                         GOTO(out_put_parent, rc);
1382         } else {
1383                 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1384                 rc = mdt_object_lock(info, msrcdir, lh_dirp,
1385                                      MDS_INODELOCK_UPDATE,
1386                                      MDT_LOCAL_LOCK);
1387                 if (rc)
1388                         GOTO(out_put_parent, rc);
1389
1390                 rc = mdt_version_get_check_save(info, msrcdir, 0);
1391                 if (rc)
1392                         GOTO(out_unlock_parent, rc);
1393         }
1394
1395         /* 2: sanity check and find the object to be migrated. */
1396         fid_zero(old_fid);
1397         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1398         if (rc != 0)
1399                 GOTO(out_unlock_parent, rc);
1400
1401         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1402                 GOTO(out_unlock_parent, rc = -EINVAL);
1403
1404         if (!fid_is_md_operative(old_fid))
1405                 GOTO(out_unlock_parent, rc = -EPERM);
1406
1407         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1408         if (IS_ERR(mold))
1409                 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1410
1411         if (mdt_object_remote(mold)) {
1412                 CERROR("%s: source "DFID" is on the remote MDT\n",
1413                        mdt_obd_name(info->mti_mdt), PFID(old_fid));
1414                 GOTO(out_put_child, rc = -EREMOTE);
1415         }
1416
1417         if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1418             !mdt_object_remote(msrcdir)) {
1419                 CERROR("%s: parent "DFID" is still on the same"
1420                        " MDT, which should be migrated first:"
1421                        " rc = %d\n", mdt_obd_name(info->mti_mdt),
1422                        PFID(mdt_object_fid(msrcdir)), -EPERM);
1423                 GOTO(out_put_child, rc = -EPERM);
1424         }
1425
1426         /* 3: iterate the linkea of the object and lock all of the objects */
1427         CFS_INIT_LIST_HEAD(&lock_list);
1428         rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1429         if (rc != 0)
1430                 GOTO(out_put_child, rc);
1431
1432         /* 4: lock of the object migrated object */
1433         lh_childp = &info->mti_lh[MDT_LH_OLD];
1434         mdt_lock_reg_init(lh_childp, LCK_EX);
1435         rc = mdt_object_lock(info, mold, lh_childp,
1436                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1437                              MDS_INODELOCK_LAYOUT, MDT_CROSS_LOCK);
1438         if (rc != 0)
1439                 GOTO(out_unlock_list, rc);
1440
1441         ma->ma_need = MA_LMV;
1442         ma->ma_valid = 0;
1443         ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1444         ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1445         rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1446         if (rc != 0)
1447                 GOTO(out_unlock_list, rc);
1448
1449         if ((ma->ma_valid & MA_LMV)) {
1450                 struct lmv_mds_md_v1 *lmm1;
1451
1452                 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1453                 lmm1 = &ma->ma_lmv->lmv_md_v1;
1454                 if (lmm1->lmv_magic != LMV_MAGIC_MIGRATE) {
1455                         CERROR("%s: can not migrate striped dir "DFID
1456                                ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1457                                PFID(mdt_object_fid(mold)), -EPERM);
1458                         GOTO(out_unlock_child, rc = -EPERM);
1459                 }
1460
1461                 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1462                         GOTO(out_unlock_child, rc = -EINVAL);
1463
1464                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1465                                        &lmm1->lmv_stripe_fids[1]);
1466                 if (IS_ERR(mnew))
1467                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1468
1469                 if (!mdt_object_remote(mnew)) {
1470                         CERROR("%s: "DFID" being migrated is on this MDT:"
1471                                " rc  = %d\n", mdt_obd_name(info->mti_mdt),
1472                                PFID(rr->rr_fid2), -EPERM);
1473                         GOTO(out_put_new, rc = -EPERM);
1474                 }
1475
1476                 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1477                 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1478                 rc = mdt_remote_object_lock(info, mnew,
1479                                             &lh_tgtp->mlh_rreg_lh,
1480                                             lh_tgtp->mlh_rreg_mode,
1481                                             MDS_INODELOCK_UPDATE);
1482                 if (rc != 0) {
1483                         lh_tgtp = NULL;
1484                         GOTO(out_put_new, rc);
1485                 }
1486         } else {
1487                 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1488                                        rr->rr_fid2);
1489                 if (IS_ERR(mnew))
1490                         GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1491                 if (!mdt_object_remote(mnew)) {
1492                         CERROR("%s: Migration "DFID" is on this MDT !\n",
1493                                mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid2));
1494                         GOTO(out_put_new, rc = -EXDEV);
1495                 }
1496         }
1497
1498         /* 5: migrate it */
1499         mdt_reint_init_ma(info, ma);
1500
1501         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1502                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1503
1504         rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1505                          old_fid, &rr->rr_name, mdt_object_child(mnew), ma);
1506         if (rc != 0)
1507                 GOTO(out_unlock_new, rc);
1508 out_unlock_new:
1509         if (lh_tgtp != NULL)
1510                 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1511 out_put_new:
1512         if (mnew)
1513                 mdt_object_put(info->mti_env, mnew);
1514 out_unlock_child:
1515         mdt_object_unlock(info, mold, lh_childp, rc);
1516 out_unlock_list:
1517         mdt_unlock_list(info, &lock_list, rc);
1518 out_put_child:
1519         mdt_object_put(info->mti_env, mold);
1520 out_unlock_parent:
1521         mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1522 out_put_parent:
1523         mdt_object_put(info->mti_env, msrcdir);
1524
1525         RETURN(rc);
1526 }
1527
1528 /*
1529  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1530  * 2 - src child; 3 - tgt child.
1531  * Update on disk version of src child.
1532  */
1533 /**
1534  * For DNE phase I, only these renames are allowed
1535  *      mv src_p/src_c tgt_p/tgt_c
1536  * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1537  * 2. src_p and tgt_p are same directory, and tgt_c does not
1538  *    exists. In this case, all of modification will happen
1539  *    in the MDT where ithesource parent is, only one remote
1540  *    update is needed, i.e. set c_time/m_time on the child.
1541  *    And tgt_c will be still in the same MDT as the original
1542  *    src_c.
1543  */
1544 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1545                                      struct mdt_lock_handle *lhc)
1546 {
1547         struct mdt_reint_record *rr = &info->mti_rr;
1548         struct md_attr          *ma = &info->mti_attr;
1549         struct ptlrpc_request   *req = mdt_info_req(info);
1550         struct mdt_object       *msrcdir;
1551         struct mdt_object       *mtgtdir;
1552         struct mdt_object       *mold;
1553         struct mdt_object       *mnew = NULL;
1554         struct mdt_lock_handle  *lh_srcdirp;
1555         struct mdt_lock_handle  *lh_tgtdirp;
1556         struct mdt_lock_handle  *lh_oldp = NULL;
1557         struct mdt_lock_handle  *lh_newp = NULL;
1558         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1559         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1560         int                      rc;
1561         ENTRY;
1562
1563         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1564                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1565                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1566
1567         /* step 1: lock the source dir. */
1568         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1569         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1570         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1571                                        MDS_INODELOCK_UPDATE);
1572         if (IS_ERR(msrcdir))
1573                 RETURN(PTR_ERR(msrcdir));
1574
1575         rc = mdt_version_get_check_save(info, msrcdir, 0);
1576         if (rc)
1577                 GOTO(out_unlock_source, rc);
1578
1579         /* step 2: find & lock the target dir. */
1580         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1581         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1582         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1583                 mdt_object_get(info->mti_env, msrcdir);
1584                 mtgtdir = msrcdir;
1585                 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1586                         rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1587                                          MDS_INODELOCK_UPDATE);
1588                         if (rc != 0)
1589                                 GOTO(out_unlock_source, rc);
1590                         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1591                 }
1592         } else {
1593                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1594                                           rr->rr_fid2);
1595                 if (IS_ERR(mtgtdir))
1596                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1597
1598                 /* check early, the real version will be saved after locking */
1599                 rc = mdt_version_get_check(info, mtgtdir, 1);
1600                 if (rc)
1601                         GOTO(out_put_target, rc);
1602
1603                 if (unlikely(mdt_object_remote(mtgtdir))) {
1604                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1605                                "on different MDTs\n", PFID(rr->rr_fid1),
1606                                PFID(rr->rr_fid2));
1607                         GOTO(out_put_target, rc = -EXDEV);
1608                 } else {
1609                         if (likely(mdt_object_exists(mtgtdir))) {
1610                                 /* we lock the target dir if it is local */
1611                                 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1612                                                      MDS_INODELOCK_UPDATE,
1613                                                      MDT_LOCAL_LOCK);
1614                                 if (rc != 0)
1615                                         GOTO(out_put_target, rc);
1616                                 /* get and save correct version after locking */
1617                                 mdt_version_get_save(info, mtgtdir, 1);
1618                         } else {
1619                                 GOTO(out_put_target, rc = -ESTALE);
1620                         }
1621                 }
1622         }
1623
1624         /* step 3: find & lock the old object. */
1625         fid_zero(old_fid);
1626         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1627         if (rc != 0)
1628                 GOTO(out_unlock_target, rc);
1629
1630         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1631                 GOTO(out_unlock_target, rc = -EINVAL);
1632
1633         if (!fid_is_md_operative(old_fid))
1634                 GOTO(out_unlock_target, rc = -EPERM);
1635
1636         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1637         if (IS_ERR(mold))
1638                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1639
1640         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1641         /* save version after locking */
1642         mdt_version_get_save(info, mold, 2);
1643         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1644
1645         /* step 4: find & lock the new object. */
1646         /* new target object may not exist now */
1647         /* lookup with version checking */
1648         fid_zero(new_fid);
1649         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1650                                       3);
1651         if (rc == 0) {
1652                 /* the new_fid should have been filled at this moment */
1653                 if (lu_fid_eq(old_fid, new_fid))
1654                         GOTO(out_put_old, rc);
1655
1656                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1657                     lu_fid_eq(new_fid, rr->rr_fid2))
1658                         GOTO(out_put_old, rc = -EINVAL);
1659
1660                 if (!fid_is_md_operative(new_fid))
1661                         GOTO(out_put_old, rc = -EPERM);
1662
1663                 if (mdt_object_remote(mold)) {
1664                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1665                                PFID(old_fid));
1666                         GOTO(out_put_old, rc = -EXDEV);
1667                 }
1668
1669                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1670                 if (IS_ERR(mnew))
1671                         GOTO(out_put_old, rc = PTR_ERR(mnew));
1672
1673                 if (mdt_object_remote(mnew)) {
1674                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1675                                PFID(new_fid));
1676                         GOTO(out_put_new, rc = -EXDEV);
1677                 }
1678
1679                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1680                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1681                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1682                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1683                 if (rc != 0)
1684                         GOTO(out_put_new, rc);
1685
1686                 /* We used to acquire MDS_INODELOCK_FULL here but we
1687                  * can't do this now because a running HSM restore on
1688                  * the rename onto victim will hold the layout
1689                  * lock. See LU-4002. */
1690
1691                 lh_newp = &info->mti_lh[MDT_LH_NEW];
1692                 mdt_lock_reg_init(lh_newp, LCK_EX);
1693                 rc = mdt_object_lock(info, mnew, lh_newp,
1694                                      MDS_INODELOCK_LOOKUP |
1695                                      MDS_INODELOCK_UPDATE,
1696                                      MDT_CROSS_LOCK);
1697                 if (rc != 0)
1698                         GOTO(out_unlock_old, rc);
1699
1700                 /* get and save version after locking */
1701                 mdt_version_get_save(info, mnew, 3);
1702                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1703         } else if (rc != -EREMOTE && rc != -ENOENT) {
1704                 GOTO(out_put_old, rc);
1705         } else {
1706                 /* If mnew does not exist and mold are remote directory,
1707                  * it only allows rename if they are under same directory */
1708                 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1709                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1710                                PFID(old_fid));
1711                         GOTO(out_put_old, rc = -EXDEV);
1712                 }
1713
1714                 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1715                 mdt_lock_reg_init(lh_oldp, LCK_EX);
1716                 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1717                                      MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1718                 if (rc != 0)
1719                         GOTO(out_put_old, rc);
1720
1721                 mdt_enoent_version_save(info, 3);
1722         }
1723
1724         /* step 5: rename it */
1725         mdt_reint_init_ma(info, ma);
1726
1727         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1728                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1729
1730         /* Check if @dst is subdir of @src. */
1731         rc = mdt_rename_sanity(info, old_fid);
1732         if (rc)
1733                 GOTO(out_unlock_new, rc);
1734
1735         if (mnew != NULL)
1736                 mutex_lock(&mnew->mot_lov_mutex);
1737
1738         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1739                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1740                         mnew != NULL ? mdt_object_child(mnew) : NULL,
1741                         &rr->rr_tgt_name, ma);
1742
1743         if (mnew != NULL)
1744                 mutex_unlock(&mnew->mot_lov_mutex);
1745
1746         /* handle last link of tgt object */
1747         if (rc == 0) {
1748                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1749                 if (mnew)
1750                         mdt_handle_last_unlink(info, mnew, ma);
1751
1752                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1753                                          msrcdir, mtgtdir);
1754         }
1755
1756         EXIT;
1757 out_unlock_new:
1758         if (mnew != NULL)
1759                 mdt_object_unlock(info, mnew, lh_newp, rc);
1760 out_unlock_old:
1761         mdt_object_unlock(info, mold, lh_oldp, rc);
1762 out_put_new:
1763         if (mnew != NULL)
1764                 mdt_object_put(info->mti_env, mnew);
1765 out_put_old:
1766         mdt_object_put(info->mti_env, mold);
1767 out_unlock_target:
1768         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1769 out_put_target:
1770         mdt_object_put(info->mti_env, mtgtdir);
1771 out_unlock_source:
1772         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1773         return rc;
1774 }
1775
1776 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1777                                        struct mdt_lock_handle *lhc,
1778                                        bool  rename)
1779 {
1780         struct mdt_reint_record *rr = &info->mti_rr;
1781         struct ptlrpc_request   *req = mdt_info_req(info);
1782         struct lustre_handle    rename_lh = { 0 };
1783         int                     rc;
1784         ENTRY;
1785
1786         if (info->mti_dlm_req)
1787                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1788
1789         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1) ||
1790             fid_is_obf(rr->rr_fid2) || fid_is_dot_lustre(rr->rr_fid2))
1791                 RETURN(-EPERM);
1792
1793         rc = mdt_rename_lock(info, &rename_lh);
1794         if (rc != 0) {
1795                 CERROR("%s: can't lock FS for rename: rc  = %d\n",
1796                        mdt_obd_name(info->mti_mdt), rc);
1797                 RETURN(rc);
1798         }
1799
1800         if (rename)
1801                 rc = mdt_reint_rename_internal(info, lhc);
1802         else
1803                 rc = mdt_reint_migrate_internal(info, lhc);
1804
1805         if (lustre_handle_is_used(&rename_lh))
1806                 mdt_rename_unlock(&rename_lh);
1807
1808         RETURN(rc);
1809 }
1810
1811 static int mdt_reint_rename(struct mdt_thread_info *info,
1812                             struct mdt_lock_handle *lhc)
1813 {
1814         return mdt_reint_rename_or_migrate(info, lhc, true);
1815 }
1816
1817 static int mdt_reint_migrate(struct mdt_thread_info *info,
1818                             struct mdt_lock_handle *lhc)
1819 {
1820         return mdt_reint_rename_or_migrate(info, lhc, false);
1821 }
1822
1823 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1824                            struct mdt_lock_handle *lhc);
1825
1826 static mdt_reinter reinters[REINT_MAX] = {
1827         [REINT_SETATTR]  = mdt_reint_setattr,
1828         [REINT_CREATE]   = mdt_reint_create,
1829         [REINT_LINK]     = mdt_reint_link,
1830         [REINT_UNLINK]   = mdt_reint_unlink,
1831         [REINT_RENAME]   = mdt_reint_rename,
1832         [REINT_OPEN]     = mdt_reint_open,
1833         [REINT_SETXATTR] = mdt_reint_setxattr,
1834         [REINT_RMENTRY]  = mdt_reint_unlink,
1835         [REINT_MIGRATE]   = mdt_reint_migrate,
1836 };
1837
1838 int mdt_reint_rec(struct mdt_thread_info *info,
1839                   struct mdt_lock_handle *lhc)
1840 {
1841         int rc;
1842         ENTRY;
1843
1844         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1845
1846         RETURN(rc);
1847 }