summaryrefslogtreecommitdiff
path: root/mach-defpager/setup.c
blob: 495a815abdff74b451383e17eab4439a63b48a6d (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
/* Backing store access callbacks for Hurd version of Mach default pager.
   Copyright (C) 2001,02 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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 <errno.h>
#include <stddef.h>
#include <assert.h>
#include <mach.h>

#include "file_io.h"
#include "default_pager_S.h"

/* This should be in some system header...  XXX  */
static inline int page_aligned (vm_offset_t num)
{
  return trunc_page (num) == num;
}

/* From serverboot/kalloc.c.  */
extern void *kalloc (vm_size_t);

extern mach_port_t default_pager_default_port; /* default_pager.c */

kern_return_t
default_pager_paging_storage (mach_port_t pager,
			      mach_port_t device,
			      recnum_t *runs, mach_msg_type_number_t nrun,
			      default_pager_filename_t name,
			      boolean_t add)
{
  struct file_direct *fdp;
  int sizes[DEV_GET_SIZE_COUNT];
  natural_t count;
  mach_msg_type_number_t i;
  error_t err;
  recnum_t devsize;

  if (pager != default_pager_default_port)
    return KERN_INVALID_ARGUMENT;

  if (! add)
    return remove_paging_file (name); /* XXX ? */

  if (nrun < 2 || nrun % 2 != 0)
    return EINVAL;

  count = DEV_GET_SIZE_COUNT;
  err = device_get_status (device, DEV_GET_SIZE, sizes, &count);
  if (err)
    return err;
  if (count < DEV_GET_SIZE_COUNT || sizes[DEV_GET_SIZE_RECORD_SIZE] <= 0)
    return EINVAL;
  devsize = sizes[DEV_GET_SIZE_DEVICE_SIZE] / sizes[DEV_GET_SIZE_RECORD_SIZE];

  if (vm_page_size % sizes[DEV_GET_SIZE_RECORD_SIZE] != 0)
    /* We can't write disk blocks larger than pages.  */
    return EINVAL;

  fdp = kalloc (offsetof (struct file_direct, runs[nrun]));
  if (fdp == 0)
    return ENOMEM;

  fdp->device = device;
  fdp->bshift = ffs (sizes[DEV_GET_SIZE_RECORD_SIZE]) - 1;
  fdp->fd_bsize = sizes[DEV_GET_SIZE_RECORD_SIZE];
  fdp->nruns = nrun / 2;
  fdp->fd_size = 0;
  for (i = 0; i < nrun; i += 2)
    {
      fdp->runs[i].start = runs[i];
      fdp->runs[i].length = runs[i + 1];
      if (fdp->runs[i].start + fdp->runs[i].length > devsize)
	{
	  kfree (fdp);
	  return EINVAL;
	}
      fdp->fd_size += fdp->runs[i].length;
    }

  /* Now really do it.  */
  create_paging_partition (name, fdp, 0, -3);
  return 0;
}


/* Called to read a page from backing store.  */
int
page_read_file_direct (struct file_direct *fdp,
		       vm_offset_t offset,
		       vm_size_t size,
		       vm_offset_t *addr,			/* out */
		       mach_msg_type_number_t *size_read)	/* out */
{
  struct storage_run *r;
  error_t err;
  char *readloc;
  char *page;
  mach_msg_type_number_t nread;

  assert (page_aligned (offset));
  assert (size == vm_page_size);

  offset >>= fdp->bshift;

  assert (offset + (size >> fdp->bshift) <= fdp->fd_size);

  /* Find the run containing the beginning of the page.  */
  for (r = fdp->runs; offset > r->length; ++r)
    offset -= r->length;

  if (offset + (size >> fdp->bshift) <= r->length)
    /* The first run contains the whole page.  */
    return device_read (fdp->device, 0, r->start + offset,
			size, (char **) addr, size_read);

  /* Read the first part of the run.  */
  err = device_read (fdp->device, 0, r->start + offset,
		     (r->length - offset) << fdp->bshift,
		     (char **) addr, &nread);
  if (err)
    return err;

  size -= nread;
  readloc = (char *) *addr;
  do
    {
      readloc += nread;
      offset += nread >> fdp->bshift;
      if (offset > r->length)
	offset -= r++->length;

      /* We always get another out-of-line page, so we have to copy
	 out of that page and deallocate it.  */
      err = device_read (fdp->device, 0, r->start + offset,
			 (r->length - offset) << fdp->bshift,
			 &page, &nread);
      if (err)
	{
	  vm_deallocate (mach_task_self (),
			 (vm_address_t) *addr, vm_page_size);
	  return err;
	}
      memcpy (readloc, page, nread);
      vm_deallocate (mach_task_self (), (vm_address_t) page, vm_page_size);
      size -= nread;
    } while (size > 0);

  *size_read = vm_page_size;
  return 0;
}

/* Called to write a page to backing store.  */
int
page_write_file_direct(struct file_direct *fdp,
		       vm_offset_t offset,
		       vm_offset_t addr,
		       vm_size_t size,
		       vm_offset_t *size_written) /* out */
{
  struct storage_run *r;
  error_t err;
  int wrote;

  assert (page_aligned (offset));
  assert (size == vm_page_size);

  offset >>= fdp->bshift;

  assert (offset + (size >> fdp->bshift) <= fdp->fd_size);

  /* Find the run containing the beginning of the page.  */
  for (r = fdp->runs; offset > r->length; ++r)
    offset -= r->length;

  if (offset + (size >> fdp->bshift) <= r->length)
    /* The first run contains the whole page.  */
    return device_write (fdp->device, 0, r->start + offset,
			 (char *) addr, size, size_written);

  /* Write the first part of the run.  */
  err = device_write (fdp->device, 0,
		      r->start + offset, (char *) addr,
		      (r->length - offset) << fdp->bshift,
		      &wrote);
  if (err)
    return err;

  size -= wrote;
  do
    {
      mach_msg_type_number_t segsize;

      addr += wrote;
      offset += wrote >> fdp->bshift;
      if (offset > r->length)
	offset -= r++->length;

      segsize = (r->length - offset) << fdp->bshift;
      if (segsize > size)
	segsize = size;
      err = device_write (fdp->device, 0, r->start + offset,
			  (char *) addr, segsize, &wrote);
      if (err)
	{
	  vm_deallocate (mach_task_self (),
			 (vm_address_t) addr, vm_page_size);
	  return err;
	}

      size -= wrote;
    } while (size > 0);

  *size_written = vm_page_size;
  return 0;
}


/* Compatibility entry points used by default_pager_paging_file RPC.  */

kern_return_t
add_paging_file(master_device_port, file_name, linux_signature)
	mach_port_t		master_device_port;
	char			*file_name;
	int			linux_signature;
{
  error_t err;
  mach_port_t dev;
  int sizes[DEV_GET_SIZE_COUNT];
  natural_t count;
  char *devname = file_name;

  assert (linux_signature == 0);

  if (!strncmp (file_name, "/dev/", 5))
    devname += 5;

  err = device_open (master_device_port, D_READ|D_WRITE, devname, &dev);
  if (err)
    return err;

  count = DEV_GET_SIZE_COUNT;
  err = device_get_status (dev, DEV_GET_SIZE, sizes, &count);
  if (!err && count < DEV_GET_SIZE_COUNT)
    err = EGRATUITOUS;
  if (err)
    mach_port_deallocate (mach_task_self (), dev);
  else
    {
      struct file_direct *fdp;
      fdp = kalloc (offsetof (struct file_direct, runs[1]));
      if (fdp == 0)
	return ENOMEM;

      fdp->device = dev;
      fdp->fd_bsize = sizes[DEV_GET_SIZE_RECORD_SIZE];
      fdp->bshift = ffs (sizes[DEV_GET_SIZE_RECORD_SIZE]) - 1;
      fdp->fd_size = sizes[DEV_GET_SIZE_DEVICE_SIZE] >> fdp->bshift;
      fdp->nruns = 1;
      fdp->runs[0].start = 0;
      fdp->runs[0].length = fdp->fd_size;

      /* Now really do it.  */
      create_paging_partition (file_name, fdp, 0, 0);
    }

  return err;
}

/*
 * Destroy a paging_partition given a file name
 */
kern_return_t
remove_paging_file (char *file_name)
{
  struct file_direct *fdp = 0;
  kern_return_t kr;

  kr = destroy_paging_partition(file_name, &fdp);
  if (kr == KERN_SUCCESS && fdp != 0)
    {
      mach_port_deallocate (mach_task_self (), fdp->device);
      kfree (fdp, (char *) &fdp->runs[fdp->nruns] - (char *) fdp);
    }
  return kr;
}