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)
129 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
132 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
133 !(file->flags & EXT2_FILE_BUF_DIRTY))
136 /* Is this an uninit block? */
137 if (file->physblock && file->inode.i_flags & EXT4_EXTENTS_FL) {
138 retval = ext2fs_bmap2(fs, file->ino, &file->inode, BMAP_BUFFER,
139 0, file->blockno, &ret_flags, &dontcare);
142 if (ret_flags & BMAP_RET_UNINIT) {
143 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
144 BMAP_BUFFER, BMAP_SET,
153 * OK, the physical block hasn't been allocated yet.
156 if (!file->physblock) {
157 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
158 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
159 file->blockno, 0, &file->physblock);
164 retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf);
168 file->flags &= ~EXT2_FILE_BUF_DIRTY;
174 * This function synchronizes the file's block buffer and the current
175 * file position, possibly invalidating block buffer if necessary
177 static errcode_t sync_buffer_position(ext2_file_t file)
182 b = file->pos / file->fs->blocksize;
183 if (b != file->blockno) {
184 retval = ext2fs_file_flush(file);
187 file->flags &= ~EXT2_FILE_BUF_VALID;
194 * This function loads the file's block buffer with valid data from
195 * the disk as necessary.
197 * If dontfill is true, then skip initializing the buffer since we're
198 * going to be replacing its entire contents anyway. If set, then the
199 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
202 static errcode_t load_buffer(ext2_file_t file, int dontfill)
204 ext2_filsys fs = file->fs;
208 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
209 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
210 BMAP_BUFFER, 0, file->blockno, &ret_flags,
215 if (file->physblock &&
216 !(ret_flags & BMAP_RET_UNINIT)) {
217 retval = io_channel_read_blk64(fs->io,
223 memset(file->buf, 0, fs->blocksize);
225 file->flags |= EXT2_FILE_BUF_VALID;
231 errcode_t ext2fs_file_close(ext2_file_t file)
235 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
237 retval = ext2fs_file_flush(file);
240 ext2fs_free_mem(&file->buf);
241 ext2fs_free_mem(&file);
248 ext2fs_file_read_inline_data(ext2_file_t file, void *buf,
249 unsigned int wanted, unsigned int *got)
253 unsigned int count = 0;
257 retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
262 if (file->pos >= size)
265 count = size - file->pos;
268 memcpy(buf, file->buf + file->pos, count);
270 buf = (char *) buf + count;
279 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
280 unsigned int wanted, unsigned int *got)
283 errcode_t retval = 0;
284 unsigned int start, c, count = 0;
286 char *ptr = (char *) buf;
288 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
291 /* If an inode has inline data, things get complicated. */
292 if (file->inode.i_flags & EXT4_INLINE_DATA_FL)
293 return ext2fs_file_read_inline_data(file, buf, wanted, got);
295 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
296 retval = sync_buffer_position(file);
299 retval = load_buffer(file, 0);
303 start = file->pos % fs->blocksize;
304 c = fs->blocksize - start;
307 left = EXT2_I_SIZE(&file->inode) - file->pos ;
311 memcpy(ptr, file->buf+start, c);
326 ext2fs_file_write_inline_data(ext2_file_t file, const void *buf,
327 unsigned int nbytes, unsigned int *written)
331 unsigned int count = 0;
335 retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
340 if (file->pos < size) {
341 count = nbytes - file->pos;
342 memcpy(file->buf + file->pos, buf, count);
344 retval = ext2fs_inline_data_set(fs, file->ino, &file->inode,
346 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE)
353 /* Update inode size */
354 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
357 rc = ext2fs_file_set_size2(file, file->pos);
368 retval = ext2fs_inline_data_expand(fs, file->ino);
372 * reload inode and return no space error
374 * XXX: file->inode could be copied from the outside
375 * in ext2fs_file_open2(). We have no way to modify
378 retval = ext2fs_read_inode(fs, file->ino, &file->inode);
381 return EXT2_ET_INLINE_DATA_NO_SPACE;
385 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
386 unsigned int nbytes, unsigned int *written)
389 errcode_t retval = 0;
390 unsigned int start, c, count = 0;
391 const char *ptr = (const char *) buf;
393 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
396 if (!(file->flags & EXT2_FILE_WRITE))
397 return EXT2_ET_FILE_RO;
399 /* If an inode has inline data, things get complicated. */
400 if (file->inode.i_flags & EXT4_INLINE_DATA_FL) {
401 retval = ext2fs_file_write_inline_data(file, buf, nbytes,
403 if (retval != EXT2_ET_INLINE_DATA_NO_SPACE)
405 /* fall through to read data from the block */
410 retval = sync_buffer_position(file);
414 start = file->pos % fs->blocksize;
415 c = fs->blocksize - start;
420 * We only need to do a read-modify-update cycle if
421 * we're doing a partial write.
423 retval = load_buffer(file, (c == fs->blocksize));
428 * OK, the physical block hasn't been allocated yet.
431 if (!file->physblock) {
432 retval = ext2fs_bmap2(fs, file->ino, &file->inode,
434 file->ino ? BMAP_ALLOC : 0,
441 file->flags |= EXT2_FILE_BUF_DIRTY;
442 memcpy(file->buf+start, ptr, c);
450 /* Update inode size */
451 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
454 rc = ext2fs_file_set_size2(file, file->pos);
464 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
465 int whence, __u64 *ret_pos)
467 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
469 if (whence == EXT2_SEEK_SET)
471 else if (whence == EXT2_SEEK_CUR)
473 else if (whence == EXT2_SEEK_END)
474 file->pos = EXT2_I_SIZE(&file->inode) + offset;
476 return EXT2_ET_INVALID_ARGUMENT;
479 *ret_pos = file->pos;
484 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
485 int whence, ext2_off_t *ret_pos)
487 __u64 loffset, ret_loffset = 0;
491 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
493 *ret_pos = (ext2_off_t) ret_loffset;
499 * This function returns the size of the file, according to the inode
501 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
503 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
504 return EXT2_ET_MAGIC_EXT2_FILE;
505 *ret_size = EXT2_I_SIZE(&file->inode);
510 * This function returns the size of the file, according to the inode
512 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
516 if (ext2fs_file_get_lsize(file, &size))
518 if ((size >> 32) != 0)
523 /* Zero the parts of the last block that are past EOF. */
524 static errcode_t ext2fs_file_zero_past_offset(ext2_file_t file,
527 ext2_filsys fs = file->fs;
529 ext2_off64_t off = offset % fs->blocksize;
537 retval = sync_buffer_position(file);
541 /* Is there an initialized block at the end? */
542 retval = ext2fs_bmap2(fs, file->ino, NULL, NULL, 0,
543 offset / fs->blocksize, &ret_flags, &blk);
546 if ((blk == 0) || (ret_flags & BMAP_RET_UNINIT))
549 /* Zero to the end of the block */
550 retval = ext2fs_get_mem(fs->blocksize, &b);
554 /* Read/zero/write block */
555 retval = io_channel_read_blk64(fs->io, blk, 1, b);
559 memset(b + off, 0, fs->blocksize - off);
561 retval = io_channel_write_blk64(fs->io, blk, 1, b);
571 * This function sets the size of the file, truncating it if necessary
574 errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
576 ext2_off64_t old_size;
578 blk64_t old_truncate, truncate_block;
580 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
582 if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode,
583 (size - 1) / file->fs->blocksize))
584 return EXT2_ET_FILE_TOO_BIG;
585 truncate_block = ((size + file->fs->blocksize - 1) >>
586 EXT2_BLOCK_SIZE_BITS(file->fs->super));
587 old_size = EXT2_I_SIZE(&file->inode);
588 old_truncate = ((old_size + file->fs->blocksize - 1) >>
589 EXT2_BLOCK_SIZE_BITS(file->fs->super));
591 retval = ext2fs_inode_size_set(file->fs, &file->inode, size);
596 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
601 retval = ext2fs_file_zero_past_offset(file, size);
605 if (truncate_block >= old_truncate)
608 return ext2fs_punch(file->fs, file->ino, &file->inode, 0,
609 truncate_block, ~0ULL);
612 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
614 return ext2fs_file_set_size2(file, size);