Subversion Repositories HelenOS

Rev

Rev 3593 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3593 Rev 3602
Line 92... Line 92...
92
        if (lastc)
92
        if (lastc)
93
            *lastc = clst;  /* remember the last cluster number */
93
            *lastc = clst;  /* remember the last cluster number */
94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
96
        /* read FAT1 */
96
        /* read FAT1 */
97
        b = block_get(dev_handle, rscnt + fsec);
97
        b = block_get(dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
99
        assert(clst != FAT_CLST_BAD);
99
        assert(clst != FAT_CLST_BAD);
100
        block_put(b);
100
        block_put(b);
101
        clusters++;
101
        clusters++;
102
    }
102
    }
Line 112... Line 112...
112
 * @param bs        Buffer holding the boot sector of the file system.
112
 * @param bs        Buffer holding the boot sector of the file system.
113
 * @param dev_handle    Device handle of the file system.
113
 * @param dev_handle    Device handle of the file system.
114
 * @param firstc    First cluster used by the file. Can be zero if the file
114
 * @param firstc    First cluster used by the file. Can be zero if the file
115
 *          is empty.
115
 *          is empty.
116
 * @param bn        Block number.
116
 * @param bn        Block number.
-
 
117
 * @param flags     Flags passed to libblock.
117
 *
118
 *
118
 * @return      Block structure holding the requested block.
119
 * @return      Block structure holding the requested block.
119
 */
120
 */
120
block_t *
121
block_t *
121
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
122
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
122
    bn_t bn)
123
    bn_t bn, int flags)
123
{
124
{
124
    block_t *b;
125
    block_t *b;
125
    unsigned bps;
126
    unsigned bps;
126
    unsigned rscnt;     /* block address of the first FAT */
127
    unsigned rscnt;     /* block address of the first FAT */
127
    unsigned rde;
128
    unsigned rde;
Line 141... Line 142...
141
    ssa = rscnt + bs->fatcnt * sf + rds;
142
    ssa = rscnt + bs->fatcnt * sf + rds;
142
 
143
 
143
    if (firstc == FAT_CLST_ROOT) {
144
    if (firstc == FAT_CLST_ROOT) {
144
        /* root directory special case */
145
        /* root directory special case */
145
        assert(bn < rds);
146
        assert(bn < rds);
146
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn);
147
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn, flags);
147
        return b;
148
        return b;
148
    }
149
    }
149
 
150
 
150
    max_clusters = bn / bs->spc;
151
    max_clusters = bn / bs->spc;
151
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
152
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
152
        max_clusters);
153
        max_clusters);
153
    assert(clusters == max_clusters);
154
    assert(clusters == max_clusters);
154
 
155
 
155
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
156
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
156
        bn % bs->spc);
157
        bn % bs->spc, flags);
157
 
158
 
158
    return b;
159
    return b;
159
}
160
}
160
 
161
 
161
/** Fill the gap between EOF and a new file position.
162
/** Fill the gap between EOF and a new file position.
Line 181... Line 182...
181
    boundary = ROUND_UP(nodep->size, bps * spc);
182
    boundary = ROUND_UP(nodep->size, bps * spc);
182
 
183
 
183
    /* zero out already allocated space */
184
    /* zero out already allocated space */
184
    for (o = nodep->size - 1; o < pos && o < boundary;
185
    for (o = nodep->size - 1; o < pos && o < boundary;
185
        o = ALIGN_DOWN(o + bps, bps)) {
186
        o = ALIGN_DOWN(o + bps, bps)) {
-
 
187
            int flags = (o % bps == 0) ?
-
 
188
            BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
186
        b = fat_block_get(bs, nodep, o / bps);
189
        b = fat_block_get(bs, nodep, o / bps, flags);
187
        memset(b->data + o % bps, 0, bps - o % bps);
190
        memset(b->data + o % bps, 0, bps - o % bps);
188
        b->dirty = true;        /* need to sync node */
191
        b->dirty = true;        /* need to sync node */
189
        block_put(b);
192
        block_put(b);
190
    }
193
    }
191
   
194
   
Line 193... Line 196...
193
        return;
196
        return;
194
   
197
   
195
    /* zero out the initial part of the new cluster chain */
198
    /* zero out the initial part of the new cluster chain */
196
    for (o = boundary; o < pos; o += bps) {
199
    for (o = boundary; o < pos; o += bps) {
197
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
200
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
198
            (o - boundary) / bps);
201
            (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
199
        memset(b->data, 0, min(bps, pos - o));
202
        memset(b->data, 0, min(bps, pos - o));
200
        b->dirty = true;        /* need to sync node */
203
        b->dirty = true;        /* need to sync node */
201
        block_put(b);
204
        block_put(b);
202
    }
205
    }
203
}
206
}
Line 219... Line 222...
219
    fat_cluster_t *cp, value;
222
    fat_cluster_t *cp, value;
220
 
223
 
221
    bps = uint16_t_le2host(bs->bps);
224
    bps = uint16_t_le2host(bs->bps);
222
    rscnt = uint16_t_le2host(bs->rscnt);
225
    rscnt = uint16_t_le2host(bs->rscnt);
223
 
226
 
224
    b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps);
227
    b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps,
-
 
228
        BLOCK_FLAGS_NONE);
225
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
229
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
226
    value = uint16_t_le2host(*cp);
230
    value = uint16_t_le2host(*cp);
227
    block_put(b);
231
    block_put(b);
228
   
232
   
229
    return value;
233
    return value;
Line 251... Line 255...
251
    rscnt = uint16_t_le2host(bs->rscnt);
255
    rscnt = uint16_t_le2host(bs->rscnt);
252
    sf = uint16_t_le2host(bs->sec_per_fat);
256
    sf = uint16_t_le2host(bs->sec_per_fat);
253
 
257
 
254
    assert(fatno < bs->fatcnt);
258
    assert(fatno < bs->fatcnt);
255
    b = block_get(dev_handle, rscnt + sf * fatno +
259
    b = block_get(dev_handle, rscnt + sf * fatno +
256
        (clst * sizeof(fat_cluster_t)) / bps);
260
        (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
257
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
261
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
258
    *cp = host2uint16_t_le(value);
262
    *cp = host2uint16_t_le(value);
259
    b->dirty = true;        /* need to sync block */
263
    b->dirty = true;        /* need to sync block */
260
    block_put(b);
264
    block_put(b);
261
}
265
}
Line 321... Line 325...
321
    /*
325
    /*
322
     * Search FAT1 for unused clusters.
326
     * Search FAT1 for unused clusters.
323
     */
327
     */
324
    futex_down(&fat_alloc_lock);
328
    futex_down(&fat_alloc_lock);
325
    for (b = 0, cl = 0; b < sf; blk++) {
329
    for (b = 0, cl = 0; b < sf; blk++) {
326
        blk = block_get(dev_handle, rscnt + b);
330
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NOREAD);
327
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
331
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
328
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
332
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
329
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
333
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
330
                /*
334
                /*
331
                 * The cluster is free. Put it into our stack
335
                 * The cluster is free. Put it into our stack