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