* Implement udf_io and udf_get_file_map.

* Add file map and cache and do reads through the file_cache for file ICBs.
* Move the private _Read() backend from the header to the implementation file.
* Read() is now only used for directory iteration.
* Turned off the verbose debug output.
* Minor cleanup.

This fixes the abuse of the block_cache, should allow executables on UDF and
won't panic on protected content.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35652 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz 2010-02-28 07:50:28 +00:00
parent 4f3f1911cd
commit af15ee68f1
5 changed files with 318 additions and 148 deletions

View File

@ -12,6 +12,8 @@
#include "Utils.h"
#include "Volume.h"
#include <file_cache.h>
status_t
DirectoryIterator::GetNextEntry(char *name, uint32 *length, ino_t *id)
@ -103,7 +105,9 @@ Icb::Icb(Volume *volume, long_address address)
fInitStatus(B_NO_INIT),
fId(to_vnode_id(address)),
fFileEntry(&fData),
fExtendedEntry(&fData)
fExtendedEntry(&fData),
fFileCache(NULL),
fFileMap(NULL)
{
TRACE(("Icb::Icb: volume = %p, address(block = %ld, partition = %d, "
"length = %ld)\n", volume, address.block(), address.partition(),
@ -130,11 +134,25 @@ Icb::Icb(Volume *volume, long_address address)
status = header->tag().init_check(address.block());
}
if (IsFile()) {
fFileCache = file_cache_create(fVolume->ID(), fId, Length());
fFileMap = file_map_create(fVolume->ID(), fId, Length());
}
fInitStatus = status;
TRACE(("Icb::Icb: status = 0x%lx, `%s'\n", status, strerror(status)));
}
Icb::~Icb()
{
if (fFileCache != NULL) {
file_cache_delete(fFileCache);
file_map_delete(fFileMap);
}
}
status_t
Icb::GetDirectoryIterator(DirectoryIterator **iterator)
{
@ -187,6 +205,9 @@ Icb::Read(off_t pos, void *buffer, size_t *length, uint32 *block)
return B_OK;
}
if (fFileCache != NULL)
return file_cache_read(fFileCache, NULL, pos, buffer, length);
switch (_IcbTag().descriptor_flags()) {
case ICB_DESCRIPTOR_TYPE_SHORT: {
TRACE(("Icb::Read: descriptor type -> short\n"));
@ -225,6 +246,199 @@ Icb::Read(off_t pos, void *buffer, size_t *length, uint32 *block)
}
/*! \brief Does the dirty work of reading using the given DescriptorList object
to access the allocation descriptors properly.
*/
template <class DescriptorList>
status_t
Icb::_Read(DescriptorList &list, off_t pos, void *_buffer, size_t *length, uint32 *block)
{
TRACE(("Icb::_Read(): list = %p, pos = %Ld, buffer = %p, length = %ld\n",
&list, pos, _buffer, (length ? *length : 0)));
uint64 bytesLeftInFile = uint64(pos) > Length() ? 0 : Length() - pos;
size_t bytesLeft = (*length >= bytesLeftInFile) ? bytesLeftInFile : *length;
size_t bytesRead = 0;
Volume *volume = GetVolume();
status_t status = B_OK;
uint8 *buffer = (uint8 *)_buffer;
bool isFirstBlock = true;
while (bytesLeft > 0) {
TRACE(("Icb::_Read(): pos: %Ld, bytesLeft: %ld\n", pos, bytesLeft));
long_address extent;
bool isEmpty = false;
status = list.FindExtent(pos, &extent, &isEmpty);
if (status != B_OK) {
TRACE_ERROR(("Icb::_Read: error finding extent for offset %Ld. "
"status = 0x%lx `%s'\n", pos, status, strerror(status)));
break;
}
TRACE(("Icb::_Read(): found extent for offset %Ld: (block: %ld, "
"partition: %d, length: %ld, type: %d)\n", pos, extent.block(),
extent.partition(), extent.length(), extent.type()));
switch (extent.type()) {
case EXTENT_TYPE_RECORDED:
isEmpty = false;
break;
case EXTENT_TYPE_ALLOCATED:
case EXTENT_TYPE_UNALLOCATED:
isEmpty = true;
break;
default:
TRACE_ERROR(("Icb::_Read(): Invalid extent type found: %d\n",
extent.type()));
status = B_ERROR;
break;
}
if (status != B_OK)
break;
// Note the unmapped first block of the total read in
// the block output parameter if provided
if (isFirstBlock) {
isFirstBlock = false;
if (block)
*block = extent.block();
}
off_t blockOffset
= pos - off_t((pos >> volume->BlockShift()) << volume->BlockShift());
size_t readLength = volume->BlockSize() - blockOffset;
if (bytesLeft < readLength)
readLength = bytesLeft;
if (extent.length() < readLength)
readLength = extent.length();
TRACE(("Icb::_Read: reading block. offset = %Ld, length: %ld\n",
blockOffset, readLength));
if (isEmpty) {
TRACE(("Icb::_Read: reading %ld empty bytes as zeros\n",
readLength));
memset(buffer, 0, readLength);
} else {
off_t diskBlock;
status = volume->MapBlock(extent, &diskBlock);
if (status != B_OK) {
TRACE_ERROR(("Icb::_Read: could not map extent\n"));
break;
}
TRACE(("Icb::_Read: %ld bytes from disk block %Ld using "
"block_cache_get_etc()\n", readLength, diskBlock));
uint8 *data = (uint8*)block_cache_get_etc(volume->BlockCache(),
diskBlock, 0, readLength);
if (data == NULL)
break;
memcpy(buffer, data + blockOffset, readLength);
block_cache_put(volume->BlockCache(), diskBlock);
}
bytesLeft -= readLength;
bytesRead += readLength;
pos += readLength;
buffer += readLength;
}
*length = bytesRead;
return status;
}
status_t
Icb::GetFileMap(off_t offset, size_t size, file_io_vec *vecs, size_t *count)
{
switch (_IcbTag().descriptor_flags()) {
case ICB_DESCRIPTOR_TYPE_SHORT:
{
AllocationDescriptorList<ShortDescriptorAccessor> list(this,
ShortDescriptorAccessor(0));
return _GetFileMap(list, offset, size, vecs, count);
}
case ICB_DESCRIPTOR_TYPE_LONG:
{
AllocationDescriptorList<LongDescriptorAccessor> list(this);
return _GetFileMap(list, offset, size, vecs, count);
}
case ICB_DESCRIPTOR_TYPE_EXTENDED:
case ICB_DESCRIPTOR_TYPE_EMBEDDED:
default:
{
// TODO: implement?
return B_UNSUPPORTED;
}
}
}
template<class DescriptorList>
status_t
Icb::_GetFileMap(DescriptorList &list, off_t offset, size_t size,
struct file_io_vec *vecs, size_t *count)
{
size_t index = 0;
size_t max = *count;
while (true) {
long_address extent;
bool isEmpty = false;
status_t status = list.FindExtent(offset, &extent, &isEmpty);
if (status != B_OK)
return status;
switch (extent.type()) {
case EXTENT_TYPE_RECORDED:
isEmpty = false;
break;
case EXTENT_TYPE_ALLOCATED:
case EXTENT_TYPE_UNALLOCATED:
isEmpty = true;
break;
default:
return B_ERROR;
}
if (isEmpty)
vecs[index].offset = -1;
else {
off_t diskBlock;
fVolume->MapBlock(extent, &diskBlock);
vecs[index].offset = diskBlock << fVolume->BlockShift();
}
off_t length = extent.length();
vecs[index].length = length;
offset += length;
size -= length;
index++;
if (index >= max || size <= vecs[index - 1].length
|| offset >= (off_t)Length()) {
*count = index;
return index >= max ? B_BUFFER_OVERFLOW : B_OK;
}
}
// can never get here
return B_ERROR;
}
status_t
Icb::Find(const char *filename, ino_t *id)
{

View File

@ -79,6 +79,8 @@ friend class Icb;
class Icb {
public:
Icb(Volume *volume, long_address address);
~Icb();
status_t InitCheck();
ino_t Id() { return fId; }
@ -105,6 +107,12 @@ public:
status_t Read(off_t pos, void *buffer, size_t *length,
uint32 *block = NULL);
void * FileCache() { return fFileCache; }
void * FileMap() { return fFileMap; }
status_t GetFileMap(off_t offset, size_t size,
struct file_io_vec *vecs, size_t *count);
// for directories only
status_t GetDirectoryIterator(DirectoryIterator **iterator);
status_t Find(const char *filename, ino_t *id);
@ -122,11 +130,14 @@ private:
file_icb_entry *_FileEntry() { return (file_icb_entry *)fData.Block(); }
extended_file_icb_entry &_ExtendedEntry() { return *(extended_file_icb_entry *)fData.Block(); }
template <class DescriptorList>
template<class DescriptorList>
status_t _GetFileMap(DescriptorList &list, off_t offset,
size_t size, struct file_io_vec *vecs,
size_t *count);
template<class DescriptorList>
status_t _Read(DescriptorList &list, off_t pos,
void *buffer, size_t *length, uint32 *block);
private:
Volume *fVolume;
CachedBlock fData;
status_t fInitStatus;
@ -134,118 +145,11 @@ private:
SinglyLinkedList<DirectoryIterator> fIteratorList;
FileEntry<file_icb_entry> fFileEntry;
FileEntry<extended_file_icb_entry> fExtendedEntry;
void * fFileCache;
void * fFileMap;
};
/*! \brief Does the dirty work of reading using the given DescriptorList object
to access the allocation descriptors properly.
*/
template <class DescriptorList>
status_t
Icb::_Read(DescriptorList &list, off_t pos, void *_buffer, size_t *length, uint32 *block)
{
TRACE(("Icb::_Read(): list = %p, pos = %Ld, buffer = %p, length = %ld\n",
&list, pos, _buffer, (length ? *length : 0)));
uint64 bytesLeftInFile = uint64(pos) > Length() ? 0 : Length() - pos;
size_t bytesLeft = (*length >= bytesLeftInFile) ? bytesLeftInFile : *length;
size_t bytesRead = 0;
Volume *volume = GetVolume();
status_t status = B_OK;
uint8 *buffer = (uint8 *)_buffer;
bool isFirstBlock = true;
while (bytesLeft > 0) {
TRACE(("Icb::_Read(): pos: %Ld, bytesLeft: %ld\n", pos, bytesLeft));
long_address extent;
bool isEmpty = false;
status = list.FindExtent(pos, &extent, &isEmpty);
if (status != B_OK) {
TRACE_ERROR(("Icb::_Read: error finding extent for offset %Ld. "
"status = 0x%lx `%s'\n", pos, status, strerror(status)));
break;
}
TRACE(("Icb::_Read(): found extent for offset %Ld: (block: %ld, "
"partition: %d, length: %ld, type: %d)\n", pos, extent.block(),
extent.partition(), extent.length(), extent.type()));
switch (extent.type()) {
case EXTENT_TYPE_RECORDED:
isEmpty = false;
break;
case EXTENT_TYPE_ALLOCATED:
case EXTENT_TYPE_UNALLOCATED:
isEmpty = true;
break;
default:
TRACE_ERROR(("Icb::_Read(): Invalid extent type found: %d\n",
extent.type()));
status = B_ERROR;
break;
}
if (status != B_OK)
break;
// Note the unmapped first block of the total read in
// the block output parameter if provided
if (isFirstBlock) {
isFirstBlock = false;
if (block)
*block = extent.block();
}
off_t blockOffset
= pos - off_t((pos >> volume->BlockShift()) << volume->BlockShift());
size_t readLength = volume->BlockSize() - blockOffset;
if (bytesLeft < readLength)
readLength = bytesLeft;
if (extent.length() < readLength)
readLength = extent.length();
TRACE(("Icb::_Read: reading block. offset = %Ld, length: %ld\n",
blockOffset, readLength));
if (isEmpty) {
TRACE(("Icb::_Read: reading %ld empty bytes as zeros\n",
readLength));
memset(buffer, 0, readLength);
} else {
off_t diskBlock;
status = volume->MapBlock(extent, &diskBlock);
if (status != B_OK) {
TRACE_ERROR(("Icb::_Read: could not map extent\n"));
break;
}
TRACE(("Icb::_Read: %ld bytes from disk block %Ld using "
"block_cache_get_etc()\n", readLength, diskBlock));
uint8 *data = (uint8*)block_cache_get_etc(volume->BlockCache(),
diskBlock, 0, readLength);
if (data == NULL)
break;
memcpy(buffer, data + blockOffset, readLength);
block_cache_put(volume->BlockCache(), diskBlock);
}
bytesLeft -= readLength;
bytesRead += readLength;
pos += readLength;
buffer += readLength;
}
*length = bytesRead;
return status;
}
template <class Descriptor>
FileEntry<Descriptor>::FileEntry(CachedBlock *descriptorBlock)
: fDescriptorBlock(descriptorBlock)

View File

@ -228,7 +228,7 @@ private:
#define DBG(x) ;
#endif // ifdef DEBUG else
#define TRACE(x) dprintf x
#define TRACE(x) /*dprintf x*/
#define TRACE_ERROR(x) dprintf x
// These macros turn on or off extensive and generally unnecessary

View File

@ -16,6 +16,8 @@
#include <KernelExport.h>
#include <util/kernel_cpp.h>
#include <io_requests.h>
#include "Icb.h"
#include "Recognition.h"
#include "Utils.h"
@ -37,6 +39,28 @@ extern fs_volume_ops gUDFVolumeOps;
extern fs_vnode_ops gUDFVnodeOps;
// #pragma mark - io callbacks
static status_t
iterative_io_get_vecs_hook(void *cookie, io_request *request, off_t offset,
size_t size, struct file_io_vec *vecs, size_t *count)
{
Icb *icb = (Icb *)cookie;
return file_map_translate(icb->FileMap(), offset, size, vecs, count,
icb->GetVolume()->BlockSize());
}
static status_t
iterative_io_finished_hook(void *cookie, io_request *request, status_t status,
bool partialTransfer, size_t bytesTransferred)
{
// nothing to do
return B_OK;
}
// #pragma mark - fs_volume_ops fuctions
@ -298,6 +322,34 @@ udf_read(fs_volume *volume, fs_vnode *vnode, void *cookie, off_t pos,
}
static status_t
udf_io(fs_volume *volume, fs_vnode *vnode, void *cookie, io_request *request)
{
if (io_request_is_write(request)) {
notify_io_request(request, B_READ_ONLY_DEVICE);
return B_READ_ONLY_DEVICE;
}
Icb *icb = (Icb *)vnode->private_node;
if (icb->FileCache() == NULL) {
notify_io_request(request, B_BAD_VALUE);
return B_BAD_VALUE;
}
return do_iterative_fd_io(((Volume *)volume->private_volume)->Device(),
request, iterative_io_get_vecs_hook, iterative_io_finished_hook, icb);
}
static status_t
udf_get_file_map(fs_volume *_volume, fs_vnode *vnode, off_t offset, size_t size,
struct file_io_vec *vecs, size_t *count)
{
Icb *icb = (Icb *)vnode->private_node;
return icb->GetFileMap(offset, size, vecs, count);
}
static status_t
udf_open_dir(fs_volume *volume, fs_vnode *vnode, void **cookie)
{
@ -568,11 +620,11 @@ fs_vnode_ops gUDFVnodeOps = {
NULL, // write_pages
/* asynchronous I/O */
NULL, // io()
&udf_io,
NULL, // cancel_io()
/* cache file access */
NULL, // &udf_get_file_map,
&udf_get_file_map,
/* common operations */
NULL, // ioctl