Whamcloud - gitweb
LU-3529 lod: create striped directory
[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, 2013, Intel Corporation.
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
51 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
52                                      struct md_attr *ma)
53 {
54         ma->ma_need = MA_INODE;
55         ma->ma_valid = 0;
56 }
57
58 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
59                                 struct mdt_object *object,
60                                 struct mdt_body *repbody)
61 {
62         ENTRY;
63
64         /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
65          * we won't go to below*/
66         if (repbody->valid & OBD_MD_FLMDSCAPA)
67                 RETURN(rc);
68
69         if (rc == 0 && info->mti_mdt->mdt_lut.lut_mds_capa &&
70             exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
71                 struct lustre_capa *capa;
72
73                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
74                 LASSERT(capa);
75                 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
76                 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
77                                  0);
78                 if (rc == 0)
79                         repbody->valid |= OBD_MD_FLMDSCAPA;
80         }
81
82         RETURN(rc);
83 }
84
85 /**
86  * Get version of object by fid.
87  *
88  * Return real version or ENOENT_VERSION if object doesn't exist
89  */
90 static void mdt_obj_version_get(struct mdt_thread_info *info,
91                                 struct mdt_object *o, __u64 *version)
92 {
93         LASSERT(o);
94         if (mdt_object_exists(o) && !mdt_object_remote(o) &&
95             !fid_is_obf(mdt_object_fid(o)))
96                 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
97         else
98                 *version = ENOENT_VERSION;
99         CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
100                PFID(mdt_object_fid(o)), *version);
101 }
102
103 /**
104  * Check version is correct.
105  *
106  * Should be called only during replay.
107  */
108 static int mdt_version_check(struct ptlrpc_request *req,
109                              __u64 version, int idx)
110 {
111         __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
112         ENTRY;
113
114         if (!exp_connect_vbr(req->rq_export))
115                 RETURN(0);
116
117         LASSERT(req_is_replay(req));
118         /** VBR: version is checked always because costs nothing */
119         LASSERT(idx < PTLRPC_NUM_VERSIONS);
120         /** Sanity check for malformed buffers */
121         if (pre_ver == 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_ver[idx] != version) {
128                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
129                        pre_ver[idx], 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         RETURN(0);
136 }
137
138 /**
139  * Save pre-versions in reply.
140  */
141 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
142                              int idx)
143 {
144         __u64 *reply_ver;
145
146         if (!exp_connect_vbr(req->rq_export))
147                 return;
148
149         LASSERT(!req_is_replay(req));
150         LASSERT(req->rq_repmsg != NULL);
151         reply_ver = lustre_msg_get_versions(req->rq_repmsg);
152         if (reply_ver)
153                 reply_ver[idx] = version;
154 }
155
156 /**
157  * Save enoent version, it is needed when it is obvious that object doesn't
158  * exist, e.g. child during create.
159  */
160 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
161 {
162         /* save version of file name for replay, it must be ENOENT here */
163         if (!req_is_replay(mdt_info_req(info))) {
164                 info->mti_ver[idx] = ENOENT_VERSION;
165                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
166         }
167 }
168
169 /**
170  * Get version from disk and save in reply buffer.
171  *
172  * Versions are saved in reply only during normal operations not replays.
173  */
174 void mdt_version_get_save(struct mdt_thread_info *info,
175                           struct mdt_object *mto, int idx)
176 {
177         /* don't save versions during replay */
178         if (!req_is_replay(mdt_info_req(info))) {
179                 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
180                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
181         }
182 }
183
184 /**
185  * Get version from disk and check it, no save in reply.
186  */
187 int mdt_version_get_check(struct mdt_thread_info *info,
188                           struct mdt_object *mto, int idx)
189 {
190         /* only check versions during replay */
191         if (!req_is_replay(mdt_info_req(info)))
192                 return 0;
193
194         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
195         return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
196 }
197
198 /**
199  * Get version from disk and check if recovery or just save.
200  */
201 int mdt_version_get_check_save(struct mdt_thread_info *info,
202                                struct mdt_object *mto, int idx)
203 {
204         int rc = 0;
205
206         mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
207         if (req_is_replay(mdt_info_req(info)))
208                 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
209                                        idx);
210         else
211                 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
212         return rc;
213 }
214
215 /**
216  * Lookup with version checking.
217  *
218  * This checks version of 'name'. Many reint functions uses 'name' for child not
219  * FID, therefore we need to get object by name and check its version.
220  */
221 int mdt_lookup_version_check(struct mdt_thread_info *info,
222                              struct mdt_object *p, const struct lu_name *lname,
223                              struct lu_fid *fid, int idx)
224 {
225         int rc, vbrc;
226
227         rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
228                         &info->mti_spec);
229         /* Check version only during replay */
230         if (!req_is_replay(mdt_info_req(info)))
231                 return rc;
232
233         info->mti_ver[idx] = ENOENT_VERSION;
234         if (rc == 0) {
235                 struct mdt_object *child;
236                 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
237                 if (likely(!IS_ERR(child))) {
238                         mdt_obj_version_get(info, child, &info->mti_ver[idx]);
239                         mdt_object_put(info->mti_env, child);
240                 }
241         }
242         vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
243         return vbrc ? vbrc : rc;
244
245 }
246
247 /*
248  * VBR: we save three versions in reply:
249  * 0 - parent. Check that parent version is the same during replay.
250  * 1 - name. Version of 'name' if file exists with the same name or
251  * ENOENT_VERSION, it is needed because file may appear due to missed replays.
252  * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
253  * check.
254  */
255 static int mdt_md_create(struct mdt_thread_info *info)
256 {
257         struct mdt_device       *mdt = info->mti_mdt;
258         struct mdt_object       *parent;
259         struct mdt_object       *child;
260         struct mdt_lock_handle  *lh;
261         struct mdt_body         *repbody;
262         struct md_attr          *ma = &info->mti_attr;
263         struct mdt_reint_record *rr = &info->mti_rr;
264         int rc;
265         ENTRY;
266
267         DEBUG_REQ(D_INODE, mdt_info_req(info), "Create  ("DNAME"->"DFID") "
268                   "in "DFID,
269                   PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
270
271         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
272                 RETURN(-EPERM);
273
274         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
275
276         lh = &info->mti_lh[MDT_LH_PARENT];
277         mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
278
279         parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
280         if (IS_ERR(parent))
281                 RETURN(PTR_ERR(parent));
282
283         if (!mdt_object_exists(parent))
284                 GOTO(put_parent, rc = -ENOENT);
285
286         lh = &info->mti_lh[MDT_LH_PARENT];
287         if (mdt_object_remote(parent)) {
288                 mdt_lock_reg_init(lh, LCK_EX);
289                 rc = mdt_remote_object_lock(info, parent, &lh->mlh_rreg_lh,
290                                             lh->mlh_rreg_mode,
291                                             MDS_INODELOCK_UPDATE);
292                 if (rc != ELDLM_OK)
293                         GOTO(put_parent, rc);
294
295         } else {
296                 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
297                 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE,
298                                      MDT_LOCAL_LOCK);
299                 if (rc)
300                         GOTO(put_parent, rc);
301
302                 rc = mdt_version_get_check_save(info, parent, 0);
303                 if (rc)
304                         GOTO(unlock_parent, rc);
305         }
306
307         /*
308          * Check child name version during replay.
309          * During create replay a file may exist with same name.
310          */
311         rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
312                                       &info->mti_tmp_fid1, 1);
313         if (rc == 0)
314                 GOTO(unlock_parent, rc = -EEXIST);
315
316         /* -ENOENT is expected here */
317         if (rc != -ENOENT)
318                 GOTO(unlock_parent, rc);
319
320         /* save version of file name for replay, it must be ENOENT here */
321         mdt_enoent_version_save(info, 1);
322
323         child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
324         if (likely(!IS_ERR(child))) {
325                 struct md_object *next = mdt_object_child(parent);
326
327                 if (mdt_object_remote(child)) {
328                         struct seq_server_site *ss;
329                         struct lu_ucred *uc  = mdt_ucred(info);
330
331                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
332                                 if (uc->uc_gid !=
333                                     mdt->mdt_enable_remote_dir_gid &&
334                                     mdt->mdt_enable_remote_dir_gid != -1) {
335                                         CERROR("%s: Creating remote dir is only"
336                                                " permitted for administrator or"
337                                                " set mdt_enable_remote_dir_gid:"
338                                                " rc = %d\n",
339                                                 mdt_obd_name(mdt), -EPERM);
340                                         GOTO(out_put_child, rc = -EPERM);
341                                 }
342                         }
343
344                         ss = mdt_seq_site(mdt);
345                         if (ss->ss_node_id != 0 &&
346                             mdt->mdt_enable_remote_dir == 0) {
347                                 CERROR("%s: remote dir is only permitted on"
348                                        " MDT0 or set_param"
349                                        " mdt.*.enable_remote_dir=1\n",
350                                        mdt_obd_name(mdt));
351                                 GOTO(out_put_child, rc = -EPERM);
352                         }
353                         if (!mdt_is_dne_client(mdt_info_req(info)->rq_export)) {
354                                 /* Return -EIO for old client */
355                                 GOTO(out_put_child, rc = -EIO);
356                         }
357
358                 }
359                 ma->ma_need = MA_INODE;
360                 ma->ma_valid = 0;
361                 /* capa for cross-ref will be stored here */
362                 ma->ma_capa = req_capsule_server_get(info->mti_pill,
363                                                      &RMF_CAPA1);
364                 LASSERT(ma->ma_capa);
365
366                 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
367                                OBD_FAIL_MDS_REINT_CREATE_WRITE);
368
369                 /* Version of child will be updated on disk. */
370                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
371                 rc = mdt_version_get_check_save(info, child, 2);
372                 if (rc)
373                         GOTO(out_put_child, rc);
374
375                 /* Let lower layer know current lock mode. */
376                 info->mti_spec.sp_cr_mode =
377                         mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
378
379                 /*
380                  * Do not perform lookup sanity check. We know that name does
381                  * not exist.
382                  */
383                 info->mti_spec.sp_cr_lookup = 0;
384                 info->mti_spec.sp_feat = &dt_directory_features;
385
386                 rc = mdo_create(info->mti_env, next, &rr->rr_name,
387                                 mdt_object_child(child), &info->mti_spec, ma);
388                 if (rc == 0)
389                         rc = mdt_attr_get_complex(info, child, ma);
390
391                 if (rc == 0) {
392                         /* Return fid & attr to client. */
393                         if (ma->ma_valid & MA_INODE)
394                                 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
395                                                    mdt_object_fid(child));
396                 }
397 out_put_child:
398                 mdt_object_put(info->mti_env, child);
399         } else {
400                 rc = PTR_ERR(child);
401         }
402         mdt_create_pack_capa(info, rc, child, repbody);
403 unlock_parent:
404         mdt_object_unlock(info, parent, lh, rc);
405 put_parent:
406         mdt_object_put(info->mti_env, parent);
407         RETURN(rc);
408 }
409
410 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
411                  struct md_attr *ma, int flags)
412 {
413         struct mdt_lock_handle  *lh;
414         int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
415         __u64 lockpart = MDS_INODELOCK_UPDATE;
416         int rc;
417         ENTRY;
418
419         /* attr shouldn't be set on remote object */
420         LASSERT(!mdt_object_remote(mo));
421
422         lh = &info->mti_lh[MDT_LH_PARENT];
423         mdt_lock_reg_init(lh, LCK_PW);
424
425         /* Even though the new MDT will grant PERM lock to the old
426          * client, but the old client will almost ignore that during
427          * So it needs to revoke both LOOKUP and PERM lock here, so
428          * both new and old client can cancel the dcache */
429         if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
430                 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
431
432         rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
433         if (rc != 0)
434                 RETURN(rc);
435
436         if (mdt_object_exists(mo) == 0)
437                 GOTO(out_unlock, rc = -ENOENT);
438
439         /* all attrs are packed into mti_attr in unpack_setattr */
440         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
441                        OBD_FAIL_MDS_REINT_SETATTR_WRITE);
442
443         /* This is only for set ctime when rename's source is on remote MDS. */
444         if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
445                 ma->ma_attr_flags |= MDS_VTX_BYPASS;
446
447         /* VBR: update version if attr changed are important for recovery */
448         if (do_vbr) {
449                 /* update on-disk version of changed object */
450                 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
451                 rc = mdt_version_get_check_save(info, mo, 0);
452                 if (rc)
453                         GOTO(out_unlock, rc);
454         }
455
456         /* Ensure constant striping during chown(). See LU-2789. */
457         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
458                 mutex_lock(&mo->mot_lov_mutex);
459
460         /* all attrs are packed into mti_attr in unpack_setattr */
461         rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
462
463         if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
464                 mutex_unlock(&mo->mot_lov_mutex);
465
466         if (rc != 0)
467                 GOTO(out_unlock, rc);
468
469         EXIT;
470 out_unlock:
471         mdt_object_unlock(info, mo, lh, rc);
472         return rc;
473 }
474
475 /**
476  * Check HSM flags and add HS_DIRTY flag if relevant.
477  *
478  * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
479  * and is not RELEASED.
480  */
481 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
482                         struct md_attr *ma)
483 {
484         int rc;
485         ENTRY;
486
487         /* If the file was modified, add the dirty flag */
488         ma->ma_need = MA_HSM;
489         rc = mdt_attr_get_complex(info, mo, ma);
490         if (rc) {
491                 CERROR("file attribute read error for "DFID": %d.\n",
492                         PFID(mdt_object_fid(mo)), rc);
493                 RETURN(rc);
494         }
495
496         /* If an up2date copy exists in the backend, add dirty flag */
497         if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
498             && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
499                 struct mdt_lock_handle  *lh = &info->mti_lh[MDT_LH_CHILD];
500
501                 ma->ma_hsm.mh_flags |= HS_DIRTY;
502
503                 mdt_lock_reg_init(lh, LCK_PW);
504                 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
505                                      MDT_LOCAL_LOCK);
506                 if (rc != 0)
507                         RETURN(rc);
508
509                 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
510                 if (rc)
511                         CERROR("file attribute change error for "DFID": %d\n",
512                                 PFID(mdt_object_fid(mo)), rc);
513                 mdt_object_unlock(info, mo, lh, rc);
514         }
515
516         RETURN(rc);
517 }
518
519 static int mdt_reint_setattr(struct mdt_thread_info *info,
520                              struct mdt_lock_handle *lhc)
521 {
522         struct md_attr          *ma = &info->mti_attr;
523         struct mdt_reint_record *rr = &info->mti_rr;
524         struct ptlrpc_request   *req = mdt_info_req(info);
525         struct mdt_export_data  *med = &req->rq_export->exp_mdt_data;
526         struct mdt_file_data    *mfd;
527         struct mdt_object       *mo;
528         struct mdt_body         *repbody;
529         int                      som_au, rc, rc2;
530         ENTRY;
531
532         DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
533                   (unsigned int)ma->ma_attr.la_valid);
534
535         if (info->mti_dlm_req)
536                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
537
538         repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
539         mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
540         if (IS_ERR(mo))
541                 GOTO(out, rc = PTR_ERR(mo));
542
543         /* start a log jounal handle if needed */
544         if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
545                 if ((ma->ma_attr.la_valid & LA_SIZE) ||
546                     (rr->rr_flags & MRF_OPEN_TRUNC)) {
547                         /* Check write access for the O_TRUNC case */
548                         if (mdt_write_read(mo) < 0)
549                                 GOTO(out_put, rc = -ETXTBSY);
550                 }
551         } else if (info->mti_ioepoch &&
552                    (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
553                 /* Truncate case. IOEpoch is opened. */
554                 rc = mdt_write_get(mo);
555                 if (rc)
556                         GOTO(out_put, rc);
557
558                 mfd = mdt_mfd_new(med);
559                 if (mfd == NULL) {
560                         mdt_write_put(mo);
561                         GOTO(out_put, rc = -ENOMEM);
562                 }
563
564                 mdt_ioepoch_open(info, mo, 0);
565                 repbody->ioepoch = mo->mot_ioepoch;
566
567                 mdt_object_get(info->mti_env, mo);
568                 mdt_mfd_set_mode(mfd, MDS_FMODE_TRUNC);
569                 mfd->mfd_object = mo;
570                 mfd->mfd_xid = req->rq_xid;
571
572                 spin_lock(&med->med_open_lock);
573                 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
574                 spin_unlock(&med->med_open_lock);
575                 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
576         }
577
578         som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
579         if (som_au) {
580                 /* SOM Attribute update case. Find the proper mfd and update
581                  * SOM attributes on the proper object. */
582                 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
583                 LASSERT(info->mti_ioepoch);
584
585                 spin_lock(&med->med_open_lock);
586                 mfd = mdt_handle2mfd(med, &info->mti_ioepoch->handle,
587                                      req_is_replay(req));
588                 if (mfd == NULL) {
589                         spin_unlock(&med->med_open_lock);
590                         CDEBUG(D_INODE, "no handle for file close: "
591                                "fid = "DFID": cookie = "LPX64"\n",
592                                PFID(info->mti_rr.rr_fid1),
593                                info->mti_ioepoch->handle.cookie);
594                         GOTO(out_put, rc = -ESTALE);
595                 }
596                 LASSERT(mfd->mfd_mode == MDS_FMODE_SOM);
597                 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
598
599                 class_handle_unhash(&mfd->mfd_handle);
600                 cfs_list_del_init(&mfd->mfd_list);
601                 spin_unlock(&med->med_open_lock);
602
603                 mdt_mfd_close(info, mfd);
604         } else if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
605                 LASSERT((ma->ma_valid & MA_LOV) == 0);
606                 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
607                 if (rc)
608                         GOTO(out_put, rc);
609         } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
610                 struct lu_buf *buf  = &info->mti_buf;
611                 LASSERT(ma->ma_attr.la_valid == 0);
612                 buf->lb_buf = ma->ma_lmm;
613                 buf->lb_len = ma->ma_lmm_size;
614                 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
615                                   buf, XATTR_NAME_LOV, 0);
616                 if (rc)
617                         GOTO(out_put, rc);
618         } else
619                 LBUG();
620
621         /* If file data is modified, add the dirty flag */
622         if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
623                 rc = mdt_add_dirty_flag(info, mo, ma);
624
625         ma->ma_need = MA_INODE;
626         ma->ma_valid = 0;
627         rc = mdt_attr_get_complex(info, mo, ma);
628         if (rc != 0)
629                 GOTO(out_put, rc);
630
631         mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
632
633         if (info->mti_mdt->mdt_lut.lut_oss_capa &&
634             exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
635             S_ISREG(lu_object_attr(&mo->mot_obj)) &&
636             (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
637                 struct lustre_capa *capa;
638
639                 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
640                 LASSERT(capa);
641                 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
642                 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
643                 if (rc)
644                         GOTO(out_put, rc);
645                 repbody->valid |= OBD_MD_FLOSSCAPA;
646         }
647
648         EXIT;
649 out_put:
650         mdt_object_put(info->mti_env, mo);
651 out:
652         if (rc == 0)
653                 mdt_counter_incr(req, LPROC_MDT_SETATTR);
654
655         mdt_client_compatibility(info);
656         rc2 = mdt_fix_reply(info);
657         if (rc == 0)
658                 rc = rc2;
659         return rc;
660 }
661
662 static int mdt_reint_create(struct mdt_thread_info *info,
663                             struct mdt_lock_handle *lhc)
664 {
665         struct ptlrpc_request   *req = mdt_info_req(info);
666         int                     rc;
667         ENTRY;
668
669         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
670                 RETURN(err_serious(-ESTALE));
671
672         if (info->mti_dlm_req)
673                 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
674
675         if (!lu_name_is_valid(&info->mti_rr.rr_name))
676                 RETURN(-EPROTO);
677
678         switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
679         case S_IFDIR:
680                 mdt_counter_incr(req, LPROC_MDT_MKDIR);
681                 break;
682         case S_IFREG:
683         case S_IFLNK:
684         case S_IFCHR:
685         case S_IFBLK:
686         case S_IFIFO:
687         case S_IFSOCK:
688                 /* Special file should stay on the same node as parent. */
689                 mdt_counter_incr(req, LPROC_MDT_MKNOD);
690                 break;
691         default:
692                 CERROR("%s: Unsupported mode %o\n",
693                        mdt_obd_name(info->mti_mdt),
694                        info->mti_attr.ma_attr.la_mode);
695                 RETURN(err_serious(-EOPNOTSUPP));
696         }
697
698         rc = mdt_md_create(info);
699         RETURN(rc);
700 }
701
702 /*
703  * VBR: save parent version in reply and child version getting by its name.
704  * Version of child is getting and checking during its lookup. If
705  */
706 static int mdt_reint_unlink(struct mdt_thread_info *info,
707                             struct mdt_lock_handle *lhc)
708 {
709         struct mdt_reint_record *rr = &info->mti_rr;
710         struct ptlrpc_request   *req = mdt_info_req(info);
711         struct md_attr          *ma = &info->mti_attr;
712         struct lu_fid           *child_fid = &info->mti_tmp_fid1;
713         struct mdt_object       *mp;
714         struct mdt_object       *mc;
715         struct mdt_lock_handle  *parent_lh;
716         struct mdt_lock_handle  *child_lh;
717         int                      rc;
718         int                      no_name = 0;
719         ENTRY;
720
721         DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
722                   PNAME(&rr->rr_name));
723
724         if (info->mti_dlm_req)
725                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
726
727         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
728                 RETURN(err_serious(-ENOENT));
729
730         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1))
731                 RETURN(-EPERM);
732         /*
733          * step 1: Found the parent.
734          */
735         mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
736         if (IS_ERR(mp)) {
737                 rc = PTR_ERR(mp);
738                 GOTO(out, rc);
739         }
740
741         parent_lh = &info->mti_lh[MDT_LH_PARENT];
742
743         if (mdt_object_remote(mp)) {
744                 mdt_lock_reg_init(parent_lh, LCK_EX);
745                 rc = mdt_remote_object_lock(info, mp, &parent_lh->mlh_rreg_lh,
746                                             parent_lh->mlh_rreg_mode,
747                                             MDS_INODELOCK_UPDATE);
748                 if (rc != ELDLM_OK)
749                         GOTO(put_parent, rc);
750
751         } else {
752                 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
753                 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
754                                      MDT_LOCAL_LOCK);
755                 if (rc)
756                         GOTO(put_parent, rc);
757
758                 rc = mdt_version_get_check_save(info, mp, 0);
759                 if (rc)
760                         GOTO(unlock_parent, rc);
761         }
762
763         /* step 2: find & lock the child */
764         /* lookup child object along with version checking */
765         fid_zero(child_fid);
766         rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
767         if (rc != 0) {
768                 /* Name might not be able to find during resend of
769                  * remote unlink, considering following case.
770                  * dir_A is a remote directory, the name entry of
771                  * dir_A is on MDT0, the directory is on MDT1,
772                  *
773                  * 1. client sends unlink req to MDT1.
774                  * 2. MDT1 sends name delete update to MDT0.
775                  * 3. name entry is being deleted in MDT0 synchronously.
776                  * 4. MDT1 is restarted.
777                  * 5. client resends unlink req to MDT1. So it can not
778                  *    find the name entry on MDT0 anymore.
779                  * In this case, MDT1 only needs to destory the local
780                  * directory.
781                  * */
782                 if (mdt_object_remote(mp) && rc == -ENOENT &&
783                     !fid_is_zero(rr->rr_fid2) &&
784                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
785                         no_name = 1;
786                         *child_fid = *rr->rr_fid2;
787                  } else {
788                         GOTO(unlock_parent, rc);
789                  }
790         }
791
792         if (fid_is_obf(child_fid) || fid_is_dot_lustre(child_fid))
793                 GOTO(unlock_parent, rc = -EPERM);
794
795         mdt_reint_init_ma(info, ma);
796
797         /* We will lock the child regardless it is local or remote. No harm. */
798         mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
799         if (IS_ERR(mc))
800                 GOTO(unlock_parent, rc = PTR_ERR(mc));
801
802         child_lh = &info->mti_lh[MDT_LH_CHILD];
803         mdt_lock_reg_init(child_lh, LCK_EX);
804         if (mdt_object_remote(mc)) {
805                 struct mdt_body  *repbody;
806
807                 if (!fid_is_zero(rr->rr_fid2)) {
808                         CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
809                                mdt_obd_name(info->mti_mdt),
810                                PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
811                         GOTO(put_child, rc = -ENOENT);
812                 }
813                 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
814                        mdt_obd_name(info->mti_mdt),
815                        PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
816
817                 if (!mdt_is_dne_client(req->rq_export))
818                         /* Return -EIO for old client */
819                         GOTO(put_child, rc = -EIO);
820
821                 if (info->mti_spec.sp_rm_entry) {
822                         struct lu_ucred *uc  = mdt_ucred(info);
823
824                         if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
825                                 CERROR("%s: unlink remote entry is only "
826                                        "permitted for administrator: rc = %d\n",
827                                         mdt_obd_name(info->mti_mdt),
828                                         -EPERM);
829                                 GOTO(put_child, rc = -EPERM);
830                         }
831
832                         ma->ma_need = MA_INODE;
833                         ma->ma_valid = 0;
834                         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
835                         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
836                                         NULL, &rr->rr_name, ma, no_name);
837                         GOTO(put_child, rc);
838                 }
839                 /* Revoke the LOOKUP lock of the remote object granted by
840                  * this MDT. Since the unlink will happen on another MDT,
841                  * it will release the LOOKUP lock right away. Then What
842                  * would happen if another client try to grab the LOOKUP
843                  * lock at the same time with unlink XXX */
844                 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
845                                 MDT_CROSS_LOCK);
846                 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
847                 LASSERT(repbody != NULL);
848                 repbody->fid1 = *mdt_object_fid(mc);
849                 repbody->valid |= (OBD_MD_FLID | OBD_MD_MDS);
850                 GOTO(unlock_child, rc = -EREMOTE);
851         } else if (info->mti_spec.sp_rm_entry) {
852                 rc = -EPERM;
853                 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
854                        "rc = %d\n",
855                        mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
856                 GOTO(put_child, rc);
857         }
858
859         /* We used to acquire MDS_INODELOCK_FULL here but we can't do
860          * this now because a running HSM restore on the child (unlink
861          * victim) will hold the layout lock. See LU-4002. */
862         rc = mdt_object_lock(info, mc, child_lh,
863                              MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
864                              MDT_CROSS_LOCK);
865         if (rc != 0)
866                 GOTO(put_child, rc);
867
868         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
869                        OBD_FAIL_MDS_REINT_UNLINK_WRITE);
870         /* save version when object is locked */
871         mdt_version_get_save(info, mc, 1);
872         /*
873          * Now we can only make sure we need MA_INODE, in mdd layer, will check
874          * whether need MA_LOV and MA_COOKIE.
875          */
876         ma->ma_need = MA_INODE;
877         ma->ma_valid = 0;
878         mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
879
880         mutex_lock(&mc->mot_lov_mutex);
881
882         rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
883                         mdt_object_child(mc), &rr->rr_name, ma, no_name);
884
885         mutex_unlock(&mc->mot_lov_mutex);
886
887         if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
888                 rc = mdt_attr_get_complex(info, mc, ma);
889         if (rc == 0)
890                 mdt_handle_last_unlink(info, mc, ma);
891
892         if (ma->ma_valid & MA_INODE) {
893                 switch (ma->ma_attr.la_mode & S_IFMT) {
894                 case S_IFDIR:
895                         mdt_counter_incr(req, LPROC_MDT_RMDIR);
896                         break;
897                 case S_IFREG:
898                 case S_IFLNK:
899                 case S_IFCHR:
900                 case S_IFBLK:
901                 case S_IFIFO:
902                 case S_IFSOCK:
903                         mdt_counter_incr(req, LPROC_MDT_UNLINK);
904                         break;
905                 default:
906                         LASSERTF(0, "bad file type %o unlinking\n",
907                                  ma->ma_attr.la_mode);
908                 }
909         }
910
911         EXIT;
912 unlock_child:
913         mdt_object_unlock(info, mc, child_lh, rc);
914 put_child:
915         mdt_object_put(info->mti_env, mc);
916 unlock_parent:
917         mdt_object_unlock(info, mp, parent_lh, rc);
918 put_parent:
919         mdt_object_put(info->mti_env, mp);
920 out:
921         return rc;
922 }
923
924 /*
925  * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
926  * name.
927  */
928 static int mdt_reint_link(struct mdt_thread_info *info,
929                           struct mdt_lock_handle *lhc)
930 {
931         struct mdt_reint_record *rr = &info->mti_rr;
932         struct ptlrpc_request   *req = mdt_info_req(info);
933         struct md_attr          *ma = &info->mti_attr;
934         struct mdt_object       *ms;
935         struct mdt_object       *mp;
936         struct mdt_lock_handle  *lhs;
937         struct mdt_lock_handle  *lhp;
938         int rc;
939         ENTRY;
940
941         DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
942                   PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
943
944         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
945                 RETURN(err_serious(-ENOENT));
946
947         if (info->mti_dlm_req)
948                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
949
950         /* Invalid case so return error immediately instead of
951          * processing it */
952         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
953                 RETURN(-EPERM);
954
955         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1) ||
956             fid_is_obf(rr->rr_fid2) || fid_is_dot_lustre(rr->rr_fid2))
957                 RETURN(-EPERM);
958
959         /* step 1: find & lock the target parent dir */
960         lhp = &info->mti_lh[MDT_LH_PARENT];
961         mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
962         mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
963                                   MDS_INODELOCK_UPDATE);
964         if (IS_ERR(mp))
965                 RETURN(PTR_ERR(mp));
966
967         rc = mdt_version_get_check_save(info, mp, 0);
968         if (rc)
969                 GOTO(out_unlock_parent, rc);
970
971         /* step 2: find & lock the source */
972         lhs = &info->mti_lh[MDT_LH_CHILD];
973         mdt_lock_reg_init(lhs, LCK_EX);
974
975         ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
976         if (IS_ERR(ms))
977                 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
978
979         if (!mdt_object_exists(ms)) {
980                 mdt_object_put(info->mti_env, ms);
981                 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
982                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
983                 GOTO(out_unlock_parent, rc = -ENOENT);
984         }
985
986         if (mdt_object_remote(ms)) {
987                 mdt_object_put(info->mti_env, ms);
988                 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
989                        mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
990                        PFID(rr->rr_fid2));
991                 GOTO(out_unlock_parent, rc = -EXDEV);
992         }
993
994         rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
995                              MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
996         if (rc != 0) {
997                 mdt_object_put(info->mti_env, ms);
998                 GOTO(out_unlock_parent, rc);
999         }
1000
1001         /* step 3: link it */
1002         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1003                        OBD_FAIL_MDS_REINT_LINK_WRITE);
1004
1005         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1006         rc = mdt_version_get_check_save(info, ms, 1);
1007         if (rc)
1008                 GOTO(out_unlock_child, rc);
1009
1010         /** check target version by name during replay */
1011         rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1012                                       &info->mti_tmp_fid1, 2);
1013         if (rc != 0 && rc != -ENOENT)
1014                 GOTO(out_unlock_child, rc);
1015         /* save version of file name for replay, it must be ENOENT here */
1016         if (!req_is_replay(mdt_info_req(info))) {
1017                 if (rc != -ENOENT) {
1018                         CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1019                                PNAME(&rr->rr_name));
1020                         GOTO(out_unlock_child, rc = -EEXIST);
1021                 }
1022                 info->mti_ver[2] = ENOENT_VERSION;
1023                 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1024         }
1025
1026         rc = mdo_link(info->mti_env, mdt_object_child(mp),
1027               mdt_object_child(ms), &rr->rr_name, ma);
1028
1029         if (rc == 0)
1030                 mdt_counter_incr(req, LPROC_MDT_LINK);
1031
1032         EXIT;
1033 out_unlock_child:
1034         mdt_object_unlock_put(info, ms, lhs, rc);
1035 out_unlock_parent:
1036         mdt_object_unlock_put(info, mp, lhp, rc);
1037         return rc;
1038 }
1039 /**
1040  * lock the part of the directory according to the hash of the name
1041  * (lh->mlh_pdo_hash) in parallel directory lock.
1042  */
1043 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1044                               struct mdt_lock_handle *lh,
1045                               struct mdt_object *obj, __u64 ibits)
1046 {
1047         struct ldlm_res_id *res = &info->mti_res_id;
1048         struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1049         ldlm_policy_data_t *policy = &info->mti_policy;
1050         int rc;
1051
1052         /*
1053          * Finish res_id initializing by name hash marking part of
1054          * directory which is taking modification.
1055          */
1056         LASSERT(lh->mlh_pdo_hash != 0);
1057         fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1058         memset(policy, 0, sizeof(*policy));
1059         policy->l_inodebits.bits = ibits;
1060         /*
1061          * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1062          * going to be sent to client. If it is - mdt_intent_policy() path will
1063          * fix it up and turn FL_LOCAL flag off.
1064          */
1065         rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1066                           res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1067                           &info->mti_exp->exp_handle.h_cookie);
1068         return rc;
1069 }
1070
1071 static int mdt_rename_lock(struct mdt_thread_info *info,
1072                            struct lustre_handle *lh)
1073 {
1074         struct ldlm_namespace   *ns = info->mti_mdt->mdt_namespace;
1075         ldlm_policy_data_t      *policy = &info->mti_policy;
1076         struct ldlm_res_id      *res_id = &info->mti_res_id;
1077         __u64                   flags = 0;
1078         int rc;
1079         ENTRY;
1080
1081         fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1082
1083         memset(policy, 0, sizeof *policy);
1084         policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1085 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 53, 0)
1086         /* In phase I, we will not do cross-rename, so local BFL lock would
1087          * be enough
1088          */
1089         flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1090         /*
1091          * Current node is controller, that is mdt0, where we should
1092          * take BFL lock.
1093          */
1094         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1095                                     LCK_EX, &flags, ldlm_blocking_ast,
1096                                     ldlm_completion_ast, NULL, NULL, 0,
1097                                     LVB_T_NONE,
1098                                     &info->mti_exp->exp_handle.h_cookie,
1099                                     lh);
1100 #else
1101 #warning "Local rename lock is invalid for DNE phase II."
1102 #endif
1103         RETURN(rc);
1104 }
1105
1106 static void mdt_rename_unlock(struct lustre_handle *lh)
1107 {
1108         ENTRY;
1109         LASSERT(lustre_handle_is_used(lh));
1110         ldlm_lock_decref(lh, LCK_EX);
1111         EXIT;
1112 }
1113
1114 /*
1115  * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1116  * target. Source should not be ancestor of target dir. May be other rename
1117  * checks can be moved here later.
1118  */
1119 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1120 {
1121         struct mdt_reint_record *rr = &info->mti_rr;
1122         struct lu_fid dst_fid = *rr->rr_fid2;
1123         struct mdt_object *dst;
1124         int rc = 0;
1125         ENTRY;
1126
1127         do {
1128                 LASSERT(fid_is_sane(&dst_fid));
1129                 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1130                 if (!IS_ERR(dst)) {
1131                         rc = mdo_is_subdir(info->mti_env,
1132                                            mdt_object_child(dst), fid,
1133                                            &dst_fid);
1134                         mdt_object_put(info->mti_env, dst);
1135                         if (rc != -EREMOTE && rc < 0) {
1136                                 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1137                         } else {
1138                                 /* check the found fid */
1139                                 if (lu_fid_eq(&dst_fid, fid))
1140                                         rc = -EINVAL;
1141                         }
1142                 } else {
1143                         rc = PTR_ERR(dst);
1144                 }
1145         } while (rc == -EREMOTE);
1146
1147         RETURN(rc);
1148 }
1149
1150 /*
1151  * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1152  * 2 - src child; 3 - tgt child.
1153  * Update on disk version of src child.
1154  */
1155 /**
1156  * For DNE phase I, only these renames are allowed
1157  *      mv src_p/src_c tgt_p/tgt_c
1158  * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1159  * 2. src_p and tgt_p are same directory, and tgt_c does not
1160  *    exists. In this case, all of modification will happen
1161  *    in the MDT where ithesource parent is, only one remote
1162  *    update is needed, i.e. set c_time/m_time on the child.
1163  *    And tgt_c will be still in the same MDT as the original
1164  *    src_c.
1165  */
1166 static int mdt_reint_rename(struct mdt_thread_info *info,
1167                             struct mdt_lock_handle *lhc)
1168 {
1169         struct mdt_reint_record *rr = &info->mti_rr;
1170         struct md_attr          *ma = &info->mti_attr;
1171         struct ptlrpc_request   *req = mdt_info_req(info);
1172         struct mdt_object       *msrcdir;
1173         struct mdt_object       *mtgtdir;
1174         struct mdt_object       *mold;
1175         struct mdt_object       *mnew = NULL;
1176         struct mdt_lock_handle  *lh_srcdirp;
1177         struct mdt_lock_handle  *lh_tgtdirp;
1178         struct mdt_lock_handle  *lh_oldp;
1179         struct mdt_lock_handle  *lh_newp;
1180         struct lu_fid           *old_fid = &info->mti_tmp_fid1;
1181         struct lu_fid           *new_fid = &info->mti_tmp_fid2;
1182         struct lustre_handle     rename_lh = { 0 };
1183         int                      rc;
1184         ENTRY;
1185
1186         if (info->mti_dlm_req)
1187                 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1188
1189         DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1190                   PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1191                   PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1192
1193         if (fid_is_obf(rr->rr_fid1) || fid_is_dot_lustre(rr->rr_fid1) ||
1194             fid_is_obf(rr->rr_fid2) || fid_is_dot_lustre(rr->rr_fid2))
1195                 RETURN(-EPERM);
1196
1197         rc = mdt_rename_lock(info, &rename_lh);
1198         if (rc) {
1199                 CERROR("Can't lock FS for rename, rc %d\n", rc);
1200                 RETURN(rc);
1201         }
1202
1203         lh_newp = &info->mti_lh[MDT_LH_NEW];
1204
1205         /* step 1: lock the source dir. */
1206         lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1207         mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1208         msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1209                                        MDS_INODELOCK_UPDATE);
1210         if (IS_ERR(msrcdir))
1211                 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1212
1213         rc = mdt_version_get_check_save(info, msrcdir, 0);
1214         if (rc)
1215                 GOTO(out_unlock_source, rc);
1216
1217         /* step 2: find & lock the target dir. */
1218         lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1219         mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1220         if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1221                 mdt_object_get(info->mti_env, msrcdir);
1222                 mtgtdir = msrcdir;
1223                 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1224                          rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1225                                                  MDS_INODELOCK_UPDATE);
1226                          if (rc)
1227                                  GOTO(out_unlock_source, rc);
1228                          OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1229                 }
1230         } else {
1231                 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1232                                           rr->rr_fid2);
1233                 if (IS_ERR(mtgtdir))
1234                         GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1235
1236                 /* check early, the real version will be saved after locking */
1237                 rc = mdt_version_get_check(info, mtgtdir, 1);
1238                 if (rc)
1239                         GOTO(out_put_target, rc);
1240
1241                 if (unlikely(mdt_object_remote(mtgtdir))) {
1242                         CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1243                                "on different MDTs\n", PFID(rr->rr_fid1),
1244                                PFID(rr->rr_fid2));
1245                         GOTO(out_put_target, rc = -EXDEV);
1246                 } else {
1247                         if (likely(mdt_object_exists(mtgtdir))) {
1248                                 /* we lock the target dir if it is local */
1249                                 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1250                                                      MDS_INODELOCK_UPDATE,
1251                                                      MDT_LOCAL_LOCK);
1252                                 if (rc != 0)
1253                                         GOTO(out_put_target, rc);
1254                                 /* get and save correct version after locking */
1255                                 mdt_version_get_save(info, mtgtdir, 1);
1256                         } else {
1257                                 GOTO(out_put_target, rc = -ESTALE);
1258                         }
1259                 }
1260         }
1261
1262         /* step 3: find & lock the old object. */
1263         fid_zero(old_fid);
1264         rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1265         if (rc != 0)
1266                 GOTO(out_unlock_target, rc);
1267
1268         if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1269                 GOTO(out_unlock_target, rc = -EINVAL);
1270
1271         if (fid_is_obf(old_fid) || fid_is_dot_lustre(old_fid))
1272                 GOTO(out_unlock_target, rc = -EPERM);
1273
1274         mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1275         if (IS_ERR(mold))
1276                 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1277
1278         lh_oldp = &info->mti_lh[MDT_LH_OLD];
1279         mdt_lock_reg_init(lh_oldp, LCK_EX);
1280         rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1281                              MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1282         if (rc != 0) {
1283                 mdt_object_put(info->mti_env, mold);
1284                 GOTO(out_unlock_target, rc);
1285         }
1286
1287         tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1288         /* save version after locking */
1289         mdt_version_get_save(info, mold, 2);
1290         mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1291
1292         /* step 4: find & lock the new object. */
1293         /* new target object may not exist now */
1294         /* lookup with version checking */
1295         fid_zero(new_fid);
1296         rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1297                                       3);
1298         if (rc == 0) {
1299                 /* the new_fid should have been filled at this moment */
1300                 if (lu_fid_eq(old_fid, new_fid))
1301                        GOTO(out_unlock_old, rc);
1302
1303                 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1304                     lu_fid_eq(new_fid, rr->rr_fid2))
1305                         GOTO(out_unlock_old, rc = -EINVAL);
1306
1307                 if (fid_is_obf(new_fid) || fid_is_dot_lustre(new_fid))
1308                         GOTO(out_unlock_old, rc = -EPERM);
1309
1310                 if (mdt_object_remote(mold)) {
1311                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1312                                PFID(old_fid));
1313                         GOTO(out_unlock_old, rc = -EXDEV);
1314                 }
1315
1316                 mdt_lock_reg_init(lh_newp, LCK_EX);
1317                 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1318                 if (IS_ERR(mnew))
1319                         GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1320
1321                 if (mdt_object_remote(mnew)) {
1322                         mdt_object_put(info->mti_env, mnew);
1323                         CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1324                                PFID(new_fid));
1325                         GOTO(out_unlock_old, rc = -EXDEV);
1326                 }
1327
1328                 /* We used to acquire MDS_INODELOCK_FULL here but we
1329                  * can't do this now because a running HSM restore on
1330                  * the rename onto victim will hold the layout
1331                  * lock. See LU-4002. */
1332                 rc = mdt_object_lock(info, mnew, lh_newp,
1333                                      MDS_INODELOCK_LOOKUP |
1334                                      MDS_INODELOCK_UPDATE,
1335                                      MDT_CROSS_LOCK);
1336                 if (rc != 0) {
1337                         mdt_object_put(info->mti_env, mnew);
1338                         GOTO(out_unlock_old, rc);
1339                 }
1340                 /* get and save version after locking */
1341                 mdt_version_get_save(info, mnew, 3);
1342                 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1343         } else if (rc != -EREMOTE && rc != -ENOENT) {
1344                 GOTO(out_unlock_old, rc);
1345         } else {
1346                 /* If mnew does not exist and mold are remote directory,
1347                  * it only allows rename if they are under same directory */
1348                 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1349                         CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1350                                PFID(old_fid));
1351                         GOTO(out_unlock_old, rc = -EXDEV);
1352                 }
1353                 mdt_enoent_version_save(info, 3);
1354         }
1355
1356         /* step 5: rename it */
1357         mdt_reint_init_ma(info, ma);
1358
1359         mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1360                        OBD_FAIL_MDS_REINT_RENAME_WRITE);
1361
1362         /* Check if @dst is subdir of @src. */
1363         rc = mdt_rename_sanity(info, old_fid);
1364         if (rc)
1365                 GOTO(out_unlock_new, rc);
1366
1367         if (mnew != NULL)
1368                 mutex_lock(&mnew->mot_lov_mutex);
1369
1370         rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1371                         mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1372                         mnew != NULL ? mdt_object_child(mnew) : NULL,
1373                         &rr->rr_tgt_name, ma);
1374
1375         if (mnew != NULL)
1376                 mutex_unlock(&mnew->mot_lov_mutex);
1377
1378         /* handle last link of tgt object */
1379         if (rc == 0) {
1380                 mdt_counter_incr(req, LPROC_MDT_RENAME);
1381                 if (mnew)
1382                         mdt_handle_last_unlink(info, mnew, ma);
1383
1384                 mdt_rename_counter_tally(info, info->mti_mdt, req,
1385                                          msrcdir, mtgtdir);
1386         }
1387
1388         EXIT;
1389 out_unlock_new:
1390         if (mnew)
1391                 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1392 out_unlock_old:
1393         mdt_object_unlock_put(info, mold, lh_oldp, rc);
1394 out_unlock_target:
1395         mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1396 out_put_target:
1397         mdt_object_put(info->mti_env, mtgtdir);
1398 out_unlock_source:
1399         mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1400 out_rename_lock:
1401         if (lustre_handle_is_used(&rename_lh))
1402                 mdt_rename_unlock(&rename_lh);
1403         return rc;
1404 }
1405
1406 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1407                            struct mdt_lock_handle *lhc);
1408
1409 static mdt_reinter reinters[REINT_MAX] = {
1410         [REINT_SETATTR]  = mdt_reint_setattr,
1411         [REINT_CREATE]   = mdt_reint_create,
1412         [REINT_LINK]     = mdt_reint_link,
1413         [REINT_UNLINK]   = mdt_reint_unlink,
1414         [REINT_RENAME]   = mdt_reint_rename,
1415         [REINT_OPEN]     = mdt_reint_open,
1416         [REINT_SETXATTR] = mdt_reint_setxattr,
1417         [REINT_RMENTRY]  = mdt_reint_unlink
1418 };
1419
1420 int mdt_reint_rec(struct mdt_thread_info *info,
1421                   struct mdt_lock_handle *lhc)
1422 {
1423         int rc;
1424         ENTRY;
1425
1426         rc = reinters[info->mti_rr.rr_opcode](info, lhc);
1427
1428         RETURN(rc);
1429 }