]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.c
Update bcachefs sources to 1a739db0b256 bcachefs; guard against overflow in btree...
[bcachefs-tools-debian] / libbcachefs / super-io.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "checksum.h"
5 #include "counters.h"
6 #include "disk_groups.h"
7 #include "ec.h"
8 #include "error.h"
9 #include "journal.h"
10 #include "journal_sb.h"
11 #include "journal_seq_blacklist.h"
12 #include "recovery.h"
13 #include "replicas.h"
14 #include "quota.h"
15 #include "sb-clean.h"
16 #include "sb-errors.h"
17 #include "sb-members.h"
18 #include "super-io.h"
19 #include "super.h"
20 #include "trace.h"
21 #include "vstructs.h"
22
23 #include <linux/backing-dev.h>
24 #include <linux/sort.h>
25
26 static const struct blk_holder_ops bch2_sb_handle_bdev_ops = {
27 };
28
29 struct bch2_metadata_version {
30         u16             version;
31         const char      *name;
32         u64             recovery_passes;
33 };
34
35 static const struct bch2_metadata_version bch2_metadata_versions[] = {
36 #define x(n, v, _recovery_passes) {             \
37         .version = v,                           \
38         .name = #n,                             \
39         .recovery_passes = _recovery_passes,    \
40 },
41         BCH_METADATA_VERSIONS()
42 #undef x
43 };
44
45 void bch2_version_to_text(struct printbuf *out, unsigned v)
46 {
47         const char *str = "(unknown version)";
48
49         for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
50                 if (bch2_metadata_versions[i].version == v) {
51                         str = bch2_metadata_versions[i].name;
52                         break;
53                 }
54
55         prt_printf(out, "%u.%u: %s", BCH_VERSION_MAJOR(v), BCH_VERSION_MINOR(v), str);
56 }
57
58 unsigned bch2_latest_compatible_version(unsigned v)
59 {
60         if (!BCH_VERSION_MAJOR(v))
61                 return v;
62
63         for (unsigned i = 0; i < ARRAY_SIZE(bch2_metadata_versions); i++)
64                 if (bch2_metadata_versions[i].version > v &&
65                     BCH_VERSION_MAJOR(bch2_metadata_versions[i].version) ==
66                     BCH_VERSION_MAJOR(v))
67                         v = bch2_metadata_versions[i].version;
68
69         return v;
70 }
71
72 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
73                                  unsigned old_version,
74                                  unsigned new_version)
75 {
76         u64 ret = 0;
77
78         for (const struct bch2_metadata_version *i = bch2_metadata_versions;
79              i < bch2_metadata_versions + ARRAY_SIZE(bch2_metadata_versions);
80              i++)
81                 if (i->version > old_version && i->version <= new_version) {
82                         if (i->recovery_passes & RECOVERY_PASS_ALL_FSCK)
83                                 ret |= bch2_fsck_recovery_passes();
84                         ret |= i->recovery_passes;
85                 }
86
87         return ret &= ~RECOVERY_PASS_ALL_FSCK;
88 }
89
90 const char * const bch2_sb_fields[] = {
91 #define x(name, nr)     #name,
92         BCH_SB_FIELDS()
93 #undef x
94         NULL
95 };
96
97 static int bch2_sb_field_validate(struct bch_sb *, struct bch_sb_field *,
98                                   struct printbuf *);
99
100 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *sb,
101                                       enum bch_sb_field_type type)
102 {
103         /* XXX: need locking around superblock to access optional fields */
104
105         vstruct_for_each(sb, f)
106                 if (le32_to_cpu(f->type) == type)
107                         return f;
108         return NULL;
109 }
110
111 static struct bch_sb_field *__bch2_sb_field_resize(struct bch_sb_handle *sb,
112                                                    struct bch_sb_field *f,
113                                                    unsigned u64s)
114 {
115         unsigned old_u64s = f ? le32_to_cpu(f->u64s) : 0;
116         unsigned sb_u64s = le32_to_cpu(sb->sb->u64s) + u64s - old_u64s;
117
118         BUG_ON(__vstruct_bytes(struct bch_sb, sb_u64s) > sb->buffer_size);
119
120         if (!f && !u64s) {
121                 /* nothing to do: */
122         } else if (!f) {
123                 f = vstruct_last(sb->sb);
124                 memset(f, 0, sizeof(u64) * u64s);
125                 f->u64s = cpu_to_le32(u64s);
126                 f->type = 0;
127         } else {
128                 void *src, *dst;
129
130                 src = vstruct_end(f);
131
132                 if (u64s) {
133                         f->u64s = cpu_to_le32(u64s);
134                         dst = vstruct_end(f);
135                 } else {
136                         dst = f;
137                 }
138
139                 memmove(dst, src, vstruct_end(sb->sb) - src);
140
141                 if (dst > src)
142                         memset(src, 0, dst - src);
143         }
144
145         sb->sb->u64s = cpu_to_le32(sb_u64s);
146
147         return u64s ? f : NULL;
148 }
149
150 void bch2_sb_field_delete(struct bch_sb_handle *sb,
151                           enum bch_sb_field_type type)
152 {
153         struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
154
155         if (f)
156                 __bch2_sb_field_resize(sb, f, 0);
157 }
158
159 /* Superblock realloc/free: */
160
161 void bch2_free_super(struct bch_sb_handle *sb)
162 {
163         kfree(sb->bio);
164         if (!IS_ERR_OR_NULL(sb->bdev))
165                 blkdev_put(sb->bdev, sb->holder);
166         kfree(sb->holder);
167         kfree(sb->sb_name);
168
169         kfree(sb->sb);
170         memset(sb, 0, sizeof(*sb));
171 }
172
173 int bch2_sb_realloc(struct bch_sb_handle *sb, unsigned u64s)
174 {
175         size_t new_bytes = __vstruct_bytes(struct bch_sb, u64s);
176         size_t new_buffer_size;
177         struct bch_sb *new_sb;
178         struct bio *bio;
179
180         if (sb->bdev)
181                 new_bytes = max_t(size_t, new_bytes, bdev_logical_block_size(sb->bdev));
182
183         new_buffer_size = roundup_pow_of_two(new_bytes);
184
185         if (sb->sb && sb->buffer_size >= new_buffer_size)
186                 return 0;
187
188         if (sb->sb && sb->have_layout) {
189                 u64 max_bytes = 512 << sb->sb->layout.sb_max_size_bits;
190
191                 if (new_bytes > max_bytes) {
192                         pr_err("%pg: superblock too big: want %zu but have %llu",
193                                sb->bdev, new_bytes, max_bytes);
194                         return -BCH_ERR_ENOSPC_sb;
195                 }
196         }
197
198         if (sb->buffer_size >= new_buffer_size && sb->sb)
199                 return 0;
200
201         if (dynamic_fault("bcachefs:add:super_realloc"))
202                 return -BCH_ERR_ENOMEM_sb_realloc_injected;
203
204         new_sb = krealloc(sb->sb, new_buffer_size, GFP_NOFS|__GFP_ZERO);
205         if (!new_sb)
206                 return -BCH_ERR_ENOMEM_sb_buf_realloc;
207
208         sb->sb = new_sb;
209
210         if (sb->have_bio) {
211                 unsigned nr_bvecs = buf_pages(sb->sb, new_buffer_size);
212
213                 bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
214                 if (!bio)
215                         return -BCH_ERR_ENOMEM_sb_bio_realloc;
216
217                 bio_init(bio, NULL, bio->bi_inline_vecs, nr_bvecs, 0);
218
219                 kfree(sb->bio);
220                 sb->bio = bio;
221         }
222
223         sb->buffer_size = new_buffer_size;
224
225         return 0;
226 }
227
228 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *sb,
229                                           enum bch_sb_field_type type,
230                                           unsigned u64s)
231 {
232         struct bch_sb_field *f = bch2_sb_field_get_id(sb->sb, type);
233         ssize_t old_u64s = f ? le32_to_cpu(f->u64s) : 0;
234         ssize_t d = -old_u64s + u64s;
235
236         if (bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s) + d))
237                 return NULL;
238
239         if (sb->fs_sb) {
240                 struct bch_fs *c = container_of(sb, struct bch_fs, disk_sb);
241
242                 lockdep_assert_held(&c->sb_lock);
243
244                 /* XXX: we're not checking that offline device have enough space */
245
246                 for_each_online_member(c, ca) {
247                         struct bch_sb_handle *dev_sb = &ca->disk_sb;
248
249                         if (bch2_sb_realloc(dev_sb, le32_to_cpu(dev_sb->sb->u64s) + d)) {
250                                 percpu_ref_put(&ca->ref);
251                                 return NULL;
252                         }
253                 }
254         }
255
256         f = bch2_sb_field_get_id(sb->sb, type);
257         f = __bch2_sb_field_resize(sb, f, u64s);
258         if (f)
259                 f->type = cpu_to_le32(type);
260         return f;
261 }
262
263 /* Superblock validate: */
264
265 static int validate_sb_layout(struct bch_sb_layout *layout, struct printbuf *out)
266 {
267         u64 offset, prev_offset, max_sectors;
268         unsigned i;
269
270         BUILD_BUG_ON(sizeof(struct bch_sb_layout) != 512);
271
272         if (!uuid_equal(&layout->magic, &BCACHE_MAGIC) &&
273             !uuid_equal(&layout->magic, &BCHFS_MAGIC)) {
274                 prt_printf(out, "Not a bcachefs superblock layout");
275                 return -BCH_ERR_invalid_sb_layout;
276         }
277
278         if (layout->layout_type != 0) {
279                 prt_printf(out, "Invalid superblock layout type %u",
280                        layout->layout_type);
281                 return -BCH_ERR_invalid_sb_layout_type;
282         }
283
284         if (!layout->nr_superblocks) {
285                 prt_printf(out, "Invalid superblock layout: no superblocks");
286                 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
287         }
288
289         if (layout->nr_superblocks > ARRAY_SIZE(layout->sb_offset)) {
290                 prt_printf(out, "Invalid superblock layout: too many superblocks");
291                 return -BCH_ERR_invalid_sb_layout_nr_superblocks;
292         }
293
294         max_sectors = 1 << layout->sb_max_size_bits;
295
296         prev_offset = le64_to_cpu(layout->sb_offset[0]);
297
298         for (i = 1; i < layout->nr_superblocks; i++) {
299                 offset = le64_to_cpu(layout->sb_offset[i]);
300
301                 if (offset < prev_offset + max_sectors) {
302                         prt_printf(out, "Invalid superblock layout: superblocks overlap\n"
303                                "  (sb %u ends at %llu next starts at %llu",
304                                i - 1, prev_offset + max_sectors, offset);
305                         return -BCH_ERR_invalid_sb_layout_superblocks_overlap;
306                 }
307                 prev_offset = offset;
308         }
309
310         return 0;
311 }
312
313 static int bch2_sb_compatible(struct bch_sb *sb, struct printbuf *out)
314 {
315         u16 version             = le16_to_cpu(sb->version);
316         u16 version_min         = le16_to_cpu(sb->version_min);
317
318         if (!bch2_version_compatible(version)) {
319                 prt_str(out, "Unsupported superblock version ");
320                 bch2_version_to_text(out, version);
321                 prt_str(out, " (min ");
322                 bch2_version_to_text(out, bcachefs_metadata_version_min);
323                 prt_str(out, ", max ");
324                 bch2_version_to_text(out, bcachefs_metadata_version_current);
325                 prt_str(out, ")");
326                 return -BCH_ERR_invalid_sb_version;
327         }
328
329         if (!bch2_version_compatible(version_min)) {
330                 prt_str(out, "Unsupported superblock version_min ");
331                 bch2_version_to_text(out, version_min);
332                 prt_str(out, " (min ");
333                 bch2_version_to_text(out, bcachefs_metadata_version_min);
334                 prt_str(out, ", max ");
335                 bch2_version_to_text(out, bcachefs_metadata_version_current);
336                 prt_str(out, ")");
337                 return -BCH_ERR_invalid_sb_version;
338         }
339
340         if (version_min > version) {
341                 prt_str(out, "Bad minimum version ");
342                 bch2_version_to_text(out, version_min);
343                 prt_str(out, ", greater than version field ");
344                 bch2_version_to_text(out, version);
345                 return -BCH_ERR_invalid_sb_version;
346         }
347
348         return 0;
349 }
350
351 static int bch2_sb_validate(struct bch_sb_handle *disk_sb, struct printbuf *out,
352                             int rw)
353 {
354         struct bch_sb *sb = disk_sb->sb;
355         struct bch_sb_field_members_v1 *mi;
356         enum bch_opt_id opt_id;
357         u16 block_size;
358         int ret;
359
360         ret = bch2_sb_compatible(sb, out);
361         if (ret)
362                 return ret;
363
364         if (sb->features[1] ||
365             (le64_to_cpu(sb->features[0]) & (~0ULL << BCH_FEATURE_NR))) {
366                 prt_printf(out, "Filesystem has incompatible features");
367                 return -BCH_ERR_invalid_sb_features;
368         }
369
370         block_size = le16_to_cpu(sb->block_size);
371
372         if (block_size > PAGE_SECTORS) {
373                 prt_printf(out, "Block size too big (got %u, max %u)",
374                        block_size, PAGE_SECTORS);
375                 return -BCH_ERR_invalid_sb_block_size;
376         }
377
378         if (bch2_is_zero(sb->user_uuid.b, sizeof(sb->user_uuid))) {
379                 prt_printf(out, "Bad user UUID (got zeroes)");
380                 return -BCH_ERR_invalid_sb_uuid;
381         }
382
383         if (bch2_is_zero(sb->uuid.b, sizeof(sb->uuid))) {
384                 prt_printf(out, "Bad internal UUID (got zeroes)");
385                 return -BCH_ERR_invalid_sb_uuid;
386         }
387
388         if (!sb->nr_devices ||
389             sb->nr_devices > BCH_SB_MEMBERS_MAX) {
390                 prt_printf(out, "Bad number of member devices %u (max %u)",
391                        sb->nr_devices, BCH_SB_MEMBERS_MAX);
392                 return -BCH_ERR_invalid_sb_too_many_members;
393         }
394
395         if (sb->dev_idx >= sb->nr_devices) {
396                 prt_printf(out, "Bad dev_idx (got %u, nr_devices %u)",
397                        sb->dev_idx, sb->nr_devices);
398                 return -BCH_ERR_invalid_sb_dev_idx;
399         }
400
401         if (!sb->time_precision ||
402             le32_to_cpu(sb->time_precision) > NSEC_PER_SEC) {
403                 prt_printf(out, "Invalid time precision: %u (min 1, max %lu)",
404                        le32_to_cpu(sb->time_precision), NSEC_PER_SEC);
405                 return -BCH_ERR_invalid_sb_time_precision;
406         }
407
408         if (rw == READ) {
409                 /*
410                  * Been seeing a bug where these are getting inexplicably
411                  * zeroed, so we're now validating them, but we have to be
412                  * careful not to preven people's filesystems from mounting:
413                  */
414                 if (!BCH_SB_JOURNAL_FLUSH_DELAY(sb))
415                         SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
416                 if (!BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
417                         SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 1000);
418
419                 if (!BCH_SB_VERSION_UPGRADE_COMPLETE(sb))
420                         SET_BCH_SB_VERSION_UPGRADE_COMPLETE(sb, le16_to_cpu(sb->version));
421         }
422
423         for (opt_id = 0; opt_id < bch2_opts_nr; opt_id++) {
424                 const struct bch_option *opt = bch2_opt_table + opt_id;
425
426                 if (opt->get_sb != BCH2_NO_SB_OPT) {
427                         u64 v = bch2_opt_from_sb(sb, opt_id);
428
429                         prt_printf(out, "Invalid option ");
430                         ret = bch2_opt_validate(opt, v, out);
431                         if (ret)
432                                 return ret;
433
434                         printbuf_reset(out);
435                 }
436         }
437
438         /* validate layout */
439         ret = validate_sb_layout(&sb->layout, out);
440         if (ret)
441                 return ret;
442
443         vstruct_for_each(sb, f) {
444                 if (!f->u64s) {
445                         prt_printf(out, "Invalid superblock: optional field with size 0 (type %u)",
446                                le32_to_cpu(f->type));
447                         return -BCH_ERR_invalid_sb_field_size;
448                 }
449
450                 if (vstruct_next(f) > vstruct_last(sb)) {
451                         prt_printf(out, "Invalid superblock: optional field extends past end of superblock (type %u)",
452                                le32_to_cpu(f->type));
453                         return -BCH_ERR_invalid_sb_field_size;
454                 }
455         }
456
457         /* members must be validated first: */
458         mi = bch2_sb_field_get(sb, members_v1);
459         if (!mi) {
460                 prt_printf(out, "Invalid superblock: member info area missing");
461                 return -BCH_ERR_invalid_sb_members_missing;
462         }
463
464         ret = bch2_sb_field_validate(sb, &mi->field, out);
465         if (ret)
466                 return ret;
467
468         vstruct_for_each(sb, f) {
469                 if (le32_to_cpu(f->type) == BCH_SB_FIELD_members_v1)
470                         continue;
471
472                 ret = bch2_sb_field_validate(sb, f, out);
473                 if (ret)
474                         return ret;
475         }
476
477         return 0;
478 }
479
480 /* device open: */
481
482 static void bch2_sb_update(struct bch_fs *c)
483 {
484         struct bch_sb *src = c->disk_sb.sb;
485
486         lockdep_assert_held(&c->sb_lock);
487
488         c->sb.uuid              = src->uuid;
489         c->sb.user_uuid         = src->user_uuid;
490         c->sb.version           = le16_to_cpu(src->version);
491         c->sb.version_min       = le16_to_cpu(src->version_min);
492         c->sb.version_upgrade_complete = BCH_SB_VERSION_UPGRADE_COMPLETE(src);
493         c->sb.nr_devices        = src->nr_devices;
494         c->sb.clean             = BCH_SB_CLEAN(src);
495         c->sb.encryption_type   = BCH_SB_ENCRYPTION_TYPE(src);
496
497         c->sb.nsec_per_time_unit = le32_to_cpu(src->time_precision);
498         c->sb.time_units_per_sec = NSEC_PER_SEC / c->sb.nsec_per_time_unit;
499
500         /* XXX this is wrong, we need a 96 or 128 bit integer type */
501         c->sb.time_base_lo      = div_u64(le64_to_cpu(src->time_base_lo),
502                                           c->sb.nsec_per_time_unit);
503         c->sb.time_base_hi      = le32_to_cpu(src->time_base_hi);
504
505         c->sb.features          = le64_to_cpu(src->features[0]);
506         c->sb.compat            = le64_to_cpu(src->compat[0]);
507
508         for_each_member_device(c, ca) {
509                 struct bch_member m = bch2_sb_member_get(src, ca->dev_idx);
510                 ca->mi = bch2_mi_to_cpu(&m);
511         }
512 }
513
514 static int __copy_super(struct bch_sb_handle *dst_handle, struct bch_sb *src)
515 {
516         struct bch_sb_field *src_f, *dst_f;
517         struct bch_sb *dst = dst_handle->sb;
518         unsigned i;
519
520         dst->version            = src->version;
521         dst->version_min        = src->version_min;
522         dst->seq                = src->seq;
523         dst->uuid               = src->uuid;
524         dst->user_uuid          = src->user_uuid;
525         memcpy(dst->label,      src->label, sizeof(dst->label));
526
527         dst->block_size         = src->block_size;
528         dst->nr_devices         = src->nr_devices;
529
530         dst->time_base_lo       = src->time_base_lo;
531         dst->time_base_hi       = src->time_base_hi;
532         dst->time_precision     = src->time_precision;
533
534         memcpy(dst->flags,      src->flags,     sizeof(dst->flags));
535         memcpy(dst->features,   src->features,  sizeof(dst->features));
536         memcpy(dst->compat,     src->compat,    sizeof(dst->compat));
537
538         for (i = 0; i < BCH_SB_FIELD_NR; i++) {
539                 int d;
540
541                 if ((1U << i) & BCH_SINGLE_DEVICE_SB_FIELDS)
542                         continue;
543
544                 src_f = bch2_sb_field_get_id(src, i);
545                 dst_f = bch2_sb_field_get_id(dst, i);
546
547                 d = (src_f ? le32_to_cpu(src_f->u64s) : 0) -
548                     (dst_f ? le32_to_cpu(dst_f->u64s) : 0);
549                 if (d > 0) {
550                         int ret = bch2_sb_realloc(dst_handle,
551                                         le32_to_cpu(dst_handle->sb->u64s) + d);
552
553                         if (ret)
554                                 return ret;
555
556                         dst = dst_handle->sb;
557                         dst_f = bch2_sb_field_get_id(dst, i);
558                 }
559
560                 dst_f = __bch2_sb_field_resize(dst_handle, dst_f,
561                                 src_f ? le32_to_cpu(src_f->u64s) : 0);
562
563                 if (src_f)
564                         memcpy(dst_f, src_f, vstruct_bytes(src_f));
565         }
566
567         return 0;
568 }
569
570 int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
571 {
572         int ret;
573
574         lockdep_assert_held(&c->sb_lock);
575
576         ret =   bch2_sb_realloc(&c->disk_sb, 0) ?:
577                 __copy_super(&c->disk_sb, src) ?:
578                 bch2_sb_replicas_to_cpu_replicas(c) ?:
579                 bch2_sb_disk_groups_to_cpu(c);
580         if (ret)
581                 return ret;
582
583         bch2_sb_update(c);
584         return 0;
585 }
586
587 int bch2_sb_from_fs(struct bch_fs *c, struct bch_dev *ca)
588 {
589         return __copy_super(&ca->disk_sb, c->disk_sb.sb);
590 }
591
592 /* read superblock: */
593
594 static int read_one_super(struct bch_sb_handle *sb, u64 offset, struct printbuf *err)
595 {
596         struct bch_csum csum;
597         size_t bytes;
598         int ret;
599 reread:
600         bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
601         sb->bio->bi_iter.bi_sector = offset;
602         bch2_bio_map(sb->bio, sb->sb, sb->buffer_size);
603
604         ret = submit_bio_wait(sb->bio);
605         if (ret) {
606                 prt_printf(err, "IO error: %i", ret);
607                 return ret;
608         }
609
610         if (!uuid_equal(&sb->sb->magic, &BCACHE_MAGIC) &&
611             !uuid_equal(&sb->sb->magic, &BCHFS_MAGIC)) {
612                 prt_printf(err, "Not a bcachefs superblock");
613                 return -BCH_ERR_invalid_sb_magic;
614         }
615
616         ret = bch2_sb_compatible(sb->sb, err);
617         if (ret)
618                 return ret;
619
620         bytes = vstruct_bytes(sb->sb);
621
622         if (bytes > 512 << sb->sb->layout.sb_max_size_bits) {
623                 prt_printf(err, "Invalid superblock: too big (got %zu bytes, layout max %lu)",
624                        bytes, 512UL << sb->sb->layout.sb_max_size_bits);
625                 return -BCH_ERR_invalid_sb_too_big;
626         }
627
628         if (bytes > sb->buffer_size) {
629                 ret = bch2_sb_realloc(sb, le32_to_cpu(sb->sb->u64s));
630                 if (ret)
631                         return ret;
632                 goto reread;
633         }
634
635         if (BCH_SB_CSUM_TYPE(sb->sb) >= BCH_CSUM_NR) {
636                 prt_printf(err, "unknown checksum type %llu", BCH_SB_CSUM_TYPE(sb->sb));
637                 return -BCH_ERR_invalid_sb_csum_type;
638         }
639
640         /* XXX: verify MACs */
641         csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb),
642                             null_nonce(), sb->sb);
643
644         if (bch2_crc_cmp(csum, sb->sb->csum)) {
645                 prt_printf(err, "bad checksum");
646                 return -BCH_ERR_invalid_sb_csum;
647         }
648
649         sb->seq = le64_to_cpu(sb->sb->seq);
650
651         return 0;
652 }
653
654 static int __bch2_read_super(const char *path, struct bch_opts *opts,
655                     struct bch_sb_handle *sb, bool ignore_notbchfs_msg)
656 {
657         u64 offset = opt_get(*opts, sb);
658         struct bch_sb_layout layout;
659         struct printbuf err = PRINTBUF;
660         struct printbuf err2 = PRINTBUF;
661         __le64 *i;
662         int ret;
663 #ifndef __KERNEL__
664 retry:
665 #endif
666         memset(sb, 0, sizeof(*sb));
667         sb->mode        = BLK_OPEN_READ;
668         sb->have_bio    = true;
669         sb->holder      = kmalloc(1, GFP_KERNEL);
670         if (!sb->holder)
671                 return -ENOMEM;
672
673         sb->sb_name = kstrdup(path, GFP_KERNEL);
674         if (!sb->sb_name)
675                 return -ENOMEM;
676
677 #ifndef __KERNEL__
678         if (opt_get(*opts, direct_io) == false)
679                 sb->mode |= BLK_OPEN_BUFFERED;
680 #endif
681
682         if (!opt_get(*opts, noexcl))
683                 sb->mode |= BLK_OPEN_EXCL;
684
685         if (!opt_get(*opts, nochanges))
686                 sb->mode |= BLK_OPEN_WRITE;
687
688         sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
689         if (IS_ERR(sb->bdev) &&
690             PTR_ERR(sb->bdev) == -EACCES &&
691             opt_get(*opts, read_only)) {
692                 sb->mode &= ~BLK_OPEN_WRITE;
693
694                 sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops);
695                 if (!IS_ERR(sb->bdev))
696                         opt_set(*opts, nochanges, true);
697         }
698
699         if (IS_ERR(sb->bdev)) {
700                 ret = PTR_ERR(sb->bdev);
701                 goto out;
702         }
703
704         ret = bch2_sb_realloc(sb, 0);
705         if (ret) {
706                 prt_printf(&err, "error allocating memory for superblock");
707                 goto err;
708         }
709
710         if (bch2_fs_init_fault("read_super")) {
711                 prt_printf(&err, "dynamic fault");
712                 ret = -EFAULT;
713                 goto err;
714         }
715
716         ret = read_one_super(sb, offset, &err);
717         if (!ret)
718                 goto got_super;
719
720         if (opt_defined(*opts, sb))
721                 goto err;
722
723         prt_printf(&err2, "bcachefs (%s): error reading default superblock: %s\n",
724                path, err.buf);
725         if (ret == -BCH_ERR_invalid_sb_magic && ignore_notbchfs_msg)
726                 printk(KERN_INFO "%s", err2.buf);
727         else
728                 printk(KERN_ERR "%s", err2.buf);
729
730         printbuf_exit(&err2);
731         printbuf_reset(&err);
732
733         /*
734          * Error reading primary superblock - read location of backup
735          * superblocks:
736          */
737         bio_reset(sb->bio, sb->bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
738         sb->bio->bi_iter.bi_sector = BCH_SB_LAYOUT_SECTOR;
739         /*
740          * use sb buffer to read layout, since sb buffer is page aligned but
741          * layout won't be:
742          */
743         bch2_bio_map(sb->bio, sb->sb, sizeof(struct bch_sb_layout));
744
745         ret = submit_bio_wait(sb->bio);
746         if (ret) {
747                 prt_printf(&err, "IO error: %i", ret);
748                 goto err;
749         }
750
751         memcpy(&layout, sb->sb, sizeof(layout));
752         ret = validate_sb_layout(&layout, &err);
753         if (ret)
754                 goto err;
755
756         for (i = layout.sb_offset;
757              i < layout.sb_offset + layout.nr_superblocks; i++) {
758                 offset = le64_to_cpu(*i);
759
760                 if (offset == opt_get(*opts, sb))
761                         continue;
762
763                 ret = read_one_super(sb, offset, &err);
764                 if (!ret)
765                         goto got_super;
766         }
767
768         goto err;
769
770 got_super:
771         if (le16_to_cpu(sb->sb->block_size) << 9 <
772             bdev_logical_block_size(sb->bdev) &&
773             opt_get(*opts, direct_io)) {
774 #ifndef __KERNEL__
775                 opt_set(*opts, direct_io, false);
776                 bch2_free_super(sb);
777                 goto retry;
778 #endif
779                 prt_printf(&err, "block size (%u) smaller than device block size (%u)",
780                        le16_to_cpu(sb->sb->block_size) << 9,
781                        bdev_logical_block_size(sb->bdev));
782                 ret = -BCH_ERR_block_size_too_small;
783                 goto err;
784         }
785
786         ret = 0;
787         sb->have_layout = true;
788
789         ret = bch2_sb_validate(sb, &err, READ);
790         if (ret) {
791                 printk(KERN_ERR "bcachefs (%s): error validating superblock: %s\n",
792                        path, err.buf);
793                 goto err_no_print;
794         }
795 out:
796         printbuf_exit(&err);
797         return ret;
798 err:
799         printk(KERN_ERR "bcachefs (%s): error reading superblock: %s\n",
800                path, err.buf);
801 err_no_print:
802         bch2_free_super(sb);
803         goto out;
804 }
805
806 int bch2_read_super(const char *path, struct bch_opts *opts,
807                     struct bch_sb_handle *sb)
808 {
809         return __bch2_read_super(path, opts, sb, false);
810 }
811
812 /* provide a silenced version for mount.bcachefs */
813
814 int bch2_read_super_silent(const char *path, struct bch_opts *opts,
815                     struct bch_sb_handle *sb)
816 {
817         return __bch2_read_super(path, opts, sb, true);
818 }
819
820 /* write superblock: */
821
822 static void write_super_endio(struct bio *bio)
823 {
824         struct bch_dev *ca = bio->bi_private;
825
826         /* XXX: return errors directly */
827
828         if (bch2_dev_io_err_on(bio->bi_status, ca,
829                                bio_data_dir(bio)
830                                ? BCH_MEMBER_ERROR_write
831                                : BCH_MEMBER_ERROR_read,
832                                "superblock %s error: %s",
833                                bio_data_dir(bio) ? "write" : "read",
834                                bch2_blk_status_to_str(bio->bi_status)))
835                 ca->sb_write_error = 1;
836
837         closure_put(&ca->fs->sb_write);
838         percpu_ref_put(&ca->io_ref);
839 }
840
841 static void read_back_super(struct bch_fs *c, struct bch_dev *ca)
842 {
843         struct bch_sb *sb = ca->disk_sb.sb;
844         struct bio *bio = ca->disk_sb.bio;
845
846         bio_reset(bio, ca->disk_sb.bdev, REQ_OP_READ|REQ_SYNC|REQ_META);
847         bio->bi_iter.bi_sector  = le64_to_cpu(sb->layout.sb_offset[0]);
848         bio->bi_end_io          = write_super_endio;
849         bio->bi_private         = ca;
850         bch2_bio_map(bio, ca->sb_read_scratch, PAGE_SIZE);
851
852         this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_sb],
853                      bio_sectors(bio));
854
855         percpu_ref_get(&ca->io_ref);
856         closure_bio_submit(bio, &c->sb_write);
857 }
858
859 static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx)
860 {
861         struct bch_sb *sb = ca->disk_sb.sb;
862         struct bio *bio = ca->disk_sb.bio;
863
864         sb->offset = sb->layout.sb_offset[idx];
865
866         SET_BCH_SB_CSUM_TYPE(sb, bch2_csum_opt_to_type(c->opts.metadata_checksum, false));
867         sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb),
868                                 null_nonce(), sb);
869
870         bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
871         bio->bi_iter.bi_sector  = le64_to_cpu(sb->offset);
872         bio->bi_end_io          = write_super_endio;
873         bio->bi_private         = ca;
874         bch2_bio_map(bio, sb,
875                      roundup((size_t) vstruct_bytes(sb),
876                              bdev_logical_block_size(ca->disk_sb.bdev)));
877
878         this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_sb],
879                      bio_sectors(bio));
880
881         percpu_ref_get(&ca->io_ref);
882         closure_bio_submit(bio, &c->sb_write);
883 }
884
885 int bch2_write_super(struct bch_fs *c)
886 {
887         struct closure *cl = &c->sb_write;
888         struct printbuf err = PRINTBUF;
889         unsigned sb = 0, nr_wrote;
890         struct bch_devs_mask sb_written;
891         bool wrote, can_mount_without_written, can_mount_with_written;
892         unsigned degraded_flags = BCH_FORCE_IF_DEGRADED;
893         int ret = 0;
894
895         trace_and_count(c, write_super, c, _RET_IP_);
896
897         if (c->opts.very_degraded)
898                 degraded_flags |= BCH_FORCE_IF_LOST;
899
900         lockdep_assert_held(&c->sb_lock);
901
902         closure_init_stack(cl);
903         memset(&sb_written, 0, sizeof(sb_written));
904
905         /* Make sure we're using the new magic numbers: */
906         c->disk_sb.sb->magic = BCHFS_MAGIC;
907         c->disk_sb.sb->layout.magic = BCHFS_MAGIC;
908
909         le64_add_cpu(&c->disk_sb.sb->seq, 1);
910
911         if (test_bit(BCH_FS_error, &c->flags))
912                 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
913         if (test_bit(BCH_FS_topology_error, &c->flags))
914                 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
915
916         SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
917
918         bch2_sb_counters_from_cpu(c);
919         bch2_sb_members_from_cpu(c);
920         bch2_sb_members_cpy_v2_v1(&c->disk_sb);
921         bch2_sb_errors_from_cpu(c);
922
923         for_each_online_member(c, ca)
924                 bch2_sb_from_fs(c, ca);
925
926         for_each_online_member(c, ca) {
927                 printbuf_reset(&err);
928
929                 ret = bch2_sb_validate(&ca->disk_sb, &err, WRITE);
930                 if (ret) {
931                         bch2_fs_inconsistent(c, "sb invalid before write: %s", err.buf);
932                         percpu_ref_put(&ca->io_ref);
933                         goto out;
934                 }
935         }
936
937         if (c->opts.nochanges)
938                 goto out;
939
940         /*
941          * Defer writing the superblock until filesystem initialization is
942          * complete - don't write out a partly initialized superblock:
943          */
944         if (!BCH_SB_INITIALIZED(c->disk_sb.sb))
945                 goto out;
946
947         for_each_online_member(c, ca) {
948                 __set_bit(ca->dev_idx, sb_written.d);
949                 ca->sb_write_error = 0;
950         }
951
952         for_each_online_member(c, ca)
953                 read_back_super(c, ca);
954         closure_sync(cl);
955
956         for_each_online_member(c, ca) {
957                 if (ca->sb_write_error)
958                         continue;
959
960                 if (le64_to_cpu(ca->sb_read_scratch->seq) < ca->disk_sb.seq) {
961                         bch2_fs_fatal_error(c,
962                                 "Superblock write was silently dropped! (seq %llu expected %llu)",
963                                 le64_to_cpu(ca->sb_read_scratch->seq),
964                                 ca->disk_sb.seq);
965                         percpu_ref_put(&ca->io_ref);
966                         ret = -BCH_ERR_erofs_sb_err;
967                         goto out;
968                 }
969
970                 if (le64_to_cpu(ca->sb_read_scratch->seq) > ca->disk_sb.seq) {
971                         bch2_fs_fatal_error(c,
972                                 "Superblock modified by another process (seq %llu expected %llu)",
973                                 le64_to_cpu(ca->sb_read_scratch->seq),
974                                 ca->disk_sb.seq);
975                         percpu_ref_put(&ca->io_ref);
976                         ret = -BCH_ERR_erofs_sb_err;
977                         goto out;
978                 }
979         }
980
981         do {
982                 wrote = false;
983                 for_each_online_member(c, ca)
984                         if (!ca->sb_write_error &&
985                             sb < ca->disk_sb.sb->layout.nr_superblocks) {
986                                 write_one_super(c, ca, sb);
987                                 wrote = true;
988                         }
989                 closure_sync(cl);
990                 sb++;
991         } while (wrote);
992
993         for_each_online_member(c, ca) {
994                 if (ca->sb_write_error)
995                         __clear_bit(ca->dev_idx, sb_written.d);
996                 else
997                         ca->disk_sb.seq = le64_to_cpu(ca->disk_sb.sb->seq);
998         }
999
1000         nr_wrote = dev_mask_nr(&sb_written);
1001
1002         can_mount_with_written =
1003                 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1004
1005         for (unsigned i = 0; i < ARRAY_SIZE(sb_written.d); i++)
1006                 sb_written.d[i] = ~sb_written.d[i];
1007
1008         can_mount_without_written =
1009                 bch2_have_enough_devs(c, sb_written, degraded_flags, false);
1010
1011         /*
1012          * If we would be able to mount _without_ the devices we successfully
1013          * wrote superblocks to, we weren't able to write to enough devices:
1014          *
1015          * Exception: if we can mount without the successes because we haven't
1016          * written anything (new filesystem), we continue if we'd be able to
1017          * mount with the devices we did successfully write to:
1018          */
1019         if (bch2_fs_fatal_err_on(!nr_wrote ||
1020                                  !can_mount_with_written ||
1021                                  (can_mount_without_written &&
1022                                   !can_mount_with_written), c,
1023                 "Unable to write superblock to sufficient devices (from %ps)",
1024                 (void *) _RET_IP_))
1025                 ret = -1;
1026 out:
1027         /* Make new options visible after they're persistent: */
1028         bch2_sb_update(c);
1029         printbuf_exit(&err);
1030         return ret;
1031 }
1032
1033 void __bch2_check_set_feature(struct bch_fs *c, unsigned feat)
1034 {
1035         mutex_lock(&c->sb_lock);
1036         if (!(c->sb.features & (1ULL << feat))) {
1037                 c->disk_sb.sb->features[0] |= cpu_to_le64(1ULL << feat);
1038
1039                 bch2_write_super(c);
1040         }
1041         mutex_unlock(&c->sb_lock);
1042 }
1043
1044 /* Downgrade if superblock is at a higher version than currently supported: */
1045 void bch2_sb_maybe_downgrade(struct bch_fs *c)
1046 {
1047         lockdep_assert_held(&c->sb_lock);
1048
1049         /*
1050          * Downgrade, if superblock is at a higher version than currently
1051          * supported:
1052          */
1053         if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) > bcachefs_metadata_version_current)
1054                 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
1055         if (c->sb.version > bcachefs_metadata_version_current)
1056                 c->disk_sb.sb->version = cpu_to_le16(bcachefs_metadata_version_current);
1057         if (c->sb.version_min > bcachefs_metadata_version_current)
1058                 c->disk_sb.sb->version_min = cpu_to_le16(bcachefs_metadata_version_current);
1059         c->disk_sb.sb->compat[0] &= cpu_to_le64((1ULL << BCH_COMPAT_NR) - 1);
1060 }
1061
1062 void bch2_sb_upgrade(struct bch_fs *c, unsigned new_version)
1063 {
1064         lockdep_assert_held(&c->sb_lock);
1065
1066         c->disk_sb.sb->version = cpu_to_le16(new_version);
1067         c->disk_sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
1068 }
1069
1070 static const struct bch_sb_field_ops *bch2_sb_field_ops[] = {
1071 #define x(f, nr)                                        \
1072         [BCH_SB_FIELD_##f] = &bch_sb_field_ops_##f,
1073         BCH_SB_FIELDS()
1074 #undef x
1075 };
1076
1077 static const struct bch_sb_field_ops bch2_sb_field_null_ops;
1078
1079 static const struct bch_sb_field_ops *bch2_sb_field_type_ops(unsigned type)
1080 {
1081         return likely(type < ARRAY_SIZE(bch2_sb_field_ops))
1082                 ? bch2_sb_field_ops[type]
1083                 : &bch2_sb_field_null_ops;
1084 }
1085
1086 static int bch2_sb_field_validate(struct bch_sb *sb, struct bch_sb_field *f,
1087                                   struct printbuf *err)
1088 {
1089         unsigned type = le32_to_cpu(f->type);
1090         struct printbuf field_err = PRINTBUF;
1091         const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1092         int ret;
1093
1094         ret = ops->validate ? ops->validate(sb, f, &field_err) : 0;
1095         if (ret) {
1096                 prt_printf(err, "Invalid superblock section %s: %s",
1097                            bch2_sb_fields[type], field_err.buf);
1098                 prt_newline(err);
1099                 bch2_sb_field_to_text(err, sb, f);
1100         }
1101
1102         printbuf_exit(&field_err);
1103         return ret;
1104 }
1105
1106 void bch2_sb_field_to_text(struct printbuf *out, struct bch_sb *sb,
1107                            struct bch_sb_field *f)
1108 {
1109         unsigned type = le32_to_cpu(f->type);
1110         const struct bch_sb_field_ops *ops = bch2_sb_field_type_ops(type);
1111
1112         if (!out->nr_tabstops)
1113                 printbuf_tabstop_push(out, 32);
1114
1115         if (type < BCH_SB_FIELD_NR)
1116                 prt_printf(out, "%s", bch2_sb_fields[type]);
1117         else
1118                 prt_printf(out, "(unknown field %u)", type);
1119
1120         prt_printf(out, " (size %zu):", vstruct_bytes(f));
1121         prt_newline(out);
1122
1123         if (ops->to_text) {
1124                 printbuf_indent_add(out, 2);
1125                 ops->to_text(out, sb, f);
1126                 printbuf_indent_sub(out, 2);
1127         }
1128 }
1129
1130 void bch2_sb_layout_to_text(struct printbuf *out, struct bch_sb_layout *l)
1131 {
1132         unsigned i;
1133
1134         prt_printf(out, "Type:                    %u", l->layout_type);
1135         prt_newline(out);
1136
1137         prt_str(out, "Superblock max size:     ");
1138         prt_units_u64(out, 512 << l->sb_max_size_bits);
1139         prt_newline(out);
1140
1141         prt_printf(out, "Nr superblocks:          %u", l->nr_superblocks);
1142         prt_newline(out);
1143
1144         prt_str(out, "Offsets:                 ");
1145         for (i = 0; i < l->nr_superblocks; i++) {
1146                 if (i)
1147                         prt_str(out, ", ");
1148                 prt_printf(out, "%llu", le64_to_cpu(l->sb_offset[i]));
1149         }
1150         prt_newline(out);
1151 }
1152
1153 void bch2_sb_to_text(struct printbuf *out, struct bch_sb *sb,
1154                      bool print_layout, unsigned fields)
1155 {
1156         u64 fields_have = 0;
1157         unsigned nr_devices = 0;
1158
1159         if (!out->nr_tabstops)
1160                 printbuf_tabstop_push(out, 44);
1161
1162         for (int i = 0; i < sb->nr_devices; i++)
1163                 nr_devices += bch2_dev_exists(sb, i);
1164
1165         prt_printf(out, "External UUID:");
1166         prt_tab(out);
1167         pr_uuid(out, sb->user_uuid.b);
1168         prt_newline(out);
1169
1170         prt_printf(out, "Internal UUID:");
1171         prt_tab(out);
1172         pr_uuid(out, sb->uuid.b);
1173         prt_newline(out);
1174
1175         prt_str(out, "Device index:");
1176         prt_tab(out);
1177         prt_printf(out, "%u", sb->dev_idx);
1178         prt_newline(out);
1179
1180         prt_str(out, "Label:");
1181         prt_tab(out);
1182         prt_printf(out, "%.*s", (int) sizeof(sb->label), sb->label);
1183         prt_newline(out);
1184
1185         prt_str(out, "Version:");
1186         prt_tab(out);
1187         bch2_version_to_text(out, le16_to_cpu(sb->version));
1188         prt_newline(out);
1189
1190         prt_str(out, "Version upgrade complete:");
1191         prt_tab(out);
1192         bch2_version_to_text(out, BCH_SB_VERSION_UPGRADE_COMPLETE(sb));
1193         prt_newline(out);
1194
1195         prt_printf(out, "Oldest version on disk:");
1196         prt_tab(out);
1197         bch2_version_to_text(out, le16_to_cpu(sb->version_min));
1198         prt_newline(out);
1199
1200         prt_printf(out, "Created:");
1201         prt_tab(out);
1202         if (sb->time_base_lo)
1203                 bch2_prt_datetime(out, div_u64(le64_to_cpu(sb->time_base_lo), NSEC_PER_SEC));
1204         else
1205                 prt_printf(out, "(not set)");
1206         prt_newline(out);
1207
1208         prt_printf(out, "Sequence number:");
1209         prt_tab(out);
1210         prt_printf(out, "%llu", le64_to_cpu(sb->seq));
1211         prt_newline(out);
1212
1213         prt_printf(out, "Superblock size:");
1214         prt_tab(out);
1215         prt_printf(out, "%zu", vstruct_bytes(sb));
1216         prt_newline(out);
1217
1218         prt_printf(out, "Clean:");
1219         prt_tab(out);
1220         prt_printf(out, "%llu", BCH_SB_CLEAN(sb));
1221         prt_newline(out);
1222
1223         prt_printf(out, "Devices:");
1224         prt_tab(out);
1225         prt_printf(out, "%u", nr_devices);
1226         prt_newline(out);
1227
1228         prt_printf(out, "Sections:");
1229         vstruct_for_each(sb, f)
1230                 fields_have |= 1 << le32_to_cpu(f->type);
1231         prt_tab(out);
1232         prt_bitflags(out, bch2_sb_fields, fields_have);
1233         prt_newline(out);
1234
1235         prt_printf(out, "Features:");
1236         prt_tab(out);
1237         prt_bitflags(out, bch2_sb_features, le64_to_cpu(sb->features[0]));
1238         prt_newline(out);
1239
1240         prt_printf(out, "Compat features:");
1241         prt_tab(out);
1242         prt_bitflags(out, bch2_sb_compat, le64_to_cpu(sb->compat[0]));
1243         prt_newline(out);
1244
1245         prt_newline(out);
1246         prt_printf(out, "Options:");
1247         prt_newline(out);
1248         printbuf_indent_add(out, 2);
1249         {
1250                 enum bch_opt_id id;
1251
1252                 for (id = 0; id < bch2_opts_nr; id++) {
1253                         const struct bch_option *opt = bch2_opt_table + id;
1254
1255                         if (opt->get_sb != BCH2_NO_SB_OPT) {
1256                                 u64 v = bch2_opt_from_sb(sb, id);
1257
1258                                 prt_printf(out, "%s:", opt->attr.name);
1259                                 prt_tab(out);
1260                                 bch2_opt_to_text(out, NULL, sb, opt, v,
1261                                                  OPT_HUMAN_READABLE|OPT_SHOW_FULL_LIST);
1262                                 prt_newline(out);
1263                         }
1264                 }
1265         }
1266
1267         printbuf_indent_sub(out, 2);
1268
1269         if (print_layout) {
1270                 prt_newline(out);
1271                 prt_printf(out, "layout:");
1272                 prt_newline(out);
1273                 printbuf_indent_add(out, 2);
1274                 bch2_sb_layout_to_text(out, &sb->layout);
1275                 printbuf_indent_sub(out, 2);
1276         }
1277
1278         vstruct_for_each(sb, f)
1279                 if (fields & (1 << le32_to_cpu(f->type))) {
1280                         prt_newline(out);
1281                         bch2_sb_field_to_text(out, sb, f);
1282                 }
1283 }