summaryrefslogtreecommitdiff
path: root/libstore/file.c
blob: ce02f1f4234d46d648ba73d38234310164571736 (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
/* File store backend

   Copyright (C) 1995,96,97,98,2001, 2002 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.org>
   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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <hurd.h>

#include <hurd/io.h>

#include "store.h"

/* Return 0 if STORE's range is enforced by the filesystem, otherwise an
   error.  */
static error_t
enforced (struct store *store)
{
  if (store->num_runs != 1 || store->runs[0].start != 0)
    /* Can't enforce non-contiguous ranges, or one not starting at 0.  */
    return EINVAL;
  else
    {
      /* See if the the current (one) range is that the kernel is enforcing. */
      struct stat st;
      error_t err = io_stat (store->port, &st);

      if (!err
	  && store->runs[0].length != (st.st_size >> store->log2_block_size))
	/* The single run is not the whole file.  */
	err = EINVAL;

      return err;
    }
}

static error_t
file_read (struct store *store,
	   store_offset_t addr, size_t index, size_t amount, void **buf,
	   size_t *len)
{
  size_t bsize = store->block_size;
  return io_read (store->port, (char **)buf, len, addr * bsize, amount);
}

static error_t
file_write (struct store *store,
	    store_offset_t addr, size_t index, const void *buf, size_t len,
	    size_t *amount)
{
  size_t bsize = store->block_size;
  return io_write (store->port, (void *) buf, len, addr * bsize, amount);
}

static error_t
file_store_set_size (struct store *store, size_t newsize)
{
  error_t err;

  if (enforced (store) != 0)
    /* Bail out if there is more than a single run.  */
    return EOPNOTSUPP;

  err = file_set_size (store->port, newsize);

  if (!err)
  {
    /* Update STORE's size and run.  */
    store->size = newsize;
    store->runs[0].length = newsize >> store->log2_block_size;
  }

  return err;
}

static error_t
file_decode (struct store_enc *enc, const struct store_class *const *classes,
	     struct store **store)
{
  return store_std_leaf_decode (enc, _store_file_create, store);
}

static error_t
file_open (const char *name, int flags,
	   const struct store_class *const *classes,
	   struct store **store)
{
  return store_file_open (name, flags, store);
}

static error_t
fiopen (const char *name, file_t *file, int *mod_flags)
{
  if (*mod_flags & STORE_HARD_READONLY)
    *file = file_name_lookup (name, O_RDONLY, 0);
  else
    {
      *file = file_name_lookup (name, O_RDWR, 0);
      if (*file == MACH_PORT_NULL
	  && (errno == EACCES || errno == EROFS))
	{
	  *file = file_name_lookup (name, O_RDONLY, 0);
	  if (*file != MACH_PORT_NULL)
	    *mod_flags |= STORE_HARD_READONLY;
	}
      else if (*file != MACH_PORT_NULL)
	*mod_flags &= ~STORE_HARD_READONLY;
    }
  return *file == MACH_PORT_NULL ? errno : 0;
}

static void
ficlose (struct store *store)
{
  mach_port_deallocate (mach_task_self (), store->port);
  store->port = MACH_PORT_NULL;
}



static error_t
file_set_flags (struct store *store, int flags)
{
  if ((flags & ~(STORE_INACTIVE | STORE_ENFORCED)) != 0)
    /* Trying to set flags we don't support.  */
    return EINVAL;

  if (! ((store->flags | flags) & STORE_INACTIVE))
    /* Currently active and staying that way, so we must be trying to set the
       STORE_ENFORCED flag.  */
    {
      error_t err = enforced (store);
      if (err)
	return err;
    }

  if (flags & STORE_INACTIVE)
    ficlose (store);

  store->flags |= flags;	/* When inactive, anything goes.  */

  return 0;
}

static error_t
file_clear_flags (struct store *store, int flags)
{
  error_t err = 0;
  if ((flags & ~(STORE_INACTIVE | STORE_ENFORCED)) != 0)
    err = EINVAL;
  if (!err && (flags & STORE_INACTIVE))
    err = store->name
      ? fiopen (store->name, &store->port, &store->flags)
      : ENOENT;
  if (! err)
    store->flags &= ~flags;
  return err;
}

static error_t
file_map (const struct store *store, vm_prot_t prot, mach_port_t *memobj)
{
  error_t err;
  mach_port_t rd_memobj, wr_memobj;
  int ro = (store->flags & STORE_HARD_READONLY);

  if (store->num_runs != 1 || store->runs[0].start != 0)
    return EOPNOTSUPP;

  if ((prot & VM_PROT_WRITE) && ro)
    return EACCES;

  err = io_map (store->port, &rd_memobj, &wr_memobj);
  if (err)
    return err;

  *memobj = rd_memobj;

  if (ro && wr_memobj == MACH_PORT_NULL)
    return 0;
  else if (rd_memobj == wr_memobj)
    {
      if (rd_memobj != MACH_PORT_NULL)
	mach_port_mod_refs (mach_task_self (), rd_memobj,
			    MACH_PORT_RIGHT_SEND, -1);
    }
  else
    {
      if (rd_memobj != MACH_PORT_NULL)
	mach_port_deallocate (mach_task_self (), rd_memobj);
      if (wr_memobj != MACH_PORT_NULL)
	mach_port_deallocate (mach_task_self (), wr_memobj);
      err = EOPNOTSUPP;
    }

  return err;
}

const struct store_class
store_file_class =
{
  STORAGE_HURD_FILE, "file", file_read, file_write, file_store_set_size,
  store_std_leaf_allocate_encoding, store_std_leaf_encode, file_decode,
  file_set_flags, file_clear_flags, 0, 0, 0, file_open, 0, file_map
};
STORE_STD_CLASS (file);

static error_t
file_byte_read (struct store *store,
		store_offset_t addr, size_t index, size_t amount,
		void **buf, size_t *len)
{
  return io_read (store->port, (char **)buf, len, addr, amount);
}

static error_t
file_byte_write (struct store *store,
		 store_offset_t addr, size_t index,
		 const void *buf, size_t len,
		 size_t *amount)
{
  return io_write (store->port, (void *) buf, len, addr, amount);
}

struct store_class
store_file_byte_class =
{
  STORAGE_HURD_FILE, "file", file_byte_read, file_byte_write,
  file_store_set_size,
  store_std_leaf_allocate_encoding, store_std_leaf_encode, file_decode,
  file_set_flags, file_clear_flags, 0, 0, 0, file_open, 0, file_map
};

/* Return a new store in STORE referring to the mach file FILE.  Consumes
   the send right FILE.  */
error_t
store_file_create (file_t file, int flags, struct store **store)
{
  struct store_run run;
  struct stat stat;
  error_t err = io_stat (file, &stat);

  if (err)
    return err;

  run.start = 0;
  run.length = stat.st_size;

  flags |= STORE_ENFORCED;	/* 'cause it's the whole file.  */

  return _store_file_create (file, flags, 1, &run, 1, store);
}

/* Like store_file_create, but doesn't query the file for information.  */
error_t
_store_file_create (file_t file, int flags, size_t block_size,
		    const struct store_run *runs, size_t num_runs,
		    struct store **store)
{
  if (block_size == 1)
    return _store_create (&store_file_byte_class,
			  file, flags, 1, runs, num_runs, 0, store);
  else
    return _store_create (&store_file_class,
			  file, flags, block_size, runs, num_runs, 0, store);
}

/* Open the file NAME, and return the corresponding store in STORE.  */
error_t
store_file_open (const char *name, int flags, struct store **store)
{
  file_t file;
  error_t err = fiopen (name, &file, &flags);
  if (! err)
    {
      err = store_file_create (file, flags, store);
      if (! err)
	{
	  err = store_set_name (*store, name);
	  if (err)
	    store_free (*store);
	}
      if (err)
	mach_port_deallocate (mach_task_self (), file);
    }
  return err;
}