xref: /vim-8.2.3635/src/memfile.c (revision 00a927d6)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * memfile.c: Contains the functions for handling blocks of memory which can
12  * be stored in a file. This is the implementation of a sort of virtual memory.
13  *
14  * A memfile consists of a sequence of blocks. The blocks numbered from 0
15  * upwards have been assigned a place in the actual file. The block number
16  * is equal to the page number in the file. The
17  * blocks with negative numbers are currently in memory only. They can be
18  * assigned a place in the file when too much memory is being used. At that
19  * moment they get a new, positive, number. A list is used for translation of
20  * negative to positive numbers.
21  *
22  * The size of a block is a multiple of a page size, normally the page size of
23  * the device the file is on. Most blocks are 1 page long. A Block of multiple
24  * pages is used for a line that does not fit in a single page.
25  *
26  * Each block can be in memory and/or in a file. The block stays in memory
27  * as long as it is locked. If it is no longer locked it can be swapped out to
28  * the file. It is only written to the file if it has been changed.
29  *
30  * Under normal operation the file is created when opening the memory file and
31  * deleted when closing the memory file. Only with recovery an existing memory
32  * file is opened.
33  */
34 
35 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
36 # include "vimio.h"	/* for lseek(), must be before vim.h */
37 #endif
38 
39 #include "vim.h"
40 
41 /*
42  * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
43  */
44 #ifdef HAVE_ST_BLKSIZE
45 # define STATFS stat
46 # define F_BSIZE st_blksize
47 # define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
48 #else
49 # ifdef HAVE_SYS_STATFS_H
50 #  include <sys/statfs.h>
51 #  define STATFS statfs
52 #  define F_BSIZE f_bsize
53 #  ifdef __MINT__		/* do we still need this? */
54 #   define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
55 #  endif
56 # endif
57 #endif
58 
59 /*
60  * for Amiga Dos 2.0x we use Flush
61  */
62 #ifdef AMIGA
63 # ifdef FEAT_ARP
64 extern int dos2;			/* this is in os_amiga.c */
65 # endif
66 # ifdef SASC
67 #  include <proto/dos.h>
68 #  include <ios1.h>			/* for chkufb() */
69 # endif
70 #endif
71 
72 #define MEMFILE_PAGE_SIZE 4096		/* default page size */
73 
74 static long_u	total_mem_used = 0;	/* total memory used for memfiles */
75 
76 static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
77 static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
78 static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
79 static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
80 static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
81 static bhdr_T *mf_release __ARGS((memfile_T *, int));
82 static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
83 static void mf_free_bhdr __ARGS((bhdr_T *));
84 static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
85 static bhdr_T *mf_rem_free __ARGS((memfile_T *));
86 static int  mf_read __ARGS((memfile_T *, bhdr_T *));
87 static int  mf_write __ARGS((memfile_T *, bhdr_T *));
88 static int  mf_trans_add __ARGS((memfile_T *, bhdr_T *));
89 static void mf_do_open __ARGS((memfile_T *, char_u *, int));
90 
91 /*
92  * The functions for using a memfile:
93  *
94  * mf_open()	    open a new or existing memfile
95  * mf_open_file()   open a swap file for an existing memfile
96  * mf_close()	    close (and delete) a memfile
97  * mf_new()	    create a new block in a memfile and lock it
98  * mf_get()	    get an existing block and lock it
99  * mf_put()	    unlock a block, may be marked for writing
100  * mf_free()	    remove a block
101  * mf_sync()	    sync changed parts of memfile to disk
102  * mf_release_all() release as much memory as possible
103  * mf_trans_del()   may translate negative to positive block number
104  * mf_fullname()    make file name full path (use before first :cd)
105  */
106 
107 /*
108  * Open an existing or new memory block file.
109  *
110  *  fname:	name of file to use (NULL means no file at all)
111  *		Note: fname must have been allocated, it is not copied!
112  *			If opening the file fails, fname is freed.
113  *  flags:	flags for open() call
114  *
115  *  If fname != NULL and file cannot be opened, fail.
116  *
117  * return value: identifier for this memory block file.
118  */
119     memfile_T *
120 mf_open(fname, flags)
121     char_u	*fname;
122     int		flags;
123 {
124     memfile_T		*mfp;
125     int			i;
126     off_t		size;
127 #if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
128 # define USE_FSTATFS
129     struct STATFS	stf;
130 #endif
131 
132     if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
133 	return NULL;
134 
135     if (fname == NULL)	    /* no file for this memfile, use memory only */
136     {
137 	mfp->mf_fname = NULL;
138 	mfp->mf_ffname = NULL;
139 	mfp->mf_fd = -1;
140     }
141     else
142     {
143 	mf_do_open(mfp, fname, flags);	/* try to open the file */
144 
145 	/* if the file cannot be opened, return here */
146 	if (mfp->mf_fd < 0)
147 	{
148 	    vim_free(mfp);
149 	    return NULL;
150 	}
151     }
152 
153     mfp->mf_free_first = NULL;		/* free list is empty */
154     mfp->mf_used_first = NULL;		/* used list is empty */
155     mfp->mf_used_last = NULL;
156     mfp->mf_dirty = FALSE;
157     mfp->mf_used_count = 0;
158     for (i = 0; i < MEMHASHSIZE; ++i)
159     {
160 	mfp->mf_hash[i] = NULL;		/* hash lists are empty */
161 	mfp->mf_trans[i] = NULL;	/* trans lists are empty */
162     }
163     mfp->mf_page_size = MEMFILE_PAGE_SIZE;
164 
165 #ifdef USE_FSTATFS
166     /*
167      * Try to set the page size equal to the block size of the device.
168      * Speeds up I/O a lot.
169      * When recovering, the actual block size will be retrieved from block 0
170      * in ml_recover().  The size used here may be wrong, therefore
171      * mf_blocknr_max must be rounded up.
172      */
173     if (mfp->mf_fd >= 0
174 	    && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
175 	    && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
176 	    && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
177 	mfp->mf_page_size = stf.F_BSIZE;
178 #endif
179 
180     if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
181 		      || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
182 	mfp->mf_blocknr_max = 0;	/* no file or empty file */
183     else
184 	mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
185 							 / mfp->mf_page_size);
186     mfp->mf_blocknr_min = -1;
187     mfp->mf_neg_count = 0;
188     mfp->mf_infile_count = mfp->mf_blocknr_max;
189 
190     /*
191      * Compute maximum number of pages ('maxmem' is in Kbyte):
192      *	'mammem' * 1Kbyte / page-size-in-bytes.
193      * Avoid overflow by first reducing page size as much as possible.
194      */
195     {
196 	int	    shift = 10;
197 	unsigned    page_size = mfp->mf_page_size;
198 
199 	while (shift > 0 && (page_size & 1) == 0)
200 	{
201 	    page_size = page_size >> 1;
202 	    --shift;
203 	}
204 	mfp->mf_used_count_max = (p_mm << shift) / page_size;
205 	if (mfp->mf_used_count_max < 10)
206 	    mfp->mf_used_count_max = 10;
207     }
208 
209     return mfp;
210 }
211 
212 /*
213  * Open a file for an existing memfile.  Used when updatecount set from 0 to
214  * some value.
215  * If the file already exists, this fails.
216  * "fname" is the name of file to use (NULL means no file at all)
217  * Note: "fname" must have been allocated, it is not copied!  If opening the
218  * file fails, "fname" is freed.
219  *
220  * return value: FAIL if file could not be opened, OK otherwise
221  */
222     int
223 mf_open_file(mfp, fname)
224     memfile_T	*mfp;
225     char_u	*fname;
226 {
227     mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
228 
229     if (mfp->mf_fd < 0)
230 	return FAIL;
231 
232     mfp->mf_dirty = TRUE;
233     return OK;
234 }
235 
236 /*
237  * close a memory file and delete the associated file if 'del_file' is TRUE
238  */
239     void
240 mf_close(mfp, del_file)
241     memfile_T	*mfp;
242     int		del_file;
243 {
244     bhdr_T	*hp, *nextp;
245     NR_TRANS	*tp, *tpnext;
246     int		i;
247 
248     if (mfp == NULL)		    /* safety check */
249 	return;
250     if (mfp->mf_fd >= 0)
251     {
252 	if (close(mfp->mf_fd) < 0)
253 	    EMSG(_(e_swapclose));
254     }
255     if (del_file && mfp->mf_fname != NULL)
256 	mch_remove(mfp->mf_fname);
257 					    /* free entries in used list */
258     for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
259     {
260 	total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
261 	nextp = hp->bh_next;
262 	mf_free_bhdr(hp);
263     }
264     while (mfp->mf_free_first != NULL)	    /* free entries in free list */
265 	vim_free(mf_rem_free(mfp));
266     for (i = 0; i < MEMHASHSIZE; ++i)	    /* free entries in trans lists */
267 	for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
268 	{
269 	    tpnext = tp->nt_next;
270 	    vim_free(tp);
271 	}
272     vim_free(mfp->mf_fname);
273     vim_free(mfp->mf_ffname);
274     vim_free(mfp);
275 }
276 
277 /*
278  * Close the swap file for a memfile.  Used when 'swapfile' is reset.
279  */
280     void
281 mf_close_file(buf, getlines)
282     buf_T	*buf;
283     int		getlines;	/* get all lines into memory? */
284 {
285     memfile_T	*mfp;
286     linenr_T	lnum;
287 
288     mfp = buf->b_ml.ml_mfp;
289     if (mfp == NULL || mfp->mf_fd < 0)		/* nothing to close */
290 	return;
291 
292     if (getlines)
293     {
294 	/* get all blocks in memory by accessing all lines (clumsy!) */
295 	mf_dont_release = TRUE;
296 	for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
297 	    (void)ml_get_buf(buf, lnum, FALSE);
298 	mf_dont_release = FALSE;
299 	/* TODO: should check if all blocks are really in core */
300     }
301 
302     if (close(mfp->mf_fd) < 0)			/* close the file */
303 	EMSG(_(e_swapclose));
304     mfp->mf_fd = -1;
305 
306     if (mfp->mf_fname != NULL)
307     {
308 	mch_remove(mfp->mf_fname);		/* delete the swap file */
309 	vim_free(mfp->mf_fname);
310 	vim_free(mfp->mf_ffname);
311 	mfp->mf_fname = NULL;
312 	mfp->mf_ffname = NULL;
313     }
314 }
315 
316 /*
317  * Set new size for a memfile.  Used when block 0 of a swapfile has been read
318  * and the size it indicates differs from what was guessed.
319  */
320     void
321 mf_new_page_size(mfp, new_size)
322     memfile_T	*mfp;
323     unsigned	new_size;
324 {
325     /* Correct the memory used for block 0 to the new size, because it will be
326      * freed with that size later on. */
327     total_mem_used += new_size - mfp->mf_page_size;
328     mfp->mf_page_size = new_size;
329 }
330 
331 /*
332  * get a new block
333  *
334  *   negative: TRUE if negative block number desired (data block)
335  */
336     bhdr_T *
337 mf_new(mfp, negative, page_count)
338     memfile_T	*mfp;
339     int		negative;
340     int		page_count;
341 {
342     bhdr_T	*hp;	/* new bhdr_T */
343     bhdr_T	*freep;	/* first block in free list */
344     char_u	*p;
345 
346     /*
347      * If we reached the maximum size for the used memory blocks, release one
348      * If a bhdr_T is returned, use it and adjust the page_count if necessary.
349      */
350     hp = mf_release(mfp, page_count);
351 
352 /*
353  * Decide on the number to use:
354  * If there is a free block, use its number.
355  * Otherwise use mf_block_min for a negative number, mf_block_max for
356  * a positive number.
357  */
358     freep = mfp->mf_free_first;
359     if (!negative && freep != NULL && freep->bh_page_count >= page_count)
360     {
361 	/*
362 	 * If the block in the free list has more pages, take only the number
363 	 * of pages needed and allocate a new bhdr_T with data
364 	 *
365 	 * If the number of pages matches and mf_release() did not return a
366 	 * bhdr_T, use the bhdr_T from the free list and allocate the data
367 	 *
368 	 * If the number of pages matches and mf_release() returned a bhdr_T,
369 	 * just use the number and free the bhdr_T from the free list
370 	 */
371 	if (freep->bh_page_count > page_count)
372 	{
373 	    if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
374 		return NULL;
375 	    hp->bh_bnum = freep->bh_bnum;
376 	    freep->bh_bnum += page_count;
377 	    freep->bh_page_count -= page_count;
378 	}
379 	else if (hp == NULL)	    /* need to allocate memory for this block */
380 	{
381 	    if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
382 		return NULL;
383 	    hp = mf_rem_free(mfp);
384 	    hp->bh_data = p;
385 	}
386 	else		    /* use the number, remove entry from free list */
387 	{
388 	    freep = mf_rem_free(mfp);
389 	    hp->bh_bnum = freep->bh_bnum;
390 	    vim_free(freep);
391 	}
392     }
393     else	/* get a new number */
394     {
395 	if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
396 	    return NULL;
397 	if (negative)
398 	{
399 	    hp->bh_bnum = mfp->mf_blocknr_min--;
400 	    mfp->mf_neg_count++;
401 	}
402 	else
403 	{
404 	    hp->bh_bnum = mfp->mf_blocknr_max;
405 	    mfp->mf_blocknr_max += page_count;
406 	}
407     }
408     hp->bh_flags = BH_LOCKED | BH_DIRTY;	/* new block is always dirty */
409     mfp->mf_dirty = TRUE;
410     hp->bh_page_count = page_count;
411     mf_ins_used(mfp, hp);
412     mf_ins_hash(mfp, hp);
413 
414     /*
415      * Init the data to all zero, to avoid reading uninitialized data.
416      * This also avoids that the passwd file ends up in the swap file!
417      */
418     (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
419 
420     return hp;
421 }
422 
423 /*
424  * get existing block 'nr' with 'page_count' pages
425  *
426  * Note: The caller should first check a negative nr with mf_trans_del()
427  */
428     bhdr_T *
429 mf_get(mfp, nr, page_count)
430     memfile_T	*mfp;
431     blocknr_T	nr;
432     int		page_count;
433 {
434     bhdr_T    *hp;
435 						/* doesn't exist */
436     if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
437 	return NULL;
438 
439     /*
440      * see if it is in the cache
441      */
442     hp = mf_find_hash(mfp, nr);
443     if (hp == NULL)	/* not in the hash list */
444     {
445 	if (nr < 0 || nr >= mfp->mf_infile_count)   /* can't be in the file */
446 	    return NULL;
447 
448 	/* could check here if the block is in the free list */
449 
450 	/*
451 	 * Check if we need to flush an existing block.
452 	 * If so, use that block.
453 	 * If not, allocate a new block.
454 	 */
455 	hp = mf_release(mfp, page_count);
456 	if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
457 	    return NULL;
458 
459 	hp->bh_bnum = nr;
460 	hp->bh_flags = 0;
461 	hp->bh_page_count = page_count;
462 	if (mf_read(mfp, hp) == FAIL)	    /* cannot read the block! */
463 	{
464 	    mf_free_bhdr(hp);
465 	    return NULL;
466 	}
467     }
468     else
469     {
470 	mf_rem_used(mfp, hp);	/* remove from list, insert in front below */
471 	mf_rem_hash(mfp, hp);
472     }
473 
474     hp->bh_flags |= BH_LOCKED;
475     mf_ins_used(mfp, hp);	/* put in front of used list */
476     mf_ins_hash(mfp, hp);	/* put in front of hash list */
477 
478     return hp;
479 }
480 
481 /*
482  * release the block *hp
483  *
484  *   dirty: Block must be written to file later
485  *   infile: Block should be in file (needed for recovery)
486  *
487  *  no return value, function cannot fail
488  */
489     void
490 mf_put(mfp, hp, dirty, infile)
491     memfile_T	*mfp;
492     bhdr_T	*hp;
493     int		dirty;
494     int		infile;
495 {
496     int		flags;
497 
498     flags = hp->bh_flags;
499 
500     if ((flags & BH_LOCKED) == 0)
501 	EMSG(_("E293: block was not locked"));
502     flags &= ~BH_LOCKED;
503     if (dirty)
504     {
505 	flags |= BH_DIRTY;
506 	mfp->mf_dirty = TRUE;
507     }
508     hp->bh_flags = flags;
509     if (infile)
510 	mf_trans_add(mfp, hp);	    /* may translate negative in positive nr */
511 }
512 
513 /*
514  * block *hp is no longer in used, may put it in the free list of memfile *mfp
515  */
516     void
517 mf_free(mfp, hp)
518     memfile_T	*mfp;
519     bhdr_T	*hp;
520 {
521     vim_free(hp->bh_data);	/* free the memory */
522     mf_rem_hash(mfp, hp);	/* get *hp out of the hash list */
523     mf_rem_used(mfp, hp);	/* get *hp out of the used list */
524     if (hp->bh_bnum < 0)
525     {
526 	vim_free(hp);		/* don't want negative numbers in free list */
527 	mfp->mf_neg_count--;
528     }
529     else
530 	mf_ins_free(mfp, hp);	/* put *hp in the free list */
531 }
532 
533 #if defined(__MORPHOS__) && defined(__libnix__)
534 /* function is missing in MorphOS libnix version */
535 extern unsigned long *__stdfiledes;
536 
537     static unsigned long
538 fdtofh(int filedescriptor)
539 {
540     return __stdfiledes[filedescriptor];
541 }
542 #endif
543 
544 /*
545  * Sync the memory file *mfp to disk.
546  * Flags:
547  *  MFS_ALL	If not given, blocks with negative numbers are not synced,
548  *		even when they are dirty!
549  *  MFS_STOP	Stop syncing when a character becomes available, but sync at
550  *		least one block.
551  *  MFS_FLUSH	Make sure buffers are flushed to disk, so they will survive a
552  *		system crash.
553  *  MFS_ZERO	Only write block 0.
554  *
555  * Return FAIL for failure, OK otherwise
556  */
557     int
558 mf_sync(mfp, flags)
559     memfile_T	*mfp;
560     int		flags;
561 {
562     int		status;
563     bhdr_T	*hp;
564 #if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
565     int		fd;
566 #endif
567     int		got_int_save = got_int;
568 
569     if (mfp->mf_fd < 0)	    /* there is no file, nothing to do */
570     {
571 	mfp->mf_dirty = FALSE;
572 	return FAIL;
573     }
574 
575     /* Only a CTRL-C while writing will break us here, not one typed
576      * previously. */
577     got_int = FALSE;
578 
579     /*
580      * sync from last to first (may reduce the probability of an inconsistent
581      * file) If a write fails, it is very likely caused by a full filesystem.
582      * Then we only try to write blocks within the existing file. If that also
583      * fails then we give up.
584      */
585     status = OK;
586     for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
587 	if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
588 		&& (hp->bh_flags & BH_DIRTY)
589 		&& (status == OK || (hp->bh_bnum >= 0
590 		    && hp->bh_bnum < mfp->mf_infile_count)))
591 	{
592 	    if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
593 		continue;
594 	    if (mf_write(mfp, hp) == FAIL)
595 	    {
596 		if (status == FAIL)	/* double error: quit syncing */
597 		    break;
598 		status = FAIL;
599 	    }
600 	    if (flags & MFS_STOP)
601 	    {
602 		/* Stop when char available now. */
603 		if (ui_char_avail())
604 		    break;
605 	    }
606 	    else
607 		ui_breakcheck();
608 	    if (got_int)
609 		break;
610 	}
611 
612     /*
613      * If the whole list is flushed, the memfile is not dirty anymore.
614      * In case of an error this flag is also set, to avoid trying all the time.
615      */
616     if (hp == NULL || status == FAIL)
617 	mfp->mf_dirty = FALSE;
618 
619     if ((flags & MFS_FLUSH) && *p_sws != NUL)
620     {
621 #if defined(UNIX)
622 # ifdef HAVE_FSYNC
623 	/*
624 	 * most Unixes have the very useful fsync() function, just what we need.
625 	 * However, with OS/2 and EMX it is also available, but there are
626 	 * reports of bad problems with it (a bug in HPFS.IFS).
627 	 * So we disable use of it here in case someone tries to be smart
628 	 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
629 	 * in the #if, as __EMX__ does not have sync(); we hope for a timely
630 	 * sync from the system itself).
631 	 */
632 #  if defined(__EMX__)
633    error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
634 #  endif
635 	if (STRCMP(p_sws, "fsync") == 0)
636 	{
637 	    if (fsync(mfp->mf_fd))
638 		status = FAIL;
639 	}
640 	else
641 # endif
642 	    /* OpenNT is strictly POSIX (Benzinger) */
643 	    /* Tandem/Himalaya NSK-OSS doesn't have sync() */
644 # if defined(__OPENNT) || defined(__TANDEM)
645 	    fflush(NULL);
646 # else
647 	    sync();
648 # endif
649 #endif
650 #ifdef VMS
651 	if (STRCMP(p_sws, "fsync") == 0)
652 	{
653 	    if (fsync(mfp->mf_fd))
654 		status = FAIL;
655 	}
656 #endif
657 #ifdef MSDOS
658 	if (_dos_commit(mfp->mf_fd))
659 	    status = FAIL;
660 #else
661 # ifdef SYNC_DUP_CLOSE
662 	/*
663 	 * Win32 is a bit more work: Duplicate the file handle and close it.
664 	 * This should flush the file to disk.
665 	 */
666 	if ((fd = dup(mfp->mf_fd)) >= 0)
667 	    close(fd);
668 # endif
669 #endif
670 #ifdef AMIGA
671 # if defined(__AROS__) || defined(__amigaos4__)
672 	if (fsync(mfp->mf_fd) != 0)
673 	    status = FAIL;
674 # else
675 	/*
676 	 * Flush() only exists for AmigaDos 2.0.
677 	 * For 1.3 it should be done with close() + open(), but then the risk
678 	 * is that the open() may fail and lose the file....
679 	 */
680 #  ifdef FEAT_ARP
681 	if (dos2)
682 #  endif
683 #  ifdef SASC
684 	{
685 	    struct UFB *fp = chkufb(mfp->mf_fd);
686 
687 	    if (fp != NULL)
688 		Flush(fp->ufbfh);
689 	}
690 #  else
691 #   if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
692 	{
693 #    if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
694 	    /* Have function (in libnix at least),
695 	     * but ain't got no prototype anywhere. */
696 	    extern unsigned long fdtofh(int filedescriptor);
697 #    endif
698 #    if !defined(__libnix__)
699 	    fflush(NULL);
700 #    else
701 	    BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
702 
703 	    if (fh != 0)
704 		Flush(fh);
705 #    endif
706 	}
707 #   else /* assume Manx */
708 	    Flush(_devtab[mfp->mf_fd].fd);
709 #   endif
710 #  endif
711 # endif
712 #endif /* AMIGA */
713     }
714 
715     got_int |= got_int_save;
716 
717     return status;
718 }
719 
720 /*
721  * For all blocks in memory file *mfp that have a positive block number set
722  * the dirty flag.  These are blocks that need to be written to a newly
723  * created swapfile.
724  */
725     void
726 mf_set_dirty(mfp)
727     memfile_T	*mfp;
728 {
729     bhdr_T	*hp;
730 
731     for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
732 	if (hp->bh_bnum > 0)
733 	    hp->bh_flags |= BH_DIRTY;
734     mfp->mf_dirty = TRUE;
735 }
736 
737 /*
738  * insert block *hp in front of hashlist of memfile *mfp
739  */
740     static void
741 mf_ins_hash(mfp, hp)
742     memfile_T	*mfp;
743     bhdr_T	*hp;
744 {
745     bhdr_T	*hhp;
746     int		hash;
747 
748     hash = MEMHASH(hp->bh_bnum);
749     hhp = mfp->mf_hash[hash];
750     hp->bh_hash_next = hhp;
751     hp->bh_hash_prev = NULL;
752     if (hhp != NULL)
753 	hhp->bh_hash_prev = hp;
754     mfp->mf_hash[hash] = hp;
755 }
756 
757 /*
758  * remove block *hp from hashlist of memfile list *mfp
759  */
760     static void
761 mf_rem_hash(mfp, hp)
762     memfile_T	*mfp;
763     bhdr_T	*hp;
764 {
765     if (hp->bh_hash_prev == NULL)
766 	mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
767     else
768 	hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
769 
770     if (hp->bh_hash_next)
771 	hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
772 }
773 
774 /*
775  * look in hash lists of memfile *mfp for block header with number 'nr'
776  */
777     static bhdr_T *
778 mf_find_hash(mfp, nr)
779     memfile_T	*mfp;
780     blocknr_T	nr;
781 {
782     bhdr_T	*hp;
783 
784     for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
785 	if (hp->bh_bnum == nr)
786 	    break;
787     return hp;
788 }
789 
790 /*
791  * insert block *hp in front of used list of memfile *mfp
792  */
793     static void
794 mf_ins_used(mfp, hp)
795     memfile_T	*mfp;
796     bhdr_T	*hp;
797 {
798     hp->bh_next = mfp->mf_used_first;
799     mfp->mf_used_first = hp;
800     hp->bh_prev = NULL;
801     if (hp->bh_next == NULL)	    /* list was empty, adjust last pointer */
802 	mfp->mf_used_last = hp;
803     else
804 	hp->bh_next->bh_prev = hp;
805     mfp->mf_used_count += hp->bh_page_count;
806     total_mem_used += hp->bh_page_count * mfp->mf_page_size;
807 }
808 
809 /*
810  * remove block *hp from used list of memfile *mfp
811  */
812     static void
813 mf_rem_used(mfp, hp)
814     memfile_T	*mfp;
815     bhdr_T	*hp;
816 {
817     if (hp->bh_next == NULL)	    /* last block in used list */
818 	mfp->mf_used_last = hp->bh_prev;
819     else
820 	hp->bh_next->bh_prev = hp->bh_prev;
821     if (hp->bh_prev == NULL)	    /* first block in used list */
822 	mfp->mf_used_first = hp->bh_next;
823     else
824 	hp->bh_prev->bh_next = hp->bh_next;
825     mfp->mf_used_count -= hp->bh_page_count;
826     total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
827 }
828 
829 /*
830  * Release the least recently used block from the used list if the number
831  * of used memory blocks gets to big.
832  *
833  * Return the block header to the caller, including the memory block, so
834  * it can be re-used. Make sure the page_count is right.
835  */
836     static bhdr_T *
837 mf_release(mfp, page_count)
838     memfile_T	*mfp;
839     int		page_count;
840 {
841     bhdr_T	*hp;
842     int		need_release;
843     buf_T	*buf;
844 
845     /* don't release while in mf_close_file() */
846     if (mf_dont_release)
847 	return NULL;
848 
849     /*
850      * Need to release a block if the number of blocks for this memfile is
851      * higher than the maximum or total memory used is over 'maxmemtot'
852      */
853     need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
854 				  || (total_mem_used >> 10) >= (long_u)p_mmt);
855 
856     /*
857      * Try to create a swap file if the amount of memory used is getting too
858      * high.
859      */
860     if (mfp->mf_fd < 0 && need_release && p_uc)
861     {
862 	/* find for which buffer this memfile is */
863 	for (buf = firstbuf; buf != NULL; buf = buf->b_next)
864 	    if (buf->b_ml.ml_mfp == mfp)
865 		break;
866 	if (buf != NULL && buf->b_may_swap)
867 	    ml_open_file(buf);
868     }
869 
870     /*
871      * don't release a block if
872      *	there is no file for this memfile
873      * or
874      *	the number of blocks for this memfile is lower than the maximum
875      *	  and
876      *	total memory used is not up to 'maxmemtot'
877      */
878     if (mfp->mf_fd < 0 || !need_release)
879 	return NULL;
880 
881     for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
882 	if (!(hp->bh_flags & BH_LOCKED))
883 	    break;
884     if (hp == NULL)	/* not a single one that can be released */
885 	return NULL;
886 
887     /*
888      * If the block is dirty, write it.
889      * If the write fails we don't free it.
890      */
891     if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
892 	return NULL;
893 
894     mf_rem_used(mfp, hp);
895     mf_rem_hash(mfp, hp);
896 
897     /*
898      * If a bhdr_T is returned, make sure that the page_count of bh_data is
899      * right
900      */
901     if (hp->bh_page_count != page_count)
902     {
903 	vim_free(hp->bh_data);
904 	if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
905 	{
906 	    vim_free(hp);
907 	    return NULL;
908 	}
909 	hp->bh_page_count = page_count;
910     }
911     return hp;
912 }
913 
914 /*
915  * release as many blocks as possible
916  * Used in case of out of memory
917  *
918  * return TRUE if any memory was released
919  */
920     int
921 mf_release_all()
922 {
923     buf_T	*buf;
924     memfile_T	*mfp;
925     bhdr_T	*hp;
926     int		retval = FALSE;
927 
928     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
929     {
930 	mfp = buf->b_ml.ml_mfp;
931 	if (mfp != NULL)
932 	{
933 	    /* If no swap file yet, may open one */
934 	    if (mfp->mf_fd < 0 && buf->b_may_swap)
935 		ml_open_file(buf);
936 
937 	    /* only if there is a swapfile */
938 	    if (mfp->mf_fd >= 0)
939 	    {
940 		for (hp = mfp->mf_used_last; hp != NULL; )
941 		{
942 		    if (!(hp->bh_flags & BH_LOCKED)
943 			    && (!(hp->bh_flags & BH_DIRTY)
944 				|| mf_write(mfp, hp) != FAIL))
945 		    {
946 			mf_rem_used(mfp, hp);
947 			mf_rem_hash(mfp, hp);
948 			mf_free_bhdr(hp);
949 			hp = mfp->mf_used_last;	/* re-start, list was changed */
950 			retval = TRUE;
951 		    }
952 		    else
953 			hp = hp->bh_prev;
954 		}
955 	    }
956 	}
957     }
958     return retval;
959 }
960 
961 /*
962  * Allocate a block header and a block of memory for it
963  */
964     static bhdr_T *
965 mf_alloc_bhdr(mfp, page_count)
966     memfile_T	*mfp;
967     int		page_count;
968 {
969     bhdr_T	*hp;
970 
971     if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
972     {
973 	if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
974 								      == NULL)
975 	{
976 	    vim_free(hp);	    /* not enough memory */
977 	    return NULL;
978 	}
979 	hp->bh_page_count = page_count;
980     }
981     return hp;
982 }
983 
984 /*
985  * Free a block header and the block of memory for it
986  */
987     static void
988 mf_free_bhdr(hp)
989     bhdr_T	*hp;
990 {
991     vim_free(hp->bh_data);
992     vim_free(hp);
993 }
994 
995 /*
996  * insert entry *hp in the free list
997  */
998     static void
999 mf_ins_free(mfp, hp)
1000     memfile_T	*mfp;
1001     bhdr_T	*hp;
1002 {
1003     hp->bh_next = mfp->mf_free_first;
1004     mfp->mf_free_first = hp;
1005 }
1006 
1007 /*
1008  * remove the first entry from the free list and return a pointer to it
1009  * Note: caller must check that mfp->mf_free_first is not NULL!
1010  */
1011     static bhdr_T *
1012 mf_rem_free(mfp)
1013     memfile_T	*mfp;
1014 {
1015     bhdr_T	*hp;
1016 
1017     hp = mfp->mf_free_first;
1018     mfp->mf_free_first = hp->bh_next;
1019     return hp;
1020 }
1021 
1022 /*
1023  * read a block from disk
1024  *
1025  * Return FAIL for failure, OK otherwise
1026  */
1027     static int
1028 mf_read(mfp, hp)
1029     memfile_T	*mfp;
1030     bhdr_T	*hp;
1031 {
1032     off_t	offset;
1033     unsigned	page_size;
1034     unsigned	size;
1035 
1036     if (mfp->mf_fd < 0)	    /* there is no file, can't read */
1037 	return FAIL;
1038 
1039     page_size = mfp->mf_page_size;
1040     offset = (off_t)page_size * hp->bh_bnum;
1041     size = page_size * hp->bh_page_count;
1042     if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1043     {
1044 	PERROR(_("E294: Seek error in swap file read"));
1045 	return FAIL;
1046     }
1047     if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1048     {
1049 	PERROR(_("E295: Read error in swap file"));
1050 	return FAIL;
1051     }
1052     return OK;
1053 }
1054 
1055 /*
1056  * write a block to disk
1057  *
1058  * Return FAIL for failure, OK otherwise
1059  */
1060     static int
1061 mf_write(mfp, hp)
1062     memfile_T	*mfp;
1063     bhdr_T	*hp;
1064 {
1065     off_t	offset;	    /* offset in the file */
1066     blocknr_T	nr;	    /* block nr which is being written */
1067     bhdr_T	*hp2;
1068     unsigned	page_size;  /* number of bytes in a page */
1069     unsigned	page_count; /* number of pages written */
1070     unsigned	size;	    /* number of bytes written */
1071 
1072     if (mfp->mf_fd < 0)	    /* there is no file, can't write */
1073 	return FAIL;
1074 
1075     if (hp->bh_bnum < 0)	/* must assign file block number */
1076 	if (mf_trans_add(mfp, hp) == FAIL)
1077 	    return FAIL;
1078 
1079     page_size = mfp->mf_page_size;
1080 
1081     /*
1082      * We don't want gaps in the file. Write the blocks in front of *hp
1083      * to extend the file.
1084      * If block 'mf_infile_count' is not in the hash list, it has been
1085      * freed. Fill the space in the file with data from the current block.
1086      */
1087     for (;;)
1088     {
1089 	nr = hp->bh_bnum;
1090 	if (nr > mfp->mf_infile_count)		/* beyond end of file */
1091 	{
1092 	    nr = mfp->mf_infile_count;
1093 	    hp2 = mf_find_hash(mfp, nr);	/* NULL catched below */
1094 	}
1095 	else
1096 	    hp2 = hp;
1097 
1098 	offset = (off_t)page_size * nr;
1099 	if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1100 	{
1101 	    PERROR(_("E296: Seek error in swap file write"));
1102 	    return FAIL;
1103 	}
1104 	if (hp2 == NULL)	    /* freed block, fill with dummy data */
1105 	    page_count = 1;
1106 	else
1107 	    page_count = hp2->bh_page_count;
1108 	size = page_size * page_count;
1109 	if ((unsigned)vim_write(mfp->mf_fd,
1110 	     (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
1111 	{
1112 	    /*
1113 	     * Avoid repeating the error message, this mostly happens when the
1114 	     * disk is full. We give the message again only after a successful
1115 	     * write or when hitting a key. We keep on trying, in case some
1116 	     * space becomes available.
1117 	     */
1118 	    if (!did_swapwrite_msg)
1119 		EMSG(_("E297: Write error in swap file"));
1120 	    did_swapwrite_msg = TRUE;
1121 	    return FAIL;
1122 	}
1123 	did_swapwrite_msg = FALSE;
1124 	if (hp2 != NULL)		    /* written a non-dummy block */
1125 	    hp2->bh_flags &= ~BH_DIRTY;
1126 					    /* appended to the file */
1127 	if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1128 	    mfp->mf_infile_count = nr + page_count;
1129 	if (nr == hp->bh_bnum)		    /* written the desired block */
1130 	    break;
1131     }
1132     return OK;
1133 }
1134 
1135 /*
1136  * Make block number for *hp positive and add it to the translation list
1137  *
1138  * Return FAIL for failure, OK otherwise
1139  */
1140     static int
1141 mf_trans_add(mfp, hp)
1142     memfile_T	*mfp;
1143     bhdr_T	*hp;
1144 {
1145     bhdr_T	*freep;
1146     blocknr_T	new_bnum;
1147     int		hash;
1148     NR_TRANS	*np;
1149     int		page_count;
1150 
1151     if (hp->bh_bnum >= 0)		    /* it's already positive */
1152 	return OK;
1153 
1154     if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1155 	return FAIL;
1156 
1157 /*
1158  * get a new number for the block.
1159  * If the first item in the free list has sufficient pages, use its number
1160  * Otherwise use mf_blocknr_max.
1161  */
1162     freep = mfp->mf_free_first;
1163     page_count = hp->bh_page_count;
1164     if (freep != NULL && freep->bh_page_count >= page_count)
1165     {
1166 	new_bnum = freep->bh_bnum;
1167 	/*
1168 	 * If the page count of the free block was larger, recude it.
1169 	 * If the page count matches, remove the block from the free list
1170 	 */
1171 	if (freep->bh_page_count > page_count)
1172 	{
1173 	    freep->bh_bnum += page_count;
1174 	    freep->bh_page_count -= page_count;
1175 	}
1176 	else
1177 	{
1178 	    freep = mf_rem_free(mfp);
1179 	    vim_free(freep);
1180 	}
1181     }
1182     else
1183     {
1184 	new_bnum = mfp->mf_blocknr_max;
1185 	mfp->mf_blocknr_max += page_count;
1186     }
1187 
1188     np->nt_old_bnum = hp->bh_bnum;	    /* adjust number */
1189     np->nt_new_bnum = new_bnum;
1190 
1191     mf_rem_hash(mfp, hp);		    /* remove from old hash list */
1192     hp->bh_bnum = new_bnum;
1193     mf_ins_hash(mfp, hp);		    /* insert in new hash list */
1194 
1195     hash = MEMHASH(np->nt_old_bnum);	    /* insert in trans list */
1196     np->nt_next = mfp->mf_trans[hash];
1197     mfp->mf_trans[hash] = np;
1198     if (np->nt_next != NULL)
1199 	np->nt_next->nt_prev = np;
1200     np->nt_prev = NULL;
1201 
1202     return OK;
1203 }
1204 
1205 /*
1206  * Lookup a translation from the trans lists and delete the entry
1207  *
1208  * Return the positive new number when found, the old number when not found
1209  */
1210     blocknr_T
1211 mf_trans_del(mfp, old_nr)
1212     memfile_T	*mfp;
1213     blocknr_T	old_nr;
1214 {
1215     int		hash;
1216     NR_TRANS	*np;
1217     blocknr_T	new_bnum;
1218 
1219     hash = MEMHASH(old_nr);
1220     for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1221 	if (np->nt_old_bnum == old_nr)
1222 	    break;
1223     if (np == NULL)		/* not found */
1224 	return old_nr;
1225 
1226     mfp->mf_neg_count--;
1227     new_bnum = np->nt_new_bnum;
1228     if (np->nt_prev != NULL)		/* remove entry from the trans list */
1229 	np->nt_prev->nt_next = np->nt_next;
1230     else
1231 	mfp->mf_trans[hash] = np->nt_next;
1232     if (np->nt_next != NULL)
1233 	np->nt_next->nt_prev = np->nt_prev;
1234     vim_free(np);
1235 
1236     return new_bnum;
1237 }
1238 
1239 /*
1240  * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1241  * Only called when creating or renaming the swapfile.	Either way it's a new
1242  * name so we must work out the full path name.
1243  */
1244     void
1245 mf_set_ffname(mfp)
1246     memfile_T	*mfp;
1247 {
1248     mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1249 }
1250 
1251 /*
1252  * Make the name of the file used for the memfile a full path.
1253  * Used before doing a :cd
1254  */
1255     void
1256 mf_fullname(mfp)
1257     memfile_T	*mfp;
1258 {
1259     if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1260     {
1261 	vim_free(mfp->mf_fname);
1262 	mfp->mf_fname = mfp->mf_ffname;
1263 	mfp->mf_ffname = NULL;
1264     }
1265 }
1266 
1267 /*
1268  * return TRUE if there are any translations pending for 'mfp'
1269  */
1270     int
1271 mf_need_trans(mfp)
1272     memfile_T	*mfp;
1273 {
1274     return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1275 }
1276 
1277 /*
1278  * Open a swap file for a memfile.
1279  * The "fname" must be in allocated memory, and is consumed (also when an
1280  * error occurs).
1281  */
1282     static void
1283 mf_do_open(mfp, fname, flags)
1284     memfile_T	*mfp;
1285     char_u	*fname;
1286     int		flags;		/* flags for open() */
1287 {
1288 #ifdef HAVE_LSTAT
1289     struct stat sb;
1290 #endif
1291 
1292     mfp->mf_fname = fname;
1293 
1294     /*
1295      * Get the full path name before the open, because this is
1296      * not possible after the open on the Amiga.
1297      * fname cannot be NameBuff, because it must have been allocated.
1298      */
1299     mf_set_ffname(mfp);
1300 #if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1301     /*
1302      * A ":!cd e:xxx" may change the directory without us knowning, use the
1303      * full pathname always.  Careful: This frees fname!
1304      */
1305     mf_fullname(mfp);
1306 #endif
1307 
1308 #ifdef HAVE_LSTAT
1309     /*
1310      * Extra security check: When creating a swap file it really shouldn't
1311      * exist yet.  If there is a symbolic link, this is most likely an attack.
1312      */
1313     if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1314     {
1315 	mfp->mf_fd = -1;
1316 	EMSG(_("E300: Swap file already exists (symlink attack?)"));
1317     }
1318     else
1319 #endif
1320     {
1321 	/*
1322 	 * try to open the file
1323 	 */
1324 	flags |= O_EXTRA | O_NOFOLLOW;
1325 #ifdef WIN32
1326 	/* Prevent handle inheritance that cause problems with Cscope
1327 	 * (swap file may not be deleted if cscope connection was open after
1328 	 * the file) */
1329 	flags |= O_NOINHERIT;
1330 #endif
1331 	mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1332     }
1333 
1334     /*
1335      * If the file cannot be opened, use memory only
1336      */
1337     if (mfp->mf_fd < 0)
1338     {
1339 	vim_free(mfp->mf_fname);
1340 	vim_free(mfp->mf_ffname);
1341 	mfp->mf_fname = NULL;
1342 	mfp->mf_ffname = NULL;
1343     }
1344     else
1345     {
1346 #ifdef HAVE_FD_CLOEXEC
1347 	int fdflags = fcntl(mfp->mf_fd, F_GETFD);
1348 	if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1349 	    fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
1350 #endif
1351 #ifdef HAVE_SELINUX
1352 	mch_copy_sec(fname, mfp->mf_fname);
1353 #endif
1354 	mch_hide(mfp->mf_fname);    /* try setting the 'hidden' flag */
1355     }
1356 }
1357