Whamcloud - gitweb
676df34396ffa66395b36a8bb387ccf74325f58c
[fs/lustre-release.git] / lustre / mdt / mdt_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mdt/mdt_reint.c
5  *  Lustre Metadata Target (mdt) reintegration routines
6  *
7  *  Copyright (C) 2002-2006 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Huang Hua <huanghua@clusterfs.com>
12  *   Author: Yury Umanets <umka@clusterfs.com>
13  *
14  *   This file is part of the Lustre file system, http://www.lustre.org
15  *   Lustre is a trademark of Cluster File Systems, Inc.
16  *
17  *   You may have signed or agreed to another license before downloading
18  *   this software.  If so, you are bound by the terms and conditions
19  *   of that agreement, and the following does not apply to you.  See the
20  *   LICENSE file included with this distribution for more information.
21  *
22  *   If you did not agree to a different license, then this copy of Lustre
23  *   is open source software; you can redistribute it and/or modify it
24  *   under the terms of version 2 of the GNU General Public License as
25  *   published by the Free Software Foundation.
26  *
27  *   In either case, Lustre is distributed in the hope that it will be
28  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *   license text for more details.
31  */
32
33 #ifndef EXPORT_SYMTAB
34 # define EXPORT_SYMTAB
35 #endif
36 #define DEBUG_SUBSYSTEM S_MDS
37
38 #include "mdt_internal.h"
39 #include "lu_time.h"
40
41 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
42                                      struct md_attr *ma)
43 {
44         ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
45         ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
46                                                &RMF_MDT_MD, RCL_SERVER);
47
48         ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
49                                                &RMF_LOGCOOKIES);
50         ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
51                                                   &RMF_LOGCOOKIES,
52                                                   RCL_SERVER);
53
54         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
55         ma->ma_valid = 0;
56 }
57
58 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
59                                 struct mdt_object *object,
60                                 struct mdt_body *repbody)
61 {
62         ENTRY;
63
64         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
65          * we won't go to below*/
66         if (repbody->valid & OBD_MD_FLMDSCAPA)
67                 RETURN(rc);
68
69         if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa) {
70                 struct lustre_capa *capa;
71
72                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
73                 LASSERT(capa);
74                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
75                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
76                                  0);
77                 if (rc == 0)
78                         repbody->valid |= OBD_MD_FLMDSCAPA;
79         }
80         if (!(repbody->valid & OBD_MD_FLMDSCAPA))
81                 lustre_shrink_reply(mdt_info_req(info), REPLY_REC_OFF+1, 0, 1);
82
83         RETURN(rc);
84 }
85
86 static int mdt_md_create(struct mdt_thread_info *info)
87 {
88         struct mdt_device       *mdt = info->mti_mdt;
89         struct mdt_object       *parent;
90         struct mdt_object       *child;
91         struct mdt_lock_handle  *lh;
92         struct mdt_body         *repbody;
93         struct md_attr          *ma = &info->mti_attr;
94         struct mdt_reint_record *rr = &info->mti_rr;
95         struct lu_name          *lname;
96         int rc;
97         ENTRY;
98
99         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  (%s->"DFID") in "DFID,
100                   rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
101
102         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
103
104         lh = &info->mti_lh[MDT_LH_PARENT];
105         mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
106
107         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
108                                       MDS_INODELOCK_UPDATE);
109         if (IS_ERR(parent))
110                 RETURN(PTR_ERR(parent));
111
112         child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
113         if (likely(!IS_ERR(child))) {
114                 struct md_object *next = mdt_object_child(parent);
115
116                 ma->ma_need = MA_INODE;
117                 ma->ma_valid = 0;
118                 /* capa for cross-ref will be stored here */
119                 ma->ma_capa = req_capsule_server_get(&info->mti_pill,
120                                                      &RMF_CAPA1);
121                 LASSERT(ma->ma_capa);
122
123                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
124                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
125
126                 /* Let lower layer know current lock mode. */
127                 info->mti_spec.sp_cr_mode =
128                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
129
130                 /*
131                  * Do perform lookup sanity check. We do not know if name exists
132                  * or not.
133                  */
134                 info->mti_spec.sp_cr_lookup = 1;
135
136                 lname = mdt_name(info->mti_env, (char *)rr->rr_name,
137                                  rr->rr_namelen);
138                 rc = mdo_create(info->mti_env, next, lname,
139                                 mdt_object_child(child),
140                                 &info->mti_spec, ma);
141                 if (rc == 0) {
142                         /* Return fid & attr to client. */
143                         if (ma->ma_valid & MA_INODE)
144                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
145                                                    mdt_object_fid(child));
146                 }
147                 mdt_object_put(info->mti_env, child);
148         } else
149                 rc = PTR_ERR(child);
150
151         mdt_create_pack_capa(info, rc, child, repbody);
152         mdt_object_unlock_put(info, parent, lh, rc);
153         RETURN(rc);
154 }
155
156 /* Partial request to create object only */
157 static int mdt_md_mkobj(struct mdt_thread_info *info)
158 {
159         struct mdt_device      *mdt = info->mti_mdt;
160         struct mdt_object      *o;
161         struct mdt_body        *repbody;
162         struct md_attr         *ma = &info->mti_attr;
163         int rc;
164         ENTRY;
165
166         DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
167                   PFID(info->mti_rr.rr_fid2));
168
169         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
170
171         o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
172         if (!IS_ERR(o)) {
173                 struct md_object *next = mdt_object_child(o);
174
175                 ma->ma_need = MA_INODE;
176                 ma->ma_valid = 0;
177
178                 /*
179                  * Cross-ref create can encounter already created obj in case of
180                  * recovery, just get attr in that case.
181                  */
182                 if (mdt_object_exists(o) == 1) {
183                         rc = mo_attr_get(info->mti_env, next, ma);
184                 } else {
185                         /*
186                          * Here, NO permission check for object_create,
187                          * such check has been done on the original MDS.
188                          */
189                         rc = mo_object_create(info->mti_env, next,
190                                               &info->mti_spec, ma);
191                 }
192                 if (rc == 0) {
193                         /* Return fid & attr to client. */
194                         if (ma->ma_valid & MA_INODE)
195                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
196                                                    mdt_object_fid(o));
197                 }
198                 mdt_object_put(info->mti_env, o);
199         } else
200                 rc = PTR_ERR(o);
201
202         mdt_create_pack_capa(info, rc, o, repbody);
203         RETURN(rc);
204 }
205
206 /* In the raw-setattr case, we lock the child inode.
207  * In the write-back case or if being called from open,
208  *               the client holds a lock already.
209  * We use the ATTR_FROM_OPEN (translated into MRF_SETATTR_LOCKED by
210  * mdt_setattr_unpack()) flag to tell these cases apart. */
211 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo, int flags)
212 {
213         struct md_attr          *ma = &info->mti_attr;
214         struct mdt_lock_handle  *lh;
215         int som_update = 0;
216         int rc;
217         ENTRY;
218
219         /* attr shouldn't be set on remote object */
220         LASSERT(mdt_object_exists(mo) >= 0);
221
222         if (info->mti_epoch)
223                 som_update = (info->mti_epoch->flags & MF_SOM_CHANGE);
224
225         /* Try to avoid object_lock if another epoch has been started
226          * already. */
227         if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
228                 RETURN(0);
229
230         lh = &info->mti_lh[MDT_LH_PARENT];
231         mdt_lock_reg_init(lh, LCK_PW);
232
233         if (!(flags & MRF_SETATTR_LOCKED)) {
234                 __u64 lockpart = MDS_INODELOCK_UPDATE;
235                 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
236                         lockpart |= MDS_INODELOCK_LOOKUP;
237
238                 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
239                 if (rc != 0)
240                         RETURN(rc);
241         }
242
243         /* Setattrs are syncronized through dlm lock taken above. If another
244          * epoch started, its attributes may be already flushed on disk,
245          * skip setattr. */
246         if (som_update && (info->mti_epoch->ioepoch != mo->mot_ioepoch))
247                 GOTO(out_unlock, rc = 0);
248
249         if (mdt_object_exists(mo) == 0)
250                 GOTO(out_unlock, rc = -ENOENT);
251
252         /* all attrs are packed into mti_attr in unpack_setattr */
253         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
254                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
255
256         /* This is only for set ctime when rename's source is on remote MDS. */
257         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
258                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
259
260         /* all attrs are packed into mti_attr in unpack_setattr */
261         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
262         if (rc != 0)
263                 GOTO(out_unlock, rc);
264
265         /* Re-enable SIZEONMDS. */
266         if (som_update) {
267                 CDEBUG(D_INODE, "Closing epoch "LPU64" on "DFID". Count %d\n",
268                        mo->mot_ioepoch, PFID(mdt_object_fid(mo)),
269                        mo->mot_epochcount);
270                 mdt_sizeonmds_enable(info, mo);
271         }
272
273         EXIT;
274 out_unlock:
275         mdt_object_unlock(info, mo, lh, rc);
276         return rc;
277 }
278
279 static int mdt_reint_setattr(struct mdt_thread_info *info,
280                              struct mdt_lock_handle *lhc)
281 {
282         struct mdt_device       *mdt = info->mti_mdt;
283         struct md_attr          *ma = &info->mti_attr;
284         struct mdt_reint_record *rr = &info->mti_rr;
285         struct ptlrpc_request   *req = mdt_info_req(info);
286         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
287         struct mdt_file_data    *mfd;
288         struct mdt_object       *mo;
289         struct md_object        *next;
290         struct mdt_body         *repbody;
291         int                      rc;
292         ENTRY;
293
294         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
295                   (unsigned int)ma->ma_attr.la_valid);
296
297         if (info->mti_dlm_req)
298                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
299         
300         repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
301         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
302         if (IS_ERR(mo))
303                 GOTO(out, rc = PTR_ERR(mo));
304
305         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
306                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
307                     (rr->rr_flags & MRF_SETATTR_LOCKED)) {
308                         /* Check write access for the O_TRUNC case */
309                         if (mdt_write_read(info->mti_mdt, mo) < 0)
310                                 GOTO(out_put, rc = -ETXTBSY);
311                 }
312         } else if (info->mti_epoch &&
313                     (info->mti_epoch->flags & MF_EPOCH_OPEN)) {
314                 /* Truncate case. */
315                 rc = mdt_write_get(info->mti_mdt, mo);
316                 if (rc)
317                         GOTO(out_put, rc);
318
319                 mfd = mdt_mfd_new();
320                 if (mfd == NULL)
321                         GOTO(out_put, rc = -ENOMEM);
322
323                 mdt_epoch_open(info, mo);
324                 repbody->ioepoch = mo->mot_ioepoch;
325
326                 mdt_object_get(info->mti_env, mo);
327                 mdt_mfd_set_mode(mfd, FMODE_EPOCHLCK);
328                 mfd->mfd_object = mo;
329                 mfd->mfd_xid = req->rq_xid;
330
331                 spin_lock(&med->med_open_lock);
332                 list_add(&mfd->mfd_list, &med->med_open_head);
333                 spin_unlock(&med->med_open_lock);
334                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
335         }
336
337         rc = mdt_attr_set(info, mo, rr->rr_flags);
338         if (rc)
339                 GOTO(out_put, rc);
340
341         if (info->mti_epoch && (info->mti_epoch->flags & MF_SOM_CHANGE)) {
342                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
343                 LASSERT(info->mti_epoch);
344
345                 spin_lock(&med->med_open_lock);
346                 /* Size-on-MDS Update. Find and free mfd. */
347                 mfd = mdt_handle2mfd(info, &info->mti_epoch->handle);
348                 if (mfd == NULL) {
349                         spin_unlock(&med->med_open_lock);
350                         CDEBUG(D_INODE | D_ERROR, "no handle for file close: "
351                                         "fid = "DFID": cookie = "LPX64"\n",
352                                         PFID(info->mti_rr.rr_fid1),
353                                         info->mti_epoch->handle.cookie);
354                         GOTO(out_put, rc = -ESTALE);
355                 }
356                 if (mfd->mfd_mode != FMODE_SOM) {
357                         CWARN("mfd_mode %d not right should be in replay\n", 
358                                mfd->mfd_mode);
359                         LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & 
360                                 MSG_REPLAY);
361                         LASSERT(mfd->mfd_mode == FMODE_EPOCH);
362                         mfd->mfd_mode = FMODE_SOM;
363                 }
364                 
365                 LASSERT(ma->ma_attr.la_valid & LA_SIZE);
366                 LASSERT(!(info->mti_epoch->flags & MF_EPOCH_CLOSE));
367
368                 class_handle_unhash(&mfd->mfd_handle);
369                 list_del_init(&mfd->mfd_list);
370                 spin_unlock(&med->med_open_lock);
371
372                 mdt_mfd_close(info, mfd);
373         }
374
375         ma->ma_need = MA_INODE;
376         ma->ma_valid = 0;
377         next = mdt_object_child(mo);
378         rc = mo_attr_get(info->mti_env, next, ma);
379         if (rc != 0)
380                 GOTO(out_put, rc);
381
382         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
383
384         if (mdt->mdt_opts.mo_oss_capa &&
385             S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
386             (ma->ma_attr.la_valid & LA_SIZE)) {
387                 struct lustre_capa *capa;
388
389                 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
390                 LASSERT(capa);
391                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
392                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
393                 if (rc)
394                         GOTO(out_put, rc);
395                 repbody->valid |= OBD_MD_FLOSSCAPA;
396         }
397
398         EXIT;
399 out_put:
400         mdt_object_put(info->mti_env, mo);
401 out:
402         return rc;
403 }
404
405 static int mdt_reint_create(struct mdt_thread_info *info,
406                             struct mdt_lock_handle *lhc)
407 {
408         int rc;
409         ENTRY;
410
411         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
412                 GOTO(out, rc = err_serious(-ESTALE));
413
414         if (info->mti_dlm_req)
415                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
416
417         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
418         case S_IFDIR:{
419                 /* Cross-ref case. */
420                 if (info->mti_cross_ref) {
421                         rc = mdt_md_mkobj(info);
422                         break;
423                 }
424         }
425         case S_IFREG:
426         case S_IFLNK:
427         case S_IFCHR:
428         case S_IFBLK:
429         case S_IFIFO:
430         case S_IFSOCK:{
431                 /* Special file should stay on the same node as parent. */
432                 LASSERT(info->mti_rr.rr_namelen > 0);
433                 rc = mdt_md_create(info);
434                 break;
435         }
436         default:
437                 rc = err_serious(-EOPNOTSUPP);
438         }
439         EXIT;
440 out:
441         return rc;
442 }
443
444 static int mdt_reint_unlink(struct mdt_thread_info *info,
445                             struct mdt_lock_handle *lhc)
446 {
447         struct mdt_reint_record *rr = &info->mti_rr;
448         struct ptlrpc_request   *req = mdt_info_req(info);
449         struct md_attr          *ma = &info->mti_attr;
450         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
451         struct mdt_object       *mp;
452         struct mdt_object       *mc;
453         struct mdt_lock_handle  *parent_lh;
454         struct mdt_lock_handle  *child_lh;
455         struct lu_name          *lname;
456         int                      rc;
457         ENTRY;
458
459         DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
460                   rr->rr_name);
461
462         if (info->mti_dlm_req)
463                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
464
465         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
466                 GOTO(out, rc = err_serious(-ENOENT));
467
468         /* step 1: lock the parent */
469         parent_lh = &info->mti_lh[MDT_LH_PARENT];
470         mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
471                           rr->rr_namelen);
472
473         mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
474                                   MDS_INODELOCK_UPDATE);
475         if (IS_ERR(mp)) {
476                 rc = PTR_ERR(mp);
477                 /* errors are possible here in cross-ref cases, see below */ 
478                 if (info->mti_cross_ref)
479                         rc = 0;
480                 GOTO(out, rc);
481         }
482
483         mdt_reint_init_ma(info, ma);
484         if (!ma->ma_lmm || !ma->ma_cookie)
485                 GOTO(out_unlock_parent, rc = -EINVAL);
486
487         if (info->mti_cross_ref) {
488                 /*
489                  * Remote partial operation. It is possible that replay may
490                  * happen on parent MDT and this operation will be repeated.
491                  * Therefore the object absense is allowed case and nothing
492                  * should be done here.
493                  */
494                 if (mdt_object_exists(mp) > 0) {
495                         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
496                         rc = mo_ref_del(info->mti_env,
497                                         mdt_object_child(mp), ma);
498                         mdt_handle_last_unlink(info, mp, ma);
499                 } else
500                         rc = 0;
501                 GOTO(out_unlock_parent, rc);
502         }
503
504         /* step 2: find & lock the child */
505         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
506         rc = mdo_lookup(info->mti_env, mdt_object_child(mp),
507                         lname, child_fid, &info->mti_spec);
508         if (rc != 0)
509                  GOTO(out_unlock_parent, rc);
510
511         /* We will lock the child regardless it is local or remote. No harm. */
512         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
513         if (IS_ERR(mc))
514                 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
515         child_lh = &info->mti_lh[MDT_LH_CHILD];
516         mdt_lock_reg_init(child_lh, LCK_EX);
517         rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
518                              MDT_CROSS_LOCK);
519         if (rc != 0) {
520                 mdt_object_put(info->mti_env, mc);
521                 GOTO(out_unlock_parent, rc);
522         }
523
524         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
525                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
526
527         /*
528          * Now we can only make sure we need MA_INODE, in mdd layer, will check
529          * whether need MA_LOV and MA_COOKIE.
530          */
531         ma->ma_need = MA_INODE;
532         ma->ma_valid = 0;
533         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
534         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
535                         mdt_object_child(mc), lname, ma);
536         if (rc == 0)
537                 mdt_handle_last_unlink(info, mc, ma);
538
539         EXIT;
540         mdt_object_unlock_put(info, mc, child_lh, rc);
541 out_unlock_parent:
542         mdt_object_unlock_put(info, mp, parent_lh, rc);
543 out:
544         mdt_shrink_reply(info);
545         return rc;
546 }
547
548 static int mdt_reint_link(struct mdt_thread_info *info,
549                           struct mdt_lock_handle *lhc)
550 {
551         struct mdt_reint_record *rr = &info->mti_rr;
552         struct ptlrpc_request   *req = mdt_info_req(info);
553         struct md_attr          *ma = &info->mti_attr;
554         struct mdt_object       *ms;
555         struct mdt_object       *mp;
556         struct mdt_lock_handle  *lhs;
557         struct mdt_lock_handle  *lhp;
558         struct lu_name          *lname;
559         int rc;
560         ENTRY;
561
562         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
563                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
564
565         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
566                 GOTO(out, rc = err_serious(-ENOENT));
567
568         if (info->mti_dlm_req)
569                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
570
571         if (info->mti_cross_ref) {
572                 /* MDT holding name ask us to add ref. */
573                 lhs = &info->mti_lh[MDT_LH_CHILD];
574                 mdt_lock_reg_init(lhs, LCK_EX);
575                 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
576                                           MDS_INODELOCK_UPDATE);
577                 if (IS_ERR(ms))
578                         GOTO(out, rc = PTR_ERR(ms));
579
580                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
581                 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
582                 mdt_object_unlock_put(info, ms, lhs, rc);
583                 GOTO(out, rc);
584         }
585
586         /* Invalid case so return error immediately instead of
587          * processing it */
588         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
589                 GOTO(out, rc = -EPERM);
590
591         /* step 1: find & lock the target parent dir */
592         lhp = &info->mti_lh[MDT_LH_PARENT];
593         mdt_lock_pdo_init(lhp, LCK_EX, rr->rr_name,
594                           rr->rr_namelen);
595         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
596                                   MDS_INODELOCK_UPDATE);
597         if (IS_ERR(mp))
598                 GOTO(out, rc = PTR_ERR(mp));
599
600         /* step 2: find & lock the source */
601         lhs = &info->mti_lh[MDT_LH_CHILD];
602         mdt_lock_reg_init(lhs, LCK_EX);
603
604         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
605         if (IS_ERR(ms))
606                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
607
608         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
609                             MDT_CROSS_LOCK);
610         if (rc != 0) {
611                 mdt_object_put(info->mti_env, ms);
612                 GOTO(out_unlock_parent, rc);
613         }
614
615         /* step 3: link it */
616         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
617                        OBD_FAIL_MDS_REINT_LINK_WRITE);
618
619         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
620         rc = mdo_link(info->mti_env, mdt_object_child(mp),
621                       mdt_object_child(ms), lname, ma);
622
623         EXIT;
624         mdt_object_unlock_put(info, ms, lhs, rc);
625 out_unlock_parent:
626         mdt_object_unlock_put(info, mp, lhp, rc);
627 out:
628         return rc;
629 }
630
631 /* partial operation for rename */
632 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
633 {
634         struct mdt_reint_record *rr = &info->mti_rr;
635         struct ptlrpc_request   *req = mdt_info_req(info);
636         struct md_attr          *ma = &info->mti_attr;
637         struct mdt_object       *mtgtdir;
638         struct mdt_object       *mtgt = NULL;
639         struct mdt_lock_handle  *lh_tgtdir;
640         struct mdt_lock_handle  *lh_tgt = NULL;
641         struct lu_fid           *tgt_fid = &info->mti_tmp_fid1;
642         struct lu_name          *lname;
643         int                      rc;
644         ENTRY;
645
646         DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
647                   rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
648
649         /* step 1: lookup & lock the tgt dir. */
650         lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
651         mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
652                           rr->rr_tgtlen);
653         mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
654                                        MDS_INODELOCK_UPDATE);
655         if (IS_ERR(mtgtdir))
656                 GOTO(out, rc = PTR_ERR(mtgtdir));
657
658         /* step 2: find & lock the target object if exists. */
659         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
660         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
661         rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
662                         lname, tgt_fid, &info->mti_spec);
663         if (rc != 0 && rc != -ENOENT) {
664                 GOTO(out_unlock_tgtdir, rc);
665         } else if (rc == 0) {
666                 /*
667                  * In case of replay that name can be already inserted, check
668                  * that and do nothing if so.
669                  */
670                 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
671                         GOTO(out_unlock_tgtdir, rc);
672
673                 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
674                 mdt_lock_reg_init(lh_tgt, LCK_EX);
675
676                 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
677                                             MDS_INODELOCK_LOOKUP);
678                 if (IS_ERR(mtgt))
679                         GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
680
681                 mdt_reint_init_ma(info, ma);
682                 if (!ma->ma_lmm || !ma->ma_cookie)
683                         GOTO(out_unlock_tgt, rc = -EINVAL);
684
685                 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
686                                     mdt_object_child(mtgt), rr->rr_fid2,
687                                     lname, ma);
688         } else /* -ENOENT */ {
689                 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
690                                      lname, rr->rr_fid2, ma);
691         }
692
693         /* handle last link of tgt object */
694         if (rc == 0 && mtgt)
695                 mdt_handle_last_unlink(info, mtgt, ma);
696
697         EXIT;
698 out_unlock_tgt:
699         if (mtgt)
700                 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
701 out_unlock_tgtdir:
702         mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
703 out:
704         mdt_shrink_reply(info);
705         return rc;
706 }
707
708 static int mdt_rename_lock(struct mdt_thread_info *info,
709                            struct lustre_handle *lh)
710 {
711         struct ldlm_namespace *ns     = info->mti_mdt->mdt_namespace;
712         ldlm_policy_data_t    *policy = &info->mti_policy;
713         struct ldlm_res_id    *res_id = &info->mti_res_id;
714         struct lu_site        *ls;
715         int rc;
716         ENTRY;
717
718         /* 
719          * Disable global rename BFL lock temporarily because 
720          * when a mds do rename recoverying, which might enqueue 
721          * BFL lock to the controller mds. and this req might be 
722          * replay req for controller mds. but we did not have 
723          * such handling in controller mds. XXX
724          */
725         RETURN(0);
726         ls = info->mti_mdt->mdt_md_dev.md_lu_dev.ld_site;
727         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
728
729         memset(policy, 0, sizeof *policy);
730         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
731
732         if (ls->ls_control_exp == NULL) {
733                 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
734
735                 /*
736                  * Current node is controller, that is mdt0, where we should
737                  * take BFL lock.
738                  */
739                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
740                                             LCK_EX, &flags, ldlm_blocking_ast,
741                                             ldlm_completion_ast, NULL, NULL, 0,
742                                             NULL, lh);
743         } else {
744                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
745                         ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL };
746                 int flags = 0;
747
748                 /*
749                  * This is the case mdt0 is remote node, issue DLM lock like
750                  * other clients.
751                  */
752                 rc = ldlm_cli_enqueue(ls->ls_control_exp, NULL, &einfo, res_id,
753                                       policy, &flags, NULL, 0, NULL, lh, 0);
754         }
755
756         RETURN(rc);
757 }
758
759 static void mdt_rename_unlock(struct lustre_handle *lh)
760 {
761         ENTRY;
762         /* Disable global rename BFL lock temporarily. see above XXX*/
763         EXIT;
764         return;
765         LASSERT(lustre_handle_is_used(lh));
766         ldlm_lock_decref(lh, LCK_EX);
767         EXIT;
768 }
769
770 /*
771  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
772  * target. Source should not be ancestor of target dir. May be other rename
773  * checks can be moved here later.
774  */
775 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
776 {
777         struct mdt_reint_record *rr = &info->mti_rr;
778         struct lu_fid dst_fid = *rr->rr_fid2;
779         struct mdt_object *dst;
780         int rc = 0;
781         ENTRY;
782
783         do {
784                 LASSERT(fid_is_sane(&dst_fid));
785                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
786                 if (!IS_ERR(dst)) {
787                         rc = mdo_is_subdir(info->mti_env,
788                                            mdt_object_child(dst), fid,
789                                            &dst_fid);
790                         mdt_object_put(info->mti_env, dst);
791                         if (rc != -EREMOTE && rc < 0) {
792                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
793                         } else {
794                                 /* check the found fid */
795                                 if (lu_fid_eq(&dst_fid, fid))
796                                         rc = -EINVAL;
797                         }
798                 } else {
799                         rc = PTR_ERR(dst);
800                 }
801         } while (rc == -EREMOTE);
802
803         RETURN(rc);
804 }
805
806 static int mdt_reint_rename(struct mdt_thread_info *info,
807                             struct mdt_lock_handle *lhc)
808 {
809         struct mdt_reint_record *rr = &info->mti_rr;
810         struct md_attr          *ma = &info->mti_attr;
811         struct ptlrpc_request   *req = mdt_info_req(info);
812         struct mdt_object       *msrcdir;
813         struct mdt_object       *mtgtdir;
814         struct mdt_object       *mold;
815         struct mdt_object       *mnew = NULL;
816         struct mdt_lock_handle  *lh_srcdirp;
817         struct mdt_lock_handle  *lh_tgtdirp;
818         struct mdt_lock_handle  *lh_oldp;
819         struct mdt_lock_handle  *lh_newp;
820         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
821         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
822         struct lustre_handle     rename_lh = { 0 };
823         struct lu_name           slname = { 0 };
824         struct lu_name          *lname;
825         int                      rc;
826         ENTRY;
827
828         if (info->mti_dlm_req)
829                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
830
831         if (info->mti_cross_ref) {
832                 rc = mdt_reint_rename_tgt(info);
833                 RETURN(rc);
834         }
835
836         DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
837                   PFID(rr->rr_fid1), rr->rr_name,
838                   PFID(rr->rr_fid2), rr->rr_tgt);
839
840         rc = mdt_rename_lock(info, &rename_lh);
841         if (rc) {
842                 CERROR("Can't lock FS for rename, rc %d\n", rc);
843                 GOTO(out, rc);
844         }
845
846         lh_newp = &info->mti_lh[MDT_LH_NEW];
847
848         /* step 1: lock the source dir. */
849         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
850         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
851                           rr->rr_namelen);
852         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
853                                        MDS_INODELOCK_UPDATE);
854         if (IS_ERR(msrcdir))
855                 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
856
857         /* step 2: find & lock the target dir. */
858         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
859         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
860                           rr->rr_tgtlen);
861         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
862                 mdt_object_get(info->mti_env, msrcdir);
863                 mtgtdir = msrcdir;
864         } else {
865                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
866                                           rr->rr_fid2);
867                 if (IS_ERR(mtgtdir))
868                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
869
870                 rc = mdt_object_exists(mtgtdir);
871                 if (rc == 0)
872                         GOTO(out_unlock_target, rc = -ESTALE);
873                 else if (rc > 0) {
874                         /* we lock the target dir if it is local */
875                         rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
876                                              MDS_INODELOCK_UPDATE,
877                                              MDT_LOCAL_LOCK);
878                         if (rc != 0)
879                                 GOTO(out_unlock_target, rc);
880                 }
881         }
882
883         /* step 3: find & lock the old object. */
884         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
885         mdt_name_copy(&slname, lname);
886         rc = mdo_lookup(info->mti_env, mdt_object_child(msrcdir),
887                         &slname, old_fid, &info->mti_spec);
888         if (rc != 0)
889                 GOTO(out_unlock_target, rc);
890
891         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
892                 GOTO(out_unlock_target, rc = -EINVAL);
893
894         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
895         if (IS_ERR(mold))
896                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
897
898         lh_oldp = &info->mti_lh[MDT_LH_OLD];
899         mdt_lock_reg_init(lh_oldp, LCK_EX);
900         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
901                              MDT_CROSS_LOCK);
902         if (rc != 0) {
903                 mdt_object_put(info->mti_env, mold);
904                 GOTO(out_unlock_target, rc);
905         }
906         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
907
908         /* step 4: find & lock the new object. */
909         /* new target object may not exist now */
910         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
911         rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
912                         lname, new_fid, &info->mti_spec);
913         if (rc == 0) {
914                 /* the new_fid should have been filled at this moment */
915                 if (lu_fid_eq(old_fid, new_fid))
916                        GOTO(out_unlock_old, rc);
917
918                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
919                     lu_fid_eq(new_fid, rr->rr_fid2))
920                         GOTO(out_unlock_old, rc = -EINVAL);
921
922                 mdt_lock_reg_init(lh_newp, LCK_EX);
923                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
924                 if (IS_ERR(mnew))
925                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
926
927                 rc = mdt_object_lock(info, mnew, lh_newp,
928                                      MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
929                 if (rc != 0) {
930                         mdt_object_put(info->mti_env, mnew);
931                         GOTO(out_unlock_old, rc);
932                 }
933                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
934         } else if (rc != -EREMOTE && rc != -ENOENT)
935                 GOTO(out_unlock_old, rc);
936
937         /* step 5: rename it */
938         mdt_reint_init_ma(info, ma);
939         if (!ma->ma_lmm || !ma->ma_cookie)
940                 GOTO(out_unlock_new, rc = -EINVAL);
941
942         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
943                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
944
945
946         /* Check if @dst is subdir of @src. */
947         rc = mdt_rename_sanity(info, old_fid);
948         if (rc)
949                 GOTO(out_unlock_new, rc);
950
951         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
952                         mdt_object_child(mtgtdir), old_fid, &slname,
953                         (mnew ? mdt_object_child(mnew) : NULL),
954                         lname, ma);
955
956         /* handle last link of tgt object */
957         if (rc == 0 && mnew)
958                 mdt_handle_last_unlink(info, mnew, ma);
959
960         EXIT;
961 out_unlock_new:
962         if (mnew)
963                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
964 out_unlock_old:
965         mdt_object_unlock_put(info, mold, lh_oldp, rc);
966 out_unlock_target:
967         mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
968 out_unlock_source:
969         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
970 out_rename_lock:
971         mdt_rename_unlock(&rename_lh);
972 out:
973         mdt_shrink_reply(info);
974         return rc;
975 }
976
977 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
978                            struct mdt_lock_handle *lhc);
979
980 static mdt_reinter reinters[REINT_MAX] = {
981         [REINT_SETATTR]  = mdt_reint_setattr,
982         [REINT_CREATE] = mdt_reint_create,
983         [REINT_LINK] = mdt_reint_link,
984         [REINT_UNLINK] = mdt_reint_unlink,
985         [REINT_RENAME] = mdt_reint_rename,
986         [REINT_OPEN] = mdt_reint_open
987 };
988
989 int mdt_reint_rec(struct mdt_thread_info *info,
990                   struct mdt_lock_handle *lhc)
991 {
992         int rc;
993         ENTRY;
994
995         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
996
997         RETURN(rc);
998 }