Skip to content

Commit

Permalink
Merge pull request #281 from rickgaiser/bd_defrag
Browse files Browse the repository at this point in the history
BD fragmentation support: variable number of fragments
  • Loading branch information
rickgaiser authored Jun 15, 2022
2 parents bc3a17f + 39c6788 commit 8d4dde5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
7 changes: 0 additions & 7 deletions common/include/usbhdfsd-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@

#include <tamtypes.h>

#define BD_MAX_FRAGMENTS 10

typedef struct bd_fragment {
u32 sector; /// start sector of fragmented bd/file
u32 count; /// number of sector in this fragment
} __attribute__((packed)) bd_fragment_t;

typedef struct bd_fraglist {
u32 count; /// number of fragments
bd_fragment_t list[BD_MAX_FRAGMENTS]; /// pointer to fragment list
} __attribute__((packed)) bd_fraglist_t;

// IOCTL function codes
/** Rename opened file. Data input to ioctl() -> new, full filename of file. */
#define USBMASS_IOCTL_RENAME 0x0000
Expand Down
23 changes: 10 additions & 13 deletions iop/fs/bdmfs_fatfs/src/fs_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,33 +438,30 @@ static int fs_getstat(iop_file_t *fd, const char *name, iox_stat_t *stat)

static int get_frag_list(FIL *file, void *rdata, unsigned int rdatalen)
{
bd_fraglist_t *l = (bd_fraglist_t*)rdata;
bd_fragment_t *f = (bd_fragment_t*)rdata;
int iMaxFragments = rdatalen / sizeof(bd_fragment_t);
int iFragCount = 0;

DWORD iClusterStart = file->obj.sclust;
DWORD iClusterCurrent = iClusterStart;

if (rdatalen < sizeof(bd_fraglist_t)) {
M_DEBUG("ERROR: rdatalen=%d\n", rdatalen);
return -1;
}

l->count = 0;
do {
DWORD iClusterNext = get_fat(&file->obj, iClusterCurrent);
if (iClusterNext != (iClusterCurrent + 1)) {
// Fragment or file end
M_DEBUG("fragment: %uc - %uc + 1\n", iClusterStart, iClusterCurrent + 1);
if (l->count < 10) {
l->list[l->count].sector = clst2sect(file->obj.fs, iClusterStart);
l->list[l->count].count = clst2sect(file->obj.fs, iClusterCurrent) - clst2sect(file->obj.fs, iClusterStart) + file->obj.fs->csize;
M_DEBUG(" - sectors: %us count %us\n", l->list[l->count].sector, l->list[l->count].count);
if (iFragCount < iMaxFragments) {
f[iFragCount].sector = clst2sect(file->obj.fs, iClusterStart);
f[iFragCount].count = clst2sect(file->obj.fs, iClusterCurrent) - clst2sect(file->obj.fs, iClusterStart) + file->obj.fs->csize;
M_DEBUG(" - sectors: %us count %us\n", f[iFragCount].sector, f[iFragCount].count);
}
l->count++;
iFragCount++;
iClusterStart = iClusterNext;
}
iClusterCurrent = iClusterNext;
} while(iClusterCurrent < file->obj.fs->n_fatent);

return l->count;
return iFragCount;
}

//---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion iop/fs/libbdm/include/bd_defrag.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <usbhdfsd-common.h>


int bd_defrag(struct block_device* bd, struct bd_fraglist* fl, u32 sector, void* buffer, u16 count);
int bd_defrag(struct block_device* bd, u32 fragcount, struct bd_fragment* fraglist, u32 sector, void* buffer, u16 count);



Expand Down
8 changes: 4 additions & 4 deletions iop/fs/libbdm/src/bd_defrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "module_debug.h"


int bd_defrag(struct block_device* bd, struct bd_fraglist* fl, u32 sector, void* buffer, u16 count)
int bd_defrag(struct block_device* bd, u32 fragcount, struct bd_fragment* fraglist, u32 sector, void* buffer, u16 count)
{
u32 sector_start = sector;
u16 count_left = count;
Expand All @@ -16,16 +16,16 @@ int bd_defrag(struct block_device* bd, struct bd_fraglist* fl, u32 sector, void*
int i;

// Locate fragment containing start sector
for (i=0; i<fl->count; i++) {
f = &fl->list[i];
for (i=0; i<fragcount; i++) {
f = &fraglist[i];
if (offset <= sector_start && (offset + f->count) > sector_start) {
// Fragment found
break;
}
offset += f->count;
}

if (i == fl->count) {
if (i == fragcount) {
M_PRINTF("%s: ERROR: fragment not found!\n", __FUNCTION__);
return -1;
}
Expand Down

0 comments on commit 8d4dde5

Please sign in to comment.