Rev 3500 | Rev 3503 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3500 | Rev 3501 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file fat_ops.c |
34 | * @file fat_ops.c |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
35 | * @brief Implementation of VFS operations for the FAT file system server. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat.h" |
38 | #include "fat.h" |
39 | #include "../../vfs/vfs.h" |
39 | #include "../../vfs/vfs.h" |
40 | #include <libfs.h> |
40 | #include <libfs.h> |
41 | #include <ipc/ipc.h> |
41 | #include <ipc/ipc.h> |
42 | #include <ipc/services.h> |
42 | #include <ipc/services.h> |
43 | #include <ipc/devmap.h> |
43 | #include <ipc/devmap.h> |
44 | #include <async.h> |
44 | #include <async.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <string.h> |
46 | #include <string.h> |
47 | #include <byteorder.h> |
47 | #include <byteorder.h> |
48 | #include <libadt/hash_table.h> |
48 | #include <libadt/hash_table.h> |
49 | #include <libadt/list.h> |
49 | #include <libadt/list.h> |
50 | #include <assert.h> |
50 | #include <assert.h> |
51 | #include <futex.h> |
51 | #include <futex.h> |
52 | #include <sys/mman.h> |
52 | #include <sys/mman.h> |
53 | #include <align.h> |
53 | #include <align.h> |
54 | 54 | ||
55 | #define BS_BLOCK 0 |
55 | #define BS_BLOCK 0 |
56 | #define BS_SIZE 512 |
56 | #define BS_SIZE 512 |
57 | 57 | ||
58 | /** Futex protecting the list of cached free FAT nodes. */ |
58 | /** Futex protecting the list of cached free FAT nodes. */ |
59 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
59 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
60 | 60 | ||
61 | /** List of cached free FAT nodes. */ |
61 | /** List of cached free FAT nodes. */ |
62 | static LIST_INITIALIZE(ffn_head); |
62 | static LIST_INITIALIZE(ffn_head); |
63 | 63 | ||
64 | #define FAT_NAME_LEN 8 |
64 | #define FAT_NAME_LEN 8 |
65 | #define FAT_EXT_LEN 3 |
65 | #define FAT_EXT_LEN 3 |
66 | 66 | ||
67 | #define FAT_PAD ' ' |
67 | #define FAT_PAD ' ' |
68 | 68 | ||
69 | #define FAT_DENTRY_UNUSED 0x00 |
69 | #define FAT_DENTRY_UNUSED 0x00 |
70 | #define FAT_DENTRY_E5_ESC 0x05 |
70 | #define FAT_DENTRY_E5_ESC 0x05 |
71 | #define FAT_DENTRY_DOT 0x2e |
71 | #define FAT_DENTRY_DOT 0x2e |
72 | #define FAT_DENTRY_ERASED 0xe5 |
72 | #define FAT_DENTRY_ERASED 0xe5 |
73 | 73 | ||
74 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
74 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
75 | 75 | ||
76 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
76 | static void dentry_name_canonify(fat_dentry_t *d, char *buf) |
77 | { |
77 | { |
78 | int i; |
78 | int i; |
79 | 79 | ||
80 | for (i = 0; i < FAT_NAME_LEN; i++) { |
80 | for (i = 0; i < FAT_NAME_LEN; i++) { |
81 | if (d->name[i] == FAT_PAD) |
81 | if (d->name[i] == FAT_PAD) |
82 | break; |
82 | break; |
83 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
83 | if (d->name[i] == FAT_DENTRY_E5_ESC) |
84 | *buf++ = 0xe5; |
84 | *buf++ = 0xe5; |
85 | else |
85 | else |
86 | *buf++ = d->name[i]; |
86 | *buf++ = d->name[i]; |
87 | } |
87 | } |
88 | if (d->ext[0] != FAT_PAD) |
88 | if (d->ext[0] != FAT_PAD) |
89 | *buf++ = '.'; |
89 | *buf++ = '.'; |
90 | for (i = 0; i < FAT_EXT_LEN; i++) { |
90 | for (i = 0; i < FAT_EXT_LEN; i++) { |
91 | if (d->ext[i] == FAT_PAD) { |
91 | if (d->ext[i] == FAT_PAD) { |
92 | *buf = '\0'; |
92 | *buf = '\0'; |
93 | return; |
93 | return; |
94 | } |
94 | } |
95 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
95 | if (d->ext[i] == FAT_DENTRY_E5_ESC) |
96 | *buf++ = 0xe5; |
96 | *buf++ = 0xe5; |
97 | else |
97 | else |
98 | *buf++ = d->ext[i]; |
98 | *buf++ = d->ext[i]; |
99 | } |
99 | } |
100 | *buf = '\0'; |
100 | *buf = '\0'; |
101 | } |
101 | } |
102 | 102 | ||
103 | static int dev_phone = -1; /* FIXME */ |
103 | static int dev_phone = -1; /* FIXME */ |
104 | static void *dev_buffer = NULL; /* FIXME */ |
104 | static void *dev_buffer = NULL; /* FIXME */ |
105 | 105 | ||
106 | /* TODO move somewhere else */ |
106 | /* TODO move somewhere else */ |
107 | typedef struct { |
107 | typedef struct { |
108 | void *data; |
108 | void *data; |
109 | size_t size; |
109 | size_t size; |
110 | bool dirty; |
110 | bool dirty; |
111 | } block_t; |
111 | } block_t; |
112 | 112 | ||
113 | static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs) |
113 | static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs) |
114 | { |
114 | { |
115 | /* FIXME */ |
115 | /* FIXME */ |
116 | block_t *b; |
116 | block_t *b; |
117 | off_t bufpos = 0; |
117 | off_t bufpos = 0; |
118 | size_t buflen = 0; |
118 | size_t buflen = 0; |
119 | off_t pos = offset * bs; |
119 | off_t pos = offset * bs; |
120 | 120 | ||
121 | assert(dev_phone != -1); |
121 | assert(dev_phone != -1); |
122 | assert(dev_buffer); |
122 | assert(dev_buffer); |
123 | 123 | ||
124 | b = malloc(sizeof(block_t)); |
124 | b = malloc(sizeof(block_t)); |
125 | if (!b) |
125 | if (!b) |
126 | return NULL; |
126 | return NULL; |
127 | 127 | ||
128 | b->data = malloc(bs); |
128 | b->data = malloc(bs); |
129 | if (!b->data) { |
129 | if (!b->data) { |
130 | free(b); |
130 | free(b); |
131 | return NULL; |
131 | return NULL; |
132 | } |
132 | } |
133 | b->size = bs; |
133 | b->size = bs; |
134 | 134 | ||
135 | if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos, |
135 | if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos, |
136 | b->data, bs, bs)) { |
136 | b->data, bs, bs)) { |
137 | free(b->data); |
137 | free(b->data); |
138 | free(b); |
138 | free(b); |
139 | return NULL; |
139 | return NULL; |
140 | } |
140 | } |
141 | 141 | ||
142 | return b; |
142 | return b; |
143 | } |
143 | } |
144 | 144 | ||
145 | static void block_put(block_t *block) |
145 | static void block_put(block_t *block) |
146 | { |
146 | { |
147 | /* FIXME */ |
147 | /* FIXME */ |
148 | free(block->data); |
148 | free(block->data); |
149 | free(block); |
149 | free(block); |
150 | } |
150 | } |
151 | 151 | ||
152 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
152 | #define FAT_BS(b) ((fat_bs_t *)((b)->data)) |
153 | 153 | ||
154 | #define FAT_CLST_RES0 0x0000 |
154 | #define FAT_CLST_RES0 0x0000 |
155 | #define FAT_CLST_RES1 0x0001 |
155 | #define FAT_CLST_RES1 0x0001 |
156 | #define FAT_CLST_FIRST 0x0002 |
156 | #define FAT_CLST_FIRST 0x0002 |
157 | #define FAT_CLST_BAD 0xfff7 |
157 | #define FAT_CLST_BAD 0xfff7 |
158 | #define FAT_CLST_LAST1 0xfff8 |
158 | #define FAT_CLST_LAST1 0xfff8 |
159 | #define FAT_CLST_LAST8 0xffff |
159 | #define FAT_CLST_LAST8 0xffff |
160 | 160 | ||
161 | /* internally used to mark root directory's parent */ |
161 | /* internally used to mark root directory's parent */ |
162 | #define FAT_CLST_ROOTPAR FAT_CLST_RES0 |
162 | #define FAT_CLST_ROOTPAR FAT_CLST_RES0 |
163 | /* internally used to mark root directory */ |
163 | /* internally used to mark root directory */ |
164 | #define FAT_CLST_ROOT FAT_CLST_RES1 |
164 | #define FAT_CLST_ROOT FAT_CLST_RES1 |
165 | 165 | ||
166 | #define fat_block_get(np, off) \ |
166 | #define fat_block_get(np, off) \ |
167 | _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off)) |
167 | _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off)) |
168 | 168 | ||
169 | static block_t * |
169 | static block_t * |
170 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset) |
170 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset) |
171 | { |
171 | { |
172 | block_t *bb; |
172 | block_t *bb; |
173 | block_t *b; |
173 | block_t *b; |
174 | unsigned bps; |
174 | unsigned bps; |
175 | unsigned spc; |
175 | unsigned spc; |
176 | unsigned rscnt; /* block address of the first FAT */ |
176 | unsigned rscnt; /* block address of the first FAT */ |
177 | unsigned fatcnt; |
177 | unsigned fatcnt; |
178 | unsigned rde; |
178 | unsigned rde; |
179 | unsigned rds; /* root directory size */ |
179 | unsigned rds; /* root directory size */ |
180 | unsigned sf; |
180 | unsigned sf; |
181 | unsigned ssa; /* size of the system area */ |
181 | unsigned ssa; /* size of the system area */ |
182 | unsigned clusters; |
182 | unsigned clusters; |
183 | fat_cluster_t clst = firstc; |
183 | fat_cluster_t clst = firstc; |
184 | unsigned i; |
184 | unsigned i; |
185 | 185 | ||
186 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
186 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
187 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
187 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
188 | spc = FAT_BS(bb)->spc; |
188 | spc = FAT_BS(bb)->spc; |
189 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
189 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
190 | fatcnt = FAT_BS(bb)->fatcnt; |
190 | fatcnt = FAT_BS(bb)->fatcnt; |
191 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
191 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
192 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
192 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
193 | block_put(bb); |
193 | block_put(bb); |
194 | 194 | ||
195 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
195 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
196 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
196 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
197 | ssa = rscnt + fatcnt * sf + rds; |
197 | ssa = rscnt + fatcnt * sf + rds; |
198 | 198 | ||
199 | if (firstc == FAT_CLST_ROOT) { |
199 | if (firstc == FAT_CLST_ROOT) { |
200 | /* root directory special case */ |
200 | /* root directory special case */ |
201 | assert(offset < rds); |
201 | assert(offset < rds); |
202 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
202 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
203 | return b; |
203 | return b; |
204 | } |
204 | } |
205 | 205 | ||
206 | clusters = offset / spc; |
206 | clusters = offset / spc; |
207 | for (i = 0; i < clusters; i++) { |
207 | for (i = 0; i < clusters; i++) { |
208 | unsigned fsec; /* sector offset relative to FAT1 */ |
208 | unsigned fsec; /* sector offset relative to FAT1 */ |
209 | unsigned fidx; /* FAT1 entry index */ |
209 | unsigned fidx; /* FAT1 entry index */ |
210 | 210 | ||
211 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
211 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
212 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
212 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
213 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
213 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
214 | /* read FAT1 */ |
214 | /* read FAT1 */ |
215 | b = block_get(dev_handle, rscnt + fsec, bps); |
215 | b = block_get(dev_handle, rscnt + fsec, bps); |
216 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
216 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
217 | assert(clst != FAT_CLST_BAD); |
217 | assert(clst != FAT_CLST_BAD); |
218 | assert(clst < FAT_CLST_LAST1); |
218 | assert(clst < FAT_CLST_LAST1); |
219 | block_put(b); |
219 | block_put(b); |
220 | } |
220 | } |
221 | 221 | ||
222 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
222 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
223 | offset % spc, bps); |
223 | offset % spc, bps); |
224 | 224 | ||
225 | return b; |
225 | return b; |
226 | } |
226 | } |
227 | 227 | ||
228 | /** Return number of blocks allocated to a file. |
228 | /** Return number of blocks allocated to a file. |
229 | * |
229 | * |
230 | * @param dev_handle Device handle of the device with the file. |
230 | * @param dev_handle Device handle of the device with the file. |
231 | * @param firstc First cluster of the file. |
231 | * @param firstc First cluster of the file. |
232 | * |
232 | * |
233 | * @return Number of blocks allocated to the file. |
233 | * @return Number of blocks allocated to the file. |
234 | */ |
234 | */ |
235 | static uint16_t |
235 | static uint16_t |
236 | _fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc) |
236 | _fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc) |
237 | { |
237 | { |
238 | block_t *bb; |
238 | block_t *bb; |
239 | block_t *b; |
239 | block_t *b; |
240 | unsigned bps; |
240 | unsigned bps; |
241 | unsigned spc; |
241 | unsigned spc; |
242 | unsigned rscnt; /* block address of the first FAT */ |
242 | unsigned rscnt; /* block address of the first FAT */ |
243 | unsigned clusters = 0; |
243 | unsigned clusters = 0; |
244 | fat_cluster_t clst = firstc; |
244 | fat_cluster_t clst = firstc; |
245 | 245 | ||
246 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
246 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
247 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
247 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
248 | spc = FAT_BS(bb)->spc; |
248 | spc = FAT_BS(bb)->spc; |
249 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
249 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
250 | block_put(bb); |
250 | block_put(bb); |
251 | 251 | ||
252 | if (firstc == FAT_CLST_RES0) { |
252 | if (firstc == FAT_CLST_RES0) { |
253 | /* No space allocated to the file. */ |
253 | /* No space allocated to the file. */ |
254 | return 0; |
254 | return 0; |
255 | } |
255 | } |
256 | 256 | ||
257 | while (clst < FAT_CLST_LAST1) { |
257 | while (clst < FAT_CLST_LAST1) { |
258 | unsigned fsec; /* sector offset relative to FAT1 */ |
258 | unsigned fsec; /* sector offset relative to FAT1 */ |
259 | unsigned fidx; /* FAT1 entry index */ |
259 | unsigned fidx; /* FAT1 entry index */ |
260 | 260 | ||
261 | assert(clst >= FAT_CLST_FIRST); |
261 | assert(clst >= FAT_CLST_FIRST); |
262 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
262 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
263 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
263 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
264 | /* read FAT1 */ |
264 | /* read FAT1 */ |
265 | b = block_get(dev_handle, rscnt + fsec, bps); |
265 | b = block_get(dev_handle, rscnt + fsec, bps); |
266 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
266 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
267 | assert(clst != FAT_CLST_BAD); |
267 | assert(clst != FAT_CLST_BAD); |
268 | block_put(b); |
268 | block_put(b); |
269 | clusters++; |
269 | clusters++; |
270 | } |
270 | } |
271 | 271 | ||
272 | return clusters * spc; |
272 | return clusters * spc; |
273 | } |
273 | } |
274 | 274 | ||
275 | static void fat_node_initialize(fat_node_t *node) |
275 | static void fat_node_initialize(fat_node_t *node) |
276 | { |
276 | { |
277 | futex_initialize(&node->lock, 1); |
277 | futex_initialize(&node->lock, 1); |
278 | node->idx = NULL; |
278 | node->idx = NULL; |
279 | node->type = 0; |
279 | node->type = 0; |
280 | link_initialize(&node->ffn_link); |
280 | link_initialize(&node->ffn_link); |
281 | node->size = 0; |
281 | node->size = 0; |
282 | node->lnkcnt = 0; |
282 | node->lnkcnt = 0; |
283 | node->refcnt = 0; |
283 | node->refcnt = 0; |
284 | node->dirty = false; |
284 | node->dirty = false; |
285 | } |
285 | } |
286 | 286 | ||
287 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
287 | static uint16_t fat_bps_get(dev_handle_t dev_handle) |
288 | { |
288 | { |
289 | block_t *bb; |
289 | block_t *bb; |
290 | uint16_t bps; |
290 | uint16_t bps; |
291 | 291 | ||
292 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
292 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
293 | assert(bb != NULL); |
293 | assert(bb != NULL); |
294 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
294 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
295 | block_put(bb); |
295 | block_put(bb); |
296 | 296 | ||
297 | return bps; |
297 | return bps; |
298 | } |
298 | } |
299 | 299 | ||
300 | typedef enum { |
300 | typedef enum { |
301 | FAT_DENTRY_SKIP, |
301 | FAT_DENTRY_SKIP, |
302 | FAT_DENTRY_LAST, |
302 | FAT_DENTRY_LAST, |
303 | FAT_DENTRY_VALID |
303 | FAT_DENTRY_VALID |
304 | } fat_dentry_clsf_t; |
304 | } fat_dentry_clsf_t; |
305 | 305 | ||
306 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
306 | static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d) |
307 | { |
307 | { |
308 | if (d->attr & FAT_ATTR_VOLLABEL) { |
308 | if (d->attr & FAT_ATTR_VOLLABEL) { |
309 | /* volume label entry */ |
309 | /* volume label entry */ |
310 | return FAT_DENTRY_SKIP; |
310 | return FAT_DENTRY_SKIP; |
311 | } |
311 | } |
312 | if (d->name[0] == FAT_DENTRY_ERASED) { |
312 | if (d->name[0] == FAT_DENTRY_ERASED) { |
313 | /* not-currently-used entry */ |
313 | /* not-currently-used entry */ |
314 | return FAT_DENTRY_SKIP; |
314 | return FAT_DENTRY_SKIP; |
315 | } |
315 | } |
316 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
316 | if (d->name[0] == FAT_DENTRY_UNUSED) { |
317 | /* never used entry */ |
317 | /* never used entry */ |
318 | return FAT_DENTRY_LAST; |
318 | return FAT_DENTRY_LAST; |
319 | } |
319 | } |
320 | if (d->name[0] == FAT_DENTRY_DOT) { |
320 | if (d->name[0] == FAT_DENTRY_DOT) { |
321 | /* |
321 | /* |
322 | * Most likely '.' or '..'. |
322 | * Most likely '.' or '..'. |
323 | * It cannot occur in a regular file name. |
323 | * It cannot occur in a regular file name. |
324 | */ |
324 | */ |
325 | return FAT_DENTRY_SKIP; |
325 | return FAT_DENTRY_SKIP; |
326 | } |
326 | } |
327 | return FAT_DENTRY_VALID; |
327 | return FAT_DENTRY_VALID; |
328 | } |
328 | } |
329 | 329 | ||
330 | static void fat_node_sync(fat_node_t *node) |
330 | static void fat_node_sync(fat_node_t *node) |
331 | { |
331 | { |
332 | /* TODO */ |
332 | /* TODO */ |
333 | } |
333 | } |
334 | 334 | ||
335 | /** Internal version of fat_node_get(). |
335 | /** Internal version of fat_node_get(). |
336 | * |
336 | * |
337 | * @param idxp Locked index structure. |
337 | * @param idxp Locked index structure. |
338 | */ |
338 | */ |
339 | static void *fat_node_get_core(fat_idx_t *idxp) |
339 | static void *fat_node_get_core(fat_idx_t *idxp) |
340 | { |
340 | { |
341 | block_t *b; |
341 | block_t *b; |
342 | fat_dentry_t *d; |
342 | fat_dentry_t *d; |
343 | fat_node_t *nodep = NULL; |
343 | fat_node_t *nodep = NULL; |
344 | unsigned bps; |
344 | unsigned bps; |
345 | unsigned dps; |
345 | unsigned dps; |
346 | 346 | ||
347 | if (idxp->nodep) { |
347 | if (idxp->nodep) { |
348 | /* |
348 | /* |
349 | * We are lucky. |
349 | * We are lucky. |
350 | * The node is already instantiated in memory. |
350 | * The node is already instantiated in memory. |
351 | */ |
351 | */ |
352 | futex_down(&idxp->nodep->lock); |
352 | futex_down(&idxp->nodep->lock); |
353 | if (!idxp->nodep->refcnt++) |
353 | if (!idxp->nodep->refcnt++) |
354 | list_remove(&idxp->nodep->ffn_link); |
354 | list_remove(&idxp->nodep->ffn_link); |
355 | futex_up(&idxp->nodep->lock); |
355 | futex_up(&idxp->nodep->lock); |
356 | return idxp->nodep; |
356 | return idxp->nodep; |
357 | } |
357 | } |
358 | 358 | ||
359 | /* |
359 | /* |
360 | * We must instantiate the node from the file system. |
360 | * We must instantiate the node from the file system. |
361 | */ |
361 | */ |
362 | 362 | ||
363 | assert(idxp->pfc); |
363 | assert(idxp->pfc); |
364 | 364 | ||
365 | futex_down(&ffn_futex); |
365 | futex_down(&ffn_futex); |
366 | if (!list_empty(&ffn_head)) { |
366 | if (!list_empty(&ffn_head)) { |
367 | /* Try to use a cached free node structure. */ |
367 | /* Try to use a cached free node structure. */ |
368 | fat_idx_t *idxp_tmp; |
368 | fat_idx_t *idxp_tmp; |
369 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
369 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
370 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
370 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
371 | goto skip_cache; |
371 | goto skip_cache; |
372 | idxp_tmp = nodep->idx; |
372 | idxp_tmp = nodep->idx; |
373 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
373 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
374 | futex_up(&nodep->lock); |
374 | futex_up(&nodep->lock); |
375 | goto skip_cache; |
375 | goto skip_cache; |
376 | } |
376 | } |
377 | list_remove(&nodep->ffn_link); |
377 | list_remove(&nodep->ffn_link); |
378 | futex_up(&ffn_futex); |
378 | futex_up(&ffn_futex); |
379 | if (nodep->dirty) |
379 | if (nodep->dirty) |
380 | fat_node_sync(nodep); |
380 | fat_node_sync(nodep); |
381 | idxp_tmp->nodep = NULL; |
381 | idxp_tmp->nodep = NULL; |
382 | futex_up(&nodep->lock); |
382 | futex_up(&nodep->lock); |
383 | futex_up(&idxp_tmp->lock); |
383 | futex_up(&idxp_tmp->lock); |
384 | } else { |
384 | } else { |
385 | skip_cache: |
385 | skip_cache: |
386 | /* Try to allocate a new node structure. */ |
386 | /* Try to allocate a new node structure. */ |
387 | futex_up(&ffn_futex); |
387 | futex_up(&ffn_futex); |
388 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
388 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
389 | if (!nodep) |
389 | if (!nodep) |
390 | return NULL; |
390 | return NULL; |
391 | } |
391 | } |
392 | fat_node_initialize(nodep); |
392 | fat_node_initialize(nodep); |
393 | 393 | ||
394 | bps = fat_bps_get(idxp->dev_handle); |
394 | bps = fat_bps_get(idxp->dev_handle); |
395 | dps = bps / sizeof(fat_dentry_t); |
395 | dps = bps / sizeof(fat_dentry_t); |
396 | 396 | ||
397 | /* Read the block that contains the dentry of interest. */ |
397 | /* Read the block that contains the dentry of interest. */ |
398 | b = _fat_block_get(idxp->dev_handle, idxp->pfc, |
398 | b = _fat_block_get(idxp->dev_handle, idxp->pfc, |
399 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
399 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
400 | assert(b); |
400 | assert(b); |
401 | 401 | ||
402 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
402 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
403 | if (d->attr & FAT_ATTR_SUBDIR) { |
403 | if (d->attr & FAT_ATTR_SUBDIR) { |
404 | /* |
404 | /* |
405 | * The only directory which does not have this bit set is the |
405 | * The only directory which does not have this bit set is the |
406 | * root directory itself. The root directory node is handled |
406 | * root directory itself. The root directory node is handled |
407 | * and initialized elsewhere. |
407 | * and initialized elsewhere. |
408 | */ |
408 | */ |
409 | nodep->type = FAT_DIRECTORY; |
409 | nodep->type = FAT_DIRECTORY; |
410 | /* |
410 | /* |
411 | * Unfortunately, the 'size' field of the FAT dentry is not |
411 | * Unfortunately, the 'size' field of the FAT dentry is not |
412 | * defined for the directory entry type. We must determine the |
412 | * defined for the directory entry type. We must determine the |
413 | * size of the directory by walking the FAT. |
413 | * size of the directory by walking the FAT. |
414 | */ |
414 | */ |
415 | nodep->size = bps * _fat_blcks_get(idxp->dev_handle, |
415 | nodep->size = bps * _fat_blcks_get(idxp->dev_handle, |
416 | uint16_t_le2host(d->firstc)); |
416 | uint16_t_le2host(d->firstc)); |
417 | } else { |
417 | } else { |
418 | nodep->type = FAT_FILE; |
418 | nodep->type = FAT_FILE; |
419 | nodep->size = uint32_t_le2host(d->size); |
419 | nodep->size = uint32_t_le2host(d->size); |
420 | } |
420 | } |
421 | nodep->firstc = uint16_t_le2host(d->firstc); |
421 | nodep->firstc = uint16_t_le2host(d->firstc); |
422 | nodep->lnkcnt = 1; |
422 | nodep->lnkcnt = 1; |
423 | nodep->refcnt = 1; |
423 | nodep->refcnt = 1; |
424 | 424 | ||
425 | block_put(b); |
425 | block_put(b); |
426 | 426 | ||
427 | /* Link the idx structure with the node structure. */ |
427 | /* Link the idx structure with the node structure. */ |
428 | nodep->idx = idxp; |
428 | nodep->idx = idxp; |
429 | idxp->nodep = nodep; |
429 | idxp->nodep = nodep; |
430 | 430 | ||
431 | return nodep; |
431 | return nodep; |
432 | } |
432 | } |
433 | 433 | ||
434 | /** Instantiate a FAT in-core node. */ |
434 | /** Instantiate a FAT in-core node. */ |
435 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
435 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
436 | { |
436 | { |
437 | void *node; |
437 | void *node; |
438 | fat_idx_t *idxp; |
438 | fat_idx_t *idxp; |
439 | 439 | ||
440 | idxp = fat_idx_get_by_index(dev_handle, index); |
440 | idxp = fat_idx_get_by_index(dev_handle, index); |
441 | if (!idxp) |
441 | if (!idxp) |
442 | return NULL; |
442 | return NULL; |
443 | /* idxp->lock held */ |
443 | /* idxp->lock held */ |
444 | node = fat_node_get_core(idxp); |
444 | node = fat_node_get_core(idxp); |
445 | futex_up(&idxp->lock); |
445 | futex_up(&idxp->lock); |
446 | return node; |
446 | return node; |
447 | } |
447 | } |
448 | 448 | ||
449 | static void fat_node_put(void *node) |
449 | static void fat_node_put(void *node) |
450 | { |
450 | { |
451 | fat_node_t *nodep = (fat_node_t *)node; |
451 | fat_node_t *nodep = (fat_node_t *)node; |
452 | 452 | ||
453 | futex_down(&nodep->lock); |
453 | futex_down(&nodep->lock); |
454 | if (!--nodep->refcnt) { |
454 | if (!--nodep->refcnt) { |
455 | futex_down(&ffn_futex); |
455 | futex_down(&ffn_futex); |
456 | list_append(&nodep->ffn_link, &ffn_head); |
456 | list_append(&nodep->ffn_link, &ffn_head); |
457 | futex_up(&ffn_futex); |
457 | futex_up(&ffn_futex); |
458 | } |
458 | } |
459 | futex_up(&nodep->lock); |
459 | futex_up(&nodep->lock); |
460 | } |
460 | } |
461 | 461 | ||
462 | static void *fat_create(int flags) |
462 | static void *fat_create(int flags) |
463 | { |
463 | { |
464 | return NULL; /* not supported at the moment */ |
464 | return NULL; /* not supported at the moment */ |
465 | } |
465 | } |
466 | 466 | ||
467 | static int fat_destroy(void *node) |
467 | static int fat_destroy(void *node) |
468 | { |
468 | { |
469 | return ENOTSUP; /* not supported at the moment */ |
469 | return ENOTSUP; /* not supported at the moment */ |
470 | } |
470 | } |
471 | 471 | ||
472 | static bool fat_link(void *prnt, void *chld, const char *name) |
472 | static bool fat_link(void *prnt, void *chld, const char *name) |
473 | { |
473 | { |
474 | return false; /* not supported at the moment */ |
474 | return false; /* not supported at the moment */ |
475 | } |
475 | } |
476 | 476 | ||
477 | static int fat_unlink(void *prnt, void *chld) |
477 | static int fat_unlink(void *prnt, void *chld) |
478 | { |
478 | { |
479 | return ENOTSUP; /* not supported at the moment */ |
479 | return ENOTSUP; /* not supported at the moment */ |
480 | } |
480 | } |
481 | 481 | ||
482 | static void *fat_match(void *prnt, const char *component) |
482 | static void *fat_match(void *prnt, const char *component) |
483 | { |
483 | { |
484 | fat_node_t *parentp = (fat_node_t *)prnt; |
484 | fat_node_t *parentp = (fat_node_t *)prnt; |
485 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
485 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
486 | unsigned i, j; |
486 | unsigned i, j; |
487 | unsigned bps; /* bytes per sector */ |
487 | unsigned bps; /* bytes per sector */ |
488 | unsigned dps; /* dentries per sector */ |
488 | unsigned dps; /* dentries per sector */ |
489 | unsigned blocks; |
489 | unsigned blocks; |
490 | fat_dentry_t *d; |
490 | fat_dentry_t *d; |
491 | block_t *b; |
491 | block_t *b; |
492 | 492 | ||
493 | futex_down(&parentp->idx->lock); |
493 | futex_down(&parentp->idx->lock); |
494 | bps = fat_bps_get(parentp->idx->dev_handle); |
494 | bps = fat_bps_get(parentp->idx->dev_handle); |
495 | dps = bps / sizeof(fat_dentry_t); |
495 | dps = bps / sizeof(fat_dentry_t); |
496 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
496 | blocks = parentp->size / bps + (parentp->size % bps != 0); |
497 | for (i = 0; i < blocks; i++) { |
497 | for (i = 0; i < blocks; i++) { |
498 | unsigned dentries; |
498 | unsigned dentries; |
499 | 499 | ||
500 | b = fat_block_get(parentp, i); |
500 | b = fat_block_get(parentp, i); |
501 | dentries = (i == blocks - 1) ? |
501 | dentries = (i == blocks - 1) ? |
502 | parentp->size % sizeof(fat_dentry_t) : |
502 | parentp->size % sizeof(fat_dentry_t) : |
503 | dps; |
503 | dps; |
504 | for (j = 0; j < dentries; j++) { |
504 | for (j = 0; j < dentries; j++) { |
505 | d = ((fat_dentry_t *)b->data) + j; |
505 | d = ((fat_dentry_t *)b->data) + j; |
506 | switch (fat_classify_dentry(d)) { |
506 | switch (fat_classify_dentry(d)) { |
507 | case FAT_DENTRY_SKIP: |
507 | case FAT_DENTRY_SKIP: |
508 | continue; |
508 | continue; |
509 | case FAT_DENTRY_LAST: |
509 | case FAT_DENTRY_LAST: |
510 | block_put(b); |
510 | block_put(b); |
511 | futex_up(&parentp->idx->lock); |
511 | futex_up(&parentp->idx->lock); |
512 | return NULL; |
512 | return NULL; |
513 | default: |
513 | default: |
514 | case FAT_DENTRY_VALID: |
514 | case FAT_DENTRY_VALID: |
515 | dentry_name_canonify(d, name); |
515 | dentry_name_canonify(d, name); |
516 | break; |
516 | break; |
517 | } |
517 | } |
518 | if (stricmp(name, component) == 0) { |
518 | if (stricmp(name, component) == 0) { |
519 | /* hit */ |
519 | /* hit */ |
520 | void *node; |
520 | void *node; |
521 | /* |
521 | /* |
522 | * Assume tree hierarchy for locking. We |
522 | * Assume tree hierarchy for locking. We |
523 | * already have the parent and now we are going |
523 | * already have the parent and now we are going |
524 | * to lock the child. Never lock in the oposite |
524 | * to lock the child. Never lock in the oposite |
525 | * order. |
525 | * order. |
526 | */ |
526 | */ |
527 | fat_idx_t *idx = fat_idx_get_by_pos( |
527 | fat_idx_t *idx = fat_idx_get_by_pos( |
528 | parentp->idx->dev_handle, parentp->firstc, |
528 | parentp->idx->dev_handle, parentp->firstc, |
529 | i * dps + j); |
529 | i * dps + j); |
530 | futex_up(&parentp->idx->lock); |
530 | futex_up(&parentp->idx->lock); |
531 | if (!idx) { |
531 | if (!idx) { |
532 | /* |
532 | /* |
533 | * Can happen if memory is low or if we |
533 | * Can happen if memory is low or if we |
534 | * run out of 32-bit indices. |
534 | * run out of 32-bit indices. |
535 | */ |
535 | */ |
536 | block_put(b); |
536 | block_put(b); |
537 | return NULL; |
537 | return NULL; |
538 | } |
538 | } |
539 | node = fat_node_get_core(idx); |
539 | node = fat_node_get_core(idx); |
540 | futex_up(&idx->lock); |
540 | futex_up(&idx->lock); |
541 | block_put(b); |
541 | block_put(b); |
542 | return node; |
542 | return node; |
543 | } |
543 | } |
544 | } |
544 | } |
545 | block_put(b); |
545 | block_put(b); |
546 | } |
546 | } |
547 | futex_up(&parentp->idx->lock); |
547 | futex_up(&parentp->idx->lock); |
548 | return NULL; |
548 | return NULL; |
549 | } |
549 | } |
550 | 550 | ||
551 | static fs_index_t fat_index_get(void *node) |
551 | static fs_index_t fat_index_get(void *node) |
552 | { |
552 | { |
553 | fat_node_t *fnodep = (fat_node_t *)node; |
553 | fat_node_t *fnodep = (fat_node_t *)node; |
554 | if (!fnodep) |
554 | if (!fnodep) |
555 | return 0; |
555 | return 0; |
556 | return fnodep->idx->index; |
556 | return fnodep->idx->index; |
557 | } |
557 | } |
558 | 558 | ||
559 | static size_t fat_size_get(void *node) |
559 | static size_t fat_size_get(void *node) |
560 | { |
560 | { |
561 | return ((fat_node_t *)node)->size; |
561 | return ((fat_node_t *)node)->size; |
562 | } |
562 | } |
563 | 563 | ||
564 | static unsigned fat_lnkcnt_get(void *node) |
564 | static unsigned fat_lnkcnt_get(void *node) |
565 | { |
565 | { |
566 | return ((fat_node_t *)node)->lnkcnt; |
566 | return ((fat_node_t *)node)->lnkcnt; |
567 | } |
567 | } |
568 | 568 | ||
569 | static bool fat_has_children(void *node) |
569 | static bool fat_has_children(void *node) |
570 | { |
570 | { |
571 | fat_node_t *nodep = (fat_node_t *)node; |
571 | fat_node_t *nodep = (fat_node_t *)node; |
572 | unsigned bps; |
572 | unsigned bps; |
573 | unsigned dps; |
573 | unsigned dps; |
574 | unsigned blocks; |
574 | unsigned blocks; |
575 | block_t *b; |
575 | block_t *b; |
576 | unsigned i, j; |
576 | unsigned i, j; |
577 | 577 | ||
578 | if (nodep->type != FAT_DIRECTORY) |
578 | if (nodep->type != FAT_DIRECTORY) |
579 | return false; |
579 | return false; |
580 | 580 | ||
581 | futex_down(&nodep->idx->lock); |
581 | futex_down(&nodep->idx->lock); |
582 | bps = fat_bps_get(nodep->idx->dev_handle); |
582 | bps = fat_bps_get(nodep->idx->dev_handle); |
583 | dps = bps / sizeof(fat_dentry_t); |
583 | dps = bps / sizeof(fat_dentry_t); |
584 | 584 | ||
585 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
585 | blocks = nodep->size / bps + (nodep->size % bps != 0); |
586 | 586 | ||
587 | for (i = 0; i < blocks; i++) { |
587 | for (i = 0; i < blocks; i++) { |
588 | unsigned dentries; |
588 | unsigned dentries; |
589 | fat_dentry_t *d; |
589 | fat_dentry_t *d; |
590 | 590 | ||
591 | b = fat_block_get(nodep, i); |
591 | b = fat_block_get(nodep, i); |
592 | dentries = (i == blocks - 1) ? |
592 | dentries = (i == blocks - 1) ? |
593 | nodep->size % sizeof(fat_dentry_t) : |
593 | nodep->size % sizeof(fat_dentry_t) : |
594 | dps; |
594 | dps; |
595 | for (j = 0; j < dentries; j++) { |
595 | for (j = 0; j < dentries; j++) { |
596 | d = ((fat_dentry_t *)b->data) + j; |
596 | d = ((fat_dentry_t *)b->data) + j; |
597 | switch (fat_classify_dentry(d)) { |
597 | switch (fat_classify_dentry(d)) { |
598 | case FAT_DENTRY_SKIP: |
598 | case FAT_DENTRY_SKIP: |
599 | continue; |
599 | continue; |
600 | case FAT_DENTRY_LAST: |
600 | case FAT_DENTRY_LAST: |
601 | block_put(b); |
601 | block_put(b); |
602 | futex_up(&nodep->idx->lock); |
602 | futex_up(&nodep->idx->lock); |
603 | return false; |
603 | return false; |
604 | default: |
604 | default: |
605 | case FAT_DENTRY_VALID: |
605 | case FAT_DENTRY_VALID: |
606 | block_put(b); |
606 | block_put(b); |
607 | futex_up(&nodep->idx->lock); |
607 | futex_up(&nodep->idx->lock); |
608 | return true; |
608 | return true; |
609 | } |
609 | } |
610 | block_put(b); |
610 | block_put(b); |
611 | futex_up(&nodep->idx->lock); |
611 | futex_up(&nodep->idx->lock); |
612 | return true; |
612 | return true; |
613 | } |
613 | } |
614 | block_put(b); |
614 | block_put(b); |
615 | } |
615 | } |
616 | 616 | ||
617 | futex_up(&nodep->idx->lock); |
617 | futex_up(&nodep->idx->lock); |
618 | return false; |
618 | return false; |
619 | } |
619 | } |
620 | 620 | ||
621 | static void *fat_root_get(dev_handle_t dev_handle) |
621 | static void *fat_root_get(dev_handle_t dev_handle) |
622 | { |
622 | { |
623 | return fat_node_get(dev_handle, 0); |
623 | return fat_node_get(dev_handle, 0); |
624 | } |
624 | } |
625 | 625 | ||
626 | static char fat_plb_get_char(unsigned pos) |
626 | static char fat_plb_get_char(unsigned pos) |
627 | { |
627 | { |
628 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
628 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
629 | } |
629 | } |
630 | 630 | ||
631 | static bool fat_is_directory(void *node) |
631 | static bool fat_is_directory(void *node) |
632 | { |
632 | { |
633 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
633 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
634 | } |
634 | } |
635 | 635 | ||
636 | static bool fat_is_file(void *node) |
636 | static bool fat_is_file(void *node) |
637 | { |
637 | { |
638 | return ((fat_node_t *)node)->type == FAT_FILE; |
638 | return ((fat_node_t *)node)->type == FAT_FILE; |
639 | } |
639 | } |
640 | 640 | ||
641 | /** libfs operations */ |
641 | /** libfs operations */ |
642 | libfs_ops_t fat_libfs_ops = { |
642 | libfs_ops_t fat_libfs_ops = { |
643 | .match = fat_match, |
643 | .match = fat_match, |
644 | .node_get = fat_node_get, |
644 | .node_get = fat_node_get, |
645 | .node_put = fat_node_put, |
645 | .node_put = fat_node_put, |
646 | .create = fat_create, |
646 | .create = fat_create, |
647 | .destroy = fat_destroy, |
647 | .destroy = fat_destroy, |
648 | .link = fat_link, |
648 | .link = fat_link, |
649 | .unlink = fat_unlink, |
649 | .unlink = fat_unlink, |
650 | .index_get = fat_index_get, |
650 | .index_get = fat_index_get, |
651 | .size_get = fat_size_get, |
651 | .size_get = fat_size_get, |
652 | .lnkcnt_get = fat_lnkcnt_get, |
652 | .lnkcnt_get = fat_lnkcnt_get, |
653 | .has_children = fat_has_children, |
653 | .has_children = fat_has_children, |
654 | .root_get = fat_root_get, |
654 | .root_get = fat_root_get, |
655 | .plb_get_char = fat_plb_get_char, |
655 | .plb_get_char = fat_plb_get_char, |
656 | .is_directory = fat_is_directory, |
656 | .is_directory = fat_is_directory, |
657 | .is_file = fat_is_file |
657 | .is_file = fat_is_file |
658 | }; |
658 | }; |
659 | 659 | ||
660 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
660 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
661 | { |
661 | { |
662 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
662 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
663 | block_t *bb; |
663 | block_t *bb; |
664 | uint16_t bps; |
664 | uint16_t bps; |
665 | uint16_t rde; |
665 | uint16_t rde; |
666 | int rc; |
666 | int rc; |
667 | 667 | ||
668 | /* |
668 | /* |
669 | * For now, we don't bother to remember dev_handle, dev_phone or |
669 | * For now, we don't bother to remember dev_handle, dev_phone or |
670 | * dev_buffer in some data structure. We use global variables because we |
670 | * dev_buffer in some data structure. We use global variables because we |
671 | * know there will be at most one mount on this file system. |
671 | * know there will be at most one mount on this file system. |
672 | * Of course, this is a huge TODO item. |
672 | * Of course, this is a huge TODO item. |
673 | */ |
673 | */ |
674 | dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE, |
674 | dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE, |
675 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
675 | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
676 | 676 | ||
677 | if (!dev_buffer) { |
677 | if (!dev_buffer) { |
678 | ipc_answer_0(rid, ENOMEM); |
678 | ipc_answer_0(rid, ENOMEM); |
679 | return; |
679 | return; |
680 | } |
680 | } |
681 | 681 | ||
682 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
682 | dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
683 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
683 | DEVMAP_CONNECT_TO_DEVICE, dev_handle); |
684 | 684 | ||
685 | if (dev_phone < 0) { |
685 | if (dev_phone < 0) { |
686 | munmap(dev_buffer, BS_SIZE); |
686 | munmap(dev_buffer, BS_SIZE); |
687 | ipc_answer_0(rid, dev_phone); |
687 | ipc_answer_0(rid, dev_phone); |
688 | return; |
688 | return; |
689 | } |
689 | } |
690 | 690 | ||
691 | rc = ipc_share_out_start(dev_phone, dev_buffer, |
691 | rc = ipc_share_out_start(dev_phone, dev_buffer, |
692 | AS_AREA_READ | AS_AREA_WRITE); |
692 | AS_AREA_READ | AS_AREA_WRITE); |
693 | if (rc != EOK) { |
693 | if (rc != EOK) { |
694 | munmap(dev_buffer, BS_SIZE); |
694 | munmap(dev_buffer, BS_SIZE); |
695 | ipc_answer_0(rid, rc); |
695 | ipc_answer_0(rid, rc); |
696 | return; |
696 | return; |
697 | } |
697 | } |
698 | 698 | ||
699 | /* Read the number of root directory entries. */ |
699 | /* Read the number of root directory entries. */ |
700 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
700 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
701 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
701 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
702 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
702 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
703 | block_put(bb); |
703 | block_put(bb); |
704 | 704 | ||
705 | if (bps != BS_SIZE) { |
705 | if (bps != BS_SIZE) { |
706 | munmap(dev_buffer, BS_SIZE); |
706 | munmap(dev_buffer, BS_SIZE); |
707 | ipc_answer_0(rid, ENOTSUP); |
707 | ipc_answer_0(rid, ENOTSUP); |
708 | return; |
708 | return; |
709 | } |
709 | } |
710 | 710 | ||
711 | rc = fat_idx_init_by_dev_handle(dev_handle); |
711 | rc = fat_idx_init_by_dev_handle(dev_handle); |
712 | if (rc != EOK) { |
712 | if (rc != EOK) { |
713 | munmap(dev_buffer, BS_SIZE); |
713 | munmap(dev_buffer, BS_SIZE); |
714 | ipc_answer_0(rid, rc); |
714 | ipc_answer_0(rid, rc); |
715 | return; |
715 | return; |
716 | } |
716 | } |
717 | 717 | ||
718 | /* Initialize the root node. */ |
718 | /* Initialize the root node. */ |
719 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
719 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
720 | if (!rootp) { |
720 | if (!rootp) { |
721 | munmap(dev_buffer, BS_SIZE); |
721 | munmap(dev_buffer, BS_SIZE); |
722 | fat_idx_fini_by_dev_handle(dev_handle); |
722 | fat_idx_fini_by_dev_handle(dev_handle); |
723 | ipc_answer_0(rid, ENOMEM); |
723 | ipc_answer_0(rid, ENOMEM); |
724 | return; |
724 | return; |
725 | } |
725 | } |
726 | fat_node_initialize(rootp); |
726 | fat_node_initialize(rootp); |
727 | 727 | ||
728 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
728 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
729 | if (!ridxp) { |
729 | if (!ridxp) { |
730 | munmap(dev_buffer, BS_SIZE); |
730 | munmap(dev_buffer, BS_SIZE); |
731 | free(rootp); |
731 | free(rootp); |
732 | fat_idx_fini_by_dev_handle(dev_handle); |
732 | fat_idx_fini_by_dev_handle(dev_handle); |
733 | ipc_answer_0(rid, ENOMEM); |
733 | ipc_answer_0(rid, ENOMEM); |
734 | return; |
734 | return; |
735 | } |
735 | } |
736 | assert(ridxp->index == 0); |
736 | assert(ridxp->index == 0); |
737 | /* ridxp->lock held */ |
737 | /* ridxp->lock held */ |
738 | 738 | ||
739 | rootp->type = FAT_DIRECTORY; |
739 | rootp->type = FAT_DIRECTORY; |
740 | rootp->firstc = FAT_CLST_ROOT; |
740 | rootp->firstc = FAT_CLST_ROOT; |
741 | rootp->refcnt = 1; |
741 | rootp->refcnt = 1; |
742 | rootp->lnkcnt = 0; /* FS root is not linked */ |
742 | rootp->lnkcnt = 0; /* FS root is not linked */ |
743 | rootp->size = rde * sizeof(fat_dentry_t); |
743 | rootp->size = rde * sizeof(fat_dentry_t); |
744 | rootp->idx = ridxp; |
744 | rootp->idx = ridxp; |
745 | ridxp->nodep = rootp; |
745 | ridxp->nodep = rootp; |
746 | 746 | ||
747 | futex_up(&ridxp->lock); |
747 | futex_up(&ridxp->lock); |
748 | 748 | ||
749 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
749 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
750 | } |
750 | } |
751 | 751 | ||
752 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
752 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
753 | { |
753 | { |
754 | ipc_answer_0(rid, ENOTSUP); |
754 | ipc_answer_0(rid, ENOTSUP); |
755 | } |
755 | } |
756 | 756 | ||
757 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
757 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
758 | { |
758 | { |
759 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
759 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
760 | } |
760 | } |
761 | 761 | ||
762 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
762 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
763 | { |
763 | { |
764 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
764 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
765 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
765 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
766 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
766 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
767 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
767 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
768 | uint16_t bps = fat_bps_get(dev_handle); |
768 | uint16_t bps = fat_bps_get(dev_handle); |
769 | size_t bytes; |
769 | size_t bytes; |
770 | block_t *b; |
770 | block_t *b; |
771 | 771 | ||
772 | if (!nodep) { |
772 | if (!nodep) { |
773 | ipc_answer_0(rid, ENOENT); |
773 | ipc_answer_0(rid, ENOENT); |
774 | return; |
774 | return; |
775 | } |
775 | } |
776 | 776 | ||
777 | ipc_callid_t callid; |
777 | ipc_callid_t callid; |
778 | size_t len; |
778 | size_t len; |
779 | if (!ipc_data_read_receive(&callid, &len)) { |
779 | if (!ipc_data_read_receive(&callid, &len)) { |
780 | fat_node_put(nodep); |
780 | fat_node_put(nodep); |
781 | ipc_answer_0(callid, EINVAL); |
781 | ipc_answer_0(callid, EINVAL); |
782 | ipc_answer_0(rid, EINVAL); |
782 | ipc_answer_0(rid, EINVAL); |
783 | return; |
783 | return; |
784 | } |
784 | } |
785 | 785 | ||
786 | if (nodep->type == FAT_FILE) { |
786 | if (nodep->type == FAT_FILE) { |
787 | /* |
787 | /* |
788 | * Our strategy for regular file reads is to read one block at |
788 | * Our strategy for regular file reads is to read one block at |
789 | * most and make use of the possibility to return less data than |
789 | * most and make use of the possibility to return less data than |
790 | * requested. This keeps the code very simple. |
790 | * requested. This keeps the code very simple. |
791 | */ |
791 | */ |
792 | bytes = min(len, bps - pos % bps); |
792 | bytes = min(len, bps - pos % bps); |
793 | b = fat_block_get(nodep, pos / bps); |
793 | b = fat_block_get(nodep, pos / bps); |
794 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
794 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
795 | bytes); |
795 | bytes); |
796 | block_put(b); |
796 | block_put(b); |
797 | } else { |
797 | } else { |
798 | unsigned bnum; |
798 | unsigned bnum; |
799 | off_t spos = pos; |
799 | off_t spos = pos; |
800 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
800 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
801 | fat_dentry_t *d; |
801 | fat_dentry_t *d; |
802 | 802 | ||
803 | assert(nodep->type == FAT_DIRECTORY); |
803 | assert(nodep->type == FAT_DIRECTORY); |
804 | assert(nodep->size % bps == 0); |
804 | assert(nodep->size % bps == 0); |
805 | assert(bps % sizeof(fat_dentry_t) == 0); |
805 | assert(bps % sizeof(fat_dentry_t) == 0); |
806 | 806 | ||
807 | /* |
807 | /* |
808 | * Our strategy for readdir() is to use the position pointer as |
808 | * Our strategy for readdir() is to use the position pointer as |
809 | * an index into the array of all dentries. On entry, it points |
809 | * an index into the array of all dentries. On entry, it points |
810 | * to the first unread dentry. If we skip any dentries, we bump |
810 | * to the first unread dentry. If we skip any dentries, we bump |
811 | * the position pointer accordingly. |
811 | * the position pointer accordingly. |
812 | */ |
812 | */ |
813 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
813 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
814 | while (bnum < nodep->size / bps) { |
814 | while (bnum < nodep->size / bps) { |
815 | off_t o; |
815 | off_t o; |
816 | 816 | ||
817 | b = fat_block_get(nodep, bnum); |
817 | b = fat_block_get(nodep, bnum); |
818 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
818 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
819 | o < bps / sizeof(fat_dentry_t); |
819 | o < bps / sizeof(fat_dentry_t); |
820 | o++, pos++) { |
820 | o++, pos++) { |
821 | d = ((fat_dentry_t *)b->data) + o; |
821 | d = ((fat_dentry_t *)b->data) + o; |
822 | switch (fat_classify_dentry(d)) { |
822 | switch (fat_classify_dentry(d)) { |
823 | case FAT_DENTRY_SKIP: |
823 | case FAT_DENTRY_SKIP: |
824 | continue; |
824 | continue; |
825 | case FAT_DENTRY_LAST: |
825 | case FAT_DENTRY_LAST: |
826 | block_put(b); |
826 | block_put(b); |
827 | goto miss; |
827 | goto miss; |
828 | default: |
828 | default: |
829 | case FAT_DENTRY_VALID: |
829 | case FAT_DENTRY_VALID: |
830 | dentry_name_canonify(d, name); |
830 | dentry_name_canonify(d, name); |
831 | block_put(b); |
831 | block_put(b); |
832 | goto hit; |
832 | goto hit; |
833 | } |
833 | } |
834 | } |
834 | } |
835 | block_put(b); |
835 | block_put(b); |
836 | bnum++; |
836 | bnum++; |
837 | } |
837 | } |
838 | miss: |
838 | miss: |
839 | fat_node_put(nodep); |
839 | fat_node_put(nodep); |
840 | ipc_answer_0(callid, ENOENT); |
840 | ipc_answer_0(callid, ENOENT); |
841 | ipc_answer_1(rid, ENOENT, 0); |
841 | ipc_answer_1(rid, ENOENT, 0); |
842 | return; |
842 | return; |
843 | hit: |
843 | hit: |
844 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
844 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
845 | bytes = (pos - spos) + 1; |
845 | bytes = (pos - spos) + 1; |
846 | } |
846 | } |
847 | 847 | ||
848 | fat_node_put(nodep); |
848 | fat_node_put(nodep); |
849 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
849 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
850 | } |
850 | } |
851 | 851 | ||
- | 852 | /** Fill the gap between EOF and a new file position. |
|
- | 853 | * |
|
- | 854 | * @param nodep FAT node with the gap. |
|
- | 855 | * @param mcl First cluster in an independent cluster chain that will |
|
- | 856 | * be later appended to the end of the node's own cluster |
|
- | 857 | * chain. If pos is still in the last allocated cluster, |
|
- | 858 | * this argument is ignored. |
|
- | 859 | * @param pos Position in the last node block. |
|
- | 860 | */ |
|
852 | static void |
861 | static void |
853 | fat_fill_gap(fat_node_t *nodep, fat_cluster_t mclst, off_t pos) |
862 | fat_fill_gap(fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
854 | { |
863 | { |
- | 864 | uint16_t bps; |
|
- | 865 | unsigned spc; |
|
- | 866 | block_t *bb, *b; |
|
- | 867 | off_t o, boundary; |
|
- | 868 | ||
- | 869 | bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE); |
|
- | 870 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
|
- | 871 | spc = FAT_BS(bb)->spc; |
|
- | 872 | block_put(bb); |
|
- | 873 | ||
- | 874 | boundary = ROUND_UP(nodep->size, bps * spc); |
|
- | 875 | ||
- | 876 | /* zero out already allocated space */ |
|
- | 877 | for (o = nodep->size - 1; o < pos && o < boundary; |
|
- | 878 | o = ALIGN_DOWN(o + bps, bps)) { |
|
- | 879 | b = fat_block_get(nodep, o / bps); |
|
- | 880 | memset(b->data + o % bps, 0, bps - o % bps); |
|
- | 881 | b->dirty = true; /* need to sync node */ |
|
- | 882 | block_put(b); |
|
- | 883 | } |
|
- | 884 | ||
855 | /* TODO */ |
885 | if (o >= pos) |
- | 886 | return; |
|
- | 887 | ||
- | 888 | /* zero out the initial part of the new cluster chain */ |
|
- | 889 | for (o = boundary; o < pos; o += bps) { |
|
- | 890 | b = _fat_block_get(nodep->idx->dev_handle, mcl, |
|
- | 891 | (o - boundary) / bps); |
|
- | 892 | memset(b->data, 0, min(bps, pos - o)); |
|
- | 893 | b->dirty = true; |
|
- | 894 | block_put(b); |
|
- | 895 | } |
|
856 | } |
896 | } |
857 | 897 | ||
858 | static int |
898 | static int |
859 | fat_alloc_clusters(unsigned nclsts, fat_cluster_t *mcl, fat_cluster_t *lcl) |
899 | fat_alloc_clusters(unsigned nclsts, fat_cluster_t *mcl, fat_cluster_t *lcl) |
860 | { |
900 | { |
861 | return ENOTSUP; /* TODO */ |
901 | return ENOTSUP; /* TODO */ |
862 | } |
902 | } |
863 | 903 | ||
864 | static void |
904 | static void |
865 | fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl) |
905 | fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl) |
866 | { |
906 | { |
867 | } |
907 | } |
868 | 908 | ||
869 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
909 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
870 | { |
910 | { |
871 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
911 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
872 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
912 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
873 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
913 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
874 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
914 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
875 | size_t bytes; |
915 | size_t bytes; |
876 | block_t *b, *bb; |
916 | block_t *b, *bb; |
877 | uint16_t bps; |
917 | uint16_t bps; |
878 | unsigned spc; |
918 | unsigned spc; |
879 | off_t clst_boundary; |
919 | off_t boundary; |
880 | 920 | ||
881 | if (!nodep) { |
921 | if (!nodep) { |
882 | ipc_answer_0(rid, ENOENT); |
922 | ipc_answer_0(rid, ENOENT); |
883 | return; |
923 | return; |
884 | } |
924 | } |
885 | 925 | ||
886 | /* XXX remove me when you are ready */ |
926 | /* XXX remove me when you are ready */ |
887 | { |
927 | { |
888 | ipc_answer_0(rid, ENOTSUP); |
928 | ipc_answer_0(rid, ENOTSUP); |
889 | fat_node_put(nodep); |
929 | fat_node_put(nodep); |
890 | return; |
930 | return; |
891 | } |
931 | } |
892 | 932 | ||
893 | ipc_callid_t callid; |
933 | ipc_callid_t callid; |
894 | size_t len; |
934 | size_t len; |
895 | if (!ipc_data_write_receive(&callid, &len)) { |
935 | if (!ipc_data_write_receive(&callid, &len)) { |
896 | fat_node_put(nodep); |
936 | fat_node_put(nodep); |
897 | ipc_answer_0(callid, EINVAL); |
937 | ipc_answer_0(callid, EINVAL); |
898 | ipc_answer_0(rid, EINVAL); |
938 | ipc_answer_0(rid, EINVAL); |
899 | return; |
939 | return; |
900 | } |
940 | } |
901 | 941 | ||
902 | /* |
942 | /* |
903 | * In all scenarios, we will attempt to write out only one block worth |
943 | * In all scenarios, we will attempt to write out only one block worth |
904 | * of data at maximum. There might be some more efficient approaches, |
944 | * of data at maximum. There might be some more efficient approaches, |
905 | * but this one greatly simplifies fat_write(). Note that we can afford |
945 | * but this one greatly simplifies fat_write(). Note that we can afford |
906 | * to do this because the client must be ready to handle the return |
946 | * to do this because the client must be ready to handle the return |
907 | * value signalizing a smaller number of bytes written. |
947 | * value signalizing a smaller number of bytes written. |
908 | */ |
948 | */ |
909 | bytes = min(len, bps - pos % bps); |
949 | bytes = min(len, bps - pos % bps); |
910 | 950 | ||
911 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
951 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
912 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
952 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
913 | spc = FAT_BS(bb)->spc; |
953 | spc = FAT_BS(bb)->spc; |
914 | block_put(bb); |
954 | block_put(bb); |
915 | 955 | ||
916 | clst_boundary = ROUND_UP(nodep->size, bps * spc); |
956 | boundary = ROUND_UP(nodep->size, bps * spc); |
917 | if (pos < clst_boundary) { |
957 | if (pos < boundary) { |
918 | /* |
958 | /* |
919 | * This is the easier case - we are either overwriting already |
959 | * This is the easier case - we are either overwriting already |
920 | * existing contents or writing behind the EOF, but still within |
960 | * existing contents or writing behind the EOF, but still within |
921 | * the limits of the last cluster. The node size may grow to the |
961 | * the limits of the last cluster. The node size may grow to the |
922 | * next block size boundary. |
962 | * next block size boundary. |
923 | */ |
963 | */ |
924 | fat_fill_gap(nodep, FAT_CLST_RES0, pos); |
964 | fat_fill_gap(nodep, FAT_CLST_RES0, pos); |
925 | b = fat_block_get(nodep, pos / bps); |
965 | b = fat_block_get(nodep, pos / bps); |
926 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
966 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
927 | bytes); |
967 | bytes); |
928 | b->dirty = true; /* need to sync block */ |
968 | b->dirty = true; /* need to sync block */ |
929 | block_put(b); |
969 | block_put(b); |
930 | if (pos + bytes > nodep->size) { |
970 | if (pos + bytes > nodep->size) { |
931 | nodep->size = pos + bytes; |
971 | nodep->size = pos + bytes; |
932 | nodep->dirty = true; /* need to sync node */ |
972 | nodep->dirty = true; /* need to sync node */ |
933 | } |
973 | } |
934 | fat_node_put(nodep); |
974 | fat_node_put(nodep); |
935 | ipc_answer_1(rid, EOK, bytes); |
975 | ipc_answer_1(rid, EOK, bytes); |
936 | return; |
976 | return; |
937 | } else { |
977 | } else { |
938 | /* |
978 | /* |
939 | * This is the more difficult case. We must allocate new |
979 | * This is the more difficult case. We must allocate new |
940 | * clusters for the node and zero them out. |
980 | * clusters for the node and zero them out. |
941 | */ |
981 | */ |
942 | int status; |
982 | int status; |
943 | unsigned nclsts; |
983 | unsigned nclsts; |
944 | fat_cluster_t mcl, lcl; |
984 | fat_cluster_t mcl, lcl; |
945 | 985 | ||
946 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - clst_boundary) / |
986 | nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
947 | bps * spc; |
987 | bps * spc; |
948 | /* create an independent chain of nclsts clusters in all FATs */ |
988 | /* create an independent chain of nclsts clusters in all FATs */ |
949 | status = fat_alloc_clusters(nclsts, &mcl, &lcl); |
989 | status = fat_alloc_clusters(nclsts, &mcl, &lcl); |
950 | if (status != EOK) { |
990 | if (status != EOK) { |
951 | /* could not allocate a chain of nclsts clusters */ |
991 | /* could not allocate a chain of nclsts clusters */ |
952 | fat_node_put(nodep); |
992 | fat_node_put(nodep); |
953 | ipc_answer_0(callid, status); |
993 | ipc_answer_0(callid, status); |
954 | ipc_answer_0(rid, status); |
994 | ipc_answer_0(rid, status); |
955 | return; |
995 | return; |
956 | } |
996 | } |
957 | /* zero fill any gaps */ |
997 | /* zero fill any gaps */ |
958 | fat_fill_gap(nodep, mcl, pos); |
998 | fat_fill_gap(nodep, mcl, pos); |
959 | b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc); |
999 | b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc); |
960 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
1000 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
961 | bytes); |
1001 | bytes); |
962 | b->dirty = true; |
1002 | b->dirty = true; /* need to sync block */ |
963 | block_put(b); |
1003 | block_put(b); |
964 | /* |
1004 | /* |
965 | * Append the cluster chain starting in mcl to the end of the |
1005 | * Append the cluster chain starting in mcl to the end of the |
966 | * node's cluster chain. |
1006 | * node's cluster chain. |
967 | */ |
1007 | */ |
968 | fat_append_clusters(nodep, mcl); |
1008 | fat_append_clusters(nodep, mcl); |
969 | nodep->size = pos + bytes; |
1009 | nodep->size = pos + bytes; |
970 | nodep->dirty = true; |
1010 | nodep->dirty = true; /* need to sync node */ |
971 | fat_node_put(nodep); |
1011 | fat_node_put(nodep); |
972 | ipc_answer_1(rid, EOK, bytes); |
1012 | ipc_answer_1(rid, EOK, bytes); |
973 | return; |
1013 | return; |
974 | } |
1014 | } |
975 | } |
1015 | } |
976 | 1016 | ||
977 | /** |
1017 | /** |
978 | * @} |
1018 | * @} |
979 | */ |
1019 | */ |
980 | 1020 |