source: rtems/cpukit/libfs/src/dosfs/fat_fat_operations.c @ 665f03a

5
Last change on this file since 665f03a was e8abdfb, checked in by Joel Sherrill <joel.sherrill@…>, on 11/20/14 at 23:21:05

dosfs/fat_fat_operations.c: Explicitly ignore return (Coverity ID 26048)

Coverity spotted that the return code from fat_set_fat_cluster()
was ignored. But it should be because we want to return the status
that caused us to hit the cleanup path.

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/**
2 * @file
3 *
4 * @brief General operations on File Allocation Table
5 * @ingroup libfs_ffo Fat Fat Operations
6 */
7
8/*
9 *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
10 *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
11 */
12
13#if HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
20#include <unistd.h>
21#include <errno.h>
22#include <stdlib.h>
23
24#include <rtems/libio_.h>
25
26#include "fat.h"
27#include "fat_fat_operations.h"
28
29/* fat_scan_fat_for_free_clusters --
30 *     Allocate chain of free clusters from Files Allocation Table
31 *
32 * PARAMETERS:
33 *     fs_info  - FS info
34 *     chain    - the number of the first allocated cluster (first cluster
35 *                in  the chain)
36 *     count    - count of clusters to allocate (chain length)
37 *
38 * RETURNS:
39 *     RC_OK on success, or error code if error occured (errno set
40 *     appropriately)
41 *
42 *
43 */
44int
45fat_scan_fat_for_free_clusters(
46    fat_fs_info_t                        *fs_info,
47    uint32_t                             *chain,
48    uint32_t                              count,
49    uint32_t                             *cls_added,
50    uint32_t                             *last_cl,
51    bool                                  zero_fill
52    )
53{
54    int            rc = RC_OK;
55    uint32_t       cl4find = 2;
56    uint32_t       save_cln = FAT_UNDEFINED_VALUE;
57    uint32_t       data_cls_val = fs_info->vol.data_cls + 2;
58    uint32_t       i = 2;
59
60    if (fs_info->vol.next_cl - 2 < fs_info->vol.data_cls)
61        cl4find = fs_info->vol.next_cl;
62
63    *cls_added = 0;
64
65    /*
66     * fs_info->vol.data_cls is exactly the count of data clusters
67     * starting at cluster 2, so the maximum valid cluster number is
68     * (fs_info->vol.data_cls + 1)
69     */
70    while (*cls_added != count && i < data_cls_val)
71    {
72        uint32_t next_cln = 0;
73
74        rc = fat_get_fat_cluster(fs_info, cl4find, &next_cln);
75        if ( rc != RC_OK )
76        {
77            if (*cls_added != 0)
78                fat_free_fat_clusters_chain(fs_info, (*chain));
79            return rc;
80        }
81
82        if (next_cln == FAT_GENFAT_FREE)
83        {
84            /*
85             * We are enforced to process allocation of the first free cluster
86             * by separate 'if' statement because otherwise undo function
87             * wouldn't work properly
88             */
89            if (*cls_added == 0)
90            {
91                *chain = cl4find;
92                rc = fat_set_fat_cluster(fs_info, cl4find, FAT_GENFAT_EOC);
93                if ( rc != RC_OK )
94                {
95                    /*
96                     * this is the first cluster we tried to allocate so no
97                     * cleanup activity needed
98                     */
99                     return rc;
100                }
101            }
102            else
103            {
104                /* set EOC value to new allocated cluster */
105                rc = fat_set_fat_cluster(fs_info, cl4find, FAT_GENFAT_EOC);
106                if ( rc != RC_OK )
107                {
108                    /* cleanup activity */
109                    fat_free_fat_clusters_chain(fs_info, (*chain));
110                    return rc;
111                }
112
113                rc = fat_set_fat_cluster(fs_info, save_cln, cl4find);
114                if ( rc != RC_OK )
115                    goto cleanup;
116            }
117
118            if (zero_fill)
119            {
120                ssize_t bytes_written =
121                    fat_cluster_set(fs_info, cl4find, 0, fs_info->vol.bpc, 0);
122
123                if (fs_info->vol.bpc != bytes_written)
124                {
125                    rc = -1;
126                    goto cleanup;
127                }
128            }
129
130            save_cln = cl4find;
131            (*cls_added)++;
132        }
133        i++;
134        cl4find++;
135        if (cl4find >= data_cls_val)
136            cl4find = 2;
137    }
138
139    *last_cl = save_cln;
140    fs_info->vol.next_cl = save_cln;
141
142    if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE)
143        fs_info->vol.free_cls -= (*cls_added);
144
145    fat_buf_release(fs_info);
146
147    return RC_OK;
148
149cleanup:
150
151    /* cleanup activity */
152    fat_free_fat_clusters_chain(fs_info, (*chain));
153
154    /*
155     * Trying to save last allocated cluster for future use
156     *
157     * NOTE: Deliberately ignoring return value.
158     */
159    (void) fat_set_fat_cluster(fs_info, cl4find, FAT_GENFAT_FREE);
160    fat_buf_release(fs_info);
161    return rc;
162}
163
164/* fat_free_fat_clusters_chain --
165 *     Free chain of clusters in Files Allocation Table.
166 *
167 * PARAMETERS:
168 *     fs_info  - FS info
169 *     chain    - number of the first cluster in  the chain
170 *
171 * RETURNS:
172 *     RC_OK on success, or -1 if error occured (errno set appropriately)
173 */
174int
175fat_free_fat_clusters_chain(
176    fat_fs_info_t                        *fs_info,
177    uint32_t                              chain
178    )
179{
180    int            rc = RC_OK, rc1 = RC_OK;
181    uint32_t       cur_cln = chain;
182    uint32_t       next_cln = 0;
183    uint32_t       freed_cls_cnt = 0;
184
185    while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val)
186    {
187        rc = fat_get_fat_cluster(fs_info, cur_cln, &next_cln);
188        if ( rc != RC_OK )
189        {
190              if(fs_info->vol.free_cls != FAT_UNDEFINED_VALUE)
191                fs_info->vol.free_cls += freed_cls_cnt;
192
193            fat_buf_release(fs_info);
194            return rc;
195        }
196
197        rc = fat_set_fat_cluster(fs_info, cur_cln, FAT_GENFAT_FREE);
198        if ( rc != RC_OK )
199            rc1 = rc;
200
201        freed_cls_cnt++;
202        cur_cln = next_cln;
203    }
204
205        fs_info->vol.next_cl = chain;
206        if (fs_info->vol.free_cls != FAT_UNDEFINED_VALUE)
207            fs_info->vol.free_cls += freed_cls_cnt;
208
209    fat_buf_release(fs_info);
210    if (rc1 != RC_OK)
211        return rc1;
212
213    return RC_OK;
214}
215
216/* fat_get_fat_cluster --
217 *     Fetches the contents of the cluster (link to next cluster in the chain)
218 *     from Files Allocation Table.
219 *
220 * PARAMETERS:
221 *     fs_info  - FS info
222 *     cln      - number of cluster to fetch the contents from
223 *     ret_val  - contents of the cluster 'cln' (link to next cluster in
224 *                the chain)
225 *
226 * RETURNS:
227 *     RC_OK on success, or -1 if error occured
228 *     and errno set appropriately
229 */
230int
231fat_get_fat_cluster(
232    fat_fs_info_t                        *fs_info,
233    uint32_t                              cln,
234    uint32_t                             *ret_val
235    )
236{
237    int                     rc = RC_OK;
238    uint8_t                *sec_buf;
239    uint32_t                sec = 0;
240    uint32_t                ofs = 0;
241
242    /* sanity check */
243    if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) )
244        rtems_set_errno_and_return_minus_one(EIO);
245
246    sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) +
247          fs_info->vol.afat_loc;
248    ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1);
249
250    rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &sec_buf);
251    if (rc != RC_OK)
252        return rc;
253
254    switch ( fs_info->vol.type )
255    {
256        case FAT_FAT12:
257            /*
258             * we are enforced in complex computations for FAT12 to escape CPU
259             * align problems for some architectures
260             */
261            *ret_val = (*(sec_buf + ofs));
262            if ( ofs == (fs_info->vol.bps - 1) )
263            {
264                rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ,
265                                    &sec_buf);
266                if (rc != RC_OK)
267                    return rc;
268
269                *ret_val |= *sec_buf << 8;
270            }
271            else
272            {
273                *ret_val |= *(sec_buf + ofs + 1) << 8;
274            }
275
276            if ( FAT_CLUSTER_IS_ODD(cln) )
277                *ret_val = (*ret_val) >> FAT12_SHIFT;
278            else
279                *ret_val = (*ret_val) & FAT_FAT12_MASK;
280            break;
281
282        case FAT_FAT16:
283            *ret_val = *((uint16_t   *)(sec_buf + ofs));
284            *ret_val = CF_LE_W(*ret_val);
285            break;
286
287        case FAT_FAT32:
288            *ret_val = *((uint32_t   *)(sec_buf + ofs));
289            *ret_val = CF_LE_L(*ret_val);
290            break;
291
292        default:
293            rtems_set_errno_and_return_minus_one(EIO);
294            break;
295    }
296
297    return RC_OK;
298}
299
300/* fat_set_fat_cluster --
301 *     Set the contents of the cluster (link to next cluster in the chain)
302 *     from Files Allocation Table.
303 *
304 * PARAMETERS:
305 *     fs_info  - FS info
306 *     cln      - number of cluster to set contents to
307 *     in_val   - value to set
308 *
309 * RETURNS:
310 *     RC_OK on success, or -1 if error occured
311 *     and errno set appropriately
312 */
313int
314fat_set_fat_cluster(
315    fat_fs_info_t                        *fs_info,
316    uint32_t                              cln,
317    uint32_t                              in_val
318    )
319{
320    int                 rc = RC_OK;
321    uint32_t            sec = 0;
322    uint32_t            ofs = 0;
323    uint16_t            fat16_clv = 0;
324    uint32_t            fat32_clv = 0;
325    uint8_t            *sec_buf = NULL;
326
327    /* sanity check */
328    if ( (cln < 2) || (cln > (fs_info->vol.data_cls + 1)) )
329        rtems_set_errno_and_return_minus_one(EIO);
330
331    sec = (FAT_FAT_OFFSET(fs_info->vol.type, cln) >> fs_info->vol.sec_log2) +
332          fs_info->vol.afat_loc;
333    ofs = FAT_FAT_OFFSET(fs_info->vol.type, cln) & (fs_info->vol.bps - 1);
334
335    rc = fat_buf_access(fs_info, sec, FAT_OP_TYPE_READ, &sec_buf);
336    if (rc != RC_OK)
337        return rc;
338
339    switch ( fs_info->vol.type )
340    {
341        case FAT_FAT12:
342            if ( FAT_CLUSTER_IS_ODD(cln) )
343            {
344                fat16_clv = ((uint16_t  )in_val) << FAT_FAT12_SHIFT;
345                *(sec_buf + ofs) &= 0x0F;
346
347                *(sec_buf + ofs) |= (uint8_t)(fat16_clv & 0x00F0);
348
349                fat_buf_mark_modified(fs_info);
350
351                if ( ofs == (fs_info->vol.bps - 1) )
352                {
353                    rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ,
354                                        &sec_buf);
355                    if (rc != RC_OK)
356                        return rc;
357
358                     *sec_buf &= 0x00;
359
360                     *sec_buf |= (uint8_t)((fat16_clv & 0xFF00)>>8);
361
362                     fat_buf_mark_modified(fs_info);
363                }
364                else
365                {
366                    *(sec_buf + ofs + 1) &= 0x00;
367
368                    *(sec_buf + ofs + 1) |= (uint8_t  )((fat16_clv & 0xFF00)>>8);
369                }
370            }
371            else
372            {
373                fat16_clv = ((uint16_t  )in_val) & FAT_FAT12_MASK;
374                *(sec_buf + ofs) &= 0x00;
375
376                *(sec_buf + ofs) |= (uint8_t)(fat16_clv & 0x00FF);
377
378                fat_buf_mark_modified(fs_info);
379
380                if ( ofs == (fs_info->vol.bps - 1) )
381                {
382                    rc = fat_buf_access(fs_info, sec + 1, FAT_OP_TYPE_READ,
383                                        &sec_buf);
384                    if (rc != RC_OK)
385                        return rc;
386
387                    *sec_buf &= 0xF0;
388
389                    *sec_buf |= (uint8_t)((fat16_clv & 0xFF00)>>8);
390
391                    fat_buf_mark_modified(fs_info);
392                }
393                else
394                {
395                    *(sec_buf + ofs + 1) &= 0xF0;
396
397                    *(sec_buf + ofs+1) |= (uint8_t)((fat16_clv & 0xFF00)>>8);
398                }
399            }
400            break;
401
402        case FAT_FAT16:
403            *((uint16_t   *)(sec_buf + ofs)) =
404                    (uint16_t  )(CT_LE_W(in_val));
405            fat_buf_mark_modified(fs_info);
406            break;
407
408        case FAT_FAT32:
409            fat32_clv = CT_LE_L((in_val & FAT_FAT32_MASK));
410
411            *((uint32_t *)(sec_buf + ofs)) &= CT_LE_L(0xF0000000);
412
413            *((uint32_t *)(sec_buf + ofs)) |= fat32_clv;
414
415            fat_buf_mark_modified(fs_info);
416            break;
417
418        default:
419            rtems_set_errno_and_return_minus_one(EIO);
420            break;
421
422    }
423
424    return RC_OK;
425}
Note: See TracBrowser for help on using the repository browser.