File: write-cache.cc

package info (click to toggle)
plex86 0.0.20011018-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,868 kB
  • ctags: 8,721
  • sloc: ansic: 46,915; cpp: 17,817; xml: 1,283; makefile: 1,130; sh: 451; asm: 360; csh: 18
file content (401 lines) | stat: -rw-r--r-- 9,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 *  plex86: run multiple x86 operating systems concurrently
 *  Copyright (C) 2000  Kevin P. Lawton
 *
 *  write-cache.cc: A write-cache implementation.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */


/*
 * Initial C++ implementation, Tom Vijlbrief, 20001230
 *
 * TODO:
 * - add support for more disks 
 */

#define DEBUG 1

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

// The C++ STL map container makes the cache real easy to implement
#include <map>

#include "plex86.h"
#include "user.h"
#include "plugin.h"

#if defined(linux)
#define LARGEFILES 1
#else
#define LARGEFILES 0
#endif

extern "C" {
static Bit64u   wcLSeek(int fd, Bit64u off64, int whence);
static unsigned wcRead(int fd, void *buffer, unsigned len);
static unsigned wcWrite(int fd, void *buffer, unsigned len);
static void     wcCommit(int fd);
}


  static void
CachePanic(char *s)
{
  fprintf(stderr, "Write cache problem: %s\n", s);
  vm_abort();
  exit(1);
}


  extern "C"
  void
plugin_fini(void)
{
  return;
}


/* Fill in the functions below.  These provide the functions
 * analogous to the libc lseek(), read() and write() functions,
 * but should be implemented with the write cache in mind.
 * Currently, these pass the functionality through to libc,
 * for a starting point.
 *
 * The commit function is called by the associated GUI button.
 * The idea is that the user wants to commit all of the previous write
 * operations in the cache to disk.  So commit the writes and
 * flush the cache.
 *
 * Note that their are builtin default handlers which simply pass-through
 * these events, when this plugin is not loaded.  If you need to fix
 * things in the plugin interface, you might need to fix them too.
 * They are in 'user/plugin.c'.  The header file is 'user/plugin.h'.
 *
 * The calls in user/plugins/bochs/iodev/harddrv.cc where changed
 * to call these functions. 
 *
 * Look in conf/freedos for a commented line, which is how you might
 * call the plugin.
 */

struct sector {
  Bit8u data[512];
};


typedef map<Bit64u,sector> sectormap;

class WriteCache {
public:
  enum CommitMode { never, user, sync };

  // default cache mode is 'user'
  WriteCache(): fd(-1), fdpers(-1), cmode(user), mustseek(1) { };

  void SetFd(int fd);
  int GetFd() { return fd; };
  void SetFdPers(int fd);
  int GetFdPers() { return fdpers; };
  void SetMode(CommitMode c) { cmode= c; }
  CommitMode GetMode() { return cmode; };

  Bit64u Seek(Bit64u off64, int whence);
  unsigned Read(void* buffer, unsigned len);
  unsigned Write(void* buffer, unsigned len);
  void Commit();
private:
  void DoSeek();

  int		fd;
  int		fdpers;
  Bit64u	pos;
  sectormap	map;
  CommitMode	cmode;
  int		mustseek;
};

void WriteCache::SetFd(int fd) {
  if (this->fd != -1 && this->fd != fd) {
    fprintf(stderr, "new fd: %d old: %d\n", fd, this->fd);
    CachePanic("already in use");
  }
  this->fd= fd;
}

Bit64u WriteCache::Seek(Bit64u off64, int whence)
{
  if (whence != 0)
    CachePanic("whence != 0 not implemented");
  if (off64 == pos) {
#if DEBUG
    fprintf(stderr, "Seek not needed: %u\n", (unsigned)off64);
#endif
    return off64;
  }
#if DEBUG
  else
    fprintf(stderr, "Seek needed: %u\n", (unsigned)off64);
#endif
  mustseek= 1;
  pos= off64;
  return off64;
}

#if LARGEFILES
extern "C" long long llseek (int fd, long long offset, int origin);
#endif

void WriteCache::DoSeek()
{
  loff_t result;

#if 1
  if (!mustseek)
    return;
  mustseek= 0;
#endif

  if (pos & 0x1FF)
    CachePanic("unaligned seek");

#if LARGEFILES
#if 0
  fprintf(stderr, "Offset: %u:%u\n",
	(unsigned) (pos>>32), (unsigned) pos);
#endif
  result= llseek(fd, pos, 0);
#else
  if (pos > 0xFFFFFFFE)
    CachePanic("offset too large");
  result= lseek(fd, (unsigned long) pos, 0);
#endif
  if (result < 0) {
    fprintf(stderr, "Offset: %u:%u\n",
	(unsigned) (pos>>32), (unsigned) pos);
    CachePanic("seek failed");
  }
}


void WriteCache::SetFdPers(int fd)
{
  int nsec= 0;
  fdpers= fd;
#if DEBUG
  fprintf(stderr, "Loading persistant cache file...\n");
#endif
  while (1) {
    Bit64u offset;
    sector s;
    int nb;
    
    nb= ::read(fdpers, &offset, sizeof(offset));
    if (nb == 0)
	break;
    if (nb != sizeof(offset))
          CachePanic("read from persistant cache file failed");
    nb= ::read(fdpers, &s, sizeof(s));
    if (nb != sizeof(s))
          CachePanic("read from persistant cache file failed");
    map[offset]= s;
    nsec++;
  }
#if DEBUG
  fprintf(stderr, "%d sectors loaded\n", nsec);
#endif
}


void WriteCache::Commit()
{
  if (cmode == never) {
    // Nothing to do...
#if DEBUG
    fprintf(stderr, "commit mode never\n");
#endif
  } else if (cmode == user || cmode == sync) {
    if (fdpers != -1 && cmode == user) {
#if DEBUG
      fprintf(stderr,
	"flushing changed sectors to persistant cache file...\n");
#endif
      if (lseek(fdpers, 0, 0) == -1)
	CachePanic("seek failed");
    } else {
#if DEBUG
      fprintf(stderr, "flushing changed sectors to disk...\n");
#endif
    }
    sectormap::const_iterator i;
    int nsec= 0;
    for (i= map.begin(); i != map.end(); i++) {
      nsec++;
#if DEBUG
      fprintf(stderr, "Write cache: committing sector at %u:%u\n",
	(unsigned)(i->first>>32), (unsigned)i->first);
#endif
      if (fdpers == -1 || cmode == sync) {
        if (i->first != pos) { // must we seek ?
          mustseek= 1;
          pos= i->first;
        }
        DoSeek();
        if (::write(fd, i->second.data,
		sizeof(i->second.data)) != sizeof(i->second.data))
          CachePanic("write to disk image failed");
        pos+= 512; // update position
      } else {
        if (::write(fdpers, &(i->first), sizeof(i->first)) != sizeof(i->first))
          CachePanic("write to persistant cache file failed");
        if (::write(fdpers, i->second.data,
	    sizeof(i->second.data)) != sizeof(i->second.data))
          CachePanic("write to persistant cache file failed");
      }
    }
    if (fdpers == -1 || cmode == sync)
      map.clear();
    if (cmode == sync)
      ftruncate(fdpers, 0);
#if DEBUG
    fprintf(stderr, "%d sectors written\n", nsec);
#endif
  } else
    CachePanic("impossible cache mode");
}


unsigned WriteCache::Read(void* buffer, unsigned len)
{
  if (len != 512)
    CachePanic("len != 512");
  
#if DEBUG
  fprintf(stderr, "cache::read\n");
#endif
  sectormap::const_iterator i;
  if ((i= map.find(pos)) != map.end()) {
    memcpy(buffer, i->second.data, len); 
    pos+= len;
    mustseek= 1; // cause we did not update the real filepointer
    return len;
  } else {
    DoSeek();
    pos+= len;
    return( ::read(fd, buffer, len) );
  }
}

unsigned WriteCache::Write(void* buffer, unsigned len)
{
  if (len != 512)
    CachePanic("len != 512");

#if DEBUG
  fprintf(stderr, "cache::write\n");
#endif
  map[pos]= *(sector*)buffer;
#if DEBUG
  fprintf(stderr, "Write cache: offset %u:%u, size %d\n",
	(unsigned)(pos>>32), (unsigned)pos, map.size());
#endif
  mustseek= 1; // cause we did not update the real filepointer
  pos+= len;
  return len;
}


// Just one cache for now...
WriteCache thecache;


  Bit64u
wcLSeek(int fd, Bit64u off64, int whence)
{
  thecache.SetFd(fd);
  return thecache.Seek(off64, whence);
}

  unsigned
wcRead(int fd, void* buffer, unsigned len)
{
  return( thecache.Read(buffer, len) );
}

  unsigned
wcWrite(int fd, void* buffer, unsigned len)
{
  return( thecache.Write(buffer, len) );
}

  void
wcCommit(int fd)
{
  thecache.Commit();
}

  extern "C"
  int
plugin_init(plugin_t *plugin, int argc, char *argv[])
{

  /* You can pass options to this plugin, from the conf file,
   * if you require any.  Maybe something like sector size hints,
   * maximum cache size before a commit is forced (we should
   * interrupt the user in the future for that), etc.
   */

  pluginRegisterWriteCache(wcLSeek, wcRead, wcWrite, wcCommit);

  for (int i= 0; i < argc; i++) {
#if DEBUG
    fprintf(stderr, "Write cache: arg %d: %s\n", i, argv[i]);
#endif
    if (strncmp(argv[i], "mode=", 5) == 0) {
      if (strcmp(argv[i]+5, "user") == 0)
        thecache.SetMode(WriteCache::user);
      else if (strcmp(argv[i]+5, "never") == 0)
        thecache.SetMode(WriteCache::never);
      else if (strcmp(argv[i]+5, "sync") == 0)
        thecache.SetMode(WriteCache::sync);
      else {
	fprintf(stderr, "Unknown mode: %s\n", argv[i]+5);
	fprintf(stderr, "defaulting to 'never'\n");
        thecache.SetMode(WriteCache::never);
      }
    } else if (strncmp(argv[i], "persistfile=", 12) == 0) {
      char *fname= argv[i]+12;
      int fd;
      if ((fd= open(fname, O_RDWR|O_CREAT, 0666)) < 0) {
	perror(fname);
	CachePanic("cannot open persistant cache file");
      }
      thecache.SetFdPers(fd);
    }
  }
  
  if (thecache.GetMode() == WriteCache::sync && thecache.GetFdPers() == -1)
    CachePanic("You cannot use sync mode without a persistant cache file");

  return 0; /* OK */
}