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