4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2014, 2017, Intel Corporation.
26 * lustre/lfsck/lfsck_striped_dir.c
28 * Author: Fan, Yong <fan.yong@intel.com>
32 * About the verification for striped directory. Some rules and assumptions:
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.
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.
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.
53 * 3.1) If the master hash type is one of the valid values, then trust the
54 * master LMV EA. Because:
56 * 3.1.1) The master hash type is visible to the client and used by the client.
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.
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.
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).
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.
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.
99 * 5) If the master LMV EA is lost, then there are two possible cases:
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.
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.
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.
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.
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).
141 #define DEBUG_SUBSYSTEM S_LFSCK
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>
151 #include "lfsck_internal.h"
153 void lfsck_lmv_put(const struct lu_env *env, struct lfsck_lmv *llmv)
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;
160 llu = list_entry(llmv, struct lfsck_lmv_unit, llu_lmv);
161 lfsck = llu->llu_lfsck;
163 spin_lock(&lfsck->li_lock);
164 list_del(&llu->llu_link);
165 spin_unlock(&lfsck->li_lock);
167 lfsck_object_put(env, llu->llu_obj);
169 LASSERT(llmv->ll_lslr != NULL);
171 OBD_FREE_PTR_ARRAY_LARGE(llmv->ll_lslr,
172 llmv->ll_stripes_allocated);
175 if (llmv->ll_lslr != NULL)
176 OBD_FREE_PTR_ARRAY_LARGE(
178 llmv->ll_stripes_allocated);
186 * Mark the specified directory as read-only by set LUSTRE_IMMUTABLE_FL.
188 * The caller has taken the ldlm lock on the @obj already.
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
195 * \retval positive number if nothing to be done
196 * \retval zero for success
197 * \retval negative error number on failure
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)
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;
211 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
214 th = lfsck_trans_create(env, dev, lfsck);
216 GOTO(log, rc = PTR_ERR(th));
219 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LMV, th);
224 la->la_valid = LA_FLAGS;
225 rc = dt_declare_attr_set(env, obj, la, th);
229 rc = dt_trans_start_local(env, dev, th);
233 dt_write_lock(env, obj, 0);
234 if (unlikely(lfsck_is_dead_obj(obj)))
235 GOTO(unlock, rc = 1);
238 rc = dt_xattr_del(env, obj, XATTR_NAME_LMV, th);
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);
253 dt_write_unlock(env, obj);
256 dt_trans_stop(env, dev, th);
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);
264 struct lfsck_namespace *ns = com->lc_file_ram;
266 ns->ln_flags |= LF_INCONSISTENT;
268 ns->ln_striped_dirs_disabled++;
274 static inline bool lfsck_is_valid_slave_lmv(struct lmv_mds_md_v1 *lmv)
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);
283 * Remove the striped directory's master LMV EA and mark it as read-only.
285 * Take ldlm lock on the striped directory before calling the
286 * lfsck_disable_master_lmv().
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
294 * \retval positive number if nothing to be done
295 * \retval zero for success
296 * \retval negative error number on failure
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)
303 struct lustre_handle lh = { 0 };
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,
311 rc = lfsck_disable_master_lmv(env, com, obj, true);
312 lfsck_ibits_unlock(&lh, LCK_EX);
319 * Remove the name entry from the striped directory's master MDT-object.
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
327 * \retval positive number for repaired successfully
328 * \retval 0 if nothing to be repaired
329 * \retval negative error number on failure
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)
336 struct lfsck_thread_info *info = lfsck_env_info(env);
337 struct dt_object *obj;
340 snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
342 obj = lfsck_object_find_bottom(env, com->lc_lfsck, fid);
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);
351 struct lfsck_namespace *ns = com->lc_file_ram;
353 ns->ln_dirent_repaired++;
360 * Remove old shard's name entry and refill the @lslr slot with new shard.
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.
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
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
379 * \retval zero for success
380 * \retval negative error number on failure
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)
390 struct lfsck_lmv *llmv = lnr->lnr_lmv;
393 rc = lfsck_remove_dirent(env, com, dir,
394 &lslr->lslr_fid, index);
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;
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;
417 * Record the slave LMV EA in the lfsck_lmv::ll_lslr.
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).
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).
428 * 1) If one of the two conflict shards can be recorded in another slot, then
429 * it is OK, go ahead. Otherwise,
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,
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
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
445 * 3.2) If no, then remove (one of) the object what has no slave LMV EA.
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.
452 * This function may be called recursively, to prevent overflow, we define
453 * LFSCK_REC_LMV_MAX_DEPTH to restrict the recursive call depth.
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)
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
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)
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;
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);
504 if (index < 0 || index >= LFSCK_LMV_MAX_STRIPES)
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;
512 OBD_ALLOC_PTR_ARRAY_LARGE(new_lslr, new_stripes);
513 if (new_lslr == NULL) {
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;
525 lslr = llmv->ll_lslr + index;
526 if (unlikely(lu_fid_eq(&lslr->lslr_fid, fid)))
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;
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;
547 if (llmv->ll_max_filled_off < index)
548 llmv->ll_max_filled_off = index;
554 if (flags != LSLF_BAD_INDEX2)
555 LASSERTF(*depth == 1, "depth = %d\n", *depth);
557 /* Handle conflict cases. */
558 switch (lslr->lslr_flags) {
560 case LSLF_BAD_INDEX2:
561 /* The existing one is a normal valid object. */
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);
573 case LSLF_BAD_INDEX2:
574 GOTO(out, rc = -EEXIST);
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
590 rc = lfsck_remove_lmv(env, com, dir, lnr);
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);
598 rc = lfsck_remove_dirent(env, com, dir,
604 /* Remove the current dangling name entry. */
605 rc = lfsck_remove_dirent(env, com, dir, fid, index);
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);
631 /* The existing one has no slave LMV EA. */
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
648 rc = lfsck_remove_lmv(env, com, dir, lnr);
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);
660 rc = lfsck_replace_lmv(env, com, dir,
661 lslr, lnr, lmv, index, flags);
665 case LSLF_BAD_INDEX2:
666 if (flags2 >= lslr->lslr_flags)
667 GOTO(out, rc = -EEXIST);
673 /* Remove the current dangling name entry. */
674 rc = lfsck_remove_dirent(env, com, dir, fid, index);
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) {
697 /* The existing one is a dangling name entry. */
700 case LSLF_BAD_INDEX2:
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,
708 /* Two dangling name entries conflict,
709 * remove the current one. */
710 rc = lfsck_remove_dirent(env, com, dir, fid, index);
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);
735 case LSLF_BAD_INDEX1: {
736 if (*depth >= LFSCK_REC_LMV_MAX_DEPTH)
739 lrls = &lfsck->li_rec_lmv_save[*depth - 1];
740 lrls->lrls_fid = lnr->lnr_fid;
741 lrls->lrls_lmv = *lmv;
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;
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;
757 if (rc == -ERANGE || rc == -EEXIST)
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;
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;
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);
793 case LSLF_BAD_INDEX2:
794 GOTO(out, rc = -EEXIST);
798 /* Remove the current dangling name entry. */
799 rc = lfsck_remove_dirent(env, com, dir, fid, index);
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);
835 return rc > 0 ? 0 : rc;
839 * Read LMV from bottom object, so it doesn't contain stripe FIDs.
841 * TODO: test migrating/foreign directory lfsck
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
848 * \retval 0 on success
849 * \retval -ENODATA on no LMV, corrupt LMV, dir is dead or foreign
850 * -ev on other failures
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)
857 struct lfsck_thread_info *info = lfsck_env_info(env);
858 struct lu_buf *buf = &info->lti_buf;
859 struct lmv_foreign_md *lfm;
862 /* use bottom object to avoid reading in shard FIDs */
863 obj = lfsck_object_find_bottom(env, lfsck, lu_object_fid(&obj->do_lu));
867 dt_read_lock(env, obj, 0);
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)) {
878 lu_buf_check_and_alloc(buf, rc);
879 rc1 = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
886 dt_read_unlock(env, obj);
888 lfsck_object_put(env, obj);
890 if (rc > offsetof(typeof(*lfm), lfm_value) &&
891 *((__u32 *)buf->lb_buf) == LMV_MAGIC_FOREIGN) {
895 value_len = le32_to_cpu(lfm->lfm_length);
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)));
902 if (rc != value_len + offsetof(typeof(*lfm), lfm_value))
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)));
907 /* no further usage/decode of foreign LMV outside */
911 if (rc == sizeof(*lmv)) {
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)
918 } else if (rc >= 0) {
927 * Parse the shard's index from the given shard name.
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.
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
940 * \retval zero or positive number for the index from the name
941 * \retval negative error number on failure
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)
946 char *name2 = lfsck_env_info(env)->lti_tmpbuf2;
953 LASSERT(name != name2);
955 len = snprintf(name2, sizeof(lfsck_env_info(env)->lti_tmpbuf2),
957 if (namelen < len + 1 || memcmp(name, name2, len) != 0)
961 if (!isdigit(name[len]))
964 idx = idx * 10 + name[len++] - '0';
965 } while (len < namelen);
967 if (idx >= LFSCK_LMV_MAX_STRIPES)
973 static inline bool lfsck_name_hash_match(struct lmv_mds_md_v1 *lmv,
974 const char *name, int namelen)
978 idx = lmv_name_to_stripe_index_old(lmv, name, namelen);
979 if (idx == lmv->lmv_master_mdt_index)
982 if (!lmv_hash_is_layout_changing(lmv->lmv_hash_type))
985 idx = lmv_name_to_stripe_index(lmv, name, namelen);
986 return (idx == lmv->lmv_master_mdt_index);
989 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
990 struct lfsck_lmv *llmv,
991 const char *name, int namelen)
993 if (llmv == NULL || !llmv->ll_lmv_slave || !llmv->ll_lmv_verified)
996 return lfsck_name_hash_match(&llmv->ll_lmv, name, namelen);
1000 * Check whether the given name is a valid entry under the @parent.
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.
1005 * If the @parent is a shard of a striped directory, then the name hash
1006 * should match the MDT, otherwise it is invalid.
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
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
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)
1023 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
1026 rc = lfsck_read_stripe_lmv(env, lfsck, parent, lmv);
1028 RETURN(rc == -ENODATA ? 0 : rc);
1030 if (lmv->lmv_magic == LMV_MAGIC_STRIPE) {
1031 if (!lfsck_is_valid_slave_lmv(lmv))
1034 if (!lfsck_name_hash_match(lmv, cname->ln_name,
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) {
1047 * Update the object's LMV EA with the given @lmv.
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
1055 * \retval positive number for nothing to be done
1056 * \retval zero if updated successfully
1057 * \retval negative error number on failure
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)
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 };
1075 LASSERT(lmv4 != lmv);
1077 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1080 lfsck_lmv_header_cpu_to_le(lmv4, lmv);
1081 lfsck_buf_init(buf, lmv4, sizeof(*lmv4));
1084 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1085 MDS_INODELOCK_UPDATE |
1086 MDS_INODELOCK_XATTR, LCK_EX);
1091 th = lfsck_trans_create(env, dev, lfsck);
1093 GOTO(log, rc = PTR_ERR(th));
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))
1100 rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
1104 rc = dt_trans_start_local(env, dev, th);
1108 dt_write_lock(env, obj, 0);
1109 if (unlikely(lfsck_is_dead_obj(obj)))
1110 GOTO(unlock, rc = 1);
1112 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
1117 dt_write_unlock(env, obj);
1120 rc1 = dt_trans_stop(env, dev, th);
1125 lfsck_ibits_unlock(&lh, LCK_EX);
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);
1138 * Check whether allow to re-genereate the lost master LMV EA.
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
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
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
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,
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;
1176 if (unlikely(!dt_try_as_dir(env, obj, true)))
1179 /* Check whether the shard and the master MDT-object matches or not. */
1180 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf), DFID":%u",
1182 rc = dt_lookup(env, obj, (struct dt_rec *)tfid,
1183 (const struct dt_key *)info->lti_tmpbuf);
1187 if (!lu_fid_eq(tfid, cfid))
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);
1194 RETURN(PTR_ERR(di));
1196 rc = iops->load(env, di, 0);
1198 rc = iops->next(env, di);
1206 rc = iops->rec(env, di, (struct dt_rec *)ent, args);
1208 rc = lfsck_unpack_ent(ent, &cookie, &type);
1213 /* skip dot and dotdot entries */
1214 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
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.
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)
1239 rc = iops->next(env, di);
1246 iops->fini(env, di);
1252 * Notify remote LFSCK instance that the object's LMV EA has been updated.
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
1261 * \retval positive number if nothing to be done
1262 * \retval zero for success
1263 * \retval negative error number on failure
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,
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;
1279 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, index);
1281 GOTO(out, rc = -ENODEV);
1283 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1286 GOTO(out, rc = -ENOMEM);
1288 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1290 ptlrpc_request_free(req);
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;
1301 lr->lr_flags = flags;
1303 ptlrpc_request_set_replen(req);
1304 rc = ptlrpc_queue_wait(req);
1305 ptlrpc_req_finished(req);
1307 GOTO(out, rc = (rc == -ENOENT ? 1 : rc));
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,
1322 * Generate request for local LFSCK instance to rescan the striped directory.
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
1328 * \retval positive number if nothing to be done
1329 * \retval zero for success
1330 * \retval negative error number on failure
1332 int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
1333 struct lfsck_component *com,
1334 struct dt_object *obj)
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;
1346 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1349 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv4);
1354 if (unlikely(llu == NULL))
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;
1362 count = lmv4->lmv_stripe_count;
1364 OBD_ALLOC_PTR_ARRAY_LARGE(lslr, count);
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;
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);
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);
1402 * Set master LMV EA for the specified striped directory.
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.
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.
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
1424 * \retval positive number if nothing to be done
1425 * \retval zero for success
1426 * \retval negative error number on failure
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)
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 };
1445 fld_range_set_mdt(range);
1446 rc = fld_server_lookup(env, ss->ss_server_fld,
1447 fid_seq(lfsck_dto2fid(obj)), range);
1451 pidx = range->lsr_index;
1452 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1453 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1458 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv3);
1459 if (rc == -ENODATA) {
1460 if (!(flags & LEF_SET_LMV_ALL))
1464 } else if (rc == 0) {
1465 if (flags & LEF_SET_LMV_ALL)
1468 if (flags & LEF_SET_LMV_HASH)
1469 lmv3->lmv_hash_type = lmv->lmv_hash_type;
1474 lmv3->lmv_magic = LMV_MAGIC;
1475 lmv3->lmv_master_mdt_index = pidx;
1476 lmv3->lmv_layout_version++;
1478 if (flags & LEF_SET_LMV_ALL) {
1479 rc = lfsck_allow_regenerate_master_lmv(env, com, obj,
1482 rc = lfsck_disable_master_lmv(env, com, obj, false);
1484 GOTO(log, rc = (rc == 0 ? 1 : rc));
1490 /* To indicate that the master has ever lost LMV EA. */
1491 lmv3->lmv_hash_type |= LMV_HASH_FLAG_LOST_LMV;
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);
1500 rc = lfsck_namespace_notify_lmv_master_local(env, com,
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);
1514 struct lfsck_namespace *ns = com->lc_file_ram;
1516 ns->ln_flags |= LF_INCONSISTENT;
1523 * Repair the bad name hash.
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.
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
1539 * \retval positive number if nothing to be done
1540 * \retval zero for success
1541 * \retval negative error number on failure
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,
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;
1557 rc = dt_lookup_dir(env, shard, dotdot, pfid);
1558 if (rc != 0 || !fid_is_sane(pfid))
1561 parent = lfsck_object_find_bottom(env, lfsck, pfid);
1563 GOTO(log, rc = PTR_ERR(parent));
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. */
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.
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,
1585 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name hash "
1586 "on the MDT %x, parent "DFID", name %s, shard_%x "DFID
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);
1592 if (parent != NULL && !IS_ERR(parent))
1593 lfsck_object_put(env, parent);
1599 * Scan the shard of a striped directory for name hash verification.
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.
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
1614 * \retval positive number for scanning successfully
1615 * \retval zero for the scanning is paused
1616 * \retval negative error number on failure
1618 int lfsck_namespace_scan_shard(const struct lu_env *env,
1619 struct lfsck_component *com,
1620 struct dt_object *child)
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;
1639 rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
1641 RETURN(rc == -ENODATA ? 1 : rc);
1643 if (lmv->lmv_magic != LMV_MAGIC_STRIPE)
1646 if (unlikely(!dt_try_as_dir(env, child, true)))
1649 OBD_ALLOC_PTR(llmv);
1653 llmv->ll_lmv_slave = 1;
1654 llmv->ll_lmv_verified = 1;
1655 llmv->ll_lmv = *lmv;
1656 atomic_set(&llmv->ll_ref, 1);
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);
1662 GOTO(out, rc = PTR_ERR(di));
1664 rc = iops->load(env, di, 0);
1666 rc = iops->next(env, di);
1671 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
1672 unlikely(!thread_is_running(thread)))
1675 rc = iops->rec(env, di, (struct dt_rec *)ent, args);
1677 rc = lfsck_unpack_ent(ent, &cookie, &type);
1680 if (bk->lb_param & LPF_FAILOUT)
1686 /* skip dot and dotdot entries */
1687 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
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);
1696 ns->ln_name_hash_repaired++;
1699 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1703 lfsck_control_speed(lfsck);
1704 if (unlikely(!thread_is_running(thread)))
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);
1712 GOTO(out, rc = -EINVAL);
1716 rc = iops->next(env, di);
1723 iops->fini(env, di);
1724 lfsck_lmv_put(env, llmv);
1730 * Verify the slave object's (of striped directory) LMV EA.
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
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
1741 * \retval positive number if nothing to be done
1742 * \retval zero for success
1743 * \retval negative error number on failure
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)
1750 struct lfsck_thread_info *info = lfsck_env_info(env);
1751 char *name = info->lti_key;
1753 struct lu_fid *pfid = &info->lti_fid3;
1754 const struct lu_fid *cfid = lfsck_dto2fid(obj);
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;
1763 if (!lfsck_is_valid_slave_lmv(clmv)) {
1764 rc = lfsck_namespace_trace_update(env, com, cfid,
1765 LNTF_UNCERTAIN_LMV, true);
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);
1778 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
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);
1788 if (unlikely(!dt_object_exists(parent)))
1791 if (unlikely(!dt_try_as_dir(env, parent, true)))
1792 GOTO(out, rc = -ENOTDIR);
1794 rc = lfsck_read_stripe_lmv(env, lfsck, parent, plmv);
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. */
1802 rc = lfsck_namespace_set_lmv_master(env, com, parent,
1803 clmv, cfid, clmv->lmv_master_mdt_index,
1808 rc1 = lfsck_namespace_trace_update(env, com, cfid,
1809 LNTF_UNCERTAIN_LMV, true);
1811 GOTO(out, rc = (rc < 0 ? rc : rc1));
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);
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)
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);
1839 snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
1840 PFID(cfid), clmv->lmv_master_mdt_index);
1841 name2 = info->lti_tmpbuf2;
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;
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);
1855 llmv->ll_lmv_verified = 1;
1860 if (parent != NULL && !IS_ERR(parent))
1861 lfsck_object_put(env, parent);
1867 * Double scan the striped directory or the shard.
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
1873 * If the target is one shard of a striped directory, then only needs to
1874 * update related tracing file.
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
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
1887 * \retval zero for success
1888 * \retval negative error number on failure
1890 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
1891 struct lfsck_component *com,
1892 struct lfsck_namespace_req *lnr)
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);
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);
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)
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++;
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);
1943 stripe_count = max(llmv->ll_max_filled_off + 1,
1944 lmv->lmv_stripe_count);
1946 if (lmv->lmv_stripe_count != stripe_count) {
1947 lmv->lmv_stripe_count = stripe_count;
1948 llmv->ll_lmv_updated = 1;
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;
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;
1963 if (llmv->ll_lmv_updated) {
1965 dir = lfsck_assistant_object_load(env, lfsck, lso);
1969 RETURN(rc == -ENOENT ? 0 : rc);
1973 lmv->lmv_layout_version++;
1974 rc = lfsck_namespace_update_lmv(env, com, dir, lmv, false);
1978 ns->ln_striped_dirs_scanned++;
1979 if (!llmv->ll_counted)
1980 ns->ln_striped_dirs_repaired++;
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 };
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;
2000 if (fid_is_zero(cfid))
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,
2009 obj = lfsck_object_find_bottom(env, lfsck, cfid);
2012 dir = lfsck_assistant_object_load(env, lfsck,
2015 if (PTR_ERR(dir) == -ENOENT)
2020 } else if (lfsck_is_dead_obj(dir)) {
2028 switch (lslr->lslr_flags) {
2030 if (llmv->ll_inline ||
2031 lslr->lslr_stripe_count != stripe_count ||
2032 (lslr->lslr_hash_type & LMV_HASH_TYPE_MASK) !=
2034 repair_lmvea = true;
2036 case LSLF_BAD_INDEX2:
2037 /* The index in the slave LMV EA is right,
2038 * the name entry should be updated. */
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) !=
2046 repair_lmvea = true;
2048 case LSLF_BAD_INDEX1:
2049 /* The index in the name entry is right,
2050 * the slave LMV EA should be updated. */
2052 repair_lmvea = true;
2061 rc1 = lfsck_links_read_with_rec(env, obj, &ldata);
2062 if (rc1 == -ENOENT) {
2067 if (rc1 == -EINVAL || rc1 == -ENODATA) {
2068 repair_linkea = true;
2075 if (ldata.ld_leh->leh_reccount != 1) {
2076 repair_linkea = true;
2080 rc1 = linkea_links_find(&ldata, cname, pfid);
2082 repair_linkea = true;
2087 dir = lfsck_assistant_object_load(env, lfsck,
2100 rc1 = lfsck_namespace_repair_dangling(env, com, dir,
2103 create_repaired = true;
2105 ns->ln_dangling_repaired++;
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;
2116 rc1 = lfsck_namespace_update_lmv(env, com, obj,
2121 if (dt_object_remote(obj)) {
2122 rc1 = fld_server_lookup(env, ss->ss_server_fld,
2123 fid_seq(lfsck_dto2fid(obj)), range);
2127 rc1 = lfsck_namespace_notify_lmv_remote(env,
2128 com, obj, LE_SET_LMV_SLAVE, 0,
2131 ns->ln_striped_shards_repaired++;
2132 rc1 = lfsck_namespace_trace_update(env, com,
2133 cfid, LNTF_RECHECK_NAME_HASH, true);
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);
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,
2156 rc1 = lfsck_namespace_trace_update(env, com,
2157 cfid, LNTF_RECHECK_NAME_HASH, true);
2166 dir = lfsck_assistant_object_load(env, lfsck,
2179 rc1 = lfsck_namespace_repair_dirent(env, com, dir, obj,
2180 info->lti_tmpbuf2, lnr->lnr_name,
2181 lnr->lnr_type, true, false);
2183 rename_repaired = true;
2185 ns->ln_dirent_repaired++;
2186 rc1 = lfsck_namespace_trace_update(env,
2188 LNTF_RECHECK_NAME_HASH, true);
2196 if (repair_linkea) {
2197 struct lustre_handle lh = { 0 };
2200 dir = lfsck_assistant_object_load(env, lfsck,
2213 rc1 = linkea_links_new(&ldata, &info->lti_big_buf,
2214 cname, lfsck_dto2fid(dir));
2218 rc1 = lfsck_ibits_lock(env, lfsck, obj, &lh,
2219 MDS_INODELOCK_UPDATE |
2220 MDS_INODELOCK_XATTR, LCK_EX);
2224 rc1 = lfsck_namespace_rebuild_linkea(env, com, obj,
2226 lfsck_ibits_unlock(&lh, LCK_EX);
2228 linkea_repaired = true;
2230 ns->ln_linkea_repaired++;
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);
2252 if (obj != NULL && !IS_ERR(obj)) {
2253 lfsck_object_put(env, obj);
2259 ns->ln_striped_shards_failed++;
2266 if (obj != NULL && !IS_ERR(obj))
2267 lfsck_object_put(env, obj);
2269 if (dir != NULL && !IS_ERR(dir))
2270 lfsck_object_put(env, dir);
2276 * Verify the shard's name entry under the striped directory.
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.
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:
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.
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.
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.
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.
2321 * \retval zero for success
2322 * \retval negative error number on failure
2324 int lfsck_namespace_handle_striped_master(const struct lu_env *env,
2325 struct lfsck_component *com,
2326 struct lfsck_namespace_req *lnr)
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;
2342 bool repaired = false;
2343 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
2346 if (unlikely(llmv->ll_ignore))
2349 dir = lfsck_assistant_object_load(env, lfsck, lso);
2353 RETURN(rc == -ENOENT ? 0 : rc);
2356 shard_idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
2358 GOTO(fail_lmv, rc = shard_idx);
2360 if (shard_idx == lfsck_dev_idx(lfsck)) {
2361 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
2364 dev = lfsck->li_bottom;
2366 struct lfsck_tgt_desc *ltd;
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);
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);
2385 GOTO(fail_lmv, rc = -ENODEV);
2391 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
2393 if (lfsck_is_dead_obj(dir))
2396 GOTO(fail_lmv, rc = PTR_ERR(obj));
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);
2403 type = LNIT_BAD_DIRENT;
2409 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
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);
2420 stripe = lfsck_shard_name_to_index(env, lnr->lnr_name, lnr->lnr_namelen,
2421 lfsck_object_type(obj),
2424 type = LNIT_BAD_DIRENT;
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. */
2436 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2437 LSLF_NO_LMVEA, LSLF_NONE, &depth);
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,
2447 llmv->ll_failed = 1;
2450 if (rc >= 0 && type == LNIT_NONE && !S_ISDIR(lnr->lnr_type))
2451 type = 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);
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);
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);
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);
2484 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT))
2488 ns->ln_items_repaired++;
2492 ns->ln_bad_type_repaired++;
2494 case LNIT_BAD_DIRENT:
2495 ns->ln_dirent_repaired++;
2505 if (obj != NULL && !IS_ERR(obj))
2506 lfsck_object_put(env, obj);
2508 lfsck_object_put(env, dir);