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