Whamcloud - gitweb
e7c4cf536c40bba7d3994f3f2832da6c3d78685a
[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         DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s\n", PFID(rr->rr_fid1),
381                   rr->rr_name);
382
383         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
384                 GOTO(out, rc = err_serious(-ENOENT));
385
386         /* step 1: lock the parent */
387         parent_lh = &info->mti_lh[MDT_LH_PARENT];
388         mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
389                           rr->rr_namelen);
390
391         mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
392                                   MDS_INODELOCK_UPDATE);
393         if (IS_ERR(mp))
394                 GOTO(out, rc = PTR_ERR(mp));
395
396         ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
397         ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
398                                                &RMF_MDT_MD, RCL_SERVER);
399
400         ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
401                                                &RMF_LOGCOOKIES);
402         ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
403                                                   &RMF_LOGCOOKIES,
404                                                   RCL_SERVER);
405         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
406         ma->ma_valid = 0;
407         if (!ma->ma_lmm || !ma->ma_cookie)
408                 GOTO(out_unlock_parent, rc = -EINVAL);
409
410         if (info->mti_cross_ref) {
411                 /*
412                  * Remote partial operation. It is possible that replay may
413                  * happen on parent MDT and this operation will be repeated.
414                  * Therefore the object absense is allowed case and nothing
415                  * should be done here.
416                  */
417                 if (mdt_object_exists(mp) > 0) {
418                         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
419                         rc = mo_ref_del(info->mti_env,
420                                         mdt_object_child(mp), ma);
421                         mdt_handle_last_unlink(info, mp, ma);
422                 } else
423                         rc = 0;
424                 GOTO(out_unlock_parent, rc);
425         }
426
427         /* step 2: find & lock the child */
428         rc = mdo_lookup(info->mti_env, mdt_object_child(mp),
429                         rr->rr_name, child_fid, &info->mti_spec);
430         if (rc != 0)
431                  GOTO(out_unlock_parent, rc);
432
433         /* We will lock the child regardless it is local or remote. No harm. */
434         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
435         if (IS_ERR(mc))
436                 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
437         child_lh = &info->mti_lh[MDT_LH_CHILD];
438         mdt_lock_reg_init(child_lh, LCK_EX);
439         rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
440                              MDT_CROSS_LOCK);
441         if (rc != 0) {
442                 mdt_object_put(info->mti_env, mc);
443                 GOTO(out_unlock_parent, rc);
444         }
445
446         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
447                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
448
449         /*
450          * Now we can only make sure we need MA_INODE, in mdd layer, will check
451          * whether need MA_LOV and MA_COOKIE.
452          */
453         ma->ma_need = MA_INODE;
454         ma->ma_valid = 0;
455         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
456         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
457                         mdt_object_child(mc), rr->rr_name, ma);
458         if (rc == 0)
459                 mdt_handle_last_unlink(info, mc, ma);
460
461         EXIT;
462         mdt_object_unlock_put(info, mc, child_lh, rc);
463 out_unlock_parent:
464         mdt_object_unlock_put(info, mp, parent_lh, rc);
465 out:
466         mdt_shrink_reply(info, REPLY_REC_OFF + 1, 0, 0);
467         mdt_lprocfs_time_end(info->mti_mdt, &info->mti_time,
468                              LPROC_MDT_REINT_UNLINK);
469         return rc;
470 }
471
472 static int mdt_reint_link(struct mdt_thread_info *info,
473                           struct mdt_lock_handle *lhc)
474 {
475         struct mdt_reint_record *rr = &info->mti_rr;
476         struct ptlrpc_request   *req = mdt_info_req(info);
477         struct md_attr          *ma = &info->mti_attr;
478         struct mdt_object       *ms;
479         struct mdt_object       *mp;
480         struct mdt_lock_handle  *lhs;
481         struct mdt_lock_handle  *lhp;
482         int rc;
483         ENTRY;
484
485         mdt_lprocfs_time_start(info->mti_mdt, &info->mti_time,
486                                LPROC_MDT_REINT_LINK);
487         
488         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
489                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
490
491         if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
492                 GOTO(out, rc = err_serious(-ENOENT));
493
494         if (info->mti_cross_ref) {
495                 /* MDT holding name ask us to add ref. */
496                 lhs = &info->mti_lh[MDT_LH_CHILD];
497                 mdt_lock_reg_init(lhs, LCK_EX);
498                 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
499                                           MDS_INODELOCK_UPDATE);
500                 if (IS_ERR(ms))
501                         GOTO(out, rc = PTR_ERR(ms));
502
503                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
504                 rc = mo_ref_add(info->mti_env, mdt_object_child(ms));
505                 mdt_object_unlock_put(info, ms, lhs, rc);
506                 mdt_lprocfs_time_end(info->mti_mdt, &info->mti_time,
507                                      LPROC_MDT_REINT_LINK);
508                 GOTO(out, rc);
509         }
510
511         /* Invalid case so return error immediately instead of
512          * processing it */
513         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
514                 GOTO(out, rc = -EPERM);
515         
516         /* step 1: find & lock the target parent dir */
517         lhp = &info->mti_lh[MDT_LH_PARENT];
518         mdt_lock_pdo_init(lhp, LCK_EX, rr->rr_name,
519                           rr->rr_namelen);
520         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
521                                   MDS_INODELOCK_UPDATE);
522         if (IS_ERR(mp))
523                 GOTO(out, rc = PTR_ERR(mp));
524
525         /* step 2: find & lock the source */
526         lhs = &info->mti_lh[MDT_LH_CHILD];
527         mdt_lock_reg_init(lhs, LCK_EX);
528  
529         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
530         if (IS_ERR(ms))
531                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
532         
533         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
534                             MDT_CROSS_LOCK);
535         if (rc != 0) {
536                 mdt_object_put(info->mti_env, ms);
537                 GOTO(out_unlock_parent, rc);
538         }
539
540         /* step 3: link it */
541         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
542                        OBD_FAIL_MDS_REINT_LINK_WRITE);
543
544         rc = mdo_link(info->mti_env, mdt_object_child(mp),
545                       mdt_object_child(ms), rr->rr_name, ma);
546
547         EXIT;
548         mdt_object_unlock_put(info, ms, lhs, rc);
549 out_unlock_parent:
550         mdt_object_unlock_put(info, mp, lhp, rc);
551 out:
552         mdt_lprocfs_time_end(info->mti_mdt, &info->mti_time,
553                              LPROC_MDT_REINT_LINK);
554         return rc;
555 }
556
557 /* partial operation for rename */
558 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
559 {
560         struct mdt_reint_record *rr = &info->mti_rr;
561         struct ptlrpc_request   *req = mdt_info_req(info);
562         struct md_attr          *ma = &info->mti_attr;
563         struct mdt_object       *mtgtdir;
564         struct mdt_object       *mtgt = NULL;
565         struct mdt_lock_handle  *lh_tgtdir;
566         struct mdt_lock_handle  *lh_tgt;
567         struct lu_fid           *tgt_fid = &info->mti_tmp_fid1;
568         int                      rc;
569         ENTRY;
570
571         DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
572                   rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
573
574         /* step 1: lookup & lock the tgt dir. */
575         lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
576         mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
577                           rr->rr_tgtlen);
578         mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
579                                        MDS_INODELOCK_UPDATE);
580         if (IS_ERR(mtgtdir))
581                 GOTO(out, rc = PTR_ERR(mtgtdir));
582
583         /* step 2: find & lock the target object if exists. */
584         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
585         rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
586                         rr->rr_tgt, tgt_fid, &info->mti_spec);
587         if (rc != 0 && rc != -ENOENT) {
588                 GOTO(out_unlock_tgtdir, rc);
589         } else if (rc == 0) {
590                 /*
591                  * In case of replay that name can be already inserted, check
592                  * that and do nothing if so.
593                  */
594                 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
595                         GOTO(out_unlock_tgtdir, rc);
596
597                 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
598                 mdt_lock_reg_init(lh_tgt, LCK_EX);
599
600                 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
601                                             MDS_INODELOCK_LOOKUP);
602                 if (IS_ERR(mtgt))
603                         GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
604
605                 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
606                                     mdt_object_child(mtgt), rr->rr_fid2,
607                                     rr->rr_tgt, ma);
608         } else /* -ENOENT */ {
609                 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
610                                      rr->rr_tgt, rr->rr_fid2,
611                                      S_ISDIR(ma->ma_attr.la_mode));
612         }
613
614         /* handle last link of tgt object */
615         if (rc == 0 && mtgt)
616                 mdt_handle_last_unlink(info, mtgt, ma);
617
618         if (mtgt != NULL)
619                 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
620         EXIT;
621 out_unlock_tgtdir:
622         mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
623 out:
624         mdt_shrink_reply(info, REPLY_REC_OFF + 1, 0, 0);
625         return rc;
626 }
627
628 static int mdt_rename_lock(struct mdt_thread_info *info,
629                            struct lustre_handle *lh)
630 {
631         struct ldlm_namespace *ns     = info->mti_mdt->mdt_namespace;
632         ldlm_policy_data_t    *policy = &info->mti_policy;
633         struct ldlm_res_id    *res_id = &info->mti_res_id;
634         struct lu_site        *ls;
635         int rc;
636         ENTRY;
637
638         ls = info->mti_mdt->mdt_md_dev.md_lu_dev.ld_site;
639         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
640
641         memset(policy, 0, sizeof *policy);
642         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
643
644         if (ls->ls_control_exp == NULL) {
645                 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
646
647                 /*
648                  * Current node is controller, that is mdt0, where we should
649                  * take BFL lock.
650                  */
651                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
652                                             LCK_EX, &flags, ldlm_blocking_ast,
653                                             ldlm_completion_ast, NULL, NULL, 0,
654                                             NULL, lh);
655         } else {
656                 int flags = 0;
657
658                 /*
659                  * This is the case mdt0 is remote node, issue DLM lock like
660                  * other clients.
661                  */
662                 rc = ldlm_cli_enqueue(ls->ls_control_exp, NULL, res_id,
663                                       LDLM_IBITS, policy, LCK_EX, &flags,
664                                       ldlm_blocking_ast, ldlm_completion_ast,
665                                       NULL, NULL, NULL, 0, NULL, lh, 0);
666         }
667
668         RETURN(rc);
669 }
670
671 static void mdt_rename_unlock(struct lustre_handle *lh)
672 {
673         ENTRY;
674         LASSERT(lustre_handle_is_used(lh));
675         ldlm_lock_decref(lh, LCK_EX);
676         EXIT;
677 }
678
679 /*
680  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
681  * target. Source should not be ancestor of target dir. May be other rename
682  * checks can be moved here later.
683  */
684 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
685 {
686         struct mdt_reint_record *rr = &info->mti_rr;
687         struct lu_fid dst_fid = *rr->rr_fid2;
688         struct mdt_object *dst;
689         int rc = 0;
690         ENTRY;
691
692         do {
693                 LASSERT(fid_is_sane(&dst_fid));
694                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
695                 if (!IS_ERR(dst)) {
696                         rc = mdo_is_subdir(info->mti_env,
697                                            mdt_object_child(dst), fid,
698                                            &dst_fid);
699                         mdt_object_put(info->mti_env, dst);
700                         if (rc != -EREMOTE && rc < 0) {
701                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
702                         } else {
703                                 /* check the found fid */
704                                 if (lu_fid_eq(&dst_fid, fid))
705                                         rc = -EINVAL;
706                         }
707                 } else {
708                         rc = PTR_ERR(dst);
709                 }
710         } while (rc == -EREMOTE);
711
712         RETURN(rc);
713 }
714
715 static int mdt_reint_rename(struct mdt_thread_info *info,
716                             struct mdt_lock_handle *lhc)
717 {
718         struct mdt_reint_record *rr = &info->mti_rr;
719         struct md_attr          *ma = &info->mti_attr;
720         struct ptlrpc_request   *req = mdt_info_req(info);
721         struct mdt_object       *msrcdir;
722         struct mdt_object       *mtgtdir;
723         struct mdt_object       *mold;
724         struct mdt_object       *mnew = NULL;
725         struct mdt_lock_handle  *lh_srcdirp;
726         struct mdt_lock_handle  *lh_tgtdirp;
727         struct mdt_lock_handle  *lh_oldp;
728         struct mdt_lock_handle  *lh_newp;
729         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
730         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
731         struct lustre_handle     rename_lh = { 0 };
732         int                      rc;
733         ENTRY;
734
735         mdt_lprocfs_time_start(info->mti_mdt, &info->mti_time,
736                                LPROC_MDT_REINT_RENAME);
737         
738         if (info->mti_cross_ref) {
739                 rc = mdt_reint_rename_tgt(info);
740                 mdt_lprocfs_time_end(info->mti_mdt, &info->mti_time,
741                                      LPROC_MDT_REINT_RENAME);
742                 RETURN(rc);
743         }
744
745         DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
746                   PFID(rr->rr_fid1), rr->rr_name,
747                   PFID(rr->rr_fid2), rr->rr_tgt);
748
749         rc = mdt_rename_lock(info, &rename_lh);
750         if (rc) {
751                 CERROR("Can't lock FS for rename, rc %d\n", rc);
752                 GOTO(out, rc);
753         }
754
755         lh_newp = &info->mti_lh[MDT_LH_NEW];
756
757         /* step 1: lock the source dir. */
758         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
759         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
760                           rr->rr_namelen);
761         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
762                                        MDS_INODELOCK_UPDATE);
763         if (IS_ERR(msrcdir))
764                 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
765
766         /* step 2: find & lock the target dir. */
767         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
768         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
769                           rr->rr_tgtlen);
770         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
771                 mdt_object_get(info->mti_env, msrcdir);
772                 mtgtdir = msrcdir;
773         } else {
774                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
775                                           rr->rr_fid2);
776                 if (IS_ERR(mtgtdir))
777                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
778
779                 rc = mdt_object_exists(mtgtdir);
780                 if (rc == 0)
781                         GOTO(out_unlock_target, rc = -ESTALE);
782                 else if (rc > 0) {
783                         /* we lock the target dir if it is local */
784                         rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
785                                              MDS_INODELOCK_UPDATE,
786                                              MDT_LOCAL_LOCK);
787                         if (rc != 0)
788                                 GOTO(out_unlock_target, rc);
789                 }
790         }
791
792         /* step 3: find & lock the old object. */
793         rc = mdo_lookup(info->mti_env, mdt_object_child(msrcdir),
794                         rr->rr_name, old_fid, &info->mti_spec);
795         if (rc != 0)
796                 GOTO(out_unlock_target, rc);
797
798         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
799                 GOTO(out_unlock_target, rc = -EINVAL);
800
801         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
802         if (IS_ERR(mold))
803                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
804
805         lh_oldp = &info->mti_lh[MDT_LH_OLD];
806         mdt_lock_reg_init(lh_oldp, LCK_EX);
807         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
808                              MDT_CROSS_LOCK);
809         if (rc != 0) {
810                 mdt_object_put(info->mti_env, mold);
811                 GOTO(out_unlock_target, rc);
812         }
813
814         /* step 4: find & lock the new object. */
815         /* new target object may not exist now */
816         rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
817                         rr->rr_tgt, new_fid, &info->mti_spec);
818         if (rc == 0) {
819                 /* the new_fid should have been filled at this moment */
820                 if (lu_fid_eq(old_fid, new_fid))
821                        GOTO(out_unlock_old, rc);
822
823                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
824                     lu_fid_eq(new_fid, rr->rr_fid2))
825                         GOTO(out_unlock_old, rc = -EINVAL);
826
827                 mdt_lock_reg_init(lh_newp, LCK_EX);
828                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
829                 if (IS_ERR(mnew))
830                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
831
832                 rc = mdt_object_lock(info, mnew, lh_newp,
833                                      MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
834                 if (rc != 0) {
835                         mdt_object_put(info->mti_env, mnew);
836                         GOTO(out_unlock_old, rc);
837                 }
838         } else if (rc != -EREMOTE && rc != -ENOENT)
839                 GOTO(out_unlock_old, rc);
840
841         /* step 5: rename it */
842         ma->ma_lmm = req_capsule_server_get(&info->mti_pill, &RMF_MDT_MD);
843         ma->ma_lmm_size = req_capsule_get_size(&info->mti_pill,
844                                                &RMF_MDT_MD, RCL_SERVER);
845
846         ma->ma_cookie = req_capsule_server_get(&info->mti_pill,
847                                                 &RMF_LOGCOOKIES);
848         ma->ma_cookie_size = req_capsule_get_size(&info->mti_pill,
849                                                &RMF_LOGCOOKIES, RCL_SERVER);
850
851         if (!ma->ma_lmm || !ma->ma_cookie)
852                 GOTO(out_unlock_new, rc = -EINVAL);
853
854         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
855         ma->ma_valid = 0;
856         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
857                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
858
859         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
860         mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
861
862         /* Check if @dst is subdir of @src. */
863         rc = mdt_rename_sanity(info, old_fid);
864         if (rc)
865                 GOTO(out_unlock_new, rc);
866
867         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
868                         mdt_object_child(mtgtdir), old_fid, rr->rr_name,
869                         (mnew ? mdt_object_child(mnew) : NULL),
870                         rr->rr_tgt, ma);
871
872         /* handle last link of tgt object */
873         if (rc == 0 && mnew)
874                 mdt_handle_last_unlink(info, mnew, ma);
875
876 out_unlock_new:
877         if (mnew)
878                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
879 out_unlock_old:
880         mdt_object_unlock_put(info, mold, lh_oldp, rc);
881 out_unlock_target:
882         mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
883 out_unlock_source:
884         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
885 out_rename_lock:
886         mdt_rename_unlock(&rename_lh);
887 out:
888         mdt_shrink_reply(info, REPLY_REC_OFF + 1, 0, 0);
889         mdt_lprocfs_time_end(info->mti_mdt, &info->mti_time,
890                              LPROC_MDT_REINT_RENAME);
891         return rc;
892 }
893
894 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
895                            struct mdt_lock_handle *lhc);
896
897 static mdt_reinter reinters[REINT_MAX] = {
898         [REINT_SETATTR]  = mdt_reint_setattr,
899         [REINT_CREATE] = mdt_reint_create,
900         [REINT_LINK] = mdt_reint_link,
901         [REINT_UNLINK] = mdt_reint_unlink,
902         [REINT_RENAME] = mdt_reint_rename,
903         [REINT_OPEN] = mdt_reint_open
904 };
905
906 int mdt_reint_rec(struct mdt_thread_info *info,
907                   struct mdt_lock_handle *lhc)
908 {
909         int rc;
910         ENTRY;
911
912         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
913
914         RETURN(rc);
915 }