summaryrefslogtreecommitdiff
path: root/ext2fs/pager.c
blob: 9e8b5bb38fa2778922758337b323ba3a5bfbf1f2 (plain)
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* Pager for ext2fs

   Copyright (C) 1994, 1995 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <strings.h>
#include "ext2fs.h"

spin_lock_t pager_list_lock = SPIN_LOCK_INITIALIZER;
struct user_pager_info *file_pager_list;

spin_lock_t node_to_page_lock = SPIN_LOCK_INITIALIZER;

#ifdef DONT_CACHE_MEMORY_OBJECTS
#define MAY_CACHE 0
#else
#define MAY_CACHE 1
#endif

/* ---------------------------------------------------------------- */

/* Find the location on disk of page OFFSET in NODE.  Return the disk block
   in BLOCK (if unallocated, then return 0).  If *NODE_LOCK is set on return,
   then release that mutex after I/O on the data has completed.  Set LENGTH
   to be the amount of valid data on disk.  */
static error_t
find_block (struct node *node, vm_offset_t offset,
	    daddr_t *block, int *length, int create,
	    struct rwlock **node_lock)
{
  error_t err;
      
  rwlock_reader_lock (&node->dn->alloc_lock);
  *node_lock = &node->dn->alloc_lock;

  if (offset >= node->allocsize)
    {
      rwlock_reader_unlock (&node->dn->alloc_lock);
      return EIO;
    }
      
  if (offset + vm_page_size > node->allocsize)
    *length = node->allocsize - offset;
  else
    *length = vm_page_size;

  err = ext2_getblk (node, offset >> log2_block_size, create, block);
  if (err == EINVAL)
    /* Don't barf yet if the node is unallocated.  */
    {
      *block = 0;
      err = 0;
    }

  return err;
}

/* ---------------------------------------------------------------- */

static error_t
disk_pager_read_page (vm_offset_t page, vm_address_t *buf, int *writelock)
{
  int length = vm_page_size;

  if (page + vm_page_size > device_size)
    length = device_size - offset;

  err = dev_read_sync (block, (void *)buf, length);
  if (!err && length != vm_page_size)
    bzero ((void *)(*buf + length), vm_page_size - length);

  *writelock = 0;

  return err;
}

static error_t
disk_pager_write_page (vm_offset_t page, vm_address_t buf)
{
  int length = vm_page_size;
  if (page + vm_page_size > device_size)
    length = device_size - offset;
  return dev_write_sync (block, buf, length);
}

/* ---------------------------------------------------------------- */

/* Read one page for the pager backing NODE at offset PAGE, into BUF.  This
   may need to read several filesystem blocks to satisfy one page, and tries
   to consolidate the i/o if possible.  */
static error_t
file_pager_read_page (struct node *node, vm_offset_t page,
		      vm_address_t *buf, int *writelock)
{
  int offs = 0;
  struct rwlock *node_lock = NULL;
  int left = vm_page_size;
  daddr_t pending_blocks = 0;
  int num_pending_blocks = 0;

  /* Read the NUM_PENDING_BLOCKS blocks in PENDING_BLOCKS, into the buffer
     pointed to by BUF (allocating it if necessary) at offset OFFS.  OFFS in
     adjusted by the amount read, and NUM_PENDING_BLOCKS is zeroed.  Any read
     error is returned.  */
  error_t do_pending_reads ()
    {
      daddr_t dev_block = pending_block >> log2_dev_blocks_per_fs_block;
      int length = num_pending_blocks << log2_block_size;
      vm_address_t new_buf;

      err = dev_read_sync (dev_block, &new_buf, length);
      if (err)
	return err;

      if (offs == 0)
	/* First read, make the returned page be our buffer.  */
	*buf = new_buf;
      else
	/* We've already got some buffer, so copy into it.  */
	bcopy ((char *)*buf + offs, (char *)new_buf, length);

      offs += length;
      num_pending_blocks = 0;

      return 0;
    }

  while (left > 0)
    {
      u32 block, length;

      err = find_block (node, page, &block, &length, 0, &node_lock);
      if (err)
	break;

      if (block != pending_blocks + num_pending_blocks);
	{
	  err = do_dev_reads ();
	  if (err)
	    break;
	  pending_blocks = block;
	}

      if (block == 0)
	/* Reading unallocate block, just make a zero-filled one.  */
	{
	  if (offs == 0)
	    /* No page allocated to read into yet.  */
	    {
	      err = vm_allocate (mach_task_self (), &block, vm_page_size, 1);
	      if (err)
		break;
	      *writelock = 1;
	    }
	  bzero ((char *)*buf + offs, block_size);
	  offs += block_size;
	}
      else
	num_pending_blocks++;

      left -= length;
    }

  if (!err && num_pending_blocks > 0)
    do_pending_reads();
      
  if (node_lock)
    rwlock_reader_unlock (node_lock);

  return err;
}

/* ---------------------------------------------------------------- */

/* Write one page for the pager backing NODE, at offset PAGE, into BUF.  This
   may need to write several filesystem blocks to satisfy one page, and tries
   to consolidate the i/o if possible.  */
static error_t
file_pager_write_page (struct node *node, vm_offset_t page, vm_address_t buf)
{
  int offs = 0;
  struct rwlock *node_lock = NULL;
  int left = vm_page_size;
  daddr_t pending_blocks = 0;
  int num_pending_blocks = 0;

  /* Write the NUM_PENDING_BLOCKS blocks in PENDING_BLOCKS, from BUF at
     offset OFFS.  OFFS in adjusted by the amount write, and
     NUM_PENDING_BLOCKS is zeroed.  */
  error_t do_pending_writes ()
    {
      daddr_t dev_block = pending_block >> log2_dev_blocks_per_fs_block;
      int length = num_pending_blocks << log2_block_size;

      if (offs > 0)
	/* Put what we're going to write into a page-aligned buffer.  */
	{
	  bcopy (buf + offs, page_buf, length);
	  err = dev_write_sync (dev_block, page_buf, length);
	}
      else
	err = dev_write_sync (dev_block, buf, length);
      if (err)
	return err;

      offs += length;
      num_pending_blocks = 0;

      return 0;
    }

  while (left > 0)
    {
      u32 block, length;

      err = find_block (node, page, &block, &length, 1, &node_lock);
      if (err)
	break;

      if (block != pending_blocks + num_pending_blocks);
	{
	  err = do_dev_writes ();
	  if (err)
	    break;
	  pending_blocks = block;
	}

      num_pending_blocks++;
      left -= length;
    }

  if (!err && num_pending_blocks > 0)
    do_pending_writes();
      
  if (node_lock)
    rwlock_reader_unlock (node_lock);

  return err;
}

/* ---------------------------------------------------------------- */

/* Satisfy a pager read request for either the disk pager or file pager
   PAGER, to the page at offset PAGE into BUF.  WRITELOCK should be set if
   the pager should make the page writeable.  */
static error_t
pager_read_page (struct user_pager_info *pager, vm_offset_t page,
		      vm_address_t *buf, int *writelock)
{
  if (pager->type == DISK)
    return disk_pager_read_page (page, buf, writelock);
  else
    return file_pager_read_page (pager->node, page, buf, writelock);
}

/* Satisfy a pager write request for either the disk pager or file pager
   PAGER, from the page at offset PAGE from BUF.  */
static error_t
pager_write_page (struct user_pager_info *pager, vm_offset_t page,
		  vm_address_t buf)
{
  if (pager->type == DISK)
    return disk_pager_write_page (page, buf);
  else
    return file_pager_write_page (pager->node, page, buf);
}

/* ---------------------------------------------------------------- */

/* Implement the pager_unlock_page callback from the pager library.  See 
   <hurd/pager.h> for the interface description. */
error_t
pager_unlock_page (struct user_pager_info *pager,
		   vm_offset_t address)
{
  if (pager->type == DISK)
    return 0;
  else
    {
      error_t err;
      char *buf;
      struct node *node = pager->node;
      struct disknode *dn = node->dn;

      rwlock_writer_lock (&dn->alloc_lock);

      err = diskfs_catch_exception ();
      if (!err)
	err = ext2_getblk(node, address >> log2_block_size, 1, &buf);
      diskfs_end_catch_exception ();

      rwlock_writer_unlock (&dn->alloc_lock);

      return err;
    }
}

/* ---------------------------------------------------------------- */

/* Implement the pager_report_extent callback from the pager library.  See 
   <hurd/pager.h> for the interface description. */
inline error_t
pager_report_extent (struct user_pager_info *pager,
		     vm_address_t *offset,
		     vm_size_t *size)
{
  assert (pager->type == DISK || pager->type == FILE_DATA);

  *offset = 0;

  if (pager->type == DISK)
    *size = device_size;
  else
    *size = pager->node->allocsize;
  
  return 0;
}

/* Implement the pager_clear_user_data callback from the pager library.
   See <hurd/pager.h> for the interface description. */
void
pager_clear_user_data (struct user_pager_info *upi)
{
  assert (upi->type == FILE_DATA);
  spin_lock (&node_to_page_lock);
  upi->node->dn->fileinfo = 0;
  spin_unlock (&node_to_page_lock);
  diskfs_nrele_light (upi->node);
  *upi->prevp = upi->next;
  if (upi->next)
    upi->next->prevp = upi->prevp;
  free (upi);
}

/* ---------------------------------------------------------------- */

/* Create a the DISK pager, initializing DISKPAGER, and DISKPAGERPORT */
void
create_disk_pager ()
{
  disk_pager = malloc (sizeof (struct user_pager_info));
  disk_pager->type = DISK;
  disk_pager->node = 0;
  disk_pager->p = pager_create (disk_pager, MAY_CACHE, MEMORY_OBJECT_COPY_NONE);
  disk_pager_port = pager_get_port (disk_pager->p);
  mach_port_insert_right (mach_task_self (), disk_pager_port, disk_pager_port,
			  MACH_MSG_TYPE_MAKE_SEND);
}  

/* Call this to create a FILE_DATA pager and return a send right.
   NODE must be locked.  */
mach_port_t
diskfs_get_filemap (struct node *node)
{
  struct user_pager_info *upi;
  mach_port_t right;

  assert (S_ISDIR (node->dn_stat.st_mode)
	  || S_ISREG (node->dn_stat.st_mode)
	  || (S_ISLNK (node->dn_stat.st_mode)));

  spin_lock (&node_to_page_lock);
  if (!node->dn->fileinfo)
    {
      upi = malloc (sizeof (struct user_pager_info));
      upi->type = FILE_DATA;
      upi->node = node;
      diskfs_nref_light (node);
      upi->p = pager_create (upi, MAY_CACHE, MEMORY_OBJECT_COPY_DELAY);
      node->dn->fileinfo = upi;

      spin_lock (&pager_list_lock);
      upi->next = file_pager_list;
      upi->prevp = &file_pager_list;
      if (upi->next)
	upi->next->prevp = &upi->next;
      file_pager_list = upi;
      spin_unlock (&pager_list_lock);
    }
  right = pager_get_port (node->dn->fileinfo->p);
  spin_unlock (&node_to_page_lock);
  
  mach_port_insert_right (mach_task_self (), right, right,
			  MACH_MSG_TYPE_MAKE_SEND);

  return right;
} 

/* This syncs a single file (NODE) to disk.  Wait for all I/O to complete
   if WAIT is set.  NODE->lock must be held.  */
void
diskfs_file_update (struct node *node, int wait)
{
  struct user_pager_info *upi;

  spin_lock (&node_to_page_lock);
  upi = node->dn->fileinfo;
  if (upi)
    pager_reference (upi->p);
  spin_unlock (&node_to_page_lock);
  
  if (upi)
    {
      pager_sync (upi->p, wait);
      pager_unreference (upi->p);
    }
  
  pokel_sync (&node->dn->pokel, wait);

  diskfs_node_update (node, wait);
}

/* Call this when we should turn off caching so that unused memory object
   ports get freed.  */
void
drop_pager_softrefs (struct node *node)
{
  struct user_pager_info *upi;
  
  spin_lock (&node_to_page_lock);
  upi = node->dn->fileinfo;
  if (upi)
    pager_reference (upi->p);
  spin_unlock (&node_to_page_lock);

  if (MAY_CACHE && upi)
    pager_change_attributes (upi->p, 0, MEMORY_OBJECT_COPY_DELAY, 0);
  if (upi)
    pager_unreference (upi->p);
}

/* Call this when we should turn on caching because it's no longer
   important for unused memory object ports to get freed.  */
void
allow_pager_softrefs (struct node *node)
{
  struct user_pager_info *upi;
  
  spin_lock (&node_to_page_lock);
  upi = node->dn->fileinfo;
  if (upi)
    pager_reference (upi->p);
  spin_unlock (&node_to_page_lock);
  
  if (MAY_CACHE && upi)
    pager_change_attributes (upi->p, 1, MEMORY_OBJECT_COPY_DELAY, 0);
  if (upi)
    pager_unreference (upi->p);
}

/* Call this to find out the struct pager * corresponding to the
   FILE_DATA pager of inode IP.  This should be used *only* as a subsequent
   argument to register_memory_fault_area, and will be deleted when 
   the kernel interface is fixed.  NODE must be locked.  */
struct pager *
diskfs_get_filemap_pager_struct (struct node *node)
{
  /* This is safe because fileinfo can't be cleared; there must be
     an active mapping for this to be called. */
  return node->dn->fileinfo->p;
}

/* Call function FUNC (which takes one argument, a pager) on each pager, with
   all file pagers being processed before the disk pager.  Make the calls
   while holding no locks. */
static void
pager_traverse (void (*func)(struct user_pager_info *))
{
  struct user_pager_info *p;
  struct item {struct item *next; struct user_pager_info *p;} *list = 0;
  struct item *i;
  
  spin_lock (&pager_list_lock);
  for (p = file_pager_list; p; p = p->next)
    {
      i = alloca (sizeof (struct item));
      i->next = list;
      list = i;
      pager_reference (p->p);
      i->p = p;
    }
  spin_unlock (&pager_list_lock);
  
  for (i = list; i; i = i->next)
    {
      (*func)(i->p);
      pager_unreference (i->p->p);
    }
  
  (*func)(disk_pager);
}

/* Shutdown all the pagers. */
void
diskfs_shutdown_pager ()
{
  void shutdown_one (struct user_pager_info *p)
    {
      pager_shutdown (p->p);
    }

  copy_sblock ();
  write_all_disknodes ();
  pager_traverse (shutdown_one);
}

/* Sync all the pagers. */
void
diskfs_sync_everything (int wait)
{
  void sync_one (struct user_pager_info *p)
    {
      if (p != disk_pager)
	pager_sync (p->p, wait);
      else
	pokel_sync (&sblock_pokel, wait);
    }
  
  copy_sblock ();
  write_all_disknodes ();
  pager_traverse (sync_one);
}