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);
260 "%s: namespace LFSCK set the master MDT-object of 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;
499 "%s: record slave LMV EA for the striped directory "DFID": shard = "DFID", index = %u, flags = %u, flags2 = %u, depth = %d\n",
500 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(dir)), PFID(fid),
501 index, flags, flags2, *depth);
503 if (index < 0 || index >= LFSCK_LMV_MAX_STRIPES)
506 if (index >= llmv->ll_stripes_allocated) {
507 struct lfsck_slave_lmv_rec *new_lslr;
508 int new_stripes = index + 1;
509 size_t old_size = sizeof(*lslr) * llmv->ll_stripes_allocated;
511 OBD_ALLOC_PTR_ARRAY_LARGE(new_lslr, new_stripes);
512 if (new_lslr == NULL) {
518 memcpy(new_lslr, llmv->ll_lslr, old_size);
519 OBD_FREE_LARGE(llmv->ll_lslr, old_size);
520 llmv->ll_stripes_allocated = new_stripes;
521 llmv->ll_lslr = new_lslr;
524 lslr = llmv->ll_lslr + index;
525 if (unlikely(lu_fid_eq(&lslr->lslr_fid, fid)))
528 if (fid_is_zero(&lslr->lslr_fid)) {
529 lslr->lslr_fid = *fid;
530 lslr->lslr_stripe_count = lmv->lmv_stripe_count;
531 lslr->lslr_index = lmv->lmv_master_mdt_index;
532 lslr->lslr_hash_type = lmv->lmv_hash_type;
533 lslr->lslr_flags = flags;
534 llmv->ll_stripes_filled++;
535 if (flags == LSLF_NONE) {
536 if (llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
537 lmv_is_known_hash_type(lmv->lmv_hash_type))
538 llmv->ll_hash_type = lmv->lmv_hash_type;
540 if (lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
541 llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
542 llmv->ll_max_stripe_count =
543 lslr->lslr_stripe_count;
546 if (llmv->ll_max_filled_off < index)
547 llmv->ll_max_filled_off = index;
553 if (flags != LSLF_BAD_INDEX2)
554 LASSERTF(*depth == 1, "depth = %d\n", *depth);
556 /* Handle conflict cases. */
557 switch (lslr->lslr_flags) {
559 case LSLF_BAD_INDEX2:
560 /* The existing one is a normal valid object. */
563 /* The two 'valid' name entries claims the same index,
564 * the LFSCK cannot distinguish which one is correct.
565 * Then remove the master LMV EA to make all shards to
566 * be visible to client, and mark the master MDT-object
567 * as read-only. The administrator can handle the
568 * conflict with more human knowledge.
570 rc = lfsck_remove_lmv(env, com, dir, lnr);
572 case LSLF_BAD_INDEX2:
573 GOTO(out, rc = -EEXIST);
577 if (llmv->ll_lmv.lmv_hash_type &
578 LMV_HASH_FLAG_LOST_LMV) {
579 /* If the master LMV EA was re-generated by the
580 * former LFSCK reparation, and before such
581 * reparation, someone has created the conflict
582 * object, but the LFSCK did not detect such
583 * conflict, then we have to remove the master
584 * LMV EA and mark the master MDT-object as
585 * read-only. The administrator can handle the
586 * conflict with more human knowledge.
588 rc = lfsck_remove_lmv(env, com, dir, lnr);
590 /* Otherwise, remove the current name entry,
591 * and add its FID in the LFSCK tracing file
592 * for further processing.
594 rc = lfsck_namespace_trace_update(env, com, fid,
595 LNTF_CHECK_PARENT, true);
597 rc = lfsck_remove_dirent(env, com, dir,
603 /* Remove the current dangling name entry. */
604 rc = lfsck_remove_dirent(env, com, dir, fid, index);
606 case LSLF_BAD_INDEX1:
607 index = lmv->lmv_master_mdt_index;
608 lmv->lmv_master_mdt_index = shard_idx;
609 /* The name entry claims an index that is conflict
610 * with a valid existing name entry, then try the
611 * 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 conflict
618 * with other. Then we do not know how to solve
619 * the conflict. We will handle it as handle the
620 * case 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 by the
639 * former LFSCK reparation, and before such
640 * reparation, someone has created the conflict
641 * object, but the LFSCK did not detect such
642 * conflict, then we have to remove the master
643 * LMV EA and mark the master MDT-object as
644 * read-only. The administrator can handle the
645 * conflict with more human knowledge.
647 rc = lfsck_remove_lmv(env, com, dir, lnr);
649 lrls = &lfsck->li_rec_lmv_save[*depth - 1];
650 lrls->lrls_fid = lslr->lslr_fid;
651 /* Otherwise, remove the existing name entry,
652 * and add its FID in the LFSCK tracing file for
653 * further processing. Refill the slot with
654 * 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.
683 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
684 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
685 lmv->lmv_master_mdt_index = index;
686 if (rc == -ERANGE || rc == -EEXIST) {
698 /* The existing one is a dangling name entry. */
701 case LSLF_BAD_INDEX2:
703 /* Remove the existing dangling name entry.
704 * Refill the lslr slot with the given LMV.
706 rc = lfsck_replace_lmv(env, com, dir, lslr, lnr,
710 /* Two dangling name entries conflict,
711 * remove the current one.
713 rc = lfsck_remove_dirent(env, com, dir, fid, index);
715 case LSLF_BAD_INDEX1:
716 index = lmv->lmv_master_mdt_index;
717 lmv->lmv_master_mdt_index = shard_idx;
718 /* The name entry claims an index that is conflict
719 * with a valid existing name entry, then try the
720 * index in the lmv recursively.
722 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
723 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
724 lmv->lmv_master_mdt_index = index;
725 if (rc == -ERANGE || rc == -EEXIST)
726 /* If the index in the lmv is invalid or
727 * also conflict with other, then remove
728 * the existing dangling name entry.
729 * Refill the lslr slot with the given LMV.
731 rc = lfsck_replace_lmv(env, com, dir, lslr, lnr,
732 lmv, shard_idx, flags);
740 case LSLF_BAD_INDEX1: {
741 if (*depth >= LFSCK_REC_LMV_MAX_DEPTH)
744 lrls = &lfsck->li_rec_lmv_save[*depth - 1];
745 lrls->lrls_fid = lnr->lnr_fid;
746 lrls->lrls_lmv = *lmv;
748 lnr->lnr_fid = lslr->lslr_fid;
749 lmv->lmv_master_mdt_index = index;
750 lmv->lmv_stripe_count = lslr->lslr_stripe_count;
751 lmv->lmv_hash_type = lslr->lslr_hash_type;
752 index = lslr->lslr_index;
754 /* Existing one has another possible slot, try recursively. */
755 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
756 LSLF_BAD_INDEX2, flags, depth);
757 *lmv = lrls->lrls_lmv;
758 lnr->lnr_fid = lrls->lrls_fid;
761 if (rc == -ERANGE || rc == -EEXIST)
767 lslr->lslr_fid = *fid;
768 lslr->lslr_flags = flags;
769 lslr->lslr_stripe_count = lmv->lmv_stripe_count;
770 lslr->lslr_index = lmv->lmv_master_mdt_index;
771 lslr->lslr_hash_type = lmv->lmv_hash_type;
772 if (flags == LSLF_NONE) {
773 if (llmv->ll_hash_type == LMV_HASH_TYPE_UNKNOWN &&
774 lmv_is_known_hash_type(lmv->lmv_hash_type))
775 llmv->ll_hash_type = lmv->lmv_hash_type;
777 if (lslr->lslr_stripe_count <= LFSCK_LMV_MAX_STRIPES &&
778 llmv->ll_max_stripe_count < lslr->lslr_stripe_count)
779 llmv->ll_max_stripe_count =
780 lslr->lslr_stripe_count;
788 /* The two 'valid' name entries claims the same
789 * index, the LFSCK cannot distinguish which one
790 * is correct. Then remove the master LMV EA to
791 * make all shards to be visible to client, and
792 * mark the master MDT-object as read-only. The
793 * administrator can handle the conflict with
794 * more human knowledge.
796 rc = lfsck_remove_lmv(env, com, dir, lnr);
798 case LSLF_BAD_INDEX2:
799 GOTO(out, rc = -EEXIST);
803 /* Remove the current dangling name entry. */
804 rc = lfsck_remove_dirent(env, com, dir, fid, index);
806 case LSLF_BAD_INDEX1:
807 index = lmv->lmv_master_mdt_index;
808 lmv->lmv_master_mdt_index = shard_idx;
809 /* The name entry claims an index that is conflict
810 * with a valid existing name entry, then try the
811 * index in the lmv recursively.
813 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, index,
814 LSLF_BAD_INDEX2, lslr->lslr_flags, depth);
815 lmv->lmv_master_mdt_index = index;
816 if (rc == -ERANGE || rc == -EEXIST)
817 /* The index in the lmv is invalid or also
818 * conflict with other. Then we do not know how
819 * to resolve the conflict. We will handle it as
820 * handle the case of 'LSLF_NONE' vs 'LSLF_NONE'
822 rc = lfsck_remove_lmv(env, com, dir, lnr);
841 return rc > 0 ? 0 : rc;
845 * Read LMV from bottom object, so it doesn't contain stripe FIDs.
847 * TODO: test migrating/foreign directory lfsck
849 * \param[in] env thread env
850 * \param[in] lfsck lfsck instance
851 * \param[in] obj dt object
852 * \param[out] lmv LMV data pointer
854 * \retval 0 on success
855 * \retval -ENODATA on no LMV, corrupt LMV, dir is dead or foreign
856 * -ev on other failures
858 int lfsck_read_stripe_lmv(const struct lu_env *env,
859 struct lfsck_instance *lfsck,
860 struct dt_object *obj,
861 struct lmv_mds_md_v1 *lmv)
863 struct lfsck_thread_info *info = lfsck_env_info(env);
864 struct lu_buf *buf = &info->lti_buf;
865 struct lmv_foreign_md *lfm;
868 /* use bottom object to avoid reading in shard FIDs */
869 obj = lfsck_object_find_bottom(env, lfsck, lu_object_fid(&obj->do_lu));
873 dt_read_lock(env, obj, 0);
875 buf->lb_len = sizeof(*lmv);
876 rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
877 if (unlikely(rc == -ERANGE)) {
878 buf = &info->lti_big_buf;
879 /* this may be a foreign LMV */
880 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LMV);
881 if (rc > sizeof(*lmv)) {
884 lu_buf_check_and_alloc(buf, rc);
885 rc1 = dt_xattr_get(env, obj, buf, XATTR_NAME_LMV);
892 dt_read_unlock(env, obj);
894 lfsck_object_put(env, obj);
896 if (rc > offsetof(typeof(*lfm), lfm_value) &&
897 *((__u32 *)buf->lb_buf) == LMV_MAGIC_FOREIGN) {
901 value_len = le32_to_cpu(lfm->lfm_length);
903 "foreign LMV EA, magic %x, len %u, type %x, flags %x, for dir "DFID"\n",
904 le32_to_cpu(lfm->lfm_magic), value_len,
905 le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
906 PFID(lfsck_dto2fid(obj)));
908 if (rc != value_len + offsetof(typeof(*lfm), lfm_value))
910 "foreign LMV EA internal size %u does not match EA full size %d for dir "DFID"\n",
911 value_len, rc, PFID(lfsck_dto2fid(obj)));
913 /* no further usage/decode of foreign LMV outside */
917 if (rc == sizeof(*lmv)) {
919 lfsck_lmv_header_le_to_cpu(lmv, lmv);
920 /* if LMV is corrupt, return -ENODATA */
921 if (lmv->lmv_magic != LMV_MAGIC_V1 &&
922 lmv->lmv_magic != LMV_MAGIC_STRIPE)
924 } else if (rc >= 0) {
933 * Parse the shard's index from the given shard name.
935 * The valid shard name/type should be:
936 * 1) The type must be S_IFDIR
937 * 2) The name should be $FID:$index
938 * 3) the index should within valid range.
940 * \param[in] env pointer to the thread context
941 * \param[in] name the shard name
942 * \param[in] namelen the name length
943 * \param[in] type the entry's type
944 * \param[in] fid the entry's FID
946 * \retval zero or positive number for the index from the name
947 * \retval negative error number on failure
949 int lfsck_shard_name_to_index(const struct lu_env *env, const char *name,
950 int namelen, __u16 type, const struct lu_fid *fid)
952 char *name2 = lfsck_env_info(env)->lti_tmpbuf2;
959 LASSERT(name != name2);
961 len = snprintf(name2, sizeof(lfsck_env_info(env)->lti_tmpbuf2),
963 if (namelen < len + 1 || memcmp(name, name2, len) != 0)
967 if (!isdigit(name[len]))
970 idx = idx * 10 + name[len++] - '0';
971 } while (len < namelen);
973 if (idx >= LFSCK_LMV_MAX_STRIPES)
979 static inline bool lfsck_name_hash_match(struct lmv_mds_md_v1 *lmv,
980 const char *name, int namelen)
984 idx = lmv_name_to_stripe_index_old(lmv, name, namelen);
985 if (idx == lmv->lmv_master_mdt_index)
988 if (!lmv_hash_is_layout_changing(lmv->lmv_hash_type))
991 idx = lmv_name_to_stripe_index(lmv, name, namelen);
992 return (idx == lmv->lmv_master_mdt_index);
995 bool lfsck_is_valid_slave_name_entry(const struct lu_env *env,
996 struct lfsck_lmv *llmv,
997 const char *name, int namelen)
999 if (llmv == NULL || !llmv->ll_lmv_slave || !llmv->ll_lmv_verified)
1002 return lfsck_name_hash_match(&llmv->ll_lmv, name, namelen);
1006 * Check whether the given name is a valid entry under the @parent.
1008 * If the @parent is a striped directory then the @child should one
1009 * shard of the striped directory, its name should be $FID:$index.
1011 * If the @parent is a shard of a striped directory, then the name hash
1012 * should match the MDT, otherwise it is invalid.
1014 * \param[in] env pointer to the thread context
1015 * \param[in] parent the parent directory
1016 * \param[in] child the child object to be checked
1017 * \param[in] cname the name for the @child in the parent directory
1019 * \retval positive number for invalid name entry
1020 * \retval 0 if the name is valid or uncertain
1021 * \retval negative error number on failure
1023 int lfsck_namespace_check_name(const struct lu_env *env,
1024 struct lfsck_instance *lfsck,
1025 struct dt_object *parent,
1026 struct dt_object *child,
1027 const struct lu_name *cname)
1029 struct lmv_mds_md_v1 *lmv = &lfsck_env_info(env)->lti_lmv;
1032 rc = lfsck_read_stripe_lmv(env, lfsck, parent, lmv);
1034 RETURN(rc == -ENODATA ? 0 : rc);
1036 if (lmv->lmv_magic == LMV_MAGIC_STRIPE) {
1037 if (!lfsck_is_valid_slave_lmv(lmv))
1040 if (!lfsck_name_hash_match(lmv, cname->ln_name,
1043 } else if (lfsck_shard_name_to_index(env, cname->ln_name,
1044 cname->ln_namelen, lfsck_object_type(child),
1045 lfsck_dto2fid(child)) < 0) {
1053 * Update the object's LMV EA with the given @lmv.
1055 * \param[in] env pointer to the thread context
1056 * \param[in] com pointer to the lfsck component
1057 * \param[in] obj pointer to the object which LMV EA will be updated
1058 * \param[in] lmv pointer to buffer holding the new LMV EA
1059 * \param[in] locked whether the caller has held ldlm lock on the @obj or not
1061 * \retval positive number for nothing to be done
1062 * \retval zero if updated successfully
1063 * \retval negative error number on failure
1065 int lfsck_namespace_update_lmv(const struct lu_env *env,
1066 struct lfsck_component *com,
1067 struct dt_object *obj,
1068 struct lmv_mds_md_v1 *lmv, bool locked)
1070 struct lfsck_thread_info *info = lfsck_env_info(env);
1071 struct lmv_mds_md_v1 *lmv4 = &info->lti_lmv4;
1072 struct lu_buf *buf = &info->lti_buf;
1073 struct lfsck_instance *lfsck = com->lc_lfsck;
1074 struct dt_device *dev = lfsck_obj2dev(obj);
1075 struct thandle *th = NULL;
1076 struct lustre_handle lh = { 0 };
1081 LASSERT(lmv4 != lmv);
1083 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1086 lfsck_lmv_header_cpu_to_le(lmv4, lmv);
1087 lfsck_buf_init(buf, lmv4, sizeof(*lmv4));
1090 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1091 MDS_INODELOCK_UPDATE |
1092 MDS_INODELOCK_XATTR, LCK_EX);
1097 th = lfsck_trans_create(env, dev, lfsck);
1099 GOTO(log, rc = PTR_ERR(th));
1101 /* For remote updating LMV EA, there will be further LFSCK action on
1102 * remote MDT after the updating, so update the LMV EA synchronously.
1104 if (dt_object_remote(obj))
1107 rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
1111 rc = dt_trans_start_local(env, dev, th);
1115 dt_write_lock(env, obj, 0);
1116 if (unlikely(lfsck_is_dead_obj(obj)))
1117 GOTO(unlock, rc = 1);
1119 rc = dt_xattr_set(env, obj, buf, XATTR_NAME_LMV, 0, th);
1124 dt_write_unlock(env, obj);
1127 rc1 = dt_trans_stop(env, dev, th);
1132 lfsck_ibits_unlock(&lh, LCK_EX);
1136 "%s: namespace LFSCK updated the %s LMV EA for the object "DFID": rc = %d\n",
1137 lfsck_lfsck2name(lfsck),
1138 lmv->lmv_magic == LMV_MAGIC ? "master" : "slave",
1139 PFID(lfsck_dto2fid(obj)), rc);
1145 * Check whether allow to re-genereate the lost master LMV EA.
1147 * If the master MDT-object of the striped directory lost its master LMV EA,
1148 * then before the LFSCK repaired the striped directory, some ones may have
1149 * created some objects (that are not normal shards of the striped directory)
1150 * under the master MDT-object. If such case happend, then the LFSCK cannot
1151 * re-generate the lost master LMV EA to keep those objects to be visible to
1154 * \param[in] env pointer to the thread context
1155 * \param[in] com pointer to the lfsck component
1156 * \param[in] obj pointer to the master MDT-object to be checked
1157 * \param[in] cfid the shard's FID used for verification
1158 * \param[in] cidx the shard's index used for verification
1160 * \retval positive number if not allow to re-generate LMV EA
1161 * \retval zero if allow to re-generate LMV EA
1162 * \retval negative error number on failure
1164 static int lfsck_allow_regenerate_master_lmv(const struct lu_env *env,
1165 struct lfsck_component *com,
1166 struct dt_object *obj,
1167 const struct lu_fid *cfid,
1170 struct lfsck_thread_info *info = lfsck_env_info(env);
1171 struct lu_fid *tfid = &info->lti_fid3;
1172 struct lfsck_instance *lfsck = com->lc_lfsck;
1173 struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
1174 const struct dt_it_ops *iops;
1182 if (unlikely(!dt_try_as_dir(env, obj, true)))
1185 /* Check whether the shard and the master MDT-object matches or not. */
1186 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf), DFID":%u",
1188 rc = dt_lookup(env, obj, (struct dt_rec *)tfid,
1189 (const struct dt_key *)info->lti_tmpbuf);
1193 if (!lu_fid_eq(tfid, cfid))
1196 args = lfsck->li_args_dir & ~(LUDA_VERIFY | LUDA_VERIFY_DRYRUN);
1197 iops = &obj->do_index_ops->dio_it;
1198 di = iops->init(env, obj, args);
1200 RETURN(PTR_ERR(di));
1202 rc = iops->load(env, di, 0);
1204 rc = iops->next(env, di);
1212 rc = iops->rec(env, di, (struct dt_rec *)ent, args);
1214 rc = lfsck_unpack_ent(ent, &cookie, &type);
1219 /* skip dot and dotdot entries */
1220 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1223 /* If the subdir name does not match the shard name rule, then
1224 * it is quite possible that it is NOT a shard, but created by
1225 * someone after the master MDT-object lost the master LMV EA.
1226 * But it is also possible that the subdir name entry crashed,
1227 * under such double failure cases, the LFSCK cannot know how
1228 * to repair the inconsistency. For data safe, the LFSCK will
1229 * mark the master MDT-object as read-only. The administrator
1230 * can fix the bad shard name manually, then run LFSCK again.
1232 * XXX: If the subdir name matches the shard name rule, but it
1233 * is not a real shard of the striped directory, instead,
1234 * it was created by someone after the master MDT-object
1235 * lost the LMV EA, then re-generating the master LMV EA
1236 * will cause such subdir to be invisible to client, and
1237 * if its index occupies some lost shard index, then the
1238 * LFSCK will use it to replace the bad shard, and cause
1239 * the subdir (itself) to be invisible for ever.
1241 if (lfsck_shard_name_to_index(env, ent->lde_name,
1242 ent->lde_namelen, type, &ent->lde_fid) < 0)
1246 rc = iops->next(env, di);
1253 iops->fini(env, di);
1259 * Notify remote LFSCK instance that the object's LMV EA has been updated.
1261 * \param[in] env pointer to the thread context
1262 * \param[in] com pointer to the lfsck component
1263 * \param[in] obj pointer to the object on which the LMV EA will be set
1264 * \param[in] event indicate either master or slave LMV EA has been updated
1265 * \param[in] flags indicate which element(s) in the LMV EA has been updated
1266 * \param[in] index the MDT index on which the LFSCK instance to be notified
1268 * \retval positive number if nothing to be done
1269 * \retval zero for success
1270 * \retval negative error number on failure
1272 static int lfsck_namespace_notify_lmv_remote(const struct lu_env *env,
1273 struct lfsck_component *com,
1274 struct dt_object *obj,
1275 __u32 event, __u32 flags,
1278 struct lfsck_request *lr = &lfsck_env_info(env)->lti_lr;
1279 const struct lu_fid *fid = lfsck_dto2fid(obj);
1280 struct lfsck_instance *lfsck = com->lc_lfsck;
1281 struct lfsck_tgt_desc *ltd = NULL;
1282 struct ptlrpc_request *req = NULL;
1286 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, index);
1288 GOTO(out, rc = -ENODEV);
1290 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1293 GOTO(out, rc = -ENOMEM);
1295 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1297 ptlrpc_request_free(req);
1302 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1303 memset(lr, 0, sizeof(*lr));
1304 lr->lr_event = event;
1305 lr->lr_index = lfsck_dev_idx(lfsck);
1306 lr->lr_active = LFSCK_TYPE_NAMESPACE;
1308 lr->lr_flags = flags;
1310 ptlrpc_request_set_replen(req);
1311 rc = ptlrpc_queue_wait(req);
1312 ptlrpc_req_put(req);
1314 GOTO(out, rc = (rc == -ENOENT ? 1 : rc));
1318 "%s: namespace LFSCK notify LMV EA updated for the object "DFID" on MDT %x remotely with event %u, flags %u: rc = %d\n",
1319 lfsck_lfsck2name(lfsck), PFID(fid), index, event, flags, rc);
1328 * Generate request for local LFSCK instance to rescan the striped directory.
1330 * \param[in] env pointer to the thread context
1331 * \param[in] com pointer to the lfsck component
1332 * \param[in] obj pointer to the striped directory to be rescanned
1334 * \retval positive number if nothing to be done
1335 * \retval zero for success
1336 * \retval negative error number on failure
1338 int lfsck_namespace_notify_lmv_master_local(const struct lu_env *env,
1339 struct lfsck_component *com,
1340 struct dt_object *obj)
1342 struct lfsck_instance *lfsck = com->lc_lfsck;
1343 struct lfsck_namespace *ns = com->lc_file_ram;
1344 struct lmv_mds_md_v1 *lmv4 = &lfsck_env_info(env)->lti_lmv4;
1345 struct lfsck_lmv_unit *llu;
1346 struct lfsck_lmv *llmv;
1347 struct lfsck_slave_lmv_rec *lslr;
1352 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1355 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv4);
1360 if (unlikely(llu == NULL))
1363 if (lmv4->lmv_stripe_count < 1)
1364 count = LFSCK_LMV_DEF_STRIPES;
1365 else if (lmv4->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
1366 count = LFSCK_LMV_MAX_STRIPES;
1368 count = lmv4->lmv_stripe_count;
1370 OBD_ALLOC_PTR_ARRAY_LARGE(lslr, count);
1377 INIT_LIST_HEAD(&llu->llu_link);
1378 llu->llu_lfsck = lfsck;
1379 llu->llu_obj = lfsck_object_get(obj);
1380 llmv = &llu->llu_lmv;
1381 llmv->ll_lmv_master = 1;
1382 llmv->ll_inline = 1;
1383 atomic_set(&llmv->ll_ref, 1);
1384 llmv->ll_stripes_allocated = count;
1385 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
1386 llmv->ll_lslr = lslr;
1387 llmv->ll_lmv = *lmv4;
1389 down_write(&com->lc_sem);
1390 if (ns->ln_status != LS_SCANNING_PHASE1 &&
1391 ns->ln_status != LS_SCANNING_PHASE2) {
1392 ns->ln_striped_dirs_skipped++;
1393 up_write(&com->lc_sem);
1394 lfsck_lmv_put(env, llmv);
1396 ns->ln_striped_dirs_repaired++;
1397 llmv->ll_counted = 1;
1398 spin_lock(&lfsck->li_lock);
1399 list_add_tail(&llu->llu_link, &lfsck->li_list_lmv);
1400 spin_unlock(&lfsck->li_lock);
1401 up_write(&com->lc_sem);
1408 * Set master LMV EA for the specified striped directory.
1410 * First, if the master MDT-object of a striped directory lost its LMV EA,
1411 * then there may be some users have created some files under the master
1412 * MDT-object directly. Under such case, the LFSCK cannot re-generate LMV
1413 * EA for the master MDT-object, because we should keep the existing files
1414 * to be visible to client. Then the LFSCK will mark the striped directory
1415 * as read-only and keep it there to be handled by administrator manually.
1417 * If nobody has created files under the master MDT-object of the striped
1418 * directory, then we will set the master LMV EA and generate a new rescan
1419 * (the striped directory) request that will be handled later by the LFSCK
1420 * instance on the MDT later.
1422 * \param[in] env pointer to the thread context
1423 * \param[in] com pointer to the lfsck component
1424 * \param[in] obj pointer to the object on which the LMV EA will be set
1425 * \param[in] lmv pointer to the buffer holding the new LMV EA
1426 * \param[in] cfid the shard's FID used for verification
1427 * \param[in] cidx the shard's index used for verification
1428 * \param[in] flags to indicate which element(s) in the LMV EA will be set
1430 * \retval positive number if nothing to be done
1431 * \retval zero for success
1432 * \retval negative error number on failure
1434 static int lfsck_namespace_set_lmv_master(const struct lu_env *env,
1435 struct lfsck_component *com,
1436 struct dt_object *obj,
1437 struct lmv_mds_md_v1 *lmv,
1438 const struct lu_fid *cfid,
1439 __u32 cidx, __u32 flags)
1441 struct lfsck_thread_info *info = lfsck_env_info(env);
1442 struct lmv_mds_md_v1 *lmv3 = &info->lti_lmv3;
1443 struct lu_seq_range *range = &info->lti_range;
1444 struct lfsck_instance *lfsck = com->lc_lfsck;
1445 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1446 struct lustre_handle lh = { 0 };
1451 fld_range_set_mdt(range);
1452 rc = fld_server_lookup(env, ss->ss_server_fld,
1453 fid_seq(lfsck_dto2fid(obj)), range);
1457 pidx = range->lsr_index;
1458 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1459 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1464 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv3);
1465 if (rc == -ENODATA) {
1466 if (!(flags & LEF_SET_LMV_ALL))
1470 } else if (rc == 0) {
1471 if (flags & LEF_SET_LMV_ALL)
1474 if (flags & LEF_SET_LMV_HASH)
1475 lmv3->lmv_hash_type = lmv->lmv_hash_type;
1480 lmv3->lmv_magic = LMV_MAGIC;
1481 lmv3->lmv_master_mdt_index = pidx;
1482 lmv3->lmv_layout_version++;
1484 if (flags & LEF_SET_LMV_ALL) {
1485 rc = lfsck_allow_regenerate_master_lmv(env, com, obj,
1488 rc = lfsck_disable_master_lmv(env, com, obj, false);
1490 GOTO(log, rc = (rc == 0 ? 1 : rc));
1496 /* To indicate that the master has ever lost LMV EA. */
1497 lmv3->lmv_hash_type |= LMV_HASH_FLAG_LOST_LMV;
1500 rc = lfsck_namespace_update_lmv(env, com, obj, lmv3, true);
1501 if (rc == 0 && flags & LEF_SET_LMV_ALL) {
1502 if (dt_object_remote(obj))
1503 rc = lfsck_namespace_notify_lmv_remote(env, com, obj,
1504 LE_SET_LMV_MASTER, 0, pidx);
1506 rc = lfsck_namespace_notify_lmv_master_local(env, com,
1513 lfsck_ibits_unlock(&lh, LCK_EX);
1514 CDEBUG(D_LFSCK, "%s: namespace LFSCK set master LMV EA for the object "
1515 DFID" on the %s MDT %d, flags %x: rc = %d\n",
1516 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
1517 dt_object_remote(obj) ? "remote" : "local", pidx, flags, rc);
1520 struct lfsck_namespace *ns = com->lc_file_ram;
1522 ns->ln_flags |= LF_INCONSISTENT;
1529 * Repair the bad name hash.
1531 * If the name hash of some name entry under the striped directory does not
1532 * match the shard of the striped directory, then the LFSCK will repair the
1533 * inconsistency. Ideally, the LFSCK should migrate the name entry from the
1534 * current MDT to the right MDT (another one), but before the async commit
1535 * finished, the LFSCK will change the striped directory's hash type as
1536 * LMV_HASH_TYPE_UNKNOWN and mark the lmv flags as LMV_HASH_FLAG_BAD_TYPE.
1538 * \param[in] env pointer to the thread context
1539 * \param[in] com pointer to the lfsck component
1540 * \param[in] shard pointer to the shard of the striped directory that
1541 * contains the bad name entry
1542 * \param[in] llmv pointer to lfsck LMV EA structure
1543 * \param[in] name the name of the bad name hash
1545 * \retval positive number if nothing to be done
1546 * \retval zero for success
1547 * \retval negative error number on failure
1549 int lfsck_namespace_repair_bad_name_hash(const struct lu_env *env,
1550 struct lfsck_component *com,
1551 struct dt_object *shard,
1552 struct lfsck_lmv *llmv,
1555 struct lfsck_thread_info *info = lfsck_env_info(env);
1556 struct lu_fid *pfid = &info->lti_fid3;
1557 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1558 struct lfsck_instance *lfsck = com->lc_lfsck;
1559 struct dt_object *parent = NULL;
1563 rc = dt_lookup_dir(env, shard, dotdot, pfid);
1564 if (rc != 0 || !fid_is_sane(pfid))
1567 parent = lfsck_object_find_bottom(env, lfsck, pfid);
1569 GOTO(log, rc = PTR_ERR(parent));
1571 if (unlikely(!dt_object_exists(parent)))
1572 /* The parent object was previously accessed when verifying
1573 * the slave LMV EA. If this condition is true it is because
1574 * the striped directory is being removed.
1578 *lmv2 = llmv->ll_lmv;
1579 /* only set BAD_TYPE here, do not clear hash type or MIGRATION flag,
1580 * so that user can resume dir migration if this is caused by dir
1581 * migration failure.
1583 lmv2->lmv_hash_type |= LMV_HASH_FLAG_BAD_TYPE;
1584 rc = lfsck_namespace_set_lmv_master(env, com, parent, lmv2,
1585 lfsck_dto2fid(shard),
1586 llmv->ll_lmv.lmv_master_mdt_index,
1593 "%s: namespace LFSCK assistant found bad name hash on the MDT %x, parent "DFID", name %s, shard_%x "DFID": rc = %d\n",
1594 lfsck_lfsck2name(lfsck), lfsck_dev_idx(lfsck),
1595 PFID(pfid), name, llmv->ll_lmv.lmv_master_mdt_index,
1596 PFID(lfsck_dto2fid(shard)), rc);
1598 if (parent != NULL && !IS_ERR(parent))
1599 lfsck_object_put(env, parent);
1605 * Scan the shard of a striped directory for name hash verification.
1607 * During the first-stage scanning, if the LFSCK cannot make sure whether
1608 * the shard of a stripe directory contains valid slave LMV EA or not, then
1609 * it will skip the name hash verification for this shard temporarily, and
1610 * record the shard's FID in the LFSCK tracing file. As the LFSCK processing,
1611 * the slave LMV EA may has been verified/fixed by LFSCK instance on master.
1612 * Then in the second-stage scanning, the shard will be re-scanned, and for
1613 * every name entry under the shard, the name hash will be verified, and for
1614 * unmatched name entry, the LFSCK will try to fix it.
1616 * \param[in] env pointer to the thread context
1617 * \param[in] com pointer to the lfsck component
1618 * \param[in] child pointer to the directory object to be handled
1620 * \retval positive number for scanning successfully
1621 * \retval zero for the scanning is paused
1622 * \retval negative error number on failure
1624 int lfsck_namespace_scan_shard(const struct lu_env *env,
1625 struct lfsck_component *com,
1626 struct dt_object *child)
1628 struct lfsck_thread_info *info = lfsck_env_info(env);
1629 struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
1630 struct lfsck_instance *lfsck = com->lc_lfsck;
1631 struct lfsck_namespace *ns = com->lc_file_ram;
1632 struct ptlrpc_thread *thread = &lfsck->li_thread;
1633 struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
1634 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1635 struct lfsck_lmv *llmv = NULL;
1636 const struct dt_it_ops *iops;
1644 rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
1646 RETURN(rc == -ENODATA ? 1 : rc);
1648 if (lmv->lmv_magic != LMV_MAGIC_STRIPE)
1651 if (unlikely(!dt_try_as_dir(env, child, true)))
1654 OBD_ALLOC_PTR(llmv);
1658 llmv->ll_lmv_slave = 1;
1659 llmv->ll_lmv_verified = 1;
1660 llmv->ll_lmv = *lmv;
1661 atomic_set(&llmv->ll_ref, 1);
1663 args = lfsck->li_args_dir & ~(LUDA_VERIFY | LUDA_VERIFY_DRYRUN);
1664 iops = &child->do_index_ops->dio_it;
1665 di = iops->init(env, child, args);
1667 GOTO(out, rc = PTR_ERR(di));
1669 rc = iops->load(env, di, 0);
1671 rc = iops->next(env, di);
1676 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
1677 unlikely(!thread_is_running(thread)))
1680 rc = iops->rec(env, di, (struct dt_rec *)ent, args);
1682 rc = lfsck_unpack_ent(ent, &cookie, &type);
1685 if (bk->lb_param & LPF_FAILOUT)
1691 /* skip dot and dotdot entries */
1692 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1695 if (!lfsck_is_valid_slave_name_entry(env, llmv, ent->lde_name,
1696 ent->lde_namelen)) {
1697 ns->ln_flags |= LF_INCONSISTENT;
1698 rc = lfsck_namespace_repair_bad_name_hash(env, com,
1699 child, llmv, ent->lde_name);
1701 ns->ln_name_hash_repaired++;
1704 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1708 lfsck_control_speed(lfsck);
1709 if (unlikely(!thread_is_running(thread)))
1712 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
1713 spin_lock(&lfsck->li_lock);
1714 thread_set_flags(thread, SVC_STOPPING);
1715 spin_unlock(&lfsck->li_lock);
1717 GOTO(out, rc = -EINVAL);
1721 rc = iops->next(env, di);
1728 iops->fini(env, di);
1729 lfsck_lmv_put(env, llmv);
1735 * Verify the slave object's (of striped directory) LMV EA.
1737 * For the slave object of a striped directory, before traversing the shard
1738 * the LFSCK will verify whether its slave LMV EA matches its parent's master
1741 * \param[in] env pointer to the thread context
1742 * \param[in] com pointer to the lfsck component
1743 * \param[in] obj pointer to the object which LMV EA will be checked
1744 * \param[in] llmv pointer to buffer holding the slave LMV EA
1746 * \retval positive number if nothing to be done
1747 * \retval zero for success
1748 * \retval negative error number on failure
1750 int lfsck_namespace_verify_stripe_slave(const struct lu_env *env,
1751 struct lfsck_component *com,
1752 struct dt_object *obj,
1753 struct lfsck_lmv *llmv)
1755 struct lfsck_thread_info *info = lfsck_env_info(env);
1756 char *name = info->lti_key;
1758 struct lu_fid *pfid = &info->lti_fid3;
1759 const struct lu_fid *cfid = lfsck_dto2fid(obj);
1761 struct lfsck_instance *lfsck = com->lc_lfsck;
1762 struct lmv_mds_md_v1 *clmv = &llmv->ll_lmv;
1763 struct lmv_mds_md_v1 *plmv = &info->lti_lmv;
1764 struct dt_object *parent = NULL;
1768 if (!lfsck_is_valid_slave_lmv(clmv)) {
1769 rc = lfsck_namespace_trace_update(env, com, cfid,
1770 LNTF_UNCERTAIN_LMV, true);
1775 rc = dt_lookup_dir(env, obj, dotdot, pfid);
1776 if (rc != 0 || !fid_is_sane(pfid)) {
1777 rc = lfsck_namespace_trace_update(env, com, cfid,
1778 LNTF_UNCERTAIN_LMV, true);
1783 CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_ENGINE_DELAY, cfs_fail_val);
1785 parent = lfsck_object_find_bottom(env, lfsck, pfid);
1786 if (IS_ERR(parent)) {
1787 rc = lfsck_namespace_trace_update(env, com, cfid,
1788 LNTF_UNCERTAIN_LMV, true);
1793 if (unlikely(!dt_object_exists(parent)))
1796 if (unlikely(!dt_try_as_dir(env, parent, true)))
1797 GOTO(out, rc = -ENOTDIR);
1799 rc = lfsck_read_stripe_lmv(env, lfsck, parent, plmv);
1803 /* If the parent has no LMV EA, then it maybe because:
1804 * 1) The parent lost the LMV EA.
1805 * 2) The child claims a wrong (slave) LMV EA.
1808 rc = lfsck_namespace_set_lmv_master(env, com, parent,
1809 clmv, cfid, clmv->lmv_master_mdt_index,
1814 rc1 = lfsck_namespace_trace_update(env, com, cfid,
1815 LNTF_UNCERTAIN_LMV, true);
1817 GOTO(out, rc = (rc < 0 ? rc : rc1));
1820 /* Unmatched magic or stripe count. */
1821 if (unlikely(plmv->lmv_magic != LMV_MAGIC ||
1822 plmv->lmv_stripe_count != clmv->lmv_stripe_count)) {
1823 rc = lfsck_namespace_trace_update(env, com, cfid,
1824 LNTF_UNCERTAIN_LMV, true);
1829 /* If the master hash type has been set as LMV_HASH_TYPE_UNKNOWN,
1830 * then the slave hash type is not important.
1832 if ((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) ==
1833 LMV_HASH_TYPE_UNKNOWN &&
1834 plmv->lmv_hash_type & LMV_HASH_FLAG_BAD_TYPE)
1837 /* Unmatched hash type. */
1838 if (unlikely((plmv->lmv_hash_type & LMV_HASH_TYPE_MASK) !=
1839 (clmv->lmv_hash_type & LMV_HASH_TYPE_MASK))) {
1840 rc = lfsck_namespace_trace_update(env, com, cfid,
1841 LNTF_UNCERTAIN_LMV, true);
1846 snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2), DFID":%u",
1847 PFID(cfid), clmv->lmv_master_mdt_index);
1848 name2 = info->lti_tmpbuf2;
1850 rc = lfsck_links_get_first(env, obj, name, &tfid);
1851 if (rc == 0 && strcmp(name, name2) == 0 && lu_fid_eq(pfid, &tfid)) {
1852 llmv->ll_lmv_verified = 1;
1857 rc = dt_lookup_dir(env, parent, name2, &tfid);
1858 if (rc != 0 || !lu_fid_eq(cfid, &tfid))
1859 rc = lfsck_namespace_trace_update(env, com, cfid,
1860 LNTF_UNCERTAIN_LMV, true);
1862 llmv->ll_lmv_verified = 1;
1867 if (parent != NULL && !IS_ERR(parent))
1868 lfsck_object_put(env, parent);
1874 * Double scan the striped directory or the shard.
1876 * All the shards' under the given striped directory or its shard have
1877 * been scanned, the LFSCK has got the global knownledge about the LMV
1880 * If the target is one shard of a striped directory, then only needs to
1881 * update related tracing file.
1883 * If the target is the master MDT-object of a striped directory, then the
1884 * LFSCK will make the decision about whether the master LMV EA is invalid
1885 * or not, and repair it if inconsistenct; for every shard of the striped
1886 * directory, whether the slave LMV EA is invalid or not, and repair it if
1889 * \param[in] env pointer to the thread context
1890 * \param[in] com pointer to the lfsck component
1891 * \param[in] lnr pointer to the namespace request that contains the
1892 * striped directory or the shard
1894 * \retval zero for success
1895 * \retval negative error number on failure
1897 int lfsck_namespace_striped_dir_rescan(const struct lu_env *env,
1898 struct lfsck_component *com,
1899 struct lfsck_namespace_req *lnr)
1901 struct lfsck_thread_info *info = lfsck_env_info(env);
1902 struct lfsck_instance *lfsck = com->lc_lfsck;
1903 struct lfsck_namespace *ns = com->lc_file_ram;
1904 struct lfsck_lmv *llmv = lnr->lnr_lmv;
1905 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
1906 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1907 struct lfsck_assistant_object *lso = lnr->lnr_lar.lar_parent;
1908 const struct lu_fid *pfid = &lso->lso_fid;
1909 struct dt_object *dir = NULL;
1910 struct dt_object *obj = NULL;
1911 struct lu_seq_range *range = &info->lti_range;
1912 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1919 if (llmv->ll_lmv_slave) {
1920 if (llmv->ll_lmv_verified) {
1921 ns->ln_striped_shards_scanned++;
1922 lfsck_namespace_trace_update(env, com, pfid,
1923 LNTF_UNCERTAIN_LMV |
1924 LNTF_RECHECK_NAME_HASH, false);
1930 /* Either the striped directory has been disabled or only part of
1931 * the striped directory have been scanned. The LFSCK cannot repair
1932 * something based on incompleted knowledge. So skip it.
1934 if (llmv->ll_ignore || llmv->ll_exit_value <= 0)
1937 /* There ever been some failure, as to the LFSCK cannot know whether
1938 * it has got the global knowledge about the LMV EA consistency or not,
1939 * so it cannot make reparation about the incompleted knowledge.
1941 if (llmv->ll_failed) {
1942 ns->ln_striped_dirs_scanned++;
1943 ns->ln_striped_dirs_failed++;
1948 if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
1949 stripe_count = max(llmv->ll_max_filled_off + 1,
1950 llmv->ll_max_stripe_count);
1952 stripe_count = max(llmv->ll_max_filled_off + 1,
1953 lmv->lmv_stripe_count);
1955 if (lmv->lmv_stripe_count != stripe_count) {
1956 lmv->lmv_stripe_count = stripe_count;
1957 llmv->ll_lmv_updated = 1;
1960 if (!lmv_is_known_hash_type(lmv->lmv_hash_type) &&
1961 !(lmv->lmv_hash_type & LMV_HASH_FLAG_BAD_TYPE) &&
1962 lmv_is_known_hash_type(llmv->ll_hash_type)) {
1963 hash_type = llmv->ll_hash_type & LMV_HASH_TYPE_MASK;
1964 lmv->lmv_hash_type = llmv->ll_hash_type;
1965 llmv->ll_lmv_updated = 1;
1967 hash_type = lmv->lmv_hash_type & LMV_HASH_TYPE_MASK;
1968 if (!lmv_is_known_hash_type(hash_type))
1969 hash_type = LMV_HASH_TYPE_UNKNOWN;
1972 if (llmv->ll_lmv_updated) {
1974 dir = lfsck_assistant_object_load(env, lfsck, lso);
1978 RETURN(rc == -ENOENT ? 0 : rc);
1982 lmv->lmv_layout_version++;
1983 rc = lfsck_namespace_update_lmv(env, com, dir, lmv, false);
1987 ns->ln_striped_dirs_scanned++;
1988 if (!llmv->ll_counted)
1989 ns->ln_striped_dirs_repaired++;
1992 fld_range_set_mdt(range);
1993 for (i = 0; i <= llmv->ll_max_filled_off; i++) {
1994 struct lfsck_slave_lmv_rec *lslr = llmv->ll_lslr + i;
1995 const struct lu_fid *cfid = &lslr->lslr_fid;
1996 const struct lu_name *cname;
1997 struct linkea_data ldata = { NULL };
1999 bool repair_linkea = false;
2000 bool repair_lmvea = false;
2001 bool rename = false;
2002 bool create = false;
2003 bool linkea_repaired = false;
2004 bool lmvea_repaired = false;
2005 bool rename_repaired = false;
2006 bool create_repaired = false;
2009 if (fid_is_zero(cfid))
2012 lnr->lnr_fid = *cfid;
2013 lnr->lnr_namelen = scnprintf(lnr->lnr_name,
2014 lnr->lnr_size - sizeof(*lnr),
2015 DFID":%u", PFID(cfid), i);
2016 cname = lfsck_name_get_const(env, lnr->lnr_name,
2018 obj = lfsck_object_find_bottom(env, lfsck, cfid);
2021 dir = lfsck_assistant_object_load(env, lfsck,
2024 if (PTR_ERR(dir) == -ENOENT)
2029 } else if (lfsck_is_dead_obj(dir)) {
2037 switch (lslr->lslr_flags) {
2039 if (llmv->ll_inline ||
2040 lslr->lslr_stripe_count != stripe_count ||
2041 (lslr->lslr_hash_type & LMV_HASH_TYPE_MASK) !=
2043 repair_lmvea = true;
2045 case LSLF_BAD_INDEX2:
2046 /* The index in the slave LMV EA is right,
2047 * the name entry should be updated.
2050 snprintf(info->lti_tmpbuf2, sizeof(info->lti_tmpbuf2),
2051 DFID":%u", PFID(cfid), lslr->lslr_index);
2052 if (llmv->ll_inline ||
2053 lslr->lslr_stripe_count != stripe_count ||
2054 (lslr->lslr_hash_type & LMV_HASH_TYPE_MASK) !=
2056 repair_lmvea = true;
2058 case LSLF_BAD_INDEX1:
2059 /* The index in the name entry is right,
2060 * the slave LMV EA should be updated.
2063 repair_lmvea = true;
2072 rc1 = lfsck_links_read_with_rec(env, obj, &ldata);
2073 if (rc1 == -ENOENT) {
2078 if (rc1 == -EINVAL || rc1 == -ENODATA) {
2079 repair_linkea = true;
2086 if (ldata.ld_leh->leh_reccount != 1) {
2087 repair_linkea = true;
2091 rc1 = linkea_links_find(&ldata, cname, pfid);
2093 repair_linkea = true;
2098 dir = lfsck_assistant_object_load(env, lfsck,
2111 rc1 = lfsck_namespace_repair_dangling(env, com, dir,
2114 create_repaired = true;
2116 ns->ln_dangling_repaired++;
2122 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
2123 lmv2->lmv_stripe_count = stripe_count;
2124 lmv2->lmv_master_mdt_index = i;
2125 lmv2->lmv_hash_type = hash_type;
2127 rc1 = lfsck_namespace_update_lmv(env, com, obj,
2132 if (dt_object_remote(obj)) {
2133 rc1 = fld_server_lookup(env, ss->ss_server_fld,
2134 fid_seq(lfsck_dto2fid(obj)), range);
2138 rc1 = lfsck_namespace_notify_lmv_remote(env,
2139 com, obj, LE_SET_LMV_SLAVE, 0,
2142 ns->ln_striped_shards_repaired++;
2143 rc1 = lfsck_namespace_trace_update(env, com,
2144 cfid, LNTF_RECHECK_NAME_HASH, true);
2151 lmvea_repaired = true;
2152 } else if (llmv->ll_inline) {
2153 if (dt_object_remote(obj)) {
2154 rc1 = fld_server_lookup(env, ss->ss_server_fld,
2155 fid_seq(lfsck_dto2fid(obj)), range);
2159 /* The slave LMV EA on the remote shard is
2160 * correct, just notify the LFSCK instance
2161 * on such MDT to re-verify the name_hash.
2163 rc1 = lfsck_namespace_notify_lmv_remote(env,
2164 com, obj, LE_SET_LMV_SLAVE,
2165 LEF_RECHECK_NAME_HASH,
2168 rc1 = lfsck_namespace_trace_update(env, com,
2169 cfid, LNTF_RECHECK_NAME_HASH, true);
2178 dir = lfsck_assistant_object_load(env, lfsck,
2191 rc1 = lfsck_namespace_repair_dirent(env, com, dir, obj,
2192 info->lti_tmpbuf2, lnr->lnr_name,
2193 lnr->lnr_type, true, false);
2195 rename_repaired = true;
2197 ns->ln_dirent_repaired++;
2198 rc1 = lfsck_namespace_trace_update(env,
2200 LNTF_RECHECK_NAME_HASH, true);
2208 if (repair_linkea) {
2209 struct lustre_handle lh = { 0 };
2212 dir = lfsck_assistant_object_load(env, lfsck,
2225 rc1 = linkea_links_new(&ldata, &info->lti_big_buf,
2226 cname, lfsck_dto2fid(dir));
2230 rc1 = lfsck_ibits_lock(env, lfsck, obj, &lh,
2231 MDS_INODELOCK_UPDATE |
2232 MDS_INODELOCK_XATTR, LCK_EX);
2236 rc1 = lfsck_namespace_rebuild_linkea(env, com, obj,
2238 lfsck_ibits_unlock(&lh, LCK_EX);
2240 linkea_repaired = true;
2242 ns->ln_linkea_repaired++;
2247 if (create || rename || repair_linkea || repair_lmvea) {
2249 "%s: namespace LFSCK repair the shard %d "DFID" of the striped directory "DFID" with dangling %s/%s, rename %s/%s, llinkea %s/%s, repair_lmvea %s/%s: rc = %d\n",
2250 lfsck_lfsck2name(lfsck),
2251 i, PFID(cfid), PFID(pfid),
2252 create ? "yes" : "no",
2253 create_repaired ? "yes" : "no",
2254 rename ? "yes" : "no",
2255 rename_repaired ? "yes" : "no",
2256 repair_linkea ? "yes" : "no",
2257 linkea_repaired ? "yes" : "no",
2258 repair_lmvea ? "yes" : "no",
2259 lmvea_repaired ? "yes" : "no", rc1);
2262 if (obj != NULL && !IS_ERR(obj)) {
2263 lfsck_object_put(env, obj);
2269 ns->ln_striped_shards_failed++;
2276 if (obj != NULL && !IS_ERR(obj))
2277 lfsck_object_put(env, obj);
2279 if (dir != NULL && !IS_ERR(dir))
2280 lfsck_object_put(env, dir);
2286 * Verify the shard's name entry under the striped directory.
2288 * Before all shards of the striped directory scanned, the LFSCK cannot
2289 * know whether the master LMV EA is valid or not, and also cannot know
2290 * how to repair an invalid shard exactly. For example, the stripe index
2291 * stored in the shard's name does not match the stripe index stored in
2292 * the slave LMV EA, then the LFSCK cannot know which one is correct.
2293 * If the LFSCK just assumed one is correct, and fixed the other, then
2294 * as the LFSCK processing, it may find that the former reparation is
2295 * wrong and have to roll back. Unfortunately, if some applications saw
2296 * the changes and made further modification based on such changes, then
2297 * the roll back is almost impossible.
2299 * To avoid above trouble, the LFSCK will scan the master object of the
2300 * striped directory twice, that is NOT the same as normal two-stages
2301 * scanning, the double scanning the striped directory will happen both
2302 * during the first-stage scanning:
2304 * 1) When the striped directory is opened for scanning, the LFSCK will
2305 * iterate each shard in turn, and records its slave LMV EA in the
2306 * lfsck_lmv::ll_lslr. In this step, if the 'shard' (may be fake
2307 * shard) name does not match the shard naming rule, for example, it
2308 * does not contains the shard's FID, or not contains index, then we
2309 * can remove the bad name entry directly. But if the name is valid,
2310 * but the shard has no slave LMV EA or the slave LMV EA does not
2311 * match its name, then we just record related information in the
2312 * lfsck_lmv::ll_lslr in RAM.
2314 * 2) When all the known shards have been scanned, then the engine will
2315 * generate a dummy request (via lfsck_namespace_close_dir) to tell
2316 * the assistant thread that all the known shards have been scanned.
2317 * Since the assistant has got the global knowledge about the index
2318 * conflict, stripe count, hash type, and so on. Then the assistant
2319 * thread will scan the lfsck_lmv::ll_lslr, and for every shard in
2320 * the record, check and repair inconsistency.
2322 * Generally, the stripe directory has only several shards, and there
2323 * will NOT be a lof of striped directory. So double scanning striped
2324 * directory will not much affect the LFSCK performance.
2326 * \param[in] env pointer to the thread context
2327 * \param[in] com pointer to the lfsck component
2328 * \param[in] lnr pointer to the namespace request that contains the
2329 * shard's name, parent object, parent's LMV, and ect.
2331 * \retval zero for success
2332 * \retval negative error number on failure
2334 int lfsck_namespace_handle_striped_master(const struct lu_env *env,
2335 struct lfsck_component *com,
2336 struct lfsck_namespace_req *lnr)
2338 struct lfsck_thread_info *info = lfsck_env_info(env);
2339 struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
2340 struct lfsck_instance *lfsck = com->lc_lfsck;
2341 struct lfsck_namespace *ns = com->lc_file_ram;
2342 struct lfsck_lmv *llmv = lnr->lnr_lmv;
2343 struct lfsck_assistant_object *lso = lnr->lnr_lar.lar_parent;
2344 const struct lu_fid *pfid = &lso->lso_fid;
2345 struct dt_object *dir;
2346 struct dt_object *obj = NULL;
2347 struct dt_device *dev = NULL;
2352 bool repaired = false;
2353 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
2356 if (unlikely(llmv->ll_ignore))
2359 dir = lfsck_assistant_object_load(env, lfsck, lso);
2363 RETURN(rc == -ENOENT ? 0 : rc);
2366 shard_idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
2368 GOTO(fail_lmv, rc = shard_idx);
2370 if (shard_idx == lfsck_dev_idx(lfsck)) {
2371 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
2374 dev = lfsck->li_bottom;
2376 struct lfsck_tgt_desc *ltd;
2378 /* Usually, some local filesystem consistency verification
2379 * tools can guarantee the local namespace tree consistenct.
2380 * So the LFSCK will only verify the remote directory.
2382 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
2383 rc = lfsck_namespace_trace_update(env, com, pfid,
2384 LNTF_CHECK_PARENT, true);
2389 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, shard_idx);
2390 if (unlikely(ltd == NULL)) {
2392 "%s: cannot talk with MDT %x which did not join the namespace LFSCK\n",
2393 lfsck_lfsck2name(lfsck), shard_idx);
2394 lfsck_lad_set_bitmap(env, com, shard_idx);
2396 GOTO(fail_lmv, rc = -ENODEV);
2402 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
2404 if (lfsck_is_dead_obj(dir))
2407 GOTO(fail_lmv, rc = PTR_ERR(obj));
2410 if (!dt_object_exists(obj)) {
2411 stripe = lfsck_shard_name_to_index(env, lnr->lnr_name,
2412 lnr->lnr_namelen, lnr->lnr_type, &lnr->lnr_fid);
2414 type = LNIT_BAD_DIRENT;
2420 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
2422 memset(lmv, 0, sizeof(*lmv));
2423 lmv->lmv_magic = LMV_MAGIC;
2424 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2425 LSLF_DANGLING, LSLF_NONE, &depth);
2431 stripe = lfsck_shard_name_to_index(env, lnr->lnr_name, lnr->lnr_namelen,
2432 lfsck_object_type(obj),
2435 type = LNIT_BAD_DIRENT;
2440 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv);
2441 if (unlikely(rc == -ENOENT))
2442 /* It may happen when the remote object has been removed,
2443 * but the local MDT does not aware of that.
2448 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2449 LSLF_NO_LMVEA, LSLF_NONE, &depth);
2451 rc = lfsck_record_lmv(env, com, dir, lnr, lmv, stripe,
2452 lmv->lmv_master_mdt_index != stripe ?
2453 LSLF_BAD_INDEX1 : LSLF_NONE, LSLF_NONE,
2459 llmv->ll_failed = 1;
2462 if (rc >= 0 && type == LNIT_NONE && !S_ISDIR(lnr->lnr_type))
2463 type = LNIT_BAD_TYPE;
2467 rc = lfsck_namespace_repair_dirent(env, com, dir, obj,
2468 lnr->lnr_name, lnr->lnr_name,
2469 lnr->lnr_type, true, false);
2473 case LNIT_BAD_DIRENT:
2474 rc = lfsck_namespace_repair_dirent(env, com, dir, obj,
2475 lnr->lnr_name, lnr->lnr_name,
2476 lnr->lnr_type, false, false);
2486 "%s: namespace LFSCK assistant fail to handle the shard: "DFID", parent "DFID", name %.*s: rc = %d\n",
2487 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
2488 PFID(pfid), lnr->lnr_namelen, lnr->lnr_name, rc);
2490 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
2491 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
2492 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
2493 dev != NULL && dev != lfsck->li_bottom)
2494 lfsck_lad_set_bitmap(env, com, shard_idx);
2496 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT))
2500 ns->ln_items_repaired++;
2504 ns->ln_bad_type_repaired++;
2506 case LNIT_BAD_DIRENT:
2507 ns->ln_dirent_repaired++;
2517 if (obj != NULL && !IS_ERR(obj))
2518 lfsck_object_put(env, obj);
2520 lfsck_object_put(env, dir);