robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesdocs: misc #392072b52acfb8a

master
24.3 KiB788 linesraw
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{
90  memfile_T *mfp = xmalloc(sizeof(memfile_T));
91
92  if (fname == NULL) {               // no file, use memory only
93    mfp->mf_fname = NULL;
94    mfp->mf_ffname = NULL;
95    mfp->mf_fd = -1;
96  } else {                           // try to open the file
97    if (!mf_do_open(mfp, fname, flags)) {
98      xfree(mfp);
99      return NULL;                   // fail if file could not be opened
100    }
101  }
102
103  mfp->mf_free_first = NULL;         // free list is empty
104  mfp->mf_dirty = MF_DIRTY_NO;
105  mfp->mf_hash = (PMap(int64_t)) MAP_INIT;
106  mfp->mf_trans = (Map(int64_t, int64_t)) MAP_INIT;
107  mfp->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.
110  FileInfo file_info;
111  if (mfp->mf_fd >= 0 && os_fileinfo_fd(mfp->mf_fd, &file_info)) {
112    uint64_t blocksize = os_fileinfo_blocksize(&file_info);
113    if (blocksize >= MIN_SWAP_PAGE_SIZE && blocksize <= MAX_SWAP_PAGE_SIZE) {
114      STATIC_ASSERT(MAX_SWAP_PAGE_SIZE <= UINT_MAX,
115                    "MAX_SWAP_PAGE_SIZE must fit into an unsigned");
116      mfp->mf_page_size = (unsigned)blocksize;
117    }
118  }
119
120  off_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.
125  if (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
129    mfp->mf_blocknr_max = 0;
130  } else {
131    assert(sizeof(off_T) <= sizeof(blocknr_T)
132           && mfp->mf_page_size > 0
133           && mfp->mf_page_size - 1 <= INT64_MAX - size);
134    mfp->mf_blocknr_max = (((blocknr_T)size + mfp->mf_page_size - 1)
135                           / mfp->mf_page_size);
136  }
137  mfp->mf_blocknr_min = -1;
138  mfp->mf_neg_count = 0;
139  mfp->mf_infile_count = mfp->mf_blocknr_max;
140
141  return 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{
159  if (mf_do_open(mfp, fname, O_RDWR | O_CREAT | O_EXCL)) {
160    mfp->mf_dirty = MF_DIRTY_YES;
161    return OK;
162  }
163
164  return 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{
172  if (mfp == NULL) {                    // safety check
173    return;
174  }
175  if (mfp->mf_fd >= 0 && close(mfp->mf_fd) < 0) {
176    emsg(_(e_swapclose));
177  }
178  if (del_file && mfp->mf_fname != NULL) {
179    os_remove(mfp->mf_fname);
180  }
181
182  // free entries in used list
183  bhdr_T *hp;
184  map_foreach_value(&mfp->mf_hash, hp, {
185    mf_free_bhdr(hp);
186  })
187  while (mfp->mf_free_first != NULL) {  // free entries in free list
188    xfree(mf_rem_free(mfp));
189  }
190  map_destroy(int64_t, &mfp->mf_hash);
191  map_destroy(int64_t, &mfp->mf_trans);  // free hashtable and its items
192  mf_free_fnames(mfp);
193  xfree(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{
201  memfile_T *mfp = buf->b_ml.ml_mfp;
202  if (mfp == NULL || mfp->mf_fd < 0) {   // nothing to close
203    return;
204  }
205
206  if (getlines) {
207    // get all blocks in memory by accessing all lines (clumsy!)
208    for (linenr_T lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++) {
209      ml_get_buf(buf, lnum);
210    }
211  }
212
213  if (close(mfp->mf_fd) < 0) {           // close the file
214    emsg(_(e_swapclose));
215  }
216  mfp->mf_fd = -1;
217
218  if (mfp->mf_fname != NULL) {
219    os_remove(mfp->mf_fname);    // delete the swap file
220    mf_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{
228  mfp->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{
237  bhdr_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.
243  bhdr_T *freep = mfp->mf_free_first;        // first free block
244  if (!negative && freep != NULL && freep->bh_page_count >= page_count) {
245    if (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.
248      hp = mf_alloc_bhdr(mfp, page_count);
249      hp->bh_bnum = freep->bh_bnum;
250      freep->bh_bnum += page_count;
251      freep->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.
255      void *p = xmalloc((size_t)mfp->mf_page_size * page_count);
256      hp = mf_rem_free(mfp);
257      hp->bh_data = p;
258    }
259  } else {                      // get a new number
260    hp = mf_alloc_bhdr(mfp, page_count);
261    if (negative) {
262      hp->bh_bnum = mfp->mf_blocknr_min--;
263      mfp->mf_neg_count++;
264    } else {
265      hp->bh_bnum = mfp->mf_blocknr_max;
266      mfp->mf_blocknr_max += page_count;
267    }
268  }
269  hp->bh_flags = BH_LOCKED | BH_DIRTY;    // new block is always dirty
270  mfp->mf_dirty = MF_DIRTY_YES;
271  hp->bh_page_count = page_count;
272  pmap_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!
276  memset(hp->bh_data, 0, (size_t)mfp->mf_page_size * page_count);
277
278  return 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
289  if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min) {
290    return NULL;
291  }
292
293  // see if it is in the cache
294  bhdr_T *hp = pmap_get(int64_t)(&mfp->mf_hash, nr);
295  if (hp == NULL) {                             // not in the hash list
296    if (nr < 0 || nr >= mfp->mf_infile_count) {  // can't be in the file
297      return NULL;
298    }
299
300    // could check here if the block is in the free list
301
302    if (page_count > 0) {
303      hp = mf_alloc_bhdr(mfp, page_count);
304    }
305    if (hp == NULL) {
306      return NULL;
307    }
308
309    hp->bh_bnum = nr;
310    hp->bh_flags = 0;
311    hp->bh_page_count = page_count;
312    if (mf_read(mfp, hp) == FAIL) {             // cannot read the block
313      mf_free_bhdr(hp);
314      return NULL;
315    }
316  } else {
317    pmap_del(int64_t)(&mfp->mf_hash, hp->bh_bnum, NULL);
318  }
319
320  hp->bh_flags |= BH_LOCKED;
321  pmap_put(int64_t)(&mfp->mf_hash, hp->bh_bnum, hp);
322
323  return 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{
332  unsigned flags = hp->bh_flags;
333
334  if ((flags & BH_LOCKED) == 0) {
335    iemsg(_(e_block_was_not_locked));
336  }
337  flags &= ~BH_LOCKED;
338  if (dirty) {
339    flags |= BH_DIRTY;
340    if (mfp->mf_dirty != MF_DIRTY_YES_NOSYNC) {
341      mfp->mf_dirty = MF_DIRTY_YES;
342    }
343  }
344  hp->bh_flags = flags;
345  if (infile) {
346    mf_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{
353  xfree(hp->bh_data);           // free data
354  pmap_del(int64_t)(&mfp->mf_hash, hp->bh_bnum, NULL);  // get *hp out of the hash table
355  if (hp->bh_bnum < 0) {
356    xfree(hp);                  // don't want negative numbers in free list
357    mfp->mf_neg_count--;
358  } else {
359    mf_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{
379  int got_int_save = got_int;
380
381  if (mfp->mf_fd < 0) {
382    // there is no file, nothing to do
383    mfp->mf_dirty = MF_DIRTY_NO;
384    return FAIL;
385  }
386
387  // Only a CTRL-C while writing will break us here, not one typed previously.
388  got_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.
394  int status = OK;
395  bhdr_T *hp = NULL;
396  // note, "last" block is typically earlier in the hash list
397  map_foreach_value(&mfp->mf_hash, hp, {
398    if (((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))) {
402      if ((flags & MFS_ZERO) && hp->bh_bnum != 0) {
403        continue;
404      }
405      if (mf_write(mfp, hp) == FAIL) {
406        if (status == FAIL) {   // double error: quit syncing
407          break;
408        }
409        status = FAIL;
410      }
411      if (flags & MFS_STOP) {   // Stop when char available now.
412        if (os_char_avail()) {
413          break;
414        }
415      } else if (!main_loop.recursive) {  // May reach here on OOM in libuv callback
416        os_breakcheck();
417      }
418      if (got_int) {
419        break;
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.
426  if (hp == NULL || status == FAIL) {
427    mfp->mf_dirty = MF_DIRTY_NO;
428  }
429
430  if (flags & MFS_FLUSH) {
431    if (os_fsync(mfp->mf_fd)) {
432      status = FAIL;
433    }
434  }
435
436  got_int |= got_int_save;
437
438  return 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{
445  bhdr_T *hp;
446  map_foreach_value(&mfp->mf_hash, hp, {
447    if (hp->bh_bnum > 0) {
448      hp->bh_flags |= BH_DIRTY;
449    }
450  })
451  mfp->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{
461  bool retval = false;
462  FOR_ALL_BUFFERS(buf) {
463    memfile_T *mfp = buf->b_ml.ml_mfp;
464    if (mfp != NULL) {
465      // If no swap file yet, try to open one.
466      if (mfp->mf_fd < 0 && buf->b_may_swap) {
467        ml_open_file(buf);
468      }
469
470      // Flush as many blocks as possible, only if there is a swapfile.
471      if (mfp->mf_fd >= 0) {
472        for (int i = 0; i < (int)map_size(&mfp->mf_hash);) {
473          bhdr_T *hp = mfp->mf_hash.values[i];
474          if (!(hp->bh_flags & BH_LOCKED)
475              && (!(hp->bh_flags & BH_DIRTY)
476                  || mf_write(mfp, hp) != FAIL)) {
477            pmap_del(int64_t)(&mfp->mf_hash, hp->bh_bnum, NULL);
478            mf_free_bhdr(hp);
479            retval = true;
480            // Rerun with the same value of i. another item will have taken
481            // its place (or it was the last)
482          } else {
483            i++;
484          }
485        }
486      }
487    }
488  }
489  return 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{
495  bhdr_T *hp = xmalloc(sizeof(bhdr_T));
496  hp->bh_data = xmalloc((size_t)mfp->mf_page_size * page_count);
497  hp->bh_page_count = page_count;
498  return hp;
499}
500
501/// Free a block header and its block memory.
502static void mf_free_bhdr(bhdr_T *hp)
503{
504  xfree(hp->bh_data);
505  xfree(hp);
506}
507
508/// Insert a block in the free list.
509static void mf_ins_free(memfile_T *mfp, bhdr_T *hp)
510{
511  hp->bh_data = mfp->mf_free_first;
512  mfp->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{
520  bhdr_T *hp = mfp->mf_free_first;
521  mfp->mf_free_first = hp->bh_data;
522  return 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{
533  if (mfp->mf_fd < 0) {     // there is no file, can't read
534    return FAIL;
535  }
536
537  unsigned page_size = mfp->mf_page_size;
538  // TODO(elmart): Check (page_size * hp->bh_bnum) within off_T bounds.
539  off_T offset = (off_T)(page_size * hp->bh_bnum);
540  if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) {
541    PERROR(_("E294: Seek error in swap file read"));
542    return FAIL;
543  }
544  // check for overflow; we know that page_size must be > 0
545  assert(hp->bh_page_count <= UINT_MAX / page_size);
546  unsigned size = page_size * hp->bh_page_count;
547  if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size) {
548    PERROR(_("E295: Read error in swap file"));
549    return FAIL;
550  }
551
552  return 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{
565  bhdr_T *hp2;
566  unsigned page_count;      // number of pages written
567
568  if (mfp->mf_fd < 0 && !mfp->mf_reopen) {
569    // there is no file and there was no file, can't write
570    return FAIL;
571  }
572
573  if (hp->bh_bnum < 0) {    // must assign file block number
574    if (mf_trans_add(mfp, hp) == FAIL) {
575      return FAIL;
576    }
577  }
578
579  unsigned 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.
585  while (true) {
586    blocknr_T nr = hp->bh_bnum;  // block nr which is being written
587    if (nr > mfp->mf_infile_count) {            // beyond end of file
588      nr = mfp->mf_infile_count;
589      hp2 = pmap_get(int64_t)(&mfp->mf_hash, nr);  // NULL caught below
590    } else {
591      hp2 = hp;
592    }
593
594    // TODO(elmart): Check (page_size * nr) within off_T bounds.
595    off_T offset = (off_T)(page_size * nr);  // offset in the file
596    if (hp2 == NULL) {              // freed block, fill with dummy data
597      page_count = 1;
598    } else {
599      page_count = hp2->bh_page_count;
600    }
601    unsigned size = page_size * page_count;  // number of bytes written
602
603    for (int attempt = 1; attempt <= 2; attempt++) {
604      if (mfp->mf_fd >= 0) {
605        if (vim_lseek(mfp->mf_fd, offset, SEEK_SET) != offset) {
606          PERROR(_("E296: Seek error in swap file write"));
607          return FAIL;
608        }
609        void *data = (hp2 == NULL) ? hp->bh_data : hp2->bh_data;
610        if ((unsigned)write_eintr(mfp->mf_fd, data, size) == size) {
611          break;
612        }
613      }
614
615      if (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.
619        if (mfp->mf_fd >= 0) {
620          close(mfp->mf_fd);
621        }
622        mfp->mf_fd = os_open(mfp->mf_fname, mfp->mf_flags, S_IREAD | S_IWRITE);
623        mfp->mf_reopen = (mfp->mf_fd < 0);
624      }
625      if (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.
630        if (!did_swapwrite_msg) {
631          emsg(_("E297: Write error in swap file"));
632        }
633        did_swapwrite_msg = true;
634        return FAIL;
635      }
636    }
637
638    did_swapwrite_msg = false;
639    if (hp2 != NULL) {                             // written a non-dummy block
640      hp2->bh_flags &= ~BH_DIRTY;
641    }
642    if (nr + (blocknr_T)page_count > mfp->mf_infile_count) {  // appended to file
643      mfp->mf_infile_count = nr + page_count;
644    }
645    if (nr == hp->bh_bnum) {                       // written the desired block
646      break;
647    }
648  }
649  return 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{
658  if (hp->bh_bnum >= 0) {                   // it's already positive
659    return 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.
665  blocknr_T new_bnum;
666  bhdr_T *freep = mfp->mf_free_first;
667  unsigned page_count = hp->bh_page_count;
668  if (freep != NULL && freep->bh_page_count >= page_count) {
669    new_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.
672    if (freep->bh_page_count > page_count) {
673      freep->bh_bnum += page_count;
674      freep->bh_page_count -= page_count;
675    } else {
676      freep = mf_rem_free(mfp);
677      xfree(freep);
678    }
679  } else {
680    new_bnum = mfp->mf_blocknr_max;
681    mfp->mf_blocknr_max += page_count;
682  }
683
684  blocknr_T old_bnum = hp->bh_bnum;            // adjust number
685  pmap_del(int64_t)(&mfp->mf_hash, hp->bh_bnum, NULL);
686  hp->bh_bnum = new_bnum;
687  pmap_put(int64_t)(&mfp->mf_hash, new_bnum, hp);
688
689  // Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum".
690  map_put(int64_t, int64_t)(&mfp->mf_trans, old_bnum, new_bnum);
691
692  return 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{
701  blocknr_T *num = map_ref(int64_t, int64_t)(&mfp->mf_trans, old_nr, NULL);
702  if (num == NULL) {  // not found
703    return old_nr;
704  }
705
706  mfp->mf_neg_count--;
707  blocknr_T new_bnum = *num;
708
709  // remove entry from the trans list
710  map_del(int64_t, int64_t)(&mfp->mf_trans, old_nr, NULL);
711
712  return new_bnum;
713}
714
715/// Frees mf_fname and mf_ffname.
716void mf_free_fnames(memfile_T *mfp)
717{
718  XFREE_CLEAR(mfp->mf_fname);
719  XFREE_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{
729  mfp->mf_fname = fname;
730  mfp->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{
738  if (mfp == NULL || mfp->mf_fname == NULL || mfp->mf_ffname == NULL) {
739    return;
740  }
741
742  xfree(mfp->mf_fname);
743  mfp->mf_fname = mfp->mf_ffname;
744  mfp->mf_ffname = NULL;
745}
746
747/// Return true if there are any translations pending for memfile.
748bool mf_need_trans(memfile_T *mfp)
749{
750  return 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.
762  mf_set_fnames(mfp, fname);
763  assert(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.
767  FileInfo file_info;
768  if ((flags & O_CREAT)
769      && os_fileinfo_link(mfp->mf_fname, &file_info)) {
770    mfp->mf_fd = -1;
771    emsg(_("E300: Swap file already exists (symlink attack?)"));
772  } else {
773    // try to open the file
774    flags |= O_NOFOLLOW;
775    mfp->mf_flags = flags;
776    mfp->mf_fd = os_open(mfp->mf_fname, flags, S_IREAD | S_IWRITE);
777  }
778
779  // If the file cannot be opened, use memory only
780  if (mfp->mf_fd < 0) {
781    mf_free_fnames(mfp);
782    return false;
783  }
784
785  os_set_cloexec(mfp->mf_fd);
786
787  return true;
788}