blob: 53bedb9a1f6506bffa5d2cfa11f1f95ad585d368 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * unix_io.c --- This is the Unix I/O interface to the I/O manager.
3 *
4 * Implements a one-block write-through cache.
5 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000012 */
13
14#include <stdio.h>
15#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000018#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000019#include <stdlib.h>
20#include <fcntl.h>
21#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
25#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000027#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000028
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000029#include <linux/ext2_fs.h>
30
31#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000032
Theodore Ts'of3db3561997-04-26 13:34:30 +000033/*
34 * For checking structure magic numbers...
35 */
36
37#define EXT2_CHECK_MAGIC(struct, code) \
38 if ((struct)->magic != (code)) return (code)
39
Theodore Ts'o3839e651997-04-26 13:21:57 +000040struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000041 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000042 int dev;
43 int flags;
44 char *buf;
45 int buf_block_nr;
46};
47
48static errcode_t unix_open(const char *name, int flags, io_channel *channel);
49static errcode_t unix_close(io_channel channel);
50static errcode_t unix_set_blksize(io_channel channel, int blksize);
51static errcode_t unix_read_blk(io_channel channel, unsigned long block,
52 int count, void *data);
53static errcode_t unix_write_blk(io_channel channel, unsigned long block,
54 int count, const void *data);
55static errcode_t unix_flush(io_channel channel);
56
Theodore Ts'of3db3561997-04-26 13:34:30 +000057static struct struct_io_manager struct_unix_manager = {
58 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +000059 "Unix I/O Manager",
60 unix_open,
61 unix_close,
62 unix_set_blksize,
63 unix_read_blk,
64 unix_write_blk,
65 unix_flush
66};
67
68io_manager unix_io_manager = &struct_unix_manager;
69
70static errcode_t unix_open(const char *name, int flags, io_channel *channel)
71{
72 io_channel io = NULL;
73 struct unix_private_data *data = NULL;
74 errcode_t retval;
75
Theodore Ts'o50e1e101997-04-26 13:58:21 +000076 if (name == 0)
77 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000078 retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
79 (void **) &io);
80 if (retval)
81 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +000082 memset(io, 0, sizeof(struct struct_io_channel));
83 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000084 retval = ext2fs_get_mem(sizeof(struct unix_private_data),
85 (void **) &data);
86 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000088
Theodore Ts'o3839e651997-04-26 13:21:57 +000089 io->manager = unix_io_manager;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000090 retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
91 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000093
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 strcpy(io->name, name);
95 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +000096 io->block_size = 1024;
97 io->read_error = 0;
98 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +000099 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100
101 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000102 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000103 retval = ext2fs_get_mem(io->block_size, (void **) &data->buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104 data->buf_block_nr = -1;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000105 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000107
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108 data->dev = open(name, (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY);
109 if (data->dev < 0) {
110 retval = errno;
111 goto cleanup;
112 }
113 *channel = io;
114 return 0;
115
116cleanup:
117 if (io)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000118 ext2fs_free_mem((void **) &io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119 if (data) {
120 if (data->buf)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000121 ext2fs_free_mem((void **) &data->buf);
122 ext2fs_free_mem((void **) &data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 }
124 return retval;
125}
126
127static errcode_t unix_close(io_channel channel)
128{
129 struct unix_private_data *data;
130 errcode_t retval = 0;
131
Theodore Ts'of3db3561997-04-26 13:34:30 +0000132 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000134 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000135
136 if (--channel->refcount > 0)
137 return 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000138
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139 if (close(data->dev) < 0)
140 retval = errno;
141 if (data->buf)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000142 ext2fs_free_mem((void **) &data->buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 if (channel->private_data)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000144 ext2fs_free_mem((void **) &channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000145 if (channel->name)
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000146 ext2fs_free_mem((void **) &channel->name);
147 ext2fs_free_mem((void **) &channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 return retval;
149}
150
151static errcode_t unix_set_blksize(io_channel channel, int blksize)
152{
153 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000154 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155
Theodore Ts'of3db3561997-04-26 13:34:30 +0000156 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000158 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
159
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 if (channel->block_size != blksize) {
161 channel->block_size = blksize;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000162 ext2fs_free_mem((void **) &data->buf);
163 retval = ext2fs_get_mem(blksize, (void **) &data->buf);
164 if (retval)
165 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 data->buf_block_nr = -1;
167 }
168 return 0;
169}
170
171
172static errcode_t unix_read_blk(io_channel channel, unsigned long block,
173 int count, void *buf)
174{
175 struct unix_private_data *data;
176 errcode_t retval;
177 size_t size;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000178 ext2_loff_t location;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 int actual = 0;
180
Theodore Ts'of3db3561997-04-26 13:34:30 +0000181 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000183 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000184
185 /*
186 * If it's in the cache, use it!
187 */
188 if ((count == 1) && (block == data->buf_block_nr)) {
189 memcpy(buf, data->buf, channel->block_size);
190 return 0;
191 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000192#if 0
193 printf("read_block %lu (%d)\n", block, count);
194#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000196 location = (ext2_loff_t) block * channel->block_size;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000197 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
Theodore Ts'oa1230b11997-07-02 02:41:59 +0000198 retval = errno ? errno : EXT2_IO_LLSEEK_FAILED;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 goto error_out;
200 }
201 actual = read(data->dev, buf, size);
202 if (actual != size) {
203 if (actual < 0)
204 actual = 0;
205 retval = EXT2_ET_SHORT_READ;
206 goto error_out;
207 }
208 if (count == 1) {
209 data->buf_block_nr = block;
210 memcpy(data->buf, buf, size); /* Update the cache */
211 }
212 return 0;
213
214error_out:
215 memset((char *) buf+actual, 0, size-actual);
216 if (channel->read_error)
217 retval = (channel->read_error)(channel, block, count, buf,
218 size, actual, retval);
219 return retval;
220}
221
222static errcode_t unix_write_blk(io_channel channel, unsigned long block,
223 int count, const void *buf)
224{
225 struct unix_private_data *data;
226 size_t size;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000227 ext2_loff_t location;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 int actual = 0;
229 errcode_t retval;
230
Theodore Ts'of3db3561997-04-26 13:34:30 +0000231 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000233 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234
235 if (count == 1)
236 size = channel->block_size;
237 else {
238 data->buf_block_nr = -1; /* Invalidate the cache */
239 if (count < 0)
240 size = -count;
241 else
242 size = count * channel->block_size;
243 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000244
245 location = (ext2_loff_t) block * channel->block_size;
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000246 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
Theodore Ts'oa1230b11997-07-02 02:41:59 +0000247 retval = errno ? errno : EXT2_IO_LLSEEK_FAILED;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 goto error_out;
249 }
250
251 actual = write(data->dev, buf, size);
252 if (actual != size) {
253 retval = EXT2_ET_SHORT_WRITE;
254 goto error_out;
255 }
256
257 if ((count == 1) && (block == data->buf_block_nr))
258 memcpy(data->buf, buf, size); /* Update the cache */
259
260 return 0;
261
262error_out:
263 if (channel->write_error)
264 retval = (channel->write_error)(channel, block, count, buf,
265 size, actual, retval);
266 return retval;
267}
268
269/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000270 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000271 */
272static errcode_t unix_flush(io_channel channel)
273{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000274 struct unix_private_data *data;
275
276 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
277 data = (struct unix_private_data *) channel->private_data;
278 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
279
Theodore Ts'o36f21431997-06-14 07:25:40 +0000280 fsync(data->dev);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000281 return 0;
282}
283