:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / fs / minix / block.c
1 /*
2  * COPYRIGHT:        See COPYING in the top level directory
3  * PROJECT:          ReactOS kernel
4  * FILE:             services/fs/minix/minix.c
5  * PURPOSE:          Minix FSD
6  * PROGRAMMER:       David Welch (welch@mcmail.com)
7  * UPDATE HISTORY: 
8  */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ddk/ntddk.h>
13
14 //#define NDEBUG
15 #include <debug.h>
16
17 #include "minix.h"
18
19 /* FUNCTIONS ****************************************************************/
20
21 static unsigned int MinixGetBlock(PDEVICE_OBJECT DeviceObject,
22                                   PMINIX_DEVICE_EXTENSION DeviceExt,
23                                   struct minix_inode* inode, 
24                                   ULONG FileOffset)
25 {
26    int block;
27    PVOID BaseAddress;
28    ULONG blk;
29       
30    blk = FileOffset / BLOCKSIZE;
31    
32    DPRINT("MinixGetBlock(inode %x, blk %d)\n",inode,blk);
33    
34    /*
35     * The first few blocks are available in the inode
36     */
37    if (blk < 7)
38      {
39         block = inode->i_zone[blk];
40         return(block);
41      }
42    blk = blk - 7;
43    
44    /*
45     * Retrieve a single-indirect block
46     */
47    if (blk < 512)
48      {
49         block = inode->i_zone[7];
50         
51         BaseAddress = ExAllocatePool(NonPagedPool, 512);
52         
53         MinixReadSector(DeviceObject,
54                         block, 
55                         BaseAddress);
56         
57         block = ((PUSHORT)(BaseAddress))[blk];
58
59         ExFreePool(BaseAddress);
60         
61         return(block);
62      }
63    
64    /*
65     * Get a double indirect block
66     */
67    blk = blk - 512;
68    block = inode->i_zone[8];
69    
70    BaseAddress = ExAllocatePool(NonPagedPool, 512);
71    
72    MinixReadSector(DeviceObject,
73                    block,
74                    BaseAddress);
75    
76    block = ((PUSHORT)BaseAddress)[(blk>>9)&511];
77
78    ExFreePool(BaseAddress);
79    
80    
81    BaseAddress = ExAllocatePool(NonPagedPool, 512);
82       
83    MinixReadSector(DeviceObject,
84                    block,
85                    BaseAddress);
86    
87    block = ((PUSHORT)BaseAddress)[blk&512];
88
89    ExFreePool(BaseAddress);
90    
91    return(block);
92 }
93
94 NTSTATUS MinixReadBlock(PDEVICE_OBJECT DeviceObject,
95                         PMINIX_DEVICE_EXTENSION DeviceExt,
96                         struct minix_inode* inode, 
97                         ULONG FileOffset,
98                         PULONG DiskOffset)
99 {
100    unsigned int block;
101    
102    DPRINT("MinixReadBlock()\n");
103    
104    block = MinixGetBlock(DeviceObject, DeviceExt,inode, FileOffset);
105    
106    (*DiskOffset) = block * BLOCKSIZE;
107
108    return(STATUS_SUCCESS);
109 }