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_of_safe(obj->do_lu.lo_dev, struct dt_device,
48 static void scrub_file_to_cpu(struct scrub_file *des, struct scrub_file *src)
50 uuid_copy(&des->sf_uuid, &src->sf_uuid);
51 des->sf_flags = le64_to_cpu(src->sf_flags);
52 des->sf_magic = le32_to_cpu(src->sf_magic);
53 des->sf_status = le16_to_cpu(src->sf_status);
54 des->sf_param = le16_to_cpu(src->sf_param);
55 des->sf_time_last_complete =
56 le64_to_cpu(src->sf_time_last_complete);
57 des->sf_time_latest_start =
58 le64_to_cpu(src->sf_time_latest_start);
59 des->sf_time_last_checkpoint =
60 le64_to_cpu(src->sf_time_last_checkpoint);
61 des->sf_pos_latest_start =
62 le64_to_cpu(src->sf_pos_latest_start);
63 des->sf_pos_last_checkpoint =
64 le64_to_cpu(src->sf_pos_last_checkpoint);
65 des->sf_pos_first_inconsistent =
66 le64_to_cpu(src->sf_pos_first_inconsistent);
67 des->sf_items_checked =
68 le64_to_cpu(src->sf_items_checked);
69 des->sf_items_updated =
70 le64_to_cpu(src->sf_items_updated);
71 des->sf_items_failed =
72 le64_to_cpu(src->sf_items_failed);
73 des->sf_items_updated_prior =
74 le64_to_cpu(src->sf_items_updated_prior);
75 des->sf_run_time = le32_to_cpu(src->sf_run_time);
76 des->sf_success_count = le32_to_cpu(src->sf_success_count);
77 des->sf_oi_count = le16_to_cpu(src->sf_oi_count);
78 des->sf_internal_flags = le16_to_cpu(src->sf_internal_flags);
79 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
82 static void scrub_file_to_le(struct scrub_file *des, struct scrub_file *src)
84 uuid_copy(&des->sf_uuid, &src->sf_uuid);
85 des->sf_flags = cpu_to_le64(src->sf_flags);
86 des->sf_magic = cpu_to_le32(src->sf_magic);
87 des->sf_status = cpu_to_le16(src->sf_status);
88 des->sf_param = cpu_to_le16(src->sf_param);
89 des->sf_time_last_complete =
90 cpu_to_le64(src->sf_time_last_complete);
91 des->sf_time_latest_start =
92 cpu_to_le64(src->sf_time_latest_start);
93 des->sf_time_last_checkpoint =
94 cpu_to_le64(src->sf_time_last_checkpoint);
95 des->sf_pos_latest_start =
96 cpu_to_le64(src->sf_pos_latest_start);
97 des->sf_pos_last_checkpoint =
98 cpu_to_le64(src->sf_pos_last_checkpoint);
99 des->sf_pos_first_inconsistent =
100 cpu_to_le64(src->sf_pos_first_inconsistent);
101 des->sf_items_checked =
102 cpu_to_le64(src->sf_items_checked);
103 des->sf_items_updated =
104 cpu_to_le64(src->sf_items_updated);
105 des->sf_items_failed =
106 cpu_to_le64(src->sf_items_failed);
107 des->sf_items_updated_prior =
108 cpu_to_le64(src->sf_items_updated_prior);
109 des->sf_run_time = cpu_to_le32(src->sf_run_time);
110 des->sf_success_count = cpu_to_le32(src->sf_success_count);
111 des->sf_oi_count = cpu_to_le16(src->sf_oi_count);
112 des->sf_internal_flags = cpu_to_le16(src->sf_internal_flags);
113 memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
116 void scrub_file_init(struct lustre_scrub *scrub, uuid_t uuid)
118 struct scrub_file *sf = &scrub->os_file;
120 memset(sf, 0, sizeof(*sf));
121 uuid_copy(&sf->sf_uuid, &uuid);
122 sf->sf_magic = SCRUB_MAGIC_V1;
123 sf->sf_status = SS_INIT;
125 EXPORT_SYMBOL(scrub_file_init);
127 void scrub_file_reset(struct lustre_scrub *scrub, uuid_t uuid, u64 flags)
129 struct scrub_file *sf = &scrub->os_file;
131 CDEBUG(D_LFSCK, "%s: reset OI scrub file, old flags = "
132 "%#llx, add flags = %#llx\n",
133 scrub->os_name, sf->sf_flags, flags);
135 uuid_copy(&sf->sf_uuid, &uuid);
136 sf->sf_status = SS_INIT;
137 sf->sf_flags |= flags;
138 sf->sf_flags &= ~SF_AUTO;
140 sf->sf_time_latest_start = 0;
141 sf->sf_time_last_checkpoint = 0;
142 sf->sf_pos_latest_start = 0;
143 sf->sf_pos_last_checkpoint = 0;
144 sf->sf_pos_first_inconsistent = 0;
145 sf->sf_items_checked = 0;
146 sf->sf_items_updated = 0;
147 sf->sf_items_failed = 0;
148 sf->sf_items_noscrub = 0;
149 sf->sf_items_igif = 0;
150 if (!scrub->os_in_join)
151 sf->sf_items_updated_prior = 0;
153 EXPORT_SYMBOL(scrub_file_reset);
155 int scrub_file_load(const struct lu_env *env, struct lustre_scrub *scrub)
157 struct scrub_file *sf = &scrub->os_file;
158 struct lu_buf buf = {
159 .lb_buf = &scrub->os_file_disk,
160 .lb_len = sizeof(scrub->os_file_disk)
165 rc = dt_read(env, scrub->os_obj, &buf, &pos);
168 CERROR("%s: fail to load scrub file: rc = %d\n",
178 if (rc < buf.lb_len) {
179 CDEBUG(D_LFSCK, "%s: fail to load scrub file, "
180 "expected = %d: rc = %d\n",
181 scrub->os_name, (int)buf.lb_len, rc);
185 scrub_file_to_cpu(sf, &scrub->os_file_disk);
186 if (sf->sf_magic != SCRUB_MAGIC_V1) {
187 CDEBUG(D_LFSCK, "%s: invalid scrub magic 0x%x != 0x%x\n",
188 scrub->os_name, sf->sf_magic, SCRUB_MAGIC_V1);
194 EXPORT_SYMBOL(scrub_file_load);
196 int scrub_file_store(const struct lu_env *env, struct lustre_scrub *scrub)
198 struct scrub_file *sf = &scrub->os_file_disk;
199 struct dt_object *obj = scrub->os_obj;
200 struct dt_device *dev = scrub_obj2dev(obj);
201 struct lu_buf buf = {
203 .lb_len = sizeof(*sf)
210 /* Skip store under rdonly mode. */
214 scrub_file_to_le(sf, &scrub->os_file);
215 th = dt_trans_create(env, dev);
217 GOTO(log, rc = PTR_ERR(th));
219 rc = dt_declare_record_write(env, obj, &buf, pos, th);
223 rc = dt_trans_start_local(env, dev, th);
227 rc = dt_record_write(env, obj, &buf, &pos, th);
232 dt_trans_stop(env, dev, th);
236 CERROR("%s: store scrub file: rc = %d\n",
239 CDEBUG(D_LFSCK, "%s: store scrub file: rc = %d\n",
242 scrub->os_time_last_checkpoint = ktime_get_seconds();
243 scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
244 SCRUB_CHECKPOINT_INTERVAL;
247 EXPORT_SYMBOL(scrub_file_store);
249 int scrub_checkpoint(const struct lu_env *env, struct lustre_scrub *scrub)
251 struct scrub_file *sf = &scrub->os_file;
252 time64_t now = ktime_get_seconds();
255 if (likely(now < scrub->os_time_next_checkpoint ||
256 scrub->os_new_checked == 0))
259 CDEBUG(D_LFSCK, "%s: OI scrub checkpoint at pos %llu\n",
260 scrub->os_name, scrub->os_pos_current);
262 down_write(&scrub->os_rwsem);
263 sf->sf_items_checked += scrub->os_new_checked;
264 scrub->os_new_checked = 0;
265 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
266 sf->sf_time_last_checkpoint = ktime_get_real_seconds();
267 sf->sf_run_time += now - scrub->os_time_last_checkpoint;
268 rc = scrub_file_store(env, scrub);
269 up_write(&scrub->os_rwsem);
273 EXPORT_SYMBOL(scrub_checkpoint);
275 int scrub_start(int (*threadfn)(void *data), struct lustre_scrub *scrub,
276 void *data, __u32 flags)
278 struct task_struct *task;
285 if (scrub->os_file.sf_status == SS_COMPLETED) {
286 if (!(flags & SS_SET_FAILOUT))
287 flags |= SS_CLEAR_FAILOUT;
289 if (!(flags & SS_SET_DRYRUN))
290 flags |= SS_CLEAR_DRYRUN;
295 task = kthread_create(threadfn, data, "OI_scrub");
298 CERROR("%s: cannot start iteration thread: rc = %d\n",
302 spin_lock(&scrub->os_lock);
303 if (scrub->os_task) {
305 spin_unlock(&scrub->os_lock);
309 scrub->os_start_flags = flags;
310 scrub->os_task = task;
311 wake_up_process(task);
312 spin_unlock(&scrub->os_lock);
313 wait_var_event(scrub, scrub->os_running || !scrub->os_task);
317 EXPORT_SYMBOL(scrub_start);
319 void scrub_stop(struct lustre_scrub *scrub)
321 struct task_struct *task;
323 spin_lock(&scrub->os_lock);
324 scrub->os_running = 0;
325 spin_unlock(&scrub->os_lock);
326 task = xchg(&scrub->os_task, NULL);
330 EXPORT_SYMBOL(scrub_stop);
332 const char *scrub_status_names[] = {
343 const char *scrub_flags_names[] = {
351 const char *scrub_param_names[] = {
357 static void scrub_bits_dump(struct seq_file *m, int bits, const char *names[],
363 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
365 for (i = 0, flag = 1; bits != 0; i++, flag = BIT(i)) {
368 seq_printf(m, "%s%c", names[i],
369 bits != 0 ? ',' : '\n');
374 static void scrub_time_dump(struct seq_file *m, time64_t time,
378 seq_printf(m, "%s: %llu seconds\n", prefix,
379 ktime_get_real_seconds() - time);
381 seq_printf(m, "%s: N/A\n", prefix);
384 static void scrub_pos_dump(struct seq_file *m, __u64 pos, const char *prefix)
387 seq_printf(m, "%s: %llu\n", prefix, pos);
389 seq_printf(m, "%s: N/A\n", prefix);
392 void scrub_dump(struct seq_file *m, struct lustre_scrub *scrub)
394 struct scrub_file *sf = &scrub->os_file;
398 down_read(&scrub->os_rwsem);
399 seq_printf(m, "name: OI_scrub\n"
403 sf->sf_magic, (int)sf->sf_oi_count,
404 scrub_status_names[sf->sf_status]);
406 scrub_bits_dump(m, sf->sf_flags, scrub_flags_names, "flags");
408 scrub_bits_dump(m, sf->sf_param, scrub_param_names, "param");
410 scrub_time_dump(m, sf->sf_time_last_complete,
411 "time_since_last_completed");
413 scrub_time_dump(m, sf->sf_time_latest_start,
414 "time_since_latest_start");
416 scrub_time_dump(m, sf->sf_time_last_checkpoint,
417 "time_since_last_checkpoint");
419 scrub_pos_dump(m, sf->sf_pos_latest_start,
420 "latest_start_position");
422 scrub_pos_dump(m, sf->sf_pos_last_checkpoint,
423 "last_checkpoint_position");
425 scrub_pos_dump(m, sf->sf_pos_first_inconsistent,
426 "first_failure_position");
428 checked = sf->sf_items_checked + scrub->os_new_checked;
429 seq_printf(m, "checked: %llu\n"
435 "success_count: %u\n",
437 sf->sf_param & SP_DRYRUN ? "inconsistent" : "updated",
438 sf->sf_items_updated, sf->sf_items_failed,
439 sf->sf_param & SP_DRYRUN ? "inconsistent" : "updated",
440 sf->sf_items_updated_prior, sf->sf_items_noscrub,
441 sf->sf_items_igif, sf->sf_success_count);
444 if (scrub->os_running) {
445 s64 new_checked = scrub->os_new_checked;
449 /* Since the time resolution is in seconds for new system
450 * or small devices it ismore likely that duration will be
451 * zero which will lead to inaccurate results.
453 duration = ktime_get_seconds() -
454 scrub->os_time_last_checkpoint;
456 new_checked = div_s64(new_checked, duration);
458 rtime = sf->sf_run_time + duration;
460 speed = div_s64(speed, rtime);
462 seq_printf(m, "run_time: %lld seconds\n"
463 "average_speed: %lld objects/sec\n"
464 "real_time_speed: %lld objects/sec\n"
465 "current_position: %llu\n"
466 "scrub_in_prior: %s\n"
467 "scrub_full_speed: %s\n"
468 "partial_scan: %s\n",
469 rtime, speed, new_checked,
470 scrub->os_pos_current,
471 scrub->os_in_prior ? "yes" : "no",
472 scrub->os_full_speed ? "yes" : "no",
473 scrub->os_partial_scan ? "yes" : "no");
475 if (sf->sf_run_time != 0)
476 speed = div_s64(speed, sf->sf_run_time);
477 seq_printf(m, "run_time: %d seconds\n"
478 "average_speed: %lld objects/sec\n"
479 "real_time_speed: N/A\n"
480 "current_position: N/A\n",
481 sf->sf_run_time, speed);
484 up_read(&scrub->os_rwsem);
486 EXPORT_SYMBOL(scrub_dump);
488 int lustre_liru_new(struct list_head *head, const struct lu_fid *pfid,
489 const struct lu_fid *cfid, __u64 child,
490 const char *name, int namelen)
492 struct lustre_index_restore_unit *liru;
493 int len = sizeof(*liru) + namelen + 1;
495 OBD_ALLOC(liru, len);
499 INIT_LIST_HEAD(&liru->liru_link);
500 liru->liru_pfid = *pfid;
501 liru->liru_cfid = *cfid;
502 liru->liru_clid = child;
503 liru->liru_len = len;
504 memcpy(liru->liru_name, name, namelen);
505 liru->liru_name[namelen] = 0;
506 list_add_tail(&liru->liru_link, head);
510 EXPORT_SYMBOL(lustre_liru_new);
512 int lustre_index_register(struct dt_device *dev, const char *devname,
513 struct list_head *head, spinlock_t *lock, int *guard,
514 const struct lu_fid *fid,
515 __u32 keysize, __u32 recsize)
517 struct lustre_index_backup_unit *libu, *pos;
521 if (dev->dd_rdonly || *guard)
528 INIT_LIST_HEAD(&libu->libu_link);
529 libu->libu_keysize = keysize;
530 libu->libu_recsize = recsize;
531 libu->libu_fid = *fid;
534 if (unlikely(*guard)) {
541 list_for_each_entry_reverse(pos, head, libu_link) {
542 rc = lu_fid_cmp(&pos->libu_fid, fid);
544 list_add(&libu->libu_link, &pos->libu_link);
551 /* Registered already. But the former registered one
552 * has different keysize/recsize. It may because that
553 * the former values are from disk and corrupted, then
554 * replace it with new values. */
555 if (unlikely(keysize != pos->libu_keysize ||
556 recsize != pos->libu_recsize)) {
557 CWARN("%s: the index "DFID" has registered "
558 "with %u/%u, may be invalid, replace "
560 devname, PFID(fid), pos->libu_keysize,
561 pos->libu_recsize, keysize, recsize);
563 pos->libu_keysize = keysize;
564 pos->libu_recsize = recsize;
576 list_add(&libu->libu_link, head);
581 EXPORT_SYMBOL(lustre_index_register);
583 static void lustre_index_degister(struct list_head *head, spinlock_t *lock,
584 const struct lu_fid *fid)
586 struct lustre_index_backup_unit *libu;
590 list_for_each_entry_reverse(libu, head, libu_link) {
591 rc = lu_fid_cmp(&libu->libu_fid, fid);
592 /* NOT registered. */
597 list_del(&libu->libu_link);
608 lustre_index_backup_make_header(struct lustre_index_backup_header *header,
609 __u32 keysize, __u32 recsize,
610 const struct lu_fid *fid, __u32 count)
612 memset(header, 0, sizeof(*header));
613 header->libh_magic = cpu_to_le32(INDEX_BACKUP_MAGIC_V1);
614 header->libh_count = cpu_to_le32(count);
615 header->libh_keysize = cpu_to_le32(keysize);
616 header->libh_recsize = cpu_to_le32(recsize);
617 fid_cpu_to_le(&header->libh_owner, fid);
620 static int lustre_index_backup_body(const struct lu_env *env,
621 struct dt_object *obj, loff_t *pos,
622 void *buf, int bufsize)
624 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
626 struct lu_buf lbuf = {
633 th = dt_trans_create(env, dev);
637 rc = dt_declare_record_write(env, obj, &lbuf, *pos, th);
641 rc = dt_trans_start_local(env, dev, th);
645 rc = dt_record_write(env, obj, &lbuf, pos, th);
650 dt_trans_stop(env, dev, th);
654 static int lustre_index_backup_header(const struct lu_env *env,
655 struct dt_object *obj,
656 const struct lu_fid *tgt_fid,
657 __u32 keysize, __u32 recsize,
658 void *buf, int bufsize, int count)
660 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
661 struct lustre_index_backup_header *header = buf;
662 struct lu_attr *la = buf;
664 struct lu_buf lbuf = {
666 .lb_len = sizeof(*header)
668 loff_t size = sizeof(*header) + (keysize + recsize) * count;
674 LASSERT(sizeof(*la) <= bufsize);
675 LASSERT(sizeof(*header) <= bufsize);
677 rc = dt_attr_get(env, obj, la);
681 if (la->la_size > size)
684 lustre_index_backup_make_header(header, keysize, recsize,
686 th = dt_trans_create(env, dev);
690 rc = dt_declare_record_write(env, obj, &lbuf, pos, th);
695 rc = dt_declare_punch(env, obj, size, OBD_OBJECT_EOF, th);
700 rc = dt_trans_start_local(env, dev, th);
704 rc = dt_record_write(env, obj, &lbuf, &pos, th);
706 rc = dt_punch(env, obj, size, OBD_OBJECT_EOF, th);
711 dt_trans_stop(env, dev, th);
715 static int lustre_index_update_lma(const struct lu_env *env,
716 struct dt_object *obj,
717 void *buf, int bufsize)
719 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
720 struct lustre_mdt_attrs *lma = buf;
721 struct lu_buf lbuf = {
723 .lb_len = sizeof(struct lustre_ost_attrs)
726 int fl = LU_XATTR_REPLACE;
730 LASSERT(bufsize >= lbuf.lb_len);
732 rc = dt_xattr_get(env, obj, &lbuf, XATTR_NAME_LMA);
733 if (unlikely(rc == -ENODATA)) {
734 fl = LU_XATTR_CREATE;
735 lustre_lma_init(lma, lu_object_fid(&obj->do_lu),
738 } else if (rc < sizeof(*lma)) {
739 RETURN(rc < 0 ? rc : -EFAULT);
741 lustre_lma_swab(lma);
742 if (lma->lma_compat & LMAC_IDX_BACKUP)
745 lma->lma_compat |= LMAC_IDX_BACKUP;
748 lustre_lma_swab(lma);
750 th = dt_trans_create(env, dev);
754 rc = dt_declare_xattr_set(env, obj, &lbuf, XATTR_NAME_LMA, fl, th);
758 rc = dt_trans_start_local(env, dev, th);
762 rc = dt_xattr_set(env, obj, &lbuf, XATTR_NAME_LMA, fl, th);
767 dt_trans_stop(env, dev, th);
771 static int lustre_index_backup_one(const struct lu_env *env,
772 struct local_oid_storage *los,
773 struct dt_object *parent,
774 struct lustre_index_backup_unit *libu,
775 char *buf, int bufsize)
777 struct dt_device *dev = scrub_obj2dev(parent);
778 struct dt_object *tgt_obj = NULL;
779 struct dt_object *bak_obj = NULL;
780 const struct dt_it_ops *iops;
782 loff_t pos = sizeof(struct lustre_index_backup_header);
788 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
789 &libu->libu_fid, NULL));
790 if (IS_ERR_OR_NULL(tgt_obj))
791 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
793 if (!dt_object_exists(tgt_obj))
796 if (!tgt_obj->do_index_ops) {
797 struct dt_index_features feat;
799 feat.dif_flags = DT_IND_UPDATE;
800 feat.dif_keysize_min = libu->libu_keysize;
801 feat.dif_keysize_max = libu->libu_keysize;
802 feat.dif_recsize_min = libu->libu_recsize;
803 feat.dif_recsize_max = libu->libu_recsize;
804 feat.dif_ptrsize = 4;
805 rc = tgt_obj->do_ops->do_index_try(env, tgt_obj, &feat);
810 lustre_fid2lbx(buf, &libu->libu_fid, bufsize);
811 bak_obj = local_file_find_or_create(env, los, parent, buf,
812 S_IFREG | S_IRUGO | S_IWUSR);
813 if (IS_ERR_OR_NULL(bak_obj))
814 GOTO(out, rc = bak_obj ? PTR_ERR(bak_obj) : -ENOENT);
816 iops = &tgt_obj->do_index_ops->dio_it;
817 di = iops->init(env, tgt_obj, 0);
819 GOTO(out, rc = PTR_ERR(di));
821 rc = iops->load(env, di, 0);
823 rc = iops->next(env, di);
831 key = iops->key(env, di);
832 memcpy(&buf[size], key, libu->libu_keysize);
833 size += libu->libu_keysize;
835 rc = iops->rec(env, di, rec, 0);
839 size += libu->libu_recsize;
841 if (size + libu->libu_keysize + libu->libu_recsize > bufsize) {
842 rc = lustre_index_backup_body(env, bak_obj, &pos,
850 rc = iops->next(env, di);
853 if (rc >= 0 && size > 0)
854 rc = lustre_index_backup_body(env, bak_obj, &pos, buf, size);
859 rc = lustre_index_backup_header(env, bak_obj, &libu->libu_fid,
860 libu->libu_keysize, libu->libu_recsize,
861 buf, bufsize, count);
863 rc = lustre_index_update_lma(env, tgt_obj, buf, bufsize);
865 if (!rc && OBD_FAIL_CHECK(OBD_FAIL_OSD_INDEX_CRASH)) {
866 LASSERT(bufsize >= 512);
870 lustre_index_backup_body(env, tgt_obj, &pos, buf, 512);
878 if (!IS_ERR_OR_NULL(tgt_obj))
879 dt_object_put_nocache(env, tgt_obj);
880 if (!IS_ERR_OR_NULL(bak_obj))
881 dt_object_put_nocache(env, bak_obj);
885 void lustre_index_backup(const struct lu_env *env, struct dt_device *dev,
886 const char *devname, struct list_head *head,
887 spinlock_t *lock, int *guard, bool backup)
889 struct lustre_index_backup_unit *libu;
890 struct local_oid_storage *los = NULL;
891 struct dt_object *parent = NULL;
897 if (dev->dd_rdonly || *guard)
904 if (list_empty(head))
907 /* Handle kinds of failures during mount process. */
908 if (!dev->dd_lu_dev.ld_site || !dev->dd_lu_dev.ld_site->ls_top_dev)
912 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
918 lu_local_obj_fid(&fid, INDEX_BACKUP_OID);
919 parent = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
921 if (IS_ERR_OR_NULL(parent)) {
922 CERROR("%s: failed to locate backup dir: rc = %ld\n",
923 devname, parent ? PTR_ERR(parent) : -ENOENT);
928 lu_local_name_obj_fid(&fid, 1);
929 rc = local_oid_storage_init(env, dev, &fid, &los);
931 CERROR("%s: failed to init local storage: rc = %d\n",
939 while (!list_empty(head)) {
940 libu = list_entry(head->next,
941 struct lustre_index_backup_unit, libu_link);
942 list_del_init(&libu->libu_link);
946 rc = lustre_index_backup_one(env, los, parent, libu,
947 buf, INDEX_BACKUP_BUFSIZE);
948 CDEBUG(D_WARNING, "%s: backup index "DFID": rc = %d\n",
949 devname, PFID(&libu->libu_fid), rc);
958 local_oid_storage_fini(env, los);
960 dt_object_put_nocache(env, parent);
962 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
966 EXPORT_SYMBOL(lustre_index_backup);
968 int lustre_index_restore(const struct lu_env *env, struct dt_device *dev,
969 const struct lu_fid *parent_fid,
970 const struct lu_fid *tgt_fid,
971 const struct lu_fid *bak_fid, const char *name,
972 struct list_head *head, spinlock_t *lock,
973 char *buf, int bufsize)
975 struct dt_object *parent_obj = NULL;
976 struct dt_object *tgt_obj = NULL;
977 struct dt_object *bak_obj = NULL;
978 struct lustre_index_backup_header *header;
979 struct dt_index_features *feat;
980 struct dt_object_format *dof;
983 struct lu_object_conf conf;
984 struct dt_insert_rec ent;
993 bool registered = false;
996 LASSERT(bufsize >= sizeof(*la) + sizeof(*dof) +
997 sizeof(*feat) + sizeof(*header));
999 memset(buf, 0, bufsize);
1000 la = (struct lu_attr *)buf;
1001 dof = (void *)la + sizeof(*la);
1002 feat = (void *)dof + sizeof(*dof);
1003 header = (void *)feat + sizeof(*feat);
1004 lbuf.lb_buf = header;
1005 lbuf.lb_len = sizeof(*header);
1007 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1009 if (IS_ERR_OR_NULL(tgt_obj))
1010 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
1012 bak_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1014 if (IS_ERR_OR_NULL(bak_obj))
1015 GOTO(out, rc = bak_obj ? PTR_ERR(bak_obj) : -ENOENT);
1017 if (!dt_object_exists(bak_obj))
1018 GOTO(out, rc = -ENOENT);
1020 parent_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1022 if (IS_ERR_OR_NULL(parent_obj))
1023 GOTO(out, rc = parent_obj ? PTR_ERR(parent_obj) : -ENOENT);
1025 LASSERT(dt_object_exists(parent_obj));
1027 if (unlikely(!dt_try_as_dir(env, parent_obj)))
1028 GOTO(out, rc = -ENOTDIR);
1030 rc = dt_attr_get(env, tgt_obj, la);
1034 rc = dt_record_read(env, bak_obj, &lbuf, &pos);
1038 if (le32_to_cpu(header->libh_magic) != INDEX_BACKUP_MAGIC_V1)
1039 GOTO(out, rc = -EINVAL);
1041 fid_le_to_cpu(&tfid, &header->libh_owner);
1042 if (unlikely(!lu_fid_eq(tgt_fid, &tfid)))
1043 GOTO(out, rc = -EINVAL);
1045 keysize = le32_to_cpu(header->libh_keysize);
1046 recsize = le32_to_cpu(header->libh_recsize);
1047 pairsize = keysize + recsize;
1049 memset(feat, 0, sizeof(*feat));
1050 feat->dif_flags = DT_IND_UPDATE;
1051 feat->dif_keysize_min = feat->dif_keysize_max = keysize;
1052 feat->dif_recsize_min = feat->dif_recsize_max = recsize;
1053 feat->dif_ptrsize = 4;
1055 /* T1: remove old name entry and destroy old index. */
1056 th = dt_trans_create(env, dev);
1058 GOTO(out, rc = PTR_ERR(th));
1060 rc = dt_declare_delete(env, parent_obj,
1061 (const struct dt_key *)name, th);
1065 rc = dt_declare_destroy(env, tgt_obj, th);
1069 rc = dt_trans_start_local(env, dev, th);
1073 rc = dt_delete(env, parent_obj, (const struct dt_key *)name, th);
1077 dt_write_lock(env, tgt_obj, 0);
1078 rc = dt_destroy(env, tgt_obj, th);
1079 dt_write_unlock(env, tgt_obj);
1080 dt_trans_stop(env, dev, th);
1084 la->la_valid = LA_MODE | LA_UID | LA_GID;
1085 conf.loc_flags = LOC_F_NEW;
1086 dof->u.dof_idx.di_feat = feat;
1087 dof->dof_type = DFT_INDEX;
1088 ent.rec_type = S_IFREG;
1089 ent.rec_fid = tgt_fid;
1091 /* Drop cache before re-create it. */
1092 dt_object_put_nocache(env, tgt_obj);
1093 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1095 if (IS_ERR_OR_NULL(tgt_obj))
1096 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
1098 LASSERT(!dt_object_exists(tgt_obj));
1100 /* T2: create new index and insert new name entry. */
1101 th = dt_trans_create(env, dev);
1103 GOTO(out, rc = PTR_ERR(th));
1105 rc = dt_declare_create(env, tgt_obj, la, NULL, dof, th);
1109 rc = dt_declare_insert(env, parent_obj, (const struct dt_rec *)&ent,
1110 (const struct dt_key *)name, th);
1114 rc = dt_trans_start_local(env, dev, th);
1118 dt_write_lock(env, tgt_obj, 0);
1119 rc = dt_create(env, tgt_obj, la, NULL, dof, th);
1120 dt_write_unlock(env, tgt_obj);
1124 rc = dt_insert(env, parent_obj, (const struct dt_rec *)&ent,
1125 (const struct dt_key *)name, th);
1126 dt_trans_stop(env, dev, th);
1127 /* Some index name may has been inserted by OSD
1128 * automatically when create the index object. */
1129 if (unlikely(rc == -EEXIST))
1134 /* The new index will register via index_try. */
1135 rc = tgt_obj->do_ops->do_index_try(env, tgt_obj, feat);
1140 count = le32_to_cpu(header->libh_count);
1141 while (!rc && count > 0) {
1142 int size = pairsize * count;
1146 if (size > bufsize) {
1147 items = bufsize / pairsize;
1148 size = pairsize * items;
1153 rc = dt_record_read(env, bak_obj, &lbuf, &pos);
1154 for (i = 0; i < items && !rc; i++) {
1155 void *key = &buf[i * pairsize];
1156 void *rec = &buf[i * pairsize + keysize];
1158 /* Tn: restore the records. */
1159 th = dt_trans_create(env, dev);
1161 GOTO(out, rc = -ENOMEM);
1163 rc = dt_declare_insert(env, tgt_obj, rec, key, th);
1167 rc = dt_trans_start_local(env, dev, th);
1171 rc = dt_insert(env, tgt_obj, rec, key, th);
1172 if (unlikely(rc == -EEXIST))
1175 dt_trans_stop(env, dev, th);
1184 dt_trans_stop(env, dev, th);
1185 if (rc && registered)
1186 /* Degister the index to avoid overwriting the backup. */
1187 lustre_index_degister(head, lock, tgt_fid);
1190 if (!IS_ERR_OR_NULL(tgt_obj))
1191 dt_object_put_nocache(env, tgt_obj);
1192 if (!IS_ERR_OR_NULL(bak_obj))
1193 dt_object_put_nocache(env, bak_obj);
1194 if (!IS_ERR_OR_NULL(parent_obj))
1195 dt_object_put_nocache(env, parent_obj);
1198 EXPORT_SYMBOL(lustre_index_restore);