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