robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
2b52acfb8a
master
1/// An abstraction to handle blocks of memory which can be stored in a file. 2/// This is the implementation of a sort of virtual memory. 3/// 4/// A memfile consists of a sequence of blocks: 5/// - Blocks numbered from 0 upwards have been assigned a place in the actual 6/// file. The block number is equal to the page number in the file. 7/// - Blocks with negative numbers are currently in memory only. They can be 8/// assigned a place in the file when too much memory is being used. At that 9/// moment, they get a new, positive, number. A list is used for translation 10/// of negative to positive numbers. 11/// 12/// The size of a block is a multiple of a page size, normally the page size of 13/// the device the file is on. Most blocks are 1 page long. A block of multiple 14/// pages is used for a line that does not fit in a single page. 15/// 16/// Each block can be in memory and/or in a file. The block stays in memory 17/// as long as it is locked. If it is no longer locked it can be swapped out to 18/// the file. It is only written to the file if it has been changed. 19/// 20/// Under normal operation the file is created when opening the memory file and 21/// deleted when closing the memory file. Only with recovery an existing memory 22/// file is opened. 23/// 24/// The functions for using a memfile: 25/// 26/// mf_open() open a new or existing memfile 27/// mf_open_file() open a swap file for an existing memfile 28/// mf_close() close (and delete) a memfile 29/// mf_new() create a new block in a memfile and lock it 30/// mf_get() get an existing block and lock it 31/// mf_put() unlock a block, may be marked for writing 32/// mf_free() remove a block 33/// mf_sync() sync changed parts of memfile to disk 34/// mf_release_all() release as much memory as possible 35/// mf_trans_del() may translate negative to positive block number 36/// mf_fullname() make file name full path (use before first :cd) 37 38#include <assert.h> 39#include <fcntl.h> 40#include <inttypes.h> 41#include <limits.h> 42#include <stdbool.h> 43#include <stdio.h> 44#include <string.h> 45#include <sys/stat.h> 46 47#include "nvim/assert_defs.h" 48#include "nvim/buffer_defs.h" 49#include "nvim/errors.h" 50#include "nvim/fileio.h" 51#include "nvim/gettext_defs.h" 52#include "nvim/globals.h" 53#include "nvim/main.h" 54#include "nvim/map_defs.h" 55#include "nvim/memfile.h" 56#include "nvim/memfile_defs.h" 57#include "nvim/memline.h" 58#include "nvim/memory.h" 59#include "nvim/message.h" 60#include "nvim/os/fs.h" 61#include "nvim/os/fs_defs.h" 62#include "nvim/os/input.h" 63#include "nvim/os/os_defs.h" 64#include "nvim/path.h" 65#include "nvim/pos_defs.h" 66#include "nvim/types_defs.h" 67#include "nvim/vim_defs.h" 68 69#define MEMFILE_PAGE_SIZE 4096 /// default page size 70 71#include "memfile.c.generated.h" 72 73static const char e_block_was_not_locked []= N_ ("E293: Block was not locked" ); 74 75/// Open a new or existing memory block file. 76/// 77/// @param fname Name of file to use. 78/// - If NULL, it means no file (use memory only). 79/// - If not NULL: 80/// * Should correspond to an existing file. 81/// * String must have been allocated (it is not copied). 82/// * If opening the file fails, it is freed and function fails. 83 84/// @param flags Flags for open() call. 85/// 86/// @return - The open memory file, on success. 87/// - NULL, on failure (e.g. file does not exist). 88memfile_T * mf_open (char * fname ,int flags ) 89{ 90memfile_T * mfp = xmalloc (sizeof (memfile_T )); 91 92if (fname == NULL ) {// no file, use memory only 93mfp -> mf_fname = NULL ; 94mfp -> mf_ffname = NULL ; 95mfp -> mf_fd = -1 ; 96 }else {// try to open the file 97if (!mf_do_open (mfp ,fname ,flags )) { 98xfree (mfp ); 99return NULL ;// fail if file could not be opened 100 } 101 } 102 103mfp -> mf_free_first = NULL ;// free list is empty 104mfp -> mf_dirty = MF_DIRTY_NO ; 105mfp -> mf_hash = (PMap (int64_t ))MAP_INIT ; 106mfp -> mf_trans = (Map (int64_t ,int64_t ))MAP_INIT ; 107mfp -> mf_page_size = MEMFILE_PAGE_SIZE ; 108 109// Try to set the page size equal to device's block size. Speeds up I/O a lot. 110FileInfo file_info ; 111if (mfp -> mf_fd >=0 && os_fileinfo_fd (mfp -> mf_fd ,& file_info )) { 112uint64_t blocksize = os_fileinfo_blocksize (& file_info ); 113if (blocksize >=MIN_SWAP_PAGE_SIZE && blocksize <=MAX_SWAP_PAGE_SIZE ) { 114STATIC_ASSERT (MAX_SWAP_PAGE_SIZE <=UINT_MAX , 115"MAX_SWAP_PAGE_SIZE must fit into an unsigned" ); 116mfp -> mf_page_size = (unsigned )blocksize ; 117 } 118 } 119 120off_T size ; 121 122// When recovering, the actual block size will be retrieved from block 0 123// in ml_recover(). The size used here may be wrong, therefore mf_blocknr_max 124// must be rounded up. 125if (mfp -> mf_fd < 0 126|| (flags & (O_TRUNC |O_EXCL )) 127|| (size = vim_lseek (mfp -> mf_fd ,0 ,SEEK_END )) <=0 ) { 128// no file or empty file 129mfp -> mf_blocknr_max = 0 ; 130 }else { 131assert (sizeof (off_T ) <=sizeof (blocknr_T ) 132&& mfp -> mf_page_size > 0 133&& mfp -> mf_page_size - 1 <=INT64_MAX - size ); 134mfp -> mf_blocknr_max = (((blocknr_T )size + mfp -> mf_page_size - 1 ) 135 /mfp -> mf_page_size ); 136 } 137mfp -> mf_blocknr_min = -1 ; 138mfp -> mf_neg_count = 0 ; 139mfp -> mf_infile_count = mfp -> mf_blocknr_max ; 140 141return mfp ; 142} 143 144/// Open a file for an existing memfile. 145/// 146/// Used when updatecount set from 0 to some value. 147/// 148/// @param fname Name of file to use. 149/// - If NULL, it means no file (use memory only). 150/// - If not NULL: 151/// * Should correspond to an existing file. 152/// * String must have been allocated (it is not copied). 153/// * If opening the file fails, it is freed and function fails. 154/// 155/// @return OK On success. 156/// FAIL If file could not be opened. 157int mf_open_file (memfile_T * mfp ,char * fname ) 158{ 159if (mf_do_open (mfp ,fname ,O_RDWR |O_CREAT |O_EXCL )) { 160mfp -> mf_dirty = MF_DIRTY_YES ; 161return OK ; 162 } 163 164return FAIL ; 165} 166 167/// Close a memory file and optionally delete the associated file. 168/// 169/// @param del_file Whether to delete associated file. 170void mf_close (memfile_T * mfp ,bool del_file ) 171{ 172if (mfp == NULL ) {// safety check 173return ; 174 } 175if (mfp -> mf_fd >=0 && close (mfp -> mf_fd )< 0 ) { 176emsg (_ (e_swapclose )); 177 } 178if (del_file && mfp -> mf_fname != NULL ) { 179os_remove (mfp -> mf_fname ); 180 } 181 182// free entries in used list 183bhdr_T * hp ; 184map_foreach_value (& mfp -> mf_hash ,hp , { 185mf_free_bhdr (hp ); 186 }) 187while (mfp -> mf_free_first != NULL ) {// free entries in free list 188xfree (mf_rem_free (mfp )); 189 } 190map_destroy (int64_t ,& mfp -> mf_hash ); 191map_destroy (int64_t ,& mfp -> mf_trans );// free hashtable and its items 192mf_free_fnames (mfp ); 193xfree (mfp ); 194} 195 196/// Close the swap file for a memfile. Used when 'swapfile' is reset. 197/// 198/// @param getlines Whether to get all lines into memory. 199void mf_close_file (buf_T * buf ,bool getlines ) 200{ 201memfile_T * mfp = buf -> b_ml .ml_mfp ; 202if (mfp == NULL || mfp -> mf_fd < 0 ) {// nothing to close 203return ; 204 } 205 206if (getlines ) { 207// get all blocks in memory by accessing all lines (clumsy!) 208for (linenr_T lnum = 1 ;lnum <=buf -> b_ml .ml_line_count ;lnum ++ ) { 209ml_get_buf (buf ,lnum ); 210 } 211 } 212 213if (close (mfp -> mf_fd )< 0 ) {// close the file 214emsg (_ (e_swapclose )); 215 } 216mfp -> mf_fd = -1 ; 217 218if (mfp -> mf_fname != NULL ) { 219os_remove (mfp -> mf_fname );// delete the swap file 220mf_free_fnames (mfp ); 221 } 222} 223 224/// Set new size for a memfile. Used when block 0 of a swapfile has been read 225/// and the size it indicates differs from what was guessed. 226void mf_new_page_size (memfile_T * mfp ,unsigned new_size ) 227{ 228mfp -> mf_page_size = new_size ; 229} 230 231/// Get a new block 232/// 233/// @param negative Whether a negative block number is desired (data block). 234/// @param page_count Desired number of pages. 235bhdr_T * mf_new (memfile_T * mfp ,bool negative ,unsigned page_count ) 236{ 237bhdr_T * hp = NULL ; 238 239// Decide on the number to use: 240// If there is a free block, use its number. 241// Otherwise use mf_block_min for a negative number, mf_block_max for 242// a positive number. 243bhdr_T * freep = mfp -> mf_free_first ;// first free block 244if (!negative && freep != NULL && freep -> bh_page_count >=page_count ) { 245if (freep -> bh_page_count > page_count ) { 246// If the block in the free list has more pages, take only the number 247// of pages needed and allocate a new bhdr_T with data. 248hp = mf_alloc_bhdr (mfp ,page_count ); 249hp -> bh_bnum = freep -> bh_bnum ; 250freep -> bh_bnum += page_count ; 251freep -> bh_page_count -= page_count ; 252 }else {// need to allocate memory for this block 253// If the number of pages matches use the bhdr_T from the free list and 254// allocate the data. 255void * p = xmalloc ((size_t )mfp -> mf_page_size * page_count ); 256hp = mf_rem_free (mfp ); 257hp -> bh_data = p ; 258 } 259 }else {// get a new number 260hp = mf_alloc_bhdr (mfp ,page_count ); 261if (negative ) { 262hp -> bh_bnum = mfp -> mf_blocknr_min -- ; 263mfp -> mf_neg_count ++ ; 264 }else { 265hp -> bh_bnum = mfp -> mf_blocknr_max ; 266mfp -> mf_blocknr_max += page_count ; 267 } 268 } 269hp -> bh_flags = BH_LOCKED |BH_DIRTY ;// new block is always dirty 270mfp -> mf_dirty = MF_DIRTY_YES ; 271hp -> bh_page_count = page_count ; 272pmap_put (int64_t )(& mfp -> mf_hash ,hp -> bh_bnum ,hp ); 273 274// Init the data to all zero, to avoid reading uninitialized data. 275// This also avoids that the passwd file ends up in the swap file! 276memset (hp -> bh_data ,0 , (size_t )mfp -> mf_page_size * page_count ); 277 278return hp ; 279} 280 281// Get existing block "nr" with "page_count" pages. 282// 283// Caller should first check a negative nr with mf_trans_del(). 284// 285// @return NULL if not found 286bhdr_T * mf_get (memfile_T * mfp ,blocknr_T nr ,unsigned page_count ) 287{ 288// check block number exists 289if (nr >=mfp -> mf_blocknr_max || nr <=mfp -> mf_blocknr_min ) { 290return NULL ; 291 } 292 293// see if it is in the cache 294bhdr_T * hp = pmap_get (int64_t )(& mfp -> mf_hash ,nr ); 295if (hp == NULL ) {// not in the hash list 296if (nr < 0 || nr >=mfp -> mf_infile_count ) {// can't be in the file 297return NULL ; 298 } 299 300// could check here if the block is in the free list 301 302if (page_count > 0 ) { 303hp = mf_alloc_bhdr (mfp ,page_count ); 304 } 305if (hp == NULL ) { 306return NULL ; 307 } 308 309hp -> bh_bnum = nr ; 310hp -> bh_flags = 0 ; 311hp -> bh_page_count = page_count ; 312if (mf_read (mfp ,hp )== FAIL ) {// cannot read the block 313mf_free_bhdr (hp ); 314return NULL ; 315 } 316 }else { 317pmap_del (int64_t )(& mfp -> mf_hash ,hp -> bh_bnum ,NULL ); 318 } 319 320hp -> bh_flags |=BH_LOCKED ; 321pmap_put (int64_t )(& mfp -> mf_hash ,hp -> bh_bnum ,hp ); 322 323return hp ; 324} 325 326/// Release the block *hp. 327/// 328/// @param dirty Whether block must be written to file later. 329/// @param infile Whether block should be in file (needed for recovery). 330void mf_put (memfile_T * mfp ,bhdr_T * hp ,bool dirty ,bool infile ) 331{ 332unsigned flags = hp -> bh_flags ; 333 334if ((flags & BH_LOCKED )== 0 ) { 335iemsg (_ (e_block_was_not_locked )); 336 } 337flags &= ~BH_LOCKED ; 338if (dirty ) { 339flags |=BH_DIRTY ; 340if (mfp -> mf_dirty != MF_DIRTY_YES_NOSYNC ) { 341mfp -> mf_dirty = MF_DIRTY_YES ; 342 } 343 } 344hp -> bh_flags = flags ; 345if (infile ) { 346mf_trans_add (mfp ,hp );// may translate negative in positive nr 347 } 348} 349 350/// Signal block as no longer used (may put it in the free list). 351void mf_free (memfile_T * mfp ,bhdr_T * hp ) 352{ 353xfree (hp -> bh_data );// free data 354pmap_del (int64_t )(& mfp -> mf_hash ,hp -> bh_bnum ,NULL );// get *hp out of the hash table 355if (hp -> bh_bnum < 0 ) { 356xfree (hp );// don't want negative numbers in free list 357mfp -> mf_neg_count -- ; 358 }else { 359mf_ins_free (mfp ,hp );// put *hp in the free list 360 } 361} 362 363/// Sync memory file to disk. 364/// 365/// @param flags MFS_ALL If not given, blocks with negative numbers are not 366/// synced, even when they are dirty. 367/// MFS_STOP Stop syncing when a character becomes available, 368/// but sync at least one block. 369/// MFS_FLUSH Make sure buffers are flushed to disk, so they will 370/// survive a system crash. 371/// MFS_ZERO Only write block 0. 372/// 373/// @return FAIL If failure. Possible causes: 374/// - No file (nothing to do). 375/// - Write error (probably full disk). 376/// OK Otherwise. 377int mf_sync (memfile_T * mfp ,int flags ) 378{ 379int got_int_save = got_int ; 380 381if (mfp -> mf_fd < 0 ) { 382// there is no file, nothing to do 383mfp -> mf_dirty = MF_DIRTY_NO ; 384return FAIL ; 385 } 386 387// Only a CTRL-C while writing will break us here, not one typed previously. 388got_int = false; 389 390// Sync from last to first (may reduce the probability of an inconsistent 391// file). If a write fails, it is very likely caused by a full filesystem. 392// Then we only try to write blocks within the existing file. If that also 393// fails then we give up. 394int status = OK ; 395bhdr_T * hp = NULL ; 396// note, "last" block is typically earlier in the hash list 397map_foreach_value (& mfp -> mf_hash ,hp , { 398if (((flags & MFS_ALL )|| hp -> bh_bnum >=0 ) 399&& (hp -> bh_flags & BH_DIRTY ) 400&& (status == OK || (hp -> bh_bnum >=0 401&& hp -> bh_bnum < mfp -> mf_infile_count ))) { 402if ((flags & MFS_ZERO )&& hp -> bh_bnum != 0 ) { 403continue ; 404 } 405if (mf_write (mfp ,hp )== FAIL ) { 406if (status == FAIL ) {// double error: quit syncing 407break ; 408 } 409status = FAIL ; 410 } 411if (flags & MFS_STOP ) {// Stop when char available now. 412if (os_char_avail ()) { 413break ; 414 } 415 }else if (!main_loop .recursive ) {// May reach here on OOM in libuv callback 416os_breakcheck (); 417 } 418if (got_int ) { 419break ; 420 } 421 } 422 }) 423 424// If the whole list is flushed, the memfile is not dirty anymore. 425// In case of an error, dirty flag is also set, to avoid trying all the time. 426if (hp == NULL || status == FAIL ) { 427mfp -> mf_dirty = MF_DIRTY_NO ; 428 } 429 430if (flags & MFS_FLUSH ) { 431if (os_fsync (mfp -> mf_fd )) { 432status = FAIL ; 433 } 434 } 435 436got_int |=got_int_save ; 437 438return status ; 439} 440 441/// Set dirty flag for all blocks in memory file with a positive block number. 442/// These are blocks that need to be written to a newly created swapfile. 443void mf_set_dirty (memfile_T * mfp ) 444{ 445bhdr_T * hp ; 446map_foreach_value (& mfp -> mf_hash ,hp , { 447if (hp -> bh_bnum > 0 ) { 448hp -> bh_flags |=BH_DIRTY ; 449 } 450 }) 451mfp -> mf_dirty = MF_DIRTY_YES ; 452} 453 454/// Release as many blocks as possible. 455/// 456/// Used in case of out of memory 457/// 458/// @return Whether any memory was released. 459bool mf_release_all (void ) 460{ 461bool retval = false; 462FOR_ALL_BUFFERS (buf ) { 463memfile_T * mfp = buf -> b_ml .ml_mfp ; 464if (mfp != NULL ) { 465// If no swap file yet, try to open one. 466if (mfp -> mf_fd < 0 && buf -> b_may_swap ) { 467ml_open_file (buf ); 468 } 469 470// Flush as many blocks as possible, only if there is a swapfile. 471if (mfp -> mf_fd >=0 ) { 472for (int i = 0 ;i < (int )map_size (& mfp -> mf_hash );) { 473bhdr_T * hp = mfp -> mf_hash .values [i ]; 474if (!(hp -> bh_flags & BH_LOCKED ) 475&& (!(hp -> bh_flags & BH_DIRTY ) 476|| mf_write (mfp ,hp )!= FAIL )) { 477pmap_del (int64_t )(& mfp -> mf_hash ,hp -> bh_bnum ,NULL ); 478mf_free_bhdr (hp ); 479retval = true; 480// Rerun with the same value of i. another item will have taken 481// its place (or it was the last) 482 }else { 483i ++ ; 484 } 485 } 486 } 487 } 488 } 489return retval ; 490} 491 492/// Allocate a block header and a block of memory for it. 493static bhdr_T * mf_alloc_bhdr (memfile_T * mfp ,unsigned page_count ) 494{ 495bhdr_T * hp = xmalloc (sizeof (bhdr_T )); 496hp -> bh_data = xmalloc ((size_t )mfp -> mf_page_size * page_count ); 497hp -> bh_page_count = page_count ; 498return hp ; 499} 500 501/// Free a block header and its block memory. 502static void mf_free_bhdr (bhdr_T * hp ) 503{ 504xfree (hp -> bh_data ); 505xfree (hp ); 506} 507 508/// Insert a block in the free list. 509static void mf_ins_free (memfile_T * mfp ,bhdr_T * hp ) 510{ 511hp -> bh_data = mfp -> mf_free_first ; 512mfp -> mf_free_first = hp ; 513} 514 515/// Remove the first block in the free list and return it. 516/// 517/// Caller must check that mfp->mf_free_first is not NULL. 518static bhdr_T * mf_rem_free (memfile_T * mfp ) 519{ 520bhdr_T * hp = mfp -> mf_free_first ; 521mfp -> mf_free_first = hp -> bh_data ; 522return hp ; 523} 524 525/// Read a block from disk. 526/// 527/// @return OK On success. 528/// FAIL On failure. Could be: 529/// - No file. 530/// - Error reading file. 531static int mf_read (memfile_T * mfp ,bhdr_T * hp ) 532{ 533if (mfp -> mf_fd < 0 ) {// there is no file, can't read 534return FAIL ; 535 } 536 537unsigned page_size = mfp -> mf_page_size ; 538// TODO(elmart): Check (page_size * hp->bh_bnum) within off_T bounds. 539off_T offset = (off_T )(page_size * hp -> bh_bnum ); 540if (vim_lseek (mfp -> mf_fd ,offset ,SEEK_SET )!= offset ) { 541PERROR (_ ("E294: Seek error in swap file read" )); 542return FAIL ; 543 } 544// check for overflow; we know that page_size must be > 0 545assert (hp -> bh_page_count <=UINT_MAX /page_size ); 546unsigned size = page_size * hp -> bh_page_count ; 547if ((unsigned )read_eintr (mfp -> mf_fd ,hp -> bh_data ,size )!= size ) { 548PERROR (_ ("E295: Read error in swap file" )); 549return FAIL ; 550 } 551 552return OK ; 553} 554 555/// Write a block to disk. 556/// 557/// @return OK On success. 558/// FAIL On failure. Could be: 559/// - No file. 560/// - Could not translate negative block number to positive. 561/// - Seek error in swap file. 562/// - Write error in swap file. 563static int mf_write (memfile_T * mfp ,bhdr_T * hp ) 564{ 565bhdr_T * hp2 ; 566unsigned page_count ;// number of pages written 567 568if (mfp -> mf_fd < 0 && !mfp -> mf_reopen ) { 569// there is no file and there was no file, can't write 570return FAIL ; 571 } 572 573if (hp -> bh_bnum < 0 ) {// must assign file block number 574if (mf_trans_add (mfp ,hp )== FAIL ) { 575return FAIL ; 576 } 577 } 578 579unsigned page_size = mfp -> mf_page_size ;// number of bytes in a page 580 581/// We don't want gaps in the file. Write the blocks in front of *hp 582/// to extend the file. 583/// If block 'mf_infile_count' is not in the hash list, it has been 584/// freed. Fill the space in the file with data from the current block. 585while (true) { 586blocknr_T nr = hp -> bh_bnum ;// block nr which is being written 587if (nr > mfp -> mf_infile_count ) {// beyond end of file 588nr = mfp -> mf_infile_count ; 589hp2 = pmap_get (int64_t )(& mfp -> mf_hash ,nr );// NULL caught below 590 }else { 591hp2 = hp ; 592 } 593 594// TODO(elmart): Check (page_size * nr) within off_T bounds. 595off_T offset = (off_T )(page_size * nr );// offset in the file 596if (hp2 == NULL ) {// freed block, fill with dummy data 597page_count = 1 ; 598 }else { 599page_count = hp2 -> bh_page_count ; 600 } 601unsigned size = page_size * page_count ;// number of bytes written 602 603for (int attempt = 1 ;attempt <=2 ;attempt ++ ) { 604if (mfp -> mf_fd >=0 ) { 605if (vim_lseek (mfp -> mf_fd ,offset ,SEEK_SET )!= offset ) { 606PERROR (_ ("E296: Seek error in swap file write" )); 607return FAIL ; 608 } 609void * data = (hp2 == NULL ) ?hp -> bh_data :hp2 -> bh_data ; 610if ((unsigned )write_eintr (mfp -> mf_fd ,data ,size )== size ) { 611break ; 612 } 613 } 614 615if (attempt == 1 ) { 616// If the swap file is on a network drive, and the network 617// gets disconnected and then re-connected, we can maybe fix it 618// by closing and then re-opening the file. 619if (mfp -> mf_fd >=0 ) { 620close (mfp -> mf_fd ); 621 } 622mfp -> mf_fd = os_open (mfp -> mf_fname ,mfp -> mf_flags ,S_IREAD |S_IWRITE ); 623mfp -> mf_reopen = (mfp -> mf_fd < 0 ); 624 } 625if (attempt == 2 || mfp -> mf_fd < 0 ) { 626// Avoid repeating the error message, this mostly happens when the 627// disk is full. We give the message again only after a successful 628// write or when hitting a key. We keep on trying, in case some 629// space becomes available. 630if (!did_swapwrite_msg ) { 631emsg (_ ("E297: Write error in swap file" )); 632 } 633did_swapwrite_msg = true; 634return FAIL ; 635 } 636 } 637 638did_swapwrite_msg = false; 639if (hp2 != NULL ) {// written a non-dummy block 640hp2 -> bh_flags &= ~BH_DIRTY ; 641 } 642if (nr + (blocknr_T )page_count > mfp -> mf_infile_count ) {// appended to file 643mfp -> mf_infile_count = nr + page_count ; 644 } 645if (nr == hp -> bh_bnum ) {// written the desired block 646break ; 647 } 648 } 649return OK ; 650} 651 652/// Make block number positive and add it to the translation list. 653/// 654/// @return OK On success. 655/// FAIL On failure. 656static int mf_trans_add (memfile_T * mfp ,bhdr_T * hp ) 657{ 658if (hp -> bh_bnum >=0 ) {// it's already positive 659return OK ; 660 } 661 662// Get a new number for the block. 663// If the first item in the free list has sufficient pages, use its number. 664// Otherwise use mf_blocknr_max. 665blocknr_T new_bnum ; 666bhdr_T * freep = mfp -> mf_free_first ; 667unsigned page_count = hp -> bh_page_count ; 668if (freep != NULL && freep -> bh_page_count >=page_count ) { 669new_bnum = freep -> bh_bnum ; 670// If the page count of the free block was larger, reduce it. 671// If the page count matches, remove the block from the free list. 672if (freep -> bh_page_count > page_count ) { 673freep -> bh_bnum += page_count ; 674freep -> bh_page_count -= page_count ; 675 }else { 676freep = mf_rem_free (mfp ); 677xfree (freep ); 678 } 679 }else { 680new_bnum = mfp -> mf_blocknr_max ; 681mfp -> mf_blocknr_max += page_count ; 682 } 683 684blocknr_T old_bnum = hp -> bh_bnum ;// adjust number 685pmap_del (int64_t )(& mfp -> mf_hash ,hp -> bh_bnum ,NULL ); 686hp -> bh_bnum = new_bnum ; 687pmap_put (int64_t )(& mfp -> mf_hash ,new_bnum ,hp ); 688 689// Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum". 690map_put (int64_t ,int64_t )(& mfp -> mf_trans ,old_bnum ,new_bnum ); 691 692return OK ; 693} 694 695/// Lookup translation from trans list and delete the entry. 696/// 697/// @return The positive new number When found. 698/// The old number When not found. 699blocknr_T mf_trans_del (memfile_T * mfp ,blocknr_T old_nr ) 700{ 701blocknr_T * num = map_ref (int64_t ,int64_t )(& mfp -> mf_trans ,old_nr ,NULL ); 702if (num == NULL ) {// not found 703return old_nr ; 704 } 705 706mfp -> mf_neg_count -- ; 707blocknr_T new_bnum = * num ; 708 709// remove entry from the trans list 710map_del (int64_t ,int64_t )(& mfp -> mf_trans ,old_nr ,NULL ); 711 712return new_bnum ; 713} 714 715/// Frees mf_fname and mf_ffname. 716void mf_free_fnames (memfile_T * mfp ) 717{ 718XFREE_CLEAR (mfp -> mf_fname ); 719XFREE_CLEAR (mfp -> mf_ffname ); 720} 721 722/// Set the simple file name and the full file name of memfile's swapfile, out 723/// of simple file name and some other considerations. 724/// 725/// Only called when creating or renaming the swapfile. Either way it's a new 726/// name so we must work out the full path name. 727void mf_set_fnames (memfile_T * mfp ,char * fname ) 728{ 729mfp -> mf_fname = fname ; 730mfp -> mf_ffname = FullName_save (mfp -> mf_fname , false); 731} 732 733/// Make name of memfile's swapfile a full path. 734/// 735/// Used before doing a :cd 736void mf_fullname (memfile_T * mfp ) 737{ 738if (mfp == NULL || mfp -> mf_fname == NULL || mfp -> mf_ffname == NULL ) { 739return ; 740 } 741 742xfree (mfp -> mf_fname ); 743mfp -> mf_fname = mfp -> mf_ffname ; 744mfp -> mf_ffname = NULL ; 745} 746 747/// Return true if there are any translations pending for memfile. 748bool mf_need_trans (memfile_T * mfp ) 749{ 750return mfp -> mf_fname != NULL && mfp -> mf_neg_count > 0 ; 751} 752 753/// Open memfile's swapfile. 754/// 755/// "fname" must be in allocated memory, and is consumed (also when error). 756/// 757/// @param flags Flags for open(). 758/// @return A bool indicating success of the `open` call. 759static bool mf_do_open (memfile_T * mfp ,char * fname ,int flags ) 760{ 761// fname cannot be NameBuff, because it must have been allocated. 762mf_set_fnames (mfp ,fname ); 763assert (mfp -> mf_fname != NULL ); 764 765/// Extra security check: When creating a swap file it really shouldn't 766/// exist yet. If there is a symbolic link, this is most likely an attack. 767FileInfo file_info ; 768if ((flags & O_CREAT ) 769&& os_fileinfo_link (mfp -> mf_fname ,& file_info )) { 770mfp -> mf_fd = -1 ; 771emsg (_ ("E300: Swap file already exists (symlink attack?)" )); 772 }else { 773// try to open the file 774flags |=O_NOFOLLOW ; 775mfp -> mf_flags = flags ; 776mfp -> mf_fd = os_open (mfp -> mf_fname ,flags ,S_IREAD |S_IWRITE ); 777 } 778 779// If the file cannot be opened, use memory only 780if (mfp -> mf_fd < 0 ) { 781mf_free_fnames (mfp ); 782return false; 783 } 784 785os_set_cloexec (mfp -> mf_fd ); 786 787return true; 788}