summaryrefslogtreecommitdiff
path: root/serverboot/file_io.c
blob: 99966f9593420d4ee1039d53b4486beaabef86df (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
/*
 * Copyright (c) 1994 The University of Utah and
 * the Computer Systems Laboratory at the University of Utah (CSL).
 * All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software is hereby
 * granted provided that (1) source code retains these copyright, permission,
 * and disclaimer notices, and (2) redistributions including binaries
 * reproduce the notices in supporting documentation, and (3) all advertising
 * materials mentioning features or use of this software display the following
 * acknowledgement: ``This product includes software developed by the
 * Computer Systems Laboratory at the University of Utah.''
 *
 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
 * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
 * improvements that they make and grant CSL redistribution rights.
 *
 *      Author: Bryan Ford, University of Utah CSL
 *      MINIX FS patches: Csizmazia Balazs, University ELTE, Hungary
 */
/* This is just an icky kludgy "VFS layer" (harhar) for ffs and ext2 and minix.  */

#include "file_io.h"

int
open_file(master_device_port, path, fp)
	mach_port_t	master_device_port;
	char *		path;
	struct file	*fp;
{
	int rc;

	if ((rc = ext2_open_file(master_device_port, path, fp))
	    != FS_INVALID_FS)
	{
		if (rc == 0)
			fp->f_fstype = EXT2_FS;
		return rc;
	}
	if ( (rc = minix_open_file(master_device_port, path, fp))
	    != FS_INVALID_FS )
	{
		if (rc == 0)
			fp->f_fstype = MINIX_FS;
		return rc;
	}
	fp->f_fstype = BSD_FFS;
	return ffs_open_file(master_device_port, path, fp);
}

void
close_file(fp)
	register struct file	*fp;
{
	switch (fp->f_fstype) {
		case	EXT2_FS:
				ext2_close_file(fp);
				return;
		case	MINIX_FS:
				minix_close_file(fp);
				return;
		default:
				ffs_close_file(fp);
				return;
	}
}

int
read_file(fp, offset, start, size, resid)
	register struct file	*fp;
	vm_offset_t		offset;
	vm_offset_t		start;
	vm_size_t		size;
	vm_size_t		*resid;	/* out */
{
	switch (fp->f_fstype) {
		case	EXT2_FS:
				return ext2_read_file(fp, offset, start, size, resid);
		case	MINIX_FS:
				return minix_read_file(fp, offset, start, size, resid);
		default:
				return ffs_read_file(fp, offset, start, size, resid);
	}

}

int
file_is_directory(struct file *f)
{
	switch (f->f_fstype) {
		case	EXT2_FS:
				return ext2_file_is_directory(f);
		case	MINIX_FS:
				return minix_file_is_directory(f);
		default:
				return ffs_file_is_directory(f);
	}
}

int
file_is_regular(struct file *f)
{
	switch (f->f_fstype) {
		case	EXT2_FS:
				return ext2_file_is_regular(f);
		case	MINIX_FS:
				return minix_file_is_regular(f);
		default:
				return ffs_file_is_regular(f);
	}

}

int
open_file_direct(dev, fdp, is_structured)
	mach_port_t	dev;
	register struct file_direct *fdp;
	boolean_t	is_structured;
{
	int rc;


	if ((rc = ext2_open_file_direct(dev, fdp, is_structured))
	    != FS_INVALID_FS)
	{
		if (rc == 0)
			fdp->f_fstype = EXT2_FS;
		return rc;
	}
	if ( (rc = minix_open_file_direct(dev, fdp, is_structured) )
	    != FS_INVALID_FS )
	{
		if (rc == 0)
			fdp->f_fstype = MINIX_FS;
		return rc;
	}
	fdp->f_fstype = BSD_FFS;
	return ffs_open_file_direct(dev, fdp, is_structured);
}

int
add_file_direct(fdp, fp)
	register struct file_direct *fdp;
	register struct file *fp;
{
	switch (fp->f_fstype) {
		case	EXT2_FS:
				return ext2_add_file_direct(fdp, fp);
		case	MINIX_FS:
				return minix_add_file_direct(fdp, fp);
		default:
				return ffs_add_file_direct(fdp, fp);
	}
}


int
remove_file_direct(fdp)
	struct file_direct	*fdp;
{
	switch (fdp->f_fstype) {
		case	EXT2_FS:
			return ext2_remove_file_direct(fdp);
		case	MINIX_FS:
			return minix_remove_file_direct(fdp);
		default:
			return ffs_remove_file_direct(fdp);
	}
}

/*
 * some other stuff, that was previously defined as macro
 */

int
file_is_structured(fp)
	register struct	file *fp;
{
	switch (fp->f_fstype) {
		case	EXT2_FS:
			return (fp)->u.ext2.ext2_fs != 0;
		case	MINIX_FS:
			return (fp)->u.minix.minix_fs != 0;
		default:
			return (fp)->u.ffs.ffs_fs != 0;
	}
}

/*
 * Special read and write routines for default pager.
 * Assume that all offsets and sizes are multiples
 * of DEV_BSIZE.
 */

#define	fdir_blkoff(fdp, offset)	/* offset % fd_bsize */ \
	((offset) & ((fdp)->fd_bsize - 1))
#define	fdir_lblkno(fdp, offset)	/* offset / fd_bsize */ \
	((offset) >> (fdp)->fd_bshift)

#define	fdir_fsbtodb(fdp, block)	/* offset * fd_bsize / DEV_BSIZE */ \
	((block) << (fdp)->fd_fsbtodb)

/*
 * Read all or part of a data block, and
 * return a pointer to the appropriate part.
 * Caller must deallocate the block when done.
 */
int
page_read_file_direct(fdp, offset, size, addr, size_read)
	register 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 */
{
	vm_offset_t		off;
	register daddr_t	file_block;
	daddr_t			disk_block;

	if (offset % DEV_BSIZE != 0 ||
	    size % DEV_BSIZE != 0)
	    panic("page_read_file_direct");

	if (offset >= (fdp->fd_size << fdp->fd_bshift))
	    return (FS_NOT_IN_FILE);

	off = fdir_blkoff(fdp, offset);
	file_block = fdir_lblkno(fdp, offset);

	if (file_is_device(fdp)) {
	    disk_block = file_block;
	} else {
	    disk_block = fdp->fd_blocks[file_block];
	    if (disk_block == 0)
		return (FS_NOT_IN_FILE);

	    if (size > fdp->fd_bsize) {
	        /* Read only as much as is contiguous on disk.  */
		daddr_t b = file_block + 1;
		while (b < file_block + fdp->fd_size &&
		       fdp->fd_blocks[b] == disk_block + fdir_fsbtodb(fdp, 1))
		  ++b;
	        size = (b - file_block) * fdp->fd_bsize;
	    }
	}

	return (device_read(fdp->fd_dev,
			0,
			(recnum_t) (fdir_fsbtodb(fdp, disk_block) + btodb(off)),
			(int) size,
			(char **) addr,
			size_read));
}

/*
 * Write all or part of a data block, and
 * return the amount written.
 */
int
page_write_file_direct(fdp, offset, addr, size, size_written)
	register struct file_direct *fdp;
	vm_offset_t offset;
	vm_offset_t addr;
	vm_size_t size;
	vm_offset_t *size_written;	/* out */
{
	vm_offset_t		off;
	register daddr_t	file_block;
	daddr_t			disk_block;
	int			rc, num_written;
	vm_offset_t		block_size;

	if (offset % DEV_BSIZE != 0 ||
	    size % DEV_BSIZE != 0)
	    panic("page_write_file");

	if (offset >= (fdp->fd_size << fdp->fd_bshift))
	    return (FS_NOT_IN_FILE);

	off = fdir_blkoff(fdp, offset);
	file_block = fdir_lblkno(fdp, offset);

	if (file_is_device(fdp)) {
	    disk_block = file_block;
	} else {
	    disk_block = fdp->fd_blocks[file_block];
	    if (disk_block == 0)
		return (FS_NOT_IN_FILE);

	    if (size > fdp->fd_bsize) {
	        /* Write only as much as is contiguous on disk.  */
		daddr_t b = file_block + 1;
		while (b < file_block + fdp->fd_size &&
		       fdp->fd_blocks[b] == disk_block + fdir_fsbtodb(fdp, 1))
		  ++b;
	        size = (b - file_block) * fdp->fd_bsize;
	    }
	}

	/*
	 * Write the data.  Wait for completion to keep
	 * reads from getting ahead of writes and reading
	 * stale data.
	 */
	rc = device_write(
			fdp->fd_dev,
			0,
			(recnum_t) (fdir_fsbtodb(fdp, disk_block) + btodb(off)),
			(char *) addr,
			size,
			&num_written);
	*size_written = num_written;
	return rc;
}