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.
27 struct ext2_inode inode;
35 #define BMAP_BUFFER (file->buf + fs->blocksize)
37 errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
38 struct ext2_inode *inode,
39 int flags, ext2_file_t *ret)
45 * Don't let caller create or open a file for writing if the
46 * filesystem is read-only.
48 if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
49 !(fs->flags & EXT2_FLAG_RW))
50 return EXT2_ET_RO_FILSYS;
52 retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
56 memset(file, 0, sizeof(struct ext2_file));
57 file->magic = EXT2_ET_MAGIC_EXT2_FILE;
60 file->flags = flags & EXT2_FILE_MASK;
63 memcpy(&file->inode, inode, sizeof(struct ext2_inode));
65 retval = ext2fs_read_inode(fs, ino, &file->inode);
70 retval = ext2fs_get_array(3, fs->blocksize, &file->buf);
79 ext2fs_free_mem(&file->buf);
80 ext2fs_free_mem(&file);
84 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
85 int flags, ext2_file_t *ret)
87 return ext2fs_file_open2(fs, ino, NULL, flags, ret);
91 * This function returns the filesystem handle of a file from the structure
93 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
95 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
101 * This function returns the pointer to the inode of a file from the structure
103 struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file)
105 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
110 /* This function returns the inode number from the structure */
111 ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file)
113 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
119 * This function flushes the dirty block buffer out to disk if
122 errcode_t ext2fs_file_flush(ext2_file_t file)
127 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
130 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
131 !(file->flags & EXT2_FILE_BUF_DIRTY))
135 * OK, the physical block hasn't been allocated yet.
138 if (!file->physblock) {
139 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
140 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
141 file->blockno, 0, &file->physblock);
146 retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf);
150 file->flags &= ~EXT2_FILE_BUF_DIRTY;
156 * This function synchronizes the file's block buffer and the current
157 * file position, possibly invalidating block buffer if necessary
159 static errcode_t sync_buffer_position(ext2_file_t file)
164 b = file->pos / file->fs->blocksize;
165 if (b != file->blockno) {
166 retval = ext2fs_file_flush(file);
169 file->flags &= ~EXT2_FILE_BUF_VALID;
176 * This function loads the file's block buffer with valid data from
177 * the disk as necessary.
179 * If dontfill is true, then skip initializing the buffer since we're
180 * going to be replacing its entire contents anyway. If set, then the
181 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
184 static errcode_t load_buffer(ext2_file_t file, int dontfill)
186 ext2_filsys fs = file->fs;
189 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
190 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
191 BMAP_BUFFER, 0, file->blockno, 0,
196 if (file->physblock) {
197 retval = io_channel_read_blk64(fs->io,
203 memset(file->buf, 0, fs->blocksize);
205 file->flags |= EXT2_FILE_BUF_VALID;
211 errcode_t ext2fs_file_close(ext2_file_t file)
215 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
217 retval = ext2fs_file_flush(file);
220 ext2fs_free_mem(&file->buf);
221 ext2fs_free_mem(&file);
227 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
228 unsigned int wanted, unsigned int *got)
231 errcode_t retval = 0;
232 unsigned int start, c, count = 0;
234 char *ptr = (char *) buf;
236 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
239 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
240 retval = sync_buffer_position(file);
243 retval = load_buffer(file, 0);
247 start = file->pos % fs->blocksize;
248 c = fs->blocksize - start;
251 left = EXT2_I_SIZE(&file->inode) - file->pos ;
255 memcpy(ptr, file->buf+start, c);
269 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
270 unsigned int nbytes, unsigned int *written)
273 errcode_t retval = 0;
274 unsigned int start, c, count = 0;
275 const char *ptr = (const char *) buf;
277 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
280 if (!(file->flags & EXT2_FILE_WRITE))
281 return EXT2_ET_FILE_RO;
284 retval = sync_buffer_position(file);
288 start = file->pos % fs->blocksize;
289 c = fs->blocksize - start;
294 * We only need to do a read-modify-update cycle if
295 * we're doing a partial write.
297 retval = load_buffer(file, (c == fs->blocksize));
302 * OK, the physical block hasn't been allocated yet.
305 if (!file->physblock) {
306 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
308 file->ino ? BMAP_ALLOC : 0,
315 file->flags |= EXT2_FILE_BUF_DIRTY;
316 memcpy(file->buf+start, ptr, c);
324 /* Update inode size */
325 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
328 rc = ext2fs_file_set_size2(file, file->pos);
338 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
339 int whence, __u64 *ret_pos)
341 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
343 if (whence == EXT2_SEEK_SET)
345 else if (whence == EXT2_SEEK_CUR)
347 else if (whence == EXT2_SEEK_END)
348 file->pos = EXT2_I_SIZE(&file->inode) + offset;
350 return EXT2_ET_INVALID_ARGUMENT;
353 *ret_pos = file->pos;
358 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
359 int whence, ext2_off_t *ret_pos)
361 __u64 loffset, ret_loffset;
365 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
367 *ret_pos = (ext2_off_t) ret_loffset;
373 * This function returns the size of the file, according to the inode
375 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
377 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
378 return EXT2_ET_MAGIC_EXT2_FILE;
379 *ret_size = EXT2_I_SIZE(&file->inode);
384 * This function returns the size of the file, according to the inode
386 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
390 if (ext2fs_file_get_lsize(file, &size))
392 if ((size >> 32) != 0)
397 /* Zero the parts of the last block that are past EOF. */
398 static errcode_t ext2fs_file_zero_past_offset(ext2_file_t file,
401 ext2_filsys fs = file->fs;
403 ext2_off64_t off = offset % fs->blocksize;
411 retval = sync_buffer_position(file);
415 /* Is there an initialized block at the end? */
416 retval = ext2fs_bmap2(fs, file->ino, NULL, NULL, 0,
417 offset / fs->blocksize, &ret_flags, &blk);
420 if ((blk == 0) || (ret_flags & BMAP_RET_UNINIT))
423 /* Zero to the end of the block */
424 retval = ext2fs_get_mem(fs->blocksize, &b);
428 /* Read/zero/write block */
429 retval = io_channel_read_blk64(fs->io, blk, 1, b);
433 memset(b + off, 0, fs->blocksize - off);
435 retval = io_channel_write_blk64(fs->io, blk, 1, b);
445 * This function sets the size of the file, truncating it if necessary
448 errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
450 ext2_off64_t old_size;
452 blk64_t old_truncate, truncate_block;
454 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
456 if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode,
457 (size - 1) / file->fs->blocksize))
458 return EXT2_ET_FILE_TOO_BIG;
459 truncate_block = ((size + file->fs->blocksize - 1) >>
460 EXT2_BLOCK_SIZE_BITS(file->fs->super));
461 old_size = EXT2_I_SIZE(&file->inode);
462 old_truncate = ((old_size + file->fs->blocksize - 1) >>
463 EXT2_BLOCK_SIZE_BITS(file->fs->super));
465 retval = ext2fs_inode_size_set(file->fs, &file->inode, size);
470 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
475 retval = ext2fs_file_zero_past_offset(file, size);
479 if (truncate_block >= old_truncate)
482 return ext2fs_punch(file->fs, file->ino, &file->inode, 0,
483 truncate_block, ~0ULL);
486 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
488 return ext2fs_file_set_size2(file, size);