sys/vfs/hammer2/hammer2_ioctl.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 | /* * Copyright (c) 2011-2020 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@dragonflybsd.org> * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Ioctl Functions. * * WARNING! The ioctl functions which manipulate the connection state need * to be able to run without deadlock on the volume's chain lock. * Most of these functions use a separate lock. */ #include "hammer2.h" #include <sys/kern_syscall.h> static int hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_debug_dump(hammer2_inode_t *ip, u_int flags); static int hammer2_ioctl_emerg_mode(hammer2_inode_t *ip, u_int mode); static int hammer2_ioctl_growfs(hammer2_inode_t *ip, void *data, struct ucred *cred); //static int hammer2_ioctl_inode_comp_set(hammer2_inode_t *ip, void *data); //static int hammer2_ioctl_inode_comp_rec_set(hammer2_inode_t *ip, void *data); //static int hammer2_ioctl_inode_comp_rec_set2(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_bulkfree_scan(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_destroy(hammer2_inode_t *ip, void *data); static int hammer2_ioctl_volume_list(hammer2_inode_t *ip, void *data); int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data, int fflag, struct ucred *cred) { int error; /* * Standard root cred checks, will be selectively ignored below * for ioctls that do not require root creds. */ error = caps_priv_check(cred, SYSCAP_NOVFS_IOCTL); switch(com) { case HAMMER2IOC_VERSION_GET: error = hammer2_ioctl_version_get(ip, data); break; case HAMMER2IOC_RECLUSTER: if (error == 0) error = hammer2_ioctl_recluster(ip, data); break; case HAMMER2IOC_REMOTE_SCAN: if (error == 0) error = hammer2_ioctl_remote_scan(ip, data); break; case HAMMER2IOC_REMOTE_ADD: if (error == 0) error = hammer2_ioctl_remote_add(ip, data); break; case HAMMER2IOC_REMOTE_DEL: if (error == 0) error = hammer2_ioctl_remote_del(ip, data); break; case HAMMER2IOC_REMOTE_REP: if (error == 0) error = hammer2_ioctl_remote_rep(ip, data); break; case HAMMER2IOC_SOCKET_GET: if (error == 0) error = hammer2_ioctl_socket_get(ip, data); break; case HAMMER2IOC_SOCKET_SET: if (error == 0) error = hammer2_ioctl_socket_set(ip, data); break; case HAMMER2IOC_PFS_GET: if (error == 0) error = hammer2_ioctl_pfs_get(ip, data); break; case HAMMER2IOC_PFS_LOOKUP: if (error == 0) error = hammer2_ioctl_pfs_lookup(ip, data); break; case HAMMER2IOC_PFS_CREATE: if (error == 0) error = hammer2_ioctl_pfs_create(ip, data); break; case HAMMER2IOC_PFS_DELETE: if (error == 0) error = hammer2_ioctl_pfs_delete(ip, data); break; case HAMMER2IOC_PFS_SNAPSHOT: if (error == 0) error = hammer2_ioctl_pfs_snapshot(ip, data); break; case HAMMER2IOC_INODE_GET: error = hammer2_ioctl_inode_get(ip, data); break; case HAMMER2IOC_INODE_SET: if (error == 0) error = hammer2_ioctl_inode_set(ip, data); break; case HAMMER2IOC_BULKFREE_SCAN: error = hammer2_ioctl_bulkfree_scan(ip, data); break; case HAMMER2IOC_BULKFREE_ASYNC: error = hammer2_ioctl_bulkfree_scan(ip, NULL); break; case HAMMER2IOC_DESTROY: if (error == 0) error = hammer2_ioctl_destroy(ip, data); break; case HAMMER2IOC_DEBUG_DUMP: error = hammer2_ioctl_debug_dump(ip, *(u_int *)data); break; case HAMMER2IOC_EMERG_MODE: if (error == 0) error = hammer2_ioctl_emerg_mode(ip, *(u_int *)data); break; case HAMMER2IOC_GROWFS: if (error == 0) error = hammer2_ioctl_growfs(ip, data, cred); break; case HAMMER2IOC_VOLUME_LIST: if (error == 0) error = hammer2_ioctl_volume_list(ip, data); break; case FIOSEEKDATA: case FIOSEEKHOLE: if (error == 0) error = EOPNOTSUPP; #if 0 /* doesn't work correctly yet */ error = vn_bmap_seekhole(ip->vp, com, (off_t *)data, cred); #endif break; default: error = EOPNOTSUPP; break; } return (error); } /* * Retrieve version and basic info */ static int hammer2_ioctl_version_get(hammer2_inode_t *ip, void *data) { hammer2_ioc_version_t *version = data; hammer2_dev_t *hmp; hmp = ip->pmp->pfs_hmps[0]; if (hmp) version->version = hmp->voldata.version; else version->version = -1; return 0; } static int hammer2_ioctl_recluster(hammer2_inode_t *ip, void *data) { hammer2_ioc_recluster_t *recl = data; struct vnode *vproot; struct file *fp; hammer2_cluster_t *cluster; int error; fp = holdfp(curthread, recl->fd, -1); if (fp) { error = VFS_ROOT(ip->pmp->mp, &vproot); if (error == 0) { cluster = &ip->pmp->iroot->cluster; if (cluster->focus != NULL) { hammer2_cluster_reconnect(cluster->focus->hmp, fp); error = 0; } else if (cluster->nchains == 1 && cluster->array[0].chain != NULL) { hammer2_cluster_reconnect( cluster->array[0].chain->hmp, fp); error = 0; } else { kprintf("hammer2: recluster: " "nchains=%d focus=%p\n", cluster->nchains, cluster->focus); error = EINVAL; } vput(vproot); } } else { error = EINVAL; } return error; } /* * Retrieve information about a remote */ static int hammer2_ioctl_remote_scan(hammer2_inode_t *ip, void *data) { hammer2_dev_t *hmp; hammer2_ioc_remote_t *remote = data; int copyid = remote->copyid; hmp = ip->pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT) return (EINVAL); hammer2_voldata_lock(hmp); remote->copy1 = hmp->voldata.copyinfo[copyid]; hammer2_voldata_unlock(hmp); /* * Adjust nextid (GET only) */ while (++copyid < HAMMER2_COPYID_COUNT && hmp->voldata.copyinfo[copyid].copyid == 0) { ; } if (copyid == HAMMER2_COPYID_COUNT) remote->nextid = -1; else remote->nextid = copyid; return(0); } /* * Add new remote entry */ static int hammer2_ioctl_remote_add(hammer2_inode_t *ip, void *data) { hammer2_ioc_remote_t *remote = data; hammer2_pfs_t *pmp = ip->pmp; hammer2_dev_t *hmp; int copyid = remote->copyid; int error = 0; hmp = pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); if (copyid >= HAMMER2_COPYID_COUNT) return (EINVAL); hammer2_voldata_lock(hmp); if (copyid < 0) { for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) { if (hmp->voldata.copyinfo[copyid].copyid == 0) break; } if (copyid == HAMMER2_COPYID_COUNT) { error = ENOSPC; goto failed; } } hammer2_voldata_modify(hmp); remote->copy1.copyid = copyid; hmp->voldata.copyinfo[copyid] = remote->copy1; hammer2_volconf_update(hmp, copyid); failed: hammer2_voldata_unlock(hmp); return (error); } /* * Delete existing remote entry */ static int hammer2_ioctl_remote_del(hammer2_inode_t *ip, void *data) { hammer2_ioc_remote_t *remote = data; hammer2_pfs_t *pmp = ip->pmp; hammer2_dev_t *hmp; int copyid = remote->copyid; int error = 0; hmp = pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); if (copyid >= HAMMER2_COPYID_COUNT) return (EINVAL); remote->copy1.path[sizeof(remote->copy1.path) - 1] = 0; hammer2_voldata_lock(hmp); if (copyid < 0) { for (copyid = 1; copyid < HAMMER2_COPYID_COUNT; ++copyid) { if (hmp->voldata.copyinfo[copyid].copyid == 0) continue; if (strcmp(remote->copy1.path, hmp->voldata.copyinfo[copyid].path) == 0) { break; } } if (copyid == HAMMER2_COPYID_COUNT) { error = ENOENT; goto failed; } } hammer2_voldata_modify(hmp); hmp->voldata.copyinfo[copyid].copyid = 0; hammer2_volconf_update(hmp, copyid); failed: hammer2_voldata_unlock(hmp); return (error); } /* * Replace existing remote entry */ static int hammer2_ioctl_remote_rep(hammer2_inode_t *ip, void *data) { hammer2_ioc_remote_t *remote = data; hammer2_dev_t *hmp; int copyid = remote->copyid; hmp = ip->pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT) return (EINVAL); hammer2_voldata_lock(hmp); hammer2_voldata_modify(hmp); /*hammer2_volconf_update(hmp, copyid);*/ hammer2_voldata_unlock(hmp); return(0); } /* * Retrieve communications socket */ static int hammer2_ioctl_socket_get(hammer2_inode_t *ip, void *data) { return (EOPNOTSUPP); } /* * Set communications socket for connection */ static int hammer2_ioctl_socket_set(hammer2_inode_t *ip, void *data) { hammer2_ioc_remote_t *remote = data; hammer2_dev_t *hmp; int copyid = remote->copyid; hmp = ip->pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); if (copyid < 0 || copyid >= HAMMER2_COPYID_COUNT) return (EINVAL); hammer2_voldata_lock(hmp); hammer2_voldata_unlock(hmp); return(0); } /* * Used to scan and retrieve PFS information. PFS's are directories under * the super-root. * * To scan PFSs pass name_key=0. The function will scan for the next * PFS and set all fields, as well as set name_next to the next key. * When no PFSs remain, name_next is set to (hammer2_key_t)-1. * * To retrieve a particular PFS by key, specify the key but note that * the ioctl will return the lowest key >= specified_key, so the caller * must verify the key. * * To retrieve the PFS associated with the file descriptor, pass * name_key set to (hammer2_key_t)-1. */ static int hammer2_ioctl_pfs_get(hammer2_inode_t *ip, void *data) { const hammer2_inode_data_t *ripdata; hammer2_dev_t *hmp; hammer2_ioc_pfs_t *pfs; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; hammer2_key_t save_key; int error; hmp = ip->pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); pfs = data; save_key = pfs->name_key; error = 0; /* * Setup */ if (save_key == (hammer2_key_t)-1) { hammer2_inode_lock(ip->pmp->iroot, 0); parent = NULL; chain = hammer2_inode_chain(ip->pmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); } else { hammer2_inode_lock(hmp->spmp->iroot, 0); parent = hammer2_inode_chain(hmp->spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); chain = hammer2_chain_lookup(&parent, &key_next, pfs->name_key, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_SHARED); } /* * Locate next PFS */ while (chain) { if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) break; if (parent == NULL) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); chain = NULL; break; } chain = hammer2_chain_next(&parent, chain, &key_next, key_next, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_SHARED); } error = hammer2_error_to_errno(error); /* * Load the data being returned by the ioctl. */ if (chain && chain->error == 0) { ripdata = &chain->data->ipdata; pfs->name_key = ripdata->meta.name_key; pfs->pfs_type = ripdata->meta.pfs_type; pfs->pfs_subtype = ripdata->meta.pfs_subtype; pfs->pfs_clid = ripdata->meta.pfs_clid; pfs->pfs_fsid = ripdata->meta.pfs_fsid; KKASSERT(ripdata->meta.name_len < sizeof(pfs->name)); bcopy(ripdata->filename, pfs->name, ripdata->meta.name_len); pfs->name[ripdata->meta.name_len] = 0; ripdata = NULL; /* safety */ /* * Calculate name_next, if any. We are only accessing * chain->bref so we can ignore chain->error (if the key * is used later it will error then). */ if (parent == NULL) { pfs->name_next = (hammer2_key_t)-1; } else { chain = hammer2_chain_next(&parent, chain, &key_next, key_next, HAMMER2_KEY_MAX, &error, HAMMER2_LOOKUP_SHARED); if (chain) pfs->name_next = chain->bref.key; else pfs->name_next = (hammer2_key_t)-1; } } else { pfs->name_next = (hammer2_key_t)-1; error = ENOENT; } /* * Cleanup */ if (chain) { hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } if (save_key == (hammer2_key_t)-1) { hammer2_inode_unlock(ip->pmp->iroot); } else { hammer2_inode_unlock(hmp->spmp->iroot); } return (error); } /* * Find a specific PFS by name */ static int hammer2_ioctl_pfs_lookup(hammer2_inode_t *ip, void *data) { const hammer2_inode_data_t *ripdata; hammer2_dev_t *hmp; hammer2_ioc_pfs_t *pfs; hammer2_chain_t *parent; hammer2_chain_t *chain; hammer2_key_t key_next; hammer2_key_t lhc; int error; size_t len; hmp = ip->pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); pfs = data; error = 0; hammer2_inode_lock(hmp->spmp->iroot, HAMMER2_RESOLVE_SHARED); parent = hammer2_inode_chain(hmp->spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS | HAMMER2_RESOLVE_SHARED); pfs->name[sizeof(pfs->name) - 1] = 0; len = strlen(pfs->name); lhc = hammer2_dirhash(pfs->name, len); chain = hammer2_chain_lookup(&parent, &key_next, lhc, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_SHARED); while (chain) { if (hammer2_chain_dirent_test(chain, pfs->name, len)) break; chain = hammer2_chain_next(&parent, chain, &key_next, key_next, lhc + HAMMER2_DIRHASH_LOMASK, &error, HAMMER2_LOOKUP_SHARED); } error = hammer2_error_to_errno(error); /* * Load the data being returned by the ioctl. */ if (chain && chain->error == 0) { KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE); ripdata = &chain->data->ipdata; pfs->name_key = ripdata->meta.name_key; pfs->pfs_type = ripdata->meta.pfs_type; pfs->pfs_subtype = ripdata->meta.pfs_subtype; pfs->pfs_clid = ripdata->meta.pfs_clid; pfs->pfs_fsid = ripdata->meta.pfs_fsid; ripdata = NULL; hammer2_chain_unlock(chain); hammer2_chain_drop(chain); } else if (error == 0) { error = ENOENT; } if (parent) { hammer2_chain_unlock(parent); hammer2_chain_drop(parent); } hammer2_inode_unlock(hmp->spmp->iroot); return (error); } /* * Create a new PFS under the super-root */ static int hammer2_ioctl_pfs_create(hammer2_inode_t *ip, void *data) { hammer2_inode_data_t *nipdata; hammer2_chain_t *nchain; hammer2_dev_t *hmp; hammer2_dev_t *force_local; hammer2_ioc_pfs_t *pfs; hammer2_inode_t *nip; hammer2_tid_t mtid; int error; hmp = ip->pmp->pfs_hmps[0]; /* XXX */ if (hmp == NULL) return (EINVAL); pfs = data; nip = NULL; if (pfs->name[0] == 0) return(EINVAL); pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure 0-termination */ if (hammer2_ioctl_pfs_lookup(ip, pfs) == 0) return(EEXIST); hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH); mtid = hammer2_trans_sub(hmp->spmp); nip = hammer2_inode_create_pfs(hmp->spmp, pfs->name, strlen(pfs->name), &error); if (error == 0) { atomic_set_int(&nip->flags, HAMMER2_INODE_NOSIDEQ); hammer2_inode_modify(nip); nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS); error = hammer2_chain_modify(nchain, mtid, 0, 0); KKASSERT(error == 0); nipdata = &nchain->data->ipdata; nip->meta.pfs_type = pfs->pfs_type; nip->meta.pfs_subtype = pfs->pfs_subtype; nip->meta.pfs_clid = pfs->pfs_clid; nip->meta.pfs_fsid = pfs->pfs_fsid; nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT; /* * Set default compression and check algorithm. This * can be changed later. * * Do not allow compression on PFS's with the special name * "boot", the boot loader can't decompress (yet). */ nip->meta.comp_algo = HAMMER2_ENC_ALGO(HAMMER2_COMP_DEFAULT); nip->meta.check_algo = HAMMER2_ENC_ALGO(HAMMER2_CHECK_DEFAULT); if (strcasecmp(pfs->name, "boot") == 0) { nip->meta.comp_algo = HAMMER2_ENC_ALGO(HAMMER2_COMP_AUTOZERO); } /* * Super-root isn't mounted, fsync it */ hammer2_chain_unlock(nchain); hammer2_inode_ref(nip); hammer2_inode_unlock(nip); hammer2_inode_chain_sync(nip); hammer2_inode_chain_flush(nip, HAMMER2_XOP_INODE_STOP | HAMMER2_XOP_FSSYNC); hammer2_inode_drop(nip); /* nip is dead */ /* * We still have a ref on the chain, relock and associate * with an appropriate PFS. */ force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL; hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS); nipdata = &nchain->data->ipdata; kprintf("ADD LOCAL PFS (IOCTL): %s\n", nipdata->filename); hammer2_pfsalloc(nchain, nipdata, force_local); hammer2_chain_unlock(nchain); hammer2_chain_drop(nchain); } hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_SIDEQ); return (error); } /* * Destroy an existing PFS under the super-root */ static int hammer2_ioctl_pfs_delete(hammer2_inode_t *ip, void *data) { hammer2_ioc_pfs_t *pfs = data; hammer2_dev_t *hmp; hammer2_pfs_t *spmp; hammer2_pfs_t *pmp; hammer2_xop_unlink_t *xop; hammer2_inode_t *dip; int error; int i; /* * The PFS should be probed, so we should be able to * locate it. We only delete the PFS from the * specific H2 block device (hmp), not all of * them. We must remove the PFS from the cluster * before we can destroy it. */ hmp = ip->pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); pfs->name[sizeof(pfs->name) - 1] = 0; /* ensure termination */ lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] != hmp) continue; if (pmp->pfs_names[i] && strcmp(pmp->pfs_names[i], pfs->name) == 0) { break; } } if (i != HAMMER2_MAXCLUSTER) break; } if (pmp == NULL) { lockmgr(&hammer2_mntlk, LK_RELEASE); return ENOENT; } if (pmp->mp) { lockmgr(&hammer2_mntlk, LK_RELEASE); return EBUSY; } /* * Ok, we found the pmp and we have the index. Permanently remove * the PFS from the cluster */ kprintf("FOUND PFS %s CLINDEX %d\n", pfs->name, i); hammer2_pfsdealloc(pmp, i, 1); lockmgr(&hammer2_mntlk, LK_RELEASE); /* * Now destroy the PFS under its device using the per-device * super-root. */ spmp = hmp->spmp; dip = spmp->iroot; hammer2_trans_init(spmp, 0); hammer2_inode_lock(dip, 0); xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); hammer2_xop_setname(&xop->head, pfs->name, strlen(pfs->name)); xop->isdir = 2; xop->dopermanent = H2DOPERM_PERMANENT | H2DOPERM_FORCE; hammer2_xop_start(&xop->head, &hammer2_unlink_desc); error = hammer2_xop_collect(&xop->head, 0); hammer2_inode_unlock(dip); #if 0 if (error == 0) { ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); if (ip) { hammer2_inode_unlink_finisher(ip, NULL); hammer2_inode_unlock(ip); } } else { hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); } #endif hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); hammer2_trans_done(spmp, HAMMER2_TRANS_SIDEQ); return (hammer2_error_to_errno(error)); } static int hammer2_ioctl_pfs_snapshot(hammer2_inode_t *ip, void *data) { hammer2_ioc_pfs_t *pfs = data; hammer2_dev_t *hmp; hammer2_pfs_t *pmp; hammer2_chain_t *chain; hammer2_inode_t *nip; hammer2_tid_t mtid; size_t name_len; int error; #if 0 uuid_t opfs_clid; #endif if (pfs->name[0] == 0) return(EINVAL); if (pfs->name[sizeof(pfs->name)-1] != 0) return(EINVAL); pmp = ip->pmp; ip = pmp->iroot; hmp = pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); lockmgr(&hmp->bulklk, LK_EXCLUSIVE); /* * NOSYNC is for debugging. We skip the filesystem sync and use * a normal transaction (which is less likely to stall). used for * testing filesystem consistency. * * In normal mode we sync the filesystem and use a flush transaction. */ if (pfs->pfs_flags & HAMMER2_PFSFLAGS_NOSYNC) { hammer2_trans_init(pmp, 0); } else { hammer2_vfs_sync(pmp->mp, MNT_WAIT); hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH); } mtid = hammer2_trans_sub(pmp); hammer2_inode_lock(ip, 0); hammer2_inode_modify(ip); ip->meta.pfs_lsnap_tid = mtid; /* XXX cluster it! */ chain = hammer2_inode_chain(ip, 0, HAMMER2_RESOLVE_ALWAYS); name_len = strlen(pfs->name); hmp = chain->hmp; /* * Create the snapshot directory under the super-root * * Set PFS type, generate a unique filesystem id, and generate * a cluster id. Use the same clid when snapshotting a PFS root, * which theoretically allows the snapshot to be used as part of * the same cluster (perhaps as a cache). * * Note that pfs_lsnap_tid must be set in the snapshot as well, * ensuring that any nocrc/nocomp file data modifications force * a copy-on-write. * * Copy the (flushed) blockref array. Theoretically we could use * chain_duplicate() but it becomes difficult to disentangle * the shared core so for now just brute-force it. */ hammer2_chain_unlock(chain); nip = hammer2_inode_create_pfs(hmp->spmp, pfs->name, name_len, &error); hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS); if (nip) { hammer2_dev_t *force_local; hammer2_chain_t *nchain; hammer2_inode_data_t *wipdata; hammer2_tid_t starting_inum; atomic_set_int(&nip->flags, HAMMER2_INODE_NOSIDEQ); hammer2_inode_modify(nip); nchain = hammer2_inode_chain(nip, 0, HAMMER2_RESOLVE_ALWAYS); error = hammer2_chain_modify(nchain, mtid, 0, 0); KKASSERT(error == 0); wipdata = &nchain->data->ipdata; starting_inum = ip->pmp->inode_tid + 1; nip->meta.pfs_inum = starting_inum; nip->meta.pfs_type = HAMMER2_PFSTYPE_MASTER; nip->meta.pfs_subtype = HAMMER2_PFSSUBTYPE_SNAPSHOT; nip->meta.op_flags |= HAMMER2_OPFLAG_PFSROOT; nip->meta.pfs_lsnap_tid = mtid; nchain->bref.embed.stats = chain->bref.embed.stats; kern_uuidgen(&nip->meta.pfs_fsid, 1); #if 0 /* * Give the snapshot its own private cluster id. As a * snapshot no further synchronization with the original * cluster will be done. */ if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY) nip->meta.pfs_clid = opfs_clid; else kern_uuidgen(&nip->meta.pfs_clid, 1); #endif kern_uuidgen(&nip->meta.pfs_clid, 1); nchain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT; /* XXX hack blockset copy */ /* XXX doesn't work with real cluster */ wipdata->meta = nip->meta; hammer2_spin_ex(&pmp->blockset_spin); wipdata->u.blockset = pmp->pfs_iroot_blocksets[0]; hammer2_spin_unex(&pmp->blockset_spin); KKASSERT(wipdata == &nchain->data->ipdata); hammer2_chain_unlock(nchain); hammer2_inode_ref(nip); hammer2_inode_unlock(nip); hammer2_inode_chain_sync(nip); hammer2_inode_chain_flush(nip, HAMMER2_XOP_INODE_STOP | HAMMER2_XOP_FSSYNC); /* XXX | HAMMER2_XOP_VOLHDR */ hammer2_inode_drop(nip); /* nip is dead */ force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL; hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS); wipdata = &nchain->data->ipdata; kprintf("SNAPSHOT LOCAL PFS (IOCTL): %s\n", wipdata->filename); hammer2_pfsalloc(nchain, wipdata, force_local); nchain->pmp->inode_tid = starting_inum; hammer2_chain_unlock(nchain); hammer2_chain_drop(nchain); } hammer2_chain_unlock(chain); hammer2_chain_drop(chain); hammer2_inode_unlock(ip); if (pfs->pfs_flags & HAMMER2_PFSFLAGS_NOSYNC) { hammer2_trans_done(pmp, 0); } else { hammer2_trans_done(pmp, HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_SIDEQ); } lockmgr(&hmp->bulklk, LK_RELEASE); return (hammer2_error_to_errno(error)); } /* * Retrieve the raw inode structure, non-inclusive of node-specific data. */ static int hammer2_ioctl_inode_get(hammer2_inode_t *ip, void *data) { hammer2_ioc_inode_t *ino = data; hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED); ino->data_count = hammer2_inode_data_count(ip); ino->inode_count = hammer2_inode_inode_count(ip); bzero(&ino->ip_data, sizeof(ino->ip_data)); ino->ip_data.meta = ip->meta; hammer2_inode_unlock(ip); return 0; } /* * Set various parameters in an inode which cannot be set through * normal filesystem VNOPS. */ static int hammer2_ioctl_inode_set(hammer2_inode_t *ip, void *data) { hammer2_ioc_inode_t *ino = data; hammer2_trans_init(ip->pmp, 0); hammer2_inode_lock(ip, 0); if ((ino->flags & HAMMER2IOC_INODE_FLAG_CHECK) && ip->meta.check_algo != ino->ip_data.meta.check_algo) { hammer2_inode_modify(ip); ip->meta.check_algo = ino->ip_data.meta.check_algo; } if ((ino->flags & HAMMER2IOC_INODE_FLAG_COMP) && ip->meta.comp_algo != ino->ip_data.meta.comp_algo) { hammer2_inode_modify(ip); ip->meta.comp_algo = ino->ip_data.meta.comp_algo; } /* Ignore these flags for now...*/ if ((ino->flags & HAMMER2IOC_INODE_FLAG_IQUOTA) && ip->meta.inode_quota != ino->ip_data.meta.inode_quota) { hammer2_inode_modify(ip); ip->meta.inode_quota = ino->ip_data.meta.inode_quota; } if ((ino->flags & HAMMER2IOC_INODE_FLAG_DQUOTA) && ip->meta.data_quota != ino->ip_data.meta.data_quota) { hammer2_inode_modify(ip); ip->meta.data_quota = ino->ip_data.meta.data_quota; } if ((ino->flags & HAMMER2IOC_INODE_FLAG_COPIES) && ip->meta.ncopies != ino->ip_data.meta.ncopies) { hammer2_inode_modify(ip); ip->meta.ncopies = ino->ip_data.meta.ncopies; } hammer2_inode_unlock(ip); hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ); return (0); } static int hammer2_ioctl_debug_dump(hammer2_inode_t *ip, u_int flags) { hammer2_chain_t *chain; int count = 100000; int i; for (i = 0; i < ip->cluster.nchains; ++i) { chain = ip->cluster.array[i].chain; if (chain == NULL) continue; kprintf("cluster #%d\n", i); hammer2_dump_chain(chain, 0, 0, &count, 'i', flags); } return 0; } /* * Turn on or off emergency mode on a filesystem. */ static int hammer2_ioctl_emerg_mode(hammer2_inode_t *ip, u_int mode) { hammer2_pfs_t *pmp; hammer2_dev_t *hmp; int i; pmp = ip->pmp; if (mode) { kprintf("hammer2: WARNING: Emergency mode enabled\n"); atomic_set_int(&pmp->flags, HAMMER2_PMPF_EMERG); } else { kprintf("hammer2: WARNING: Emergency mode disabled\n"); atomic_clear_int(&pmp->flags, HAMMER2_PMPF_EMERG); } for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { hmp = pmp->pfs_hmps[i]; if (hmp == NULL) continue; if (mode) atomic_set_int(&hmp->hflags, HMNT2_EMERG); else atomic_clear_int(&hmp->hflags, HMNT2_EMERG); } return 0; } /* * Do a bulkfree scan on media related to the PFS. This routine will * flush all PFSs associated with the media before doing the bulkfree * scan. * * This version can only run on non-clustered media. A new ioctl or a * temporary mount of @LOCAL will be needed to run on clustered media. */ static int hammer2_ioctl_bulkfree_scan(hammer2_inode_t *ip, void *data) { hammer2_ioc_bulkfree_t *bfi = data; hammer2_dev_t *hmp; hammer2_pfs_t *pmp; hammer2_chain_t *vchain; int error; int didsnap; pmp = ip->pmp; ip = pmp->iroot; hmp = pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); if (bfi == NULL) return (EINVAL); /* * Bulkfree has to be serialized to guarantee at least one sync * inbetween bulkfrees. */ error = lockmgr(&hmp->bflock, LK_EXCLUSIVE | LK_PCATCH); if (error) return error; /* * Sync all mounts related to the media */ lockmgr(&hammer2_mntlk, LK_EXCLUSIVE); TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) { int etmp; int i; for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) { if (pmp->pfs_hmps[i] != hmp) continue; etmp = hammer2_vfs_sync_pmp(pmp, MNT_WAIT); if (etmp && (error == 0 || error == ENOSPC)) error = etmp; break; } } lockmgr(&hammer2_mntlk, LK_RELEASE); if (error && error != ENOSPC) goto failed; /* * If we have an ENOSPC error we have to bulkfree on the live * topology. Otherwise we can bulkfree on a snapshot. */ if (error) { kprintf("hammer2: WARNING! Bulkfree forced to use live " "topology due to ENOSPC\n"); vchain = &hmp->vchain; hammer2_chain_ref(vchain); didsnap = 0; } else { vchain = hammer2_chain_bulksnap(hmp); didsnap = 1; } /* * Normal bulkfree operations do not require a transaction because * they operate on a snapshot, and so can run concurrently with * any operation except another bulkfree. * * If we are running bulkfree on the live topology we have to be * in a FLUSH transaction. */ if (didsnap == 0) hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH); if (bfi) { hammer2_thr_freeze(&hmp->bfthr); error = hammer2_bulkfree_pass(hmp, vchain, bfi); hammer2_thr_unfreeze(&hmp->bfthr); } if (didsnap) { hammer2_chain_bulkdrop(vchain); } else { hammer2_chain_drop(vchain); hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_SIDEQ); } error = hammer2_error_to_errno(error); failed: lockmgr(&hmp->bflock, LK_RELEASE); return error; } /* * Unconditionally delete meta-data in a hammer2 filesystem */ static int hammer2_ioctl_destroy(hammer2_inode_t *ip, void *data) { hammer2_ioc_destroy_t *iocd = data; hammer2_pfs_t *pmp = ip->pmp; int error; if (pmp->ronly) { error = EROFS; return error; } switch(iocd->cmd) { case HAMMER2_DELETE_FILE: /* * Destroy a bad directory entry by name. Caller must * pass the directory as fd. */ { hammer2_xop_unlink_t *xop; if (iocd->path[sizeof(iocd->path)-1]) { error = EINVAL; break; } if (ip->meta.type != HAMMER2_OBJTYPE_DIRECTORY) { error = EINVAL; break; } hammer2_pfs_memory_wait(pmp); hammer2_trans_init(pmp, 0); hammer2_inode_lock(ip, 0); xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING); hammer2_xop_setname(&xop->head, iocd->path, strlen(iocd->path)); xop->isdir = -1; xop->dopermanent = H2DOPERM_PERMANENT | H2DOPERM_FORCE | H2DOPERM_IGNINO; hammer2_xop_start(&xop->head, &hammer2_unlink_desc); error = hammer2_xop_collect(&xop->head, 0); error = hammer2_error_to_errno(error); hammer2_inode_unlock(ip); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); hammer2_trans_done(pmp, HAMMER2_TRANS_SIDEQ); } break; case HAMMER2_DELETE_INUM: /* * Destroy a bad inode by inode number. */ { hammer2_xop_lookup_t *xop; if (iocd->inum < 1) { error = EINVAL; break; } hammer2_pfs_memory_wait(pmp); hammer2_trans_init(pmp, 0); xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING); xop->lhc = iocd->inum; hammer2_xop_start(&xop->head, &hammer2_delete_desc); error = hammer2_xop_collect(&xop->head, 0); error = hammer2_error_to_errno(error); hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); hammer2_trans_done(pmp, HAMMER2_TRANS_SIDEQ); } break; default: error = EINVAL; break; } return error; } /* * Grow a filesystem into its partition size */ static int hammer2_ioctl_growfs(hammer2_inode_t *ip, void *data, struct ucred *cred) { hammer2_ioc_growfs_t *grow = data; hammer2_dev_t *hmp; hammer2_off_t size, delta; hammer2_tid_t mtid; struct partinfo part; struct vattr_lite va; struct buf *bp; int error; int i; hmp = ip->pmp->pfs_hmps[0]; if (hmp->nvolumes > 1) { kprintf("hammer2: growfs currently unsupported " "with multiple volumes\n"); return EOPNOTSUPP; } KKASSERT(hmp->total_size == hmp->voldata.volu_size); /* * Extract from disklabel */ if (VOP_IOCTL(hmp->devvp, DIOCGPART, (void *)&part, 0, cred, NULL) == 0) { size = part.media_size; kprintf("hammer2: growfs partition-auto to %016jx\n", (intmax_t)size); } else if (VOP_GETATTR_LITE(hmp->devvp, &va) == 0) { size = va.va_size; kprintf("hammer2: growfs fstat-auto to %016jx\n", (intmax_t)size); } else { return EINVAL; } /* * Expand to devvp size unless specified. */ grow->modified = 0; if (grow->size == 0) { grow->size = size; } else if (grow->size > size) { kprintf("hammer2: growfs size %016jx exceeds device size " "%016jx\n", (intmax_t)grow->size, (intmax_t)size); return EINVAL; } /* * This is typically ~8MB alignment to avoid edge cases accessing * reserved blocks at the base of each 2GB zone. */ grow->size &= ~HAMMER2_VOLUME_ALIGNMASK64; delta = grow->size - hmp->voldata.volu_size; /* * Maximum allowed size is 2^63 */ if (grow->size > 0x7FFFFFFFFFFFFFFFLU) { kprintf("hammer2: growfs failure, limit is 2^63 - 1 bytes\n"); return EINVAL; } /* * We can't shrink a filesystem */ if (grow->size < hmp->voldata.volu_size) { kprintf("hammer2: growfs failure, " "would shrink from %016jx to %016jx\n", (intmax_t)hmp->voldata.volu_size, (intmax_t)grow->size); return EINVAL; } if (delta == 0) { kprintf("hammer2: growfs - size did not change\n"); return 0; } /* * Clear any new volume header backups that we extend into. * Skip volume headers that are already part of the filesystem. */ for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) { if (i * HAMMER2_ZONE_BYTES64 < hmp->voldata.volu_size) continue; if (i * HAMMER2_ZONE_BYTES64 >= grow->size) break; kprintf("hammer2: growfs - clear volhdr %d ", i); error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64, HAMMER2_VOLUME_BYTES, &bp); if (error) { brelse(bp); kprintf("I/O error %d\n", error); return EINVAL; } bzero(bp->b_data, HAMMER2_VOLUME_BYTES); error = bwrite(bp); if (error) { kprintf("I/O error %d\n", error); return EINVAL; } kprintf("\n"); } hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH); mtid = hammer2_trans_sub(hmp->spmp); kprintf("hammer2: growfs - expand by %016jx to %016jx mtid %016jx\n", (intmax_t)delta, (intmax_t)grow->size, (intmax_t)mtid); hammer2_voldata_lock(hmp); hammer2_voldata_modify(hmp); /* * NOTE: Just adjusting total_size for a single-volume filesystem * or for the last volume in a multi-volume filesystem, is * fine. But we can't grow any other partition in a multi-volume * filesystem. For now we just punt (at the top) on any * multi-volume filesystem. */ hmp->voldata.volu_size = grow->size; hmp->voldata.total_size += delta; hmp->voldata.allocator_size += delta; hmp->voldata.allocator_free += delta; hmp->total_size += delta; hmp->volumes[0].size += delta; /* note: indexes first (only) volume */ hammer2_voldata_unlock(hmp); hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH | HAMMER2_TRANS_SIDEQ); grow->modified = 1; /* * Flush the mess right here and now. We could just let the * filesystem syncer do it, but this was a sensitive operation * so don't take any chances. */ hammer2_vfs_sync(ip->pmp->mp, MNT_WAIT); return 0; } /* * Get a list of volumes. */ static int hammer2_ioctl_volume_list(hammer2_inode_t *ip, void *data) { hammer2_ioc_volume_list_t *vollist = data; hammer2_ioc_volume_t entry; hammer2_volume_t *vol; hammer2_dev_t *hmp; hammer2_pfs_t *pmp; int i, error = 0, cnt = 0; pmp = ip->pmp; hmp = pmp->pfs_hmps[0]; if (hmp == NULL) return (EINVAL); hammer2_voldata_lock(hmp); for (i = 0; i < hmp->nvolumes; ++i) { if (cnt >= vollist->nvolumes) break; vol = &hmp->volumes[i]; bzero(&entry, sizeof(entry)); /* copy hammer2_volume_t fields */ entry.id = vol->id; bcopy(vol->dev->path, entry.path, sizeof(entry.path)); entry.offset = vol->offset; entry.size = vol->size; error = copyout(&entry, &vollist->volumes[cnt], sizeof(entry)); if (error) goto failed; cnt++; } vollist->nvolumes = cnt; vollist->version = hmp->voldata.version; bcopy(pmp->pfs_names[0], vollist->pfs_name, sizeof(vollist->pfs_name)); failed: hammer2_voldata_unlock(hmp); return error; } |