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);
76 ext2fs_free_mem(&file->buf);
77 ext2fs_free_mem(&file);
81 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
82 int flags, ext2_file_t *ret)
84 return ext2fs_file_open2(fs, ino, NULL, flags, ret);
88 * This function returns the filesystem handle of a file from the structure
90 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
92 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
98 * This function flushes the dirty block buffer out to disk if
101 errcode_t ext2fs_file_flush(ext2_file_t file)
106 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
109 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
110 !(file->flags & EXT2_FILE_BUF_DIRTY))
114 * OK, the physical block hasn't been allocated yet.
117 if (!file->physblock) {
118 retval = ext2fs_bmap(fs, file->ino, &file->inode,
119 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
120 file->blockno, &file->physblock);
125 retval = io_channel_write_blk(fs->io, file->physblock,
130 file->flags &= ~EXT2_FILE_BUF_DIRTY;
136 * This function synchronizes the file's block buffer and the current
137 * file position, possibly invalidating block buffer if necessary
139 static errcode_t sync_buffer_position(ext2_file_t file)
144 b = file->pos / file->fs->blocksize;
145 if (b != file->blockno) {
146 retval = ext2fs_file_flush(file);
149 file->flags &= ~EXT2_FILE_BUF_VALID;
156 * This function loads the file's block buffer with valid data from
157 * the disk as necessary.
159 * If dontfill is true, then skip initializing the buffer since we're
160 * going to be replacing its entire contents anyway. If set, then the
161 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
164 static errcode_t load_buffer(ext2_file_t file, int dontfill)
166 ext2_filsys fs = file->fs;
169 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
170 retval = ext2fs_bmap(fs, file->ino, &file->inode,
171 BMAP_BUFFER, 0, file->blockno,
176 if (file->physblock) {
177 retval = io_channel_read_blk(fs->io,
183 memset(file->buf, 0, fs->blocksize);
185 file->flags |= EXT2_FILE_BUF_VALID;
191 errcode_t ext2fs_file_close(ext2_file_t file)
195 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
197 retval = ext2fs_file_flush(file);
199 ext2fs_free_mem(&file->buf);
200 ext2fs_free_mem(&file);
206 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
207 unsigned int wanted, unsigned int *got)
210 errcode_t retval = 0;
211 unsigned int start, c, count = 0;
213 char *ptr = (char *) buf;
215 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
218 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
219 retval = sync_buffer_position(file);
222 retval = load_buffer(file, 0);
226 start = file->pos % fs->blocksize;
227 c = fs->blocksize - start;
230 left = EXT2_I_SIZE(&file->inode) - file->pos ;
234 memcpy(ptr, file->buf+start, c);
248 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
249 unsigned int nbytes, unsigned int *written)
252 errcode_t retval = 0;
253 unsigned int start, c, count = 0;
254 const char *ptr = (const char *) buf;
256 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
259 if (!(file->flags & EXT2_FILE_WRITE))
260 return EXT2_ET_FILE_RO;
263 retval = sync_buffer_position(file);
267 start = file->pos % fs->blocksize;
268 c = fs->blocksize - start;
273 * We only need to do a read-modify-update cycle if
274 * we're doing a partial write.
276 retval = load_buffer(file, (c == fs->blocksize));
280 file->flags |= EXT2_FILE_BUF_DIRTY;
281 memcpy(file->buf+start, ptr, c);
294 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
295 int whence, __u64 *ret_pos)
297 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
299 if (whence == EXT2_SEEK_SET)
301 else if (whence == EXT2_SEEK_CUR)
303 else if (whence == EXT2_SEEK_END)
304 file->pos = EXT2_I_SIZE(&file->inode) + offset;
306 return EXT2_ET_INVALID_ARGUMENT;
309 *ret_pos = file->pos;
314 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
315 int whence, ext2_off_t *ret_pos)
317 __u64 loffset, ret_loffset;
321 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
323 *ret_pos = (ext2_off_t) ret_loffset;
329 * This function returns the size of the file, according to the inode
331 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
333 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
334 return EXT2_ET_MAGIC_EXT2_FILE;
335 *ret_size = EXT2_I_SIZE(&file->inode);
340 * This function returns the size of the file, according to the inode
342 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
346 if (ext2fs_file_get_lsize(file, &size))
348 if ((size >> 32) != 0)
354 * This function sets the size of the file, truncating it if necessary
356 * XXX still need to call truncate
358 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
361 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
363 file->inode.i_size = size;
364 file->inode.i_size_high = 0;
366 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
372 * XXX truncate inode if necessary