xref: /linux/fs/bcachefs/sb-clean.c (revision 031fba65fc202abf1f193e321be7a2c274fd88ba)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "btree_update_interior.h"
5 #include "buckets.h"
6 #include "error.h"
7 #include "journal_io.h"
8 #include "replicas.h"
9 #include "sb-clean.h"
10 #include "super-io.h"
11 
12 /*
13  * BCH_SB_FIELD_clean:
14  *
15  * Btree roots, and a few other things, are recovered from the journal after an
16  * unclean shutdown - but after a clean shutdown, to avoid having to read the
17  * journal, we can store them in the superblock.
18  *
19  * bch_sb_field_clean simply contains a list of journal entries, stored exactly
20  * as they would be in the journal:
21  */
22 
23 int bch2_sb_clean_validate_late(struct bch_fs *c, struct bch_sb_field_clean *clean,
24 				int write)
25 {
26 	struct jset_entry *entry;
27 	int ret;
28 
29 	for (entry = clean->start;
30 	     entry < (struct jset_entry *) vstruct_end(&clean->field);
31 	     entry = vstruct_next(entry)) {
32 		ret = bch2_journal_entry_validate(c, NULL, entry,
33 						  le16_to_cpu(c->disk_sb.sb->version),
34 						  BCH_SB_BIG_ENDIAN(c->disk_sb.sb),
35 						  write);
36 		if (ret)
37 			return ret;
38 	}
39 
40 	return 0;
41 }
42 
43 static struct bkey_i *btree_root_find(struct bch_fs *c,
44 				      struct bch_sb_field_clean *clean,
45 				      struct jset *j,
46 				      enum btree_id id, unsigned *level)
47 {
48 	struct bkey_i *k;
49 	struct jset_entry *entry, *start, *end;
50 
51 	if (clean) {
52 		start = clean->start;
53 		end = vstruct_end(&clean->field);
54 	} else {
55 		start = j->start;
56 		end = vstruct_last(j);
57 	}
58 
59 	for (entry = start; entry < end; entry = vstruct_next(entry))
60 		if (entry->type == BCH_JSET_ENTRY_btree_root &&
61 		    entry->btree_id == id)
62 			goto found;
63 
64 	return NULL;
65 found:
66 	if (!entry->u64s)
67 		return ERR_PTR(-EINVAL);
68 
69 	k = entry->start;
70 	*level = entry->level;
71 	return k;
72 }
73 
74 int bch2_verify_superblock_clean(struct bch_fs *c,
75 				 struct bch_sb_field_clean **cleanp,
76 				 struct jset *j)
77 {
78 	unsigned i;
79 	struct bch_sb_field_clean *clean = *cleanp;
80 	struct printbuf buf1 = PRINTBUF;
81 	struct printbuf buf2 = PRINTBUF;
82 	int ret = 0;
83 
84 	if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
85 			"superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
86 			le64_to_cpu(clean->journal_seq),
87 			le64_to_cpu(j->seq))) {
88 		kfree(clean);
89 		*cleanp = NULL;
90 		return 0;
91 	}
92 
93 	for (i = 0; i < BTREE_ID_NR; i++) {
94 		struct bkey_i *k1, *k2;
95 		unsigned l1 = 0, l2 = 0;
96 
97 		k1 = btree_root_find(c, clean, NULL, i, &l1);
98 		k2 = btree_root_find(c, NULL, j, i, &l2);
99 
100 		if (!k1 && !k2)
101 			continue;
102 
103 		printbuf_reset(&buf1);
104 		printbuf_reset(&buf2);
105 
106 		if (k1)
107 			bch2_bkey_val_to_text(&buf1, c, bkey_i_to_s_c(k1));
108 		else
109 			prt_printf(&buf1, "(none)");
110 
111 		if (k2)
112 			bch2_bkey_val_to_text(&buf2, c, bkey_i_to_s_c(k2));
113 		else
114 			prt_printf(&buf2, "(none)");
115 
116 		mustfix_fsck_err_on(!k1 || !k2 ||
117 				    IS_ERR(k1) ||
118 				    IS_ERR(k2) ||
119 				    k1->k.u64s != k2->k.u64s ||
120 				    memcmp(k1, k2, bkey_bytes(&k1->k)) ||
121 				    l1 != l2, c,
122 			"superblock btree root %u doesn't match journal after clean shutdown\n"
123 			"sb:      l=%u %s\n"
124 			"journal: l=%u %s\n", i,
125 			l1, buf1.buf,
126 			l2, buf2.buf);
127 	}
128 fsck_err:
129 	printbuf_exit(&buf2);
130 	printbuf_exit(&buf1);
131 	return ret;
132 }
133 
134 struct bch_sb_field_clean *bch2_read_superblock_clean(struct bch_fs *c)
135 {
136 	struct bch_sb_field_clean *clean, *sb_clean;
137 	int ret;
138 
139 	mutex_lock(&c->sb_lock);
140 	sb_clean = bch2_sb_field_get(c->disk_sb.sb, clean);
141 
142 	if (fsck_err_on(!sb_clean, c,
143 			"superblock marked clean but clean section not present")) {
144 		SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
145 		c->sb.clean = false;
146 		mutex_unlock(&c->sb_lock);
147 		return NULL;
148 	}
149 
150 	clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
151 			GFP_KERNEL);
152 	if (!clean) {
153 		mutex_unlock(&c->sb_lock);
154 		return ERR_PTR(-BCH_ERR_ENOMEM_read_superblock_clean);
155 	}
156 
157 	ret = bch2_sb_clean_validate_late(c, clean, READ);
158 	if (ret) {
159 		mutex_unlock(&c->sb_lock);
160 		return ERR_PTR(ret);
161 	}
162 
163 	mutex_unlock(&c->sb_lock);
164 
165 	return clean;
166 fsck_err:
167 	mutex_unlock(&c->sb_lock);
168 	return ERR_PTR(ret);
169 }
170 
171 static struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
172 {
173 	struct jset_entry *entry = *end;
174 	unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
175 
176 	memset(entry, 0, u64s * sizeof(u64));
177 	/*
178 	 * The u64s field counts from the start of data, ignoring the shared
179 	 * fields.
180 	 */
181 	entry->u64s = cpu_to_le16(u64s - 1);
182 
183 	*end = vstruct_next(*end);
184 	return entry;
185 }
186 
187 void bch2_journal_super_entries_add_common(struct bch_fs *c,
188 					   struct jset_entry **end,
189 					   u64 journal_seq)
190 {
191 	struct bch_dev *ca;
192 	unsigned i, dev;
193 
194 	percpu_down_read(&c->mark_lock);
195 
196 	if (!journal_seq) {
197 		for (i = 0; i < ARRAY_SIZE(c->usage); i++)
198 			bch2_fs_usage_acc_to_base(c, i);
199 	} else {
200 		bch2_fs_usage_acc_to_base(c, journal_seq & JOURNAL_BUF_MASK);
201 	}
202 
203 	{
204 		struct jset_entry_usage *u =
205 			container_of(jset_entry_init(end, sizeof(*u)),
206 				     struct jset_entry_usage, entry);
207 
208 		u->entry.type	= BCH_JSET_ENTRY_usage;
209 		u->entry.btree_id = BCH_FS_USAGE_inodes;
210 		u->v		= cpu_to_le64(c->usage_base->nr_inodes);
211 	}
212 
213 	{
214 		struct jset_entry_usage *u =
215 			container_of(jset_entry_init(end, sizeof(*u)),
216 				     struct jset_entry_usage, entry);
217 
218 		u->entry.type	= BCH_JSET_ENTRY_usage;
219 		u->entry.btree_id = BCH_FS_USAGE_key_version;
220 		u->v		= cpu_to_le64(atomic64_read(&c->key_version));
221 	}
222 
223 	for (i = 0; i < BCH_REPLICAS_MAX; i++) {
224 		struct jset_entry_usage *u =
225 			container_of(jset_entry_init(end, sizeof(*u)),
226 				     struct jset_entry_usage, entry);
227 
228 		u->entry.type	= BCH_JSET_ENTRY_usage;
229 		u->entry.btree_id = BCH_FS_USAGE_reserved;
230 		u->entry.level	= i;
231 		u->v		= cpu_to_le64(c->usage_base->persistent_reserved[i]);
232 	}
233 
234 	for (i = 0; i < c->replicas.nr; i++) {
235 		struct bch_replicas_entry *e =
236 			cpu_replicas_entry(&c->replicas, i);
237 		struct jset_entry_data_usage *u =
238 			container_of(jset_entry_init(end, sizeof(*u) + e->nr_devs),
239 				     struct jset_entry_data_usage, entry);
240 
241 		u->entry.type	= BCH_JSET_ENTRY_data_usage;
242 		u->v		= cpu_to_le64(c->usage_base->replicas[i]);
243 		unsafe_memcpy(&u->r, e, replicas_entry_bytes(e),
244 			      "embedded variable length struct");
245 	}
246 
247 	for_each_member_device(ca, c, dev) {
248 		unsigned b = sizeof(struct jset_entry_dev_usage) +
249 			sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR;
250 		struct jset_entry_dev_usage *u =
251 			container_of(jset_entry_init(end, b),
252 				     struct jset_entry_dev_usage, entry);
253 
254 		u->entry.type = BCH_JSET_ENTRY_dev_usage;
255 		u->dev = cpu_to_le32(dev);
256 		u->buckets_ec		= cpu_to_le64(ca->usage_base->buckets_ec);
257 
258 		for (i = 0; i < BCH_DATA_NR; i++) {
259 			u->d[i].buckets = cpu_to_le64(ca->usage_base->d[i].buckets);
260 			u->d[i].sectors	= cpu_to_le64(ca->usage_base->d[i].sectors);
261 			u->d[i].fragmented = cpu_to_le64(ca->usage_base->d[i].fragmented);
262 		}
263 	}
264 
265 	percpu_up_read(&c->mark_lock);
266 
267 	for (i = 0; i < 2; i++) {
268 		struct jset_entry_clock *clock =
269 			container_of(jset_entry_init(end, sizeof(*clock)),
270 				     struct jset_entry_clock, entry);
271 
272 		clock->entry.type = BCH_JSET_ENTRY_clock;
273 		clock->rw	= i;
274 		clock->time	= cpu_to_le64(atomic64_read(&c->io_clock[i].now));
275 	}
276 }
277 
278 static int bch2_sb_clean_validate(struct bch_sb *sb,
279 				  struct bch_sb_field *f,
280 				  struct printbuf *err)
281 {
282 	struct bch_sb_field_clean *clean = field_to_type(f, clean);
283 
284 	if (vstruct_bytes(&clean->field) < sizeof(*clean)) {
285 		prt_printf(err, "wrong size (got %zu should be %zu)",
286 		       vstruct_bytes(&clean->field), sizeof(*clean));
287 		return -BCH_ERR_invalid_sb_clean;
288 	}
289 
290 	return 0;
291 }
292 
293 static void bch2_sb_clean_to_text(struct printbuf *out, struct bch_sb *sb,
294 				  struct bch_sb_field *f)
295 {
296 	struct bch_sb_field_clean *clean = field_to_type(f, clean);
297 	struct jset_entry *entry;
298 
299 	prt_printf(out, "flags:          %x",	le32_to_cpu(clean->flags));
300 	prt_newline(out);
301 	prt_printf(out, "journal_seq:    %llu",	le64_to_cpu(clean->journal_seq));
302 	prt_newline(out);
303 
304 	for (entry = clean->start;
305 	     entry != vstruct_end(&clean->field);
306 	     entry = vstruct_next(entry)) {
307 		if (entry->type == BCH_JSET_ENTRY_btree_keys &&
308 		    !entry->u64s)
309 			continue;
310 
311 		bch2_journal_entry_to_text(out, NULL, entry);
312 		prt_newline(out);
313 	}
314 }
315 
316 const struct bch_sb_field_ops bch_sb_field_ops_clean = {
317 	.validate	= bch2_sb_clean_validate,
318 	.to_text	= bch2_sb_clean_to_text,
319 };
320 
321 int bch2_fs_mark_dirty(struct bch_fs *c)
322 {
323 	int ret;
324 
325 	/*
326 	 * Unconditionally write superblock, to verify it hasn't changed before
327 	 * we go rw:
328 	 */
329 
330 	mutex_lock(&c->sb_lock);
331 	SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
332 
333 	bch2_sb_maybe_downgrade(c);
334 	c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALWAYS);
335 
336 	ret = bch2_write_super(c);
337 	mutex_unlock(&c->sb_lock);
338 
339 	return ret;
340 }
341 
342 void bch2_fs_mark_clean(struct bch_fs *c)
343 {
344 	struct bch_sb_field_clean *sb_clean;
345 	struct jset_entry *entry;
346 	unsigned u64s;
347 	int ret;
348 
349 	mutex_lock(&c->sb_lock);
350 	if (BCH_SB_CLEAN(c->disk_sb.sb))
351 		goto out;
352 
353 	SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
354 
355 	c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info);
356 	c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_metadata);
357 	c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_extents_above_btree_updates));
358 	c->disk_sb.sb->features[0] &= cpu_to_le64(~(1ULL << BCH_FEATURE_btree_updates_journalled));
359 
360 	u64s = sizeof(*sb_clean) / sizeof(u64) + c->journal.entry_u64s_reserved;
361 
362 	sb_clean = bch2_sb_field_resize(&c->disk_sb, clean, u64s);
363 	if (!sb_clean) {
364 		bch_err(c, "error resizing superblock while setting filesystem clean");
365 		goto out;
366 	}
367 
368 	sb_clean->flags		= 0;
369 	sb_clean->journal_seq	= cpu_to_le64(atomic64_read(&c->journal.seq));
370 
371 	/* Trying to catch outstanding bug: */
372 	BUG_ON(le64_to_cpu(sb_clean->journal_seq) > S64_MAX);
373 
374 	entry = sb_clean->start;
375 	bch2_journal_super_entries_add_common(c, &entry, 0);
376 	entry = bch2_btree_roots_to_journal_entries(c, entry, entry);
377 	BUG_ON((void *) entry > vstruct_end(&sb_clean->field));
378 
379 	memset(entry, 0,
380 	       vstruct_end(&sb_clean->field) - (void *) entry);
381 
382 	/*
383 	 * this should be in the write path, and we should be validating every
384 	 * superblock section:
385 	 */
386 	ret = bch2_sb_clean_validate_late(c, sb_clean, WRITE);
387 	if (ret) {
388 		bch_err(c, "error writing marking filesystem clean: validate error");
389 		goto out;
390 	}
391 
392 	bch2_write_super(c);
393 out:
394 	mutex_unlock(&c->sb_lock);
395 }
396