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