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