1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1996,2008 Oracle. All rights reserved. 5 * 6 * $Id: mp_method.c,v 12.61 2008/03/13 15:21:21 mbrey Exp $ 7 */ 8 9#include "db_config.h" 10 11#include "db_int.h" 12#include "dbinc/mp.h" 13#include "dbinc/db_page.h" 14#include "dbinc/hash.h" 15 16/* 17 * __memp_env_create -- 18 * Mpool specific creation of the DB_ENV structure. 19 * 20 * PUBLIC: int __memp_env_create __P((DB_ENV *)); 21 */ 22int 23__memp_env_create(dbenv) 24 DB_ENV *dbenv; 25{ 26 /* 27 * !!! 28 * Our caller has not yet had the opportunity to reset the panic 29 * state or turn off mutex locking, and so we can neither check 30 * the panic state or acquire a mutex in the DB_ENV create path. 31 * 32 * We default to 32 8K pages. We don't default to a flat 256K, because 33 * some systems require significantly more memory to hold 32 pages than 34 * others. For example, HP-UX with POSIX pthreads needs 88 bytes for 35 * a POSIX pthread mutex and almost 200 bytes per buffer header, while 36 * Solaris needs 24 and 52 bytes for the same structures. The minimum 37 * number of hash buckets is 37. These contain a mutex also. 38 */ 39 dbenv->mp_bytes = dbenv->mp_max_bytes = 40 32 * ((8 * 1024) + sizeof(BH)) + 37 * sizeof(DB_MPOOL_HASH); 41 dbenv->mp_ncache = 1; 42 43 return (0); 44} 45 46/* 47 * __memp_env_destroy -- 48 * Mpool specific destruction of the DB_ENV structure. 49 * 50 * PUBLIC: void __memp_env_destroy __P((DB_ENV *)); 51 */ 52void 53__memp_env_destroy(dbenv) 54 DB_ENV *dbenv; 55{ 56 COMPQUIET(dbenv, NULL); 57} 58 59/* 60 * __memp_get_cachesize -- 61 * {DB_ENV,DB}->get_cachesize. 62 * 63 * PUBLIC: int __memp_get_cachesize 64 * PUBLIC: __P((DB_ENV *, u_int32_t *, u_int32_t *, int *)); 65 */ 66int 67__memp_get_cachesize(dbenv, gbytesp, bytesp, ncachep) 68 DB_ENV *dbenv; 69 u_int32_t *gbytesp, *bytesp; 70 int *ncachep; 71{ 72 ENV *env; 73 MPOOL *mp; 74 75 env = dbenv->env; 76 77 ENV_NOT_CONFIGURED(env, 78 env->mp_handle, "DB_ENV->get_cachesize", DB_INIT_MPOOL); 79 80 if (MPOOL_ON(env)) { 81 /* Cannot be set after open, no lock required to read. */ 82 mp = env->mp_handle->reginfo[0].primary; 83 if (gbytesp != NULL) 84 *gbytesp = mp->stat.st_gbytes; 85 if (bytesp != NULL) 86 *bytesp = mp->stat.st_bytes; 87 if (ncachep != NULL) 88 *ncachep = (int)mp->nreg; 89 } else { 90 if (gbytesp != NULL) 91 *gbytesp = dbenv->mp_gbytes; 92 if (bytesp != NULL) 93 *bytesp = dbenv->mp_bytes; 94 if (ncachep != NULL) 95 *ncachep = (int)dbenv->mp_ncache; 96 } 97 return (0); 98} 99 100/* 101 * __memp_set_cachesize -- 102 * {DB_ENV,DB}->set_cachesize. 103 * 104 * PUBLIC: int __memp_set_cachesize __P((DB_ENV *, u_int32_t, u_int32_t, int)); 105 */ 106int 107__memp_set_cachesize(dbenv, gbytes, bytes, arg_ncache) 108 DB_ENV *dbenv; 109 u_int32_t gbytes, bytes; 110 int arg_ncache; 111{ 112 ENV *env; 113 u_int ncache; 114 115 env = dbenv->env; 116 117 /* Normalize the cache count. */ 118 ncache = arg_ncache <= 0 ? 1 : (u_int)arg_ncache; 119 120 /* 121 * You can only store 4GB-1 in an unsigned 32-bit value, so correct for 122 * applications that specify 4GB cache sizes -- we know what they meant. 123 */ 124 if (sizeof(roff_t) == 4 && gbytes / ncache == 4 && bytes == 0) { 125 --gbytes; 126 bytes = GIGABYTE - 1; 127 } else { 128 gbytes += bytes / GIGABYTE; 129 bytes %= GIGABYTE; 130 } 131 132 /* 133 * !!! 134 * With 32-bit region offsets, individual cache regions must be smaller 135 * than 4GB. Also, cache sizes larger than 10TB would cause 32-bit 136 * wrapping in the calculation of the number of hash buckets. See 137 * __memp_open for details. 138 */ 139 if (!F_ISSET(env, ENV_OPEN_CALLED)) { 140 if (sizeof(roff_t) <= 4 && gbytes / ncache >= 4) { 141 __db_errx(env, 142 "individual cache size too large: maximum is 4GB"); 143 return (EINVAL); 144 } 145 if (gbytes / ncache > 10000) { 146 __db_errx(env, 147 "individual cache size too large: maximum is 10TB"); 148 return (EINVAL); 149 } 150 } 151 152 /* 153 * If the application requested less than 500Mb, increase the cachesize 154 * by 25% and factor in the size of the hash buckets to account for our 155 * overhead. (I'm guessing caches over 500Mb are specifically sized, 156 * that is, it's a large server and the application actually knows how 157 * much memory is available. We only document the 25% overhead number, 158 * not the hash buckets, but I don't see a reason to confuse the issue, 159 * it shouldn't matter to an application.) 160 * 161 * There is a minimum cache size, regardless. 162 */ 163 if (gbytes == 0) { 164 if (bytes < 500 * MEGABYTE) 165 bytes += (bytes / 4) + 37 * sizeof(DB_MPOOL_HASH); 166 if (bytes / ncache < DB_CACHESIZE_MIN) 167 bytes = ncache * DB_CACHESIZE_MIN; 168 } 169 170 if (F_ISSET(env, ENV_OPEN_CALLED)) 171 return (__memp_resize(env->mp_handle, gbytes, bytes)); 172 173 dbenv->mp_gbytes = gbytes; 174 dbenv->mp_bytes = bytes; 175 dbenv->mp_ncache = ncache; 176 177 return (0); 178} 179 180/* 181 * __memp_set_config -- 182 * Set the cache subsystem configuration. 183 * 184 * PUBLIC: int __memp_set_config __P((DB_ENV *, u_int32_t, int)); 185 */ 186int 187__memp_set_config(dbenv, which, on) 188 DB_ENV *dbenv; 189 u_int32_t which; 190 int on; 191{ 192 DB_MPOOL *dbmp; 193 ENV *env; 194 MPOOL *mp; 195 196 env = dbenv->env; 197 198 ENV_NOT_CONFIGURED(env, 199 env->mp_handle, "DB_ENV->memp_set_config", DB_INIT_MPOOL); 200 201 switch (which) { 202 case DB_MEMP_SUPPRESS_WRITE: 203 case DB_MEMP_SYNC_INTERRUPT: 204 if (MPOOL_ON(env)) { 205 dbmp = env->mp_handle; 206 mp = dbmp->reginfo[0].primary; 207 if (on) 208 FLD_SET(mp->config_flags, which); 209 else 210 FLD_CLR(mp->config_flags, which); 211 } 212 break; 213 default: 214 return (EINVAL); 215 } 216 return (0); 217} 218 219/* 220 * __memp_get_config -- 221 * Return the cache subsystem configuration. 222 * 223 * PUBLIC: int __memp_get_config __P((DB_ENV *, u_int32_t, int *)); 224 */ 225int 226__memp_get_config(dbenv, which, onp) 227 DB_ENV *dbenv; 228 u_int32_t which; 229 int *onp; 230{ 231 DB_MPOOL *dbmp; 232 ENV *env; 233 MPOOL *mp; 234 235 env = dbenv->env; 236 237 ENV_REQUIRES_CONFIG(env, 238 env->mp_handle, "DB_ENV->memp_get_config", DB_INIT_MPOOL); 239 240 switch (which) { 241 case DB_MEMP_SUPPRESS_WRITE: 242 case DB_MEMP_SYNC_INTERRUPT: 243 if (MPOOL_ON(env)) { 244 dbmp = env->mp_handle; 245 mp = dbmp->reginfo[0].primary; 246 *onp = FLD_ISSET(mp->config_flags, which) ? 1 : 0; 247 } else 248 *onp = 0; 249 break; 250 default: 251 return (EINVAL); 252 } 253 return (0); 254} 255 256/* 257 * PUBLIC: int __memp_get_mp_max_openfd __P((DB_ENV *, int *)); 258 */ 259int 260__memp_get_mp_max_openfd(dbenv, maxopenfdp) 261 DB_ENV *dbenv; 262 int *maxopenfdp; 263{ 264 DB_MPOOL *dbmp; 265 DB_THREAD_INFO *ip; 266 ENV *env; 267 MPOOL *mp; 268 269 env = dbenv->env; 270 271 ENV_NOT_CONFIGURED(env, 272 env->mp_handle, "DB_ENV->get_mp_max_openfd", DB_INIT_MPOOL); 273 274 if (MPOOL_ON(env)) { 275 dbmp = env->mp_handle; 276 mp = dbmp->reginfo[0].primary; 277 ENV_ENTER(env, ip); 278 MPOOL_SYSTEM_LOCK(env); 279 *maxopenfdp = mp->mp_maxopenfd; 280 MPOOL_SYSTEM_UNLOCK(env); 281 ENV_LEAVE(env, ip); 282 } else 283 *maxopenfdp = dbenv->mp_maxopenfd; 284 return (0); 285} 286 287/* 288 * __memp_set_mp_max_openfd -- 289 * Set the maximum number of open fd's when flushing the cache. 290 * PUBLIC: int __memp_set_mp_max_openfd __P((DB_ENV *, int)); 291 */ 292int 293__memp_set_mp_max_openfd(dbenv, maxopenfd) 294 DB_ENV *dbenv; 295 int maxopenfd; 296{ 297 DB_MPOOL *dbmp; 298 DB_THREAD_INFO *ip; 299 ENV *env; 300 MPOOL *mp; 301 302 env = dbenv->env; 303 304 ENV_NOT_CONFIGURED(env, 305 env->mp_handle, "DB_ENV->set_mp_max_openfd", DB_INIT_MPOOL); 306 307 if (MPOOL_ON(env)) { 308 dbmp = env->mp_handle; 309 mp = dbmp->reginfo[0].primary; 310 ENV_ENTER(env, ip); 311 MPOOL_SYSTEM_LOCK(env); 312 mp->mp_maxopenfd = maxopenfd; 313 MPOOL_SYSTEM_UNLOCK(env); 314 ENV_LEAVE(env, ip); 315 } else 316 dbenv->mp_maxopenfd = maxopenfd; 317 return (0); 318} 319 320/* 321 * PUBLIC: int __memp_get_mp_max_write __P((DB_ENV *, int *, db_timeout_t *)); 322 */ 323int 324__memp_get_mp_max_write(dbenv, maxwritep, maxwrite_sleepp) 325 DB_ENV *dbenv; 326 int *maxwritep; 327 db_timeout_t *maxwrite_sleepp; 328{ 329 DB_MPOOL *dbmp; 330 DB_THREAD_INFO *ip; 331 ENV *env; 332 MPOOL *mp; 333 334 env = dbenv->env; 335 336 ENV_NOT_CONFIGURED(env, 337 env->mp_handle, "DB_ENV->get_mp_max_write", DB_INIT_MPOOL); 338 339 if (MPOOL_ON(env)) { 340 dbmp = env->mp_handle; 341 mp = dbmp->reginfo[0].primary; 342 ENV_ENTER(env, ip); 343 MPOOL_SYSTEM_LOCK(env); 344 *maxwritep = mp->mp_maxwrite; 345 *maxwrite_sleepp = mp->mp_maxwrite_sleep; 346 MPOOL_SYSTEM_UNLOCK(env); 347 ENV_LEAVE(env, ip); 348 } else { 349 *maxwritep = dbenv->mp_maxwrite; 350 *maxwrite_sleepp = dbenv->mp_maxwrite_sleep; 351 } 352 return (0); 353} 354 355/* 356 * __memp_set_mp_max_write -- 357 * Set the maximum continuous I/O count. 358 * 359 * PUBLIC: int __memp_set_mp_max_write __P((DB_ENV *, int, db_timeout_t)); 360 */ 361int 362__memp_set_mp_max_write(dbenv, maxwrite, maxwrite_sleep) 363 DB_ENV *dbenv; 364 int maxwrite; 365 db_timeout_t maxwrite_sleep; 366{ 367 DB_MPOOL *dbmp; 368 DB_THREAD_INFO *ip; 369 ENV *env; 370 MPOOL *mp; 371 372 env = dbenv->env; 373 374 ENV_NOT_CONFIGURED(env, 375 env->mp_handle, "DB_ENV->get_mp_max_write", DB_INIT_MPOOL); 376 377 if (MPOOL_ON(env)) { 378 dbmp = env->mp_handle; 379 mp = dbmp->reginfo[0].primary; 380 ENV_ENTER(env, ip); 381 MPOOL_SYSTEM_LOCK(env); 382 mp->mp_maxwrite = maxwrite; 383 mp->mp_maxwrite_sleep = maxwrite_sleep; 384 MPOOL_SYSTEM_UNLOCK(env); 385 ENV_LEAVE(env, ip); 386 } else { 387 dbenv->mp_maxwrite = maxwrite; 388 dbenv->mp_maxwrite_sleep = maxwrite_sleep; 389 } 390 return (0); 391} 392 393/* 394 * PUBLIC: int __memp_get_mp_mmapsize __P((DB_ENV *, size_t *)); 395 */ 396int 397__memp_get_mp_mmapsize(dbenv, mp_mmapsizep) 398 DB_ENV *dbenv; 399 size_t *mp_mmapsizep; 400{ 401 DB_MPOOL *dbmp; 402 DB_THREAD_INFO *ip; 403 ENV *env; 404 MPOOL *mp; 405 406 env = dbenv->env; 407 408 ENV_NOT_CONFIGURED(env, 409 env->mp_handle, "DB_ENV->get_mp_max_mmapsize", DB_INIT_MPOOL); 410 411 if (MPOOL_ON(env)) { 412 dbmp = env->mp_handle; 413 mp = dbmp->reginfo[0].primary; 414 ENV_ENTER(env, ip); 415 MPOOL_SYSTEM_LOCK(env); 416 *mp_mmapsizep = mp->mp_mmapsize; 417 MPOOL_SYSTEM_UNLOCK(env); 418 ENV_LEAVE(env, ip); 419 } else 420 *mp_mmapsizep = dbenv->mp_mmapsize; 421 return (0); 422} 423 424/* 425 * __memp_set_mp_mmapsize -- 426 * DB_ENV->set_mp_mmapsize. 427 * 428 * PUBLIC: int __memp_set_mp_mmapsize __P((DB_ENV *, size_t)); 429 */ 430int 431__memp_set_mp_mmapsize(dbenv, mp_mmapsize) 432 DB_ENV *dbenv; 433 size_t mp_mmapsize; 434{ 435 DB_MPOOL *dbmp; 436 DB_THREAD_INFO *ip; 437 ENV *env; 438 MPOOL *mp; 439 440 env = dbenv->env; 441 442 ENV_NOT_CONFIGURED(env, 443 env->mp_handle, "DB_ENV->get_mp_max_mmapsize", DB_INIT_MPOOL); 444 445 if (MPOOL_ON(env)) { 446 dbmp = env->mp_handle; 447 mp = dbmp->reginfo[0].primary; 448 ENV_ENTER(env, ip); 449 MPOOL_SYSTEM_LOCK(env); 450 mp->mp_mmapsize = mp_mmapsize; 451 MPOOL_SYSTEM_UNLOCK(env); 452 ENV_LEAVE(env, ip); 453 } else 454 dbenv->mp_mmapsize = mp_mmapsize; 455 return (0); 456} 457 458/* 459 * __memp_nameop 460 * Remove or rename a file in the pool. 461 * 462 * PUBLIC: int __memp_nameop __P((ENV *, 463 * PUBLIC: u_int8_t *, const char *, const char *, const char *, int)); 464 * 465 * XXX 466 * Undocumented interface: DB private. 467 */ 468int 469__memp_nameop(env, fileid, newname, fullold, fullnew, inmem) 470 ENV *env; 471 u_int8_t *fileid; 472 const char *newname, *fullold, *fullnew; 473 int inmem; 474{ 475 DB_MPOOL *dbmp; 476 DB_MPOOL_HASH *hp, *nhp; 477 MPOOL *mp; 478 MPOOLFILE *mfp; 479 roff_t newname_off; 480 u_int32_t bucket; 481 int locked, ret; 482 size_t nlen; 483 void *p; 484 485#undef op_is_remove 486#define op_is_remove (newname == NULL) 487 488 COMPQUIET(bucket, 0); 489 COMPQUIET(hp, NULL); 490 COMPQUIET(newname_off, 0); 491 COMPQUIET(nlen, 0); 492 493 dbmp = NULL; 494 mfp = NULL; 495 nhp = NULL; 496 p = NULL; 497 locked = ret = 0; 498 499 if (!MPOOL_ON(env)) 500 goto fsop; 501 502 dbmp = env->mp_handle; 503 mp = dbmp->reginfo[0].primary; 504 hp = R_ADDR(dbmp->reginfo, mp->ftab); 505 506 if (!op_is_remove) { 507 nlen = strlen(newname); 508 if ((ret = __memp_alloc(dbmp, dbmp->reginfo, 509 NULL, nlen + 1, &newname_off, &p)) != 0) 510 return (ret); 511 memcpy(p, newname, nlen + 1); 512 } 513 514 /* 515 * Remove or rename a file that the mpool might know about. We assume 516 * that the fop layer has the file locked for exclusive access, so we 517 * don't worry about locking except for the mpool mutexes. Checkpoint 518 * can happen at any time, independent of file locking, so we have to 519 * do the actual unlink or rename system call while holding 520 * all affected buckets locked. 521 * 522 * If this is a rename and this is a memory file then we need 523 * to make sure that the new name does not exist. Since we 524 * are locking two buckets lock them in ascending order. 525 */ 526 if (inmem) { 527 DB_ASSERT(env, fullold != NULL); 528 hp += FNBUCKET(fullold, strlen(fullold)); 529 if (!op_is_remove) { 530 bucket = FNBUCKET(newname, nlen); 531 nhp = R_ADDR(dbmp->reginfo, mp->ftab); 532 nhp += bucket; 533 } 534 } else 535 hp += FNBUCKET(fileid, DB_FILE_ID_LEN); 536 537 if (nhp != NULL && nhp < hp) 538 MUTEX_LOCK(env, nhp->mtx_hash); 539 MUTEX_LOCK(env, hp->mtx_hash); 540 if (nhp != NULL && nhp > hp) 541 MUTEX_LOCK(env, nhp->mtx_hash); 542 locked = 1; 543 544 if (!op_is_remove && inmem) { 545 SH_TAILQ_FOREACH(mfp, &nhp->hash_bucket, q, __mpoolfile) 546 if (!mfp->deadfile && 547 mfp->no_backing_file && strcmp(newname, 548 R_ADDR(dbmp->reginfo, mfp->path_off)) == 0) 549 break; 550 if (mfp != NULL) { 551 ret = EEXIST; 552 goto err; 553 } 554 } 555 556 /* 557 * Find the file -- if mpool doesn't know about this file, that may 558 * not be an error. 559 */ 560 SH_TAILQ_FOREACH(mfp, &hp->hash_bucket, q, __mpoolfile) { 561 /* Ignore non-active files. */ 562 if (mfp->deadfile || F_ISSET(mfp, MP_TEMP)) 563 continue; 564 565 /* Try to match on fileid. */ 566 if (memcmp(fileid, R_ADDR( 567 dbmp->reginfo, mfp->fileid_off), DB_FILE_ID_LEN) != 0) 568 continue; 569 570 break; 571 } 572 573 if (mfp == NULL) { 574 if (inmem) { 575 ret = ENOENT; 576 goto err; 577 } 578 goto fsop; 579 } 580 581 if (op_is_remove) { 582 MUTEX_LOCK(env, mfp->mutex); 583 /* 584 * In-memory dbs have an artificially incremented ref count so 585 * they do not get reclaimed as long as they exist. Since we 586 * are now deleting the database, we need to dec that count. 587 */ 588 if (mfp->no_backing_file) 589 mfp->mpf_cnt--; 590 mfp->deadfile = 1; 591 MUTEX_UNLOCK(env, mfp->mutex); 592 } else { 593 /* 594 * Else, it's a rename. We've allocated memory for the new 595 * name. Swap it with the old one. If it's in memory we 596 * need to move it the right bucket. 597 */ 598 p = R_ADDR(dbmp->reginfo, mfp->path_off); 599 mfp->path_off = newname_off; 600 601 if (inmem && hp != nhp) { 602 DB_ASSERT(env, nhp != NULL); 603 SH_TAILQ_REMOVE(&hp->hash_bucket, mfp, q, __mpoolfile); 604 mfp->bucket = bucket; 605 SH_TAILQ_INSERT_TAIL(&nhp->hash_bucket, mfp, q); 606 } 607 } 608 609fsop: /* 610 * If this is a real file, then mfp could be NULL, because 611 * mpool isn't turned on, and we still need to do the file ops. 612 */ 613 if (mfp == NULL || !mfp->no_backing_file) { 614 if (op_is_remove) { 615 /* 616 * !!! 617 * Replication may ask us to unlink a file that's been 618 * renamed. Don't complain if it doesn't exist. 619 */ 620 if ((ret = __os_unlink(env, fullold, 0)) == ENOENT) 621 ret = 0; 622 } else { 623 /* 624 * Defensive only, fullnew should never be 625 * NULL. 626 */ 627 DB_ASSERT(env, fullnew != NULL); 628 if (fullnew == NULL) { 629 ret = EINVAL; 630 goto err; 631 } 632 ret = __os_rename(env, fullold, fullnew, 1); 633 } 634 } 635 636 /* Delete the memory we no longer need. */ 637err: if (p != NULL) 638 __memp_free(&dbmp->reginfo[0], NULL, p); 639 640 /* If we have buckets locked, unlock them when done moving files. */ 641 if (locked == 1) { 642 MUTEX_UNLOCK(env, hp->mtx_hash); 643 if (nhp != NULL && nhp != hp) 644 MUTEX_UNLOCK(env, nhp->mtx_hash); 645 } 646 return (ret); 647} 648 649/* 650 * __memp_ftruncate __ 651 * Truncate the file. 652 * 653 * PUBLIC: int __memp_ftruncate __P((DB_MPOOLFILE *, 654 * PUBLIC: DB_THREAD_INFO *, db_pgno_t, u_int32_t)); 655 */ 656int 657__memp_ftruncate(dbmfp, ip, pgno, flags) 658 DB_MPOOLFILE *dbmfp; 659 DB_THREAD_INFO *ip; 660 db_pgno_t pgno; 661 u_int32_t flags; 662{ 663 ENV *env; 664 MPOOLFILE *mfp; 665 void *pagep; 666 db_pgno_t last_pgno, pg; 667 int ret; 668 669 env = dbmfp->env; 670 mfp = dbmfp->mfp; 671 672 MUTEX_LOCK(env, mfp->mutex); 673 last_pgno = mfp->last_pgno; 674 MUTEX_UNLOCK(env, mfp->mutex); 675 676 if (pgno > last_pgno) { 677 if (LF_ISSET(MP_TRUNC_RECOVER)) 678 return (0); 679 __db_errx(env, "Truncate beyond the end of file"); 680 return (EINVAL); 681 } 682 683 pg = pgno; 684 do { 685 if ((ret = __memp_fget(dbmfp, &pg, 686 ip, NULL, DB_MPOOL_FREE, &pagep)) != 0) 687 return (ret); 688 } while (pg++ < last_pgno); 689 690 /* 691 * If we are aborting an extend of a file, the call to __os_truncate 692 * could extend the file if the new page(s) had not yet been 693 * written to disk. We do not want to extend the file to pages 694 * whose log records are not yet flushed [#14031]. In addition if 695 * we are out of disk space we can generate an error [#12743]. 696 */ 697 MUTEX_LOCK(env, mfp->mutex); 698 if (!F_ISSET(mfp, MP_TEMP) && 699 !mfp->no_backing_file && pgno <= mfp->last_flushed_pgno) 700#ifdef HAVE_FTRUNCATE 701 ret = __os_truncate(env, 702 dbmfp->fhp, pgno, mfp->stat.st_pagesize); 703#else 704 ret = __db_zero_extend(env, 705 dbmfp->fhp, pgno, mfp->last_pgno, mfp->stat.st_pagesize); 706#endif 707 708 /* 709 * This set could race with another thread of control that extending 710 * the file. It's not a problem because we should have the page 711 * locked at a higher level of the system. 712 */ 713 if (ret == 0) { 714 mfp->last_pgno = pgno - 1; 715 if (mfp->last_flushed_pgno > mfp->last_pgno) 716 mfp->last_flushed_pgno = mfp->last_pgno; 717 } 718 MUTEX_UNLOCK(env, mfp->mutex); 719 720 return (ret); 721} 722 723#ifdef HAVE_FTRUNCATE 724/* 725 * Support routines for maintaining a sorted freelist while we try to rearrange 726 * and truncate the file. 727 */ 728 729/* 730 * __memp_alloc_freelist -- 731 * Allocate mpool space for the freelist. 732 * 733 * PUBLIC: int __memp_alloc_freelist __P((DB_MPOOLFILE *, 734 * PUBLIC: u_int32_t, db_pgno_t **)); 735 */ 736int 737__memp_alloc_freelist(dbmfp, nelems, listp) 738 DB_MPOOLFILE *dbmfp; 739 u_int32_t nelems; 740 db_pgno_t **listp; 741{ 742 DB_MPOOL *dbmp; 743 ENV *env; 744 MPOOLFILE *mfp; 745 void *retp; 746 int ret; 747 748 env = dbmfp->env; 749 dbmp = env->mp_handle; 750 mfp = dbmfp->mfp; 751 752 *listp = NULL; 753 754 /* 755 * These fields are protected because the database layer 756 * has the metapage locked while manipulating them. 757 */ 758 mfp->free_ref++; 759 if (mfp->free_size != 0) 760 return (EBUSY); 761 762 /* Allocate at least a few slots. */ 763 mfp->free_cnt = nelems; 764 if (nelems == 0) 765 nelems = 50; 766 767 if ((ret = __memp_alloc(dbmp, dbmp->reginfo, 768 NULL, nelems * sizeof(db_pgno_t), &mfp->free_list, &retp)) != 0) 769 return (ret); 770 771 mfp->free_size = nelems * sizeof(db_pgno_t); 772 *listp = retp; 773 return (0); 774} 775 776/* 777 * __memp_free_freelist -- 778 * Free the list. 779 * 780 * PUBLIC: int __memp_free_freelist __P((DB_MPOOLFILE *)); 781 */ 782int 783__memp_free_freelist(dbmfp) 784 DB_MPOOLFILE *dbmfp; 785{ 786 DB_MPOOL *dbmp; 787 ENV *env; 788 MPOOLFILE *mfp; 789 790 env = dbmfp->env; 791 dbmp = env->mp_handle; 792 mfp = dbmfp->mfp; 793 794 DB_ASSERT(env, mfp->free_ref > 0); 795 if (--mfp->free_ref > 0) 796 return (0); 797 798 DB_ASSERT(env, mfp->free_size != 0); 799 800 MPOOL_SYSTEM_LOCK(env); 801 __memp_free(dbmp->reginfo, NULL, R_ADDR(dbmp->reginfo, mfp->free_list)); 802 MPOOL_SYSTEM_UNLOCK(env); 803 804 mfp->free_cnt = 0; 805 mfp->free_list = 0; 806 mfp->free_size = 0; 807 return (0); 808} 809 810/* 811 * __memp_get_freelst -- 812 * Return current list. 813 * 814 * PUBLIC: int __memp_get_freelist __P(( 815 * PUBLIC: DB_MPOOLFILE *, u_int32_t *, db_pgno_t **)); 816 */ 817int 818__memp_get_freelist(dbmfp, nelemp, listp) 819 DB_MPOOLFILE *dbmfp; 820 u_int32_t *nelemp; 821 db_pgno_t **listp; 822{ 823 DB_MPOOL *dbmp; 824 ENV *env; 825 MPOOLFILE *mfp; 826 827 env = dbmfp->env; 828 dbmp = env->mp_handle; 829 mfp = dbmfp->mfp; 830 831 if (mfp->free_size == 0) { 832 *nelemp = 0; 833 *listp = NULL; 834 } else { 835 *nelemp = mfp->free_cnt; 836 *listp = R_ADDR(dbmp->reginfo, mfp->free_list); 837 } 838 839 return (0); 840} 841 842/* 843 * __memp_extend_freelist -- 844 * Extend the list. 845 * 846 * PUBLIC: int __memp_extend_freelist __P(( 847 * PUBLIC: DB_MPOOLFILE *, u_int32_t , db_pgno_t **)); 848 */ 849int 850__memp_extend_freelist(dbmfp, count, listp) 851 DB_MPOOLFILE *dbmfp; 852 u_int32_t count; 853 db_pgno_t **listp; 854{ 855 DB_MPOOL *dbmp; 856 ENV *env; 857 MPOOLFILE *mfp; 858 int ret; 859 void *retp; 860 861 env = dbmfp->env; 862 dbmp = env->mp_handle; 863 mfp = dbmfp->mfp; 864 865 if (mfp->free_size == 0) 866 return (EINVAL); 867 868 if (count * sizeof(db_pgno_t) > mfp->free_size) { 869 mfp->free_size = 870 (size_t)DB_ALIGN(count * sizeof(db_pgno_t), 512); 871 *listp = R_ADDR(dbmp->reginfo, mfp->free_list); 872 if ((ret = __memp_alloc(dbmp, dbmp->reginfo, 873 NULL, mfp->free_size, &mfp->free_list, &retp)) != 0) 874 return (ret); 875 876 memcpy(retp, *listp, mfp->free_cnt * sizeof(db_pgno_t)); 877 878 MPOOL_SYSTEM_LOCK(env); 879 __memp_free(dbmp->reginfo, NULL, *listp); 880 MPOOL_SYSTEM_UNLOCK(env); 881 } 882 883 mfp->free_cnt = count; 884 *listp = R_ADDR(dbmp->reginfo, mfp->free_list); 885 886 return (0); 887} 888#endif 889 890/* 891 * __memp_set_last_pgno -- set the last page of the file 892 * 893 * PUBLIC: void __memp_set_last_pgno __P((DB_MPOOLFILE *, db_pgno_t)); 894 */ 895void 896__memp_set_last_pgno(dbmfp, pgno) 897 DB_MPOOLFILE *dbmfp; 898 db_pgno_t pgno; 899{ 900 dbmfp->mfp->last_pgno = pgno; 901} 902