Whamcloud - gitweb
LU-4684 migrate: migrate striped directory
[fs/lustre-release.git] / lustre / lfsck / lfsck_striped_dir.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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_striped_dir.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 /*
32  * About the verification for striped directory. Some rules and assumptions:
33  *
34  * 1) lmv_magic: The magic may be wrong. But it is almost impossible (1/2^32
35  *    probability) that a master LMV EA claims as a slave LMV EA by wrong,
36  *    so we can ignore such race case and the reverse case.
37  *
38  * 2) lmv_master_mdt_index: The master index can be self-verified by compared
39  *    with the MDT index directly. The slave stripe index can be verified by
40  *    compared with the file name. Although both the name entry and the LMV EA
41  *    can be wrong, it is almost impossible that they hit the same bad data
42  *    So if they match each other, then trust them. Similarly, for the shard,
43  *    it stores index in both slave LMV EA and in linkEA, if the two copies
44  *    match, then trust them.
45  *
46  * 3) lmv_hash_type: The valid hash type should be LMV_HASH_TYPE_ALL_CHARS or
47  *    LMV_HASH_TYPE_FNV_1A_64. If the LFSCK instance on some slave finds that
48  *    the name hash against the hash function does not match the MDT, then it
49  *    will change the master LMV EA hash type as LMV_HASH_TYPE_UNKNOWN. With
50  *    such hash type, the whole striped directory still can be accessed via
51  *    lookup/readdir, and also support unlink, but cannot add new name entry.
52  *
53  * 3.1) If the master hash type is one of the valid values, then trust the
54  *      master LMV EA. Because:
55  *
56  * 3.1.1) The master hash type is visible to the client and used by the client.
57  *
58  * 3.1.2) For a given name, different hash types may map the name entry to the
59  *        same MDT. So simply checking one name entry or some name entries may
60  *        cannot verify whether the hash type is correct or not.
61  *
62  * 3.1.3) Different shards can claim different hash types, it is not easy to
63  *        distinguish which ones are correct. Even though the master is wrong,
64  *        as the LFSCK processing, some LFSCK instance on other MDT may finds
65  *        unmatched name hash, then it will change the master hash type to
66  *        LMV_HASH_TYPE_UNKNOWN as described above. The worst case is euqal
67  *        to the case without the LFSCK.
68  *
69  * 3.2) If the master hash type is invalid, nor LMV_HASH_TYPE_UNKNOWN, then
70  *      trust the first shard with valid hash type (ALL_CHARS or FNV_1A_64).
71  *      If the shard is also worng, means there are double failures, then as
72  *      the LFSCK processing, other LFSCK instances on the other MDTs may
73  *      find unmatched name hash, and then, the master hash type will be
74  *      changed to LMV_HASH_TYPE_UNKNOWN as described in the 3).
75  *
76  * 3.3) If the master hash type is LMV_HASH_TYPE_UNKNOWN, then it is possible
77  *      that some other LFSCK instance on other MDT found bad name hash, then
78  *      changed the master hash type to LMV_HASH_TYPE_UNKNOWN as described in
79  *      the 3). But it also maybe because of data corruption in master LMV EA.
80  *      To make such two cases to be distinguishable, when the LFSCK changes
81  *      the master hash type to LMV_HASH_TYPE_UNKNOWN, it will mark in the
82  *      master LMV EA (new lmv flags LMV_HASH_FLAG_BAD_TYPE). Then subsequent
83  *      LFSCK checking can distinguish them: for former case, turst the master
84  *      LMV EA with nothing to be done; otherwise, trust the first shard with
85  *      valid hash type (ALL_CHARS or FNV_1A_64) as the 3.2) does.
86  *
87  * 4) lmv_stripe_count: For a shard of a striped directory, if its index has
88  *    been verified as the 2), then the stripe count must be larger than its
89  *    index. For the master object, by scanning each shard's index, the LFSCK
90  *    can know the highest index, and the stripe count must be larger than the
91  *    known highest index. If the stipe count in the LMV EA matches above two
92  *    rules, then it is may be trustable. If both the master claimed stripe
93  *    count and the slave claimed stripe count match each own rule, but they
94  *    are not the same, then trust the master. Because the stripe count in
95  *    the master LMV EA is visible to client and used to distribute the name
96  *    entry to some shard, but the slave LMV EA is only used for verification
97  *    and invisible to client.
98  *
99  * 5) If the master LMV EA is lost, then there are two possible cases:
100  *
101  * 5.1) The slave claims slave LMV EA by wrong, means that the parent was not
102  *      a striped directory, but its sub-directory has a wrong slave LMV EA.
103  *      It is very very race case, similar as the 1), can be ignored.
104  *
105  * 5.2) The parent directory is a striped directory, but the master LMV EA
106  *      is lost or crashed. Then the LFSCK needs to re-generate the master
107  *      LMV EA: the lmv_master_mdt_index is from the MDT device index; the
108  *      lmv_hash_type is from the first valid shard; the lmv_stripe_count
109  *      will be calculated via scanning all the shards.
110  *
111  * 5.2.1) Before re-generating the master LMV EA, the LFSCK needs to check
112  *        whether someone has created some file(s) under the master object
113  *        after the master LMV EA disappear. If yes, the LFSCK will cannot
114  *        re-generate the master LMV EA, otherwise, such new created files
115  *        will be invisible to client. Under such case, the LFSCK will mark
116  *        the master object as read only (without master LMV EA). Then all
117  *        things under the master MDT-object, including those new created
118  *        files and the shards themselves, will be visibile to client. And
119  *        then the administrator can handle the bad striped directory with
120  *        more human knowledge.
121  *
122  * 5.2.2) If someone created some special sub-directory under the master
123  *        MDT-object with the same naming rule as shard name $FID:$index,
124  *        as to the LFSCK cannot detect it before re-generating the master
125  *        LMV EA, then such sub-directory itself will be invisible after
126  *        the LFSCK re-generating the master LMV EA. The sub-items under
127  *        such sub-directory are still visible to client. As the LFSCK
128  *        processing, if such sub-directory cause some conflict with other
129  *        normal shard, such as the index conflict, then the LFSCK will
130  *        remove the master LMV EA and change the master MDT-object to
131  *        read-only mode as the 5.2.1). But if there is no conflict, the
132  *        LFSCK will regard such sub-directory as a striped shard that
133  *        lost its slave LMV EA, and will re-generate slave LMV EA for it.
134  *
135  * 5.2.3) Anytime, if the LFSCK found some shards name/index conflict,
136  *        and cannot make the distinguish which one is right, then it
137  *        will remove the master LMV EA and change the MDT-object to
138  *        read-only mode as the 5.2.2).
139  */
140
141 #define DEBUG_SUBSYSTEM S_LFSCK
142
143 #include <lu_object.h>
144 #include <dt_object.h>
145 #include <md_object.h>
146 #include <lustre_fid.h>
147 #include <lustre_lib.h>
148 #include <lustre_net.h>
149 #include <lustre_lmv.h>
150
151 #include "lfsck_internal.h"
152
153 void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv)
154 {
155         if (llmv != NULL && atomic_dec_and_test(&llmv->ll_ref)) {
156                 if (llmv->ll_inline) {
157                         struct lfsck_lmv_unit   *llu;
158                         struct lfsck_instance   *lfsck;
159
160                         llu = list_entry(llmv, struct lfsck_lmv_unit, llu_lmv);
161                         lfsck = llu->llu_lfsck;
162
163                         spin_lock(&lfsck->li_lock);
164                         list_del(&llu->llu_link);
165                         spin_unlock(&lfsck->li_lock);
166
167                         lfsck_object_put(env, llu->llu_obj);
168
169                         LASSERT(llmv->ll_lslr != NULL);
170
171                         OBD_FREE_LARGE(llmv->ll_lslr,
172                                        sizeof(*llmv->ll_lslr) *
173                                        llmv->ll_stripes_allocated);
174                         OBD_FREE_PTR(llu);
175                 } else {
176                         if (llmv->ll_lslr != NULL)
177                                 OBD_FREE_LARGE(llmv->ll_lslr,
178                                         sizeof(*llmv->ll_lslr) *
179                                         llmv->ll_stripes_allocated);
180
181                         OBD_FREE_PTR(llmv);
182                 }
183         }
184 }
185
186 /**
187  * Mark the specified directory as read-only by set LUSTRE_IMMUTABLE_FL.
188  *
189  * The caller has taken the ldlm lock on the @obj already.
190  *
191  * \param[in] env       pointer to the thread context
192  * \param[in] com       pointer to the lfsck component
193  * \param[in] obj       pointer to the object to be handled
194  * \param[in] del_lmv   true if need to drop the LMV EA
195  *
196  * \retval              positive number if nothing to be done
197  * \retval              zero for success
198  * \retval              negative error number on failure
199  */
200 static int lfsck_disable_master_lmv(const struct lu_env *env,
201                                     struct lfsck_component *com,
202                                     struct dt_object *obj, bool del_lmv)
203 {
204         struct lfsck_thread_info        *info   = lfsck_env_info(env);
205         struct lu_attr                  *la     = &info->lti_la;
206         struct lfsck_instance           *lfsck  = com->lc_lfsck;
207         struct dt_device                *dev    = lfsck_obj2dev(obj);
208         struct thandle                  *th     = NULL;
209         int                              rc     = 0;
210         ENTRY;
211
212         th = dt_trans_create(env, dev);
213         if (IS_ERR(th))
214                 GOTO(log, rc = PTR_ERR(th));
215
216         if (del_lmv) {
217                 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LMV, th);
218                 if (rc != 0)
219                         GOTO(stop, rc);
220         }
221
222         la->la_valid = LA_FLAGS;
223         rc = dt_declare_attr_set(env, obj, la, th);
224         if (rc != 0)
225                 GOTO(stop, rc);
226
227         rc = dt_trans_start_local(env, dev, th);
228         if (rc != 0)
229                 GOTO(stop, rc);
230
231         dt_write_lock(env, obj, 0);
232         if (unlikely(lfsck_is_dead_obj(obj)))
233                 GOTO(unlock, rc = 1);
234
235         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
236                 GOTO(unlock, rc = 0);
237
238         if (del_lmv) {
239                 rc = dt_xattr_del(env, obj, XATTR_NAME_LMV, th);
240                 if (rc != 0)
241                         GOTO(unlock, rc);
242         }
243
244         rc = dt_attr_get(env, obj, la);
245         if (rc == 0 && !(la->la_flags & LUSTRE_IMMUTABLE_FL)) {
246                 la->la_valid = LA_FLAGS;
247                 la->la_flags |= LUSTRE_IMMUTABLE_FL;
248                 rc = dt_attr_set(env, obj, la, th);
249         }
250
251         GOTO(unlock, rc);
252
253 unlock:
254         dt_write_unlock(env, obj);
255
256 stop:
257         dt_trans_stop(env, dev, th);
258
259 log:
260         CDEBUG(D_LFSCK, "%s: namespace LFSCK set the master MDT-object of "
261                "the striped directory "DFID" as read-only: rc = %d\n",
262                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
263
264         if (rc <= 0) {
265                 struct lfsck_namespace *ns = com->lc_file_ram;
266
267                 ns->ln_flags |= LF_INCONSISTENT;
268                 if (rc == 0)
269                         ns->ln_striped_dirs_disabled++;
270         }
271
272         return rc;
273 }
274
275 static inline bool lfsck_is_valid_slave_lmv(struct lmv_mds_md_v1 *lmv)
276 {
277         return lmv->lmv_stripe_count >= 1 &&
278                lmv->lmv_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
279                lmv->lmv_stripe_count > lmv->lmv_master_mdt_index &&
280                lmv_is_known_hash_type(lmv->lmv_hash_type);
281 }
282
283 /**
284  * Remove the striped directory's master LMV EA and mark it as read-only.
285  *
286  * Take ldlm lock on the striped directory before calling the
287  * lfsck_disable_master_lmv().
288  *
289  * \param[in] env       pointer to the thread context
290  * \param[in] com       pointer to the lfsck component
291  * \param[in] obj       pointer to the striped directory to be handled
292  * \param[in] lnr       pointer to the namespace request that contains the
293  *                      striped directory to be handled and other information
294  *
295  * \retval              positive number if nothing to be done
296  * \retval              zero for success
297  * \retval              negative error number on failure
298  */
299 static int lfsck_remove_lmv(const struct lu_env *env,
300                             struct lfsck_component *com,
301                             struct dt_object *obj,
302                             struct lfsck_namespace_req *lnr)
303 {
304         struct lustre_handle     lh     = { 0 };
305         int                      rc;
306
307         lnr->lnr_lmv->ll_ignore = 1;
308         rc = lfsck_ibits_lock(env, com->lc_lfsck, obj, &lh,
309                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
310                               LCK_EX);
311         if (rc == 0) {
312                 rc = lfsck_disable_master_lmv(env, com, obj, true);
313                 lfsck_ibits_unlock(&lh, LCK_EX);
314         }
315
316         return rc;
317 }
318
319 /**
320  * Remove the name entry from the striped directory's master MDT-object.
321  *
322  * \param[in] env       pointer to the thread context
323  * \param[in] com       pointer to the lfsck component
324  * \param[in] dir       pointer to the striped directory
325  * \param[in] fid       the shard's FID which name entry will be removed
326  * \param[in] index     the shard's index which name entry will be removed
327  *
328  * \retval              positive number for repaired successfully
329  * \retval              0 if nothing to be repaired
330  * \retval              negative error number on failure
331  */
332 static int lfsck_remove_dirent(const struct lu_env *env,
333                                struct lfsck_component *com,
334                                struct dt_object *dir,
335                                const struct lu_fid *fid, __u32 index)
336 {
337         struct lfsck_thread_info        *info = lfsck_env_info(env);
338         struct dt_object                *obj;
339         int                              rc;
340
341         snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
342                  PFID(fid), index);
343         obj = lfsck_object_find_bottom(env, com->lc_lfsck, fid);
344         if (IS_ERR(obj))
345                 return PTR_ERR(obj);
346
347         rc = lfsck_namespace_repair_dirent(env, com, dir, obj,
348                                         info->lti_tmpbuf2, info->lti_tmpbuf2,
349                                         S_IFDIR, false, false);
350         lfsck_object_put(env, obj);
351         if (rc > 0) {
352                 struct lfsck_namespace *ns = com->lc_file_ram;
353
354                 ns->ln_dirent_repaired++;
355         }
356
357         return rc;
358 }
359
360 /**
361  * Remove old shard's name entry and refill the @lslr slot with new shard.
362  *
363  * Some old shard held the specified @lslr slot, but it is an invalid shard.
364  * This function will remove the bad shard's name entry, and refill the @lslr
365  * slot with the new shard.
366  *
367  * \param[in] env       pointer to the thread context
368  * \param[in] com       pointer to the lfsck component
369  * \param[in] dir       pointer to the striped directory to be handled
370  * \param[in] lslr      pointer to lfsck_disable_master_lmv slot which content
371  *                      will be replaced by the given information
372  * \param[in] lnr       contain the shard's FID to be used to fill the
373  *                      @lslr slot, it also records the known max filled index
374  *                      and the known max stripe count
375  * \param[in] lmv       contain the slave LMV EA to be used to fill the
376  *                      @lslr slot
377  * \param[in] index     the old shard's index in the striped directory
378  * \param[in] flags     the new shard's flags in the @lslr slot
379  *
380  * \retval              zero for success
381  * \retval              negative error number on failure
382  */
383 static int lfsck_replace_lmv(const struct lu_env *env,
384                              struct lfsck_component *com,
385                              struct dt_object *dir,
386                              struct lfsck_slave_lmv_rec *lslr,
387                              struct lfsck_namespace_req *lnr,
388                              struct lmv_mds_md_v1 *lmv,
389                              __u32 index, __u32 flags)
390 {
391         struct lfsck_lmv *llmv = lnr->lnr_lmv;
392         int               rc;
393
394         rc = lfsck_remove_dirent(env, com, dir,
395                                  &lslr->lslr_fid, index);
396         if (rc < 0)
397                 return rc;
398
399         lslr->lslr_fid = lnr->lnr_fid;
400         lslr->lslr_flags = flags;
401         lslr->lslr_stripe_count = lmv->lmv_stripe_count;
402         lslr->lslr_index = lmv->lmv_master_mdt_index;
403         lslr->lslr_hash_type = lmv->lmv_hash_type;
404         if (flags == LSLF_NONE) {
405                 if (llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
406                     lmv_is_known_hash_type(lmv->lmv_hash_type))
407                         llmv->ll_hash_type = lmv->lmv_hash_type;
408
409                 if (lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
410                     llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
411                         llmv->ll_max_stripe_count = lslr->lslr_stripe_count;
412         }
413
414         return 0;
415 }
416
417 /**
418  * Record the slave LMV EA in the lfsck_lmv::ll_lslr.
419  *
420  * If the lfsck_lmv::ll_lslr slot corresponding to the given @shard_idx is free,
421  * then fill the slot with the given @lnr/@lmv/@flags directly (maybe need to
422  * extend the lfsck_lmv::ll_lslr buffer).
423  *
424  * If the lfsck_lmv::ll_lslr slot corresponding to the given @shard_idx is taken
425  * by other shard, then the LFSCK will try to resolve the conflict by checking
426  * the two conflict shards' flags, and try other possible slot (if one of them
427  * claims another possible @shard_idx).
428  *
429  * 1) If one of the two conflict shards can be recorded in another slot, then
430  *    it is OK, go ahead. Otherwise,
431  *
432  * 2) If one of them is dangling name entry, then remove (one of) the dangling
433  *    name entry (and replace related @lslr slot if needed). Otherwise,
434  *
435  * 3) If one of them has no slave LMV EA, then check whether the master LMV
436  *    EA has ever been lost and re-generated (LMV_HASH_FLAG_LOST_LMV in the
437  *    master LMV EA).
438  *
439  * 3.1) If yes, then it is possible that such object is not a real shard of
440  *      the striped directory, instead, it was created by someone after the
441  *      master LMV EA lost with the name that matches the shard naming rule.
442  *      Then the LFSCK will remove the master LMV EA and mark the striped
443  *      directory as read-only to allow those non-shard files to be visible
444  *      to client.
445  *
446  * 3.2) If no, then remove (one of) the object what has no slave LMV EA.
447  *
448  * 4) If all above efforts cannot work, then the LFSCK cannot know how to
449  *    recover the striped directory. To make the administrator can see the
450  *    conflicts, the LFSCK will remove the master LMV EA and mark the striped
451  *    directory as read-only.
452  *
453  * This function may be called recursively, to prevent overflow, we define
454  * LFSCK_REC_LMV_MAX_DEPTH to restrict the recursive call depth.
455  *
456  * \param[in] env       pointer to the thread context
457  * \param[in] com       pointer to the lfsck component
458  * \param[in] dir       pointer to the striped directory to be handled
459  * \param[in] lnr       contain the shard's FID to fill the @lslr slot,
460  *                      it also records the known max filled index and
461  *                      the known max stripe count
462  * \param[in] lmv       pointer to the slave LMV EA to be recorded
463  * \param[in] shard_idx the shard's index used for locating the @lslr slot,
464  *                      it can be the index stored in the shard's name,
465  *                      it also can be the index stored in the slave LMV EA
466  *                      (for recursive case)
467  * \param[in] flags     the shard's flags to be recorded in the @lslr slot
468  *                      to indicate the shard status, such as whether has
469  *                      slave LMV EA, whether dangling name entry, whether
470  *                      the name entry and slave LMV EA unmatched, and ect
471  * \param[in] flags2    when be called recursively, the @flags2 tells the
472  *                      former conflict shard's flags in the @lslr slot.
473  * \param[in,out] depth To prevent to be called recurisively too deep,
474  *                      we define the max depth can be called recursively
475  *                      (LFSCK_REC_LMV_MAX_DEPTH)
476  *
477  * \retval              zero for success
478  * \retval              "-ERANGE" for invalid @shard_idx
479  * \retval              "-EEXIST" for the required lslr slot has been
480  *                      occupied by other shard
481  * \retval              other negative error number on failure
482  */
483 static int lfsck_record_lmv(const struct lu_env *env,
484                             struct lfsck_component *com,
485                             struct dt_object *dir,
486                             struct lfsck_namespace_req *lnr,
487                             struct lmv_mds_md_v1 *lmv, __u32 shard_idx,
488                             __u32 flags, __u32 flags2, __u32 *depth)
489 {
490         struct lfsck_instance      *lfsck = com->lc_lfsck;
491         struct lfsck_lmv           *llmv  = lnr->lnr_lmv;
492         const struct lu_fid        *fid   = &lnr->lnr_fid;
493         struct lfsck_slave_lmv_rec *lslr;
494         struct lfsck_rec_lmv_save  *lrls;
495         int                         index = shard_idx;
496         int                         rc    = 0;
497         ENTRY;
498
499         CDEBUG(D_LFSCK, "%s: record slave LMV EA for the striped directory "
500                DFID": shard = "DFID", index = %u, flags = %u, flags2 = %u, "
501                "depth = %d\n", lfsck_lfsck2name(lfsck),
502                PFID(lfsck_dto2fid(dir)), PFID(fid),
503                index, flags, flags2, *depth);
504
505         if (index < 0 || index >= LFSCK_LMV_MAX_STRIPES)
506                 RETURN(-ERANGE);
507
508         if (index >= llmv->ll_stripes_allocated) {
509                 struct lfsck_slave_lmv_rec *new_lslr;
510                 int new_stripes = index + 1;
511                 size_t old_size = sizeof(*lslr) * llmv->ll_stripes_allocated;
512
513                 OBD_ALLOC_LARGE(new_lslr, sizeof(*new_lslr) * new_stripes);
514                 if (new_lslr == NULL) {
515                         llmv->ll_failed = 1;
516
517                         RETURN(-ENOMEM);
518                 }
519
520                 memcpy(new_lslr, llmv->ll_lslr, old_size);
521                 OBD_FREE_LARGE(llmv->ll_lslr, old_size);
522                 llmv->ll_stripes_allocated = new_stripes;
523                 llmv->ll_lslr = new_lslr;
524         }
525
526         lslr = llmv->ll_lslr + index;
527         if (unlikely(lu_fid_eq(&lslr->lslr_fid, fid)))
528                 RETURN(0);
529
530         if (fid_is_zero(&lslr->lslr_fid)) {
531                 lslr->lslr_fid = *fid;
532                 lslr->lslr_stripe_count = lmv->lmv_stripe_count;
533                 lslr->lslr_index = lmv->lmv_master_mdt_index;
534                 lslr->lslr_hash_type = lmv->lmv_hash_type;
535                 lslr->lslr_flags = flags;
536                 llmv->ll_stripes_filled++;
537                 if (flags == LSLF_NONE) {
538                         if (llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
539                             lmv_is_known_hash_type(lmv->lmv_hash_type))
540                                 llmv->ll_hash_type = lmv->lmv_hash_type;
541
542                         if (lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
543                             llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
544                                 llmv->ll_max_stripe_count =
545                                                         lslr->lslr_stripe_count;
546                 }
547
548                 if (llmv->ll_max_filled_off < index)
549                         llmv->ll_max_filled_off = index;
550
551                 RETURN(0);
552         }
553
554         (*depth)++;
555         if (flags != LSLF_BAD_INDEX2)
556                 LASSERTF(*depth == 1, "depth = %d\n", *depth);
557
558         /* Handle conflict cases. */
559         switch (lslr->lslr_flags) {
560         case LSLF_NONE:
561         case LSLF_BAD_INDEX2:
562                 /* The existing one is a normal valid object. */
563                 switch (flags) {
564                 case LSLF_NONE:
565                         /* The two 'valid' name entries claims the same
566                          * index, the LFSCK cannot distinguish which one
567                          * is correct. Then remove the master LMV EA to
568                          * make all shards to be visible to client, and
569                          * mark the master MDT-object as read-only. The
570                          * administrator can handle the conflict with
571                          * more human knowledge. */
572                         rc = lfsck_remove_lmv(env, com, dir, lnr);
573                         break;
574                 case LSLF_BAD_INDEX2:
575                         GOTO(out, rc = -EEXIST);
576                 case LSLF_NO_LMVEA:
577
578 no_lmvea:
579                         if (llmv->ll_lmv.lmv_hash_type &
580                             LMV_HASH_FLAG_LOST_LMV) {
581                                 /* If the master LMV EA was re-generated
582                                  * by the former LFSCK reparation, and
583                                  * before such reparation, someone has
584                                  * created the conflict object, but the
585                                  * LFSCK did not detect such conflict,
586                                  * then we have to remove the master
587                                  * LMV EA and mark the master MDT-object
588                                  * as read-only. The administrator can
589                                  * handle the conflict with more human
590                                  * knowledge. */
591                                 rc = lfsck_remove_lmv(env, com, dir, lnr);
592                         } else {
593                                 /* Otherwise, remove the current name entry,
594                                  * and add its FID in the LFSCK tracing file
595                                  * for further processing. */
596                                 rc = lfsck_namespace_trace_update(env, com, fid,
597                                                 LNTF_CHECK_PARENT, true);
598                                 if (rc == 0)
599                                         rc = lfsck_remove_dirent(env, com, dir,
600                                                                  fid, index);
601                         }
602
603                         break;
604                 case LSLF_DANGLING:
605                         /* Remove the current dangling name entry. */
606                         rc = lfsck_remove_dirent(env, com, dir, fid, index);
607                         break;
608                 case LSLF_BAD_INDEX1:
609                         index = lmv->lmv_master_mdt_index;
610                         lmv->lmv_master_mdt_index = shard_idx;
611                         /* The name entry claims an index that is conflict
612                          * with a valid existing name entry, then try the
613                          * index in the lmv recursively. */
614                         rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
615                                 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
616                         lmv->lmv_master_mdt_index = index;
617                         if (rc == -ERANGE || rc == -EEXIST)
618                                 /* The index in the lmv is invalid or
619                                  * also conflict with other. Then we do
620                                  * not know how to resolve the conflict.
621                                  * We will handle it as handle the case
622                                  * of 'LSLF_NONE' vs 'LSLF_NONE'. */
623                                 rc = lfsck_remove_lmv(env, com, dir, lnr);
624
625                         break;
626                 default:
627                         break;
628                 }
629
630                 break;
631         case LSLF_NO_LMVEA:
632                 /* The existing one has no slave LMV EA. */
633                 switch (flags) {
634                 case LSLF_NONE:
635
636 none:
637                         if (llmv->ll_lmv.lmv_hash_type &
638                             LMV_HASH_FLAG_LOST_LMV) {
639                                 /* If the master LMV EA was re-generated
640                                  * by the former LFSCK reparation, and
641                                  * before such reparation, someone has
642                                  * created the conflict object, but the
643                                  * LFSCK did not detect such conflict,
644                                  * then we have to remove the master
645                                  * LMV EA and mark the master MDT-object
646                                  * as read-only. The administrator can
647                                  * handle the conflict with more human
648                                  * knowledge. */
649                                 rc = lfsck_remove_lmv(env, com, dir, lnr);
650                         } else {
651                                 lrls = &lfsck->li_rec_lmv_save[*depth - 1];
652                                 lrls->lrls_fid = lslr->lslr_fid;
653                                 /* Otherwise, remove the existing name entry,
654                                  * and add its FID in the LFSCK tracing file
655                                  * for further processing. Refill the slot
656                                  * with current slave LMV EA. */
657                                 rc = lfsck_namespace_trace_update(env,
658                                                 com, &lrls->lrls_fid,
659                                                 LNTF_CHECK_PARENT, true);
660                                 if (rc == 0)
661                                         rc = lfsck_replace_lmv(env, com, dir,
662                                                 lslr, lnr, lmv, index, flags);
663                         }
664
665                         break;
666                 case LSLF_BAD_INDEX2:
667                         if (flags2 >= lslr->lslr_flags)
668                                 GOTO(out, rc = -EEXIST);
669
670                         goto none;
671                 case LSLF_NO_LMVEA:
672                         goto no_lmvea;
673                 case LSLF_DANGLING:
674                         /* Remove the current dangling name entry. */
675                         rc = lfsck_remove_dirent(env, com, dir, fid, index);
676                         break;
677                 case LSLF_BAD_INDEX1:
678                         index = lmv->lmv_master_mdt_index;
679                         lmv->lmv_master_mdt_index = shard_idx;
680                         /* The name entry claims an index that is conflict
681                          * with a valid existing name entry, then try the
682                          * index in the lmv recursively. */
683                         rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
684                                 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
685                         lmv->lmv_master_mdt_index = index;
686                         if (rc == -ERANGE || rc == -EEXIST) {
687                                 index = shard_idx;
688                                 goto no_lmvea;
689                         }
690
691                         break;
692                 default:
693                         break;
694                 }
695
696                 break;
697         case LSLF_DANGLING:
698                 /* The existing one is a dangling name entry. */
699                 switch (flags) {
700                 case LSLF_NONE:
701                 case LSLF_BAD_INDEX2:
702                 case LSLF_NO_LMVEA:
703                         /* Remove the existing dangling name entry.
704                          * Refill the lslr slot with the given LMV. */
705                         rc = lfsck_replace_lmv(env, com, dir, lslr, lnr,
706                                                lmv, index, flags);
707                         break;
708                 case LSLF_DANGLING:
709                         /* Two dangling name entries conflict,
710                          * remove the current one. */
711                         rc = lfsck_remove_dirent(env, com, dir, fid, index);
712                         break;
713                 case LSLF_BAD_INDEX1:
714                         index = lmv->lmv_master_mdt_index;
715                         lmv->lmv_master_mdt_index = shard_idx;
716                         /* The name entry claims an index that is conflict
717                          * with a valid existing name entry, then try the
718                          * index in the lmv recursively. */
719                         rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
720                                 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
721                         lmv->lmv_master_mdt_index = index;
722                         if (rc == -ERANGE || rc == -EEXIST)
723                                 /* If the index in the lmv is invalid or
724                                  * also conflict with other, then remove
725                                  * the existing dangling name entry.
726                                  * Refill the lslr slot with the given LMV. */
727                                 rc = lfsck_replace_lmv(env, com, dir, lslr, lnr,
728                                                        lmv, shard_idx, flags);
729
730                         break;
731                 default:
732                         break;
733                 }
734
735                 break;
736         case LSLF_BAD_INDEX1: {
737                 if (*depth >= LFSCK_REC_LMV_MAX_DEPTH)
738                         goto conflict;
739
740                 lrls = &lfsck->li_rec_lmv_save[*depth - 1];
741                 lrls->lrls_fid = lnr->lnr_fid;
742                 lrls->lrls_lmv = *lmv;
743
744                 lnr->lnr_fid = lslr->lslr_fid;
745                 lmv->lmv_master_mdt_index = index;
746                 lmv->lmv_stripe_count = lslr->lslr_stripe_count;
747                 lmv->lmv_hash_type = lslr->lslr_hash_type;
748                 index = lslr->lslr_index;
749
750                 /* The existing one has another possible slot,
751                  * try it recursively. */
752                 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
753                                       LSLF_BAD_INDEX2, flags, depth);
754                 *lmv = lrls->lrls_lmv;
755                 lnr->lnr_fid = lrls->lrls_fid;
756                 index = shard_idx;
757                 if (rc != 0) {
758                         if (rc == -ERANGE || rc == -EEXIST)
759                                 goto conflict;
760
761                         break;
762                 }
763
764                 lslr->lslr_fid = *fid;
765                 lslr->lslr_flags = flags;
766                 lslr->lslr_stripe_count = lmv->lmv_stripe_count;
767                 lslr->lslr_index = lmv->lmv_master_mdt_index;
768                 lslr->lslr_hash_type = lmv->lmv_hash_type;
769                 if (flags == LSLF_NONE) {
770                         if (llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
771                             lmv_is_known_hash_type(lmv->lmv_hash_type))
772                                 llmv->ll_hash_type = lmv->lmv_hash_type;
773
774                         if (lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
775                             llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
776                                 llmv->ll_max_stripe_count =
777                                                         lslr->lslr_stripe_count;
778                 }
779
780                 break;
781
782 conflict:
783                 switch (flags) {
784                 case LSLF_NONE:
785                         /* The two 'valid' name entries claims the same
786                          * index, the LFSCK cannot distinguish which one
787                          * is correct. Then remove the master LMV EA to
788                          * make all shards to be visible to client, and
789                          * mark the master MDT-object as read-only. The
790                          * administrator can handle the conflict with
791                          * more human knowledge. */
792                         rc = lfsck_remove_lmv(env, com, dir, lnr);
793                         break;
794                 case LSLF_BAD_INDEX2:
795                         GOTO(out, rc = -EEXIST);
796                 case LSLF_NO_LMVEA:
797                         goto no_lmvea;
798                 case LSLF_DANGLING:
799                         /* Remove the current dangling name entry. */
800                         rc = lfsck_remove_dirent(env, com, dir, fid, index);
801                         break;
802                 case LSLF_BAD_INDEX1:
803                         index = lmv->lmv_master_mdt_index;
804                         lmv->lmv_master_mdt_index = shard_idx;
805                         /* The name entry claims an index that is conflict
806                          * with a valid existing name entry, then try the
807                          * index in the lmv recursively. */
808                         rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
809                                 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
810                         lmv->lmv_master_mdt_index = index;
811                         if (rc == -ERANGE || rc == -EEXIST)
812                                 /* The index in the lmv is invalid or
813                                  * also conflict with other. Then we do
814                                  * not know how to resolve the conflict.
815                                  * We will handle it as handle the case
816                                  * of 'LSLF_NONE' vs 'LSLF_NONE'. */
817                                 rc = lfsck_remove_lmv(env, com, dir, lnr);
818
819                         break;
820                 }
821
822                 break;
823         }
824         default:
825                 break;
826         }
827
828         if (rc < 0)
829                 llmv->ll_failed = 1;
830
831         GOTO(out, rc);
832
833 out:
834         (*depth)--;
835
836         return rc > 0 ? 0 : rc;
837 }
838
839 int lfsck_read_stripe_lmv(const struct lu_env *env, struct dt_object *obj,
840                           struct lmv_mds_md_v1 *lmv)
841 {
842         struct lfsck_thread_info *info = lfsck_env_info(env);
843         struct lu_buf *buf = &info->lti_buf;
844         int size = sizeof(*lmv) + sizeof(struct lu_fid) * 2;
845         int rc;
846
847         dt_read_lock(env, obj, 0);
848         buf->lb_buf = lmv;
849         buf->lb_len = sizeof(*lmv);
850         rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
851         if (unlikely(rc == -ERANGE)) {
852                 buf = &info->lti_big_buf;
853                 lu_buf_check_and_alloc(buf, size);
854                 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
855                 /* For the in-migration directory, its LMV EA contains
856                  * not only the LMV header, but also the FIDs for both
857                  * source and target. So the LMV EA size is larger. */
858                 if (rc == size) {
859                         rc = sizeof(*lmv);
860                         memcpy(lmv, buf->lb_buf, rc);
861                 }
862         }
863         dt_read_unlock(env, obj);
864         if (rc != sizeof(*lmv))
865                 return rc > 0 ? -EINVAL : rc;
866
867         lfsck_lmv_header_le_to_cpu(lmv, lmv);
868         if ((lmv->lmv_magic == LMV_MAGIC &&
869              !(lmv->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) ||
870             (lmv->lmv_magic == LMV_MAGIC_STRIPE &&
871              !(lmv->lmv_hash_type & LMV_HASH_FLAG_DEAD)))
872                 return 0;
873
874         return -ENODATA;
875 }
876
877 /**
878  * Parse the shard's index from the given shard name.
879  *
880  * The valid shard name/type should be:
881  * 1) The type must be S_IFDIR
882  * 2) The name should be $FID:$index
883  * 3) the index should within valid range.
884  *
885  * \param[in] env       pointer to the thread context
886  * \param[in] name      the shard name
887  * \param[in] namelen   the name length
888  * \param[in] type      the entry's type
889  * \param[in] fid       the entry's FID
890  *
891  * \retval              zero or positive number for the index from the name
892  * \retval              negative error number on failure
893  */
894 int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
895                               int namelen, __u16 type, const struct lu_fid *fid)
896 {
897         char    *name2  = lfsck_env_info(env)->lti_tmpbuf2;
898         int      len;
899         int      idx    = 0;
900
901         if (!S_ISDIR(type))
902                 return -ENOTDIR;
903
904         LASSERT(name != name2);
905
906         len = snprintf(name2, sizeof(lfsck_env_info(env)->lti_tmpbuf2),
907                        DFID":", PFID(fid));
908         if (namelen < len + 1 || memcmp(name, name2, len) != 0)
909                 return -EINVAL;
910
911         do {
912                 if (!isdigit(name[len]))
913                         return -EINVAL;
914
915                 idx = idx * 10 + name[len++] - '0';
916         } while (len < namelen);
917
918         if (idx >= LFSCK_LMV_MAX_STRIPES)
919                 return -EINVAL;
920
921         return idx;
922 }
923
924 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
925                                      struct lfsck_lmv *llmv,
926                                      const char *name, int namelen)
927 {
928         struct lmv_mds_md_v1    *lmv;
929         int                      idx;
930
931         if (llmv == NULL || !llmv->ll_lmv_slave || !llmv->ll_lmv_verified)
932                 return true;
933
934         lmv = &llmv->ll_lmv;
935         idx = lmv_name_to_stripe_index(lmv->lmv_hash_type,
936                                        lmv->lmv_stripe_count,
937                                        name, namelen);
938         if (unlikely(idx != lmv->lmv_master_mdt_index))
939                 return false;
940
941         return true;
942 }
943
944 /**
945  * Check whether the given name is a valid entry under the @parent.
946  *
947  * If the @parent is a striped directory then the @child should one
948  * shard of the striped directory, its name should be $FID:$index.
949  *
950  * If the @parent is a shard of a striped directory, then the name hash
951  * should match the MDT, otherwise it is invalid.
952  *
953  * \param[in] env       pointer to the thread context
954  * \param[in] parent    the parent directory
955  * \param[in] child     the child object to be checked
956  * \param[in] cname     the name for the @child in the parent directory
957  *
958  * \retval              positive number for invalid name entry
959  * \retval              0 if the name is valid or uncertain
960  * \retval              negative error number on failure
961  */
962 int lfsck_namespace_check_name(const struct lu_env *env,
963                                struct dt_object *parent,
964                                struct dt_object *child,
965                                const struct lu_name *cname)
966 {
967         struct lmv_mds_md_v1    *lmv = &lfsck_env_info(env)->lti_lmv;
968         int                      idx;
969         int                      rc;
970
971         rc = lfsck_read_stripe_lmv(env, parent, lmv);
972         if (rc != 0)
973                 RETURN(rc == -ENODATA ? 0 : rc);
974
975         if (lmv->lmv_magic == LMV_MAGIC_STRIPE) {
976                 if (!lfsck_is_valid_slave_lmv(lmv))
977                         return 0;
978
979                 idx = lmv_name_to_stripe_index(lmv->lmv_hash_type,
980                                                lmv->lmv_stripe_count,
981                                                cname->ln_name,
982                                                cname->ln_namelen);
983                 if (unlikely(idx != lmv->lmv_master_mdt_index))
984                         return 1;
985         } else if (lfsck_shard_name_to_index(env, cname->ln_name,
986                         cname->ln_namelen, lfsck_object_type(child),
987                         lfsck_dto2fid(child)) < 0) {
988                 return 1;
989         }
990
991         return 0;
992 }
993
994 /**
995  * Update the object's LMV EA with the given @lmv.
996  *
997  * \param[in] env       pointer to the thread context
998  * \param[in] com       pointer to the lfsck component
999  * \param[in] obj       pointer to the object which LMV EA will be updated
1000  * \param[in] lmv       pointer to buffer holding the new LMV EA
1001  * \param[in] locked    whether the caller has held ldlm lock on the @obj or not
1002  *
1003  * \retval              positive number for nothing to be done
1004  * \retval              zero if updated successfully
1005  * \retval              negative error number on failure
1006  */
1007 int lfsck_namespace_update_lmv(const struct lu_env *env,
1008                                struct lfsck_component *com,
1009                                struct dt_object *obj,
1010                                struct lmv_mds_md_v1 *lmv, bool locked)
1011 {
1012         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1013         struct lmv_mds_md_v1            *lmv4   = &info->lti_lmv4;
1014         struct lu_buf                   *buf    = &info->lti_buf;
1015         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1016         struct dt_device                *dev    = lfsck_obj2dev(obj);
1017         struct thandle                  *th     = NULL;
1018         struct lustre_handle             lh     = { 0 };
1019         int                              rc     = 0;
1020         int                              rc1    = 0;
1021         ENTRY;
1022
1023         LASSERT(lmv4 != lmv);
1024
1025         lfsck_lmv_header_cpu_to_le(lmv4, lmv);
1026         lfsck_buf_init(buf, lmv4, sizeof(*lmv4));
1027
1028         if (!locked) {
1029                 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1030                                       MDS_INODELOCK_UPDATE |
1031                                       MDS_INODELOCK_XATTR, LCK_EX);
1032                 if (rc != 0)
1033                         GOTO(log, rc);
1034         }
1035
1036         th = dt_trans_create(env, dev);
1037         if (IS_ERR(th))
1038                 GOTO(log, rc = PTR_ERR(th));
1039
1040         /* For remote updating LMV EA, there will be further LFSCK action on
1041          * remote MDT after the updating, so update the LMV EA synchronously. */
1042         if (dt_object_remote(obj))
1043                 th->th_sync = 1;
1044
1045         rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
1046         if (rc != 0)
1047                 GOTO(stop, rc);
1048
1049         rc = dt_trans_start_local(env, dev, th);
1050         if (rc != 0)
1051                 GOTO(stop, rc);
1052
1053         dt_write_lock(env, obj, 0);
1054         if (unlikely(lfsck_is_dead_obj(obj)))
1055                 GOTO(unlock, rc = 1);
1056
1057         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1058                 GOTO(unlock, rc = 0);
1059
1060         rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
1061
1062         GOTO(unlock, rc);
1063
1064 unlock:
1065         dt_write_unlock(env, obj);
1066
1067 stop:
1068         rc1 = dt_trans_stop(env, dev, th);
1069         if (rc == 0)
1070                 rc = rc1;
1071
1072 log:
1073         lfsck_ibits_unlock(&lh, LCK_EX);
1074         CDEBUG(D_LFSCK, "%s: namespace LFSCK updated the %s LMV EA "
1075                "for the object "DFID": rc = %d\n",
1076                lfsck_lfsck2name(lfsck),
1077                lmv->lmv_magic == LMV_MAGIC ? "master" : "slave",
1078                PFID(lfsck_dto2fid(obj)), rc);
1079
1080         return rc;
1081 }
1082
1083 /**
1084  * Check whether allow to re-genereate the lost master LMV EA.
1085  *
1086  * If the master MDT-object of the striped directory lost its master LMV EA,
1087  * then before the LFSCK repaired the striped directory, some ones may have
1088  * created some objects (that are not normal shards of the striped directory)
1089  * under the master MDT-object. If such case happend, then the LFSCK cannot
1090  * re-generate the lost master LMV EA to keep those objects to be visible to
1091  * client.
1092  *
1093  * \param[in] env       pointer to the thread context
1094  * \param[in] com       pointer to the lfsck component
1095  * \param[in] obj       pointer to the master MDT-object to be checked
1096  * \param[in] cfid      the shard's FID used for verification
1097  * \param[in] cidx      the shard's index used for verification
1098  *
1099  * \retval              positive number if not allow to re-generate LMV EA
1100  * \retval              zero if allow to re-generate LMV EA
1101  * \retval              negative error number on failure
1102  */
1103 static int lfsck_allow_regenerate_master_lmv(const struct lu_env *env,
1104                                              struct lfsck_component *com,
1105                                              struct dt_object *obj,
1106                                              const struct lu_fid *cfid,
1107                                              __u32 cidx)
1108 {
1109         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1110         struct lu_fid                   *tfid   = &info->lti_fid3;
1111         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1112         struct lu_dirent                *ent    =
1113                         (struct lu_dirent *)info->lti_key;
1114         const struct dt_it_ops          *iops;
1115         struct dt_it                    *di;
1116         __u64                            cookie;
1117         __u32                            args;
1118         int                              rc;
1119         __u16                            type;
1120         ENTRY;
1121
1122         if (unlikely(!dt_try_as_dir(env, obj)))
1123                 RETURN(-ENOTDIR);
1124
1125         /* Check whether the shard and the master MDT-object matches or not. */
1126         snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf), DFID":%u",
1127                  PFID(cfid), cidx);
1128         rc = dt_lookup(env, obj, (struct dt_rec *)tfid,
1129                        (const struct dt_key *)info->lti_tmpbuf);
1130         if (rc != 0)
1131                 RETURN(rc);
1132
1133         if (!lu_fid_eq(tfid, cfid))
1134                 RETURN(-ENOENT);
1135
1136         args = lfsck->li_args_dir & ~(LUDA_VERIFY | LUDA_VERIFY_DRYRUN);
1137         iops = &obj->do_index_ops->dio_it;
1138         di = iops->init(env, obj, args);
1139         if (IS_ERR(di))
1140                 RETURN(PTR_ERR(di));
1141
1142         rc = iops->load(env, di, 0);
1143         if (rc == 0)
1144                 rc = iops->next(env, di);
1145         else if (rc > 0)
1146                 rc = 0;
1147
1148         if (rc != 0)
1149                 GOTO(out, rc);
1150
1151         do {
1152                 rc = iops->rec(env, di, (struct dt_rec *)ent, args);
1153                 if (rc == 0)
1154                         rc = lfsck_unpack_ent(ent, &cookie, &type);
1155
1156                 if (rc != 0)
1157                         GOTO(out, rc);
1158
1159                 /* skip dot and dotdot entries */
1160                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1161                         goto next;
1162
1163                 /* If the subdir name does not match the shard name rule, then
1164                  * it is quite possible that it is NOT a shard, but created by
1165                  * someone after the master MDT-object lost the master LMV EA.
1166                  * But it is also possible that the subdir name entry crashed,
1167                  * under such double failure cases, the LFSCK cannot know how
1168                  * to repair the inconsistency. For data safe, the LFSCK will
1169                  * mark the master MDT-object as read-only. The administrator
1170                  * can fix the bad shard name manually, then run LFSCK again.
1171                  *
1172                  * XXX: If the subdir name matches the shard name rule, but it
1173                  *      is not a real shard of the striped directory, instead,
1174                  *      it was created by someone after the master MDT-object
1175                  *      lost the LMV EA, then re-generating the master LMV EA
1176                  *      will cause such subdir to be invisible to client, and
1177                  *      if its index occupies some lost shard index, then the
1178                  *      LFSCK will use it to replace the bad shard, and cause
1179                  *      the subdir (itself) to be invisible for ever. */
1180                 if (lfsck_shard_name_to_index(env, ent->lde_name,
1181                                 ent->lde_namelen, type, &ent->lde_fid) < 0)
1182                         GOTO(out, rc = 1);
1183
1184 next:
1185                 rc = iops->next(env, di);
1186         } while (rc == 0);
1187
1188         GOTO(out, rc = 0);
1189
1190 out:
1191         iops->put(env, di);
1192         iops->fini(env, di);
1193
1194         return rc;
1195 }
1196
1197 /**
1198  * Notify remote LFSCK instance that the object's LMV EA has been updated.
1199  *
1200  * \param[in] env       pointer to the thread context
1201  * \param[in] com       pointer to the lfsck component
1202  * \param[in] obj       pointer to the object on which the LMV EA will be set
1203  * \param[in] event     indicate either master or slave LMV EA has been updated
1204  * \param[in] flags     indicate which element(s) in the LMV EA has been updated
1205  * \param[in] index     the MDT index on which the LFSCK instance to be notified
1206  *
1207  * \retval              positive number if nothing to be done
1208  * \retval              zero for success
1209  * \retval              negative error number on failure
1210  */
1211 static int lfsck_namespace_notify_lmv_remote(const struct lu_env *env,
1212                                              struct lfsck_component *com,
1213                                              struct dt_object *obj,
1214                                              __u32 event, __u32 flags,
1215                                              __u32 index)
1216 {
1217         struct lfsck_request            *lr     = &lfsck_env_info(env)->lti_lr;
1218         const struct lu_fid             *fid    = lfsck_dto2fid(obj);
1219         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1220         struct lfsck_tgt_desc           *ltd    = NULL;
1221         struct ptlrpc_request           *req    = NULL;
1222         int                              rc;
1223         ENTRY;
1224
1225         ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, index);
1226         if (ltd == NULL)
1227                 GOTO(out, rc = -ENODEV);
1228
1229         req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1230                                    &RQF_LFSCK_NOTIFY);
1231         if (req == NULL)
1232                 GOTO(out, rc = -ENOMEM);
1233
1234         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1235         if (rc != 0) {
1236                 ptlrpc_request_free(req);
1237
1238                 GOTO(out, rc);
1239         }
1240
1241         lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1242         memset(lr, 0, sizeof(*lr));
1243         lr->lr_event = event;
1244         lr->lr_index = lfsck_dev_idx(lfsck);
1245         lr->lr_active = LFSCK_TYPE_NAMESPACE;
1246         lr->lr_fid = *fid;
1247         lr->lr_flags = flags;
1248
1249         ptlrpc_request_set_replen(req);
1250         rc = ptlrpc_queue_wait(req);
1251         ptlrpc_req_finished(req);
1252
1253         GOTO(out, rc = (rc == -ENOENT ? 1 : rc));
1254
1255 out:
1256         CDEBUG(D_LFSCK, "%s: namespace LFSCK notify LMV EA updated for the "
1257                "object "DFID" on MDT %x remotely with event %u, flags %u: "
1258                "rc = %d\n", lfsck_lfsck2name(lfsck), PFID(fid), index,
1259                event, flags, rc);
1260
1261         if (ltd != NULL)
1262                 lfsck_tgt_put(ltd);
1263
1264         return rc;
1265 }
1266
1267 /**
1268  * Generate request for local LFSCK instance to rescan the striped directory.
1269  *
1270  * \param[in] env       pointer to the thread context
1271  * \param[in] com       pointer to the lfsck component
1272  * \param[in] obj       pointer to the striped directory to be rescanned
1273  *
1274  * \retval              positive number if nothing to be done
1275  * \retval              zero for success
1276  * \retval              negative error number on failure
1277  */
1278 int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
1279                                             struct lfsck_component *com,
1280                                             struct dt_object *obj)
1281 {
1282         struct lfsck_instance      *lfsck = com->lc_lfsck;
1283         struct lfsck_namespace     *ns    = com->lc_file_ram;
1284         struct lmv_mds_md_v1       *lmv4  = &lfsck_env_info(env)->lti_lmv4;
1285         struct lfsck_lmv_unit      *llu;
1286         struct lfsck_lmv           *llmv;
1287         struct lfsck_slave_lmv_rec *lslr;
1288         int                         count = 0;
1289         int                         rc;
1290         ENTRY;
1291
1292         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1293                 RETURN(0);
1294
1295         rc = lfsck_read_stripe_lmv(env, obj, lmv4);
1296         if (rc != 0)
1297                 RETURN(rc);
1298
1299         OBD_ALLOC_PTR(llu);
1300         if (unlikely(llu == NULL))
1301                 RETURN(-ENOMEM);
1302
1303         if (lmv4->lmv_stripe_count < 1)
1304                 count = LFSCK_LMV_DEF_STRIPES;
1305         else if (lmv4->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
1306                 count = LFSCK_LMV_MAX_STRIPES;
1307         else
1308                 count = lmv4->lmv_stripe_count;
1309
1310         OBD_ALLOC_LARGE(lslr, sizeof(struct lfsck_slave_lmv_rec) * count);
1311         if (lslr == NULL) {
1312                 OBD_FREE_PTR(llu);
1313
1314                 RETURN(-ENOMEM);
1315         }
1316
1317         INIT_LIST_HEAD(&llu->llu_link);
1318         llu->llu_lfsck = lfsck;
1319         llu->llu_obj = lfsck_object_get(obj);
1320         llmv = &llu->llu_lmv;
1321         llmv->ll_lmv_master = 1;
1322         llmv->ll_inline = 1;
1323         atomic_set(&llmv->ll_ref, 1);
1324         llmv->ll_stripes_allocated = count;
1325         llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
1326         llmv->ll_lslr = lslr;
1327         llmv->ll_lmv = *lmv4;
1328
1329         down_write(&com->lc_sem);
1330         if (ns->ln_status != LS_SCANNING_PHASE1 &&
1331             ns->ln_status != LS_SCANNING_PHASE2) {
1332                 ns->ln_striped_dirs_skipped++;
1333                 up_write(&com->lc_sem);
1334                 lfsck_lmv_put(env, llmv);
1335         } else {
1336                 ns->ln_striped_dirs_repaired++;
1337                 llmv->ll_counted = 1;
1338                 spin_lock(&lfsck->li_lock);
1339                 list_add_tail(&llu->llu_link, &lfsck->li_list_lmv);
1340                 spin_unlock(&lfsck->li_lock);
1341                 up_write(&com->lc_sem);
1342         }
1343
1344         RETURN(0);
1345 }
1346
1347 /**
1348  * Set master LMV EA for the specified striped directory.
1349  *
1350  * First, if the master MDT-object of a striped directory lost its LMV EA,
1351  * then there may be some users have created some files under the master
1352  * MDT-object directly. Under such case, the LFSCK cannot re-generate LMV
1353  * EA for the master MDT-object, because we should keep the existing files
1354  * to be visible to client. Then the LFSCK will mark the striped directory
1355  * as read-only and keep it there to be handled by administrator manually.
1356  *
1357  * If nobody has created files under the master MDT-object of the striped
1358  * directory, then we will set the master LMV EA and generate a new rescan
1359  * (the striped directory) request that will be handled later by the LFSCK
1360  * instance on the MDT later.
1361  *
1362  * \param[in] env       pointer to the thread context
1363  * \param[in] com       pointer to the lfsck component
1364  * \param[in] obj       pointer to the object on which the LMV EA will be set
1365  * \param[in] lmv       pointer to the buffer holding the new LMV EA
1366  * \param[in] cfid      the shard's FID used for verification
1367  * \param[in] cidx      the shard's index used for verification
1368  * \param[in] flags     to indicate which element(s) in the LMV EA will be set
1369  *
1370  * \retval              positive number if nothing to be done
1371  * \retval              zero for success
1372  * \retval              negative error number on failure
1373  */
1374 static int lfsck_namespace_set_lmv_master(const struct lu_env *env,
1375                                           struct lfsck_component *com,
1376                                           struct dt_object *obj,
1377                                           struct lmv_mds_md_v1 *lmv,
1378                                           const struct lu_fid *cfid,
1379                                           __u32 cidx, __u32 flags)
1380 {
1381         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1382         struct lmv_mds_md_v1            *lmv3   = &info->lti_lmv3;
1383         struct lu_seq_range             *range  = &info->lti_range;
1384         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1385         struct seq_server_site          *ss     = lfsck_dev_site(lfsck);
1386         struct lustre_handle             lh     = { 0 };
1387         int                              pidx   = -1;
1388         int                              rc     = 0;
1389         ENTRY;
1390
1391         fld_range_set_mdt(range);
1392         rc = fld_server_lookup(env, ss->ss_server_fld,
1393                                fid_seq(lfsck_dto2fid(obj)), range);
1394         if (rc != 0)
1395                 GOTO(log, rc);
1396
1397         pidx = range->lsr_index;
1398         rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1399                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1400                               LCK_EX);
1401         if (rc != 0)
1402                 GOTO(log, rc);
1403
1404         rc = lfsck_read_stripe_lmv(env, obj, lmv3);
1405         if (rc == -ENODATA) {
1406                 if (!(flags & LEF_SET_LMV_ALL))
1407                         GOTO(log, rc);
1408
1409                 *lmv3 = *lmv;
1410         } else if (rc == 0) {
1411                 if (flags & LEF_SET_LMV_ALL)
1412                         GOTO(log, rc = 1);
1413
1414                 if (flags & LEF_SET_LMV_HASH)
1415                         lmv3->lmv_hash_type = lmv->lmv_hash_type;
1416         } else {
1417                 GOTO(log, rc);
1418         }
1419
1420         lmv3->lmv_magic = LMV_MAGIC;
1421         lmv3->lmv_master_mdt_index = pidx;
1422         lmv3->lmv_layout_version++;
1423
1424         if (flags & LEF_SET_LMV_ALL) {
1425                 rc = lfsck_allow_regenerate_master_lmv(env, com, obj,
1426                                                        cfid, cidx);
1427                 if (rc > 0) {
1428                         rc = lfsck_disable_master_lmv(env, com, obj, false);
1429
1430                         GOTO(log, rc = (rc == 0 ? 1 : rc));
1431                 }
1432
1433                 if (rc < 0)
1434                         GOTO(log, rc);
1435
1436                 /* To indicate that the master has ever lost LMV EA. */
1437                 lmv3->lmv_hash_type |= LMV_HASH_FLAG_LOST_LMV;
1438         }
1439
1440         rc = lfsck_namespace_update_lmv(env, com, obj, lmv3, true);
1441         if (rc == 0 && flags & LEF_SET_LMV_ALL) {
1442                 if (dt_object_remote(obj))
1443                         rc = lfsck_namespace_notify_lmv_remote(env, com, obj,
1444                                                 LE_SET_LMV_MASTER, 0, pidx);
1445                 else
1446                         rc = lfsck_namespace_notify_lmv_master_local(env, com,
1447                                                                      obj);
1448         }
1449
1450         GOTO(log, rc);
1451
1452 log:
1453         lfsck_ibits_unlock(&lh, LCK_EX);
1454         CDEBUG(D_LFSCK, "%s: namespace LFSCK set master LMV EA for the object "
1455                DFID" on the %s MDT %d, flags %x: rc = %d\n",
1456                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
1457                dt_object_remote(obj) ? "remote" : "local", pidx, flags, rc);
1458
1459         if (rc <= 0) {
1460                 struct lfsck_namespace *ns = com->lc_file_ram;
1461
1462                 ns->ln_flags |= LF_INCONSISTENT;
1463         }
1464
1465         return rc;
1466 }
1467
1468 /**
1469  * Repair the bad name hash.
1470  *
1471  * If the name hash of some name entry under the striped directory does not
1472  * match the shard of the striped directory, then the LFSCK will repair the
1473  * inconsistency. Ideally, the LFSCK should migrate the name entry from the
1474  * current MDT to the right MDT (another one), but before the async commit
1475  * finished, the LFSCK will change the striped directory's hash type as
1476  * LMV_HASH_TYPE_UNKNOWN and mark the lmv flags as LMV_HASH_FLAG_BAD_TYPE.
1477  *
1478  * \param[in] env       pointer to the thread context
1479  * \param[in] com       pointer to the lfsck component
1480  * \param[in] shard     pointer to the shard of the striped directory that
1481  *                      contains the bad name entry
1482  * \param[in] llmv      pointer to lfsck LMV EA structure
1483  * \param[in] name      the name of the bad name hash
1484  *
1485  * \retval              positive number if nothing to be done
1486  * \retval              zero for success
1487  * \retval              negative error number on failure
1488  */
1489 int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
1490                                          struct lfsck_component *com,
1491                                          struct dt_object *shard,
1492                                          struct lfsck_lmv *llmv,
1493                                          const char *name)
1494 {
1495         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1496         struct lu_fid                   *pfid   = &info->lti_fid3;
1497         struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
1498         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1499         struct dt_object                *parent = NULL;
1500         int                              rc     = 0;
1501         ENTRY;
1502
1503         rc = dt_lookup(env, shard, (struct dt_rec *)pfid,
1504                        (const struct dt_key *)dotdot);
1505         if (rc != 0 || !fid_is_sane(pfid))
1506                 GOTO(log, rc);
1507
1508         parent = lfsck_object_find_bottom(env, lfsck, pfid);
1509         if (IS_ERR(parent))
1510                 GOTO(log, rc = PTR_ERR(parent));
1511
1512         if (unlikely(!dt_object_exists(parent)))
1513                 /* The parent object was previously accessed when verifying
1514                  * the slave LMV EA.  If this condition is true it is because
1515                  * the striped directory is being removed. */
1516                 GOTO(log, rc = 1);
1517
1518         *lmv2 = llmv->ll_lmv;
1519         lmv2->lmv_hash_type = LMV_HASH_TYPE_UNKNOWN | LMV_HASH_FLAG_BAD_TYPE;
1520         rc = lfsck_namespace_set_lmv_master(env, com, parent, lmv2,
1521                                             lfsck_dto2fid(shard),
1522                                             llmv->ll_lmv.lmv_master_mdt_index,
1523                                             LEF_SET_LMV_HASH);
1524
1525         GOTO(log, rc);
1526
1527 log:
1528         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name hash "
1529                "on the MDT %x, parent "DFID", name %s, shard_%x "DFID
1530                ": rc = %d\n",
1531                lfsck_lfsck2name(lfsck), lfsck_dev_idx(lfsck),
1532                PFID(pfid), name, llmv->ll_lmv.lmv_master_mdt_index,
1533                PFID(lfsck_dto2fid(shard)), rc);
1534
1535         if (parent != NULL && !IS_ERR(parent))
1536                 lfsck_object_put(env, parent);
1537
1538         return rc;
1539 }
1540
1541 /**
1542  * Scan the shard of a striped directory for name hash verification.
1543  *
1544  * During the first-stage scanning, if the LFSCK cannot make sure whether
1545  * the shard of a stripe directory contains valid slave LMV EA or not, then
1546  * it will skip the name hash verification for this shard temporarily, and
1547  * record the shard's FID in the LFSCK tracing file. As the LFSCK processing,
1548  * the slave LMV EA may has been verified/fixed by LFSCK instance on master.
1549  * Then in the second-stage scanning, the shard will be re-scanned, and for
1550  * every name entry under the shard, the name hash will be verified, and for
1551  * unmatched name entry, the LFSCK will try to fix it.
1552  *
1553  * \param[in] env       pointer to the thread context
1554  * \param[in] com       pointer to the lfsck component
1555  * \param[in] child     pointer to the directory object to be handled
1556  *
1557  * \retval              positive number for scanning successfully
1558  * \retval              zero for the scanning is paused
1559  * \retval              negative error number on failure
1560  */
1561 int lfsck_namespace_scan_shard(const struct lu_env *env,
1562                                struct lfsck_component *com,
1563                                struct dt_object *child)
1564 {
1565         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1566         struct lmv_mds_md_v1            *lmv    = &info->lti_lmv;
1567         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1568         struct lfsck_namespace          *ns     = com->lc_file_ram;
1569         struct ptlrpc_thread            *thread = &lfsck->li_thread;
1570         struct lu_dirent                *ent    =
1571                         (struct lu_dirent *)info->lti_key;
1572         struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
1573         struct lfsck_lmv                *llmv   = NULL;
1574         const struct dt_it_ops          *iops;
1575         struct dt_it                    *di;
1576         __u64                            cookie;
1577         __u32                            args;
1578         int                              rc;
1579         __u16                            type;
1580         ENTRY;
1581
1582         rc = lfsck_read_stripe_lmv(env, child, lmv);
1583         if (rc != 0)
1584                 RETURN(rc == -ENODATA ? 1 : rc);
1585
1586         if (lmv->lmv_magic != LMV_MAGIC_STRIPE)
1587                 RETURN(1);
1588
1589         if (unlikely(!dt_try_as_dir(env, child)))
1590                 RETURN(-ENOTDIR);
1591
1592         OBD_ALLOC_PTR(llmv);
1593         if (llmv == NULL)
1594                 RETURN(-ENOMEM);
1595
1596         llmv->ll_lmv_slave = 1;
1597         llmv->ll_lmv_verified = 1;
1598         llmv->ll_lmv = *lmv;
1599         atomic_set(&llmv->ll_ref, 1);
1600
1601         args = lfsck->li_args_dir & ~(LUDA_VERIFY | LUDA_VERIFY_DRYRUN);
1602         iops = &child->do_index_ops->dio_it;
1603         di = iops->init(env, child, args);
1604         if (IS_ERR(di))
1605                 GOTO(out, rc = PTR_ERR(di));
1606
1607         rc = iops->load(env, di, 0);
1608         if (rc == 0)
1609                 rc = iops->next(env, di);
1610         else if (rc > 0)
1611                 rc = 0;
1612
1613         while (rc == 0) {
1614                 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
1615                     unlikely(!thread_is_running(thread)))
1616                         GOTO(out, rc = 0);
1617
1618                 rc = iops->rec(env, di, (struct dt_rec *)ent, args);
1619                 if (rc == 0)
1620                         rc = lfsck_unpack_ent(ent, &cookie, &type);
1621
1622                 if (rc != 0) {
1623                         if (bk->lb_param & LPF_FAILOUT)
1624                                 GOTO(out, rc);
1625
1626                         goto next;
1627                 }
1628
1629                 /* skip dot and dotdot entries */
1630                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1631                         goto next;
1632
1633                 if (!lfsck_is_valid_slave_name_entry(env, llmv, ent->lde_name,
1634                                                      ent->lde_namelen)) {
1635                         ns->ln_flags |= LF_INCONSISTENT;
1636                         rc = lfsck_namespace_repair_bad_name_hash(env, com,
1637                                                 child, llmv, ent->lde_name);
1638                         if (rc == 0)
1639                                 ns->ln_name_hash_repaired++;
1640                 }
1641
1642                 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1643                         GOTO(out, rc);
1644
1645                 /* Rate control. */
1646                 lfsck_control_speed(lfsck);
1647                 if (unlikely(!thread_is_running(thread)))
1648                         GOTO(out, rc = 0);
1649
1650                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
1651                         spin_lock(&lfsck->li_lock);
1652                         thread_set_flags(thread, SVC_STOPPING);
1653                         spin_unlock(&lfsck->li_lock);
1654
1655                         GOTO(out, rc = -EINVAL);
1656                 }
1657
1658 next:
1659                 rc = iops->next(env, di);
1660         }
1661
1662         GOTO(out, rc);
1663
1664 out:
1665         iops->put(env, di);
1666         iops->fini(env, di);
1667         lfsck_lmv_put(env, llmv);
1668
1669         return rc;
1670 }
1671
1672 /**
1673  * Verify the slave object's (of striped directory) LMV EA.
1674  *
1675  * For the slave object of a striped directory, before traversing the shard
1676  * the LFSCK will verify whether its slave LMV EA matches its parent's master
1677  * LMV EA or not.
1678  *
1679  * \param[in] env       pointer to the thread context
1680  * \param[in] com       pointer to the lfsck component
1681  * \param[in] obj       pointer to the object which LMV EA will be checked
1682  * \param[in] llmv      pointer to buffer holding the slave LMV EA
1683  *
1684  * \retval              positive number if nothing to be done
1685  * \retval              zero for success
1686  * \retval              negative error number on failure
1687  */
1688 int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
1689                                         struct lfsck_component *com,
1690                                         struct dt_object *obj,
1691                                         struct lfsck_lmv *llmv)
1692 {
1693         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1694         char                            *name   = info->lti_key;
1695         char                            *name2;
1696         struct lu_fid                   *pfid   = &info->lti_fid3;
1697         const struct lu_fid             *cfid   = lfsck_dto2fid(obj);
1698         struct lu_fid                    tfid;
1699         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1700         struct lmv_mds_md_v1            *clmv   = &llmv->ll_lmv;
1701         struct lmv_mds_md_v1            *plmv   = &info->lti_lmv;
1702         struct dt_object                *parent = NULL;
1703         int                              rc     = 0;
1704         ENTRY;
1705
1706         if (!lfsck_is_valid_slave_lmv(clmv)) {
1707                 rc = lfsck_namespace_trace_update(env, com, cfid,
1708                                         LNTF_UNCERTAIN_LMV, true);
1709
1710                 GOTO(out, rc);
1711         }
1712
1713         rc = dt_lookup(env, obj, (struct dt_rec *)pfid,
1714                        (const struct dt_key *)dotdot);
1715         if (rc != 0 || !fid_is_sane(pfid)) {
1716                 rc = lfsck_namespace_trace_update(env, com, cfid,
1717                                         LNTF_UNCERTAIN_LMV, true);
1718
1719                 GOTO(out, rc);
1720         }
1721
1722         CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
1723
1724         parent = lfsck_object_find_bottom(env, lfsck, pfid);
1725         if (IS_ERR(parent)) {
1726                 rc = lfsck_namespace_trace_update(env, com, cfid,
1727                                         LNTF_UNCERTAIN_LMV, true);
1728
1729                 GOTO(out, rc);
1730         }
1731
1732         if (unlikely(!dt_object_exists(parent)))
1733                 GOTO(out, rc = 1);
1734
1735         if (unlikely(!dt_try_as_dir(env, parent)))
1736                 GOTO(out, rc = -ENOTDIR);
1737
1738         rc = lfsck_read_stripe_lmv(env, parent, plmv);
1739         if (rc != 0) {
1740                 int rc1;
1741
1742                 /* If the parent has no LMV EA, then it maybe because:
1743                  * 1) The parent lost the LMV EA.
1744                  * 2) The child claims a wrong (slave) LMV EA. */
1745                 if (rc == -ENODATA)
1746                         rc = lfsck_namespace_set_lmv_master(env, com, parent,
1747                                         clmv, cfid, clmv->lmv_master_mdt_index,
1748                                         LEF_SET_LMV_ALL);
1749                 else
1750                         rc = 0;
1751
1752                 rc1 = lfsck_namespace_trace_update(env, com, cfid,
1753                                                    LNTF_UNCERTAIN_LMV, true);
1754
1755                 GOTO(out, rc = (rc < 0 ? rc : rc1));
1756         }
1757
1758         /* Unmatched magic or stripe count. */
1759         if (unlikely(plmv->lmv_magic != LMV_MAGIC ||
1760                      plmv->lmv_stripe_count != clmv->lmv_stripe_count)) {
1761                 rc = lfsck_namespace_trace_update(env, com, cfid,
1762                                                   LNTF_UNCERTAIN_LMV, true);
1763
1764                 GOTO(out, rc);
1765         }
1766
1767         /* If the master hash type has been set as LMV_HASH_TYPE_UNKNOWN,
1768          * then the slave hash type is not important. */
1769         if ((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) ==
1770             LMV_HASH_TYPE_UNKNOWN &&
1771             plmv->lmv_hash_type & LMV_HASH_FLAG_BAD_TYPE)
1772                 GOTO(out, rc = 0);
1773
1774         /* Unmatched hash type. */
1775         if (unlikely((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) !=
1776                      (clmv->lmv_hash_type & LMV_HASH_TYPE_MASK))) {
1777                 rc = lfsck_namespace_trace_update(env, com, cfid,
1778                                                   LNTF_UNCERTAIN_LMV, true);
1779
1780                 GOTO(out, rc);
1781         }
1782
1783         snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
1784                  PFID(cfid), clmv->lmv_master_mdt_index);
1785         name2 = info->lti_tmpbuf2;
1786
1787         rc = lfsck_links_get_first(env, obj, name, &tfid);
1788         if (rc == 0 && strcmp(name, name2) == 0 && lu_fid_eq(pfid, &tfid)) {
1789                 llmv->ll_lmv_verified = 1;
1790
1791                 GOTO(out, rc);
1792         }
1793
1794         rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1795                        (const struct dt_key *)name2);
1796         if (rc != 0 || !lu_fid_eq(cfid, &tfid))
1797                 rc = lfsck_namespace_trace_update(env, com, cfid,
1798                                                   LNTF_UNCERTAIN_LMV, true);
1799         else
1800                 llmv->ll_lmv_verified = 1;
1801
1802         GOTO(out, rc);
1803
1804 out:
1805         if (parent != NULL && !IS_ERR(parent))
1806                 lfsck_object_put(env, parent);
1807
1808         return rc;
1809 }
1810
1811 /**
1812  * Double scan the striped directory or the shard.
1813  *
1814  * All the shards' under the given striped directory or its shard have
1815  * been scanned, the LFSCK has got the global knownledge about the LMV
1816  * EA consistency.
1817  *
1818  * If the target is one shard of a striped directory, then only needs to
1819  * update related tracing file.
1820  *
1821  * If the target is the master MDT-object of a striped directory, then the
1822  * LFSCK will make the decision about whether the master LMV EA is invalid
1823  * or not, and repair it if inconsistenct; for every shard of the striped
1824  * directory, whether the slave LMV EA is invalid or not, and repair it if
1825  * inconsistent.
1826  *
1827  * \param[in] env       pointer to the thread context
1828  * \param[in] com       pointer to the lfsck component
1829  * \param[in] lnr       pointer to the namespace request that contains the
1830  *                      striped directory or the shard
1831  *
1832  * \retval              zero for success
1833  * \retval              negative error number on failure
1834  */
1835 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
1836                                        struct lfsck_component *com,
1837                                        struct lfsck_namespace_req *lnr)
1838 {
1839         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1840         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1841         struct lfsck_namespace          *ns     = com->lc_file_ram;
1842         struct lfsck_lmv                *llmv   = lnr->lnr_lmv;
1843         struct lmv_mds_md_v1            *lmv    = &llmv->ll_lmv;
1844         struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
1845         struct lfsck_assistant_object   *lso    = lnr->lnr_lar.lar_parent;
1846         const struct lu_fid             *pfid   = &lso->lso_fid;
1847         struct dt_object                *dir    = NULL;
1848         struct dt_object                *obj    = NULL;
1849         struct lu_seq_range             *range  = &info->lti_range;
1850         struct seq_server_site          *ss     = lfsck_dev_site(lfsck);
1851         __u32                            stripe_count;
1852         __u32                            hash_type;
1853         int                              rc     = 0;
1854         int                              i;
1855         ENTRY;
1856
1857         if (llmv->ll_lmv_slave) {
1858                 if (llmv->ll_lmv_verified) {
1859                         ns->ln_striped_shards_scanned++;
1860                         lfsck_namespace_trace_update(env, com, pfid,
1861                                         LNTF_UNCERTAIN_LMV |
1862                                         LNTF_RECHECK_NAME_HASH, false);
1863                 }
1864
1865                 RETURN(0);
1866         }
1867
1868         /* Either the striped directory has been disabled or only part of
1869          * the striped directory have been scanned. The LFSCK cannot repair
1870          * something based on incompleted knowledge. So skip it. */
1871         if (llmv->ll_ignore || llmv->ll_exit_value <= 0)
1872                 RETURN(0);
1873
1874         /* There ever been some failure, as to the LFSCK cannot know whether
1875          * it has got the global knowledge about the LMV EA consistency or not,
1876          * so it cannot make reparation about the incompleted knowledge. */
1877         if (llmv->ll_failed) {
1878                 ns->ln_striped_dirs_scanned++;
1879                 ns->ln_striped_dirs_failed++;
1880
1881                 RETURN(0);
1882         }
1883
1884         if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
1885                 stripe_count = max(llmv->ll_max_filled_off + 1,
1886                                    llmv->ll_max_stripe_count);
1887         else
1888                 stripe_count = max(llmv->ll_max_filled_off + 1,
1889                                    lmv->lmv_stripe_count);
1890
1891         if (lmv->lmv_stripe_count != stripe_count) {
1892                 lmv->lmv_stripe_count = stripe_count;
1893                 llmv->ll_lmv_updated = 1;
1894         }
1895
1896         if (!lmv_is_known_hash_type(lmv->lmv_hash_type) &&
1897             !(lmv->lmv_hash_type & LMV_HASH_FLAG_BAD_TYPE) &&
1898             lmv_is_known_hash_type(llmv->ll_hash_type)) {
1899                 hash_type = llmv->ll_hash_type & LMV_HASH_TYPE_MASK;
1900                 lmv->lmv_hash_type = llmv->ll_hash_type;
1901                 llmv->ll_lmv_updated = 1;
1902         } else {
1903                 hash_type = lmv->lmv_hash_type & LMV_HASH_TYPE_MASK;
1904                 if (!lmv_is_known_hash_type(hash_type))
1905                         hash_type = LMV_HASH_TYPE_UNKNOWN;
1906         }
1907
1908         if (llmv->ll_lmv_updated) {
1909                 if (dir == NULL) {
1910                         dir = lfsck_assistant_object_load(env, lfsck, lso);
1911                         if (IS_ERR(dir)) {
1912                                 rc = PTR_ERR(dir);
1913
1914                                 RETURN(rc == -ENOENT ? 0 : rc);
1915                         }
1916                 }
1917
1918                 lmv->lmv_layout_version++;
1919                 rc = lfsck_namespace_update_lmv(env, com, dir, lmv, false);
1920                 if (rc != 0)
1921                         RETURN(rc);
1922
1923                 ns->ln_striped_dirs_scanned++;
1924                 if (!llmv->ll_counted)
1925                         ns->ln_striped_dirs_repaired++;
1926         }
1927
1928         fld_range_set_mdt(range);
1929         for (i = 0; i <= llmv->ll_max_filled_off; i++) {
1930                 struct lfsck_slave_lmv_rec *lslr = llmv->ll_lslr + i;
1931                 const struct lu_fid *cfid = &lslr->lslr_fid;
1932                 const struct lu_name *cname;
1933                 struct linkea_data ldata = { NULL };
1934                 int rc1 = 0;
1935                 bool repair_linkea = false;
1936                 bool repair_lmvea = false;
1937                 bool rename = false;
1938                 bool create = false;
1939                 bool linkea_repaired = false;
1940                 bool lmvea_repaired = false;
1941                 bool rename_repaired = false;
1942                 bool create_repaired = false;
1943
1944                 /* LMV EA hole. */
1945                 if (fid_is_zero(cfid))
1946                         continue;
1947
1948                 lnr->lnr_fid = *cfid;
1949                 lnr->lnr_namelen = snprintf(lnr->lnr_name,
1950                                             lnr->lnr_size - sizeof(*lnr),
1951                                             DFID":%u", PFID(cfid), i);
1952                 cname = lfsck_name_get_const(env, lnr->lnr_name,
1953                                              lnr->lnr_namelen);
1954                 obj = lfsck_object_find_bottom(env, lfsck, cfid);
1955                 if (IS_ERR(obj)) {
1956                         if (dir == NULL) {
1957                                 dir = lfsck_assistant_object_load(env, lfsck,
1958                                                                   lso);
1959                                 if (IS_ERR(dir)) {
1960                                         if (PTR_ERR(dir) == -ENOENT)
1961                                                 RETURN(0);
1962
1963                                         dir = NULL;
1964                                 }
1965                         } else if (lfsck_is_dead_obj(dir)) {
1966                                 GOTO(out, rc = 0);
1967                         }
1968
1969                         rc1 = PTR_ERR(obj);
1970                         goto next;
1971                 }
1972
1973                 switch (lslr->lslr_flags) {
1974                 case LSLF_NONE:
1975                         if (llmv->ll_inline ||
1976                             lslr->lslr_stripe_count != stripe_count ||
1977                             (lslr->lslr_hash_type & LMV_HASH_TYPE_MASK) !=
1978                              hash_type)
1979                                 repair_lmvea = true;
1980                         break;
1981                 case LSLF_BAD_INDEX2:
1982                         /* The index in the slave LMV EA is right,
1983                          * the name entry should be updated. */
1984                         rename = true;
1985                         snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2),
1986                                  DFID":%u", PFID(cfid), lslr->lslr_index);
1987                         if (llmv->ll_inline ||
1988                             lslr->lslr_stripe_count != stripe_count ||
1989                             (lslr->lslr_hash_type & LMV_HASH_TYPE_MASK) !=
1990                              hash_type)
1991                                 repair_lmvea = true;
1992                         break;
1993                 case LSLF_BAD_INDEX1:
1994                         /* The index in the name entry is right,
1995                          * the slave LMV EA should be updated. */
1996                 case LSLF_NO_LMVEA:
1997                         repair_lmvea = true;
1998                         break;
1999                 case LSLF_DANGLING:
2000                         create = true;
2001                         goto repair;
2002                 default:
2003                         break;
2004                 }
2005
2006                 rc1 = lfsck_links_read_with_rec(env, obj, &ldata);
2007                 if (rc1 == -ENOENT) {
2008                         create = true;
2009                         goto repair;
2010                 }
2011
2012                 if (rc1 == -EINVAL || rc1 == -ENODATA) {
2013                         repair_linkea = true;
2014                         goto repair;
2015                 }
2016
2017                 if (rc1 != 0)
2018                         goto next;
2019
2020                 if (ldata.ld_leh->leh_reccount != 1) {
2021                         repair_linkea = true;
2022                         goto repair;
2023                 }
2024
2025                 rc1 = linkea_links_find(&ldata, cname, pfid);
2026                 if (rc1 != 0)
2027                         repair_linkea = true;
2028
2029 repair:
2030                 if (create) {
2031                         if (dir == NULL) {
2032                                 dir = lfsck_assistant_object_load(env, lfsck,
2033                                                                   lso);
2034                                 if (IS_ERR(dir)) {
2035                                         rc1 = PTR_ERR(dir);
2036
2037                                         if (rc1 == -ENOENT)
2038                                                 GOTO(out, rc = 0);
2039
2040                                         dir = NULL;
2041                                         goto next;
2042                                 }
2043                         }
2044
2045                         rc1 = lfsck_namespace_repair_dangling(env, com, dir,
2046                                                               obj, lnr);
2047                         if (rc1 >= 0) {
2048                                 create_repaired = true;
2049                                 if (rc == 0)
2050                                         ns->ln_dangling_repaired++;
2051                         }
2052                 }
2053
2054                 if (repair_lmvea) {
2055                         *lmv2 = *lmv;
2056                         lmv2->lmv_magic = LMV_MAGIC_STRIPE;
2057                         lmv2->lmv_stripe_count = stripe_count;
2058                         lmv2->lmv_master_mdt_index = i;
2059                         lmv2->lmv_hash_type = hash_type;
2060
2061                         rc1 = lfsck_namespace_update_lmv(env, com, obj,
2062                                                          lmv2, false);
2063                         if (rc1 < 0)
2064                                 goto next;
2065
2066                         if (dt_object_remote(obj)) {
2067                                 rc1 = fld_server_lookup(env, ss->ss_server_fld,
2068                                         fid_seq(lfsck_dto2fid(obj)), range);
2069                                 if (rc1 != 0)
2070                                         goto next;
2071
2072                                 rc1 = lfsck_namespace_notify_lmv_remote(env,
2073                                                 com, obj, LE_SET_LMV_SLAVE, 0,
2074                                                 range->lsr_index);
2075                         } else {
2076                                 ns->ln_striped_shards_repaired++;
2077                                 rc1 = lfsck_namespace_trace_update(env, com,
2078                                         cfid, LNTF_RECHECK_NAME_HASH, true);
2079                         }
2080
2081                         if (rc1 < 0)
2082                                 goto next;
2083
2084                         if (rc1 >= 0)
2085                                 lmvea_repaired = true;
2086                 } else if (llmv->ll_inline) {
2087                         if (dt_object_remote(obj)) {
2088                                 rc1 = fld_server_lookup(env, ss->ss_server_fld,
2089                                         fid_seq(lfsck_dto2fid(obj)), range);
2090                                 if (rc1 != 0)
2091                                         goto next;
2092
2093                                 /* The slave LMV EA on the remote shard is
2094                                  * correct, just notify the LFSCK instance
2095                                  * on such MDT to re-verify the name_hash. */
2096                                 rc1 = lfsck_namespace_notify_lmv_remote(env,
2097                                                 com, obj, LE_SET_LMV_SLAVE,
2098                                                 LEF_RECHECK_NAME_HASH,
2099                                                 range->lsr_index);
2100                         } else {
2101                                 rc1 = lfsck_namespace_trace_update(env, com,
2102                                         cfid, LNTF_RECHECK_NAME_HASH, true);
2103                         }
2104
2105                         if (rc1 < 0)
2106                                 goto next;
2107                 }
2108
2109                 if (rename) {
2110                         if (dir == NULL) {
2111                                 dir = lfsck_assistant_object_load(env, lfsck,
2112                                                                   lso);
2113                                 if (IS_ERR(dir)) {
2114                                         rc1 = PTR_ERR(dir);
2115
2116                                         if (rc1 == -ENOENT)
2117                                                 GOTO(out, rc = 0);
2118
2119                                         dir = NULL;
2120                                         goto next;
2121                                 }
2122                         }
2123
2124                         rc1 = lfsck_namespace_repair_dirent(env, com, dir, obj,
2125                                         info->lti_tmpbuf2, lnr->lnr_name,
2126                                         lnr->lnr_type, true, false);
2127                         if (rc1 >= 0) {
2128                                 rename_repaired = true;
2129                                 if (rc1 > 0) {
2130                                         ns->ln_dirent_repaired++;
2131                                         rc1 = lfsck_namespace_trace_update(env,
2132                                                 com, cfid,
2133                                                 LNTF_RECHECK_NAME_HASH, true);
2134                                 }
2135                         }
2136
2137                         if (rc1 < 0)
2138                                 goto next;
2139                 }
2140
2141                 if (repair_linkea) {
2142                         struct lustre_handle lh = { 0 };
2143
2144                         rc1 = linkea_links_new(&ldata, &info->lti_big_buf,
2145                                                cname, lfsck_dto2fid(dir));
2146                         if (rc1 != 0)
2147                                 goto next;
2148
2149                         if (dir == NULL) {
2150                                 dir = lfsck_assistant_object_load(env, lfsck,
2151                                                                   lso);
2152                                 if (IS_ERR(dir)) {
2153                                         rc1 = PTR_ERR(dir);
2154
2155                                         if (rc1 == -ENOENT)
2156                                                 GOTO(out, rc = 0);
2157
2158                                         dir = NULL;
2159                                         goto next;
2160                                 }
2161                         }
2162
2163                         rc1 = lfsck_ibits_lock(env, lfsck, obj, &lh,
2164                                                MDS_INODELOCK_UPDATE |
2165                                                MDS_INODELOCK_XATTR, LCK_EX);
2166                         if (rc1 != 0)
2167                                 goto next;
2168
2169                         rc1 = lfsck_namespace_rebuild_linkea(env, com, obj,
2170                                                              &ldata);
2171                         lfsck_ibits_unlock(&lh, LCK_EX);
2172                         if (rc1 >= 0) {
2173                                 linkea_repaired = true;
2174                                 if (rc1 > 0)
2175                                         ns->ln_linkea_repaired++;
2176                         }
2177                 }
2178
2179 next:
2180                 CDEBUG(D_LFSCK, "%s: namespace LFSCK repair the shard "
2181                       "%d "DFID" of the striped directory "DFID" with "
2182                       "dangling %s/%s, rename %s/%s, llinkea %s/%s, "
2183                       "repair_lmvea %s/%s: rc = %d\n", lfsck_lfsck2name(lfsck),
2184                       i, PFID(cfid), PFID(&lnr->lnr_fid),
2185                       create ? "yes" : "no", create_repaired ? "yes" : "no",
2186                       rename ? "yes" : "no", rename_repaired ? "yes" : "no",
2187                       repair_linkea ? "yes" : "no",
2188                       linkea_repaired ? "yes" : "no",
2189                       repair_lmvea ? "yes" : "no",
2190                       lmvea_repaired ? "yes" : "no", rc1);
2191
2192                 if (obj != NULL && !IS_ERR(obj)) {
2193                         lfsck_object_put(env, obj);
2194                         obj = NULL;
2195                 }
2196
2197                 if (rc1 < 0) {
2198                         rc = rc1;
2199                         ns->ln_striped_shards_failed++;
2200                 }
2201         }
2202
2203         GOTO(out, rc);
2204
2205 out:
2206         if (obj != NULL && !IS_ERR(obj))
2207                 lfsck_object_put(env, obj);
2208
2209         if (dir != NULL && !IS_ERR(dir))
2210                 lfsck_object_put(env, dir);
2211
2212         return rc;
2213 }
2214
2215 /**
2216  * Verify the shard's name entry under the striped directory.
2217  *
2218  * Before all shards of the striped directory scanned, the LFSCK cannot
2219  * know whether the master LMV EA is valid or not, and also cannot know
2220  * how to repair an invalid shard exactly. For example, the stripe index
2221  * stored in the shard's name does not match the stripe index stored in
2222  * the slave LMV EA, then the LFSCK cannot know which one is correct.
2223  * If the LFSCK just assumed one is correct, and fixed the other, then
2224  * as the LFSCK processing, it may find that the former reparation is
2225  * wrong and have to roll back. Unfortunately, if some applications saw
2226  * the changes and made further modification based on such changes, then
2227  * the roll back is almost impossible.
2228  *
2229  * To avoid above trouble, the LFSCK will scan the master object of the
2230  * striped directory twice, that is NOT the same as normal two-stages
2231  * scanning, the double scanning the striped directory will happen both
2232  * during the first-stage scanning:
2233  *
2234  * 1) When the striped directory is opened for scanning, the LFSCK will
2235  *    iterate each shard in turn, and records its slave LMV EA in the
2236  *    lfsck_lmv::ll_lslr. In this step, if the 'shard' (may be fake
2237  *    shard) name does not match the shard naming rule, for example, it
2238  *    does not contains the shard's FID, or not contains index, then we
2239  *    can remove the bad name entry directly. But if the name is valid,
2240  *    but the shard has no slave LMV EA or the slave LMV EA does not
2241  *    match its name, then we just record related information in the
2242  *    lfsck_lmv::ll_lslr in RAM.
2243  *
2244  * 2) When all the known shards have been scanned, then the engine will
2245  *    generate a dummy request (via lfsck_namespace_close_dir) to tell
2246  *    the assistant thread that all the known shards have been scanned.
2247  *    Since the assistant has got the global knowledge about the index
2248  *    conflict, stripe count, hash type, and so on. Then the assistant
2249  *    thread will scan the lfsck_lmv::ll_lslr, and for every shard in
2250  *    the record, check and repair inconsistency.
2251  *
2252  * Generally, the stripe directory has only several shards, and there
2253  * will NOT be a lof of striped directory. So double scanning striped
2254  * directory will not much affect the LFSCK performance.
2255  *
2256  * \param[in] env       pointer to the thread context
2257  * \param[in] com       pointer to the lfsck component
2258  * \param[in] lnr       pointer to the namespace request that contains the
2259  *                      shard's name, parent object, parent's LMV, and ect.
2260  *
2261  * \retval              zero for success
2262  * \retval              negative error number on failure
2263  */
2264 int lfsck_namespace_handle_striped_master(const struct lu_env *env,
2265                                           struct lfsck_component *com,
2266                                           struct lfsck_namespace_req *lnr)
2267 {
2268         struct lfsck_thread_info   *info        = lfsck_env_info(env);
2269         struct lmv_mds_md_v1       *lmv         = &info->lti_lmv;
2270         struct lfsck_instance      *lfsck       = com->lc_lfsck;
2271         struct lfsck_namespace     *ns          = com->lc_file_ram;
2272         struct lfsck_lmv           *llmv        = lnr->lnr_lmv;
2273         struct lfsck_assistant_object *lso      = lnr->lnr_lar.lar_parent;
2274         const struct lu_fid        *pfid        = &lso->lso_fid;
2275         struct dt_object           *dir;
2276         struct dt_object           *obj         = NULL;
2277         struct dt_device           *dev         = NULL;
2278         int                         shard_idx   = 0;
2279         int                         stripe      = 0;
2280         int                         rc          = 0;
2281         int                         depth       = 0;
2282         bool                        repaired    = false;
2283         enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
2284         ENTRY;
2285
2286         if (unlikely(llmv->ll_ignore))
2287                 RETURN(0);
2288
2289         dir = lfsck_assistant_object_load(env, lfsck, lso);
2290         if (IS_ERR(dir)) {
2291                 rc = PTR_ERR(dir);
2292
2293                 RETURN(rc == -ENOENT ? 0 : rc);
2294         }
2295
2296         shard_idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
2297         if (shard_idx < 0)
2298                 GOTO(fail_lmv, rc = shard_idx);
2299
2300         if (shard_idx == lfsck_dev_idx(lfsck)) {
2301                 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
2302                         GOTO(out, rc = 0);
2303
2304                 dev = lfsck->li_bottom;
2305         } else {
2306                 struct lfsck_tgt_desc *ltd;
2307
2308                 /* Usually, some local filesystem consistency verification
2309                  * tools can guarantee the local namespace tree consistenct.
2310                  * So the LFSCK will only verify the remote directory. */
2311                 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
2312                         rc = lfsck_namespace_trace_update(env, com, pfid,
2313                                                 LNTF_CHECK_PARENT, true);
2314
2315                         GOTO(out, rc);
2316                 }
2317
2318                 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, shard_idx);
2319                 if (unlikely(ltd == NULL)) {
2320                         CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
2321                                "did not join the namespace LFSCK\n",
2322                                lfsck_lfsck2name(lfsck), shard_idx);
2323                         lfsck_lad_set_bitmap(env, com, shard_idx);
2324
2325                         GOTO(fail_lmv, rc = -ENODEV);
2326                 }
2327
2328                 dev = ltd->ltd_tgt;
2329         }
2330
2331         obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
2332         if (IS_ERR(obj)) {
2333                 if (lfsck_is_dead_obj(dir))
2334                         RETURN(0);
2335
2336                 GOTO(fail_lmv, rc = PTR_ERR(obj));
2337         }
2338
2339         if (!dt_object_exists(obj)) {
2340                 stripe = lfsck_shard_name_to_index(env, lnr->lnr_name,
2341                                 lnr->lnr_namelen, lnr->lnr_type, &lnr->lnr_fid);
2342                 if (stripe < 0) {
2343                         type = LNIT_BAD_DIRENT;
2344
2345                         GOTO(out, rc = 0);
2346                 }
2347
2348 dangling:
2349                 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
2350                 if (rc == 0) {
2351                         memset(lmv, 0, sizeof(*lmv));
2352                         lmv->lmv_magic = LMV_MAGIC;
2353                         rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2354                                               LSLF_DANGLING, LSLF_NONE, &depth);
2355                 }
2356
2357                 GOTO(out, rc);
2358         }
2359
2360         stripe = lfsck_shard_name_to_index(env, lnr->lnr_name, lnr->lnr_namelen,
2361                                            lfsck_object_type(obj),
2362                                            &lnr->lnr_fid);
2363         if (stripe < 0) {
2364                 type = LNIT_BAD_DIRENT;
2365
2366                 GOTO(out, rc = 0);
2367         }
2368
2369         rc = lfsck_read_stripe_lmv(env, obj, lmv);
2370         if (unlikely(rc == -ENOENT))
2371                 /* It may happen when the remote object has been removed,
2372                  * but the local MDT does not aware of that. */
2373                 goto dangling;
2374
2375         if (rc == -ENODATA)
2376                 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2377                                       LSLF_NO_LMVEA, LSLF_NONE, &depth);
2378         else if (rc == 0)
2379                 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2380                                       lmv->lmv_master_mdt_index != stripe ?
2381                                       LSLF_BAD_INDEX1 : LSLF_NONE, LSLF_NONE,
2382                                       &depth);
2383
2384         GOTO(out, rc);
2385
2386 fail_lmv:
2387         llmv->ll_failed = 1;
2388
2389 out:
2390         if (rc >= 0 && type == LNIT_NONE && !S_ISDIR(lnr->lnr_type))
2391                 type = LNIT_BAD_TYPE;
2392
2393         switch (type) {
2394         case LNIT_BAD_TYPE:
2395                 rc = lfsck_namespace_repair_dirent(env, com, dir, obj,
2396                                                    lnr->lnr_name, lnr->lnr_name,
2397                                                    lnr->lnr_type, true, false);
2398                 if (rc > 0)
2399                         repaired = true;
2400                 break;
2401         case LNIT_BAD_DIRENT:
2402                 rc = lfsck_namespace_repair_dirent(env, com, dir, obj,
2403                                                    lnr->lnr_name, lnr->lnr_name,
2404                                                    lnr->lnr_type, false, false);
2405                 if (rc > 0)
2406                         repaired = true;
2407                 break;
2408         default:
2409                 break;
2410         }
2411
2412         if (rc < 0) {
2413                 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
2414                        "the shard: "DFID", parent "DFID", name %.*s: rc = %d\n",
2415                        lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
2416                        PFID(pfid), lnr->lnr_namelen, lnr->lnr_name, rc);
2417
2418                 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
2419                      rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
2420                      rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
2421                     dev != NULL && dev != lfsck->li_bottom)
2422                         lfsck_lad_set_bitmap(env, com, shard_idx);
2423
2424                 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT))
2425                         rc = 0;
2426         } else {
2427                 if (repaired) {
2428                         ns->ln_items_repaired++;
2429
2430                         switch (type) {
2431                         case LNIT_BAD_TYPE:
2432                                 ns->ln_bad_type_repaired++;
2433                                 break;
2434                         case LNIT_BAD_DIRENT:
2435                                 ns->ln_dirent_repaired++;
2436                                 break;
2437                         default:
2438                                 break;
2439                         }
2440                 }
2441
2442                 rc = 0;
2443         }
2444
2445         if (obj != NULL && !IS_ERR(obj))
2446                 lfsck_object_put(env, obj);
2447
2448         lfsck_object_put(env, dir);
2449
2450         return rc;
2451 }