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