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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2017, Intel Corporation.
26 * lustre/obdclass/scrub.c
28 * The OI scrub is used for checking and (re)building Object Index files
29 * that are usually backend special. Here are some general scrub related
30 * functions that can be shared by different backends for OI scrub.
32 * Author: Fan Yong <fan.yong@intel.com>
35 #define DEBUG_SUBSYSTEM S_LFSCK
37 #include <linux/kthread.h>
38 #include <lustre_scrub.h>
39 #include <lustre_lib.h>
40 #include <lustre_fid.h>
42 static inline struct dt_device *scrub_obj2dev(struct dt_object *obj)
44 return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
47 static void scrub_file_to_cpu(struct scrub_file *des, struct scrub_file *src)
49 uuid_copy(&des->sf_uuid, &src->sf_uuid);
50 des->sf_flags = le64_to_cpu(src->sf_flags);
51 des->sf_magic = le32_to_cpu(src->sf_magic);
52 des->sf_status = le16_to_cpu(src->sf_status);
53 des->sf_param = le16_to_cpu(src->sf_param);
54 des->sf_time_last_complete =
55 le64_to_cpu(src->sf_time_last_complete);
56 des->sf_time_latest_start =
57 le64_to_cpu(src->sf_time_latest_start);
58 des->sf_time_last_checkpoint =
59 le64_to_cpu(src->sf_time_last_checkpoint);
60 des->sf_pos_latest_start =
61 le64_to_cpu(src->sf_pos_latest_start);
62 des->sf_pos_last_checkpoint =
63 le64_to_cpu(src->sf_pos_last_checkpoint);
64 des->sf_pos_first_inconsistent =
65 le64_to_cpu(src->sf_pos_first_inconsistent);
66 des->sf_items_checked =
67 le64_to_cpu(src->sf_items_checked);
68 des->sf_items_updated =
69 le64_to_cpu(src->sf_items_updated);
70 des->sf_items_failed =
71 le64_to_cpu(src->sf_items_failed);
72 des->sf_items_updated_prior =
73 le64_to_cpu(src->sf_items_updated_prior);
74 des->sf_run_time = le32_to_cpu(src->sf_run_time);
75 des->sf_success_count = le32_to_cpu(src->sf_success_count);
76 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
77 des->sf_internal_flags = le16_to_cpu(src->sf_internal_flags);
78 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
81 static void scrub_file_to_le(struct scrub_file *des, struct scrub_file *src)
83 uuid_copy(&des->sf_uuid, &src->sf_uuid);
84 des->sf_flags = cpu_to_le64(src->sf_flags);
85 des->sf_magic = cpu_to_le32(src->sf_magic);
86 des->sf_status = cpu_to_le16(src->sf_status);
87 des->sf_param = cpu_to_le16(src->sf_param);
88 des->sf_time_last_complete =
89 cpu_to_le64(src->sf_time_last_complete);
90 des->sf_time_latest_start =
91 cpu_to_le64(src->sf_time_latest_start);
92 des->sf_time_last_checkpoint =
93 cpu_to_le64(src->sf_time_last_checkpoint);
94 des->sf_pos_latest_start =
95 cpu_to_le64(src->sf_pos_latest_start);
96 des->sf_pos_last_checkpoint =
97 cpu_to_le64(src->sf_pos_last_checkpoint);
98 des->sf_pos_first_inconsistent =
99 cpu_to_le64(src->sf_pos_first_inconsistent);
100 des->sf_items_checked =
101 cpu_to_le64(src->sf_items_checked);
102 des->sf_items_updated =
103 cpu_to_le64(src->sf_items_updated);
104 des->sf_items_failed =
105 cpu_to_le64(src->sf_items_failed);
106 des->sf_items_updated_prior =
107 cpu_to_le64(src->sf_items_updated_prior);
108 des->sf_run_time = cpu_to_le32(src->sf_run_time);
109 des->sf_success_count = cpu_to_le32(src->sf_success_count);
110 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
111 des->sf_internal_flags = cpu_to_le16(src->sf_internal_flags);
112 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
115 void scrub_file_init(struct lustre_scrub *scrub, uuid_t uuid)
117 struct scrub_file *sf = &scrub->os_file;
119 memset(sf, 0, sizeof(*sf));
120 uuid_copy(&sf->sf_uuid, &uuid);
121 sf->sf_magic = SCRUB_MAGIC_V1;
122 sf->sf_status = SS_INIT;
124 EXPORT_SYMBOL(scrub_file_init);
126 void scrub_file_reset(struct lustre_scrub *scrub, uuid_t uuid, u64 flags)
128 struct scrub_file *sf = &scrub->os_file;
130 CDEBUG(D_LFSCK, "%s: reset OI scrub file, old flags = "
131 "%#llx, add flags = %#llx\n",
132 scrub->os_name, sf->sf_flags, flags);
134 uuid_copy(&sf->sf_uuid, &uuid);
135 sf->sf_status = SS_INIT;
136 sf->sf_flags |= flags;
137 sf->sf_flags &= ~SF_AUTO;
139 sf->sf_time_latest_start = 0;
140 sf->sf_time_last_checkpoint = 0;
141 sf->sf_pos_latest_start = 0;
142 sf->sf_pos_last_checkpoint = 0;
143 sf->sf_pos_first_inconsistent = 0;
144 sf->sf_items_checked = 0;
145 sf->sf_items_updated = 0;
146 sf->sf_items_failed = 0;
147 sf->sf_items_noscrub = 0;
148 sf->sf_items_igif = 0;
149 if (!scrub->os_in_join)
150 sf->sf_items_updated_prior = 0;
152 EXPORT_SYMBOL(scrub_file_reset);
154 int scrub_file_load(const struct lu_env *env, struct lustre_scrub *scrub)
156 struct scrub_file *sf = &scrub->os_file;
157 struct lu_buf buf = {
158 .lb_buf = &scrub->os_file_disk,
159 .lb_len = sizeof(scrub->os_file_disk)
164 rc = dt_read(env, scrub->os_obj, &buf, &pos);
167 CERROR("%s: fail to load scrub file: rc = %d\n",
177 if (rc < buf.lb_len) {
178 CDEBUG(D_LFSCK, "%s: fail to load scrub file, "
179 "expected = %d: rc = %d\n",
180 scrub->os_name, (int)buf.lb_len, rc);
184 scrub_file_to_cpu(sf, &scrub->os_file_disk);
185 if (sf->sf_magic != SCRUB_MAGIC_V1) {
186 CDEBUG(D_LFSCK, "%s: invalid scrub magic 0x%x != 0x%x\n",
187 scrub->os_name, sf->sf_magic, SCRUB_MAGIC_V1);
193 EXPORT_SYMBOL(scrub_file_load);
195 int scrub_file_store(const struct lu_env *env, struct lustre_scrub *scrub)
197 struct scrub_file *sf = &scrub->os_file_disk;
198 struct dt_object *obj = scrub->os_obj;
199 struct dt_device *dev = scrub_obj2dev(obj);
200 struct lu_buf buf = {
202 .lb_len = sizeof(*sf)
209 /* Skip store under rdonly mode. */
213 scrub_file_to_le(sf, &scrub->os_file);
214 th = dt_trans_create(env, dev);
216 GOTO(log, rc = PTR_ERR(th));
218 rc = dt_declare_record_write(env, obj, &buf, pos, th);
222 rc = dt_trans_start_local(env, dev, th);
226 rc = dt_record_write(env, obj, &buf, &pos, th);
231 dt_trans_stop(env, dev, th);
235 CERROR("%s: store scrub file: rc = %d\n",
238 CDEBUG(D_LFSCK, "%s: store scrub file: rc = %d\n",
241 scrub->os_time_last_checkpoint = ktime_get_seconds();
242 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
243 SCRUB_CHECKPOINT_INTERVAL;
246 EXPORT_SYMBOL(scrub_file_store);
248 int scrub_checkpoint(const struct lu_env *env, struct lustre_scrub *scrub)
250 struct scrub_file *sf = &scrub->os_file;
251 time64_t now = ktime_get_seconds();
254 if (likely(now < scrub->os_time_next_checkpoint ||
255 scrub->os_new_checked == 0))
258 CDEBUG(D_LFSCK, "%s: OI scrub checkpoint at pos %llu\n",
259 scrub->os_name, scrub->os_pos_current);
261 down_write(&scrub->os_rwsem);
262 sf->sf_items_checked += scrub->os_new_checked;
263 scrub->os_new_checked = 0;
264 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
265 sf->sf_time_last_checkpoint = ktime_get_real_seconds();
266 sf->sf_run_time += now - scrub->os_time_last_checkpoint;
267 rc = scrub_file_store(env, scrub);
268 up_write(&scrub->os_rwsem);
272 EXPORT_SYMBOL(scrub_checkpoint);
274 int scrub_start(int (*threadfn)(void *data), struct lustre_scrub *scrub,
275 void *data, __u32 flags)
277 struct ptlrpc_thread *thread = &scrub->os_thread;
278 struct task_struct *task;
283 /* os_lock: sync status between stop and scrub thread */
284 spin_lock(&scrub->os_lock);
285 if (thread_is_running(thread)) {
286 spin_unlock(&scrub->os_lock);
290 if (unlikely(thread_is_stopping(thread))) {
291 spin_unlock(&scrub->os_lock);
292 wait_event_idle(thread->t_ctl_waitq,
293 thread_is_stopped(thread));
296 spin_unlock(&scrub->os_lock);
298 if (scrub->os_file.sf_status == SS_COMPLETED) {
299 if (!(flags & SS_SET_FAILOUT))
300 flags |= SS_CLEAR_FAILOUT;
302 if (!(flags & SS_SET_DRYRUN))
303 flags |= SS_CLEAR_DRYRUN;
308 scrub->os_start_flags = flags;
309 thread_set_flags(thread, 0);
310 task = kthread_run(threadfn, data, "OI_scrub");
313 CERROR("%s: cannot start iteration thread: rc = %d\n",
318 wait_event_idle(thread->t_ctl_waitq,
319 thread_is_running(thread) || thread_is_stopped(thread));
323 EXPORT_SYMBOL(scrub_start);
325 void scrub_stop(struct lustre_scrub *scrub)
327 struct ptlrpc_thread *thread = &scrub->os_thread;
329 /* os_lock: sync status between stop and scrub thread */
330 spin_lock(&scrub->os_lock);
331 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
332 thread_set_flags(thread, SVC_STOPPING);
333 spin_unlock(&scrub->os_lock);
334 wake_up_all(&thread->t_ctl_waitq);
335 wait_event_idle(thread->t_ctl_waitq,
336 thread_is_stopped(thread));
337 /* Do not skip the last lock/unlock, which can guarantee that
338 * the caller cannot return until the OI scrub thread exit. */
339 spin_lock(&scrub->os_lock);
341 spin_unlock(&scrub->os_lock);
343 EXPORT_SYMBOL(scrub_stop);
345 const char *scrub_status_names[] = {
356 const char *scrub_flags_names[] = {
364 const char *scrub_param_names[] = {
370 static void scrub_bits_dump(struct seq_file *m, int bits, const char *names[],
376 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
378 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
381 seq_printf(m, "%s%c", names[i],
382 bits != 0 ? ',' : '\n');
387 static void scrub_time_dump(struct seq_file *m, time64_t time,
391 seq_printf(m, "%s: %llu seconds\n", prefix,
392 ktime_get_real_seconds() - time);
394 seq_printf(m, "%s: N/A\n", prefix);
397 static void scrub_pos_dump(struct seq_file *m, __u64 pos, const char *prefix)
400 seq_printf(m, "%s: %llu\n", prefix, pos);
402 seq_printf(m, "%s: N/A\n", prefix);
405 void scrub_dump(struct seq_file *m, struct lustre_scrub *scrub)
407 struct scrub_file *sf = &scrub->os_file;
411 down_read(&scrub->os_rwsem);
412 seq_printf(m, "name: OI_scrub\n"
416 sf->sf_magic, (int)sf->sf_oi_count,
417 scrub_status_names[sf->sf_status]);
419 scrub_bits_dump(m, sf->sf_flags, scrub_flags_names, "flags");
421 scrub_bits_dump(m, sf->sf_param, scrub_param_names, "param");
423 scrub_time_dump(m, sf->sf_time_last_complete,
424 "time_since_last_completed");
426 scrub_time_dump(m, sf->sf_time_latest_start,
427 "time_since_latest_start");
429 scrub_time_dump(m, sf->sf_time_last_checkpoint,
430 "time_since_last_checkpoint");
432 scrub_pos_dump(m, sf->sf_pos_latest_start,
433 "latest_start_position");
435 scrub_pos_dump(m, sf->sf_pos_last_checkpoint,
436 "last_checkpoint_position");
438 scrub_pos_dump(m, sf->sf_pos_first_inconsistent,
439 "first_failure_position");
441 checked = sf->sf_items_checked + scrub->os_new_checked;
442 seq_printf(m, "checked: %llu\n"
448 "success_count: %u\n",
450 sf->sf_param & SP_DRYRUN ? "inconsistent" : "updated",
451 sf->sf_items_updated, sf->sf_items_failed,
452 sf->sf_param & SP_DRYRUN ? "inconsistent" : "updated",
453 sf->sf_items_updated_prior, sf->sf_items_noscrub,
454 sf->sf_items_igif, sf->sf_success_count);
457 if (thread_is_running(&scrub->os_thread)) {
458 s64 new_checked = scrub->os_new_checked;
462 /* Since the time resolution is in seconds for new system
463 * or small devices it ismore likely that duration will be
464 * zero which will lead to inaccurate results.
466 duration = ktime_get_seconds() -
467 scrub->os_time_last_checkpoint;
469 new_checked = div_s64(new_checked, duration);
471 rtime = sf->sf_run_time + duration;
473 speed = div_s64(speed, rtime);
475 seq_printf(m, "run_time: %lld seconds\n"
476 "average_speed: %lld objects/sec\n"
477 "real_time_speed: %lld objects/sec\n"
478 "current_position: %llu\n"
479 "scrub_in_prior: %s\n"
480 "scrub_full_speed: %s\n"
481 "partial_scan: %s\n",
482 rtime, speed, new_checked,
483 scrub->os_pos_current,
484 scrub->os_in_prior ? "yes" : "no",
485 scrub->os_full_speed ? "yes" : "no",
486 scrub->os_partial_scan ? "yes" : "no");
488 if (sf->sf_run_time != 0)
489 speed = div_s64(speed, sf->sf_run_time);
490 seq_printf(m, "run_time: %ld seconds\n"
491 "average_speed: %lld objects/sec\n"
492 "real_time_speed: N/A\n"
493 "current_position: N/A\n",
494 sf->sf_run_time, speed);
497 up_read(&scrub->os_rwsem);
499 EXPORT_SYMBOL(scrub_dump);
501 int lustre_liru_new(struct list_head *head, const struct lu_fid *pfid,
502 const struct lu_fid *cfid, __u64 child,
503 const char *name, int namelen)
505 struct lustre_index_restore_unit *liru;
506 int len = sizeof(*liru) + namelen + 1;
508 OBD_ALLOC(liru, len);
512 INIT_LIST_HEAD(&liru->liru_link);
513 liru->liru_pfid = *pfid;
514 liru->liru_cfid = *cfid;
515 liru->liru_clid = child;
516 liru->liru_len = len;
517 memcpy(liru->liru_name, name, namelen);
518 liru->liru_name[namelen] = 0;
519 list_add_tail(&liru->liru_link, head);
523 EXPORT_SYMBOL(lustre_liru_new);
525 int lustre_index_register(struct dt_device *dev, const char *devname,
526 struct list_head *head, spinlock_t *lock, int *guard,
527 const struct lu_fid *fid,
528 __u32 keysize, __u32 recsize)
530 struct lustre_index_backup_unit *libu, *pos;
534 if (dev->dd_rdonly || *guard)
541 INIT_LIST_HEAD(&libu->libu_link);
542 libu->libu_keysize = keysize;
543 libu->libu_recsize = recsize;
544 libu->libu_fid = *fid;
547 if (unlikely(*guard)) {
554 list_for_each_entry_reverse(pos, head, libu_link) {
555 rc = lu_fid_cmp(&pos->libu_fid, fid);
557 list_add(&libu->libu_link, &pos->libu_link);
564 /* Registered already. But the former registered one
565 * has different keysize/recsize. It may because that
566 * the former values are from disk and corrupted, then
567 * replace it with new values. */
568 if (unlikely(keysize != pos->libu_keysize ||
569 recsize != pos->libu_recsize)) {
570 CWARN("%s: the index "DFID" has registered "
571 "with %u/%u, may be invalid, replace "
573 devname, PFID(fid), pos->libu_keysize,
574 pos->libu_recsize, keysize, recsize);
576 pos->libu_keysize = keysize;
577 pos->libu_recsize = recsize;
589 list_add(&libu->libu_link, head);
594 EXPORT_SYMBOL(lustre_index_register);
596 static void lustre_index_degister(struct list_head *head, spinlock_t *lock,
597 const struct lu_fid *fid)
599 struct lustre_index_backup_unit *libu;
603 list_for_each_entry_reverse(libu, head, libu_link) {
604 rc = lu_fid_cmp(&libu->libu_fid, fid);
605 /* NOT registered. */
610 list_del(&libu->libu_link);
621 lustre_index_backup_make_header(struct lustre_index_backup_header *header,
622 __u32 keysize, __u32 recsize,
623 const struct lu_fid *fid, __u32 count)
625 memset(header, 0, sizeof(*header));
626 header->libh_magic = cpu_to_le32(INDEX_BACKUP_MAGIC_V1);
627 header->libh_count = cpu_to_le32(count);
628 header->libh_keysize = cpu_to_le32(keysize);
629 header->libh_recsize = cpu_to_le32(recsize);
630 fid_cpu_to_le(&header->libh_owner, fid);
633 static int lustre_index_backup_body(const struct lu_env *env,
634 struct dt_object *obj, loff_t *pos,
635 void *buf, int bufsize)
637 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
639 struct lu_buf lbuf = {
646 th = dt_trans_create(env, dev);
650 rc = dt_declare_record_write(env, obj, &lbuf, *pos, th);
654 rc = dt_trans_start_local(env, dev, th);
658 rc = dt_record_write(env, obj, &lbuf, pos, th);
663 dt_trans_stop(env, dev, th);
667 static int lustre_index_backup_header(const struct lu_env *env,
668 struct dt_object *obj,
669 const struct lu_fid *tgt_fid,
670 __u32 keysize, __u32 recsize,
671 void *buf, int bufsize, int count)
673 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
674 struct lustre_index_backup_header *header = buf;
675 struct lu_attr *la = buf;
677 struct lu_buf lbuf = {
679 .lb_len = sizeof(*header)
681 loff_t size = sizeof(*header) + (keysize + recsize) * count;
687 LASSERT(sizeof(*la) <= bufsize);
688 LASSERT(sizeof(*header) <= bufsize);
690 rc = dt_attr_get(env, obj, la);
694 if (la->la_size > size)
697 lustre_index_backup_make_header(header, keysize, recsize,
699 th = dt_trans_create(env, dev);
703 rc = dt_declare_record_write(env, obj, &lbuf, pos, th);
708 rc = dt_declare_punch(env, obj, size, OBD_OBJECT_EOF, th);
713 rc = dt_trans_start_local(env, dev, th);
717 rc = dt_record_write(env, obj, &lbuf, &pos, th);
719 rc = dt_punch(env, obj, size, OBD_OBJECT_EOF, th);
724 dt_trans_stop(env, dev, th);
728 static int lustre_index_update_lma(const struct lu_env *env,
729 struct dt_object *obj,
730 void *buf, int bufsize)
732 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
733 struct lustre_mdt_attrs *lma = buf;
734 struct lu_buf lbuf = {
736 .lb_len = sizeof(struct lustre_ost_attrs)
739 int fl = LU_XATTR_REPLACE;
743 LASSERT(bufsize >= lbuf.lb_len);
745 rc = dt_xattr_get(env, obj, &lbuf, XATTR_NAME_LMA);
746 if (unlikely(rc == -ENODATA)) {
747 fl = LU_XATTR_CREATE;
748 lustre_lma_init(lma, lu_object_fid(&obj->do_lu),
751 } else if (rc < sizeof(*lma)) {
752 RETURN(rc < 0 ? rc : -EFAULT);
754 lustre_lma_swab(lma);
755 if (lma->lma_compat & LMAC_IDX_BACKUP)
758 lma->lma_compat |= LMAC_IDX_BACKUP;
761 lustre_lma_swab(lma);
763 th = dt_trans_create(env, dev);
767 rc = dt_declare_xattr_set(env, obj, &lbuf, XATTR_NAME_LMA, fl, th);
771 rc = dt_trans_start_local(env, dev, th);
775 rc = dt_xattr_set(env, obj, &lbuf, XATTR_NAME_LMA, fl, th);
780 dt_trans_stop(env, dev, th);
784 static int lustre_index_backup_one(const struct lu_env *env,
785 struct local_oid_storage *los,
786 struct dt_object *parent,
787 struct lustre_index_backup_unit *libu,
788 char *buf, int bufsize)
790 struct dt_device *dev = scrub_obj2dev(parent);
791 struct dt_object *tgt_obj = NULL;
792 struct dt_object *bak_obj = NULL;
793 const struct dt_it_ops *iops;
795 loff_t pos = sizeof(struct lustre_index_backup_header);
801 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
802 &libu->libu_fid, NULL));
803 if (IS_ERR_OR_NULL(tgt_obj))
804 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
806 if (!dt_object_exists(tgt_obj))
809 if (!tgt_obj->do_index_ops) {
810 struct dt_index_features feat;
812 feat.dif_flags = DT_IND_UPDATE;
813 feat.dif_keysize_min = libu->libu_keysize;
814 feat.dif_keysize_max = libu->libu_keysize;
815 feat.dif_recsize_min = libu->libu_recsize;
816 feat.dif_recsize_max = libu->libu_recsize;
817 feat.dif_ptrsize = 4;
818 rc = tgt_obj->do_ops->do_index_try(env, tgt_obj, &feat);
823 lustre_fid2lbx(buf, &libu->libu_fid, bufsize);
824 bak_obj = local_file_find_or_create(env, los, parent, buf,
825 S_IFREG | S_IRUGO | S_IWUSR);
826 if (IS_ERR_OR_NULL(bak_obj))
827 GOTO(out, rc = bak_obj ? PTR_ERR(bak_obj) : -ENOENT);
829 iops = &tgt_obj->do_index_ops->dio_it;
830 di = iops->init(env, tgt_obj, 0);
832 GOTO(out, rc = PTR_ERR(di));
834 rc = iops->load(env, di, 0);
836 rc = iops->next(env, di);
844 key = iops->key(env, di);
845 memcpy(&buf[size], key, libu->libu_keysize);
846 size += libu->libu_keysize;
848 rc = iops->rec(env, di, rec, 0);
852 size += libu->libu_recsize;
854 if (size + libu->libu_keysize + libu->libu_recsize > bufsize) {
855 rc = lustre_index_backup_body(env, bak_obj, &pos,
863 rc = iops->next(env, di);
866 if (rc >= 0 && size > 0)
867 rc = lustre_index_backup_body(env, bak_obj, &pos, buf, size);
872 rc = lustre_index_backup_header(env, bak_obj, &libu->libu_fid,
873 libu->libu_keysize, libu->libu_recsize,
874 buf, bufsize, count);
876 rc = lustre_index_update_lma(env, tgt_obj, buf, bufsize);
878 if (!rc && OBD_FAIL_CHECK(OBD_FAIL_OSD_INDEX_CRASH)) {
879 LASSERT(bufsize >= 512);
883 lustre_index_backup_body(env, tgt_obj, &pos, buf, 512);
891 if (!IS_ERR_OR_NULL(tgt_obj))
892 dt_object_put_nocache(env, tgt_obj);
893 if (!IS_ERR_OR_NULL(bak_obj))
894 dt_object_put_nocache(env, bak_obj);
898 void lustre_index_backup(const struct lu_env *env, struct dt_device *dev,
899 const char *devname, struct list_head *head,
900 spinlock_t *lock, int *guard, bool backup)
902 struct lustre_index_backup_unit *libu;
903 struct local_oid_storage *los = NULL;
904 struct dt_object *parent = NULL;
910 if (dev->dd_rdonly || *guard)
917 if (list_empty(head))
920 /* Handle kinds of failures during mount process. */
921 if (!dev->dd_lu_dev.ld_site || !dev->dd_lu_dev.ld_site->ls_top_dev)
925 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
931 lu_local_obj_fid(&fid, INDEX_BACKUP_OID);
932 parent = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
934 if (IS_ERR_OR_NULL(parent)) {
935 CERROR("%s: failed to locate backup dir: rc = %ld\n",
936 devname, parent ? PTR_ERR(parent) : -ENOENT);
941 lu_local_name_obj_fid(&fid, 1);
942 rc = local_oid_storage_init(env, dev, &fid, &los);
944 CERROR("%s: failed to init local storage: rc = %d\n",
952 while (!list_empty(head)) {
953 libu = list_entry(head->next,
954 struct lustre_index_backup_unit, libu_link);
955 list_del_init(&libu->libu_link);
959 rc = lustre_index_backup_one(env, los, parent, libu,
960 buf, INDEX_BACKUP_BUFSIZE);
961 CDEBUG(D_WARNING, "%s: backup index "DFID": rc = %d\n",
962 devname, PFID(&libu->libu_fid), rc);
971 local_oid_storage_fini(env, los);
973 dt_object_put_nocache(env, parent);
975 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
979 EXPORT_SYMBOL(lustre_index_backup);
981 int lustre_index_restore(const struct lu_env *env, struct dt_device *dev,
982 const struct lu_fid *parent_fid,
983 const struct lu_fid *tgt_fid,
984 const struct lu_fid *bak_fid, const char *name,
985 struct list_head *head, spinlock_t *lock,
986 char *buf, int bufsize)
988 struct dt_object *parent_obj = NULL;
989 struct dt_object *tgt_obj = NULL;
990 struct dt_object *bak_obj = NULL;
991 struct lustre_index_backup_header *header;
992 struct dt_index_features *feat;
993 struct dt_object_format *dof;
996 struct lu_object_conf conf;
997 struct dt_insert_rec ent;
1006 bool registered = false;
1009 LASSERT(bufsize >= sizeof(*la) + sizeof(*dof) +
1010 sizeof(*feat) + sizeof(*header));
1012 memset(buf, 0, bufsize);
1013 la = (struct lu_attr *)buf;
1014 dof = (void *)la + sizeof(*la);
1015 feat = (void *)dof + sizeof(*dof);
1016 header = (void *)feat + sizeof(*feat);
1017 lbuf.lb_buf = header;
1018 lbuf.lb_len = sizeof(*header);
1020 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1022 if (IS_ERR_OR_NULL(tgt_obj))
1023 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
1025 bak_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1027 if (IS_ERR_OR_NULL(bak_obj))
1028 GOTO(out, rc = bak_obj ? PTR_ERR(bak_obj) : -ENOENT);
1030 if (!dt_object_exists(bak_obj))
1031 GOTO(out, rc = -ENOENT);
1033 parent_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1035 if (IS_ERR_OR_NULL(parent_obj))
1036 GOTO(out, rc = parent_obj ? PTR_ERR(parent_obj) : -ENOENT);
1038 LASSERT(dt_object_exists(parent_obj));
1040 if (unlikely(!dt_try_as_dir(env, parent_obj)))
1041 GOTO(out, rc = -ENOTDIR);
1043 rc = dt_attr_get(env, tgt_obj, la);
1047 rc = dt_record_read(env, bak_obj, &lbuf, &pos);
1051 if (le32_to_cpu(header->libh_magic) != INDEX_BACKUP_MAGIC_V1)
1052 GOTO(out, rc = -EINVAL);
1054 fid_le_to_cpu(&tfid, &header->libh_owner);
1055 if (unlikely(!lu_fid_eq(tgt_fid, &tfid)))
1056 GOTO(out, rc = -EINVAL);
1058 keysize = le32_to_cpu(header->libh_keysize);
1059 recsize = le32_to_cpu(header->libh_recsize);
1060 pairsize = keysize + recsize;
1062 memset(feat, 0, sizeof(*feat));
1063 feat->dif_flags = DT_IND_UPDATE;
1064 feat->dif_keysize_min = feat->dif_keysize_max = keysize;
1065 feat->dif_recsize_min = feat->dif_recsize_max = recsize;
1066 feat->dif_ptrsize = 4;
1068 /* T1: remove old name entry and destroy old index. */
1069 th = dt_trans_create(env, dev);
1071 GOTO(out, rc = PTR_ERR(th));
1073 rc = dt_declare_delete(env, parent_obj,
1074 (const struct dt_key *)name, th);
1078 rc = dt_declare_destroy(env, tgt_obj, th);
1082 rc = dt_trans_start_local(env, dev, th);
1086 rc = dt_delete(env, parent_obj, (const struct dt_key *)name, th);
1090 dt_write_lock(env, tgt_obj, 0);
1091 rc = dt_destroy(env, tgt_obj, th);
1092 dt_write_unlock(env, tgt_obj);
1093 dt_trans_stop(env, dev, th);
1097 la->la_valid = LA_MODE | LA_UID | LA_GID;
1098 conf.loc_flags = LOC_F_NEW;
1099 dof->u.dof_idx.di_feat = feat;
1100 dof->dof_type = DFT_INDEX;
1101 ent.rec_type = S_IFREG;
1102 ent.rec_fid = tgt_fid;
1104 /* Drop cache before re-create it. */
1105 dt_object_put_nocache(env, tgt_obj);
1106 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1108 if (IS_ERR_OR_NULL(tgt_obj))
1109 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
1111 LASSERT(!dt_object_exists(tgt_obj));
1113 /* T2: create new index and insert new name entry. */
1114 th = dt_trans_create(env, dev);
1116 GOTO(out, rc = PTR_ERR(th));
1118 rc = dt_declare_create(env, tgt_obj, la, NULL, dof, th);
1122 rc = dt_declare_insert(env, parent_obj, (const struct dt_rec *)&ent,
1123 (const struct dt_key *)name, th);
1127 rc = dt_trans_start_local(env, dev, th);
1131 dt_write_lock(env, tgt_obj, 0);
1132 rc = dt_create(env, tgt_obj, la, NULL, dof, th);
1133 dt_write_unlock(env, tgt_obj);
1137 rc = dt_insert(env, parent_obj, (const struct dt_rec *)&ent,
1138 (const struct dt_key *)name, th);
1139 dt_trans_stop(env, dev, th);
1140 /* Some index name may has been inserted by OSD
1141 * automatically when create the index object. */
1142 if (unlikely(rc == -EEXIST))
1147 /* The new index will register via index_try. */
1148 rc = tgt_obj->do_ops->do_index_try(env, tgt_obj, feat);
1153 count = le32_to_cpu(header->libh_count);
1154 while (!rc && count > 0) {
1155 int size = pairsize * count;
1159 if (size > bufsize) {
1160 items = bufsize / pairsize;
1161 size = pairsize * items;
1166 rc = dt_record_read(env, bak_obj, &lbuf, &pos);
1167 for (i = 0; i < items && !rc; i++) {
1168 void *key = &buf[i * pairsize];
1169 void *rec = &buf[i * pairsize + keysize];
1171 /* Tn: restore the records. */
1172 th = dt_trans_create(env, dev);
1174 GOTO(out, rc = -ENOMEM);
1176 rc = dt_declare_insert(env, tgt_obj, rec, key, th);
1180 rc = dt_trans_start_local(env, dev, th);
1184 rc = dt_insert(env, tgt_obj, rec, key, th);
1185 if (unlikely(rc == -EEXIST))
1188 dt_trans_stop(env, dev, th);
1197 dt_trans_stop(env, dev, th);
1198 if (rc && registered)
1199 /* Degister the index to avoid overwriting the backup. */
1200 lustre_index_degister(head, lock, tgt_fid);
1203 if (!IS_ERR_OR_NULL(tgt_obj))
1204 dt_object_put_nocache(env, tgt_obj);
1205 if (!IS_ERR_OR_NULL(bak_obj))
1206 dt_object_put_nocache(env, bak_obj);
1207 if (!IS_ERR_OR_NULL(parent_obj))
1208 dt_object_put_nocache(env, parent_obj);
1211 EXPORT_SYMBOL(lustre_index_restore);