Whamcloud - gitweb
LU-1304 mdt: get attributes with explicit calls
[fs/lustre-release.git] / lustre / mdt / mdt_reint.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include "mdt_internal.h"
50 #include "lu_time.h"
51
52 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
53                                      struct md_attr *ma)
54 {
55         ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
56         ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
57                                                &RMF_MDT_MD, RCL_SERVER);
58
59         ma->ma_cookie = req_capsule_server_get(info->mti_pill,
60                                                &RMF_LOGCOOKIES);
61         ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
62                                                   &RMF_LOGCOOKIES,
63                                                   RCL_SERVER);
64
65         ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
66         ma->ma_valid = 0;
67 }
68
69 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
70                                 struct mdt_object *object,
71                                 struct mdt_body *repbody)
72 {
73         ENTRY;
74
75         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
76          * we won't go to below*/
77         if (repbody->valid & OBD_MD_FLMDSCAPA)
78                 RETURN(rc);
79
80         if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
81             info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
82                 struct lustre_capa *capa;
83
84                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
85                 LASSERT(capa);
86                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
87                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
88                                  0);
89                 if (rc == 0)
90                         repbody->valid |= OBD_MD_FLMDSCAPA;
91         }
92
93         RETURN(rc);
94 }
95
96 /**
97  * Get version of object by fid.
98  *
99  * Return real version or ENOENT_VERSION if object doesn't exist
100  */
101 static void mdt_obj_version_get(struct mdt_thread_info *info,
102                                 struct mdt_object *o, __u64 *version)
103 {
104         LASSERT(o);
105         LASSERT(mdt_object_exists(o) >= 0);
106         if (mdt_object_exists(o) > 0 && !mdt_object_obf(o))
107                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
108         else
109                 *version = ENOENT_VERSION;
110         CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
111                PFID(mdt_object_fid(o)), *version);
112 }
113
114 /**
115  * Check version is correct.
116  *
117  * Should be called only during replay.
118  */
119 static int mdt_version_check(struct ptlrpc_request *req,
120                              __u64 version, int idx)
121 {
122         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
123         ENTRY;
124
125         if (!exp_connect_vbr(req->rq_export))
126                 RETURN(0);
127
128         LASSERT(req_is_replay(req));
129         /** VBR: version is checked always because costs nothing */
130         LASSERT(idx < PTLRPC_NUM_VERSIONS);
131         /** Sanity check for malformed buffers */
132         if (pre_ver == NULL) {
133                 CERROR("No versions in request buffer\n");
134                 cfs_spin_lock(&req->rq_export->exp_lock);
135                 req->rq_export->exp_vbr_failed = 1;
136                 cfs_spin_unlock(&req->rq_export->exp_lock);
137                 RETURN(-EOVERFLOW);
138         } else if (pre_ver[idx] != version) {
139                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
140                        pre_ver[idx], version);
141                 cfs_spin_lock(&req->rq_export->exp_lock);
142                 req->rq_export->exp_vbr_failed = 1;
143                 cfs_spin_unlock(&req->rq_export->exp_lock);
144                 RETURN(-EOVERFLOW);
145         }
146         RETURN(0);
147 }
148
149 /**
150  * Save pre-versions in reply.
151  */
152 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
153                              int idx)
154 {
155         __u64 *reply_ver;
156
157         if (!exp_connect_vbr(req->rq_export))
158                 return;
159
160         LASSERT(!req_is_replay(req));
161         LASSERT(req->rq_repmsg != NULL);
162         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
163         if (reply_ver)
164                 reply_ver[idx] = version;
165 }
166
167 /**
168  * Save enoent version, it is needed when it is obvious that object doesn't
169  * exist, e.g. child during create.
170  */
171 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
172 {
173         /* save version of file name for replay, it must be ENOENT here */
174         if (!req_is_replay(mdt_info_req(info))) {
175                 info->mti_ver[idx] = ENOENT_VERSION;
176                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
177         }
178 }
179
180 /**
181  * Get version from disk and save in reply buffer.
182  *
183  * Versions are saved in reply only during normal operations not replays.
184  */
185 void mdt_version_get_save(struct mdt_thread_info *info,
186                           struct mdt_object *mto, int idx)
187 {
188         /* don't save versions during replay */
189         if (!req_is_replay(mdt_info_req(info))) {
190                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
191                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
192         }
193 }
194
195 /**
196  * Get version from disk and check it, no save in reply.
197  */
198 int mdt_version_get_check(struct mdt_thread_info *info,
199                           struct mdt_object *mto, int idx)
200 {
201         /* only check versions during replay */
202         if (!req_is_replay(mdt_info_req(info)))
203                 return 0;
204
205         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
206         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
207 }
208
209 /**
210  * Get version from disk and check if recovery or just save.
211  */
212 int mdt_version_get_check_save(struct mdt_thread_info *info,
213                                struct mdt_object *mto, int idx)
214 {
215         int rc = 0;
216
217         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
218         if (req_is_replay(mdt_info_req(info)))
219                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
220                                        idx);
221         else
222                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
223         return rc;
224 }
225
226 /**
227  * Lookup with version checking.
228  *
229  * This checks version of 'name'. Many reint functions uses 'name' for child not
230  * FID, therefore we need to get object by name and check its version.
231  */
232 int mdt_lookup_version_check(struct mdt_thread_info *info,
233                              struct mdt_object *p, struct lu_name *lname,
234                              struct lu_fid *fid, int idx)
235 {
236         int rc, vbrc;
237
238         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
239                         &info->mti_spec);
240         /* Check version only during replay */
241         if (!req_is_replay(mdt_info_req(info)))
242                 return rc;
243
244         info->mti_ver[idx] = ENOENT_VERSION;
245         if (rc == 0) {
246                 struct mdt_object *child;
247                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
248                 if (likely(!IS_ERR(child))) {
249                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
250                         mdt_object_put(info->mti_env, child);
251                 }
252         }
253         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
254         return vbrc ? vbrc : rc;
255
256 }
257
258 /*
259  * VBR: we save three versions in reply:
260  * 0 - parent. Check that parent version is the same during replay.
261  * 1 - name. Version of 'name' if file exists with the same name or
262  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
263  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
264  * check.
265  */
266 static int mdt_md_create(struct mdt_thread_info *info)
267 {
268         struct mdt_device       *mdt = info->mti_mdt;
269         struct mdt_object       *parent;
270         struct mdt_object       *child;
271         struct mdt_lock_handle  *lh;
272         struct mdt_body         *repbody;
273         struct md_attr          *ma = &info->mti_attr;
274         struct mdt_reint_record *rr = &info->mti_rr;
275         struct lu_name          *lname;
276         int rc;
277         ENTRY;
278
279         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  (%s->"DFID") in "DFID,
280                   rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
281
282         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
283
284         lh = &info->mti_lh[MDT_LH_PARENT];
285         mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
286
287         parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
288                                       MDS_INODELOCK_UPDATE);
289         if (IS_ERR(parent))
290                 RETURN(PTR_ERR(parent));
291
292         if (mdt_object_obf(parent))
293                 GOTO(out_put_parent, rc = -EPERM);
294
295         rc = mdt_version_get_check_save(info, parent, 0);
296         if (rc)
297                 GOTO(out_put_parent, rc);
298
299         /*
300          * Check child name version during replay.
301          * During create replay a file may exist with same name.
302          */
303         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
304         rc = mdt_lookup_version_check(info, parent, lname,
305                                       &info->mti_tmp_fid1, 1);
306         if (rc == 0)
307                 GOTO(out_put_parent, rc = -EEXIST);
308
309         /* -ENOENT is expected here */
310         if (rc != -ENOENT)
311                 GOTO(out_put_parent, rc);
312
313         /* save version of file name for replay, it must be ENOENT here */
314         mdt_enoent_version_save(info, 1);
315
316         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
317         if (likely(!IS_ERR(child))) {
318                 struct md_object *next = mdt_object_child(parent);
319
320                 ma->ma_need = MA_INODE;
321                 ma->ma_valid = 0;
322                 /* capa for cross-ref will be stored here */
323                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
324                                                      &RMF_CAPA1);
325                 LASSERT(ma->ma_capa);
326
327                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
328                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
329
330                 /* Version of child will be updated on disk. */
331                 info->mti_mos = child;
332                 rc = mdt_version_get_check_save(info, child, 2);
333                 if (rc)
334                         GOTO(out_put_child, rc);
335
336                 /* Let lower layer know current lock mode. */
337                 info->mti_spec.sp_cr_mode =
338                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
339
340                 /*
341                  * Do not perform lookup sanity check. We know that name does
342                  * not exist.
343                  */
344                 info->mti_spec.sp_cr_lookup = 0;
345                 info->mti_spec.sp_feat = &dt_directory_features;
346
347                 rc = mdo_create(info->mti_env, next, lname,
348                                 mdt_object_child(child),
349                                 &info->mti_spec, ma);
350                 if (rc == 0)
351                         rc = mdt_attr_get_complex(info, child, ma);
352
353                 if (rc == 0) {
354                         /* Return fid & attr to client. */
355                         if (ma->ma_valid & MA_INODE)
356                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
357                                                    mdt_object_fid(child));
358                 }
359 out_put_child:
360                 mdt_object_put(info->mti_env, child);
361         } else {
362                 rc = PTR_ERR(child);
363         }
364         mdt_create_pack_capa(info, rc, child, repbody);
365 out_put_parent:
366         mdt_object_unlock_put(info, parent, lh, rc);
367         RETURN(rc);
368 }
369
370 /* Partial request to create object only */
371 static int mdt_md_mkobj(struct mdt_thread_info *info)
372 {
373         struct mdt_device      *mdt = info->mti_mdt;
374         struct mdt_object      *o;
375         struct mdt_body        *repbody;
376         struct md_attr         *ma = &info->mti_attr;
377         int rc;
378         ENTRY;
379
380         DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
381                   PFID(info->mti_rr.rr_fid2));
382
383         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
384
385         o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
386         if (!IS_ERR(o)) {
387                 struct md_object *next = mdt_object_child(o);
388
389                 ma->ma_need = MA_INODE;
390                 ma->ma_valid = 0;
391
392                 /*
393                  * Cross-ref create can encounter already created obj in case of
394                  * recovery, just get attr in that case.
395                  */
396                 if (mdt_object_exists(o) == 1) {
397                         rc = mdt_attr_get_complex(info, o, ma);
398                 } else {
399                         /*
400                          * Here, NO permission check for object_create,
401                          * such check has been done on the original MDS.
402                          */
403                         rc = mo_object_create(info->mti_env, next,
404                                               &info->mti_spec, ma);
405                 }
406                 if (rc == 0) {
407                         /* Return fid & attr to client. */
408                         if (ma->ma_valid & MA_INODE)
409                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
410                                                    mdt_object_fid(o));
411                 }
412                 mdt_object_put(info->mti_env, o);
413         } else
414                 rc = PTR_ERR(o);
415
416         mdt_create_pack_capa(info, rc, o, repbody);
417         RETURN(rc);
418 }
419
420 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
421                  struct md_attr *ma, int flags)
422 {
423         struct mdt_lock_handle  *lh;
424         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
425         __u64 lockpart = MDS_INODELOCK_UPDATE;
426         int rc;
427         ENTRY;
428
429         /* attr shouldn't be set on remote object */
430         LASSERT(mdt_object_exists(mo) >= 0);
431
432         lh = &info->mti_lh[MDT_LH_PARENT];
433         mdt_lock_reg_init(lh, LCK_PW);
434
435         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
436                 lockpart |= MDS_INODELOCK_LOOKUP;
437
438         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
439         if (rc != 0)
440                 RETURN(rc);
441
442         if (mdt_object_exists(mo) == 0)
443                 GOTO(out_unlock, rc = -ENOENT);
444
445         /* all attrs are packed into mti_attr in unpack_setattr */
446         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
447                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
448
449         /* This is only for set ctime when rename's source is on remote MDS. */
450         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
451                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
452
453         /* VBR: update version if attr changed are important for recovery */
454         if (do_vbr) {
455                 /* update on-disk version of changed object */
456                 info->mti_mos = mo;
457                 rc = mdt_version_get_check_save(info, mo, 0);
458                 if (rc)
459                         GOTO(out_unlock, rc);
460         }
461
462         /* all attrs are packed into mti_attr in unpack_setattr */
463         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
464         if (rc != 0)
465                 GOTO(out_unlock, rc);
466
467         EXIT;
468 out_unlock:
469         mdt_object_unlock(info, mo, lh, rc);
470         return rc;
471 }
472
473 static int mdt_reint_setattr(struct mdt_thread_info *info,
474                              struct mdt_lock_handle *lhc)
475 {
476         struct md_attr          *ma = &info->mti_attr;
477         struct mdt_reint_record *rr = &info->mti_rr;
478         struct ptlrpc_request   *req = mdt_info_req(info);
479         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
480         struct mdt_file_data    *mfd;
481         struct mdt_object       *mo;
482         struct mdt_body         *repbody;
483         int                      som_au, rc, rc2;
484         ENTRY;
485
486         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
487                   (unsigned int)ma->ma_attr.la_valid);
488
489         if (info->mti_dlm_req)
490                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
491
492         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
493         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
494         if (IS_ERR(mo))
495                 GOTO(out, rc = PTR_ERR(mo));
496
497         if (mdt_object_obf(mo))
498                 GOTO(out_put, rc = -EPERM);
499
500         /* start a log jounal handle if needed */
501         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
502                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
503                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
504                         /* Check write access for the O_TRUNC case */
505                         if (mdt_write_read(mo) < 0)
506                                 GOTO(out_put, rc = -ETXTBSY);
507                 }
508         } else if (info->mti_ioepoch &&
509                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
510                 /* Truncate case. IOEpoch is opened. */
511                 rc = mdt_write_get(mo);
512                 if (rc)
513                         GOTO(out_put, rc);
514
515                 mfd = mdt_mfd_new();
516                 if (mfd == NULL) {
517                         mdt_write_put(mo);
518                         GOTO(out_put, rc = -ENOMEM);
519                 }
520
521                 mdt_ioepoch_open(info, mo, 0);
522                 repbody->ioepoch = mo->mot_ioepoch;
523
524                 mdt_object_get(info->mti_env, mo);
525                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
526                 mfd->mfd_object = mo;
527                 mfd->mfd_xid = req->rq_xid;
528
529                 cfs_spin_lock(&med->med_open_lock);
530                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
531                 cfs_spin_unlock(&med->med_open_lock);
532                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
533         }
534
535         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
536         if (som_au) {
537                 /* SOM Attribute update case. Find the proper mfd and update
538                  * SOM attributes on the proper object. */
539                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
540                 LASSERT(info->mti_ioepoch);
541
542                 cfs_spin_lock(&med->med_open_lock);
543                 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
544                 if (mfd == NULL) {
545                         cfs_spin_unlock(&med->med_open_lock);
546                         CDEBUG(D_INODE, "no handle for file close: "
547                                "fid = "DFID": cookie = "LPX64"\n",
548                                PFID(info->mti_rr.rr_fid1),
549                                info->mti_ioepoch->handle.cookie);
550                         GOTO(out_put, rc = -ESTALE);
551                 }
552                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
553                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
554
555                 class_handle_unhash(&mfd->mfd_handle);
556                 cfs_list_del_init(&mfd->mfd_list);
557                 cfs_spin_unlock(&med->med_open_lock);
558
559                 /* Close the found mfd, update attributes. */
560                 ma->ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
561                 OBD_ALLOC_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
562                 if (ma->ma_lmm == NULL)
563                         GOTO(out_put, rc = -ENOMEM);
564
565                 mdt_mfd_close(info, mfd);
566
567                 OBD_FREE_LARGE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
568         } else {
569                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
570                 if (rc)
571                         GOTO(out_put, rc);
572         }
573
574         ma->ma_need = MA_INODE;
575         ma->ma_valid = 0;
576         rc = mdt_attr_get_complex(info, mo, ma);
577         if (rc != 0)
578                 GOTO(out_put, rc);
579
580         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
581
582         if (info->mti_mdt->mdt_opts.mo_oss_capa &&
583             info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
584             S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
585             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
586                 struct lustre_capa *capa;
587
588                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
589                 LASSERT(capa);
590                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
591                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
592                 if (rc)
593                         GOTO(out_put, rc);
594                 repbody->valid |= OBD_MD_FLOSSCAPA;
595         }
596
597         EXIT;
598 out_put:
599         mdt_object_put(info->mti_env, mo);
600 out:
601         if (rc == 0)
602                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
603
604         mdt_client_compatibility(info);
605         rc2 = mdt_fix_reply(info);
606         if (rc == 0)
607                 rc = rc2;
608         return rc;
609 }
610
611 static int mdt_reint_create(struct mdt_thread_info *info,
612                             struct mdt_lock_handle *lhc)
613 {
614         struct ptlrpc_request   *req = mdt_info_req(info);
615         int                     rc;
616         ENTRY;
617
618         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
619                 RETURN(err_serious(-ESTALE));
620
621         if (info->mti_dlm_req)
622                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
623
624         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
625         case S_IFDIR:{
626                 /* Cross-ref case. */
627                 /* TODO: we can add LPROC_MDT_CROSS for cross-ref stats */
628                 if (info->mti_cross_ref) {
629                         rc = mdt_md_mkobj(info);
630                 } else {
631                         LASSERT(info->mti_rr.rr_namelen > 0);
632                         mdt_counter_incr(req, LPROC_MDT_MKDIR);
633                         rc = mdt_md_create(info);
634                 }
635                 break;
636         }
637         case S_IFREG:
638         case S_IFLNK:
639         case S_IFCHR:
640         case S_IFBLK:
641         case S_IFIFO:
642         case S_IFSOCK:{
643                 /* Special file should stay on the same node as parent. */
644                 LASSERT(info->mti_rr.rr_namelen > 0);
645                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
646                 rc = mdt_md_create(info);
647                 break;
648         }
649         default:
650                 rc = err_serious(-EOPNOTSUPP);
651         }
652         RETURN(rc);
653 }
654
655 /*
656  * VBR: save parent version in reply and child version getting by its name.
657  * Version of child is getting and checking during its lookup. If
658  */
659 static int mdt_reint_unlink(struct mdt_thread_info *info,
660                             struct mdt_lock_handle *lhc)
661 {
662         struct mdt_reint_record *rr = &info->mti_rr;
663         struct ptlrpc_request   *req = mdt_info_req(info);
664         struct md_attr          *ma = &info->mti_attr;
665         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
666         struct mdt_object       *mp;
667         struct mdt_object       *mc;
668         struct mdt_lock_handle  *parent_lh;
669         struct mdt_lock_handle  *child_lh;
670         struct lu_name          *lname;
671         int                      rc;
672         ENTRY;
673
674         DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
675                   rr->rr_name);
676
677         if (info->mti_dlm_req)
678                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
679
680         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
681                 RETURN(err_serious(-ENOENT));
682
683         /*
684          * step 1: lock the parent. Note, this may be child in case of
685          * remote operation denoted by ->mti_cross_ref flag.
686          */
687         parent_lh = &info->mti_lh[MDT_LH_PARENT];
688         if (info->mti_cross_ref) {
689                 /*
690                  * Init reg lock for cross ref case when we need to do only
691                  * ref del locally.
692                  */
693                 mdt_lock_reg_init(parent_lh, LCK_PW);
694         } else {
695                 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
696                                   rr->rr_namelen);
697         }
698         mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
699                                   MDS_INODELOCK_UPDATE);
700         if (IS_ERR(mp)) {
701                 rc = PTR_ERR(mp);
702                 /* errors are possible here in cross-ref cases, see below */
703                 if (info->mti_cross_ref)
704                         rc = 0;
705                 GOTO(out, rc);
706         }
707
708         if (mdt_object_obf(mp))
709                 GOTO(out_unlock_parent, rc = -EPERM);
710
711         rc = mdt_version_get_check_save(info, mp, 0);
712         if (rc)
713                 GOTO(out_unlock_parent, rc);
714
715         mdt_reint_init_ma(info, ma);
716         if (!ma->ma_lmm || !ma->ma_cookie)
717                 GOTO(out_unlock_parent, rc = -EINVAL);
718
719         if (info->mti_cross_ref) {
720                 /*
721                  * Remote partial operation. It is possible that replay may
722                  * happen on parent MDT and this operation will be repeated.
723                  * Therefore the object absense is allowed case and nothing
724                  * should be done here.
725                  */
726                 if (mdt_object_exists(mp) > 0) {
727                         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
728                         rc = mo_ref_del(info->mti_env,
729                                         mdt_object_child(mp), ma);
730                         if (rc == 0)
731                                 mdt_handle_last_unlink(info, mp, ma);
732                 } else
733                         rc = 0;
734                 GOTO(out_unlock_parent, rc);
735         }
736
737         /* step 2: find & lock the child */
738         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
739         /* lookup child object along with version checking */
740         fid_zero(child_fid);
741         rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
742         if (rc != 0)
743                  GOTO(out_unlock_parent, rc);
744
745         /* We will lock the child regardless it is local or remote. No harm. */
746         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
747         if (IS_ERR(mc))
748                 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
749         child_lh = &info->mti_lh[MDT_LH_CHILD];
750         mdt_lock_reg_init(child_lh, LCK_EX);
751         rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
752                              MDT_CROSS_LOCK);
753         if (rc != 0) {
754                 mdt_object_put(info->mti_env, mc);
755                 GOTO(out_unlock_parent, rc);
756         }
757
758         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
759                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
760         /* save version when object is locked */
761         mdt_version_get_save(info, mc, 1);
762         /*
763          * Now we can only make sure we need MA_INODE, in mdd layer, will check
764          * whether need MA_LOV and MA_COOKIE.
765          */
766         ma->ma_need = MA_INODE;
767         ma->ma_valid = 0;
768         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
769         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
770                         mdt_object_child(mc), lname, ma);
771         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
772                 rc = mdt_attr_get_complex(info, mc, ma);
773         if (rc == 0)
774                 mdt_handle_last_unlink(info, mc, ma);
775
776         if (ma->ma_valid & MA_INODE) {
777                 switch (ma->ma_attr.la_mode & S_IFMT) {
778                 case S_IFDIR:
779                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
780                         break;
781                 case S_IFREG:
782                 case S_IFLNK:
783                 case S_IFCHR:
784                 case S_IFBLK:
785                 case S_IFIFO:
786                 case S_IFSOCK:
787                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
788                         break;
789                 default:
790                         LASSERTF(0, "bad file type %o unlinking\n",
791                                  ma->ma_attr.la_mode);
792                 }
793         }
794
795         EXIT;
796
797         mdt_object_unlock_put(info, mc, child_lh, rc);
798 out_unlock_parent:
799         mdt_object_unlock_put(info, mp, parent_lh, rc);
800 out:
801         return rc;
802 }
803
804 /*
805  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
806  * name.
807  */
808 static int mdt_reint_link(struct mdt_thread_info *info,
809                           struct mdt_lock_handle *lhc)
810 {
811         struct mdt_reint_record *rr = &info->mti_rr;
812         struct ptlrpc_request   *req = mdt_info_req(info);
813         struct md_attr          *ma = &info->mti_attr;
814         struct mdt_object       *ms;
815         struct mdt_object       *mp;
816         struct mdt_lock_handle  *lhs;
817         struct mdt_lock_handle  *lhp;
818         struct lu_name          *lname;
819         int rc;
820         ENTRY;
821
822         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
823                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
824
825         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
826                 RETURN(err_serious(-ENOENT));
827
828         if (info->mti_dlm_req)
829                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
830
831         if (info->mti_cross_ref) {
832                 /* MDT holding name ask us to add ref. */
833                 lhs = &info->mti_lh[MDT_LH_CHILD];
834                 mdt_lock_reg_init(lhs, LCK_EX);
835                 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
836                                           MDS_INODELOCK_UPDATE);
837                 if (IS_ERR(ms))
838                         RETURN(PTR_ERR(ms));
839
840                 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
841                 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
842                 mdt_object_unlock_put(info, ms, lhs, rc);
843                 RETURN(rc);
844         }
845
846         /* Invalid case so return error immediately instead of
847          * processing it */
848         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
849                 RETURN(-EPERM);
850
851         /* step 1: find & lock the target parent dir */
852         lhp = &info->mti_lh[MDT_LH_PARENT];
853         mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
854                           rr->rr_namelen);
855         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
856                                   MDS_INODELOCK_UPDATE);
857         if (IS_ERR(mp))
858                 RETURN(PTR_ERR(mp));
859
860         if (mdt_object_obf(mp))
861                 GOTO(out_unlock_parent, rc = -EPERM);
862
863         rc = mdt_version_get_check_save(info, mp, 0);
864         if (rc)
865                 GOTO(out_unlock_parent, rc);
866
867         /* step 2: find & lock the source */
868         lhs = &info->mti_lh[MDT_LH_CHILD];
869         mdt_lock_reg_init(lhs, LCK_EX);
870
871         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
872         if (IS_ERR(ms))
873                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
874
875         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
876                             MDT_CROSS_LOCK);
877         if (rc != 0) {
878                 mdt_object_put(info->mti_env, ms);
879                 GOTO(out_unlock_parent, rc);
880         }
881
882         /* step 3: link it */
883         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
884                        OBD_FAIL_MDS_REINT_LINK_WRITE);
885
886         info->mti_mos = ms;
887         rc = mdt_version_get_check_save(info, ms, 1);
888         if (rc)
889                 GOTO(out_unlock_child, rc);
890
891         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
892         /** check target version by name during replay */
893         rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
894         if (rc != 0 && rc != -ENOENT)
895                 GOTO(out_unlock_child, rc);
896         /* save version of file name for replay, it must be ENOENT here */
897         if (!req_is_replay(mdt_info_req(info))) {
898                 info->mti_ver[2] = ENOENT_VERSION;
899                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
900         }
901
902         rc = mdo_link(info->mti_env, mdt_object_child(mp),
903                       mdt_object_child(ms), lname, ma);
904
905         if (rc == 0)
906                 mdt_counter_incr(req, LPROC_MDT_LINK);
907
908         EXIT;
909 out_unlock_child:
910         mdt_object_unlock_put(info, ms, lhs, rc);
911 out_unlock_parent:
912         mdt_object_unlock_put(info, mp, lhp, rc);
913         return rc;
914 }
915 /**
916  * lock the part of the directory according to the hash of the name
917  * (lh->mlh_pdo_hash) in parallel directory lock.
918  */
919 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
920                        struct mdt_lock_handle *lh,
921                        struct mdt_object *obj, __u64 ibits)
922 {
923         struct ldlm_res_id *res_id = &info->mti_res_id;
924         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
925         ldlm_policy_data_t *policy = &info->mti_policy;
926         int rc;
927
928         /*
929          * Finish res_id initializing by name hash marking part of
930          * directory which is taking modification.
931          */
932         LASSERT(lh->mlh_pdo_hash != 0);
933         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
934         memset(policy, 0, sizeof(*policy));
935         policy->l_inodebits.bits = ibits;
936         /*
937          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
938          * going to be sent to client. If it is - mdt_intent_policy() path will
939          * fix it up and turn FL_LOCAL flag off.
940          */
941         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
942                           res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
943                           &info->mti_exp->exp_handle.h_cookie);
944         return rc;
945 }
946
947 /* partial operation for rename */
948 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
949 {
950         struct mdt_reint_record *rr = &info->mti_rr;
951         struct ptlrpc_request   *req = mdt_info_req(info);
952         struct md_attr          *ma = &info->mti_attr;
953         struct mdt_object       *mtgtdir;
954         struct mdt_object       *mtgt = NULL;
955         struct mdt_lock_handle  *lh_tgtdir;
956         struct mdt_lock_handle  *lh_tgt = NULL;
957         struct lu_fid           *tgt_fid = &info->mti_tmp_fid1;
958         struct lu_name          *lname;
959         int                      rc;
960         ENTRY;
961
962         DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
963                   rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
964
965         /* step 1: lookup & lock the tgt dir. */
966         lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
967         mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
968                           rr->rr_tgtlen);
969         mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
970                                        MDS_INODELOCK_UPDATE);
971         if (IS_ERR(mtgtdir))
972                 RETURN(PTR_ERR(mtgtdir));
973
974         /* step 2: find & lock the target object if exists. */
975         mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
976         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
977         rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
978                         lname, tgt_fid, &info->mti_spec);
979         if (rc != 0 && rc != -ENOENT) {
980                 GOTO(out_unlock_tgtdir, rc);
981         } else if (rc == 0) {
982                 /*
983                  * In case of replay that name can be already inserted, check
984                  * that and do nothing if so.
985                  */
986                 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
987                         GOTO(out_unlock_tgtdir, rc);
988
989                 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
990                 mdt_lock_reg_init(lh_tgt, LCK_EX);
991
992                 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
993                                             MDS_INODELOCK_LOOKUP);
994                 if (IS_ERR(mtgt))
995                         GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
996
997                 mdt_reint_init_ma(info, ma);
998                 if (!ma->ma_lmm || !ma->ma_cookie)
999                         GOTO(out_unlock_tgt, rc = -EINVAL);
1000
1001                 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
1002                                     mdt_object_child(mtgt), rr->rr_fid2,
1003                                     lname, ma);
1004         } else /* -ENOENT */ {
1005                 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
1006                                      lname, rr->rr_fid2, ma);
1007         }
1008
1009         /* handle last link of tgt object */
1010         if (rc == 0 && mtgt)
1011                 mdt_handle_last_unlink(info, mtgt, ma);
1012
1013         EXIT;
1014 out_unlock_tgt:
1015         if (mtgt)
1016                 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
1017 out_unlock_tgtdir:
1018         mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
1019         return rc;
1020 }
1021
1022 static int mdt_rename_lock(struct mdt_thread_info *info,
1023                            struct lustre_handle *lh)
1024 {
1025         struct ldlm_namespace *ns     = info->mti_mdt->mdt_namespace;
1026         ldlm_policy_data_t    *policy = &info->mti_policy;
1027         struct ldlm_res_id    *res_id = &info->mti_res_id;
1028         struct md_site        *ms;
1029         int rc;
1030         ENTRY;
1031
1032         ms = mdt_md_site(info->mti_mdt);
1033         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1034
1035         memset(policy, 0, sizeof *policy);
1036         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1037
1038         if (ms->ms_control_exp == NULL) {
1039                 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1040
1041                 /*
1042                  * Current node is controller, that is mdt0, where we should
1043                  * take BFL lock.
1044                  */
1045                 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1046                                             LCK_EX, &flags, ldlm_blocking_ast,
1047                                             ldlm_completion_ast, NULL, NULL, 0,
1048                                             &info->mti_exp->exp_handle.h_cookie,
1049                                             lh);
1050         } else {
1051                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
1052                      ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1053                 int flags = 0;
1054
1055                 /*
1056                  * This is the case mdt0 is remote node, issue DLM lock like
1057                  * other clients.
1058                  */
1059                 rc = ldlm_cli_enqueue(ms->ms_control_exp, NULL, &einfo, res_id,
1060                                       policy, &flags, NULL, 0, lh, 0);
1061         }
1062
1063         RETURN(rc);
1064 }
1065
1066 static void mdt_rename_unlock(struct lustre_handle *lh)
1067 {
1068         ENTRY;
1069         LASSERT(lustre_handle_is_used(lh));
1070         ldlm_lock_decref(lh, LCK_EX);
1071         EXIT;
1072 }
1073
1074 /*
1075  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1076  * target. Source should not be ancestor of target dir. May be other rename
1077  * checks can be moved here later.
1078  */
1079 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1080 {
1081         struct mdt_reint_record *rr = &info->mti_rr;
1082         struct lu_fid dst_fid = *rr->rr_fid2;
1083         struct mdt_object *dst;
1084         int rc = 0;
1085         ENTRY;
1086
1087         do {
1088                 LASSERT(fid_is_sane(&dst_fid));
1089                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1090                 if (!IS_ERR(dst)) {
1091                         rc = mdo_is_subdir(info->mti_env,
1092                                            mdt_object_child(dst), fid,
1093                                            &dst_fid);
1094                         mdt_object_put(info->mti_env, dst);
1095                         if (rc != -EREMOTE && rc < 0) {
1096                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1097                         } else {
1098                                 /* check the found fid */
1099                                 if (lu_fid_eq(&dst_fid, fid))
1100                                         rc = -EINVAL;
1101                         }
1102                 } else {
1103                         rc = PTR_ERR(dst);
1104                 }
1105         } while (rc == -EREMOTE);
1106
1107         RETURN(rc);
1108 }
1109
1110 /*
1111  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1112  * 2 - src child; 3 - tgt child.
1113  * Update on disk version of src child.
1114  */
1115 static int mdt_reint_rename(struct mdt_thread_info *info,
1116                             struct mdt_lock_handle *lhc)
1117 {
1118         struct mdt_reint_record *rr = &info->mti_rr;
1119         struct md_attr          *ma = &info->mti_attr;
1120         struct ptlrpc_request   *req = mdt_info_req(info);
1121         struct mdt_object       *msrcdir;
1122         struct mdt_object       *mtgtdir;
1123         struct mdt_object       *mold;
1124         struct mdt_object       *mnew = NULL;
1125         struct mdt_lock_handle  *lh_srcdirp;
1126         struct mdt_lock_handle  *lh_tgtdirp;
1127         struct mdt_lock_handle  *lh_oldp;
1128         struct mdt_lock_handle  *lh_newp;
1129         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1130         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1131         struct lustre_handle     rename_lh = { 0 };
1132         struct lu_name           slname = { 0 };
1133         struct lu_name          *lname;
1134         int                      rc;
1135         ENTRY;
1136
1137         if (info->mti_dlm_req)
1138                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1139
1140         if (info->mti_cross_ref) {
1141                 rc = mdt_reint_rename_tgt(info);
1142                 RETURN(rc);
1143         }
1144
1145         DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1146                   PFID(rr->rr_fid1), rr->rr_name,
1147                   PFID(rr->rr_fid2), rr->rr_tgt);
1148
1149         rc = mdt_rename_lock(info, &rename_lh);
1150         if (rc) {
1151                 CERROR("Can't lock FS for rename, rc %d\n", rc);
1152                 RETURN(rc);
1153         }
1154
1155         lh_newp = &info->mti_lh[MDT_LH_NEW];
1156
1157         /* step 1: lock the source dir. */
1158         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1159         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1160                           rr->rr_namelen);
1161         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1162                                        MDS_INODELOCK_UPDATE);
1163         if (IS_ERR(msrcdir))
1164                 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1165
1166         if (mdt_object_obf(msrcdir))
1167                 GOTO(out_unlock_source, rc = -EPERM);
1168
1169         rc = mdt_version_get_check_save(info, msrcdir, 0);
1170         if (rc)
1171                 GOTO(out_unlock_source, rc);
1172
1173         /* step 2: find & lock the target dir. */
1174         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1175         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1176                           rr->rr_tgtlen);
1177         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1178                 mdt_object_get(info->mti_env, msrcdir);
1179                 mtgtdir = msrcdir;
1180                 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1181                          rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1182                                                  MDS_INODELOCK_UPDATE);
1183                          if (rc)
1184                                  GOTO(out_unlock_source, rc);
1185                          OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1186                 }
1187         } else {
1188                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1189                                           rr->rr_fid2);
1190                 if (IS_ERR(mtgtdir))
1191                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1192
1193                 if (mdt_object_obf(mtgtdir))
1194                         GOTO(out_put_target, rc = -EPERM);
1195
1196                 /* check early, the real version will be saved after locking */
1197                 rc = mdt_version_get_check(info, mtgtdir, 1);
1198                 if (rc)
1199                         GOTO(out_put_target, rc);
1200
1201                 rc = mdt_object_exists(mtgtdir);
1202                 if (rc == 0) {
1203                         GOTO(out_put_target, rc = -ESTALE);
1204                 } else if (rc > 0) {
1205                         /* we lock the target dir if it is local */
1206                         rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1207                                              MDS_INODELOCK_UPDATE,
1208                                              MDT_LOCAL_LOCK);
1209                         if (rc != 0)
1210                                 GOTO(out_put_target, rc);
1211                         /* get and save correct version after locking */
1212                         mdt_version_get_save(info, mtgtdir, 1);
1213                 }
1214         }
1215
1216         /* step 3: find & lock the old object. */
1217         lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1218         mdt_name_copy(&slname, lname);
1219         fid_zero(old_fid);
1220         rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1221         if (rc != 0)
1222                 GOTO(out_unlock_target, rc);
1223
1224         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1225                 GOTO(out_unlock_target, rc = -EINVAL);
1226
1227         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1228         if (IS_ERR(mold))
1229                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1230
1231         if (mdt_object_obf(mold)) {
1232                 mdt_object_put(info->mti_env, mold);
1233                 GOTO(out_unlock_target, rc = -EPERM);
1234         }
1235
1236         lh_oldp = &info->mti_lh[MDT_LH_OLD];
1237         mdt_lock_reg_init(lh_oldp, LCK_EX);
1238         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1239                              MDT_CROSS_LOCK);
1240         if (rc != 0) {
1241                 mdt_object_put(info->mti_env, mold);
1242                 GOTO(out_unlock_target, rc);
1243         }
1244
1245         info->mti_mos = mold;
1246         /* save version after locking */
1247         mdt_version_get_save(info, mold, 2);
1248         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1249
1250         /* step 4: find & lock the new object. */
1251         /* new target object may not exist now */
1252         lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1253         /* lookup with version checking */
1254         fid_zero(new_fid);
1255         rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1256         if (rc == 0) {
1257                 /* the new_fid should have been filled at this moment */
1258                 if (lu_fid_eq(old_fid, new_fid))
1259                        GOTO(out_unlock_old, rc);
1260
1261                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1262                     lu_fid_eq(new_fid, rr->rr_fid2))
1263                         GOTO(out_unlock_old, rc = -EINVAL);
1264
1265                 mdt_lock_reg_init(lh_newp, LCK_EX);
1266                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1267                 if (IS_ERR(mnew))
1268                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1269
1270                 if (mdt_object_obf(mnew)) {
1271                         mdt_object_put(info->mti_env, mnew);
1272                         GOTO(out_unlock_old, rc = -EPERM);
1273                 }
1274
1275                 rc = mdt_object_lock(info, mnew, lh_newp,
1276                                      MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1277                 if (rc != 0) {
1278                         mdt_object_put(info->mti_env, mnew);
1279                         GOTO(out_unlock_old, rc);
1280                 }
1281                 /* get and save version after locking */
1282                 mdt_version_get_save(info, mnew, 3);
1283                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1284         } else if (rc != -EREMOTE && rc != -ENOENT) {
1285                 GOTO(out_unlock_old, rc);
1286         } else {
1287                 mdt_enoent_version_save(info, 3);
1288         }
1289
1290         /* step 5: rename it */
1291         mdt_reint_init_ma(info, ma);
1292         if (!ma->ma_lmm || !ma->ma_cookie)
1293                 GOTO(out_unlock_new, rc = -EINVAL);
1294
1295         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1296                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1297
1298
1299         /* Check if @dst is subdir of @src. */
1300         rc = mdt_rename_sanity(info, old_fid);
1301         if (rc)
1302                 GOTO(out_unlock_new, rc);
1303
1304         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1305                         mdt_object_child(mtgtdir), old_fid, &slname,
1306                         (mnew ? mdt_object_child(mnew) : NULL),
1307                         lname, ma);
1308
1309         /* handle last link of tgt object */
1310         if (rc == 0) {
1311                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1312                 if (mnew)
1313                         mdt_handle_last_unlink(info, mnew, ma);
1314
1315                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1316                                          msrcdir, mtgtdir);
1317         }
1318
1319         EXIT;
1320 out_unlock_new:
1321         if (mnew)
1322                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1323 out_unlock_old:
1324         mdt_object_unlock_put(info, mold, lh_oldp, rc);
1325 out_unlock_target:
1326         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1327 out_put_target:
1328         mdt_object_put(info->mti_env, mtgtdir);
1329 out_unlock_source:
1330         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1331 out_rename_lock:
1332         mdt_rename_unlock(&rename_lh);
1333         return rc;
1334 }
1335
1336 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1337                            struct mdt_lock_handle *lhc);
1338
1339 static mdt_reinter reinters[REINT_MAX] = {
1340         [REINT_SETATTR]  = mdt_reint_setattr,
1341         [REINT_CREATE]   = mdt_reint_create,
1342         [REINT_LINK]     = mdt_reint_link,
1343         [REINT_UNLINK]   = mdt_reint_unlink,
1344         [REINT_RENAME]   = mdt_reint_rename,
1345         [REINT_OPEN]     = mdt_reint_open,
1346         [REINT_SETXATTR] = mdt_reint_setxattr
1347 };
1348
1349 int mdt_reint_rec(struct mdt_thread_info *info,
1350                   struct mdt_lock_handle *lhc)
1351 {
1352         int rc;
1353         ENTRY;
1354
1355         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1356
1357         RETURN(rc);
1358 }