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;
37 unsigned char sha[EXT2FS_SHA512_LENGTH];
39 typedef struct block_entry *block_entry_t;
41 #define BMAP_BUFFER (file->buf + fs->blocksize)
43 errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
44 struct ext2_inode *inode,
45 int flags, ext2_file_t *ret)
51 * Don't let caller create or open a file for writing if the
52 * filesystem is read-only.
54 if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
55 !(fs->flags & EXT2_FLAG_RW))
56 return EXT2_ET_RO_FILSYS;
58 retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
62 memset(file, 0, sizeof(struct ext2_file));
63 file->magic = EXT2_ET_MAGIC_EXT2_FILE;
66 file->flags = flags & EXT2_FILE_MASK;
69 memcpy(&file->inode, inode, sizeof(struct ext2_inode));
71 retval = ext2fs_read_inode(fs, ino, &file->inode);
76 retval = ext2fs_get_array(3, fs->blocksize, &file->buf);
85 ext2fs_free_mem(&file->buf);
86 ext2fs_free_mem(&file);
90 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
91 int flags, ext2_file_t *ret)
93 return ext2fs_file_open2(fs, ino, NULL, flags, ret);
97 * This function returns the filesystem handle of a file from the structure
99 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
101 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
107 * This function returns the pointer to the inode of a file from the structure
109 struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file)
111 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
116 /* This function returns the inode number from the structure */
117 ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file)
119 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
125 * This function flushes the dirty block buffer out to disk if
128 errcode_t ext2fs_file_flush(ext2_file_t file)
135 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
138 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
139 !(file->flags & EXT2_FILE_BUF_DIRTY))
142 /* Is this an uninit block? */
143 if (file->physblock && file->inode.i_flags & EXT4_EXTENTS_FL) {
144 retval = ext2fs_bmap2(fs, file->ino, &file->inode, BMAP_BUFFER,
145 0, file->blockno, &ret_flags, &dontcare);
148 if (ret_flags & BMAP_RET_UNINIT) {
149 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
150 BMAP_BUFFER, BMAP_SET,
159 * OK, the physical block hasn't been allocated yet.
162 if (!file->physblock) {
163 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
164 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
165 file->blockno, 0, &file->physblock);
170 retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf);
174 file->flags &= ~EXT2_FILE_BUF_DIRTY;
180 * This function synchronizes the file's block buffer and the current
181 * file position, possibly invalidating block buffer if necessary
183 static errcode_t sync_buffer_position(ext2_file_t file)
188 b = file->pos / file->fs->blocksize;
189 if (b != file->blockno) {
190 retval = ext2fs_file_flush(file);
193 file->flags &= ~EXT2_FILE_BUF_VALID;
200 * This function loads the file's block buffer with valid data from
201 * the disk as necessary.
203 * If dontfill is true, then skip initializing the buffer since we're
204 * going to be replacing its entire contents anyway. If set, then the
205 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
208 static errcode_t load_buffer(ext2_file_t file, int dontfill)
210 ext2_filsys fs = file->fs;
214 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
215 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
216 BMAP_BUFFER, 0, file->blockno, &ret_flags,
221 if (file->physblock &&
222 !(ret_flags & BMAP_RET_UNINIT)) {
223 retval = io_channel_read_blk64(fs->io,
229 memset(file->buf, 0, fs->blocksize);
231 file->flags |= EXT2_FILE_BUF_VALID;
237 errcode_t ext2fs_file_close(ext2_file_t file)
241 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
243 retval = ext2fs_file_flush(file);
246 ext2fs_free_mem(&file->buf);
247 ext2fs_free_mem(&file);
254 ext2fs_file_read_inline_data(ext2_file_t file, void *buf,
255 unsigned int wanted, unsigned int *got)
259 unsigned int count = 0;
263 retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
268 if (file->pos >= size)
271 count = size - file->pos;
274 memcpy(buf, file->buf + file->pos, count);
276 buf = (char *) buf + count;
285 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
286 unsigned int wanted, unsigned int *got)
289 errcode_t retval = 0;
290 unsigned int start, c, count = 0;
292 char *ptr = (char *) buf;
294 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
297 /* If an inode has inline data, things get complicated. */
298 if (file->inode.i_flags & EXT4_INLINE_DATA_FL)
299 return ext2fs_file_read_inline_data(file, buf, wanted, got);
301 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
302 retval = sync_buffer_position(file);
305 retval = load_buffer(file, 0);
309 start = file->pos % fs->blocksize;
310 c = fs->blocksize - start;
313 left = EXT2_I_SIZE(&file->inode) - file->pos ;
317 memcpy(ptr, file->buf+start, c);
332 ext2fs_file_write_inline_data(ext2_file_t file, const void *buf,
333 unsigned int nbytes, unsigned int *written)
337 unsigned int count = 0;
341 retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
346 if (file->pos < size) {
347 count = nbytes - file->pos;
348 memcpy(file->buf + file->pos, buf, count);
350 retval = ext2fs_inline_data_set(fs, file->ino, &file->inode,
352 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE)
359 /* Update inode size */
360 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
363 rc = ext2fs_file_set_size2(file, file->pos);
374 retval = ext2fs_inline_data_expand(fs, file->ino);
378 * reload inode and return no space error
380 * XXX: file->inode could be copied from the outside
381 * in ext2fs_file_open2(). We have no way to modify
384 retval = ext2fs_read_inode(fs, file->ino, &file->inode);
387 return EXT2_ET_INLINE_DATA_NO_SPACE;
391 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
392 unsigned int nbytes, unsigned int *written)
395 errcode_t retval = 0;
396 unsigned int start, c, count = 0;
397 const char *ptr = (const char *) buf;
398 block_entry_t new_block = NULL, old_block = NULL;
401 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
404 if (!(file->flags & EXT2_FILE_WRITE))
405 return EXT2_ET_FILE_RO;
407 /* If an inode has inline data, things get complicated. */
408 if (file->inode.i_flags & EXT4_INLINE_DATA_FL) {
409 retval = ext2fs_file_write_inline_data(file, buf, nbytes,
411 if (retval != EXT2_ET_INLINE_DATA_NO_SPACE)
413 /* fall through to read data from the block */
418 retval = sync_buffer_position(file);
422 start = file->pos % fs->blocksize;
423 c = fs->blocksize - start;
428 * We only need to do a read-modify-update cycle if
429 * we're doing a partial write.
431 retval = load_buffer(file, (c == fs->blocksize));
435 file->flags |= EXT2_FILE_BUF_DIRTY;
436 memcpy(file->buf+start, ptr, c);
439 * OK, the physical block hasn't been allocated yet.
442 if (!file->physblock) {
443 bmap_flags = (file->ino ? BMAP_ALLOC : 0);
444 if (fs->flags & EXT2_FLAG_SHARE_DUP) {
445 new_block = calloc(1, sizeof(*new_block));
447 retval = EXT2_ET_NO_MEMORY;
450 ext2fs_sha512((const unsigned char*)file->buf,
451 fs->blocksize, new_block->sha);
452 old_block = ext2fs_hashmap_lookup(
455 sizeof(new_block->sha));
459 file->physblock = old_block->physblock;
460 bmap_flags |= BMAP_SET;
465 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
477 new_block->physblock = file->physblock;
478 ext2fs_hashmap_add(fs->block_sha_map, new_block,
479 new_block->sha, sizeof(new_block->sha));
482 if (bmap_flags & BMAP_SET) {
483 ext2fs_iblk_add_blocks(fs, &file->inode, 1);
484 ext2fs_write_inode(fs, file->ino, &file->inode);
495 /* Update inode size */
496 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
499 rc = ext2fs_file_set_size2(file, file->pos);
509 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
510 int whence, __u64 *ret_pos)
512 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
514 if (whence == EXT2_SEEK_SET)
516 else if (whence == EXT2_SEEK_CUR)
518 else if (whence == EXT2_SEEK_END)
519 file->pos = EXT2_I_SIZE(&file->inode) + offset;
521 return EXT2_ET_INVALID_ARGUMENT;
524 *ret_pos = file->pos;
529 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
530 int whence, ext2_off_t *ret_pos)
532 __u64 loffset, ret_loffset = 0;
536 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
538 *ret_pos = (ext2_off_t) ret_loffset;
544 * This function returns the size of the file, according to the inode
546 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
548 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
549 return EXT2_ET_MAGIC_EXT2_FILE;
550 *ret_size = EXT2_I_SIZE(&file->inode);
555 * This function returns the size of the file, according to the inode
557 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
561 if (ext2fs_file_get_lsize(file, &size))
563 if ((size >> 32) != 0)
568 /* Zero the parts of the last block that are past EOF. */
569 static errcode_t ext2fs_file_zero_past_offset(ext2_file_t file,
572 ext2_filsys fs = file->fs;
574 ext2_off64_t off = offset % fs->blocksize;
582 retval = sync_buffer_position(file);
586 /* Is there an initialized block at the end? */
587 retval = ext2fs_bmap2(fs, file->ino, NULL, NULL, 0,
588 offset / fs->blocksize, &ret_flags, &blk);
591 if ((blk == 0) || (ret_flags & BMAP_RET_UNINIT))
594 /* Zero to the end of the block */
595 retval = ext2fs_get_mem(fs->blocksize, &b);
599 /* Read/zero/write block */
600 retval = io_channel_read_blk64(fs->io, blk, 1, b);
604 memset(b + off, 0, fs->blocksize - off);
606 retval = io_channel_write_blk64(fs->io, blk, 1, b);
616 * This function sets the size of the file, truncating it if necessary
619 errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
621 ext2_off64_t old_size;
623 blk64_t old_truncate, truncate_block;
625 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
627 if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode,
628 (size - 1) / file->fs->blocksize))
629 return EXT2_ET_FILE_TOO_BIG;
630 truncate_block = ((size + file->fs->blocksize - 1) >>
631 EXT2_BLOCK_SIZE_BITS(file->fs->super));
632 old_size = EXT2_I_SIZE(&file->inode);
633 old_truncate = ((old_size + file->fs->blocksize - 1) >>
634 EXT2_BLOCK_SIZE_BITS(file->fs->super));
636 retval = ext2fs_inode_size_set(file->fs, &file->inode, size);
641 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
646 retval = ext2fs_file_zero_past_offset(file, size);
650 if (truncate_block >= old_truncate)
653 return ext2fs_punch(file->fs, file->ino, &file->inode, 0,
654 truncate_block, ~0ULL);
657 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
659 return ext2fs_file_set_size2(file, size);