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_open2(ext2_filsys fs, ext2_ino_t ino,
36 struct ext2_inode *inode,
37 int flags, ext2_file_t *ret)
43 * Don't let caller create or open a file for writing if the
44 * filesystem is read-only.
46 if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
47 !(fs->flags & EXT2_FLAG_RW))
48 return EXT2_ET_RO_FILSYS;
50 retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
54 memset(file, 0, sizeof(struct ext2_file));
55 file->magic = EXT2_ET_MAGIC_EXT2_FILE;
58 file->flags = flags & EXT2_FILE_MASK;
61 memcpy(&file->inode, inode, sizeof(struct ext2_inode));
63 retval = ext2fs_read_inode(fs, ino, &file->inode);
68 retval = ext2fs_get_mem(fs->blocksize * 3, &file->buf);
77 ext2fs_free_mem(&file->buf);
78 ext2fs_free_mem(&file);
82 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
83 int flags, ext2_file_t *ret)
85 return ext2fs_file_open2(fs, ino, NULL, flags, ret);
89 * This function returns the filesystem handle of a file from the structure
91 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
93 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
99 * This function flushes the dirty block buffer out to disk if
102 errcode_t ext2fs_file_flush(ext2_file_t file)
107 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
110 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
111 !(file->flags & EXT2_FILE_BUF_DIRTY))
115 * OK, the physical block hasn't been allocated yet.
118 if (!file->physblock) {
119 retval = ext2fs_bmap(fs, file->ino, &file->inode,
120 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
121 file->blockno, &file->physblock);
126 retval = io_channel_write_blk(fs->io, file->physblock,
131 file->flags &= ~EXT2_FILE_BUF_DIRTY;
137 * This function synchronizes the file's block buffer and the current
138 * file position, possibly invalidating block buffer if necessary
140 static errcode_t sync_buffer_position(ext2_file_t file)
145 b = file->pos / file->fs->blocksize;
146 if (b != file->blockno) {
147 retval = ext2fs_file_flush(file);
150 file->flags &= ~EXT2_FILE_BUF_VALID;
157 * This function loads the file's block buffer with valid data from
158 * the disk as necessary.
160 * If dontfill is true, then skip initializing the buffer since we're
161 * going to be replacing its entire contents anyway. If set, then the
162 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
165 static errcode_t load_buffer(ext2_file_t file, int dontfill)
167 ext2_filsys fs = file->fs;
170 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
171 retval = ext2fs_bmap(fs, file->ino, &file->inode,
172 BMAP_BUFFER, 0, file->blockno,
177 if (file->physblock) {
178 retval = io_channel_read_blk(fs->io,
184 memset(file->buf, 0, fs->blocksize);
186 file->flags |= EXT2_FILE_BUF_VALID;
192 errcode_t ext2fs_file_close(ext2_file_t file)
196 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
198 retval = ext2fs_file_flush(file);
201 ext2fs_free_mem(&file->buf);
202 ext2fs_free_mem(&file);
208 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
209 unsigned int wanted, unsigned int *got)
212 errcode_t retval = 0;
213 unsigned int start, c, count = 0;
215 char *ptr = (char *) buf;
217 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
220 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
221 retval = sync_buffer_position(file);
224 retval = load_buffer(file, 0);
228 start = file->pos % fs->blocksize;
229 c = fs->blocksize - start;
232 left = EXT2_I_SIZE(&file->inode) - file->pos ;
236 memcpy(ptr, file->buf+start, c);
250 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
251 unsigned int nbytes, unsigned int *written)
254 errcode_t retval = 0;
255 unsigned int start, c, count = 0;
256 const char *ptr = (const char *) buf;
258 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
261 if (!(file->flags & EXT2_FILE_WRITE))
262 return EXT2_ET_FILE_RO;
265 retval = sync_buffer_position(file);
269 start = file->pos % fs->blocksize;
270 c = fs->blocksize - start;
275 * We only need to do a read-modify-update cycle if
276 * we're doing a partial write.
278 retval = load_buffer(file, (c == fs->blocksize));
282 file->flags |= EXT2_FILE_BUF_DIRTY;
283 memcpy(file->buf+start, ptr, c);
296 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
297 int whence, __u64 *ret_pos)
299 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
301 if (whence == EXT2_SEEK_SET)
303 else if (whence == EXT2_SEEK_CUR)
305 else if (whence == EXT2_SEEK_END)
306 file->pos = EXT2_I_SIZE(&file->inode) + offset;
308 return EXT2_ET_INVALID_ARGUMENT;
311 *ret_pos = file->pos;
316 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
317 int whence, ext2_off_t *ret_pos)
319 __u64 loffset, ret_loffset;
323 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
325 *ret_pos = (ext2_off_t) ret_loffset;
331 * This function returns the size of the file, according to the inode
333 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
335 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
336 return EXT2_ET_MAGIC_EXT2_FILE;
337 *ret_size = EXT2_I_SIZE(&file->inode);
342 * This function returns the size of the file, according to the inode
344 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
348 if (ext2fs_file_get_lsize(file, &size))
350 if ((size >> 32) != 0)
356 * This function sets the size of the file, truncating it if necessary
358 * XXX still need to call truncate
360 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
363 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
365 file->inode.i_size = size;
366 file->inode.i_size_high = 0;
368 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
374 * XXX truncate inode if necessary