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