Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * bcachefs setup/teardown code, and some metadata io - read a superblock and | |
4 | * figure out what to do with it. | |
5 | * | |
6 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> | |
7 | * Copyright 2012 Google, Inc. | |
8 | */ | |
9 | ||
10 | #include "bcachefs.h" | |
7b3f84ea KO |
11 | #include "alloc_background.h" |
12 | #include "alloc_foreground.h" | |
5b8a9227 | 13 | #include "bkey_sort.h" |
1c6fdbd8 KO |
14 | #include "btree_cache.h" |
15 | #include "btree_gc.h" | |
2ca88e5a | 16 | #include "btree_key_cache.h" |
1c6fdbd8 KO |
17 | #include "btree_update_interior.h" |
18 | #include "btree_io.h" | |
19 | #include "chardev.h" | |
20 | #include "checksum.h" | |
21 | #include "clock.h" | |
22 | #include "compress.h" | |
23 | #include "debug.h" | |
24 | #include "disk_groups.h" | |
cd575ddf | 25 | #include "ec.h" |
1c6fdbd8 KO |
26 | #include "error.h" |
27 | #include "fs.h" | |
28 | #include "fs-io.h" | |
29 | #include "fsck.h" | |
30 | #include "inode.h" | |
31 | #include "io.h" | |
32 | #include "journal.h" | |
33 | #include "journal_reclaim.h" | |
1dd7f9d9 | 34 | #include "journal_seq_blacklist.h" |
1c6fdbd8 KO |
35 | #include "move.h" |
36 | #include "migrate.h" | |
37 | #include "movinggc.h" | |
38 | #include "quota.h" | |
39 | #include "rebalance.h" | |
40 | #include "recovery.h" | |
41 | #include "replicas.h" | |
14b393ee | 42 | #include "subvolume.h" |
1c6fdbd8 KO |
43 | #include "super.h" |
44 | #include "super-io.h" | |
45 | #include "sysfs.h" | |
46 | #include "trace.h" | |
47 | ||
48 | #include <linux/backing-dev.h> | |
49 | #include <linux/blkdev.h> | |
50 | #include <linux/debugfs.h> | |
51 | #include <linux/device.h> | |
52 | #include <linux/idr.h> | |
1c6fdbd8 KO |
53 | #include <linux/module.h> |
54 | #include <linux/percpu.h> | |
55 | #include <linux/random.h> | |
56 | #include <linux/sysfs.h> | |
57 | #include <crypto/hash.h> | |
58 | ||
59 | MODULE_LICENSE("GPL"); | |
60 | MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); | |
61 | ||
62 | #define KTYPE(type) \ | |
63 | static const struct attribute_group type ## _group = { \ | |
64 | .attrs = type ## _files \ | |
65 | }; \ | |
66 | \ | |
67 | static const struct attribute_group *type ## _groups[] = { \ | |
68 | &type ## _group, \ | |
69 | NULL \ | |
70 | }; \ | |
71 | \ | |
72 | static const struct kobj_type type ## _ktype = { \ | |
73 | .release = type ## _release, \ | |
74 | .sysfs_ops = &type ## _sysfs_ops, \ | |
75 | .default_groups = type ## _groups \ | |
76 | } | |
77 | ||
78 | static void bch2_fs_release(struct kobject *); | |
79 | static void bch2_dev_release(struct kobject *); | |
80 | ||
81 | static void bch2_fs_internal_release(struct kobject *k) | |
82 | { | |
83 | } | |
84 | ||
85 | static void bch2_fs_opts_dir_release(struct kobject *k) | |
86 | { | |
87 | } | |
88 | ||
89 | static void bch2_fs_time_stats_release(struct kobject *k) | |
90 | { | |
91 | } | |
92 | ||
93 | KTYPE(bch2_fs); | |
94 | KTYPE(bch2_fs_internal); | |
95 | KTYPE(bch2_fs_opts_dir); | |
96 | KTYPE(bch2_fs_time_stats); | |
97 | KTYPE(bch2_dev); | |
98 | ||
99 | static struct kset *bcachefs_kset; | |
100 | static LIST_HEAD(bch_fs_list); | |
101 | static DEFINE_MUTEX(bch_fs_list_lock); | |
102 | ||
103 | static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait); | |
104 | ||
105 | static void bch2_dev_free(struct bch_dev *); | |
106 | static int bch2_dev_alloc(struct bch_fs *, unsigned); | |
107 | static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); | |
108 | static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); | |
109 | ||
110 | struct bch_fs *bch2_dev_to_fs(dev_t dev) | |
111 | { | |
112 | struct bch_fs *c; | |
113 | struct bch_dev *ca; | |
114 | unsigned i; | |
115 | ||
116 | mutex_lock(&bch_fs_list_lock); | |
117 | rcu_read_lock(); | |
118 | ||
119 | list_for_each_entry(c, &bch_fs_list, list) | |
120 | for_each_member_device_rcu(ca, c, i, NULL) | |
ec4ab9d2 | 121 | if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { |
1c6fdbd8 KO |
122 | closure_get(&c->cl); |
123 | goto found; | |
124 | } | |
125 | c = NULL; | |
126 | found: | |
127 | rcu_read_unlock(); | |
128 | mutex_unlock(&bch_fs_list_lock); | |
129 | ||
130 | return c; | |
131 | } | |
132 | ||
133 | static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) | |
134 | { | |
135 | struct bch_fs *c; | |
136 | ||
137 | lockdep_assert_held(&bch_fs_list_lock); | |
138 | ||
139 | list_for_each_entry(c, &bch_fs_list, list) | |
140 | if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) | |
141 | return c; | |
142 | ||
143 | return NULL; | |
144 | } | |
145 | ||
146 | struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) | |
147 | { | |
148 | struct bch_fs *c; | |
149 | ||
150 | mutex_lock(&bch_fs_list_lock); | |
151 | c = __bch2_uuid_to_fs(uuid); | |
152 | if (c) | |
153 | closure_get(&c->cl); | |
154 | mutex_unlock(&bch_fs_list_lock); | |
155 | ||
156 | return c; | |
157 | } | |
158 | ||
180fb49d KO |
159 | static void bch2_dev_usage_journal_reserve(struct bch_fs *c) |
160 | { | |
161 | struct bch_dev *ca; | |
162 | unsigned i, nr = 0, u64s = | |
4b8f89af KO |
163 | ((sizeof(struct jset_entry_dev_usage) + |
164 | sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) / | |
165 | sizeof(u64); | |
180fb49d KO |
166 | |
167 | rcu_read_lock(); | |
168 | for_each_member_device_rcu(ca, c, i, NULL) | |
169 | nr++; | |
170 | rcu_read_unlock(); | |
171 | ||
172 | bch2_journal_entry_res_resize(&c->journal, | |
173 | &c->dev_usage_journal_res, u64s * nr); | |
174 | } | |
175 | ||
1c6fdbd8 KO |
176 | /* Filesystem RO/RW: */ |
177 | ||
178 | /* | |
179 | * For startup/shutdown of RW stuff, the dependencies are: | |
180 | * | |
181 | * - foreground writes depend on copygc and rebalance (to free up space) | |
182 | * | |
183 | * - copygc and rebalance depend on mark and sweep gc (they actually probably | |
184 | * don't because they either reserve ahead of time or don't block if | |
185 | * allocations fail, but allocations can require mark and sweep gc to run | |
186 | * because of generation number wraparound) | |
187 | * | |
188 | * - all of the above depends on the allocator threads | |
189 | * | |
190 | * - allocator depends on the journal (when it rewrites prios and gens) | |
191 | */ | |
192 | ||
193 | static void __bch2_fs_read_only(struct bch_fs *c) | |
194 | { | |
195 | struct bch_dev *ca; | |
d5f70c1f | 196 | unsigned i, clean_passes = 0; |
1c6fdbd8 KO |
197 | |
198 | bch2_rebalance_stop(c); | |
e6d11615 | 199 | bch2_copygc_stop(c); |
1c6fdbd8 KO |
200 | bch2_gc_thread_stop(c); |
201 | ||
202 | /* | |
203 | * Flush journal before stopping allocators, because flushing journal | |
204 | * blacklist entries involves allocating new btree nodes: | |
205 | */ | |
206 | bch2_journal_flush_all_pins(&c->journal); | |
207 | ||
2340fd9d KO |
208 | /* |
209 | * If the allocator threads didn't all start up, the btree updates to | |
210 | * write out alloc info aren't going to work: | |
211 | */ | |
b935a8a6 | 212 | if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags)) |
2340fd9d | 213 | goto nowrote_alloc; |
b935a8a6 | 214 | |
039fc4c5 | 215 | bch_verbose(c, "flushing journal and stopping allocators"); |
460651ee | 216 | |
039fc4c5 KO |
217 | bch2_journal_flush_all_pins(&c->journal); |
218 | set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); | |
61c8d7c8 | 219 | |
039fc4c5 KO |
220 | do { |
221 | clean_passes++; | |
430735cd | 222 | |
039fc4c5 KO |
223 | if (bch2_journal_flush_all_pins(&c->journal)) |
224 | clean_passes = 0; | |
430735cd KO |
225 | |
226 | /* | |
039fc4c5 KO |
227 | * In flight interior btree updates will generate more journal |
228 | * updates and btree updates (alloc btree): | |
430735cd | 229 | */ |
039fc4c5 KO |
230 | if (bch2_btree_interior_updates_nr_pending(c)) { |
231 | closure_wait_event(&c->btree_interior_update_wait, | |
232 | !bch2_btree_interior_updates_nr_pending(c)); | |
233 | clean_passes = 0; | |
234 | } | |
00b8ccf7 | 235 | flush_work(&c->btree_interior_update_work); |
d5f70c1f | 236 | |
039fc4c5 KO |
237 | if (bch2_journal_flush_all_pins(&c->journal)) |
238 | clean_passes = 0; | |
d5f70c1f | 239 | } while (clean_passes < 2); |
039fc4c5 | 240 | bch_verbose(c, "flushing journal and stopping allocators complete"); |
2340fd9d KO |
241 | |
242 | set_bit(BCH_FS_ALLOC_CLEAN, &c->flags); | |
243 | nowrote_alloc: | |
00b8ccf7 KO |
244 | closure_wait_event(&c->btree_interior_update_wait, |
245 | !bch2_btree_interior_updates_nr_pending(c)); | |
246 | flush_work(&c->btree_interior_update_work); | |
247 | ||
1c6fdbd8 KO |
248 | for_each_member_device(ca, c, i) |
249 | bch2_dev_allocator_stop(ca); | |
250 | ||
b935a8a6 | 251 | clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); |
039fc4c5 | 252 | clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); |
b935a8a6 | 253 | |
1c6fdbd8 KO |
254 | bch2_fs_journal_stop(&c->journal); |
255 | ||
256 | /* | |
257 | * the journal kicks off btree writes via reclaim - wait for in flight | |
258 | * writes after stopping journal: | |
259 | */ | |
a5cd80ea | 260 | bch2_btree_flush_all_writes(c); |
1c6fdbd8 KO |
261 | |
262 | /* | |
263 | * After stopping journal: | |
264 | */ | |
265 | for_each_member_device(ca, c, i) | |
266 | bch2_dev_allocator_remove(c, ca); | |
267 | } | |
268 | ||
269 | static void bch2_writes_disabled(struct percpu_ref *writes) | |
270 | { | |
271 | struct bch_fs *c = container_of(writes, struct bch_fs, writes); | |
272 | ||
273 | set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); | |
274 | wake_up(&bch_read_only_wait); | |
275 | } | |
276 | ||
277 | void bch2_fs_read_only(struct bch_fs *c) | |
278 | { | |
134915f3 | 279 | if (!test_bit(BCH_FS_RW, &c->flags)) { |
9ae28f82 | 280 | bch2_journal_reclaim_stop(&c->journal); |
1c6fdbd8 | 281 | return; |
134915f3 | 282 | } |
1c6fdbd8 KO |
283 | |
284 | BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); | |
285 | ||
286 | /* | |
287 | * Block new foreground-end write operations from starting - any new | |
288 | * writes will return -EROFS: | |
289 | * | |
290 | * (This is really blocking new _allocations_, writes to previously | |
291 | * allocated space can still happen until stopping the allocator in | |
292 | * bch2_dev_allocator_stop()). | |
293 | */ | |
294 | percpu_ref_kill(&c->writes); | |
295 | ||
f516c872 | 296 | cancel_work_sync(&c->ec_stripe_delete_work); |
1c6fdbd8 KO |
297 | |
298 | /* | |
299 | * If we're not doing an emergency shutdown, we want to wait on | |
300 | * outstanding writes to complete so they don't see spurious errors due | |
301 | * to shutting down the allocator: | |
302 | * | |
303 | * If we are doing an emergency shutdown outstanding writes may | |
304 | * hang until we shutdown the allocator so we don't want to wait | |
305 | * on outstanding writes before shutting everything down - but | |
306 | * we do need to wait on them before returning and signalling | |
307 | * that going RO is complete: | |
308 | */ | |
309 | wait_event(bch_read_only_wait, | |
310 | test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) || | |
311 | test_bit(BCH_FS_EMERGENCY_RO, &c->flags)); | |
312 | ||
313 | __bch2_fs_read_only(c); | |
314 | ||
315 | wait_event(bch_read_only_wait, | |
316 | test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); | |
317 | ||
318 | clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); | |
319 | ||
320 | if (!bch2_journal_error(&c->journal) && | |
321 | !test_bit(BCH_FS_ERROR, &c->flags) && | |
3aea4342 | 322 | !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) && |
a0e0bda1 | 323 | test_bit(BCH_FS_STARTED, &c->flags) && |
2340fd9d | 324 | test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) && |
b2930396 KO |
325 | !c->opts.norecovery) { |
326 | bch_verbose(c, "marking filesystem clean"); | |
134915f3 | 327 | bch2_fs_mark_clean(c); |
b2930396 | 328 | } |
1c6fdbd8 | 329 | |
134915f3 | 330 | clear_bit(BCH_FS_RW, &c->flags); |
1c6fdbd8 KO |
331 | } |
332 | ||
333 | static void bch2_fs_read_only_work(struct work_struct *work) | |
334 | { | |
335 | struct bch_fs *c = | |
336 | container_of(work, struct bch_fs, read_only_work); | |
337 | ||
1ada1606 | 338 | down_write(&c->state_lock); |
1c6fdbd8 | 339 | bch2_fs_read_only(c); |
1ada1606 | 340 | up_write(&c->state_lock); |
1c6fdbd8 KO |
341 | } |
342 | ||
343 | static void bch2_fs_read_only_async(struct bch_fs *c) | |
344 | { | |
345 | queue_work(system_long_wq, &c->read_only_work); | |
346 | } | |
347 | ||
348 | bool bch2_fs_emergency_read_only(struct bch_fs *c) | |
349 | { | |
350 | bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags); | |
351 | ||
1c6fdbd8 | 352 | bch2_journal_halt(&c->journal); |
9f115ce9 | 353 | bch2_fs_read_only_async(c); |
1c6fdbd8 KO |
354 | |
355 | wake_up(&bch_read_only_wait); | |
356 | return ret; | |
357 | } | |
358 | ||
134915f3 | 359 | static int bch2_fs_read_write_late(struct bch_fs *c) |
1c6fdbd8 | 360 | { |
134915f3 | 361 | int ret; |
1c6fdbd8 | 362 | |
134915f3 KO |
363 | ret = bch2_gc_thread_start(c); |
364 | if (ret) { | |
365 | bch_err(c, "error starting gc thread"); | |
366 | return ret; | |
367 | } | |
368 | ||
e6d11615 KO |
369 | ret = bch2_copygc_start(c); |
370 | if (ret) { | |
371 | bch_err(c, "error starting copygc thread"); | |
372 | return ret; | |
134915f3 KO |
373 | } |
374 | ||
375 | ret = bch2_rebalance_start(c); | |
376 | if (ret) { | |
377 | bch_err(c, "error starting rebalance thread"); | |
378 | return ret; | |
379 | } | |
380 | ||
97fd13ad KO |
381 | schedule_work(&c->ec_stripe_delete_work); |
382 | ||
134915f3 KO |
383 | return 0; |
384 | } | |
1c6fdbd8 | 385 | |
e731d466 | 386 | static int __bch2_fs_read_write(struct bch_fs *c, bool early) |
134915f3 KO |
387 | { |
388 | struct bch_dev *ca; | |
389 | unsigned i; | |
390 | int ret; | |
391 | ||
aae15aaf KO |
392 | if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) { |
393 | bch_err(c, "cannot go rw, unfixed btree errors"); | |
394 | return -EROFS; | |
395 | } | |
396 | ||
134915f3 KO |
397 | if (test_bit(BCH_FS_RW, &c->flags)) |
398 | return 0; | |
399 | ||
619f5bee KO |
400 | /* |
401 | * nochanges is used for fsck -n mode - we have to allow going rw | |
402 | * during recovery for that to work: | |
403 | */ | |
404 | if (c->opts.norecovery || | |
405 | (c->opts.nochanges && | |
406 | (!early || c->opts.read_only))) | |
407 | return -EROFS; | |
330581f1 | 408 | |
2c944fa1 KO |
409 | bch_info(c, "going read-write"); |
410 | ||
134915f3 KO |
411 | ret = bch2_fs_mark_dirty(c); |
412 | if (ret) | |
413 | goto err; | |
1c6fdbd8 | 414 | |
2340fd9d KO |
415 | clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags); |
416 | ||
1c6fdbd8 KO |
417 | for_each_rw_member(ca, c, i) |
418 | bch2_dev_allocator_add(c, ca); | |
419 | bch2_recalc_capacity(c); | |
420 | ||
134915f3 KO |
421 | for_each_rw_member(ca, c, i) { |
422 | ret = bch2_dev_allocator_start(ca); | |
423 | if (ret) { | |
424 | bch_err(c, "error starting allocator threads"); | |
1c6fdbd8 KO |
425 | percpu_ref_put(&ca->io_ref); |
426 | goto err; | |
427 | } | |
134915f3 | 428 | } |
1c6fdbd8 | 429 | |
134915f3 | 430 | set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); |
1c6fdbd8 | 431 | |
59a74051 KO |
432 | for_each_rw_member(ca, c, i) |
433 | bch2_wake_allocator(ca); | |
434 | ||
134915f3 KO |
435 | if (!early) { |
436 | ret = bch2_fs_read_write_late(c); | |
437 | if (ret) | |
438 | goto err; | |
439 | } | |
1c6fdbd8 | 440 | |
134915f3 KO |
441 | percpu_ref_reinit(&c->writes); |
442 | set_bit(BCH_FS_RW, &c->flags); | |
4932e07e | 443 | set_bit(BCH_FS_WAS_RW, &c->flags); |
134915f3 | 444 | return 0; |
1c6fdbd8 KO |
445 | err: |
446 | __bch2_fs_read_only(c); | |
134915f3 KO |
447 | return ret; |
448 | } | |
449 | ||
450 | int bch2_fs_read_write(struct bch_fs *c) | |
451 | { | |
452 | return __bch2_fs_read_write(c, false); | |
453 | } | |
454 | ||
455 | int bch2_fs_read_write_early(struct bch_fs *c) | |
456 | { | |
457 | lockdep_assert_held(&c->state_lock); | |
458 | ||
134915f3 | 459 | return __bch2_fs_read_write(c, true); |
1c6fdbd8 KO |
460 | } |
461 | ||
462 | /* Filesystem startup/shutdown: */ | |
463 | ||
d5e4dcc2 | 464 | static void __bch2_fs_free(struct bch_fs *c) |
1c6fdbd8 KO |
465 | { |
466 | unsigned i; | |
1a21bf98 | 467 | int cpu; |
1c6fdbd8 KO |
468 | |
469 | for (i = 0; i < BCH_TIME_STAT_NR; i++) | |
470 | bch2_time_stats_exit(&c->times[i]); | |
471 | ||
14b393ee | 472 | bch2_fs_snapshots_exit(c); |
1c6fdbd8 KO |
473 | bch2_fs_quota_exit(c); |
474 | bch2_fs_fsio_exit(c); | |
cd575ddf | 475 | bch2_fs_ec_exit(c); |
1c6fdbd8 KO |
476 | bch2_fs_encryption_exit(c); |
477 | bch2_fs_io_exit(c); | |
c823c339 | 478 | bch2_fs_btree_interior_update_exit(c); |
36e9d698 | 479 | bch2_fs_btree_iter_exit(c); |
2ca88e5a | 480 | bch2_fs_btree_key_cache_exit(&c->btree_key_cache); |
1c6fdbd8 | 481 | bch2_fs_btree_cache_exit(c); |
9620c3ec | 482 | bch2_fs_replicas_exit(c); |
1c6fdbd8 KO |
483 | bch2_fs_journal_exit(&c->journal); |
484 | bch2_io_clock_exit(&c->io_clock[WRITE]); | |
485 | bch2_io_clock_exit(&c->io_clock[READ]); | |
486 | bch2_fs_compress_exit(c); | |
f1d786a0 KO |
487 | bch2_journal_keys_free(&c->journal_keys); |
488 | bch2_journal_entries_free(&c->journal_entries); | |
9166b41d | 489 | percpu_free_rwsem(&c->mark_lock); |
5e82a9a1 | 490 | free_percpu(c->online_reserved); |
1a21bf98 | 491 | |
67e0dd8f | 492 | if (c->btree_paths_bufs) |
1a21bf98 | 493 | for_each_possible_cpu(cpu) |
67e0dd8f | 494 | kfree(per_cpu_ptr(c->btree_paths_bufs, cpu)->path); |
1a21bf98 | 495 | |
67e0dd8f | 496 | free_percpu(c->btree_paths_bufs); |
5663a415 | 497 | free_percpu(c->pcpu); |
35189e09 | 498 | mempool_exit(&c->large_bkey_pool); |
1c6fdbd8 KO |
499 | mempool_exit(&c->btree_bounce_pool); |
500 | bioset_exit(&c->btree_bio); | |
1c6fdbd8 KO |
501 | mempool_exit(&c->fill_iter); |
502 | percpu_ref_exit(&c->writes); | |
1c6fdbd8 | 503 | kfree(rcu_dereference_protected(c->disk_groups, 1)); |
1dd7f9d9 | 504 | kfree(c->journal_seq_blacklist_table); |
b5e8a699 | 505 | kfree(c->unused_inode_hints); |
e6d11615 | 506 | free_heap(&c->copygc_heap); |
1c6fdbd8 | 507 | |
731bdd2e KO |
508 | if (c->io_complete_wq ) |
509 | destroy_workqueue(c->io_complete_wq ); | |
1c6fdbd8 KO |
510 | if (c->copygc_wq) |
511 | destroy_workqueue(c->copygc_wq); | |
9f1833ca KO |
512 | if (c->btree_io_complete_wq) |
513 | destroy_workqueue(c->btree_io_complete_wq); | |
731bdd2e KO |
514 | if (c->btree_update_wq) |
515 | destroy_workqueue(c->btree_update_wq); | |
1c6fdbd8 | 516 | |
9d8022db | 517 | bch2_free_super(&c->disk_sb); |
1c6fdbd8 KO |
518 | kvpfree(c, sizeof(*c)); |
519 | module_put(THIS_MODULE); | |
520 | } | |
521 | ||
522 | static void bch2_fs_release(struct kobject *kobj) | |
523 | { | |
524 | struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); | |
525 | ||
d5e4dcc2 | 526 | __bch2_fs_free(c); |
1c6fdbd8 KO |
527 | } |
528 | ||
d5e4dcc2 | 529 | void __bch2_fs_stop(struct bch_fs *c) |
1c6fdbd8 KO |
530 | { |
531 | struct bch_dev *ca; | |
532 | unsigned i; | |
533 | ||
af1c6871 KO |
534 | bch_verbose(c, "shutting down"); |
535 | ||
1dd7f9d9 KO |
536 | set_bit(BCH_FS_STOPPING, &c->flags); |
537 | ||
1ada1606 | 538 | down_write(&c->state_lock); |
883f1a7c | 539 | bch2_fs_read_only(c); |
1ada1606 | 540 | up_write(&c->state_lock); |
883f1a7c | 541 | |
1c6fdbd8 KO |
542 | for_each_member_device(ca, c, i) |
543 | if (ca->kobj.state_in_sysfs && | |
544 | ca->disk_sb.bdev) | |
545 | sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); | |
546 | ||
547 | if (c->kobj.state_in_sysfs) | |
548 | kobject_del(&c->kobj); | |
549 | ||
550 | bch2_fs_debug_exit(c); | |
551 | bch2_fs_chardev_exit(c); | |
552 | ||
553 | kobject_put(&c->time_stats); | |
554 | kobject_put(&c->opts_dir); | |
555 | kobject_put(&c->internal); | |
556 | ||
1c6fdbd8 KO |
557 | /* btree prefetch might have kicked off reads in the background: */ |
558 | bch2_btree_flush_all_reads(c); | |
559 | ||
560 | for_each_member_device(ca, c, i) | |
561 | cancel_work_sync(&ca->io_error_work); | |
562 | ||
1c6fdbd8 | 563 | cancel_work_sync(&c->read_only_work); |
d5e4dcc2 KO |
564 | } |
565 | ||
566 | void bch2_fs_free(struct bch_fs *c) | |
567 | { | |
568 | unsigned i; | |
569 | ||
570 | mutex_lock(&bch_fs_list_lock); | |
571 | list_del(&c->list); | |
572 | mutex_unlock(&bch_fs_list_lock); | |
573 | ||
574 | closure_sync(&c->cl); | |
575 | closure_debug_destroy(&c->cl); | |
576 | ||
577 | for (i = 0; i < c->sb.nr_devices; i++) { | |
578 | struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); | |
1c6fdbd8 | 579 | |
d5e4dcc2 KO |
580 | if (ca) { |
581 | bch2_free_super(&ca->disk_sb); | |
582 | bch2_dev_free(ca); | |
583 | } | |
584 | } | |
1c6fdbd8 | 585 | |
af1c6871 KO |
586 | bch_verbose(c, "shutdown complete"); |
587 | ||
1c6fdbd8 KO |
588 | kobject_put(&c->kobj); |
589 | } | |
590 | ||
d5e4dcc2 KO |
591 | void bch2_fs_stop(struct bch_fs *c) |
592 | { | |
593 | __bch2_fs_stop(c); | |
594 | bch2_fs_free(c); | |
595 | } | |
596 | ||
e2b60560 | 597 | static int bch2_fs_online(struct bch_fs *c) |
1c6fdbd8 KO |
598 | { |
599 | struct bch_dev *ca; | |
1c6fdbd8 | 600 | unsigned i; |
e2b60560 | 601 | int ret = 0; |
1c6fdbd8 KO |
602 | |
603 | lockdep_assert_held(&bch_fs_list_lock); | |
604 | ||
e2b60560 KO |
605 | if (__bch2_uuid_to_fs(c->sb.uuid)) { |
606 | bch_err(c, "filesystem UUID already open"); | |
607 | return -EINVAL; | |
608 | } | |
1c6fdbd8 KO |
609 | |
610 | ret = bch2_fs_chardev_init(c); | |
e2b60560 KO |
611 | if (ret) { |
612 | bch_err(c, "error creating character device"); | |
613 | return ret; | |
614 | } | |
1c6fdbd8 KO |
615 | |
616 | bch2_fs_debug_init(c); | |
617 | ||
e2b60560 KO |
618 | ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: |
619 | kobject_add(&c->internal, &c->kobj, "internal") ?: | |
620 | kobject_add(&c->opts_dir, &c->kobj, "options") ?: | |
621 | kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: | |
622 | bch2_opts_create_sysfs_files(&c->opts_dir); | |
623 | if (ret) { | |
624 | bch_err(c, "error creating sysfs objects"); | |
625 | return ret; | |
626 | } | |
1c6fdbd8 | 627 | |
1ada1606 | 628 | down_write(&c->state_lock); |
1c6fdbd8 | 629 | |
e2b60560 KO |
630 | for_each_member_device(ca, c, i) { |
631 | ret = bch2_dev_sysfs_online(c, ca); | |
632 | if (ret) { | |
633 | bch_err(c, "error creating sysfs objects"); | |
3a402c8d | 634 | percpu_ref_put(&ca->ref); |
1c6fdbd8 | 635 | goto err; |
3a402c8d | 636 | } |
e2b60560 | 637 | } |
1c6fdbd8 | 638 | |
e2b60560 | 639 | BUG_ON(!list_empty(&c->list)); |
1c6fdbd8 | 640 | list_add(&c->list, &bch_fs_list); |
1c6fdbd8 | 641 | err: |
1ada1606 | 642 | up_write(&c->state_lock); |
e2b60560 | 643 | return ret; |
1c6fdbd8 KO |
644 | } |
645 | ||
646 | static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) | |
647 | { | |
648 | struct bch_sb_field_members *mi; | |
649 | struct bch_fs *c; | |
ecf37a4a | 650 | unsigned i, iter_size; |
7be9ab63 | 651 | int ret = 0; |
1c6fdbd8 KO |
652 | |
653 | pr_verbose_init(opts, ""); | |
654 | ||
655 | c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); | |
7be9ab63 CW |
656 | if (!c) { |
657 | c = ERR_PTR(-ENOMEM); | |
1c6fdbd8 | 658 | goto out; |
7be9ab63 | 659 | } |
1c6fdbd8 KO |
660 | |
661 | __module_get(THIS_MODULE); | |
662 | ||
505b7a4c KO |
663 | closure_init(&c->cl, NULL); |
664 | ||
665 | c->kobj.kset = bcachefs_kset; | |
666 | kobject_init(&c->kobj, &bch2_fs_ktype); | |
667 | kobject_init(&c->internal, &bch2_fs_internal_ktype); | |
668 | kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); | |
669 | kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); | |
670 | ||
1c6fdbd8 KO |
671 | c->minor = -1; |
672 | c->disk_sb.fs_sb = true; | |
673 | ||
1ada1606 | 674 | init_rwsem(&c->state_lock); |
1c6fdbd8 KO |
675 | mutex_init(&c->sb_lock); |
676 | mutex_init(&c->replicas_gc_lock); | |
677 | mutex_init(&c->btree_root_lock); | |
678 | INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); | |
679 | ||
680 | init_rwsem(&c->gc_lock); | |
681 | ||
682 | for (i = 0; i < BCH_TIME_STAT_NR; i++) | |
683 | bch2_time_stats_init(&c->times[i]); | |
684 | ||
e6d11615 | 685 | bch2_fs_copygc_init(c); |
2ca88e5a | 686 | bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); |
b092dadd KO |
687 | bch2_fs_allocator_background_init(c); |
688 | bch2_fs_allocator_foreground_init(c); | |
1c6fdbd8 KO |
689 | bch2_fs_rebalance_init(c); |
690 | bch2_fs_quota_init(c); | |
691 | ||
692 | INIT_LIST_HEAD(&c->list); | |
693 | ||
4d8100da KO |
694 | mutex_init(&c->usage_scratch_lock); |
695 | ||
1c6fdbd8 | 696 | mutex_init(&c->bio_bounce_pages_lock); |
14b393ee | 697 | mutex_init(&c->snapshot_table_lock); |
1c6fdbd8 | 698 | |
1c6fdbd8 | 699 | spin_lock_init(&c->btree_write_error_lock); |
1c6fdbd8 | 700 | |
f1d786a0 | 701 | INIT_LIST_HEAD(&c->journal_entries); |
5b593ee1 | 702 | INIT_LIST_HEAD(&c->journal_iters); |
f1d786a0 | 703 | |
1c6fdbd8 KO |
704 | INIT_LIST_HEAD(&c->fsck_errors); |
705 | mutex_init(&c->fsck_error_lock); | |
706 | ||
703e2a43 KO |
707 | INIT_LIST_HEAD(&c->ec_stripe_head_list); |
708 | mutex_init(&c->ec_stripe_head_lock); | |
709 | ||
710 | INIT_LIST_HEAD(&c->ec_stripe_new_list); | |
711 | mutex_init(&c->ec_stripe_new_lock); | |
712 | ||
8dd6ed94 BH |
713 | INIT_LIST_HEAD(&c->data_progress_list); |
714 | mutex_init(&c->data_progress_lock); | |
715 | ||
cd575ddf KO |
716 | spin_lock_init(&c->ec_stripes_heap_lock); |
717 | ||
1c6fdbd8 KO |
718 | seqcount_init(&c->gc_pos_lock); |
719 | ||
5e82a9a1 KO |
720 | seqcount_init(&c->usage_lock); |
721 | ||
ef1b2092 KO |
722 | sema_init(&c->io_in_flight, 128); |
723 | ||
1c6fdbd8 KO |
724 | c->copy_gc_enabled = 1; |
725 | c->rebalance.enabled = 1; | |
726 | c->promote_whole_extents = true; | |
727 | ||
991ba021 KO |
728 | c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; |
729 | c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; | |
730 | c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; | |
731 | c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; | |
1c6fdbd8 KO |
732 | |
733 | bch2_fs_btree_cache_init_early(&c->btree_cache); | |
734 | ||
fca1223c KO |
735 | mutex_init(&c->sectors_available_lock); |
736 | ||
e2b60560 KO |
737 | ret = percpu_init_rwsem(&c->mark_lock); |
738 | if (ret) | |
73e6ab95 KO |
739 | goto err; |
740 | ||
1c6fdbd8 | 741 | mutex_lock(&c->sb_lock); |
e2b60560 KO |
742 | ret = bch2_sb_to_fs(c, sb); |
743 | mutex_unlock(&c->sb_lock); | |
1c6fdbd8 | 744 | |
e2b60560 | 745 | if (ret) |
1c6fdbd8 | 746 | goto err; |
1c6fdbd8 KO |
747 | |
748 | scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid); | |
749 | ||
2430e72f KO |
750 | /* Compat: */ |
751 | if (sb->version <= bcachefs_metadata_version_inode_v2 && | |
752 | !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) | |
753 | SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); | |
754 | ||
755 | if (sb->version <= bcachefs_metadata_version_inode_v2 && | |
756 | !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) | |
757 | SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); | |
758 | ||
1c6fdbd8 | 759 | c->opts = bch2_opts_default; |
8244f320 KO |
760 | ret = bch2_opts_from_sb(&c->opts, sb); |
761 | if (ret) | |
762 | goto err; | |
763 | ||
1c6fdbd8 KO |
764 | bch2_opts_apply(&c->opts, opts); |
765 | ||
8244f320 | 766 | c->block_bits = ilog2(block_sectors(c)); |
1c6fdbd8 KO |
767 | c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); |
768 | ||
7be9ab63 | 769 | if (bch2_fs_init_fault("fs_alloc")) { |
e2b60560 KO |
770 | bch_err(c, "fs_alloc fault injected"); |
771 | ret = -EFAULT; | |
1c6fdbd8 | 772 | goto err; |
7be9ab63 | 773 | } |
1c6fdbd8 | 774 | |
ae2f17d5 | 775 | iter_size = sizeof(struct sort_iter) + |
1c6fdbd8 | 776 | (btree_blocks(c) + 1) * 2 * |
ae2f17d5 | 777 | sizeof(struct sort_iter_set); |
1c6fdbd8 | 778 | |
b5e8a699 KO |
779 | c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); |
780 | ||
731bdd2e | 781 | if (!(c->btree_update_wq = alloc_workqueue("bcachefs", |
2f33ece9 | 782 | WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || |
9f1833ca | 783 | !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", |
9f2772c4 | 784 | WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || |
2f33ece9 KO |
785 | !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", |
786 | WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || | |
731bdd2e KO |
787 | !(c->io_complete_wq = alloc_workqueue("bcachefs_io", |
788 | WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) || | |
134915f3 KO |
789 | percpu_ref_init(&c->writes, bch2_writes_disabled, |
790 | PERCPU_REF_INIT_DEAD, GFP_KERNEL) || | |
1c6fdbd8 KO |
791 | mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || |
792 | bioset_init(&c->btree_bio, 1, | |
793 | max(offsetof(struct btree_read_bio, bio), | |
794 | offsetof(struct btree_write_bio, wbio.bio)), | |
795 | BIOSET_NEED_BVECS) || | |
5663a415 | 796 | !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || |
5e82a9a1 | 797 | !(c->online_reserved = alloc_percpu(u64)) || |
67e0dd8f | 798 | !(c->btree_paths_bufs = alloc_percpu(struct btree_path_buf)) || |
1c6fdbd8 KO |
799 | mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, |
800 | btree_bytes(c)) || | |
35189e09 | 801 | mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || |
b5e8a699 | 802 | !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, |
e2b60560 | 803 | sizeof(u64), GFP_KERNEL))) { |
7be9ab63 CW |
804 | ret = -ENOMEM; |
805 | goto err; | |
806 | } | |
807 | ||
e2b60560 KO |
808 | ret = bch2_io_clock_init(&c->io_clock[READ]) ?: |
809 | bch2_io_clock_init(&c->io_clock[WRITE]) ?: | |
810 | bch2_fs_journal_init(&c->journal) ?: | |
811 | bch2_fs_replicas_init(c) ?: | |
812 | bch2_fs_btree_cache_init(c) ?: | |
813 | bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: | |
814 | bch2_fs_btree_iter_init(c) ?: | |
815 | bch2_fs_btree_interior_update_init(c) ?: | |
816 | bch2_fs_subvolumes_init(c) ?: | |
817 | bch2_fs_io_init(c) ?: | |
818 | bch2_fs_encryption_init(c) ?: | |
819 | bch2_fs_compress_init(c) ?: | |
820 | bch2_fs_ec_init(c) ?: | |
821 | bch2_fs_fsio_init(c); | |
7be9ab63 | 822 | if (ret) |
1c6fdbd8 KO |
823 | goto err; |
824 | ||
fae1157d KO |
825 | if (c->opts.nochanges) |
826 | set_bit(JOURNAL_NOCHANGES, &c->journal.flags); | |
827 | ||
1c6fdbd8 KO |
828 | mi = bch2_sb_get_members(c->disk_sb.sb); |
829 | for (i = 0; i < c->sb.nr_devices; i++) | |
830 | if (bch2_dev_exists(c->disk_sb.sb, mi, i) && | |
7be9ab63 | 831 | bch2_dev_alloc(c, i)) { |
e2b60560 | 832 | ret = -EEXIST; |
1c6fdbd8 | 833 | goto err; |
7be9ab63 | 834 | } |
1c6fdbd8 | 835 | |
4b8f89af KO |
836 | bch2_journal_entry_res_resize(&c->journal, |
837 | &c->btree_root_journal_res, | |
838 | BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); | |
839 | bch2_dev_usage_journal_reserve(c); | |
840 | bch2_journal_entry_res_resize(&c->journal, | |
841 | &c->clock_journal_res, | |
842 | (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); | |
843 | ||
1c6fdbd8 | 844 | mutex_lock(&bch_fs_list_lock); |
e2b60560 | 845 | ret = bch2_fs_online(c); |
1c6fdbd8 | 846 | mutex_unlock(&bch_fs_list_lock); |
e2b60560 KO |
847 | |
848 | if (ret) | |
1c6fdbd8 | 849 | goto err; |
1c6fdbd8 | 850 | out: |
7be9ab63 | 851 | pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); |
1c6fdbd8 KO |
852 | return c; |
853 | err: | |
854 | bch2_fs_free(c); | |
7be9ab63 | 855 | c = ERR_PTR(ret); |
1c6fdbd8 KO |
856 | goto out; |
857 | } | |
858 | ||
619f5bee KO |
859 | noinline_for_stack |
860 | static void print_mount_opts(struct bch_fs *c) | |
861 | { | |
862 | enum bch_opt_id i; | |
863 | char buf[512]; | |
864 | struct printbuf p = PBUF(buf); | |
865 | bool first = true; | |
866 | ||
867 | strcpy(buf, "(null)"); | |
868 | ||
869 | if (c->opts.read_only) { | |
870 | pr_buf(&p, "ro"); | |
871 | first = false; | |
872 | } | |
873 | ||
874 | for (i = 0; i < bch2_opts_nr; i++) { | |
875 | const struct bch_option *opt = &bch2_opt_table[i]; | |
876 | u64 v = bch2_opt_get_by_id(&c->opts, i); | |
877 | ||
8244f320 | 878 | if (!(opt->flags & OPT_MOUNT)) |
619f5bee KO |
879 | continue; |
880 | ||
881 | if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) | |
882 | continue; | |
883 | ||
884 | if (!first) | |
885 | pr_buf(&p, ","); | |
886 | first = false; | |
887 | bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE); | |
888 | } | |
889 | ||
890 | bch_info(c, "mounted with opts: %s", buf); | |
891 | } | |
892 | ||
893 | int bch2_fs_start(struct bch_fs *c) | |
1c6fdbd8 | 894 | { |
1c6fdbd8 KO |
895 | struct bch_sb_field_members *mi; |
896 | struct bch_dev *ca; | |
a420eea6 | 897 | time64_t now = ktime_get_real_seconds(); |
1c6fdbd8 KO |
898 | unsigned i; |
899 | int ret = -EINVAL; | |
900 | ||
1ada1606 | 901 | down_write(&c->state_lock); |
1c6fdbd8 | 902 | |
134915f3 | 903 | BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); |
1c6fdbd8 KO |
904 | |
905 | mutex_lock(&c->sb_lock); | |
906 | ||
907 | for_each_online_member(ca, c, i) | |
908 | bch2_sb_from_fs(c, ca); | |
909 | ||
910 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
911 | for_each_online_member(ca, c, i) | |
912 | mi->members[ca->dev_idx].last_mount = cpu_to_le64(now); | |
913 | ||
914 | mutex_unlock(&c->sb_lock); | |
915 | ||
916 | for_each_rw_member(ca, c, i) | |
917 | bch2_dev_allocator_add(c, ca); | |
918 | bch2_recalc_capacity(c); | |
919 | ||
920 | ret = BCH_SB_INITIALIZED(c->disk_sb.sb) | |
921 | ? bch2_fs_recovery(c) | |
922 | : bch2_fs_initialize(c); | |
923 | if (ret) | |
924 | goto err; | |
cd575ddf KO |
925 | |
926 | ret = bch2_opts_check_may_set(c); | |
927 | if (ret) | |
928 | goto err; | |
1c6fdbd8 | 929 | |
619f5bee | 930 | ret = -EINVAL; |
e2b60560 KO |
931 | if (bch2_fs_init_fault("fs_start")) { |
932 | bch_err(c, "fs_start fault injected"); | |
1c6fdbd8 | 933 | goto err; |
e2b60560 | 934 | } |
1c6fdbd8 | 935 | |
a9310ab0 KO |
936 | set_bit(BCH_FS_STARTED, &c->flags); |
937 | ||
9f20ed15 KO |
938 | /* |
939 | * Allocator threads don't start filling copygc reserve until after we | |
940 | * set BCH_FS_STARTED - wake them now: | |
2ee47eec KO |
941 | * |
942 | * XXX ugly hack: | |
943 | * Need to set ca->allocator_state here instead of relying on the | |
944 | * allocator threads to do it to avoid racing with the copygc threads | |
945 | * checking it and thinking they have no alloc reserve: | |
9f20ed15 | 946 | */ |
2ee47eec KO |
947 | for_each_online_member(ca, c, i) { |
948 | ca->allocator_state = ALLOCATOR_running; | |
9f20ed15 | 949 | bch2_wake_allocator(ca); |
2ee47eec | 950 | } |
9f20ed15 | 951 | |
619f5bee | 952 | if (c->opts.read_only || c->opts.nochanges) { |
1c6fdbd8 KO |
953 | bch2_fs_read_only(c); |
954 | } else { | |
619f5bee KO |
955 | ret = !test_bit(BCH_FS_RW, &c->flags) |
956 | ? bch2_fs_read_write(c) | |
957 | : bch2_fs_read_write_late(c); | |
958 | if (ret) | |
1c6fdbd8 KO |
959 | goto err; |
960 | } | |
961 | ||
619f5bee KO |
962 | print_mount_opts(c); |
963 | ret = 0; | |
1c6fdbd8 | 964 | out: |
1ada1606 | 965 | up_write(&c->state_lock); |
619f5bee | 966 | return ret; |
1c6fdbd8 KO |
967 | err: |
968 | switch (ret) { | |
969 | case BCH_FSCK_ERRORS_NOT_FIXED: | |
970 | bch_err(c, "filesystem contains errors: please report this to the developers"); | |
971 | pr_cont("mount with -o fix_errors to repair\n"); | |
1c6fdbd8 KO |
972 | break; |
973 | case BCH_FSCK_REPAIR_UNIMPLEMENTED: | |
974 | bch_err(c, "filesystem contains errors: please report this to the developers"); | |
975 | pr_cont("repair unimplemented: inform the developers so that it can be added\n"); | |
1c6fdbd8 KO |
976 | break; |
977 | case BCH_FSCK_REPAIR_IMPOSSIBLE: | |
978 | bch_err(c, "filesystem contains errors, but repair impossible"); | |
1c6fdbd8 KO |
979 | break; |
980 | case BCH_FSCK_UNKNOWN_VERSION: | |
e2b60560 | 981 | bch_err(c, "unknown metadata version"); |
1c6fdbd8 KO |
982 | break; |
983 | case -ENOMEM: | |
e2b60560 | 984 | bch_err(c, "cannot allocate memory"); |
1c6fdbd8 KO |
985 | break; |
986 | case -EIO: | |
e2b60560 | 987 | bch_err(c, "IO error"); |
1c6fdbd8 KO |
988 | break; |
989 | } | |
990 | ||
9516950c KO |
991 | if (ret >= 0) |
992 | ret = -EIO; | |
1c6fdbd8 KO |
993 | goto out; |
994 | } | |
995 | ||
996 | static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) | |
997 | { | |
998 | struct bch_sb_field_members *sb_mi; | |
999 | ||
1000 | sb_mi = bch2_sb_get_members(sb); | |
1001 | if (!sb_mi) | |
1002 | return "Invalid superblock: member info area missing"; | |
1003 | ||
8244f320 | 1004 | if (le16_to_cpu(sb->block_size) != block_sectors(c)) |
1c6fdbd8 KO |
1005 | return "mismatched block size"; |
1006 | ||
1007 | if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) < | |
1008 | BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) | |
1009 | return "new cache bucket size is too small"; | |
1010 | ||
1011 | return NULL; | |
1012 | } | |
1013 | ||
1014 | static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) | |
1015 | { | |
1016 | struct bch_sb *newest = | |
1017 | le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; | |
1018 | struct bch_sb_field_members *mi = bch2_sb_get_members(newest); | |
1019 | ||
1020 | if (!uuid_equal(&fs->uuid, &sb->uuid)) | |
1021 | return "device not a member of filesystem"; | |
1022 | ||
1023 | if (!bch2_dev_exists(newest, mi, sb->dev_idx)) | |
1024 | return "device has been removed"; | |
1025 | ||
1026 | if (fs->block_size != sb->block_size) | |
1027 | return "mismatched block size"; | |
1028 | ||
1029 | return NULL; | |
1030 | } | |
1031 | ||
1032 | /* Device startup/shutdown: */ | |
1033 | ||
1034 | static void bch2_dev_release(struct kobject *kobj) | |
1035 | { | |
1036 | struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); | |
1037 | ||
1038 | kfree(ca); | |
1039 | } | |
1040 | ||
1041 | static void bch2_dev_free(struct bch_dev *ca) | |
1042 | { | |
59a74051 KO |
1043 | bch2_dev_allocator_stop(ca); |
1044 | ||
1c6fdbd8 KO |
1045 | cancel_work_sync(&ca->io_error_work); |
1046 | ||
1047 | if (ca->kobj.state_in_sysfs && | |
1048 | ca->disk_sb.bdev) | |
1049 | sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); | |
1050 | ||
1051 | if (ca->kobj.state_in_sysfs) | |
1052 | kobject_del(&ca->kobj); | |
1053 | ||
1054 | bch2_free_super(&ca->disk_sb); | |
1055 | bch2_dev_journal_exit(ca); | |
1056 | ||
1057 | free_percpu(ca->io_done); | |
1058 | bioset_exit(&ca->replica_set); | |
1059 | bch2_dev_buckets_free(ca); | |
d1170ce5 | 1060 | free_page((unsigned long) ca->sb_read_scratch); |
1c6fdbd8 KO |
1061 | |
1062 | bch2_time_stats_exit(&ca->io_latency[WRITE]); | |
1063 | bch2_time_stats_exit(&ca->io_latency[READ]); | |
1064 | ||
1065 | percpu_ref_exit(&ca->io_ref); | |
1066 | percpu_ref_exit(&ca->ref); | |
1067 | kobject_put(&ca->kobj); | |
1068 | } | |
1069 | ||
1070 | static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) | |
1071 | { | |
1072 | ||
1073 | lockdep_assert_held(&c->state_lock); | |
1074 | ||
1075 | if (percpu_ref_is_zero(&ca->io_ref)) | |
1076 | return; | |
1077 | ||
1078 | __bch2_dev_read_only(c, ca); | |
1079 | ||
1080 | reinit_completion(&ca->io_ref_completion); | |
1081 | percpu_ref_kill(&ca->io_ref); | |
1082 | wait_for_completion(&ca->io_ref_completion); | |
1083 | ||
1084 | if (ca->kobj.state_in_sysfs) { | |
1085 | sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); | |
1086 | sysfs_remove_link(&ca->kobj, "block"); | |
1087 | } | |
1088 | ||
1089 | bch2_free_super(&ca->disk_sb); | |
1090 | bch2_dev_journal_exit(ca); | |
1091 | } | |
1092 | ||
1093 | static void bch2_dev_ref_complete(struct percpu_ref *ref) | |
1094 | { | |
1095 | struct bch_dev *ca = container_of(ref, struct bch_dev, ref); | |
1096 | ||
1097 | complete(&ca->ref_completion); | |
1098 | } | |
1099 | ||
1100 | static void bch2_dev_io_ref_complete(struct percpu_ref *ref) | |
1101 | { | |
1102 | struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); | |
1103 | ||
1104 | complete(&ca->io_ref_completion); | |
1105 | } | |
1106 | ||
1107 | static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) | |
1108 | { | |
1109 | int ret; | |
1110 | ||
1111 | if (!c->kobj.state_in_sysfs) | |
1112 | return 0; | |
1113 | ||
1114 | if (!ca->kobj.state_in_sysfs) { | |
1115 | ret = kobject_add(&ca->kobj, &c->kobj, | |
1116 | "dev-%u", ca->dev_idx); | |
1117 | if (ret) | |
1118 | return ret; | |
1119 | } | |
1120 | ||
1121 | if (ca->disk_sb.bdev) { | |
1122 | struct kobject *block = bdev_kobj(ca->disk_sb.bdev); | |
1123 | ||
1124 | ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); | |
1125 | if (ret) | |
1126 | return ret; | |
1127 | ||
1128 | ret = sysfs_create_link(&ca->kobj, block, "block"); | |
1129 | if (ret) | |
1130 | return ret; | |
1131 | } | |
1132 | ||
1133 | return 0; | |
1134 | } | |
1135 | ||
1136 | static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, | |
1137 | struct bch_member *member) | |
1138 | { | |
1139 | struct bch_dev *ca; | |
1140 | ||
1141 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
1142 | if (!ca) | |
1143 | return NULL; | |
1144 | ||
1145 | kobject_init(&ca->kobj, &bch2_dev_ktype); | |
1146 | init_completion(&ca->ref_completion); | |
1147 | init_completion(&ca->io_ref_completion); | |
1148 | ||
1149 | init_rwsem(&ca->bucket_lock); | |
1150 | ||
1c6fdbd8 KO |
1151 | INIT_WORK(&ca->io_error_work, bch2_io_error_work); |
1152 | ||
1153 | bch2_time_stats_init(&ca->io_latency[READ]); | |
1154 | bch2_time_stats_init(&ca->io_latency[WRITE]); | |
1155 | ||
1156 | ca->mi = bch2_mi_to_cpu(member); | |
1157 | ca->uuid = member->uuid; | |
1158 | ||
1159 | if (opt_defined(c->opts, discard)) | |
1160 | ca->mi.discard = opt_get(c->opts, discard); | |
1161 | ||
1162 | if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, | |
1163 | 0, GFP_KERNEL) || | |
1164 | percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, | |
1165 | PERCPU_REF_INIT_DEAD, GFP_KERNEL) || | |
d1170ce5 | 1166 | !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || |
1c6fdbd8 KO |
1167 | bch2_dev_buckets_alloc(c, ca) || |
1168 | bioset_init(&ca->replica_set, 4, | |
1169 | offsetof(struct bch_write_bio, bio), 0) || | |
1170 | !(ca->io_done = alloc_percpu(*ca->io_done))) | |
1171 | goto err; | |
1172 | ||
1173 | return ca; | |
1174 | err: | |
1175 | bch2_dev_free(ca); | |
1176 | return NULL; | |
1177 | } | |
1178 | ||
1179 | static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, | |
1180 | unsigned dev_idx) | |
1181 | { | |
1182 | ca->dev_idx = dev_idx; | |
1183 | __set_bit(ca->dev_idx, ca->self.d); | |
1184 | scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); | |
1185 | ||
1186 | ca->fs = c; | |
1187 | rcu_assign_pointer(c->devs[ca->dev_idx], ca); | |
1188 | ||
1189 | if (bch2_dev_sysfs_online(c, ca)) | |
1190 | pr_warn("error creating sysfs objects"); | |
1191 | } | |
1192 | ||
1193 | static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) | |
1194 | { | |
1195 | struct bch_member *member = | |
1196 | bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx; | |
1197 | struct bch_dev *ca = NULL; | |
1198 | int ret = 0; | |
1199 | ||
1200 | pr_verbose_init(c->opts, ""); | |
1201 | ||
1202 | if (bch2_fs_init_fault("dev_alloc")) | |
1203 | goto err; | |
1204 | ||
1205 | ca = __bch2_dev_alloc(c, member); | |
1206 | if (!ca) | |
1207 | goto err; | |
1208 | ||
220d2062 KO |
1209 | ca->fs = c; |
1210 | ||
2436cb9f | 1211 | if (ca->mi.state == BCH_MEMBER_STATE_rw && |
59a74051 KO |
1212 | bch2_dev_allocator_start(ca)) { |
1213 | bch2_dev_free(ca); | |
1214 | goto err; | |
1215 | } | |
1216 | ||
1c6fdbd8 KO |
1217 | bch2_dev_attach(c, ca, dev_idx); |
1218 | out: | |
1219 | pr_verbose_init(c->opts, "ret %i", ret); | |
1220 | return ret; | |
1221 | err: | |
1222 | if (ca) | |
1223 | bch2_dev_free(ca); | |
1224 | ret = -ENOMEM; | |
1225 | goto out; | |
1226 | } | |
1227 | ||
1228 | static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) | |
1229 | { | |
1230 | unsigned ret; | |
1231 | ||
1232 | if (bch2_dev_is_online(ca)) { | |
1233 | bch_err(ca, "already have device online in slot %u", | |
1234 | sb->sb->dev_idx); | |
1235 | return -EINVAL; | |
1236 | } | |
1237 | ||
1238 | if (get_capacity(sb->bdev->bd_disk) < | |
1239 | ca->mi.bucket_size * ca->mi.nbuckets) { | |
1240 | bch_err(ca, "cannot online: device too small"); | |
1241 | return -EINVAL; | |
1242 | } | |
1243 | ||
1244 | BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); | |
1245 | ||
1246 | if (get_capacity(sb->bdev->bd_disk) < | |
1247 | ca->mi.bucket_size * ca->mi.nbuckets) { | |
1248 | bch_err(ca, "device too small"); | |
1249 | return -EINVAL; | |
1250 | } | |
1251 | ||
1252 | ret = bch2_dev_journal_init(ca, sb->sb); | |
1253 | if (ret) | |
1254 | return ret; | |
1255 | ||
1256 | /* Commit: */ | |
1257 | ca->disk_sb = *sb; | |
1258 | memset(sb, 0, sizeof(*sb)); | |
1259 | ||
1c6fdbd8 KO |
1260 | percpu_ref_reinit(&ca->io_ref); |
1261 | ||
1262 | return 0; | |
1263 | } | |
1264 | ||
1265 | static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) | |
1266 | { | |
1267 | struct bch_dev *ca; | |
1268 | int ret; | |
1269 | ||
1270 | lockdep_assert_held(&c->state_lock); | |
1271 | ||
1272 | if (le64_to_cpu(sb->sb->seq) > | |
1273 | le64_to_cpu(c->disk_sb.sb->seq)) | |
1274 | bch2_sb_to_fs(c, sb->sb); | |
1275 | ||
1276 | BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || | |
1277 | !c->devs[sb->sb->dev_idx]); | |
1278 | ||
1279 | ca = bch_dev_locked(c, sb->sb->dev_idx); | |
1280 | ||
1281 | ret = __bch2_dev_attach_bdev(ca, sb); | |
1282 | if (ret) | |
1283 | return ret; | |
1284 | ||
1285 | bch2_dev_sysfs_online(c, ca); | |
1286 | ||
1287 | if (c->sb.nr_devices == 1) | |
1288 | snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); | |
1289 | snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); | |
1290 | ||
1291 | rebalance_wakeup(c); | |
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | /* Device management: */ | |
1296 | ||
1297 | /* | |
1298 | * Note: this function is also used by the error paths - when a particular | |
1299 | * device sees an error, we call it to determine whether we can just set the | |
1300 | * device RO, or - if this function returns false - we'll set the whole | |
1301 | * filesystem RO: | |
1302 | * | |
1303 | * XXX: maybe we should be more explicit about whether we're changing state | |
1304 | * because we got an error or what have you? | |
1305 | */ | |
1306 | bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, | |
1307 | enum bch_member_state new_state, int flags) | |
1308 | { | |
1309 | struct bch_devs_mask new_online_devs; | |
1c6fdbd8 KO |
1310 | struct bch_dev *ca2; |
1311 | int i, nr_rw = 0, required; | |
1312 | ||
1313 | lockdep_assert_held(&c->state_lock); | |
1314 | ||
1315 | switch (new_state) { | |
2436cb9f | 1316 | case BCH_MEMBER_STATE_rw: |
1c6fdbd8 | 1317 | return true; |
2436cb9f KO |
1318 | case BCH_MEMBER_STATE_ro: |
1319 | if (ca->mi.state != BCH_MEMBER_STATE_rw) | |
1c6fdbd8 KO |
1320 | return true; |
1321 | ||
1322 | /* do we have enough devices to write to? */ | |
1323 | for_each_member_device(ca2, c, i) | |
1324 | if (ca2 != ca) | |
2436cb9f | 1325 | nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; |
1c6fdbd8 KO |
1326 | |
1327 | required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) | |
1328 | ? c->opts.metadata_replicas | |
1329 | : c->opts.metadata_replicas_required, | |
1330 | !(flags & BCH_FORCE_IF_DATA_DEGRADED) | |
1331 | ? c->opts.data_replicas | |
1332 | : c->opts.data_replicas_required); | |
1333 | ||
1334 | return nr_rw >= required; | |
2436cb9f KO |
1335 | case BCH_MEMBER_STATE_failed: |
1336 | case BCH_MEMBER_STATE_spare: | |
1337 | if (ca->mi.state != BCH_MEMBER_STATE_rw && | |
1338 | ca->mi.state != BCH_MEMBER_STATE_ro) | |
1c6fdbd8 KO |
1339 | return true; |
1340 | ||
1341 | /* do we have enough devices to read from? */ | |
1342 | new_online_devs = bch2_online_devs(c); | |
1343 | __clear_bit(ca->dev_idx, new_online_devs.d); | |
1344 | ||
fcb3431b | 1345 | return bch2_have_enough_devs(c, new_online_devs, flags, false); |
1c6fdbd8 KO |
1346 | default: |
1347 | BUG(); | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | static bool bch2_fs_may_start(struct bch_fs *c) | |
1352 | { | |
1c6fdbd8 KO |
1353 | struct bch_sb_field_members *mi; |
1354 | struct bch_dev *ca; | |
fcb3431b KO |
1355 | unsigned i, flags = 0; |
1356 | ||
1357 | if (c->opts.very_degraded) | |
1358 | flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; | |
1c6fdbd8 | 1359 | |
fcb3431b KO |
1360 | if (c->opts.degraded) |
1361 | flags |= BCH_FORCE_IF_DEGRADED; | |
1362 | ||
1363 | if (!c->opts.degraded && | |
1364 | !c->opts.very_degraded) { | |
1c6fdbd8 KO |
1365 | mutex_lock(&c->sb_lock); |
1366 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1367 | ||
1368 | for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { | |
1369 | if (!bch2_dev_exists(c->disk_sb.sb, mi, i)) | |
1370 | continue; | |
1371 | ||
1372 | ca = bch_dev_locked(c, i); | |
1373 | ||
1374 | if (!bch2_dev_is_online(ca) && | |
2436cb9f KO |
1375 | (ca->mi.state == BCH_MEMBER_STATE_rw || |
1376 | ca->mi.state == BCH_MEMBER_STATE_ro)) { | |
1c6fdbd8 KO |
1377 | mutex_unlock(&c->sb_lock); |
1378 | return false; | |
1379 | } | |
1380 | } | |
1381 | mutex_unlock(&c->sb_lock); | |
1382 | } | |
1383 | ||
fcb3431b | 1384 | return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); |
1c6fdbd8 KO |
1385 | } |
1386 | ||
1387 | static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) | |
1388 | { | |
74ed7e56 KO |
1389 | /* |
1390 | * Device going read only means the copygc reserve get smaller, so we | |
1391 | * don't want that happening while copygc is in progress: | |
1392 | */ | |
1393 | bch2_copygc_stop(c); | |
1394 | ||
1c6fdbd8 KO |
1395 | /* |
1396 | * The allocator thread itself allocates btree nodes, so stop it first: | |
1397 | */ | |
1398 | bch2_dev_allocator_stop(ca); | |
1399 | bch2_dev_allocator_remove(c, ca); | |
1400 | bch2_dev_journal_stop(&c->journal, ca); | |
74ed7e56 KO |
1401 | |
1402 | bch2_copygc_start(c); | |
1c6fdbd8 KO |
1403 | } |
1404 | ||
e2b60560 | 1405 | static int __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) |
1c6fdbd8 KO |
1406 | { |
1407 | lockdep_assert_held(&c->state_lock); | |
1408 | ||
2436cb9f | 1409 | BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); |
1c6fdbd8 KO |
1410 | |
1411 | bch2_dev_allocator_add(c, ca); | |
1412 | bch2_recalc_capacity(c); | |
1413 | ||
e2b60560 | 1414 | return bch2_dev_allocator_start(ca); |
1c6fdbd8 KO |
1415 | } |
1416 | ||
1417 | int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, | |
1418 | enum bch_member_state new_state, int flags) | |
1419 | { | |
1420 | struct bch_sb_field_members *mi; | |
1421 | int ret = 0; | |
1422 | ||
1423 | if (ca->mi.state == new_state) | |
1424 | return 0; | |
1425 | ||
1426 | if (!bch2_dev_state_allowed(c, ca, new_state, flags)) | |
1427 | return -EINVAL; | |
1428 | ||
2436cb9f | 1429 | if (new_state != BCH_MEMBER_STATE_rw) |
1c6fdbd8 KO |
1430 | __bch2_dev_read_only(c, ca); |
1431 | ||
2436cb9f | 1432 | bch_notice(ca, "%s", bch2_member_states[new_state]); |
1c6fdbd8 KO |
1433 | |
1434 | mutex_lock(&c->sb_lock); | |
1435 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1436 | SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state); | |
1437 | bch2_write_super(c); | |
1438 | mutex_unlock(&c->sb_lock); | |
1439 | ||
e2b60560 KO |
1440 | if (new_state == BCH_MEMBER_STATE_rw) |
1441 | ret = __bch2_dev_read_write(c, ca); | |
1c6fdbd8 KO |
1442 | |
1443 | rebalance_wakeup(c); | |
1444 | ||
1445 | return ret; | |
1446 | } | |
1447 | ||
1448 | int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, | |
1449 | enum bch_member_state new_state, int flags) | |
1450 | { | |
1451 | int ret; | |
1452 | ||
1ada1606 | 1453 | down_write(&c->state_lock); |
1c6fdbd8 | 1454 | ret = __bch2_dev_set_state(c, ca, new_state, flags); |
1ada1606 | 1455 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1456 | |
1457 | return ret; | |
1458 | } | |
1459 | ||
1460 | /* Device add/removal: */ | |
1461 | ||
c0ebe3e4 | 1462 | static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) |
5d20ba48 KO |
1463 | { |
1464 | struct btree_trans trans; | |
1465 | size_t i; | |
1466 | int ret; | |
1467 | ||
1468 | bch2_trans_init(&trans, c, 0, 0); | |
1469 | ||
1470 | for (i = 0; i < ca->mi.nbuckets; i++) { | |
a9cb0a67 KO |
1471 | ret = lockrestart_do(&trans, |
1472 | bch2_btree_key_cache_flush(&trans, | |
1473 | BTREE_ID_alloc, POS(ca->dev_idx, i))); | |
5d20ba48 KO |
1474 | if (ret) |
1475 | break; | |
1476 | } | |
1477 | bch2_trans_exit(&trans); | |
1478 | ||
a9cb0a67 KO |
1479 | if (ret) { |
1480 | bch_err(c, "error %i removing dev alloc info", ret); | |
5d20ba48 | 1481 | return ret; |
a9cb0a67 | 1482 | } |
5d20ba48 | 1483 | |
41f8b09e | 1484 | return bch2_btree_delete_range(c, BTREE_ID_alloc, |
5d20ba48 KO |
1485 | POS(ca->dev_idx, 0), |
1486 | POS(ca->dev_idx + 1, 0), | |
d248ee56 | 1487 | 0, NULL); |
5d20ba48 KO |
1488 | } |
1489 | ||
1c6fdbd8 KO |
1490 | int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) |
1491 | { | |
1492 | struct bch_sb_field_members *mi; | |
1493 | unsigned dev_idx = ca->dev_idx, data; | |
1494 | int ret = -EINVAL; | |
1495 | ||
1ada1606 | 1496 | down_write(&c->state_lock); |
1c6fdbd8 | 1497 | |
31ba2cd3 KO |
1498 | /* |
1499 | * We consume a reference to ca->ref, regardless of whether we succeed | |
1500 | * or fail: | |
1501 | */ | |
1502 | percpu_ref_put(&ca->ref); | |
1c6fdbd8 | 1503 | |
2436cb9f | 1504 | if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { |
1c6fdbd8 KO |
1505 | bch_err(ca, "Cannot remove without losing data"); |
1506 | goto err; | |
1507 | } | |
1508 | ||
1509 | __bch2_dev_read_only(c, ca); | |
1510 | ||
1c6fdbd8 KO |
1511 | ret = bch2_dev_data_drop(c, ca->dev_idx, flags); |
1512 | if (ret) { | |
1513 | bch_err(ca, "Remove failed: error %i dropping data", ret); | |
1514 | goto err; | |
1515 | } | |
1516 | ||
1517 | ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); | |
1518 | if (ret) { | |
1519 | bch_err(ca, "Remove failed: error %i flushing journal", ret); | |
1520 | goto err; | |
1521 | } | |
1522 | ||
5d20ba48 | 1523 | ret = bch2_dev_remove_alloc(c, ca); |
1c6fdbd8 KO |
1524 | if (ret) { |
1525 | bch_err(ca, "Remove failed, error deleting alloc info"); | |
1526 | goto err; | |
1527 | } | |
1528 | ||
1529 | /* | |
1530 | * must flush all existing journal entries, they might have | |
1531 | * (overwritten) keys that point to the device we're removing: | |
1532 | */ | |
1533 | bch2_journal_flush_all_pins(&c->journal); | |
31ba2cd3 KO |
1534 | /* |
1535 | * hack to ensure bch2_replicas_gc2() clears out entries to this device | |
1536 | */ | |
1537 | bch2_journal_meta(&c->journal); | |
1c6fdbd8 KO |
1538 | ret = bch2_journal_error(&c->journal); |
1539 | if (ret) { | |
1540 | bch_err(ca, "Remove failed, journal error"); | |
1541 | goto err; | |
1542 | } | |
1543 | ||
31ba2cd3 KO |
1544 | ret = bch2_replicas_gc2(c); |
1545 | if (ret) { | |
1546 | bch_err(ca, "Remove failed: error %i from replicas gc", ret); | |
1547 | goto err; | |
1548 | } | |
1549 | ||
1550 | data = bch2_dev_has_data(c, ca); | |
1551 | if (data) { | |
1552 | char data_has_str[100]; | |
1553 | ||
1554 | bch2_flags_to_text(&PBUF(data_has_str), | |
1555 | bch2_data_types, data); | |
1556 | bch_err(ca, "Remove failed, still has data (%s)", data_has_str); | |
1557 | ret = -EBUSY; | |
1558 | goto err; | |
1559 | } | |
1560 | ||
1c6fdbd8 KO |
1561 | __bch2_dev_offline(c, ca); |
1562 | ||
1563 | mutex_lock(&c->sb_lock); | |
1564 | rcu_assign_pointer(c->devs[ca->dev_idx], NULL); | |
1565 | mutex_unlock(&c->sb_lock); | |
1566 | ||
1567 | percpu_ref_kill(&ca->ref); | |
1568 | wait_for_completion(&ca->ref_completion); | |
1569 | ||
1570 | bch2_dev_free(ca); | |
1571 | ||
1572 | /* | |
1573 | * Free this device's slot in the bch_member array - all pointers to | |
1574 | * this device must be gone: | |
1575 | */ | |
1576 | mutex_lock(&c->sb_lock); | |
1577 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1578 | memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid)); | |
1579 | ||
1580 | bch2_write_super(c); | |
1581 | ||
1582 | mutex_unlock(&c->sb_lock); | |
1ada1606 | 1583 | up_write(&c->state_lock); |
180fb49d KO |
1584 | |
1585 | bch2_dev_usage_journal_reserve(c); | |
1c6fdbd8 KO |
1586 | return 0; |
1587 | err: | |
2436cb9f | 1588 | if (ca->mi.state == BCH_MEMBER_STATE_rw && |
d3bb629d | 1589 | !percpu_ref_is_zero(&ca->io_ref)) |
1c6fdbd8 | 1590 | __bch2_dev_read_write(c, ca); |
1ada1606 | 1591 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1592 | return ret; |
1593 | } | |
1594 | ||
1595 | /* Add new device to running filesystem: */ | |
1596 | int bch2_dev_add(struct bch_fs *c, const char *path) | |
1597 | { | |
1598 | struct bch_opts opts = bch2_opts_empty(); | |
1599 | struct bch_sb_handle sb; | |
1600 | const char *err; | |
1601 | struct bch_dev *ca = NULL; | |
1602 | struct bch_sb_field_members *mi; | |
1603 | struct bch_member dev_mi; | |
1604 | unsigned dev_idx, nr_devices, u64s; | |
1605 | int ret; | |
1606 | ||
1607 | ret = bch2_read_super(path, &opts, &sb); | |
e8536925 KO |
1608 | if (ret) { |
1609 | bch_err(c, "device add error: error reading super: %i", ret); | |
1c6fdbd8 | 1610 | return ret; |
e8536925 | 1611 | } |
1c6fdbd8 KO |
1612 | |
1613 | err = bch2_sb_validate(&sb); | |
e8536925 KO |
1614 | if (err) { |
1615 | bch_err(c, "device add error: error validating super: %s", err); | |
1c6fdbd8 | 1616 | return -EINVAL; |
e8536925 | 1617 | } |
1c6fdbd8 KO |
1618 | |
1619 | dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx]; | |
1620 | ||
1621 | err = bch2_dev_may_add(sb.sb, c); | |
e8536925 KO |
1622 | if (err) { |
1623 | bch_err(c, "device add error: %s", err); | |
1c6fdbd8 | 1624 | return -EINVAL; |
e8536925 | 1625 | } |
1c6fdbd8 KO |
1626 | |
1627 | ca = __bch2_dev_alloc(c, &dev_mi); | |
1628 | if (!ca) { | |
1629 | bch2_free_super(&sb); | |
1630 | return -ENOMEM; | |
1631 | } | |
1632 | ||
1633 | ret = __bch2_dev_attach_bdev(ca, &sb); | |
1634 | if (ret) { | |
1635 | bch2_dev_free(ca); | |
1636 | return ret; | |
1637 | } | |
1638 | ||
1c6fdbd8 | 1639 | ret = bch2_dev_journal_alloc(ca); |
e8536925 KO |
1640 | if (ret) { |
1641 | bch_err(c, "device add error: journal alloc failed"); | |
1c6fdbd8 | 1642 | goto err; |
e8536925 | 1643 | } |
1c6fdbd8 | 1644 | |
1ada1606 | 1645 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1646 | mutex_lock(&c->sb_lock); |
1647 | ||
1c6fdbd8 | 1648 | ret = bch2_sb_from_fs(c, ca); |
e8536925 KO |
1649 | if (ret) { |
1650 | bch_err(c, "device add error: new device superblock too small"); | |
1c6fdbd8 | 1651 | goto err_unlock; |
e8536925 | 1652 | } |
1c6fdbd8 KO |
1653 | |
1654 | mi = bch2_sb_get_members(ca->disk_sb.sb); | |
1655 | ||
1656 | if (!bch2_sb_resize_members(&ca->disk_sb, | |
1657 | le32_to_cpu(mi->field.u64s) + | |
1658 | sizeof(dev_mi) / sizeof(u64))) { | |
e8536925 | 1659 | bch_err(c, "device add error: new device superblock too small"); |
1c6fdbd8 KO |
1660 | ret = -ENOSPC; |
1661 | goto err_unlock; | |
1662 | } | |
1663 | ||
1664 | if (dynamic_fault("bcachefs:add:no_slot")) | |
1665 | goto no_slot; | |
1666 | ||
1667 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1668 | for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) | |
1669 | if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx)) | |
1670 | goto have_slot; | |
1671 | no_slot: | |
e8536925 | 1672 | bch_err(c, "device add error: already have maximum number of devices"); |
1c6fdbd8 KO |
1673 | ret = -ENOSPC; |
1674 | goto err_unlock; | |
1675 | ||
1676 | have_slot: | |
1677 | nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); | |
1678 | u64s = (sizeof(struct bch_sb_field_members) + | |
1679 | sizeof(struct bch_member) * nr_devices) / sizeof(u64); | |
1680 | ||
1c6fdbd8 | 1681 | mi = bch2_sb_resize_members(&c->disk_sb, u64s); |
e8536925 KO |
1682 | if (!mi) { |
1683 | bch_err(c, "device add error: no room in superblock for member info"); | |
1684 | ret = -ENOSPC; | |
1c6fdbd8 | 1685 | goto err_unlock; |
e8536925 | 1686 | } |
1c6fdbd8 KO |
1687 | |
1688 | /* success: */ | |
1689 | ||
1690 | mi->members[dev_idx] = dev_mi; | |
a420eea6 | 1691 | mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds()); |
1c6fdbd8 KO |
1692 | c->disk_sb.sb->nr_devices = nr_devices; |
1693 | ||
1694 | ca->disk_sb.sb->dev_idx = dev_idx; | |
1695 | bch2_dev_attach(c, ca, dev_idx); | |
1696 | ||
1697 | bch2_write_super(c); | |
1698 | mutex_unlock(&c->sb_lock); | |
1699 | ||
180fb49d KO |
1700 | bch2_dev_usage_journal_reserve(c); |
1701 | ||
d62ab355 | 1702 | ret = bch2_trans_mark_dev_sb(c, ca); |
e8536925 KO |
1703 | if (ret) { |
1704 | bch_err(c, "device add error: error marking new superblock: %i", ret); | |
bfcf840d | 1705 | goto err_late; |
e8536925 | 1706 | } |
8d6b6222 | 1707 | |
09943313 KO |
1708 | ca->new_fs_bucket_idx = 0; |
1709 | ||
2436cb9f | 1710 | if (ca->mi.state == BCH_MEMBER_STATE_rw) { |
e2b60560 | 1711 | ret = __bch2_dev_read_write(c, ca); |
e8536925 KO |
1712 | if (ret) { |
1713 | bch_err(c, "device add error: error going RW on new device: %i", ret); | |
1c6fdbd8 | 1714 | goto err_late; |
e8536925 | 1715 | } |
1c6fdbd8 KO |
1716 | } |
1717 | ||
1ada1606 | 1718 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1719 | return 0; |
1720 | ||
1721 | err_unlock: | |
1722 | mutex_unlock(&c->sb_lock); | |
1ada1606 | 1723 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1724 | err: |
1725 | if (ca) | |
1726 | bch2_dev_free(ca); | |
1727 | bch2_free_super(&sb); | |
1c6fdbd8 KO |
1728 | return ret; |
1729 | err_late: | |
bfcf840d | 1730 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1731 | return -EINVAL; |
1732 | } | |
1733 | ||
1734 | /* Hot add existing device to running filesystem: */ | |
1735 | int bch2_dev_online(struct bch_fs *c, const char *path) | |
1736 | { | |
1737 | struct bch_opts opts = bch2_opts_empty(); | |
1738 | struct bch_sb_handle sb = { NULL }; | |
1739 | struct bch_sb_field_members *mi; | |
1740 | struct bch_dev *ca; | |
1741 | unsigned dev_idx; | |
1742 | const char *err; | |
1743 | int ret; | |
1744 | ||
1ada1606 | 1745 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1746 | |
1747 | ret = bch2_read_super(path, &opts, &sb); | |
1748 | if (ret) { | |
1ada1606 | 1749 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1750 | return ret; |
1751 | } | |
1752 | ||
1753 | dev_idx = sb.sb->dev_idx; | |
1754 | ||
1755 | err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); | |
e2b60560 KO |
1756 | if (err) { |
1757 | bch_err(c, "error bringing %s online: %s", path, err); | |
1c6fdbd8 | 1758 | goto err; |
e2b60560 | 1759 | } |
1c6fdbd8 | 1760 | |
e2b60560 KO |
1761 | ret = bch2_dev_attach_bdev(c, &sb); |
1762 | if (ret) | |
1c6fdbd8 | 1763 | goto err; |
1c6fdbd8 KO |
1764 | |
1765 | ca = bch_dev_locked(c, dev_idx); | |
bfcf840d | 1766 | |
e2b60560 KO |
1767 | ret = bch2_trans_mark_dev_sb(c, ca); |
1768 | if (ret) { | |
1769 | bch_err(c, "error bringing %s online: error %i from bch2_trans_mark_dev_sb", | |
1770 | path, ret); | |
bfcf840d KO |
1771 | goto err; |
1772 | } | |
1773 | ||
2436cb9f | 1774 | if (ca->mi.state == BCH_MEMBER_STATE_rw) { |
e2b60560 KO |
1775 | ret = __bch2_dev_read_write(c, ca); |
1776 | if (ret) | |
1c6fdbd8 KO |
1777 | goto err; |
1778 | } | |
1779 | ||
1780 | mutex_lock(&c->sb_lock); | |
1781 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1782 | ||
1783 | mi->members[ca->dev_idx].last_mount = | |
a420eea6 | 1784 | cpu_to_le64(ktime_get_real_seconds()); |
1c6fdbd8 KO |
1785 | |
1786 | bch2_write_super(c); | |
1787 | mutex_unlock(&c->sb_lock); | |
1788 | ||
1ada1606 | 1789 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1790 | return 0; |
1791 | err: | |
1ada1606 | 1792 | up_write(&c->state_lock); |
1c6fdbd8 | 1793 | bch2_free_super(&sb); |
1c6fdbd8 KO |
1794 | return -EINVAL; |
1795 | } | |
1796 | ||
1797 | int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) | |
1798 | { | |
1ada1606 | 1799 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1800 | |
1801 | if (!bch2_dev_is_online(ca)) { | |
1802 | bch_err(ca, "Already offline"); | |
1ada1606 | 1803 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1804 | return 0; |
1805 | } | |
1806 | ||
2436cb9f | 1807 | if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { |
1c6fdbd8 | 1808 | bch_err(ca, "Cannot offline required disk"); |
1ada1606 | 1809 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1810 | return -EINVAL; |
1811 | } | |
1812 | ||
1813 | __bch2_dev_offline(c, ca); | |
1814 | ||
1ada1606 | 1815 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1816 | return 0; |
1817 | } | |
1818 | ||
1819 | int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) | |
1820 | { | |
1821 | struct bch_member *mi; | |
1822 | int ret = 0; | |
1823 | ||
1ada1606 | 1824 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1825 | |
1826 | if (nbuckets < ca->mi.nbuckets) { | |
1827 | bch_err(ca, "Cannot shrink yet"); | |
1828 | ret = -EINVAL; | |
1829 | goto err; | |
1830 | } | |
1831 | ||
1832 | if (bch2_dev_is_online(ca) && | |
1833 | get_capacity(ca->disk_sb.bdev->bd_disk) < | |
1834 | ca->mi.bucket_size * nbuckets) { | |
1835 | bch_err(ca, "New size larger than device"); | |
1836 | ret = -EINVAL; | |
1837 | goto err; | |
1838 | } | |
1839 | ||
1840 | ret = bch2_dev_buckets_resize(c, ca, nbuckets); | |
1841 | if (ret) { | |
1842 | bch_err(ca, "Resize error: %i", ret); | |
1843 | goto err; | |
1844 | } | |
1845 | ||
224ec3e6 KO |
1846 | ret = bch2_trans_mark_dev_sb(c, ca); |
1847 | if (ret) { | |
1848 | goto err; | |
1849 | } | |
1850 | ||
1c6fdbd8 KO |
1851 | mutex_lock(&c->sb_lock); |
1852 | mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx]; | |
1853 | mi->nbuckets = cpu_to_le64(nbuckets); | |
1854 | ||
1855 | bch2_write_super(c); | |
1856 | mutex_unlock(&c->sb_lock); | |
1857 | ||
1858 | bch2_recalc_capacity(c); | |
1859 | err: | |
1ada1606 | 1860 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1861 | return ret; |
1862 | } | |
1863 | ||
1864 | /* return with ref on ca->ref: */ | |
1865 | struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path) | |
1866 | { | |
1c6fdbd8 KO |
1867 | struct bch_dev *ca; |
1868 | dev_t dev; | |
1869 | unsigned i; | |
1870 | int ret; | |
1871 | ||
1872 | ret = lookup_bdev(path, &dev); | |
1873 | if (ret) | |
1874 | return ERR_PTR(ret); | |
1875 | ||
3a402c8d KO |
1876 | rcu_read_lock(); |
1877 | for_each_member_device_rcu(ca, c, i, NULL) | |
1c6fdbd8 KO |
1878 | if (ca->disk_sb.bdev->bd_dev == dev) |
1879 | goto found; | |
1c6fdbd8 KO |
1880 | ca = ERR_PTR(-ENOENT); |
1881 | found: | |
3a402c8d KO |
1882 | rcu_read_unlock(); |
1883 | ||
1c6fdbd8 KO |
1884 | return ca; |
1885 | } | |
1886 | ||
1887 | /* Filesystem open: */ | |
1888 | ||
1889 | struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, | |
1890 | struct bch_opts opts) | |
1891 | { | |
1892 | struct bch_sb_handle *sb = NULL; | |
1893 | struct bch_fs *c = NULL; | |
625104ea | 1894 | struct bch_sb_field_members *mi; |
1c6fdbd8 KO |
1895 | unsigned i, best_sb = 0; |
1896 | const char *err; | |
e2b60560 | 1897 | int ret = 0; |
1c6fdbd8 KO |
1898 | |
1899 | pr_verbose_init(opts, ""); | |
1900 | ||
1901 | if (!nr_devices) { | |
1902 | c = ERR_PTR(-EINVAL); | |
1903 | goto out2; | |
1904 | } | |
1905 | ||
1906 | if (!try_module_get(THIS_MODULE)) { | |
1907 | c = ERR_PTR(-ENODEV); | |
1908 | goto out2; | |
1909 | } | |
1910 | ||
1911 | sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); | |
e2b60560 KO |
1912 | if (!sb) { |
1913 | ret = -ENOMEM; | |
1c6fdbd8 | 1914 | goto err; |
e2b60560 | 1915 | } |
1c6fdbd8 KO |
1916 | |
1917 | for (i = 0; i < nr_devices; i++) { | |
1918 | ret = bch2_read_super(devices[i], &opts, &sb[i]); | |
1919 | if (ret) | |
1920 | goto err; | |
1921 | ||
1922 | err = bch2_sb_validate(&sb[i]); | |
1923 | if (err) | |
1924 | goto err_print; | |
1925 | } | |
1926 | ||
1927 | for (i = 1; i < nr_devices; i++) | |
1928 | if (le64_to_cpu(sb[i].sb->seq) > | |
1929 | le64_to_cpu(sb[best_sb].sb->seq)) | |
1930 | best_sb = i; | |
1931 | ||
625104ea KO |
1932 | mi = bch2_sb_get_members(sb[best_sb].sb); |
1933 | ||
1934 | i = 0; | |
1935 | while (i < nr_devices) { | |
1936 | if (i != best_sb && | |
1937 | !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) { | |
1938 | pr_info("%pg has been removed, skipping", sb[i].bdev); | |
1939 | bch2_free_super(&sb[i]); | |
1940 | array_remove_item(sb, nr_devices, i); | |
1941 | continue; | |
1942 | } | |
1943 | ||
1c6fdbd8 KO |
1944 | err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); |
1945 | if (err) | |
1946 | goto err_print; | |
625104ea | 1947 | i++; |
1c6fdbd8 KO |
1948 | } |
1949 | ||
1c6fdbd8 | 1950 | c = bch2_fs_alloc(sb[best_sb].sb, opts); |
7be9ab63 CW |
1951 | if (IS_ERR(c)) { |
1952 | ret = PTR_ERR(c); | |
1c6fdbd8 | 1953 | goto err; |
7be9ab63 | 1954 | } |
1c6fdbd8 | 1955 | |
1ada1606 | 1956 | down_write(&c->state_lock); |
e2b60560 KO |
1957 | for (i = 0; i < nr_devices; i++) { |
1958 | ret = bch2_dev_attach_bdev(c, &sb[i]); | |
1959 | if (ret) { | |
1ada1606 | 1960 | up_write(&c->state_lock); |
e2b60560 | 1961 | goto err; |
1c6fdbd8 | 1962 | } |
e2b60560 | 1963 | } |
1ada1606 | 1964 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1965 | |
1966 | err = "insufficient devices"; | |
1967 | if (!bch2_fs_may_start(c)) | |
1968 | goto err_print; | |
1969 | ||
1970 | if (!c->opts.nostart) { | |
619f5bee KO |
1971 | ret = bch2_fs_start(c); |
1972 | if (ret) | |
1973 | goto err; | |
1c6fdbd8 KO |
1974 | } |
1975 | out: | |
1976 | kfree(sb); | |
1977 | module_put(THIS_MODULE); | |
1978 | out2: | |
1979 | pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); | |
1980 | return c; | |
1981 | err_print: | |
1982 | pr_err("bch_fs_open err opening %s: %s", | |
1983 | devices[0], err); | |
1984 | ret = -EINVAL; | |
1985 | err: | |
7be9ab63 | 1986 | if (!IS_ERR_OR_NULL(c)) |
1c6fdbd8 | 1987 | bch2_fs_stop(c); |
e2b60560 KO |
1988 | if (sb) |
1989 | for (i = 0; i < nr_devices; i++) | |
1990 | bch2_free_super(&sb[i]); | |
1c6fdbd8 KO |
1991 | c = ERR_PTR(ret); |
1992 | goto out; | |
1993 | } | |
1994 | ||
1995 | static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb, | |
1996 | struct bch_opts opts) | |
1997 | { | |
1998 | const char *err; | |
1999 | struct bch_fs *c; | |
2000 | bool allocated_fs = false; | |
619f5bee | 2001 | int ret; |
1c6fdbd8 KO |
2002 | |
2003 | err = bch2_sb_validate(sb); | |
2004 | if (err) | |
2005 | return err; | |
2006 | ||
2007 | mutex_lock(&bch_fs_list_lock); | |
2008 | c = __bch2_uuid_to_fs(sb->sb->uuid); | |
2009 | if (c) { | |
2010 | closure_get(&c->cl); | |
2011 | ||
2012 | err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb); | |
2013 | if (err) | |
2014 | goto err; | |
2015 | } else { | |
7be9ab63 | 2016 | allocated_fs = true; |
1c6fdbd8 | 2017 | c = bch2_fs_alloc(sb->sb, opts); |
1c6fdbd8 | 2018 | |
7be9ab63 CW |
2019 | err = "bch2_fs_alloc() error"; |
2020 | if (IS_ERR(c)) | |
2021 | goto err; | |
1c6fdbd8 KO |
2022 | } |
2023 | ||
2024 | err = "bch2_dev_online() error"; | |
2025 | ||
2026 | mutex_lock(&c->sb_lock); | |
2027 | if (bch2_dev_attach_bdev(c, sb)) { | |
2028 | mutex_unlock(&c->sb_lock); | |
2029 | goto err; | |
2030 | } | |
2031 | mutex_unlock(&c->sb_lock); | |
2032 | ||
2033 | if (!c->opts.nostart && bch2_fs_may_start(c)) { | |
619f5bee KO |
2034 | err = "error starting filesystem"; |
2035 | ret = bch2_fs_start(c); | |
2036 | if (ret) | |
1c6fdbd8 KO |
2037 | goto err; |
2038 | } | |
2039 | ||
2040 | closure_put(&c->cl); | |
2041 | mutex_unlock(&bch_fs_list_lock); | |
2042 | ||
2043 | return NULL; | |
2044 | err: | |
2045 | mutex_unlock(&bch_fs_list_lock); | |
2046 | ||
7be9ab63 | 2047 | if (allocated_fs && !IS_ERR(c)) |
1c6fdbd8 KO |
2048 | bch2_fs_stop(c); |
2049 | else if (c) | |
2050 | closure_put(&c->cl); | |
2051 | ||
2052 | return err; | |
2053 | } | |
2054 | ||
2055 | const char *bch2_fs_open_incremental(const char *path) | |
2056 | { | |
2057 | struct bch_sb_handle sb; | |
2058 | struct bch_opts opts = bch2_opts_empty(); | |
2059 | const char *err; | |
2060 | ||
2061 | if (bch2_read_super(path, &opts, &sb)) | |
2062 | return "error reading superblock"; | |
2063 | ||
2064 | err = __bch2_fs_open_incremental(&sb, opts); | |
2065 | bch2_free_super(&sb); | |
2066 | ||
2067 | return err; | |
2068 | } | |
2069 | ||
2070 | /* Global interfaces/init */ | |
2071 | ||
2072 | static void bcachefs_exit(void) | |
2073 | { | |
2074 | bch2_debug_exit(); | |
2075 | bch2_vfs_exit(); | |
2076 | bch2_chardev_exit(); | |
14ba3706 | 2077 | bch2_btree_key_cache_exit(); |
1c6fdbd8 KO |
2078 | if (bcachefs_kset) |
2079 | kset_unregister(bcachefs_kset); | |
2080 | } | |
2081 | ||
2082 | static int __init bcachefs_init(void) | |
2083 | { | |
2084 | bch2_bkey_pack_test(); | |
1c6fdbd8 KO |
2085 | |
2086 | if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || | |
14ba3706 | 2087 | bch2_btree_key_cache_init() || |
1c6fdbd8 KO |
2088 | bch2_chardev_init() || |
2089 | bch2_vfs_init() || | |
2090 | bch2_debug_init()) | |
2091 | goto err; | |
2092 | ||
2093 | return 0; | |
2094 | err: | |
2095 | bcachefs_exit(); | |
2096 | return -ENOMEM; | |
2097 | } | |
2098 | ||
2099 | #define BCH_DEBUG_PARAM(name, description) \ | |
2100 | bool bch2_##name; \ | |
2101 | module_param_named(name, bch2_##name, bool, 0644); \ | |
2102 | MODULE_PARM_DESC(name, description); | |
2103 | BCH_DEBUG_PARAMS() | |
2104 | #undef BCH_DEBUG_PARAM | |
2105 | ||
26609b61 | 2106 | unsigned bch2_metadata_version = bcachefs_metadata_version_current; |
1c6fdbd8 KO |
2107 | module_param_named(version, bch2_metadata_version, uint, 0400); |
2108 | ||
2109 | module_exit(bcachefs_exit); | |
2110 | module_init(bcachefs_init); |