Whamcloud - gitweb
LU-10193 osd-ldiskfs: backup index object with plain format
[fs/lustre-release.git] / lustre / include / lustre_scrub.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2017, Intel Corporation.
24  */
25 /*
26  * lustre/include/lustre_scrub.h
27  *
28  * Shared definitions and declarations for Lustre OI scrub.
29  *
30  * Author: Fan Yong <fan.yong@intel.com>
31  */
32
33 #ifndef _LUSTRE_SCRUB_H
34 # define _LUSTRE_SCRUB_H
35
36 #include <dt_object.h>
37 #include <lustre_net.h>
38
39 #define OSD_OI_FID_OID_BITS_MAX 10
40 #define OSD_OI_FID_NR_MAX       (1UL << OSD_OI_FID_OID_BITS_MAX)
41 #define SCRUB_OI_BITMAP_SIZE    (OSD_OI_FID_NR_MAX >> 3)
42 #define PFID_STRIPE_IDX_BITS    16
43 #define PFID_STRIPE_COUNT_MASK  ((1 << PFID_STRIPE_IDX_BITS) - 1)
44
45 #define SCRUB_MAGIC_V1                  0x4C5FD252
46 #define SCRUB_CHECKPOINT_INTERVAL       60
47 #define SCRUB_WINDOW_SIZE               1024
48
49 #define HALF_SEC                        msecs_to_jiffies(MSEC_PER_SEC >> 1)
50
51 enum scrub_next_status {
52         /* exit current loop and process next group */
53         SCRUB_NEXT_BREAK        = 1,
54
55         /* skip current object and process next bit */
56         SCRUB_NEXT_CONTINUE     = 2,
57
58         /* exit all the loops */
59         SCRUB_NEXT_EXIT         = 3,
60
61         /* wait for free cache slot */
62         SCRUB_NEXT_WAIT         = 4,
63
64         /* simulate system crash during OI scrub */
65         SCRUB_NEXT_CRASH        = 5,
66
67         /* simulate failure during OI scrub */
68         SCRUB_NEXT_FATAL        = 6,
69
70         /* new created object, no scrub on it */
71         SCRUB_NEXT_NOSCRUB      = 7,
72
73         /* the object has no FID-in-LMA */
74         SCRUB_NEXT_NOLMA        = 8,
75
76         /* for OST-object */
77         SCRUB_NEXT_OSTOBJ       = 9,
78
79         /* old OST-object, no LMA or no FID-on-OST flags in LMA */
80         SCRUB_NEXT_OSTOBJ_OLD   = 10,
81 };
82
83 enum scrub_local_file_flags {
84         SLFF_SCAN_SUBITEMS      = 0x0001,
85         SLFF_HIDE_FID           = 0x0002,
86         SLFF_SHOW_NAME          = 0x0004,
87         SLFF_NO_OI              = 0x0008,
88         SLFF_IDX_IN_FID         = 0x0010,
89 };
90
91 enum scrub_status {
92         /* The scrub file is new created, for new MDT, upgrading from old disk,
93          * or re-creating the scrub file manually. */
94         SS_INIT         = 0,
95
96         /* The scrub is checking/repairing the OI files. */
97         SS_SCANNING     = 1,
98
99         /* The scrub checked/repaired the OI files successfully. */
100         SS_COMPLETED    = 2,
101
102         /* The scrub failed to check/repair the OI files. */
103         SS_FAILED       = 3,
104
105         /* The scrub is stopped manually, the OI files may be inconsistent. */
106         SS_STOPPED      = 4,
107
108         /* The scrub is paused automatically when umount. */
109         SS_PAUSED       = 5,
110
111         /* The scrub crashed during the scanning, should be restarted. */
112         SS_CRASHED      = 6,
113 };
114
115 enum scrub_flags {
116         /* OI files have been recreated, OI mappings should be re-inserted. */
117         SF_RECREATED    = 0x0000000000000001ULL,
118
119         /* OI files are invalid, should be rebuild ASAP */
120         SF_INCONSISTENT = 0x0000000000000002ULL,
121
122         /* OI scrub is triggered automatically. */
123         SF_AUTO         = 0x0000000000000004ULL,
124
125         /* The device is upgraded from 1.8 format. */
126         SF_UPGRADE      = 0x0000000000000008ULL,
127 };
128
129 enum scrub_param {
130         /* Exit when fail. */
131         SP_FAILOUT      = 0x0001,
132
133         /* Check only without repairing. */
134         SP_DRYRUN       = 0x0002,
135 };
136
137 enum scrub_start {
138         /* Set failout flag. */
139         SS_SET_FAILOUT          = 0x00000001,
140
141         /* Clear failout flag. */
142         SS_CLEAR_FAILOUT        = 0x00000002,
143
144         /* Reset scrub start position. */
145         SS_RESET                = 0x00000004,
146
147         /* Trigger full scrub automatically. */
148         SS_AUTO_FULL            = 0x00000008,
149
150         /* Trigger partial scrub automatically. */
151         SS_AUTO_PARTIAL         = 0x00000010,
152
153         /* Set dryrun flag. */
154         SS_SET_DRYRUN           = 0x00000020,
155
156         /* Clear dryrun flag. */
157         SS_CLEAR_DRYRUN         = 0x00000040,
158 };
159
160 enum osd_lf_flags {
161         OLF_SCAN_SUBITEMS       = 0x0001,
162         OLF_HIDE_FID            = 0x0002,
163         OLF_SHOW_NAME           = 0x0004,
164         OLF_NO_OI               = 0x0008,
165         OLF_IDX_IN_FID          = 0x0010,
166         OLF_NOT_BACKUP          = 0x0020,
167 };
168
169 /* There are some overhead to detect OI inconsistency automatically
170  * during normal RPC handling. We do not want to always auto detect
171  * OI inconsistency especailly when OI scrub just done recently.
172  *
173  * The 'auto_scrub' defines the time (united as second) interval to
174  * enable auto detect OI inconsistency since last OI scurb done. */
175 enum auto_scrub {
176         /* Disable auto scrub. */
177         AS_NEVER        = 0,
178
179         /* 1 second is too short interval, it is almost equal to always auto
180          * detect inconsistent OI, usually used for test. */
181         AS_ALWAYS       = 1,
182
183         /* Enable auto detect OI inconsistency one month (60 * 60 * 24 * 30)
184          * after last OI scrub. */
185         AS_DEFAULT      = 2592000LL,
186 };
187
188 struct scrub_file {
189         /* 128-bit uuid for volume. */
190         __u8    sf_uuid[16];
191
192         /* See 'enum scrub_flags'. */
193         __u64   sf_flags;
194
195         /* The scrub magic. */
196         __u32   sf_magic;
197
198         /* See 'enum scrub_status'. */
199         __u16   sf_status;
200
201         /* See 'enum scrub_param'. */
202         __u16   sf_param;
203
204         /* The time for the last OI scrub completed. */
205         __u64   sf_time_last_complete;
206
207         /* The time for the latest OI scrub ran. */
208         __u64   sf_time_latest_start;
209
210         /* The time for the last OI scrub checkpoint. */
211         __u64   sf_time_last_checkpoint;
212
213         /* The position for the latest OI scrub started from. */
214         __u64   sf_pos_latest_start;
215
216         /* The position for the last OI scrub checkpoint. */
217         __u64   sf_pos_last_checkpoint;
218
219         /* The position for the first should be updated object. */
220         __u64   sf_pos_first_inconsistent;
221
222         /* How many objects have been checked. */
223         __u64   sf_items_checked;
224
225         /* How many objects have been updated. */
226         __u64   sf_items_updated;
227
228         /* How many objects failed to be processed. */
229         __u64   sf_items_failed;
230
231         /* How many prior objects have been updated during scanning. */
232         __u64   sf_items_updated_prior;
233
234         /* How many objects marked as LDISKFS_STATE_LUSTRE_NOSCRUB. */
235         __u64   sf_items_noscrub;
236
237         /* How many IGIF objects. */
238         __u64   sf_items_igif;
239
240         /* How long the OI scrub has run. */
241         __u32   sf_run_time;
242
243         /* How many completed OI scrub ran on the device. */
244         __u32   sf_success_count;
245
246         /* How many OI files. */
247         __u16   sf_oi_count;
248
249         /* Keep the flags after scrub reset. See 'enum scrub_internal_flags' */
250         __u16   sf_internal_flags;
251
252         __u32   sf_reserved_1;
253         __u64   sf_reserved_2[16];
254
255         /* Bitmap for OI files recreated case. */
256         __u8    sf_oi_bitmap[SCRUB_OI_BITMAP_SIZE];
257 };
258
259 struct lustre_scrub {
260         /* Object for the scrub file. */
261         struct dt_object       *os_obj;
262
263         struct ptlrpc_thread    os_thread;
264         struct list_head        os_inconsistent_items;
265
266         /* write lock for scrub prep/update/post/checkpoint,
267          * read lock for scrub dump. */
268         struct rw_semaphore     os_rwsem;
269         spinlock_t              os_lock;
270
271         /* Scrub file in memory. */
272         struct scrub_file       os_file;
273
274         /* Buffer for scrub file load/store. */
275         struct scrub_file       os_file_disk;
276
277         const char             *os_name;
278
279         /* The time for last checkpoint, jiffies */
280         cfs_time_t              os_time_last_checkpoint;
281
282         /* The time for next checkpoint, jiffies */
283         cfs_time_t              os_time_next_checkpoint;
284
285         /* How many objects have been checked since last checkpoint. */
286         __u64                   os_new_checked;
287         __u64                   os_pos_current;
288         __u32                   os_start_flags;
289         unsigned int            os_in_prior:1, /* process inconsistent item
290                                                 * found by RPC prior */
291                                 os_waiting:1, /* Waiting for scan window. */
292                                 os_full_speed:1, /* run w/o speed limit */
293                                 os_paused:1, /* The scrub is paused. */
294                                 os_convert_igif:1,
295                                 os_partial_scan:1,
296                                 os_in_join:1,
297                                 os_full_scrub:1;
298 };
299
300 #define INDEX_BACKUP_MAGIC_V1   0x1E41F208
301 #define INDEX_BACKUP_BUFSIZE    (4096 * 4)
302
303 enum lustre_index_backup_policy {
304         /* By default, do not backup the index */
305         LIBP_NONE       = 0,
306
307         /* Backup the dirty index objects when umount */
308         LIBP_AUTO       = 1,
309 };
310
311 struct lustre_index_backup_header {
312         __u32           libh_magic;
313         __u32           libh_count;
314         __u32           libh_keysize;
315         __u32           libh_recsize;
316         struct lu_fid   libh_owner;
317         __u64           libh_pad[60]; /* keep header 512 bytes aligned */
318 };
319
320 struct lustre_index_backup_unit {
321         struct list_head        libu_link;
322         struct lu_fid           libu_fid;
323         __u32                   libu_keysize;
324         __u32                   libu_recsize;
325 };
326
327 struct lustre_index_restore_unit {
328         struct list_head        liru_link;
329         struct lu_fid           liru_pfid;
330         struct lu_fid           liru_cfid;
331         __u64                   liru_clid;
332         int                     liru_len;
333         char                    liru_name[0];
334 };
335
336 void scrub_file_init(struct lustre_scrub *scrub, __u8 *uuid);
337 void scrub_file_reset(struct lustre_scrub *scrub, __u8 *uuid, __u64 flags);
338 int scrub_file_load(const struct lu_env *env, struct lustre_scrub *scrub);
339 int scrub_file_store(const struct lu_env *env, struct lustre_scrub *scrub);
340 int scrub_checkpoint(const struct lu_env *env, struct lustre_scrub *scrub);
341 int scrub_start(int (*threadfn)(void *data), struct lustre_scrub *scrub,
342                 void *data, __u32 flags);
343 void scrub_stop(struct lustre_scrub *scrub);
344 void scrub_dump(struct seq_file *m, struct lustre_scrub *scrub);
345
346 int lustre_liru_new(struct list_head *head, const struct lu_fid *pfid,
347                     const struct lu_fid *cfid, __u64 child,
348                     const char *name, int namelen);
349
350 int lustre_index_register(struct dt_device *dev, const char *devname,
351                           struct list_head *head, spinlock_t *lock, int *guard,
352                           const struct lu_fid *fid,
353                           __u32 keysize, __u32 recsize);
354
355 void lustre_index_backup(const struct lu_env *env, struct dt_device *dev,
356                          const char *devname, struct list_head *head,
357                          spinlock_t *lock, int *guard, bool backup);
358 int lustre_index_restore(const struct lu_env *env, struct dt_device *dev,
359                          const struct lu_fid *parent_fid,
360                          const struct lu_fid *tgt_fid,
361                          const struct lu_fid *bak_fid, const char *name,
362                          struct list_head *head, spinlock_t *lock,
363                          char *buf, int bufsize);
364
365 static inline void lustre_fid2lbx(char *buf, const struct lu_fid *fid, int len)
366 {
367         snprintf(buf, len, DFID_NOBRACE".lbx", PFID(fid));
368 }
369
370 static inline const char *osd_scrub2name(struct lustre_scrub *scrub)
371 {
372         return scrub->os_name;
373 }
374 #endif /* _LUSTRE_SCRUB_H */