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