1997-10-26 02:37:42 +04:00
|
|
|
/*
|
|
|
|
* fileio.c --- Simple file I/O routines
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
1997-10-26 02:37:42 +04:00
|
|
|
* Copyright (C) 1997 Theodore Ts'o.
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
2010-05-18 05:31:56 +04:00
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
1997-10-26 02:37:42 +04:00
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
2011-09-19 01:34:37 +04:00
|
|
|
#include "config.h"
|
1997-10-26 02:37:42 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
#include "ext2_fs.h"
|
1997-10-26 02:37:42 +04:00
|
|
|
#include "ext2fs.h"
|
2013-12-16 08:26:25 +04:00
|
|
|
#include "ext2fsP.h"
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
struct ext2_file {
|
|
|
|
errcode_t magic;
|
|
|
|
ext2_filsys fs;
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
ext2_ino_t ino;
|
1997-10-26 02:37:42 +04:00
|
|
|
struct ext2_inode inode;
|
|
|
|
int flags;
|
2003-01-23 02:25:39 +03:00
|
|
|
__u64 pos;
|
2009-06-02 00:15:40 +04:00
|
|
|
blk64_t blockno;
|
|
|
|
blk64_t physblock;
|
1997-10-26 02:37:42 +04:00
|
|
|
char *buf;
|
|
|
|
};
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
#define BMAP_BUFFER (file->buf + fs->blocksize)
|
|
|
|
|
2003-08-21 08:40:26 +04:00
|
|
|
errcode_t ext2fs_file_open2(ext2_filsys fs, ext2_ino_t ino,
|
|
|
|
struct ext2_inode *inode,
|
|
|
|
int flags, ext2_file_t *ret)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
ext2_file_t file;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't let caller create or open a file for writing if the
|
|
|
|
* filesystem is read-only.
|
|
|
|
*/
|
|
|
|
if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
|
|
|
|
!(fs->flags & EXT2_FLAG_RW))
|
|
|
|
return EXT2_ET_RO_FILSYS;
|
|
|
|
|
2003-08-01 17:41:07 +04:00
|
|
|
retval = ext2fs_get_mem(sizeof(struct ext2_file), &file);
|
1997-10-26 06:41:24 +03:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
memset(file, 0, sizeof(struct ext2_file));
|
|
|
|
file->magic = EXT2_ET_MAGIC_EXT2_FILE;
|
|
|
|
file->fs = fs;
|
|
|
|
file->ino = ino;
|
|
|
|
file->flags = flags & EXT2_FILE_MASK;
|
|
|
|
|
2003-08-21 08:40:26 +04:00
|
|
|
if (inode) {
|
|
|
|
memcpy(&file->inode, inode, sizeof(struct ext2_inode));
|
|
|
|
} else {
|
|
|
|
retval = ext2fs_read_inode(fs, ino, &file->inode);
|
|
|
|
if (retval)
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2007-11-10 03:01:06 +03:00
|
|
|
retval = ext2fs_get_array(3, fs->blocksize, &file->buf);
|
1997-10-26 06:41:24 +03:00
|
|
|
if (retval)
|
1997-10-26 02:37:42 +04:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*ret = file;
|
|
|
|
return 0;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
fail:
|
|
|
|
if (file->buf)
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&file->buf);
|
|
|
|
ext2fs_free_mem(&file);
|
1997-10-26 02:37:42 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-08-21 08:40:26 +04:00
|
|
|
errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
|
|
|
|
int flags, ext2_file_t *ret)
|
|
|
|
{
|
|
|
|
return ext2fs_file_open2(fs, ino, NULL, flags, ret);
|
|
|
|
}
|
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
/*
|
|
|
|
* This function returns the filesystem handle of a file from the structure
|
|
|
|
*/
|
|
|
|
ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
|
|
|
|
{
|
|
|
|
if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
|
|
|
|
return 0;
|
|
|
|
return file->fs;
|
|
|
|
}
|
|
|
|
|
2010-07-10 17:32:46 +04:00
|
|
|
/*
|
|
|
|
* This function returns the pointer to the inode of a file from the structure
|
|
|
|
*/
|
|
|
|
struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file)
|
|
|
|
{
|
|
|
|
if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
|
|
|
|
return NULL;
|
|
|
|
return &file->inode;
|
|
|
|
}
|
|
|
|
|
2012-02-17 18:56:33 +04:00
|
|
|
/* This function returns the inode number from the structure */
|
|
|
|
ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file)
|
|
|
|
{
|
|
|
|
if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
|
|
|
|
return 0;
|
|
|
|
return file->ino;
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
/*
|
|
|
|
* This function flushes the dirty block buffer out to disk if
|
|
|
|
* necessary.
|
|
|
|
*/
|
2002-02-20 09:06:25 +03:00
|
|
|
errcode_t ext2fs_file_flush(ext2_file_t file)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
errcode_t retval;
|
2001-05-05 20:42:24 +04:00
|
|
|
ext2_filsys fs;
|
2014-12-03 06:57:14 +03:00
|
|
|
int ret_flags;
|
|
|
|
blk64_t dontcare;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
2001-05-05 20:42:24 +04:00
|
|
|
fs = file->fs;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
if (!(file->flags & EXT2_FILE_BUF_VALID) ||
|
|
|
|
!(file->flags & EXT2_FILE_BUF_DIRTY))
|
|
|
|
return 0;
|
|
|
|
|
2014-12-03 06:57:14 +03:00
|
|
|
/* Is this an uninit block? */
|
|
|
|
if (file->physblock && file->inode.i_flags & EXT4_EXTENTS_FL) {
|
|
|
|
retval = ext2fs_bmap2(fs, file->ino, &file->inode, BMAP_BUFFER,
|
|
|
|
0, file->blockno, &ret_flags, &dontcare);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
if (ret_flags & BMAP_RET_UNINIT) {
|
|
|
|
retval = ext2fs_bmap2(fs, file->ino, &file->inode,
|
|
|
|
BMAP_BUFFER, BMAP_SET,
|
|
|
|
file->blockno, 0,
|
|
|
|
&file->physblock);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
/*
|
|
|
|
* OK, the physical block hasn't been allocated yet.
|
|
|
|
* Allocate it.
|
|
|
|
*/
|
|
|
|
if (!file->physblock) {
|
2009-06-02 00:15:40 +04:00
|
|
|
retval = ext2fs_bmap2(fs, file->ino, &file->inode,
|
2003-08-21 08:40:26 +04:00
|
|
|
BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0,
|
2009-06-02 00:15:40 +04:00
|
|
|
file->blockno, 0, &file->physblock);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:50:04 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
file->flags &= ~EXT2_FILE_BUF_DIRTY;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
/*
|
|
|
|
* This function synchronizes the file's block buffer and the current
|
|
|
|
* file position, possibly invalidating block buffer if necessary
|
|
|
|
*/
|
|
|
|
static errcode_t sync_buffer_position(ext2_file_t file)
|
|
|
|
{
|
2013-10-07 17:51:48 +04:00
|
|
|
blk64_t b;
|
2001-05-05 20:42:24 +04:00
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
b = file->pos / file->fs->blocksize;
|
|
|
|
if (b != file->blockno) {
|
|
|
|
retval = ext2fs_file_flush(file);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
file->flags &= ~EXT2_FILE_BUF_VALID;
|
|
|
|
}
|
|
|
|
file->blockno = b;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function loads the file's block buffer with valid data from
|
|
|
|
* the disk as necessary.
|
|
|
|
*
|
|
|
|
* If dontfill is true, then skip initializing the buffer since we're
|
|
|
|
* going to be replacing its entire contents anyway. If set, then the
|
|
|
|
* function basically only sets file->physblock and EXT2_FILE_BUF_VALID
|
|
|
|
*/
|
|
|
|
#define DONTFILL 1
|
|
|
|
static errcode_t load_buffer(ext2_file_t file, int dontfill)
|
|
|
|
{
|
|
|
|
ext2_filsys fs = file->fs;
|
|
|
|
errcode_t retval;
|
2014-12-03 06:57:14 +03:00
|
|
|
int ret_flags;
|
2001-05-05 20:42:24 +04:00
|
|
|
|
|
|
|
if (!(file->flags & EXT2_FILE_BUF_VALID)) {
|
2009-06-02 00:15:40 +04:00
|
|
|
retval = ext2fs_bmap2(fs, file->ino, &file->inode,
|
2014-12-03 06:57:14 +03:00
|
|
|
BMAP_BUFFER, 0, file->blockno, &ret_flags,
|
2001-05-05 20:42:24 +04:00
|
|
|
&file->physblock);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
if (!dontfill) {
|
2014-12-03 06:57:14 +03:00
|
|
|
if (file->physblock &&
|
|
|
|
!(ret_flags & BMAP_RET_UNINIT)) {
|
2013-12-12 21:50:04 +04:00
|
|
|
retval = io_channel_read_blk64(fs->io,
|
|
|
|
file->physblock,
|
|
|
|
1, file->buf);
|
2001-05-05 20:42:24 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
} else
|
|
|
|
memset(file->buf, 0, fs->blocksize);
|
|
|
|
}
|
|
|
|
file->flags |= EXT2_FILE_BUF_VALID;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
errcode_t ext2fs_file_close(ext2_file_t file)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
|
|
|
|
retval = ext2fs_file_flush(file);
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
if (file->buf)
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&file->buf);
|
|
|
|
ext2fs_free_mem(&file);
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-03 09:35:51 +04:00
|
|
|
static errcode_t
|
|
|
|
ext2fs_file_read_inline_data(ext2_file_t file, void *buf,
|
|
|
|
unsigned int wanted, unsigned int *got)
|
|
|
|
{
|
|
|
|
ext2_filsys fs;
|
|
|
|
errcode_t retval;
|
|
|
|
unsigned int count = 0;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
fs = file->fs;
|
|
|
|
retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
|
|
|
|
file->buf, &size);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (file->pos >= size)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
count = size - file->pos;
|
|
|
|
if (count > wanted)
|
|
|
|
count = wanted;
|
|
|
|
memcpy(buf, file->buf + file->pos, count);
|
|
|
|
file->pos += count;
|
|
|
|
buf += count;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (got)
|
|
|
|
*got = count;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
|
1997-11-03 22:16:55 +03:00
|
|
|
unsigned int wanted, unsigned int *got)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
ext2_filsys fs;
|
2001-05-05 20:42:24 +04:00
|
|
|
errcode_t retval = 0;
|
2003-01-23 02:25:39 +03:00
|
|
|
unsigned int start, c, count = 0;
|
|
|
|
__u64 left;
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
char *ptr = (char *) buf;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
fs = file->fs;
|
|
|
|
|
2014-03-03 09:35:51 +04:00
|
|
|
/* If an inode has inline data, things get complicated. */
|
|
|
|
if (file->inode.i_flags & EXT4_INLINE_DATA_FL)
|
|
|
|
return ext2fs_file_read_inline_data(file, buf, wanted, got);
|
|
|
|
|
2003-01-23 02:25:39 +03:00
|
|
|
while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) {
|
2001-05-05 20:42:24 +04:00
|
|
|
retval = sync_buffer_position(file);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2001-05-05 20:42:24 +04:00
|
|
|
retval = load_buffer(file, 0);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2001-05-05 20:42:24 +04:00
|
|
|
|
|
|
|
start = file->pos % fs->blocksize;
|
|
|
|
c = fs->blocksize - start;
|
|
|
|
if (c > wanted)
|
|
|
|
c = wanted;
|
2003-01-23 02:25:39 +03:00
|
|
|
left = EXT2_I_SIZE(&file->inode) - file->pos ;
|
2001-05-05 20:42:24 +04:00
|
|
|
if (c > left)
|
|
|
|
c = left;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
memcpy(ptr, file->buf+start, c);
|
|
|
|
file->pos += c;
|
|
|
|
ptr += c;
|
|
|
|
count += c;
|
|
|
|
wanted -= c;
|
1997-10-26 02:37:42 +04:00
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
fail:
|
1997-10-26 02:37:42 +04:00
|
|
|
if (got)
|
|
|
|
*got = count;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-03 09:35:51 +04:00
|
|
|
static errcode_t
|
|
|
|
ext2fs_file_write_inline_data(ext2_file_t file, const void *buf,
|
|
|
|
unsigned int nbytes, unsigned int *written)
|
|
|
|
{
|
|
|
|
ext2_filsys fs;
|
|
|
|
errcode_t retval;
|
|
|
|
unsigned int count = 0;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
fs = file->fs;
|
|
|
|
retval = ext2fs_inline_data_get(fs, file->ino, &file->inode,
|
|
|
|
file->buf, &size);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (file->pos < size) {
|
|
|
|
count = nbytes - file->pos;
|
|
|
|
memcpy(file->buf + file->pos, buf, count);
|
|
|
|
|
|
|
|
retval = ext2fs_inline_data_set(fs, file->ino, &file->inode,
|
|
|
|
file->buf, count);
|
|
|
|
if (retval == EXT2_ET_INLINE_DATA_NO_SPACE)
|
|
|
|
goto expand;
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
file->pos += count;
|
|
|
|
|
|
|
|
/* Update inode size */
|
|
|
|
if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
|
|
|
|
errcode_t rc;
|
|
|
|
|
|
|
|
rc = ext2fs_file_set_size2(file, file->pos);
|
|
|
|
if (retval == 0)
|
|
|
|
retval = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (written)
|
|
|
|
*written = count;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
expand:
|
|
|
|
retval = ext2fs_inline_data_expand(fs, file->ino);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
/*
|
|
|
|
* reload inode and return no space error
|
|
|
|
*
|
|
|
|
* XXX: file->inode could be copied from the outside
|
|
|
|
* in ext2fs_file_open2(). We have no way to modify
|
|
|
|
* the outside inode.
|
|
|
|
*/
|
|
|
|
retval = ext2fs_read_inode(fs, file->ino, &file->inode);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
return EXT2_ET_INLINE_DATA_NO_SPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-20 09:06:25 +03:00
|
|
|
errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
|
1997-11-03 22:16:55 +03:00
|
|
|
unsigned int nbytes, unsigned int *written)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
ext2_filsys fs;
|
2001-05-05 20:42:24 +04:00
|
|
|
errcode_t retval = 0;
|
|
|
|
unsigned int start, c, count = 0;
|
2002-03-08 07:52:56 +03:00
|
|
|
const char *ptr = (const char *) buf;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
fs = file->fs;
|
|
|
|
|
|
|
|
if (!(file->flags & EXT2_FILE_WRITE))
|
1997-10-31 09:07:47 +03:00
|
|
|
return EXT2_ET_FILE_RO;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
2014-03-03 09:35:51 +04:00
|
|
|
/* If an inode has inline data, things get complicated. */
|
|
|
|
if (file->inode.i_flags & EXT4_INLINE_DATA_FL) {
|
|
|
|
retval = ext2fs_file_write_inline_data(file, buf, nbytes,
|
|
|
|
written);
|
|
|
|
if (retval != EXT2_ET_INLINE_DATA_NO_SPACE)
|
|
|
|
return retval;
|
|
|
|
/* fall through to read data from the block */
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
while (nbytes > 0) {
|
|
|
|
retval = sync_buffer_position(file);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
start = file->pos % fs->blocksize;
|
|
|
|
c = fs->blocksize - start;
|
|
|
|
if (c > nbytes)
|
|
|
|
c = nbytes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only need to do a read-modify-update cycle if
|
|
|
|
* we're doing a partial write.
|
|
|
|
*/
|
|
|
|
retval = load_buffer(file, (c == fs->blocksize));
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2001-05-05 20:42:24 +04:00
|
|
|
|
2013-12-12 22:19:46 +04:00
|
|
|
/*
|
|
|
|
* OK, the physical block hasn't been allocated yet.
|
|
|
|
* Allocate it.
|
|
|
|
*/
|
|
|
|
if (!file->physblock) {
|
|
|
|
retval = ext2fs_bmap2(fs, file->ino, &file->inode,
|
|
|
|
BMAP_BUFFER,
|
|
|
|
file->ino ? BMAP_ALLOC : 0,
|
|
|
|
file->blockno, 0,
|
|
|
|
&file->physblock);
|
|
|
|
if (retval)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
file->flags |= EXT2_FILE_BUF_DIRTY;
|
|
|
|
memcpy(file->buf+start, ptr, c);
|
|
|
|
file->pos += c;
|
|
|
|
ptr += c;
|
|
|
|
count += c;
|
|
|
|
nbytes -= c;
|
1997-10-26 02:37:42 +04:00
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
fail:
|
2013-10-08 07:30:10 +04:00
|
|
|
/* Update inode size */
|
|
|
|
if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) {
|
|
|
|
errcode_t rc;
|
|
|
|
|
|
|
|
rc = ext2fs_file_set_size2(file, file->pos);
|
|
|
|
if (retval == 0)
|
|
|
|
retval = rc;
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
if (written)
|
|
|
|
*written = count;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-01-23 02:25:39 +03:00
|
|
|
errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset,
|
|
|
|
int whence, __u64 *ret_pos)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
|
|
|
|
if (whence == EXT2_SEEK_SET)
|
|
|
|
file->pos = offset;
|
|
|
|
else if (whence == EXT2_SEEK_CUR)
|
|
|
|
file->pos += offset;
|
|
|
|
else if (whence == EXT2_SEEK_END)
|
2003-01-23 02:25:39 +03:00
|
|
|
file->pos = EXT2_I_SIZE(&file->inode) + offset;
|
1997-10-26 02:37:42 +04:00
|
|
|
else
|
1997-10-31 09:07:47 +03:00
|
|
|
return EXT2_ET_INVALID_ARGUMENT;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
if (ret_pos)
|
|
|
|
*ret_pos = file->pos;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-23 02:25:39 +03:00
|
|
|
errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
|
|
|
|
int whence, ext2_off_t *ret_pos)
|
|
|
|
{
|
|
|
|
__u64 loffset, ret_loffset;
|
|
|
|
errcode_t retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2003-01-23 02:25:39 +03:00
|
|
|
loffset = offset;
|
|
|
|
retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset);
|
2003-03-10 22:32:09 +03:00
|
|
|
if (ret_pos)
|
|
|
|
*ret_pos = (ext2_off_t) ret_loffset;
|
2003-01-23 02:25:39 +03:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
/*
|
|
|
|
* This function returns the size of the file, according to the inode
|
|
|
|
*/
|
2003-01-23 02:25:39 +03:00
|
|
|
errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size)
|
1997-11-03 22:16:55 +03:00
|
|
|
{
|
|
|
|
if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
|
2003-01-23 02:25:39 +03:00
|
|
|
return EXT2_ET_MAGIC_EXT2_FILE;
|
|
|
|
*ret_size = EXT2_I_SIZE(&file->inode);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function returns the size of the file, according to the inode
|
|
|
|
*/
|
|
|
|
ext2_off_t ext2fs_file_get_size(ext2_file_t file)
|
|
|
|
{
|
|
|
|
__u64 size;
|
|
|
|
|
|
|
|
if (ext2fs_file_get_lsize(file, &size))
|
|
|
|
return 0;
|
|
|
|
if ((size >> 32) != 0)
|
1997-11-03 22:16:55 +03:00
|
|
|
return 0;
|
2003-01-23 02:25:39 +03:00
|
|
|
return size;
|
1997-11-03 22:16:55 +03:00
|
|
|
}
|
1997-10-26 02:37:42 +04:00
|
|
|
|
2013-12-12 22:31:35 +04:00
|
|
|
/* Zero the parts of the last block that are past EOF. */
|
2013-12-17 03:56:36 +04:00
|
|
|
static errcode_t ext2fs_file_zero_past_offset(ext2_file_t file,
|
|
|
|
ext2_off64_t offset)
|
2013-12-12 22:31:35 +04:00
|
|
|
{
|
|
|
|
ext2_filsys fs = file->fs;
|
|
|
|
char *b = NULL;
|
|
|
|
ext2_off64_t off = offset % fs->blocksize;
|
|
|
|
blk64_t blk;
|
|
|
|
int ret_flags;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
if (off == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
retval = sync_buffer_position(file);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Is there an initialized block at the end? */
|
|
|
|
retval = ext2fs_bmap2(fs, file->ino, NULL, NULL, 0,
|
|
|
|
offset / fs->blocksize, &ret_flags, &blk);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
if ((blk == 0) || (ret_flags & BMAP_RET_UNINIT))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Zero to the end of the block */
|
|
|
|
retval = ext2fs_get_mem(fs->blocksize, &b);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* Read/zero/write block */
|
|
|
|
retval = io_channel_read_blk64(fs->io, blk, 1, b);
|
|
|
|
if (retval)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
memset(b + off, 0, fs->blocksize - off);
|
|
|
|
|
|
|
|
retval = io_channel_write_blk64(fs->io, blk, 1, b);
|
|
|
|
if (retval)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out:
|
|
|
|
ext2fs_free_mem(&b);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
/*
|
|
|
|
* This function sets the size of the file, truncating it if necessary
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
1997-11-03 22:16:55 +03:00
|
|
|
*/
|
2010-07-22 17:51:23 +04:00
|
|
|
errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size)
|
1997-11-03 22:16:55 +03:00
|
|
|
{
|
2010-07-22 17:51:23 +04:00
|
|
|
ext2_off64_t old_size;
|
1997-11-03 22:16:55 +03:00
|
|
|
errcode_t retval;
|
2010-07-22 17:51:23 +04:00
|
|
|
blk64_t old_truncate, truncate_block;
|
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2013-12-12 21:40:31 +04:00
|
|
|
if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode,
|
|
|
|
(size - 1) / file->fs->blocksize))
|
|
|
|
return EXT2_ET_FILE_TOO_BIG;
|
2010-07-22 17:51:23 +04:00
|
|
|
truncate_block = ((size + file->fs->blocksize - 1) >>
|
2013-10-08 19:51:23 +04:00
|
|
|
EXT2_BLOCK_SIZE_BITS(file->fs->super));
|
2011-07-09 03:54:57 +04:00
|
|
|
old_size = EXT2_I_SIZE(&file->inode);
|
2010-07-22 17:51:23 +04:00
|
|
|
old_truncate = ((old_size + file->fs->blocksize - 1) >>
|
2013-10-08 19:51:23 +04:00
|
|
|
EXT2_BLOCK_SIZE_BITS(file->fs->super));
|
2010-07-22 17:51:23 +04:00
|
|
|
|
2014-07-26 22:34:56 +04:00
|
|
|
retval = ext2fs_inode_size_set(file->fs, &file->inode, size);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
2013-10-01 05:27:28 +04:00
|
|
|
|
2003-08-21 08:40:26 +04:00
|
|
|
if (file->ino) {
|
|
|
|
retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
1997-11-03 22:16:55 +03:00
|
|
|
|
2013-12-12 22:31:35 +04:00
|
|
|
retval = ext2fs_file_zero_past_offset(file, size);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
2012-02-16 00:11:46 +04:00
|
|
|
if (truncate_block >= old_truncate)
|
2010-07-22 17:51:23 +04:00
|
|
|
return 0;
|
1997-11-03 22:16:55 +03:00
|
|
|
|
2010-07-22 17:51:23 +04:00
|
|
|
return ext2fs_punch(file->fs, file->ino, &file->inode, 0,
|
|
|
|
truncate_block, ~0ULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
|
|
|
|
{
|
|
|
|
return ext2fs_file_set_size2(file, size);
|
1997-11-03 22:16:55 +03:00
|
|
|
}
|