Whamcloud - gitweb
c6da88607aabe801751c7ef2fe66ef20ac79e047
[fs/lustre-release.git] / lustre / lfsck / lfsck_striped_dir.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_striped_dir.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 /*
32  * About the verification for striped directory. Some rules and assumptions:
33  *
34  * 1) lmv_magic: The magic may be wrong. But it is almost impossible (1/2^32
35  *    probability) that a master LMV EA claims as a slave LMV EA by wrong,
36  *    so we can ignore such race case and the reverse case.
37  *
38  * 2) lmv_master_mdt_index: The master index can be self-verified by compared
39  *    with the MDT index directly. The slave stripe index can be verified by
40  *    compared with the file name. Although both the name entry and the LMV EA
41  *    can be wrong, it is almost impossible that they hit the same bad data
42  *    So if they match each other, then trust them. Similarly, for the shard,
43  *    it stores index in both slave LMV EA and in linkEA, if the two copies
44  *    match, then trust them.
45  *
46  * 3) lmv_hash_type: The valid hash type should be LMV_HASH_TYPE_ALL_CHARS or
47  *    LMV_HASH_TYPE_FNV_1A_64. If the LFSCK instance on some slave finds that
48  *    the name hash against the hash function does not match the MDT, then it
49  *    will change the master LMV EA hash type as LMV_HASH_TYPE_UNKNOWN. With
50  *    such hash type, the whole striped directory still can be accessed via
51  *    lookup/readdir, and also support unlink, but cannot add new name entry.
52  *
53  * 3.1) If the master hash type is one of the valid values, then trust the
54  *      master LMV EA. Because:
55  *
56  * 3.1.1) The master hash type is visible to the client and used by the client.
57  *
58  * 3.1.2) For a given name, different hash types may map the name entry to the
59  *        same MDT. So simply checking one name entry or some name entries may
60  *        cannot verify whether the hash type is correct or not.
61  *
62  * 3.1.3) Different shards can claim different hash types, it is not easy to
63  *        distinguish which ones are correct. Even though the master is wrong,
64  *        as the LFSCK processing, some LFSCK instance on other MDT may finds
65  *        unmatched name hash, then it will change the master hash type to
66  *        LMV_HASH_TYPE_UNKNOWN as described above. The worst case is euqal
67  *        to the case without the LFSCK.
68  *
69  * 3.2) If the master hash type is invalid, nor LMV_HASH_TYPE_UNKNOWN, then
70  *      trust the first shard with valid hash type (ALL_CHARS or FNV_1A_64).
71  *      If the shard is also worng, means there are double failures, then as
72  *      the LFSCK processing, other LFSCK instances on the other MDTs may
73  *      find unmatched name hash, and then, the master hash type will be
74  *      changed to LMV_HASH_TYPE_UNKNOWN as described in the 3).
75  *
76  * 3.3) If the master hash type is LMV_HASH_TYPE_UNKNOWN, then it is possible
77  *      that some other LFSCK instance on other MDT found bad name hash, then
78  *      changed the master hash type to LMV_HASH_TYPE_UNKNOWN as described in
79  *      the 3). But it also maybe because of data corruption in master LMV EA.
80  *      To make such two cases to be distinguishable, when the LFSCK changes
81  *      the master hash type to LMV_HASH_TYPE_UNKNOWN, it will mark in the
82  *      master LMV EA (new lmv flags LMV_HASH_FLAG_BAD_TYPE). Then subsequent
83  *      LFSCK checking can distinguish them: for former case, turst the master
84  *      LMV EA with nothing to be done; otherwise, trust the first shard with
85  *      valid hash type (ALL_CHARS or FNV_1A_64) as the 3.2) does.
86  *
87  * 4) lmv_stripe_count: For a shard of a striped directory, if its index has
88  *    been verified as the 2), then the stripe count must be larger than its
89  *    index. For the master object, by scanning each shard's index, the LFSCK
90  *    can know the highest index, and the stripe count must be larger than the
91  *    known highest index. If the stipe count in the LMV EA matches above two
92  *    rules, then it is may be trustable. If both the master claimed stripe
93  *    count and the slave claimed stripe count match each own rule, but they
94  *    are not the same, then trust the master. Because the stripe count in
95  *    the master LMV EA is visible to client and used to distribute the name
96  *    entry to some shard, but the slave LMV EA is only used for verification
97  *    and invisible to client.
98  *
99  * 5) If the master LMV EA is lost, then there are two possible cases:
100  *
101  * 5.1) The slave claims slave LMV EA by wrong, means that the parent was not
102  *      a striped directory, but its sub-directory has a wrong slave LMV EA.
103  *      It is very very race case, similar as the 1), can be ignored.
104  *
105  * 5.2) The parent directory is a striped directory, but the master LMV EA
106  *      is lost or crashed. Then the LFSCK needs to re-generate the master
107  *      LMV EA: the lmv_master_mdt_index is from the MDT device index; the
108  *      lmv_hash_type is from the first valid shard; the lmv_stripe_count
109  *      will be calculated via scanning all the shards.
110  *
111  * 5.2.1) Before re-generating the master LMV EA, the LFSCK needs to check
112  *        whether someone has created some file(s) under the master object
113  *        after the master LMV EA disappear. If yes, the LFSCK will cannot
114  *        re-generate the master LMV EA, otherwise, such new created files
115  *        will be invisible to client. Under such case, the LFSCK will mark
116  *        the master object as read only (without master LMV EA). Then all
117  *        things under the master MDT-object, including those new created
118  *        files and the shards themselves, will be visibile to client. And
119  *        then the administrator can handle the bad striped directory with
120  *        more human knowledge.
121  *
122  * 5.2.2) If someone created some special sub-directory under the master
123  *        MDT-object with the same naming rule as shard name $FID:$index,
124  *        as to the LFSCK cannot detect it before re-generating the master
125  *        LMV EA, then such sub-directory itself will be invisible after
126  *        the LFSCK re-generating the master LMV EA. The sub-items under
127  *        such sub-directory are still visible to client. As the LFSCK
128  *        processing, if such sub-directory cause some conflict with other
129  *        normal shard, such as the index conflict, then the LFSCK will
130  *        remove the master LMV EA and change the master MDT-object to
131  *        read-only mode as the 5.2.1). But if there is no conflict, the
132  *        LFSCK will regard such sub-directory as a striped shard that
133  *        lost its slave LMV EA, and will re-generate slave LMV EA for it.
134  *
135  * 5.2.3) Anytime, if the LFSCK found some shards name/index conflict,
136  *        and cannot make the distinguish which one is right, then it
137  *        will remove the master LMV EA and change the MDT-object to
138  *        read-only mode as the 5.2.2).
139  */
140
141 #define DEBUG_SUBSYSTEM S_LFSCK
142
143 #include <lustre/lustre_idl.h>
144 #include <lu_object.h>
145 #include <dt_object.h>
146 #include <md_object.h>
147 #include <lustre_fid.h>
148 #include <lustre_lib.h>
149 #include <lustre_net.h>
150 #include <lustre_lmv.h>
151 #include <lustre/lustre_user.h>
152
153 #include "lfsck_internal.h"
154
155 void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv)
156 {
157         if (llmv != NULL && atomic_dec_and_test(&llmv->ll_ref)) {
158                 if (llmv->ll_lslr != NULL)
159                         OBD_FREE_LARGE(llmv->ll_lslr,
160                                 sizeof(struct lfsck_slave_lmv_rec) *
161                                 llmv->ll_stripes_allocated);
162
163                 OBD_FREE_PTR(llmv);
164         }
165 }
166
167 static inline bool lfsck_is_valid_slave_lmv(struct lmv_mds_md_v1 *lmv)
168 {
169         return lmv->lmv_stripe_count >= 1 &&
170                lmv->lmv_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
171                lmv->lmv_stripe_count > lmv->lmv_master_mdt_index &&
172                lmv_is_known_hash_type(lmv->lmv_hash_type);
173 }
174
175 int lfsck_read_stripe_lmv(const struct lu_env *env, struct dt_object *obj,
176                           struct lmv_mds_md_v1 *lmv)
177 {
178         struct dt_object *bottom;
179         int               rc;
180
181         /* Currently, we only store the LMV header on disk. It is the LOD's
182          * duty to iterate the master MDT-object's directory to compose the
183          * integrated LMV EA. But here, we only want to load the LMV header,
184          * so we need to bypass LOD to avoid unnecessary iteration in LOD. */
185         bottom = lu2dt(container_of0(obj->do_lu.lo_header->loh_layers.prev,
186                                      struct lu_object, lo_linkage));
187         if (unlikely(bottom == NULL))
188                 return -ENOENT;
189
190         dt_read_lock(env, bottom, 0);
191         rc = dt_xattr_get(env, bottom, lfsck_buf_get(env, lmv, sizeof(*lmv)),
192                           XATTR_NAME_LMV, BYPASS_CAPA);
193         dt_read_unlock(env, bottom);
194         if (rc != sizeof(*lmv))
195                 return rc > 0 ? -EINVAL : rc;
196
197         lfsck_lmv_header_le_to_cpu(lmv, lmv);
198         if ((lmv->lmv_magic == LMV_MAGIC &&
199              !(lmv->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) ||
200             (lmv->lmv_magic == LMV_MAGIC_STRIPE &&
201              !(lmv->lmv_hash_type & LMV_HASH_FLAG_DEAD)))
202                 return 0;
203
204         return -ENODATA;
205 }
206
207 /**
208  * Parse the shard's index from the given shard name.
209  *
210  * The valid shard name/type should be:
211  * 1) The type must be S_IFDIR
212  * 2) The name should be $FID:$index
213  * 3) the index should within valid range.
214  *
215  * \param[in] env       pointer to the thread context
216  * \param[in] name      the shard name
217  * \param[in] namelen   the name length
218  * \param[in] type      the entry's type
219  * \param[in] fid       the entry's FID
220  *
221  * \retval              zero or positive number for the index from the name
222  * \retval              negative error number on failure
223  */
224 int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
225                               int namelen, __u16 type, const struct lu_fid *fid)
226 {
227         char    *name2  = lfsck_env_info(env)->lti_tmpbuf2;
228         int      len;
229         int      idx    = 0;
230
231         if (!S_ISDIR(type))
232                 return -ENOTDIR;
233
234         LASSERT(name != name2);
235
236         len = snprintf(name2, sizeof(lfsck_env_info(env)->lti_tmpbuf2),
237                        DFID":", PFID(fid));
238         if (namelen < len + 1 || memcmp(name, name2, len) != 0)
239                 return -EINVAL;
240
241         do {
242                 if (!isdigit(name[len]))
243                         return -EINVAL;
244
245                 idx = idx * 10 + name[len++] - '0';
246         } while (len < namelen);
247
248         if (idx >= LFSCK_LMV_MAX_STRIPES)
249                 return -EINVAL;
250
251         return idx;
252 }
253
254 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
255                                      struct lfsck_lmv *llmv,
256                                      const char *name, int namelen)
257 {
258         struct lmv_mds_md_v1    *lmv;
259         int                      idx;
260
261         if (llmv == NULL || !llmv->ll_lmv_slave || !llmv->ll_lmv_verified)
262                 return true;
263
264         lmv = &llmv->ll_lmv;
265         idx = lmv_name_to_stripe_index(lmv->lmv_hash_type,
266                                        lmv->lmv_stripe_count,
267                                        name, namelen);
268         if (unlikely(idx != lmv->lmv_master_mdt_index))
269                 return false;
270
271         return true;
272 }
273
274 /**
275  * Check whether the given name is a valid entry under the @parent.
276  *
277  * If the @parent is a striped directory then the @child should one
278  * shard of the striped directory, its name should be $FID:$index.
279  *
280  * If the @parent is a shard of a striped directory, then the name hash
281  * should match the MDT, otherwise it is invalid.
282  *
283  * \param[in] env       pointer to the thread context
284  * \param[in] parent    the parent directory
285  * \param[in] child     the child object to be checked
286  * \param[in] cname     the name for the @child in the parent directory
287  *
288  * \retval              positive number for invalid name entry
289  * \retval              0 if the name is valid or uncertain
290  * \retval              negative error number on failure
291  */
292 int lfsck_namespace_check_name(const struct lu_env *env,
293                                struct dt_object *parent,
294                                struct dt_object *child,
295                                const struct lu_name *cname)
296 {
297         struct lmv_mds_md_v1    *lmv = &lfsck_env_info(env)->lti_lmv;
298         int                      idx;
299         int                      rc;
300
301         rc = lfsck_read_stripe_lmv(env, parent, lmv);
302         if (rc != 0)
303                 RETURN(rc == -ENODATA ? 0 : rc);
304
305         if (lmv->lmv_magic == LMV_MAGIC_STRIPE) {
306                 if (!lfsck_is_valid_slave_lmv(lmv))
307                         return 0;
308
309                 idx = lmv_name_to_stripe_index(lmv->lmv_hash_type,
310                                                lmv->lmv_stripe_count,
311                                                cname->ln_name,
312                                                cname->ln_namelen);
313                 if (unlikely(idx != lmv->lmv_master_mdt_index))
314                         return 1;
315         } else if (lfsck_shard_name_to_index(env, cname->ln_name,
316                         cname->ln_namelen, lfsck_object_type(child),
317                         lfsck_dto2fid(child)) < 0) {
318                 return 1;
319         }
320
321         return 0;
322 }
323
324 /**
325  * Update the object's LMV EA with the given @lmv.
326  *
327  * \param[in] env       pointer to the thread context
328  * \param[in] com       pointer to the lfsck component
329  * \param[in] obj       pointer to the object which LMV EA will be updated
330  * \param[in] lmv       pointer to buffer holding the new LMV EA
331  * \param[in] locked    whether the caller has held ldlm lock on the @obj or not
332  *
333  * \retval              positive number for nothing to be done
334  * \retval              zero if updated successfully
335  * \retval              negative error number on failure
336  */
337 int lfsck_namespace_update_lmv(const struct lu_env *env,
338                                struct lfsck_component *com,
339                                struct dt_object *obj,
340                                struct lmv_mds_md_v1 *lmv, bool locked)
341 {
342         struct lfsck_thread_info        *info   = lfsck_env_info(env);
343         struct lmv_mds_md_v1            *lmv4   = &info->lti_lmv4;
344         struct lu_buf                   *buf    = &info->lti_buf;
345         struct lfsck_instance           *lfsck  = com->lc_lfsck;
346         struct dt_device                *dev    = lfsck_obj2dt_dev(obj);
347         struct thandle                  *th     = NULL;
348         struct lustre_handle             lh     = { 0 };
349         int                              rc     = 0;
350         int                              rc1    = 0;
351         ENTRY;
352
353         LASSERT(lmv4 != lmv);
354
355         lfsck_lmv_header_cpu_to_le(lmv4, lmv);
356         lfsck_buf_init(buf, lmv4, sizeof(*lmv4));
357
358         if (!locked) {
359                 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
360                                       MDS_INODELOCK_UPDATE |
361                                       MDS_INODELOCK_XATTR, LCK_EX);
362                 if (rc != 0)
363                         GOTO(log, rc);
364         }
365
366         th = dt_trans_create(env, dev);
367         if (IS_ERR(th))
368                 GOTO(log, rc = PTR_ERR(th));
369
370         /* For remote updating LMV EA, there will be further LFSCK action on
371          * remote MDT after the updating, so update the LMV EA synchronously. */
372         if (dt_object_remote(obj))
373                 th->th_sync = 1;
374
375         rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
376         if (rc != 0)
377                 GOTO(stop, rc);
378
379         rc = dt_trans_start_local(env, dev, th);
380         if (rc != 0)
381                 GOTO(stop, rc);
382
383         dt_write_lock(env, obj, 0);
384         if (unlikely(lfsck_is_dead_obj(obj)))
385                 GOTO(unlock, rc = 1);
386
387         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
388                 GOTO(unlock, rc = 0);
389
390         rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th, BYPASS_CAPA);
391
392         GOTO(unlock, rc);
393
394 unlock:
395         dt_write_unlock(env, obj);
396
397 stop:
398         rc1 = dt_trans_stop(env, dev, th);
399         if (rc == 0)
400                 rc = rc1;
401
402 log:
403         lfsck_ibits_unlock(&lh, LCK_EX);
404         CDEBUG(D_LFSCK, "%s: namespace LFSCK updated the %s LMV EA "
405                "for the object "DFID": rc = %d\n",
406                lfsck_lfsck2name(lfsck),
407                lmv->lmv_magic == LMV_MAGIC ? "master" : "slave",
408                PFID(lfsck_dto2fid(obj)), rc);
409
410         return rc;
411 }
412
413 /**
414  * Set master LMV EA for the specified striped directory.
415  *
416  * \param[in] env       pointer to the thread context
417  * \param[in] com       pointer to the lfsck component
418  * \param[in] dir       pointer to the object on which the LMV EA will be set
419  * \param[in] lmv       pointer to the buffer holding the new LMV EA
420  * \param[in] cfid      the shard's FID used for verification
421  * \param[in] cidx      the shard's index used for verification
422  * \param[in] flags     to indicate which element(s) in the LMV EA will be set
423  *
424  * \retval              positive number if nothing to be done
425  * \retval              zero for succeed
426  * \retval              negative error number on failure
427  */
428 static int lfsck_namespace_set_lmv_master(const struct lu_env *env,
429                                           struct lfsck_component *com,
430                                           struct dt_object *dir,
431                                           struct lmv_mds_md_v1 *lmv,
432                                           const struct lu_fid *cfid,
433                                           __u32 cidx, __u32 flags)
434 {
435         struct lfsck_thread_info        *info   = lfsck_env_info(env);
436         struct lmv_mds_md_v1            *lmv3   = &info->lti_lmv3;
437         struct lfsck_instance           *lfsck  = com->lc_lfsck;
438         struct dt_object                *obj;
439         struct lustre_handle             lh     = { 0 };
440         int                              pidx   = -1;
441         int                              rc     = 0;
442         ENTRY;
443
444         /* Find the bottom object to bypass LOD when set LMV EA. */
445         obj = lu2dt(container_of0(dir->do_lu.lo_header->loh_layers.prev,
446                                   struct lu_object, lo_linkage));
447         if (unlikely(obj == NULL))
448                 RETURN(-ENOENT);
449
450         if (dt_object_remote(obj)) {
451                 struct lu_seq_range     *range  = &info->lti_range;
452                 struct seq_server_site  *ss     =
453                         lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
454
455                 fld_range_set_mdt(range);
456                 rc = fld_server_lookup(env, ss->ss_server_fld,
457                                        fid_seq(lfsck_dto2fid(obj)), range);
458                 if (rc != 0)
459                         GOTO(log, rc);
460
461                 pidx = range->lsr_index;
462         } else {
463                 pidx = lfsck_dev_idx(lfsck->li_bottom);
464         }
465
466         /* XXX: it will be improved with subsequent patches landed. */
467
468         rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
469                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
470                               LCK_EX);
471         if (rc != 0)
472                 GOTO(log, rc);
473
474         rc = lfsck_read_stripe_lmv(env, obj, lmv3);
475         if (rc != 0)
476                 GOTO(log, rc);
477
478         lmv3->lmv_hash_type = lmv->lmv_hash_type;
479         lmv3->lmv_magic = LMV_MAGIC;
480         lmv3->lmv_master_mdt_index = pidx;
481
482         rc = lfsck_namespace_update_lmv(env, com, obj, lmv3, true);
483
484         GOTO(log, rc);
485
486 log:
487         lfsck_ibits_unlock(&lh, LCK_EX);
488         CDEBUG(D_LFSCK, "%s: namespace LFSCK set master LMV EA for the object "
489                DFID" on the %s MDT %d, flags %x: rc = %d\n",
490                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
491                dt_object_remote(obj) ? "remote" : "local", pidx, flags, rc);
492
493         if (rc <= 0) {
494                 struct lfsck_namespace *ns = com->lc_file_ram;
495
496                 ns->ln_flags |= LF_INCONSISTENT;
497         }
498
499         return rc;
500 }
501
502 /**
503  * Repair the bad name hash.
504  *
505  * If the name hash of some name entry under the striped directory does not
506  * match the shard of the striped directory, then the LFSCK will repair the
507  * inconsistency. Ideally, the LFSCK should migrate the name entry from the
508  * current MDT to the right MDT (another one), but before the async commit
509  * finished, the LFSCK will change the striped directory's hash type as
510  * LMV_HASH_TYPE_UNKNOWN and mark the lmv flags as LMV_HASH_FLAG_BAD_TYPE.
511  *
512  * \param[in] env       pointer to the thread context
513  * \param[in] com       pointer to the lfsck component
514  * \param[in] shard     pointer to the shard of the striped directory that
515  *                      contains the bad name entry
516  * \param[in] llmv      pointer to lfsck LMV EA structure
517  * \param[in] name      the name of the bad name hash
518  *
519  * \retval              positive number if nothing to be done
520  * \retval              zero for succeed
521  * \retval              negative error number on failure
522  */
523 int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
524                                          struct lfsck_component *com,
525                                          struct dt_object *shard,
526                                          struct lfsck_lmv *llmv,
527                                          const char *name)
528 {
529         struct lfsck_thread_info        *info   = lfsck_env_info(env);
530         struct lu_fid                   *pfid   = &info->lti_fid3;
531         struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
532         struct lfsck_instance           *lfsck  = com->lc_lfsck;
533         struct dt_object                *parent = NULL;
534         int                              rc     = 0;
535         ENTRY;
536
537         rc = dt_lookup(env, shard, (struct dt_rec *)pfid,
538                        (const struct dt_key *)dotdot, BYPASS_CAPA);
539         if (rc != 0 || !fid_is_sane(pfid))
540                 GOTO(log, rc);
541
542         parent = lfsck_object_find_bottom(env, lfsck, pfid);
543         if (IS_ERR(parent))
544                 GOTO(log, rc = PTR_ERR(parent));
545
546         *lmv2 = llmv->ll_lmv;
547         lmv2->lmv_hash_type = LMV_HASH_TYPE_UNKNOWN | LMV_HASH_FLAG_BAD_TYPE;
548         rc = lfsck_namespace_set_lmv_master(env, com, parent, lmv2,
549                                             lfsck_dto2fid(shard),
550                                             llmv->ll_lmv.lmv_master_mdt_index,
551                                             LEF_SET_LMV_HASH);
552
553         GOTO(log, rc);
554
555 log:
556         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name hash "
557                "on the MDT %x, parent "DFID", name %s, shard_%x "DFID
558                ": rc = %d\n",
559                lfsck_lfsck2name(lfsck), lfsck_dev_idx(lfsck->li_bottom),
560                PFID(pfid), name, llmv->ll_lmv.lmv_master_mdt_index,
561                PFID(lfsck_dto2fid(shard)), rc);
562
563         if (parent != NULL && !IS_ERR(parent))
564                 lfsck_object_put(env, parent);
565
566         return rc;
567 }
568
569 /**
570  * Verify the slave object's (of striped directory) LMV EA.
571  *
572  * For the slave object of a striped directory, before traversing the shard
573  * the LFSCK will verify whether its slave LMV EA matches its parent's master
574  * LMV EA or not.
575  *
576  * \param[in] env       pointer to the thread context
577  * \param[in] com       pointer to the lfsck component
578  * \param[in] obj       pointer to the object which LMV EA will be checked
579  * \param[in] llmv      pointer to buffer holding the slave LMV EA
580  *
581  * \retval              zero for succeed
582  * \retval              negative error number on failure
583  */
584 int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
585                                         struct lfsck_component *com,
586                                         struct dt_object *obj,
587                                         struct lfsck_lmv *llmv)
588 {
589         struct lfsck_thread_info        *info   = lfsck_env_info(env);
590         char                            *name   = info->lti_key;
591         char                            *name2;
592         struct lu_fid                   *pfid   = &info->lti_fid3;
593         struct lu_fid                   *tfid   = &info->lti_fid4;
594         const struct lu_fid             *cfid   = lfsck_dto2fid(obj);
595         struct lfsck_instance           *lfsck  = com->lc_lfsck;
596         struct lmv_mds_md_v1            *clmv   = &llmv->ll_lmv;
597         struct lmv_mds_md_v1            *plmv   = &info->lti_lmv;
598         struct dt_object                *parent = NULL;
599         int                              rc     = 0;
600         ENTRY;
601
602         if (!lfsck_is_valid_slave_lmv(clmv)) {
603                 rc = lfsck_namespace_trace_update(env, com, cfid,
604                                         LNTF_UNCERTAIN_LMV, true);
605
606                 GOTO(out, rc);
607         }
608
609         rc = dt_lookup(env, obj, (struct dt_rec *)pfid,
610                        (const struct dt_key *)dotdot, BYPASS_CAPA);
611         if (rc != 0 || !fid_is_sane(pfid)) {
612                 rc = lfsck_namespace_trace_update(env, com, cfid,
613                                         LNTF_UNCERTAIN_LMV, true);
614
615                 GOTO(out, rc);
616         }
617
618         parent = lfsck_object_find(env, lfsck, pfid);
619         if (IS_ERR(parent)) {
620                 rc = lfsck_namespace_trace_update(env, com, cfid,
621                                         LNTF_UNCERTAIN_LMV, true);
622
623                 GOTO(out, rc);
624         }
625
626         rc = lfsck_read_stripe_lmv(env, parent, plmv);
627         if (rc != 0) {
628                 int rc1;
629
630                 /* If the parent has no LMV EA, then it maybe because:
631                  * 1) The parent lost the LMV EA.
632                  * 2) The child claims a wrong (slave) LMV EA. */
633
634                 /* XXX: to be improved. */
635                 rc = 0;
636
637                 rc1 = lfsck_namespace_trace_update(env, com, cfid,
638                                                    LNTF_UNCERTAIN_LMV, true);
639
640                 GOTO(out, rc = (rc < 0 ? rc : rc1));
641         }
642
643         /* Unmatched magic or stripe count. */
644         if (unlikely(plmv->lmv_magic != LMV_MAGIC ||
645                      plmv->lmv_stripe_count != clmv->lmv_stripe_count)) {
646                 rc = lfsck_namespace_trace_update(env, com, cfid,
647                                                   LNTF_UNCERTAIN_LMV, true);
648
649                 GOTO(out, rc);
650         }
651
652         /* If the master hash type has been set as LMV_HASH_TYPE_UNKNOWN,
653          * then the slave hash type is not important. */
654         if ((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) ==
655             LMV_HASH_TYPE_UNKNOWN &&
656             plmv->lmv_hash_type & LMV_HASH_FLAG_BAD_TYPE)
657                 GOTO(out, rc = 0);
658
659         /* Unmatched hash type. */
660         if (unlikely((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) !=
661                      (clmv->lmv_hash_type & LMV_HASH_TYPE_MASK))) {
662                 rc = lfsck_namespace_trace_update(env, com, cfid,
663                                                   LNTF_UNCERTAIN_LMV, true);
664
665                 GOTO(out, rc);
666         }
667
668         snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
669                  PFID(cfid), clmv->lmv_master_mdt_index);
670         name2 = info->lti_tmpbuf2;
671
672         rc = lfsck_links_get_first(env, obj, name, tfid);
673         if (rc == 0 && strcmp(name, name2) == 0 && lu_fid_eq(pfid, tfid)) {
674                 llmv->ll_lmv_verified = 1;
675
676                 GOTO(out, rc);
677         }
678
679         rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
680                        (const struct dt_key *)name2, BYPASS_CAPA);
681         if (rc != 0 || !lu_fid_eq(cfid, tfid))
682                 rc = lfsck_namespace_trace_update(env, com, cfid,
683                                                   LNTF_UNCERTAIN_LMV, true);
684         else
685                 llmv->ll_lmv_verified = 1;
686
687         GOTO(out, rc);
688
689 out:
690         if (parent != NULL && !IS_ERR(parent))
691                 lfsck_object_put(env, parent);
692
693         return rc;
694 }
695
696 /**
697  * Double scan the striped directory or the shard.
698  *
699  * \param[in] env       pointer to the thread context
700  * \param[in] com       pointer to the lfsck component
701  * \param[in] lnr       pointer to the namespace request that contains the
702  *                      striped directory or the shard
703  *
704  * \retval              zero for succeed
705  * \retval              negative error number on failure
706  */
707 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
708                                        struct lfsck_component *com,
709                                        struct lfsck_namespace_req *lnr)
710 {
711         struct lfsck_namespace          *ns     = com->lc_file_ram;
712         struct lfsck_lmv                *llmv   = lnr->lnr_lmv;
713         struct dt_object                *dir    = lnr->lnr_obj;
714         ENTRY;
715
716         /* XXX: it will be improved with subsequent patches landed. */
717
718         if (llmv->ll_lmv_slave && llmv->ll_lmv_verified) {
719                 ns->ln_striped_shards_scanned++;
720                 lfsck_namespace_trace_update(env, com,
721                                         lfsck_dto2fid(dir),
722                                         LNTF_UNCERTAIN_LMV |
723                                         LNTF_RECHECK_NAME_HASH, false);
724         }
725
726         RETURN(0);
727 }