2 * fileio.c --- Simple file I/O routines
4 * Copyright (C) 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
18 #if EXT2_FLAT_INCLUDES
21 #include <linux/ext2_fs.h>
30 struct ext2_inode inode;
38 #define BMAP_BUFFER (file->buf + fs->blocksize)
40 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
41 int flags, ext2_file_t *ret)
47 * Don't let caller create or open a file for writing if the
48 * filesystem is read-only.
50 if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
51 !(fs->flags & EXT2_FLAG_RW))
52 return EXT2_ET_RO_FILSYS;
54 retval = ext2fs_get_mem(sizeof(struct ext2_file), (void **) &file);
58 memset(file, 0, sizeof(struct ext2_file));
59 file->magic = EXT2_ET_MAGIC_EXT2_FILE;
62 file->flags = flags & EXT2_FILE_MASK;
64 retval = ext2fs_read_inode(fs, ino, &file->inode);
68 retval = ext2fs_get_mem(fs->blocksize * 3, (void **) &file->buf);
77 ext2fs_free_mem((void **) &file->buf);
78 ext2fs_free_mem((void **) &file);
83 * This function returns the filesystem handle of a file from the structure
85 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
87 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
93 * This function flushes the dirty block buffer out to disk if
96 static errcode_t ext2fs_file_flush(ext2_file_t file)
101 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
104 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
105 !(file->flags & EXT2_FILE_BUF_DIRTY))
109 * OK, the physical block hasn't been allocated yet.
112 if (!file->physblock) {
113 retval = ext2fs_bmap(fs, file->ino, &file->inode,
114 BMAP_BUFFER, BMAP_ALLOC,
115 file->blockno, &file->physblock);
120 retval = io_channel_write_blk(fs->io, file->physblock,
125 file->flags &= ~EXT2_FILE_BUF_DIRTY;
131 * This function synchronizes the file's block buffer and the current
132 * file position, possibly invalidating block buffer if necessary
134 static errcode_t sync_buffer_position(ext2_file_t file)
139 b = file->pos / file->fs->blocksize;
140 if (b != file->blockno) {
141 retval = ext2fs_file_flush(file);
144 file->flags &= ~EXT2_FILE_BUF_VALID;
151 * This function loads the file's block buffer with valid data from
152 * the disk as necessary.
154 * If dontfill is true, then skip initializing the buffer since we're
155 * going to be replacing its entire contents anyway. If set, then the
156 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
159 static errcode_t load_buffer(ext2_file_t file, int dontfill)
161 ext2_filsys fs = file->fs;
164 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
165 retval = ext2fs_bmap(fs, file->ino, &file->inode,
166 BMAP_BUFFER, 0, file->blockno,
171 if (file->physblock) {
172 retval = io_channel_read_blk(fs->io,
178 memset(file->buf, 0, fs->blocksize);
180 file->flags |= EXT2_FILE_BUF_VALID;
186 errcode_t ext2fs_file_close(ext2_file_t file)
190 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
192 retval = ext2fs_file_flush(file);
195 ext2fs_free_mem((void **) &file->buf);
196 ext2fs_free_mem((void **) &file);
202 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
203 unsigned int wanted, unsigned int *got)
206 errcode_t retval = 0;
207 unsigned int start, left, c, count = 0;
208 char *ptr = (char *) buf;
210 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
213 while ((file->pos < file->inode.i_size) && (wanted > 0)) {
214 retval = sync_buffer_position(file);
217 retval = load_buffer(file, 0);
221 start = file->pos % fs->blocksize;
222 c = fs->blocksize - start;
225 left = file->inode.i_size - file->pos ;
229 memcpy(ptr, file->buf+start, c);
243 errcode_t ext2fs_file_write(ext2_file_t file, void *buf,
244 unsigned int nbytes, unsigned int *written)
248 errcode_t retval = 0;
249 unsigned int start, c, count = 0;
250 char *ptr = (char *) buf;
253 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
256 if (!(file->flags & EXT2_FILE_WRITE))
257 return EXT2_ET_FILE_RO;
260 retval = sync_buffer_position(file);
264 start = file->pos % fs->blocksize;
265 c = fs->blocksize - start;
270 * We only need to do a read-modify-update cycle if
271 * we're doing a partial write.
273 retval = load_buffer(file, (c == fs->blocksize));
277 file->flags |= EXT2_FILE_BUF_DIRTY;
278 memcpy(file->buf+start, ptr, c);
291 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
292 int whence, ext2_off_t *ret_pos)
294 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
296 if (whence == EXT2_SEEK_SET)
298 else if (whence == EXT2_SEEK_CUR)
300 else if (whence == EXT2_SEEK_END)
301 file->pos = file->inode.i_size + offset;
303 return EXT2_ET_INVALID_ARGUMENT;
306 *ret_pos = file->pos;
312 * This function returns the size of the file, according to the inode
314 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
316 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
318 return file->inode.i_size;
322 * This function sets the size of the file, truncating it if necessary
324 * XXX still need to call truncate
326 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
329 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
331 file->inode.i_size = size;
332 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
337 * XXX truncate inode if necessary