mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader.git] / modules / ps2fs / journal.c
blobdfe1ee1cfb54ba956680b6e8a0da88449dab9146
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
10 # $Id: journal.c 911 2005-03-14 21:02:17Z oopo $
11 # PFS metadata journal related routines
14 #include "pfs.h"
16 ///////////////////////////////////////////////////////////////////////////////
17 // Globals
19 pfs_journal_t journalBuf;
21 ///////////////////////////////////////////////////////////////////////////////
22 // Function defenitions
24 int journalCheckSum(void *header)
26 u32 *ptr=(u32 *)header;
27 u32 sum=0;
28 int i;
29 for(i=2; i < 256; i++)
30 sum+=ptr[i];
31 return sum & 0xFFFF;
34 void journalWrite(pfs_mount_t *pfsMount, pfs_cache_t *clink, u32 numBuffers)
36 u32 i=0;
37 u32 logSector=2;
39 for(i=0; i <numBuffers;i++)
41 if((clink[i].flags & CACHE_FLAG_DIRTY) && clink[i].pfsMount == pfsMount) {
42 if(clink[i].flags & (CACHE_FLAG_SEGD|CACHE_FLAG_SEGI))
43 clink[i].u.inode->checksum=inodeCheckSum(clink[i].u.inode);
44 journalBuf.log[journalBuf.num].sector = clink[i].sector << blockSize;
45 journalBuf.log[journalBuf.num].sub = clink[i].sub;
46 journalBuf.log[journalBuf.num].logSector = logSector;
47 journalBuf.num+=1;
49 logSector+=2;
52 if(pfsMount->blockDev->transfer(pfsMount->fd, clink->u.inode, 0,
53 (pfsMount->log.number << pfsMount->sector_scale) + 2, numBuffers*2,
54 IOCTL2_TMODE_WRITE)>=0)
55 journalFlush(pfsMount);
58 int journalReset(pfs_mount_t *pfsMount)
60 int rv;
62 memset(&journalBuf, 0, sizeof(pfs_journal_t));
63 journalBuf.magic=PFS_JOUNRNAL_MAGIC;
65 pfsMount->blockDev->flushCache(pfsMount->fd);
67 rv = pfsMount->blockDev->transfer(pfsMount->fd, &journalBuf, 0,
68 (pfsMount->log.number << pfsMount->sector_scale), 2, IOCTL2_TMODE_WRITE);
70 pfsMount->blockDev->flushCache(pfsMount->fd);
71 return rv;
74 int journalResetThis(block_device *blockDev, int fd, u32 sector)
76 memset(&journalBuf, 0, sizeof(pfs_journal_t));
77 journalBuf.magic=PFS_JOUNRNAL_MAGIC;
78 return blockDev->transfer(fd, &journalBuf, 0, sector, 2, 1);
81 int journalFlush(pfs_mount_t *pfsMount)
82 {// this write any thing that in are journal buffer :)
83 int rv;
85 pfsMount->blockDev->flushCache(pfsMount->fd);
87 journalBuf.checksum=journalCheckSum(&journalBuf);
89 rv=pfsMount->blockDev->transfer(pfsMount->fd, &journalBuf, 0,
90 (pfsMount->log.number << pfsMount->sector_scale), 2, IOCTL2_TMODE_WRITE);
92 pfsMount->blockDev->flushCache(pfsMount->fd);
93 return rv;
96 int journalResetore(pfs_mount_t *pfsMount)
98 int rv;
99 int result;
100 pfs_cache_t *clink;
101 u32 i;
104 // Read journal buffer from disk
105 rv = pfsMount->blockDev->transfer(pfsMount->fd, &journalBuf, 0,
106 (pfsMount->log.number << pfsMount->sector_scale), 2, IOCTL2_TMODE_READ);
108 if(rv || (journalBuf.magic != PFS_JOUNRNAL_MAGIC) ||
109 (journalBuf.checksum != (u16)journalCheckSum(&journalBuf)))
111 printf("ps2fs: Error: cannot read log/invalid log\n");
112 return journalReset(pfsMount);
115 if(journalBuf.num == 0)
117 dprintf("ps2fs: System cleanly un-mounted, no journal restore needed\n");
118 return journalReset(pfsMount);
121 clink = cacheAllocClean(&result);
122 if(!clink)
123 return result;
125 for(i = 0; i < journalBuf.num; i++)
127 printf("ps2fs: Log overwrite %d:%08lx\n", journalBuf.log[i].sub, journalBuf.log[i].sector);
129 // Read data in from log section on disk into cache buffer
130 rv = pfsMount->blockDev->transfer(pfsMount->fd, clink->u.data, 0,
131 (pfsMount->log.number << pfsMount->sector_scale) + journalBuf.log[i].logSector, 2,
132 IOCTL2_TMODE_READ);
134 // Write from cache buffer into destination location on disk
135 if(!rv)
136 pfsMount->blockDev->transfer(pfsMount->fd, clink->u.data, journalBuf.log[i].sub,
137 journalBuf.log[i].sector, 2, IOCTL2_TMODE_WRITE);
140 cacheAdd(clink);
141 return journalReset(pfsMount);