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