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
25 struct ext2_inode inode;
33 #define BMAP_BUFFER (file->buf + fs->blocksize)
35 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
36 int flags, ext2_file_t *ret)
42 * Don't let caller create or open a file for writing if the
43 * filesystem is read-only.
45 if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
46 !(fs->flags & EXT2_FLAG_RW))
47 return EXT2_ET_RO_FILSYS;
49 retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
53 memset(file, 0, sizeof(struct ext2_file));
54 file->magic = EXT2_ET_MAGIC_EXT2_FILE;
57 file->flags = flags & EXT2_FILE_MASK;
59 retval = ext2fs_read_inode(fs, ino, &file->inode);
63 retval = ext2fs_get_mem(fs->blocksize * 3, &file->buf);
72 ext2fs_free_mem(&file->buf);
73 ext2fs_free_mem(&file);
78 * This function returns the filesystem handle of a file from the structure
80 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
82 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
88 * This function flushes the dirty block buffer out to disk if
91 errcode_t ext2fs_file_flush(ext2_file_t file)
96 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
99 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
100 !(file->flags & EXT2_FILE_BUF_DIRTY))
104 * OK, the physical block hasn't been allocated yet.
107 if (!file->physblock) {
108 retval = ext2fs_bmap(fs, file->ino, &file->inode,
109 BMAP_BUFFER, BMAP_ALLOC,
110 file->blockno, &file->physblock);
115 retval = io_channel_write_blk(fs->io, file->physblock,
120 file->flags &= ~EXT2_FILE_BUF_DIRTY;
126 * This function synchronizes the file's block buffer and the current
127 * file position, possibly invalidating block buffer if necessary
129 static errcode_t sync_buffer_position(ext2_file_t file)
134 b = file->pos / file->fs->blocksize;
135 if (b != file->blockno) {
136 retval = ext2fs_file_flush(file);
139 file->flags &= ~EXT2_FILE_BUF_VALID;
146 * This function loads the file's block buffer with valid data from
147 * the disk as necessary.
149 * If dontfill is true, then skip initializing the buffer since we're
150 * going to be replacing its entire contents anyway. If set, then the
151 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
154 static errcode_t load_buffer(ext2_file_t file, int dontfill)
156 ext2_filsys fs = file->fs;
159 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
160 retval = ext2fs_bmap(fs, file->ino, &file->inode,
161 BMAP_BUFFER, 0, file->blockno,
166 if (file->physblock) {
167 retval = io_channel_read_blk(fs->io,
173 memset(file->buf, 0, fs->blocksize);
175 file->flags |= EXT2_FILE_BUF_VALID;
181 errcode_t ext2fs_file_close(ext2_file_t file)
185 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
187 retval = ext2fs_file_flush(file);
190 ext2fs_free_mem(&file->buf);
191 ext2fs_free_mem(&file);
197 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
198 unsigned int wanted, unsigned int *got)
201 errcode_t retval = 0;
202 unsigned int start, c, count = 0;
204 char *ptr = (char *) buf;
206 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
209 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
210 retval = sync_buffer_position(file);
213 retval = load_buffer(file, 0);
217 start = file->pos % fs->blocksize;
218 c = fs->blocksize - start;
221 left = EXT2_I_SIZE(&file->inode) - file->pos ;
225 memcpy(ptr, file->buf+start, c);
239 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
240 unsigned int nbytes, unsigned int *written)
243 errcode_t retval = 0;
244 unsigned int start, c, count = 0;
245 const char *ptr = (const char *) buf;
247 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
250 if (!(file->flags & EXT2_FILE_WRITE))
251 return EXT2_ET_FILE_RO;
254 retval = sync_buffer_position(file);
258 start = file->pos % fs->blocksize;
259 c = fs->blocksize - start;
264 * We only need to do a read-modify-update cycle if
265 * we're doing a partial write.
267 retval = load_buffer(file, (c == fs->blocksize));
271 file->flags |= EXT2_FILE_BUF_DIRTY;
272 memcpy(file->buf+start, ptr, c);
285 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
286 int whence, __u64 *ret_pos)
288 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
290 if (whence == EXT2_SEEK_SET)
292 else if (whence == EXT2_SEEK_CUR)
294 else if (whence == EXT2_SEEK_END)
295 file->pos = EXT2_I_SIZE(&file->inode) + offset;
297 return EXT2_ET_INVALID_ARGUMENT;
300 *ret_pos = file->pos;
305 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
306 int whence, ext2_off_t *ret_pos)
308 __u64 loffset, ret_loffset;
312 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
314 *ret_pos = (ext2_off_t) ret_loffset;
320 * This function returns the size of the file, according to the inode
322 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
324 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
325 return EXT2_ET_MAGIC_EXT2_FILE;
326 *ret_size = EXT2_I_SIZE(&file->inode);
331 * This function returns the size of the file, according to the inode
333 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
337 if (ext2fs_file_get_lsize(file, &size))
339 if ((size >> 32) != 0)
345 * This function sets the size of the file, truncating it if necessary
347 * XXX still need to call truncate
349 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
352 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
354 file->inode.i_size = size;
355 file->inode.i_size_high = 0;
356 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
361 * XXX truncate inode if necessary