Subversion Repositories Kolibri OS

Rev

Rev 4265 | Rev 5565 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4265 Rev 5201
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2
;;                                                              ;;
3
;; Contains common resource allocation + freeing code.          ;;
3
;; Contains common resource allocation + freeing code.          ;;
4
;;                                                              ;;
4
;;                                                              ;;
5
;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;;
5
;; Copyright (C) KolibriOS team 2013-2014. All rights reserved. ;;
6
;; Distributed under the terms of the new BSD license.          ;;
6
;; Distributed under terms of the GNU General Public License    ;;
7
;;                                                              ;;
7
;;                                                              ;;
8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
9
 
-
 
10
$Revision: 4891 $
-
 
11
 
9
 
12
 
10
;---------------------------------------------------------------------
13
;---------------------------------------------------------------------
11
; Frees a resource (block/inode).
14
; Frees a resource (block/inode).
12
; Input:        eax = resource ID.
15
; Input:        eax = resource ID.
13
;               edi = function pointer of ext2_bg_*_bitmap form, to
16
;               edi = function pointer of ext2_bg_*_bitmap form, to
14
;                     get bitmap of resource.
17
;                     get bitmap of resource.
15
;               ecx = 0, block; 1, inode.
18
;               ecx = 0, block; 1, inode.
16
;               ebp = pointer to EXTFS.
19
;               ebp = pointer to EXTFS.
17
; Output:       Block marked as free in block group.
20
; Output:       Block marked as free in block group.
18
;               eax = error code.
21
;               eax = error code.
19
;---------------------------------------------------------------------
22
;---------------------------------------------------------------------
20
ext2_resource_free:
23
ext2_resource_free:
21
        push    ebx edx esi
24
        push    ebx edx esi
22
 
25
 
23
        ; Get block group.
26
        ; Get block group.
24
        sub     eax, [ebp + EXTFS.superblock + EXT2_SB_STRUC.first_data_block]
27
        sub     eax, [ebp + EXTFS.superblock + EXT2_SB_STRUC.first_data_block]
25
        xor     edx, edx
28
        xor     edx, edx
26
        div     [ebp + EXTFS.superblock + EXT2_SB_STRUC.blocks_per_group]
29
        div     [ebp + EXTFS.superblock + EXT2_SB_STRUC.blocks_per_group]
27
        push    eax edx
30
        push    eax edx
28
 
31
 
29
        call    edi
32
        call    edi
30
        test    eax, eax
33
        test    eax, eax
31
        jz      .fail
34
        jz      .fail
32
        mov     esi, eax
35
        mov     esi, eax
33
 
36
 
34
        ; Read the bitmap.
37
        ; Read the bitmap.
35
        mov     eax, ebx
38
        mov     eax, ebx
36
        mov     edx, eax
39
        mov     edx, eax
37
        mov     ebx, [ebp + EXTFS.ext2_save_block]
40
        mov     ebx, [ebp + EXTFS.ext2_save_block]
38
        call    ext2_block_read
41
        call    ext2_block_read
39
        test    eax, eax
42
        test    eax, eax
40
        jnz     .fail
43
        jnz     .fail
41
 
44
 
42
        pop     eax
45
        pop     eax
43
        ; Mark bit free.
46
        ; Mark bit free.
44
        call    bitmap_clear_bit
47
        call    bitmap_clear_bit
45
        test    eax, eax
48
        test    eax, eax
46
        jz      @F
49
        jz      @F
47
 
50
 
48
        ; No need to save anything.
51
        ; No need to save anything.
49
        xor     eax, eax
52
        xor     eax, eax
50
 
53
 
51
        add     esp, 4
54
        add     esp, 4
52
        jmp     .return
55
        jmp     .return
53
 
56
 
54
    @@:
57
    @@:
55
        mov     eax, edx
58
        mov     eax, edx
56
        mov     ebx, [ebp + EXTFS.ext2_save_block]
59
        mov     ebx, [ebp + EXTFS.ext2_save_block]
57
        call    ext2_block_write
60
        call    ext2_block_write
58
        test    eax, eax
61
        test    eax, eax
59
        jnz     .fail
62
        jnz     .fail
60
 
63
 
61
        ; Read the descriptor.
64
        ; Read the descriptor.
62
        mov     eax, [esp]
65
        mov     eax, [esp]
63
        call    ext2_bg_read_desc
66
        call    ext2_bg_read_desc
64
        test    eax, eax
67
        test    eax, eax
65
        jz      .fail_bg_desc_read
68
        jz      .fail_bg_desc_read
66
 
69
 
67
        lea     eax, [eax + EXT2_BLOCK_GROUP_DESC.free_blocks_count]
70
        lea     eax, [eax + EXT2_BLOCK_GROUP_DESC.free_blocks_count]
68
        shl     ecx, 1
71
        shl     ecx, 1
69
        add     eax, ecx
72
        add     eax, ecx
70
        inc     word[eax]
73
        inc     word[eax]
71
 
74
 
72
        lea     eax, [ebp + EXTFS.superblock + EXT2_SB_STRUC.free_block_count]
75
        lea     eax, [ebp + EXTFS.superblock + EXT2_SB_STRUC.free_block_count]
73
        shl     ecx, 1
76
        shl     ecx, 1
74
        add     eax, ecx
77
        add     eax, ecx
75
        inc     dword[eax]
78
        inc     dword[eax]
76
 
79
 
77
        pop     eax
80
        pop     eax
78
        call    ext2_bg_write_desc
81
        call    ext2_bg_write_desc
79
 
82
 
80
    .return:
83
    .return:
81
        pop     esi edx ebx
84
        pop     esi edx ebx
82
        ret
85
        ret
83
 
86
 
84
    .fail:
87
    .fail:
85
        add     esp, 4
88
        add     esp, 4
86
    .fail_bg_desc_read:
89
    .fail_bg_desc_read:
87
        add     esp, 4
90
        add     esp, 4
88
        xor     eax, eax
91
        xor     eax, eax
89
        not     eax
92
        not     eax
90
        jmp     .return
93
        jmp     .return
91
 
94
 
92
;---------------------------------------------------------------------
95
;---------------------------------------------------------------------
93
; Allocates a resource.
96
; Allocates a resource.
94
; Input:        eax = inode ID for "preference".
97
; Input:        eax = inode ID for "preference".
95
;               ebp = pointer to EXTFS.
98
;               ebp = pointer to EXTFS.
96
;               [esp + 4], func pointer to ext2_bg_*_bitmap
99
;               [esp + 4], func pointer to ext2_bg_*_bitmap
97
;               [esp + 8], pointer to free_*_count in SB.
100
;               [esp + 8], pointer to free_*_count in SB.
98
;               [esp + 12], *_per_group
101
;               [esp + 12], *_per_group
99
;               [esp + 16], offset to free_*_count in bg descriptor.
102
;               [esp + 16], offset to free_*_count in bg descriptor.
100
;               [esp + 20], *_count
103
;               [esp + 20], *_count
101
; Output:       Resource marked as set in block group.
104
; Output:       Resource marked as set in block group.
102
;               eax = error code.
105
;               eax = error code.
103
;               ebx = resource ID.
106
;               ebx = resource ID.
104
;---------------------------------------------------------------------
107
;---------------------------------------------------------------------
105
ext2_resource_alloc:
108
ext2_resource_alloc:
106
        ; Block allocation is a pretty serious area, since bad allocation
109
        ; Block allocation is a pretty serious area, since bad allocation
107
        ; can lead to fragmentation. Thus, the best way to allocate that
110
        ; can lead to fragmentation. Thus, the best way to allocate that
108
        ; comes to mind is to allocate around an inode as much as possible.
111
        ; comes to mind is to allocate around an inode as much as possible.
109
        ; On the other hand, this isn't about a single inode/file/directory,
112
        ; On the other hand, this isn't about a single inode/file/directory,
110
        ; and focusing just around the preferred inode would lead to
113
        ; and focusing just around the preferred inode would lead to
111
        ; congestion. Thus, after much thought, the chosen allocation algorithm
114
        ; congestion. Thus, after much thought, the chosen allocation algorithm
112
        ; is to search forward, then backward.
115
        ; is to search forward, then backward.
113
        push    ecx edx esi edi
116
        push    ecx edx esi edi
114
 
117
 
115
        cmp     dword[esp + 16 + 8], 0
118
        cmp     dword[esp + 16 + 8], 0
116
        jnz     @F
119
        jnz     @F
117
 
120
 
118
        ; No free blocks.
121
        ; No free blocks.
119
        xor     eax, eax
122
        xor     eax, eax
120
        not     eax
123
        not     eax
121
        pop     edi esi edx ecx
124
        pop     edi esi edx ecx
122
        ret     20
125
        ret     20
123
 
126
 
124
    @@:
127
    @@:
125
        ; Calculate which block group the preferred inode belongs to.
128
        ; Calculate which block group the preferred inode belongs to.
126
        dec     eax
129
        dec     eax
127
        xor     edx, edx
130
        xor     edx, edx
128
        
131
        
129
        ; EAX = block group.
132
        ; EAX = block group.
130
        div     [ebp + EXTFS.superblock + EXT2_SB_STRUC.inodes_per_group]
133
        div     [ebp + EXTFS.superblock + EXT2_SB_STRUC.inodes_per_group]
131
        push    eax
134
        push    eax
132
        push    eax
135
        push    eax
133
 
136
 
134
        mov     edi, .forward
137
        mov     edi, .forward
135
 
138
 
136
    .test_block_group:
139
    .test_block_group:
137
        call    dword[esp + 16 + 8 + 4]
140
        call    dword[esp + 16 + 8 + 4]
138
        test    eax, eax
141
        test    eax, eax
139
        jz      .fail
142
        jz      .fail
140
        mov     esi, eax
143
        mov     esi, eax
141
 
144
 
142
        mov     eax, ebx
145
        mov     eax, ebx
143
        mov     edx, eax
146
        mov     edx, eax
144
        mov     ebx, [ebp + EXTFS.ext2_save_block]
147
        mov     ebx, [ebp + EXTFS.ext2_save_block]
145
        call    ext2_block_read
148
        call    ext2_block_read
146
        test    eax, eax
149
        test    eax, eax
147
        jnz     .fail
150
        jnz     .fail
148
 
151
 
149
        mov     ecx, [esp + 16 + 8 + 12]
152
        mov     ecx, [esp + 16 + 8 + 12]
150
        call    ext2_find_free_bit
153
        call    ext2_find_free_bit
151
        cmp     eax, 0xFFFFFFFF
154
        cmp     eax, 0xFFFFFFFF
152
        jne     @F
155
        jne     @F
153
 
156
 
154
        mov     eax, edi
157
        mov     eax, edi
155
        jmp     eax
158
        jmp     eax
156
 
159
 
157
    @@:
160
    @@:
158
        mov     ecx, eax
161
        mov     ecx, eax
159
 
162
 
160
        mov     eax, edx
163
        mov     eax, edx
161
        mov     ebx, [ebp + EXTFS.ext2_save_block]
164
        mov     ebx, [ebp + EXTFS.ext2_save_block]
162
        call    ext2_block_write
165
        call    ext2_block_write
163
        test    eax, eax
166
        test    eax, eax
164
        jnz     .fail
167
        jnz     .fail
165
 
168
 
166
        ; ecx: the index of the matched entry.
169
        ; ecx: the index of the matched entry.
167
        ; [esp]: block group where we found.
170
        ; [esp]: block group where we found.
168
        ; [esp + 4]: starting block group.
171
        ; [esp + 4]: starting block group.
169
        ; esi: block group descriptor.
172
        ; esi: block group descriptor.
170
        mov     eax, [esp]                             ; Index of block group in which we found.
173
        mov     eax, [esp]                             ; Index of block group in which we found.
171
        mul     dword[esp + 16 + 8 + 12]
174
        mul     dword[esp + 16 + 8 + 12]
172
        add     eax, ecx
175
        add     eax, ecx
173
        mov     ebx, eax
176
        mov     ebx, eax
174
 
177
 
175
        mov     eax, [esp + 16 + 8 + 8]
178
        mov     eax, [esp + 16 + 8 + 8]
176
        dec     dword[eax]
179
        dec     dword[eax]
177
 
180
 
178
        mov     eax, esi
181
        mov     eax, esi
179
        add     eax, [esp + 16 + 8 + 16]
182
        add     eax, [esp + 16 + 8 + 16]
180
        dec     word[eax]
183
        dec     word[eax]
181
 
184
 
182
        pop     eax
185
        pop     eax
183
        call    ext2_bg_write_desc
186
        call    ext2_bg_write_desc
184
 
187
 
185
        add     esp, 4
188
        add     esp, 4
186
        jmp     .return
189
        jmp     .return
187
 
190
 
188
    ; Continue forward.
191
    ; Continue forward.
189
    .forward:        
192
    .forward:        
190
        inc     dword[esp]
193
        inc     dword[esp]
191
        mov     eax, [esp]
194
        mov     eax, [esp]
192
        mul     dword[esp + 16 + 8 + 12]
195
        mul     dword[esp + 16 + 8 + 12]
193
        cmp     eax, [esp + 16 + 8 + 20]
196
        cmp     eax, [esp + 16 + 8 + 20]
194
        jbe     @F
197
        jbe     @F
195
 
198
 
196
        ; We need to go backward.
199
        ; We need to go backward.
197
        mov     eax, [esp + 4]
200
        mov     eax, [esp + 4]
198
        mov     [esp], eax
201
        mov     [esp], eax
199
        mov     edi, .backward
202
        mov     edi, .backward
200
        jmp     .backward
203
        jmp     .backward
201
 
204
 
202
    @@:
205
    @@:
203
        mov     eax, [esp]
206
        mov     eax, [esp]
204
        jmp     .test_block_group
207
        jmp     .test_block_group
205
 
208
 
206
    ; Continue backward.
209
    ; Continue backward.
207
    .backward:
210
    .backward:
208
        cmp     dword[esp], 0
211
        cmp     dword[esp], 0
209
        je      .fail
212
        je      .fail
210
 
213
 
211
        dec     dword[esp]
214
        dec     dword[esp]
212
        mov     eax, [esp]
215
        mov     eax, [esp]
213
        jmp     .test_block_group
216
        jmp     .test_block_group
214
 
217
 
215
    .return:
218
    .return:
216
        pop     edi esi edx ecx
219
        pop     edi esi edx ecx
217
        ret     20
220
        ret     20
218
 
221
 
219
    .fail:
222
    .fail:
220
        add     esp, 8
223
        add     esp, 8
221
        xor     eax, eax
224
        xor     eax, eax
222
        not     eax
225
        not     eax
223
        jmp     .return
226
        jmp     .return