Lines Matching full:file
2 * fileio.c --- Simple file I/O routines
7 * This file may be redistributed under the terms of the GNU Library
41 #define BMAP_BUFFER (file->buf + fs->blocksize)
47 ext2_file_t file; in ext2fs_file_open2() local
51 * Don't let caller create or open a file for writing if the in ext2fs_file_open2()
58 retval = ext2fs_get_mem(sizeof(struct ext2_file), &file); in ext2fs_file_open2()
62 memset(file, 0, sizeof(struct ext2_file)); in ext2fs_file_open2()
63 file->magic = EXT2_ET_MAGIC_EXT2_FILE; in ext2fs_file_open2()
64 file->fs = fs; in ext2fs_file_open2()
65 file->ino = ino; in ext2fs_file_open2()
66 file->flags = flags & EXT2_FILE_MASK; in ext2fs_file_open2()
69 memcpy(&file->inode, inode, sizeof(struct ext2_inode)); in ext2fs_file_open2()
71 retval = ext2fs_read_inode(fs, ino, &file->inode); in ext2fs_file_open2()
76 retval = ext2fs_get_array(3, fs->blocksize, &file->buf); in ext2fs_file_open2()
80 *ret = file; in ext2fs_file_open2()
84 if (file->buf) in ext2fs_file_open2()
85 ext2fs_free_mem(&file->buf); in ext2fs_file_open2()
86 ext2fs_free_mem(&file); in ext2fs_file_open2()
97 * This function returns the filesystem handle of a file from the structure
99 ext2_filsys ext2fs_file_get_fs(ext2_file_t file) in ext2fs_file_get_fs() argument
101 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_fs()
103 return file->fs; in ext2fs_file_get_fs()
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) in ext2fs_file_get_inode() argument
111 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_inode()
113 return &file->inode; in ext2fs_file_get_inode()
117 ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file) in ext2fs_file_get_inode_num() argument
119 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_inode_num()
121 return file->ino; in ext2fs_file_get_inode_num()
128 errcode_t ext2fs_file_flush(ext2_file_t file) in ext2fs_file_flush() argument
135 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_flush()
136 fs = file->fs; in ext2fs_file_flush()
138 if (!(file->flags & EXT2_FILE_BUF_VALID) || in ext2fs_file_flush()
139 !(file->flags & EXT2_FILE_BUF_DIRTY)) in ext2fs_file_flush()
143 if (file->physblock && file->inode.i_flags & EXT4_EXTENTS_FL) { in ext2fs_file_flush()
144 retval = ext2fs_bmap2(fs, file->ino, &file->inode, BMAP_BUFFER, in ext2fs_file_flush()
145 0, file->blockno, &ret_flags, &dontcare); in ext2fs_file_flush()
149 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in ext2fs_file_flush()
151 file->blockno, 0, in ext2fs_file_flush()
152 &file->physblock); in ext2fs_file_flush()
162 if (!file->physblock) { in ext2fs_file_flush()
163 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in ext2fs_file_flush()
164 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0, in ext2fs_file_flush()
165 file->blockno, 0, &file->physblock); in ext2fs_file_flush()
170 retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf); in ext2fs_file_flush()
174 file->flags &= ~EXT2_FILE_BUF_DIRTY; in ext2fs_file_flush()
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) in sync_buffer_position() argument
188 b = file->pos / file->fs->blocksize; in sync_buffer_position()
189 if (b != file->blockno) { in sync_buffer_position()
190 retval = ext2fs_file_flush(file); in sync_buffer_position()
193 file->flags &= ~EXT2_FILE_BUF_VALID; in sync_buffer_position()
195 file->blockno = b; in sync_buffer_position()
200 * This function loads the file's block buffer with valid data from
205 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
208 static errcode_t load_buffer(ext2_file_t file, int dontfill) in load_buffer() argument
210 ext2_filsys fs = file->fs; in load_buffer()
214 if (!(file->flags & EXT2_FILE_BUF_VALID)) { in load_buffer()
215 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in load_buffer()
216 BMAP_BUFFER, 0, file->blockno, &ret_flags, in load_buffer()
217 &file->physblock); in load_buffer()
221 if (file->physblock && in load_buffer()
224 file->physblock, in load_buffer()
225 1, file->buf); in load_buffer()
229 memset(file->buf, 0, fs->blocksize); in load_buffer()
231 file->flags |= EXT2_FILE_BUF_VALID; in load_buffer()
237 errcode_t ext2fs_file_close(ext2_file_t file) in ext2fs_file_close() argument
241 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_close()
243 retval = ext2fs_file_flush(file); in ext2fs_file_close()
245 if (file->buf) in ext2fs_file_close()
246 ext2fs_free_mem(&file->buf); in ext2fs_file_close()
247 ext2fs_free_mem(&file); in ext2fs_file_close()
254 ext2fs_file_read_inline_data(ext2_file_t file, void *buf, in ext2fs_file_read_inline_data() argument
262 fs = file->fs; in ext2fs_file_read_inline_data()
263 retval = ext2fs_inline_data_get(fs, file->ino, &file->inode, in ext2fs_file_read_inline_data()
264 file->buf, &size); in ext2fs_file_read_inline_data()
268 if (file->pos >= size) in ext2fs_file_read_inline_data()
271 count = size - file->pos; in ext2fs_file_read_inline_data()
274 memcpy(buf, file->buf + file->pos, count); in ext2fs_file_read_inline_data()
275 file->pos += count; in ext2fs_file_read_inline_data()
285 errcode_t ext2fs_file_read(ext2_file_t file, void *buf, in ext2fs_file_read() argument
294 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_read()
295 fs = file->fs; in ext2fs_file_read()
298 if (file->inode.i_flags & EXT4_INLINE_DATA_FL) in ext2fs_file_read()
299 return ext2fs_file_read_inline_data(file, buf, wanted, got); in ext2fs_file_read()
301 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) { in ext2fs_file_read()
302 retval = sync_buffer_position(file); in ext2fs_file_read()
305 retval = load_buffer(file, 0); in ext2fs_file_read()
309 start = file->pos % fs->blocksize; in ext2fs_file_read()
313 left = EXT2_I_SIZE(&file->inode) - file->pos ; in ext2fs_file_read()
317 memcpy(ptr, file->buf+start, c); in ext2fs_file_read()
318 file->pos += c; in ext2fs_file_read()
332 ext2fs_file_write_inline_data(ext2_file_t file, const void *buf, in ext2fs_file_write_inline_data() argument
340 fs = file->fs; in ext2fs_file_write_inline_data()
341 retval = ext2fs_inline_data_get(fs, file->ino, &file->inode, in ext2fs_file_write_inline_data()
342 file->buf, &size); in ext2fs_file_write_inline_data()
346 if (file->pos < size) { in ext2fs_file_write_inline_data()
347 count = nbytes - file->pos; in ext2fs_file_write_inline_data()
348 memcpy(file->buf + file->pos, buf, count); in ext2fs_file_write_inline_data()
350 retval = ext2fs_inline_data_set(fs, file->ino, &file->inode, in ext2fs_file_write_inline_data()
351 file->buf, count); in ext2fs_file_write_inline_data()
357 file->pos += count; in ext2fs_file_write_inline_data()
360 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) { in ext2fs_file_write_inline_data()
363 rc = ext2fs_file_set_size2(file, file->pos); in ext2fs_file_write_inline_data()
374 retval = ext2fs_inline_data_expand(fs, file->ino); in ext2fs_file_write_inline_data()
380 * XXX: file->inode could be copied from the outside in ext2fs_file_write_inline_data()
384 retval = ext2fs_read_inode(fs, file->ino, &file->inode); in ext2fs_file_write_inline_data()
391 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf, in ext2fs_file_write() argument
401 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_write()
402 fs = file->fs; in ext2fs_file_write()
404 if (!(file->flags & EXT2_FILE_WRITE)) in ext2fs_file_write()
408 if (file->inode.i_flags & EXT4_INLINE_DATA_FL) { in ext2fs_file_write()
409 retval = ext2fs_file_write_inline_data(file, buf, nbytes, in ext2fs_file_write()
418 retval = sync_buffer_position(file); in ext2fs_file_write()
422 start = file->pos % fs->blocksize; in ext2fs_file_write()
431 retval = load_buffer(file, (c == fs->blocksize)); in ext2fs_file_write()
435 file->flags |= EXT2_FILE_BUF_DIRTY; in ext2fs_file_write()
436 memcpy(file->buf+start, ptr, c); in ext2fs_file_write()
442 if (!file->physblock) { in ext2fs_file_write()
443 bmap_flags = (file->ino ? BMAP_ALLOC : 0); in ext2fs_file_write()
450 ext2fs_sha512((const unsigned char*)file->buf, in ext2fs_file_write()
459 file->physblock = old_block->physblock; in ext2fs_file_write()
465 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in ext2fs_file_write()
468 file->blockno, 0, in ext2fs_file_write()
469 &file->physblock); in ext2fs_file_write()
477 new_block->physblock = file->physblock; in ext2fs_file_write()
483 ext2fs_iblk_add_blocks(fs, &file->inode, 1); in ext2fs_file_write()
484 ext2fs_write_inode(fs, file->ino, &file->inode); in ext2fs_file_write()
488 file->pos += c; in ext2fs_file_write()
496 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) { in ext2fs_file_write()
499 rc = ext2fs_file_set_size2(file, file->pos); in ext2fs_file_write()
509 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset, in ext2fs_file_llseek() argument
512 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_llseek()
515 file->pos = offset; in ext2fs_file_llseek()
517 file->pos += offset; in ext2fs_file_llseek()
519 file->pos = EXT2_I_SIZE(&file->inode) + offset; in ext2fs_file_llseek()
524 *ret_pos = file->pos; in ext2fs_file_llseek()
529 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset, in ext2fs_file_lseek() argument
536 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset); in ext2fs_file_lseek()
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) in ext2fs_file_get_lsize() argument
548 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_lsize()
550 *ret_size = EXT2_I_SIZE(&file->inode); in ext2fs_file_get_lsize()
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) in ext2fs_file_get_size() argument
561 if (ext2fs_file_get_lsize(file, &size)) in ext2fs_file_get_size()
569 static errcode_t ext2fs_file_zero_past_offset(ext2_file_t file, in ext2fs_file_zero_past_offset() argument
572 ext2_filsys fs = file->fs; in ext2fs_file_zero_past_offset()
582 retval = sync_buffer_position(file); in ext2fs_file_zero_past_offset()
587 retval = ext2fs_bmap2(fs, file->ino, NULL, NULL, 0, in ext2fs_file_zero_past_offset()
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) in ext2fs_file_set_size2() argument
625 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_set_size2()
627 if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode, in ext2fs_file_set_size2()
628 (size - 1) / file->fs->blocksize)) in ext2fs_file_set_size2()
630 truncate_block = ((size + file->fs->blocksize - 1) >> in ext2fs_file_set_size2()
631 EXT2_BLOCK_SIZE_BITS(file->fs->super)); in ext2fs_file_set_size2()
632 old_size = EXT2_I_SIZE(&file->inode); in ext2fs_file_set_size2()
633 old_truncate = ((old_size + file->fs->blocksize - 1) >> in ext2fs_file_set_size2()
634 EXT2_BLOCK_SIZE_BITS(file->fs->super)); in ext2fs_file_set_size2()
636 retval = ext2fs_inode_size_set(file->fs, &file->inode, size); in ext2fs_file_set_size2()
640 if (file->ino) { in ext2fs_file_set_size2()
641 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode); in ext2fs_file_set_size2()
646 retval = ext2fs_file_zero_past_offset(file, size); in ext2fs_file_set_size2()
653 return ext2fs_punch(file->fs, file->ino, &file->inode, 0, in ext2fs_file_set_size2()
657 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size) in ext2fs_file_set_size() argument
659 return ext2fs_file_set_size2(file, size); in ext2fs_file_set_size()