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 Library
8 * General Public License, version 2.
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_array(3, fs->blocksize, &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 returns the pointer to the inode of a file from the structure
101 struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file)
103 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
109 * This function flushes the dirty block buffer out to disk if
112 errcode_t ext2fs_file_flush(ext2_file_t file)
117 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
120 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
121 !(file->flags & EXT2_FILE_BUF_DIRTY))
125 * OK, the physical block hasn't been allocated yet.
128 if (!file->physblock) {
129 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
130 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
131 file->blockno, 0, &file->physblock);
136 retval = io_channel_write_blk(fs->io, file->physblock,
141 file->flags &= ~EXT2_FILE_BUF_DIRTY;
147 * This function synchronizes the file's block buffer and the current
148 * file position, possibly invalidating block buffer if necessary
150 static errcode_t sync_buffer_position(ext2_file_t file)
155 b = file->pos / file->fs->blocksize;
156 if (b != file->blockno) {
157 retval = ext2fs_file_flush(file);
160 file->flags &= ~EXT2_FILE_BUF_VALID;
167 * This function loads the file's block buffer with valid data from
168 * the disk as necessary.
170 * If dontfill is true, then skip initializing the buffer since we're
171 * going to be replacing its entire contents anyway. If set, then the
172 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
175 static errcode_t load_buffer(ext2_file_t file, int dontfill)
177 ext2_filsys fs = file->fs;
180 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
181 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
182 BMAP_BUFFER, 0, file->blockno, 0,
187 if (file->physblock) {
188 retval = io_channel_read_blk(fs->io,
194 memset(file->buf, 0, fs->blocksize);
196 file->flags |= EXT2_FILE_BUF_VALID;
202 errcode_t ext2fs_file_close(ext2_file_t file)
206 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
208 retval = ext2fs_file_flush(file);
211 ext2fs_free_mem(&file->buf);
212 ext2fs_free_mem(&file);
218 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
219 unsigned int wanted, unsigned int *got)
222 errcode_t retval = 0;
223 unsigned int start, c, count = 0;
225 char *ptr = (char *) buf;
227 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
230 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
231 retval = sync_buffer_position(file);
234 retval = load_buffer(file, 0);
238 start = file->pos % fs->blocksize;
239 c = fs->blocksize - start;
242 left = EXT2_I_SIZE(&file->inode) - file->pos ;
246 memcpy(ptr, file->buf+start, c);
260 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
261 unsigned int nbytes, unsigned int *written)
264 errcode_t retval = 0;
265 unsigned int start, c, count = 0;
266 const char *ptr = (const char *) buf;
268 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
271 if (!(file->flags & EXT2_FILE_WRITE))
272 return EXT2_ET_FILE_RO;
275 retval = sync_buffer_position(file);
279 start = file->pos % fs->blocksize;
280 c = fs->blocksize - start;
285 * We only need to do a read-modify-update cycle if
286 * we're doing a partial write.
288 retval = load_buffer(file, (c == fs->blocksize));
292 file->flags |= EXT2_FILE_BUF_DIRTY;
293 memcpy(file->buf+start, ptr, c);
306 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
307 int whence, __u64 *ret_pos)
309 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
311 if (whence == EXT2_SEEK_SET)
313 else if (whence == EXT2_SEEK_CUR)
315 else if (whence == EXT2_SEEK_END)
316 file->pos = EXT2_I_SIZE(&file->inode) + offset;
318 return EXT2_ET_INVALID_ARGUMENT;
321 *ret_pos = file->pos;
326 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
327 int whence, ext2_off_t *ret_pos)
329 __u64 loffset, ret_loffset;
333 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
335 *ret_pos = (ext2_off_t) ret_loffset;
341 * This function returns the size of the file, according to the inode
343 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
345 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
346 return EXT2_ET_MAGIC_EXT2_FILE;
347 *ret_size = EXT2_I_SIZE(&file->inode);
352 * This function returns the size of the file, according to the inode
354 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
358 if (ext2fs_file_get_lsize(file, &size))
360 if ((size >> 32) != 0)
366 * This function sets the size of the file, truncating it if necessary
369 errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
371 ext2_off64_t old_size;
373 blk64_t old_truncate, truncate_block;
375 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
377 truncate_block = ((size + file->fs->blocksize - 1) >>
378 EXT2_BLOCK_SIZE_BITS(file->fs->super)) + 1;
379 old_size = file->inode.i_size +
380 ((blk64_t)file->inode.i_size_high << 32);
381 old_truncate = ((old_size + file->fs->blocksize - 1) >>
382 EXT2_BLOCK_SIZE_BITS(file->fs->super)) + 1;
384 file->inode.i_size = size & 0xffffffff;
385 file->inode.i_size_high = (size >> 32);
387 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
392 if (truncate_block <= old_truncate)
395 return ext2fs_punch(file->fs, file->ino, &file->inode, 0,
396 truncate_block, ~0ULL);
399 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
401 return ext2fs_file_set_size2(file, size);