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 l_wait_info lwi = { 0 };
279 struct task_struct *task;
284 /* os_lock: sync status between stop and scrub thread */
285 spin_lock(&scrub->os_lock);
286 if (thread_is_running(thread)) {
287 spin_unlock(&scrub->os_lock);
291 if (unlikely(thread_is_stopping(thread))) {
292 spin_unlock(&scrub->os_lock);
293 l_wait_event(thread->t_ctl_waitq,
294 thread_is_stopped(thread),
298 spin_unlock(&scrub->os_lock);
300 if (scrub->os_file.sf_status == SS_COMPLETED) {
301 if (!(flags & SS_SET_FAILOUT))
302 flags |= SS_CLEAR_FAILOUT;
304 if (!(flags & SS_SET_DRYRUN))
305 flags |= SS_CLEAR_DRYRUN;
310 scrub->os_start_flags = flags;
311 thread_set_flags(thread, 0);
312 task = kthread_run(threadfn, data, "OI_scrub");
315 CERROR("%s: cannot start iteration thread: rc = %d\n",
320 l_wait_event(thread->t_ctl_waitq,
321 thread_is_running(thread) || thread_is_stopped(thread),
326 EXPORT_SYMBOL(scrub_start);
328 void scrub_stop(struct lustre_scrub *scrub)
330 struct ptlrpc_thread *thread = &scrub->os_thread;
331 struct l_wait_info lwi = { 0 };
333 /* os_lock: sync status between stop and scrub thread */
334 spin_lock(&scrub->os_lock);
335 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
336 thread_set_flags(thread, SVC_STOPPING);
337 spin_unlock(&scrub->os_lock);
338 wake_up_all(&thread->t_ctl_waitq);
339 l_wait_event(thread->t_ctl_waitq,
340 thread_is_stopped(thread),
342 /* Do not skip the last lock/unlock, which can guarantee that
343 * the caller cannot return until the OI scrub thread exit. */
344 spin_lock(&scrub->os_lock);
346 spin_unlock(&scrub->os_lock);
348 EXPORT_SYMBOL(scrub_stop);
350 const char *scrub_status_names[] = {
361 const char *scrub_flags_names[] = {
369 const char *scrub_param_names[] = {
375 static void scrub_bits_dump(struct seq_file *m, int bits, const char *names[],
381 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
383 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
386 seq_printf(m, "%s%c", names[i],
387 bits != 0 ? ',' : '\n');
392 static void scrub_time_dump(struct seq_file *m, time64_t time,
396 seq_printf(m, "%s: %llu seconds\n", prefix,
397 ktime_get_real_seconds() - time);
399 seq_printf(m, "%s: N/A\n", prefix);
402 static void scrub_pos_dump(struct seq_file *m, __u64 pos, const char *prefix)
405 seq_printf(m, "%s: %llu\n", prefix, pos);
407 seq_printf(m, "%s: N/A\n", prefix);
410 void scrub_dump(struct seq_file *m, struct lustre_scrub *scrub)
412 struct scrub_file *sf = &scrub->os_file;
416 down_read(&scrub->os_rwsem);
417 seq_printf(m, "name: OI_scrub\n"
421 sf->sf_magic, (int)sf->sf_oi_count,
422 scrub_status_names[sf->sf_status]);
424 scrub_bits_dump(m, sf->sf_flags, scrub_flags_names, "flags");
426 scrub_bits_dump(m, sf->sf_param, scrub_param_names, "param");
428 scrub_time_dump(m, sf->sf_time_last_complete,
429 "time_since_last_completed");
431 scrub_time_dump(m, sf->sf_time_latest_start,
432 "time_since_latest_start");
434 scrub_time_dump(m, sf->sf_time_last_checkpoint,
435 "time_since_last_checkpoint");
437 scrub_pos_dump(m, sf->sf_pos_latest_start,
438 "latest_start_position");
440 scrub_pos_dump(m, sf->sf_pos_last_checkpoint,
441 "last_checkpoint_position");
443 scrub_pos_dump(m, sf->sf_pos_first_inconsistent,
444 "first_failure_position");
446 checked = sf->sf_items_checked + scrub->os_new_checked;
447 seq_printf(m, "checked: %llu\n"
453 "success_count: %u\n",
455 sf->sf_param & SP_DRYRUN ? "inconsistent" : "updated",
456 sf->sf_items_updated, sf->sf_items_failed,
457 sf->sf_param & SP_DRYRUN ? "inconsistent" : "updated",
458 sf->sf_items_updated_prior, sf->sf_items_noscrub,
459 sf->sf_items_igif, sf->sf_success_count);
462 if (thread_is_running(&scrub->os_thread)) {
463 s64 new_checked = scrub->os_new_checked;
467 /* Since the time resolution is in seconds for new system
468 * or small devices it ismore likely that duration will be
469 * zero which will lead to inaccurate results.
471 duration = ktime_get_seconds() -
472 scrub->os_time_last_checkpoint;
474 new_checked = div_s64(new_checked, duration);
476 rtime = sf->sf_run_time + duration;
478 speed = div_s64(speed, rtime);
480 seq_printf(m, "run_time: %lld seconds\n"
481 "average_speed: %lld objects/sec\n"
482 "real-time_speed: %lld objects/sec\n"
483 "current_position: %llu\n"
484 "scrub_in_prior: %s\n"
485 "scrub_full_speed: %s\n"
486 "partial_scan: %s\n",
487 rtime, speed, new_checked,
488 scrub->os_pos_current,
489 scrub->os_in_prior ? "yes" : "no",
490 scrub->os_full_speed ? "yes" : "no",
491 scrub->os_partial_scan ? "yes" : "no");
493 if (sf->sf_run_time != 0)
494 speed = div_s64(speed, sf->sf_run_time);
495 seq_printf(m, "run_time: %ld seconds\n"
496 "average_speed: %lld objects/sec\n"
497 "real-time_speed: N/A\n"
498 "current_position: N/A\n",
499 sf->sf_run_time, speed);
502 up_read(&scrub->os_rwsem);
504 EXPORT_SYMBOL(scrub_dump);
506 int lustre_liru_new(struct list_head *head, const struct lu_fid *pfid,
507 const struct lu_fid *cfid, __u64 child,
508 const char *name, int namelen)
510 struct lustre_index_restore_unit *liru;
511 int len = sizeof(*liru) + namelen + 1;
513 OBD_ALLOC(liru, len);
517 INIT_LIST_HEAD(&liru->liru_link);
518 liru->liru_pfid = *pfid;
519 liru->liru_cfid = *cfid;
520 liru->liru_clid = child;
521 liru->liru_len = len;
522 memcpy(liru->liru_name, name, namelen);
523 liru->liru_name[namelen] = 0;
524 list_add_tail(&liru->liru_link, head);
528 EXPORT_SYMBOL(lustre_liru_new);
530 int lustre_index_register(struct dt_device *dev, const char *devname,
531 struct list_head *head, spinlock_t *lock, int *guard,
532 const struct lu_fid *fid,
533 __u32 keysize, __u32 recsize)
535 struct lustre_index_backup_unit *libu, *pos;
539 if (dev->dd_rdonly || *guard)
546 INIT_LIST_HEAD(&libu->libu_link);
547 libu->libu_keysize = keysize;
548 libu->libu_recsize = recsize;
549 libu->libu_fid = *fid;
552 if (unlikely(*guard)) {
559 list_for_each_entry_reverse(pos, head, libu_link) {
560 rc = lu_fid_cmp(&pos->libu_fid, fid);
562 list_add(&libu->libu_link, &pos->libu_link);
569 /* Registered already. But the former registered one
570 * has different keysize/recsize. It may because that
571 * the former values are from disk and corrupted, then
572 * replace it with new values. */
573 if (unlikely(keysize != pos->libu_keysize ||
574 recsize != pos->libu_recsize)) {
575 CWARN("%s: the index "DFID" has registered "
576 "with %u/%u, may be invalid, replace "
578 devname, PFID(fid), pos->libu_keysize,
579 pos->libu_recsize, keysize, recsize);
581 pos->libu_keysize = keysize;
582 pos->libu_recsize = recsize;
594 list_add(&libu->libu_link, head);
599 EXPORT_SYMBOL(lustre_index_register);
601 static void lustre_index_degister(struct list_head *head, spinlock_t *lock,
602 const struct lu_fid *fid)
604 struct lustre_index_backup_unit *libu;
608 list_for_each_entry_reverse(libu, head, libu_link) {
609 rc = lu_fid_cmp(&libu->libu_fid, fid);
610 /* NOT registered. */
615 list_del(&libu->libu_link);
626 lustre_index_backup_make_header(struct lustre_index_backup_header *header,
627 __u32 keysize, __u32 recsize,
628 const struct lu_fid *fid, __u32 count)
630 memset(header, 0, sizeof(*header));
631 header->libh_magic = cpu_to_le32(INDEX_BACKUP_MAGIC_V1);
632 header->libh_count = cpu_to_le32(count);
633 header->libh_keysize = cpu_to_le32(keysize);
634 header->libh_recsize = cpu_to_le32(recsize);
635 fid_cpu_to_le(&header->libh_owner, fid);
638 static int lustre_index_backup_body(const struct lu_env *env,
639 struct dt_object *obj, loff_t *pos,
640 void *buf, int bufsize)
642 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
644 struct lu_buf lbuf = {
651 th = dt_trans_create(env, dev);
655 rc = dt_declare_record_write(env, obj, &lbuf, *pos, th);
659 rc = dt_trans_start_local(env, dev, th);
663 rc = dt_record_write(env, obj, &lbuf, pos, th);
668 dt_trans_stop(env, dev, th);
672 static int lustre_index_backup_header(const struct lu_env *env,
673 struct dt_object *obj,
674 const struct lu_fid *tgt_fid,
675 __u32 keysize, __u32 recsize,
676 void *buf, int bufsize, int count)
678 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
679 struct lustre_index_backup_header *header = buf;
680 struct lu_attr *la = buf;
682 struct lu_buf lbuf = {
684 .lb_len = sizeof(*header)
686 loff_t size = sizeof(*header) + (keysize + recsize) * count;
692 LASSERT(sizeof(*la) <= bufsize);
693 LASSERT(sizeof(*header) <= bufsize);
695 rc = dt_attr_get(env, obj, la);
699 if (la->la_size > size)
702 lustre_index_backup_make_header(header, keysize, recsize,
704 th = dt_trans_create(env, dev);
708 rc = dt_declare_record_write(env, obj, &lbuf, pos, th);
713 rc = dt_declare_punch(env, obj, size, OBD_OBJECT_EOF, th);
718 rc = dt_trans_start_local(env, dev, th);
722 rc = dt_record_write(env, obj, &lbuf, &pos, th);
724 rc = dt_punch(env, obj, size, OBD_OBJECT_EOF, th);
729 dt_trans_stop(env, dev, th);
733 static int lustre_index_update_lma(const struct lu_env *env,
734 struct dt_object *obj,
735 void *buf, int bufsize)
737 struct dt_device *dev = lu2dt_dev(obj->do_lu.lo_dev);
738 struct lustre_mdt_attrs *lma = buf;
739 struct lu_buf lbuf = {
741 .lb_len = sizeof(struct lustre_ost_attrs)
744 int fl = LU_XATTR_REPLACE;
748 LASSERT(bufsize >= lbuf.lb_len);
750 rc = dt_xattr_get(env, obj, &lbuf, XATTR_NAME_LMA);
751 if (unlikely(rc == -ENODATA)) {
752 fl = LU_XATTR_CREATE;
753 lustre_lma_init(lma, lu_object_fid(&obj->do_lu),
756 } else if (rc < sizeof(*lma)) {
757 RETURN(rc < 0 ? rc : -EFAULT);
759 lustre_lma_swab(lma);
760 if (lma->lma_compat & LMAC_IDX_BACKUP)
763 lma->lma_compat |= LMAC_IDX_BACKUP;
766 lustre_lma_swab(lma);
768 th = dt_trans_create(env, dev);
772 rc = dt_declare_xattr_set(env, obj, &lbuf, XATTR_NAME_LMA, fl, th);
776 rc = dt_trans_start_local(env, dev, th);
780 rc = dt_xattr_set(env, obj, &lbuf, XATTR_NAME_LMA, fl, th);
785 dt_trans_stop(env, dev, th);
789 static int lustre_index_backup_one(const struct lu_env *env,
790 struct local_oid_storage *los,
791 struct dt_object *parent,
792 struct lustre_index_backup_unit *libu,
793 char *buf, int bufsize)
795 struct dt_device *dev = scrub_obj2dev(parent);
796 struct dt_object *tgt_obj = NULL;
797 struct dt_object *bak_obj = NULL;
798 const struct dt_it_ops *iops;
800 loff_t pos = sizeof(struct lustre_index_backup_header);
806 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
807 &libu->libu_fid, NULL));
808 if (IS_ERR_OR_NULL(tgt_obj))
809 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
811 if (!dt_object_exists(tgt_obj))
814 if (!tgt_obj->do_index_ops) {
815 struct dt_index_features feat;
817 feat.dif_flags = DT_IND_UPDATE;
818 feat.dif_keysize_min = libu->libu_keysize;
819 feat.dif_keysize_max = libu->libu_keysize;
820 feat.dif_recsize_min = libu->libu_recsize;
821 feat.dif_recsize_max = libu->libu_recsize;
822 feat.dif_ptrsize = 4;
823 rc = tgt_obj->do_ops->do_index_try(env, tgt_obj, &feat);
828 lustre_fid2lbx(buf, &libu->libu_fid, bufsize);
829 bak_obj = local_file_find_or_create(env, los, parent, buf,
830 S_IFREG | S_IRUGO | S_IWUSR);
831 if (IS_ERR_OR_NULL(bak_obj))
832 GOTO(out, rc = bak_obj ? PTR_ERR(bak_obj) : -ENOENT);
834 iops = &tgt_obj->do_index_ops->dio_it;
835 di = iops->init(env, tgt_obj, 0);
837 GOTO(out, rc = PTR_ERR(di));
839 rc = iops->load(env, di, 0);
841 rc = iops->next(env, di);
849 key = iops->key(env, di);
850 memcpy(&buf[size], key, libu->libu_keysize);
851 size += libu->libu_keysize;
853 rc = iops->rec(env, di, rec, 0);
857 size += libu->libu_recsize;
859 if (size + libu->libu_keysize + libu->libu_recsize > bufsize) {
860 rc = lustre_index_backup_body(env, bak_obj, &pos,
868 rc = iops->next(env, di);
871 if (rc >= 0 && size > 0)
872 rc = lustre_index_backup_body(env, bak_obj, &pos, buf, size);
877 rc = lustre_index_backup_header(env, bak_obj, &libu->libu_fid,
878 libu->libu_keysize, libu->libu_recsize,
879 buf, bufsize, count);
881 rc = lustre_index_update_lma(env, tgt_obj, buf, bufsize);
883 if (!rc && OBD_FAIL_CHECK(OBD_FAIL_OSD_INDEX_CRASH)) {
884 LASSERT(bufsize >= 512);
888 lustre_index_backup_body(env, tgt_obj, &pos, buf, 512);
896 if (!IS_ERR_OR_NULL(tgt_obj))
897 dt_object_put_nocache(env, tgt_obj);
898 if (!IS_ERR_OR_NULL(bak_obj))
899 dt_object_put_nocache(env, bak_obj);
903 void lustre_index_backup(const struct lu_env *env, struct dt_device *dev,
904 const char *devname, struct list_head *head,
905 spinlock_t *lock, int *guard, bool backup)
907 struct lustre_index_backup_unit *libu;
908 struct local_oid_storage *los = NULL;
909 struct dt_object *parent = NULL;
915 if (dev->dd_rdonly || *guard)
922 if (list_empty(head))
925 /* Handle kinds of failures during mount process. */
926 if (!dev->dd_lu_dev.ld_site || !dev->dd_lu_dev.ld_site->ls_top_dev)
930 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
936 lu_local_obj_fid(&fid, INDEX_BACKUP_OID);
937 parent = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
939 if (IS_ERR_OR_NULL(parent)) {
940 CERROR("%s: failed to locate backup dir: rc = %ld\n",
941 devname, parent ? PTR_ERR(parent) : -ENOENT);
946 lu_local_name_obj_fid(&fid, 1);
947 rc = local_oid_storage_init(env, dev, &fid, &los);
949 CERROR("%s: failed to init local storage: rc = %d\n",
957 while (!list_empty(head)) {
958 libu = list_entry(head->next,
959 struct lustre_index_backup_unit, libu_link);
960 list_del_init(&libu->libu_link);
964 rc = lustre_index_backup_one(env, los, parent, libu,
965 buf, INDEX_BACKUP_BUFSIZE);
966 CDEBUG(D_WARNING, "%s: backup index "DFID": rc = %d\n",
967 devname, PFID(&libu->libu_fid), rc);
976 local_oid_storage_fini(env, los);
978 dt_object_put_nocache(env, parent);
980 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
984 EXPORT_SYMBOL(lustre_index_backup);
986 int lustre_index_restore(const struct lu_env *env, struct dt_device *dev,
987 const struct lu_fid *parent_fid,
988 const struct lu_fid *tgt_fid,
989 const struct lu_fid *bak_fid, const char *name,
990 struct list_head *head, spinlock_t *lock,
991 char *buf, int bufsize)
993 struct dt_object *parent_obj = NULL;
994 struct dt_object *tgt_obj = NULL;
995 struct dt_object *bak_obj = NULL;
996 struct lustre_index_backup_header *header;
997 struct dt_index_features *feat;
998 struct dt_object_format *dof;
1001 struct lu_object_conf conf;
1002 struct dt_insert_rec ent;
1011 bool registered = false;
1014 LASSERT(bufsize >= sizeof(*la) + sizeof(*dof) +
1015 sizeof(*feat) + sizeof(*header));
1017 memset(buf, 0, bufsize);
1018 la = (struct lu_attr *)buf;
1019 dof = (void *)la + sizeof(*la);
1020 feat = (void *)dof + sizeof(*dof);
1021 header = (void *)feat + sizeof(*feat);
1022 lbuf.lb_buf = header;
1023 lbuf.lb_len = sizeof(*header);
1025 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1027 if (IS_ERR_OR_NULL(tgt_obj))
1028 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
1030 bak_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1032 if (IS_ERR_OR_NULL(bak_obj))
1033 GOTO(out, rc = bak_obj ? PTR_ERR(bak_obj) : -ENOENT);
1035 if (!dt_object_exists(bak_obj))
1036 GOTO(out, rc = -ENOENT);
1038 parent_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1040 if (IS_ERR_OR_NULL(parent_obj))
1041 GOTO(out, rc = parent_obj ? PTR_ERR(parent_obj) : -ENOENT);
1043 LASSERT(dt_object_exists(parent_obj));
1045 if (unlikely(!dt_try_as_dir(env, parent_obj)))
1046 GOTO(out, rc = -ENOTDIR);
1048 rc = dt_attr_get(env, tgt_obj, la);
1052 rc = dt_record_read(env, bak_obj, &lbuf, &pos);
1056 if (le32_to_cpu(header->libh_magic) != INDEX_BACKUP_MAGIC_V1)
1057 GOTO(out, rc = -EINVAL);
1059 fid_le_to_cpu(&tfid, &header->libh_owner);
1060 if (unlikely(!lu_fid_eq(tgt_fid, &tfid)))
1061 GOTO(out, rc = -EINVAL);
1063 keysize = le32_to_cpu(header->libh_keysize);
1064 recsize = le32_to_cpu(header->libh_recsize);
1065 pairsize = keysize + recsize;
1067 memset(feat, 0, sizeof(*feat));
1068 feat->dif_flags = DT_IND_UPDATE;
1069 feat->dif_keysize_min = feat->dif_keysize_max = keysize;
1070 feat->dif_recsize_min = feat->dif_recsize_max = recsize;
1071 feat->dif_ptrsize = 4;
1073 /* T1: remove old name entry and destroy old index. */
1074 th = dt_trans_create(env, dev);
1076 GOTO(out, rc = PTR_ERR(th));
1078 rc = dt_declare_delete(env, parent_obj,
1079 (const struct dt_key *)name, th);
1083 rc = dt_declare_destroy(env, tgt_obj, th);
1087 rc = dt_trans_start_local(env, dev, th);
1091 rc = dt_delete(env, parent_obj, (const struct dt_key *)name, th);
1095 dt_write_lock(env, tgt_obj, 0);
1096 rc = dt_destroy(env, tgt_obj, th);
1097 dt_write_unlock(env, tgt_obj);
1098 dt_trans_stop(env, dev, th);
1102 la->la_valid = LA_MODE | LA_UID | LA_GID;
1103 conf.loc_flags = LOC_F_NEW;
1104 dof->u.dof_idx.di_feat = feat;
1105 dof->dof_type = DFT_INDEX;
1106 ent.rec_type = S_IFREG;
1107 ent.rec_fid = tgt_fid;
1109 /* Drop cache before re-create it. */
1110 dt_object_put_nocache(env, tgt_obj);
1111 tgt_obj = lu2dt(lu_object_find_slice(env, &dev->dd_lu_dev,
1113 if (IS_ERR_OR_NULL(tgt_obj))
1114 GOTO(out, rc = tgt_obj ? PTR_ERR(tgt_obj) : -ENOENT);
1116 LASSERT(!dt_object_exists(tgt_obj));
1118 /* T2: create new index and insert new name entry. */
1119 th = dt_trans_create(env, dev);
1121 GOTO(out, rc = PTR_ERR(th));
1123 rc = dt_declare_create(env, tgt_obj, la, NULL, dof, th);
1127 rc = dt_declare_insert(env, parent_obj, (const struct dt_rec *)&ent,
1128 (const struct dt_key *)name, th);
1132 rc = dt_trans_start_local(env, dev, th);
1136 dt_write_lock(env, tgt_obj, 0);
1137 rc = dt_create(env, tgt_obj, la, NULL, dof, th);
1138 dt_write_unlock(env, tgt_obj);
1142 rc = dt_insert(env, parent_obj, (const struct dt_rec *)&ent,
1143 (const struct dt_key *)name, th);
1144 dt_trans_stop(env, dev, th);
1145 /* Some index name may has been inserted by OSD
1146 * automatically when create the index object. */
1147 if (unlikely(rc == -EEXIST))
1152 /* The new index will register via index_try. */
1153 rc = tgt_obj->do_ops->do_index_try(env, tgt_obj, feat);
1158 count = le32_to_cpu(header->libh_count);
1159 while (!rc && count > 0) {
1160 int size = pairsize * count;
1164 if (size > bufsize) {
1165 items = bufsize / pairsize;
1166 size = pairsize * items;
1171 rc = dt_record_read(env, bak_obj, &lbuf, &pos);
1172 for (i = 0; i < items && !rc; i++) {
1173 void *key = &buf[i * pairsize];
1174 void *rec = &buf[i * pairsize + keysize];
1176 /* Tn: restore the records. */
1177 th = dt_trans_create(env, dev);
1179 GOTO(out, rc = -ENOMEM);
1181 rc = dt_declare_insert(env, tgt_obj, rec, key, th);
1185 rc = dt_trans_start_local(env, dev, th);
1189 rc = dt_insert(env, tgt_obj, rec, key, th);
1190 if (unlikely(rc == -EEXIST))
1193 dt_trans_stop(env, dev, th);
1202 dt_trans_stop(env, dev, th);
1203 if (rc && registered)
1204 /* Degister the index to avoid overwriting the backup. */
1205 lustre_index_degister(head, lock, tgt_fid);
1208 if (!IS_ERR_OR_NULL(tgt_obj))
1209 dt_object_put_nocache(env, tgt_obj);
1210 if (!IS_ERR_OR_NULL(bak_obj))
1211 dt_object_put_nocache(env, bak_obj);
1212 if (!IS_ERR_OR_NULL(parent_obj))
1213 dt_object_put_nocache(env, parent_obj);
1216 EXPORT_SYMBOL(lustre_index_restore);