1 // SPDX-License-Identifier: GPL-2.0 2 #include "bcachefs.h" 3 #include "disk_groups.h" 4 #include "sb-members.h" 5 #include "super-io.h" 6 7 #include <linux/sort.h> 8 9 static int group_cmp(const void *_l, const void *_r) 10 { 11 const struct bch_disk_group *l = _l; 12 const struct bch_disk_group *r = _r; 13 14 return ((BCH_GROUP_DELETED(l) > BCH_GROUP_DELETED(r)) - 15 (BCH_GROUP_DELETED(l) < BCH_GROUP_DELETED(r))) ?: 16 ((BCH_GROUP_PARENT(l) > BCH_GROUP_PARENT(r)) - 17 (BCH_GROUP_PARENT(l) < BCH_GROUP_PARENT(r))) ?: 18 strncmp(l->label, r->label, sizeof(l->label)); 19 } 20 21 static int bch2_sb_disk_groups_validate(struct bch_sb *sb, 22 struct bch_sb_field *f, 23 struct printbuf *err) 24 { 25 struct bch_sb_field_disk_groups *groups = 26 field_to_type(f, disk_groups); 27 struct bch_disk_group *g, *sorted = NULL; 28 unsigned nr_groups = disk_groups_nr(groups); 29 unsigned i, len; 30 int ret = 0; 31 32 for (i = 0; i < sb->nr_devices; i++) { 33 struct bch_member m = bch2_sb_member_get(sb, i); 34 unsigned group_id; 35 36 if (!BCH_MEMBER_GROUP(&m)) 37 continue; 38 39 group_id = BCH_MEMBER_GROUP(&m) - 1; 40 41 if (group_id >= nr_groups) { 42 prt_printf(err, "disk %u has invalid label %u (have %u)", 43 i, group_id, nr_groups); 44 return -BCH_ERR_invalid_sb_disk_groups; 45 } 46 47 if (BCH_GROUP_DELETED(&groups->entries[group_id])) { 48 prt_printf(err, "disk %u has deleted label %u", i, group_id); 49 return -BCH_ERR_invalid_sb_disk_groups; 50 } 51 } 52 53 if (!nr_groups) 54 return 0; 55 56 for (i = 0; i < nr_groups; i++) { 57 g = groups->entries + i; 58 59 if (BCH_GROUP_DELETED(g)) 60 continue; 61 62 len = strnlen(g->label, sizeof(g->label)); 63 if (!len) { 64 prt_printf(err, "label %u empty", i); 65 return -BCH_ERR_invalid_sb_disk_groups; 66 } 67 } 68 69 sorted = kmalloc_array(nr_groups, sizeof(*sorted), GFP_KERNEL); 70 if (!sorted) 71 return -BCH_ERR_ENOMEM_disk_groups_validate; 72 73 memcpy(sorted, groups->entries, nr_groups * sizeof(*sorted)); 74 sort(sorted, nr_groups, sizeof(*sorted), group_cmp, NULL); 75 76 for (g = sorted; g + 1 < sorted + nr_groups; g++) 77 if (!BCH_GROUP_DELETED(g) && 78 !group_cmp(&g[0], &g[1])) { 79 prt_printf(err, "duplicate label %llu.%.*s", 80 BCH_GROUP_PARENT(g), 81 (int) sizeof(g->label), g->label); 82 ret = -BCH_ERR_invalid_sb_disk_groups; 83 goto err; 84 } 85 err: 86 kfree(sorted); 87 return ret; 88 } 89 90 void bch2_disk_groups_to_text(struct printbuf *out, struct bch_fs *c) 91 { 92 struct bch_disk_groups_cpu *g; 93 struct bch_dev *ca; 94 int i; 95 unsigned iter; 96 97 out->atomic++; 98 rcu_read_lock(); 99 100 g = rcu_dereference(c->disk_groups); 101 if (!g) 102 goto out; 103 104 for (i = 0; i < g->nr; i++) { 105 if (i) 106 prt_printf(out, " "); 107 108 if (g->entries[i].deleted) { 109 prt_printf(out, "[deleted]"); 110 continue; 111 } 112 113 prt_printf(out, "[parent %d devs", g->entries[i].parent); 114 for_each_member_device_rcu(ca, c, iter, &g->entries[i].devs) 115 prt_printf(out, " %s", ca->name); 116 prt_printf(out, "]"); 117 } 118 119 out: 120 rcu_read_unlock(); 121 out->atomic--; 122 } 123 124 static void bch2_sb_disk_groups_to_text(struct printbuf *out, 125 struct bch_sb *sb, 126 struct bch_sb_field *f) 127 { 128 struct bch_sb_field_disk_groups *groups = 129 field_to_type(f, disk_groups); 130 struct bch_disk_group *g; 131 unsigned nr_groups = disk_groups_nr(groups); 132 133 for (g = groups->entries; 134 g < groups->entries + nr_groups; 135 g++) { 136 if (g != groups->entries) 137 prt_printf(out, " "); 138 139 if (BCH_GROUP_DELETED(g)) 140 prt_printf(out, "[deleted]"); 141 else 142 prt_printf(out, "[parent %llu name %s]", 143 BCH_GROUP_PARENT(g), g->label); 144 } 145 } 146 147 const struct bch_sb_field_ops bch_sb_field_ops_disk_groups = { 148 .validate = bch2_sb_disk_groups_validate, 149 .to_text = bch2_sb_disk_groups_to_text 150 }; 151 152 int bch2_sb_disk_groups_to_cpu(struct bch_fs *c) 153 { 154 struct bch_sb_field_disk_groups *groups; 155 struct bch_disk_groups_cpu *cpu_g, *old_g; 156 unsigned i, g, nr_groups; 157 158 lockdep_assert_held(&c->sb_lock); 159 160 groups = bch2_sb_field_get(c->disk_sb.sb, disk_groups); 161 nr_groups = disk_groups_nr(groups); 162 163 if (!groups) 164 return 0; 165 166 cpu_g = kzalloc(struct_size(cpu_g, entries, nr_groups), GFP_KERNEL); 167 if (!cpu_g) 168 return -BCH_ERR_ENOMEM_disk_groups_to_cpu; 169 170 cpu_g->nr = nr_groups; 171 172 for (i = 0; i < nr_groups; i++) { 173 struct bch_disk_group *src = &groups->entries[i]; 174 struct bch_disk_group_cpu *dst = &cpu_g->entries[i]; 175 176 dst->deleted = BCH_GROUP_DELETED(src); 177 dst->parent = BCH_GROUP_PARENT(src); 178 } 179 180 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 181 struct bch_member m = bch2_sb_member_get(c->disk_sb.sb, i); 182 struct bch_disk_group_cpu *dst; 183 184 if (!bch2_member_exists(&m)) 185 continue; 186 187 g = BCH_MEMBER_GROUP(&m); 188 while (g) { 189 dst = &cpu_g->entries[g - 1]; 190 __set_bit(i, dst->devs.d); 191 g = dst->parent; 192 } 193 } 194 195 old_g = rcu_dereference_protected(c->disk_groups, 196 lockdep_is_held(&c->sb_lock)); 197 rcu_assign_pointer(c->disk_groups, cpu_g); 198 if (old_g) 199 kfree_rcu(old_g, rcu); 200 201 return 0; 202 } 203 204 const struct bch_devs_mask *bch2_target_to_mask(struct bch_fs *c, unsigned target) 205 { 206 struct target t = target_decode(target); 207 struct bch_devs_mask *devs; 208 209 rcu_read_lock(); 210 211 switch (t.type) { 212 case TARGET_NULL: 213 devs = NULL; 214 break; 215 case TARGET_DEV: { 216 struct bch_dev *ca = t.dev < c->sb.nr_devices 217 ? rcu_dereference(c->devs[t.dev]) 218 : NULL; 219 devs = ca ? &ca->self : NULL; 220 break; 221 } 222 case TARGET_GROUP: { 223 struct bch_disk_groups_cpu *g = rcu_dereference(c->disk_groups); 224 225 devs = g && t.group < g->nr && !g->entries[t.group].deleted 226 ? &g->entries[t.group].devs 227 : NULL; 228 break; 229 } 230 default: 231 BUG(); 232 } 233 234 rcu_read_unlock(); 235 236 return devs; 237 } 238 239 bool bch2_dev_in_target(struct bch_fs *c, unsigned dev, unsigned target) 240 { 241 struct target t = target_decode(target); 242 243 switch (t.type) { 244 case TARGET_NULL: 245 return false; 246 case TARGET_DEV: 247 return dev == t.dev; 248 case TARGET_GROUP: { 249 struct bch_disk_groups_cpu *g; 250 const struct bch_devs_mask *m; 251 bool ret; 252 253 rcu_read_lock(); 254 g = rcu_dereference(c->disk_groups); 255 m = g && t.group < g->nr && !g->entries[t.group].deleted 256 ? &g->entries[t.group].devs 257 : NULL; 258 259 ret = m ? test_bit(dev, m->d) : false; 260 rcu_read_unlock(); 261 262 return ret; 263 } 264 default: 265 BUG(); 266 } 267 } 268 269 static int __bch2_disk_group_find(struct bch_sb_field_disk_groups *groups, 270 unsigned parent, 271 const char *name, unsigned namelen) 272 { 273 unsigned i, nr_groups = disk_groups_nr(groups); 274 275 if (!namelen || namelen > BCH_SB_LABEL_SIZE) 276 return -EINVAL; 277 278 for (i = 0; i < nr_groups; i++) { 279 struct bch_disk_group *g = groups->entries + i; 280 281 if (BCH_GROUP_DELETED(g)) 282 continue; 283 284 if (!BCH_GROUP_DELETED(g) && 285 BCH_GROUP_PARENT(g) == parent && 286 strnlen(g->label, sizeof(g->label)) == namelen && 287 !memcmp(name, g->label, namelen)) 288 return i; 289 } 290 291 return -1; 292 } 293 294 static int __bch2_disk_group_add(struct bch_sb_handle *sb, unsigned parent, 295 const char *name, unsigned namelen) 296 { 297 struct bch_sb_field_disk_groups *groups = 298 bch2_sb_field_get(sb->sb, disk_groups); 299 unsigned i, nr_groups = disk_groups_nr(groups); 300 struct bch_disk_group *g; 301 302 if (!namelen || namelen > BCH_SB_LABEL_SIZE) 303 return -EINVAL; 304 305 for (i = 0; 306 i < nr_groups && !BCH_GROUP_DELETED(&groups->entries[i]); 307 i++) 308 ; 309 310 if (i == nr_groups) { 311 unsigned u64s = 312 (sizeof(struct bch_sb_field_disk_groups) + 313 sizeof(struct bch_disk_group) * (nr_groups + 1)) / 314 sizeof(u64); 315 316 groups = bch2_sb_field_resize(sb, disk_groups, u64s); 317 if (!groups) 318 return -BCH_ERR_ENOSPC_disk_label_add; 319 320 nr_groups = disk_groups_nr(groups); 321 } 322 323 BUG_ON(i >= nr_groups); 324 325 g = &groups->entries[i]; 326 327 memcpy(g->label, name, namelen); 328 if (namelen < sizeof(g->label)) 329 g->label[namelen] = '\0'; 330 SET_BCH_GROUP_DELETED(g, 0); 331 SET_BCH_GROUP_PARENT(g, parent); 332 SET_BCH_GROUP_DATA_ALLOWED(g, ~0); 333 334 return i; 335 } 336 337 int bch2_disk_path_find(struct bch_sb_handle *sb, const char *name) 338 { 339 struct bch_sb_field_disk_groups *groups = 340 bch2_sb_field_get(sb->sb, disk_groups); 341 int v = -1; 342 343 do { 344 const char *next = strchrnul(name, '.'); 345 unsigned len = next - name; 346 347 if (*next == '.') 348 next++; 349 350 v = __bch2_disk_group_find(groups, v + 1, name, len); 351 name = next; 352 } while (*name && v >= 0); 353 354 return v; 355 } 356 357 int bch2_disk_path_find_or_create(struct bch_sb_handle *sb, const char *name) 358 { 359 struct bch_sb_field_disk_groups *groups; 360 unsigned parent = 0; 361 int v = -1; 362 363 do { 364 const char *next = strchrnul(name, '.'); 365 unsigned len = next - name; 366 367 if (*next == '.') 368 next++; 369 370 groups = bch2_sb_field_get(sb->sb, disk_groups); 371 372 v = __bch2_disk_group_find(groups, parent, name, len); 373 if (v < 0) 374 v = __bch2_disk_group_add(sb, parent, name, len); 375 if (v < 0) 376 return v; 377 378 parent = v + 1; 379 name = next; 380 } while (*name && v >= 0); 381 382 return v; 383 } 384 385 void bch2_disk_path_to_text(struct printbuf *out, struct bch_sb *sb, unsigned v) 386 { 387 struct bch_sb_field_disk_groups *groups = 388 bch2_sb_field_get(sb, disk_groups); 389 struct bch_disk_group *g; 390 unsigned nr = 0; 391 u16 path[32]; 392 393 while (1) { 394 if (nr == ARRAY_SIZE(path)) 395 goto inval; 396 397 if (v >= disk_groups_nr(groups)) 398 goto inval; 399 400 g = groups->entries + v; 401 402 if (BCH_GROUP_DELETED(g)) 403 goto inval; 404 405 path[nr++] = v; 406 407 if (!BCH_GROUP_PARENT(g)) 408 break; 409 410 v = BCH_GROUP_PARENT(g) - 1; 411 } 412 413 while (nr) { 414 v = path[--nr]; 415 g = groups->entries + v; 416 417 prt_printf(out, "%.*s", (int) sizeof(g->label), g->label); 418 if (nr) 419 prt_printf(out, "."); 420 } 421 return; 422 inval: 423 prt_printf(out, "invalid label %u", v); 424 } 425 426 int __bch2_dev_group_set(struct bch_fs *c, struct bch_dev *ca, const char *name) 427 { 428 struct bch_member *mi; 429 int ret, v = -1; 430 431 if (!strlen(name) || !strcmp(name, "none")) 432 return 0; 433 434 v = bch2_disk_path_find_or_create(&c->disk_sb, name); 435 if (v < 0) 436 return v; 437 438 ret = bch2_sb_disk_groups_to_cpu(c); 439 if (ret) 440 return ret; 441 442 mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 443 SET_BCH_MEMBER_GROUP(mi, v + 1); 444 return 0; 445 } 446 447 int bch2_dev_group_set(struct bch_fs *c, struct bch_dev *ca, const char *name) 448 { 449 int ret; 450 451 mutex_lock(&c->sb_lock); 452 ret = __bch2_dev_group_set(c, ca, name) ?: 453 bch2_write_super(c); 454 mutex_unlock(&c->sb_lock); 455 456 return ret; 457 } 458 459 int bch2_opt_target_parse(struct bch_fs *c, const char *val, u64 *res, 460 struct printbuf *err) 461 { 462 struct bch_dev *ca; 463 int g; 464 465 if (!val) 466 return -EINVAL; 467 468 if (!c) 469 return 0; 470 471 if (!strlen(val) || !strcmp(val, "none")) { 472 *res = 0; 473 return 0; 474 } 475 476 /* Is it a device? */ 477 ca = bch2_dev_lookup(c, val); 478 if (!IS_ERR(ca)) { 479 *res = dev_to_target(ca->dev_idx); 480 percpu_ref_put(&ca->ref); 481 return 0; 482 } 483 484 mutex_lock(&c->sb_lock); 485 g = bch2_disk_path_find(&c->disk_sb, val); 486 mutex_unlock(&c->sb_lock); 487 488 if (g >= 0) { 489 *res = group_to_target(g); 490 return 0; 491 } 492 493 return -EINVAL; 494 } 495 496 void bch2_opt_target_to_text(struct printbuf *out, 497 struct bch_fs *c, 498 struct bch_sb *sb, 499 u64 v) 500 { 501 struct target t = target_decode(v); 502 503 switch (t.type) { 504 case TARGET_NULL: 505 prt_printf(out, "none"); 506 break; 507 case TARGET_DEV: 508 if (c) { 509 struct bch_dev *ca; 510 511 rcu_read_lock(); 512 ca = t.dev < c->sb.nr_devices 513 ? rcu_dereference(c->devs[t.dev]) 514 : NULL; 515 516 if (ca && percpu_ref_tryget(&ca->io_ref)) { 517 prt_printf(out, "/dev/%pg", ca->disk_sb.bdev); 518 percpu_ref_put(&ca->io_ref); 519 } else if (ca) { 520 prt_printf(out, "offline device %u", t.dev); 521 } else { 522 prt_printf(out, "invalid device %u", t.dev); 523 } 524 525 rcu_read_unlock(); 526 } else { 527 struct bch_member m = bch2_sb_member_get(sb, t.dev); 528 529 if (bch2_dev_exists(sb, t.dev)) { 530 prt_printf(out, "Device "); 531 pr_uuid(out, m.uuid.b); 532 prt_printf(out, " (%u)", t.dev); 533 } else { 534 prt_printf(out, "Bad device %u", t.dev); 535 } 536 } 537 break; 538 case TARGET_GROUP: 539 if (c) { 540 mutex_lock(&c->sb_lock); 541 bch2_disk_path_to_text(out, c->disk_sb.sb, t.group); 542 mutex_unlock(&c->sb_lock); 543 } else { 544 bch2_disk_path_to_text(out, sb, t.group); 545 } 546 break; 547 default: 548 BUG(); 549 } 550 } 551