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