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
16 ///////////////////////////////////////////////////////////////////////////////
19 pfs_journal_t journalBuf
;
21 ///////////////////////////////////////////////////////////////////////////////
22 // Function defenitions
24 int journalCheckSum(void *header
)
26 u32
*ptr
=(u32
*)header
;
29 for(i
=2; i
< 256; i
++)
34 void journalWrite(pfs_mount_t
*pfsMount
, pfs_cache_t
*clink
, u32 numBuffers
)
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
;
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
)
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
);
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 :)
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
);
96 int journalResetore(pfs_mount_t
*pfsMount
)
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
);
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,
134 // Write from cache buffer into destination location on disk
136 pfsMount
->blockDev
->transfer(pfsMount
->fd
, clink
->u
.data
, journalBuf
.log
[i
].sub
,
137 journalBuf
.log
[i
].sector
, 2, IOCTL2_TMODE_WRITE
);
141 return journalReset(pfsMount
);