source: rtems/cpukit/libfs/src/dosfs/fat.c @ fae59c9

5
Last change on this file since fae59c9 was fae59c9, checked in by Sebastian Huber <sebastian.huber@…>, on 09/06/17 at 08:12:06

dosfs: Support a cluster size of 64KiB

Close #3003.

  • Property mode set to 100644
File size: 29.1 KB
Line 
1/**
2 * @file
3 *
4 * @brief Low-level Operations on a Volume with a DOSFS FAT filesystem
5 * @ingroup libfs
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#include <stdint.h>
24
25#include <rtems/libio_.h>
26
27#include "fat.h"
28#include "fat_fat_operations.h"
29
30static int
31 _fat_block_release(fat_fs_info_t *fs_info);
32
33static inline uint32_t
34fat_cluster_num_to_block_num (const fat_fs_info_t *fs_info,
35                              uint32_t             cln)
36{
37    uint32_t blk;
38
39    if ( (cln == 0) && (fs_info->vol.type & (FAT_FAT12 | FAT_FAT16)) )
40        blk = fat_sector_num_to_block_num(fs_info, fs_info->vol.rdir_loc);
41    else
42    {
43        cln -= FAT_RSRVD_CLN;
44        blk = cln << (fs_info->vol.bpc_log2 - fs_info->vol.bytes_per_block_log2);
45        blk += fat_sector_num_to_block_num(fs_info, fs_info->vol.data_fsec);
46    }
47
48    return blk;
49}
50
51int
52fat_buf_access(fat_fs_info_t   *fs_info,
53               const uint32_t   sec_num,
54               const int        op_type,
55               uint8_t        **sec_buf)
56{
57    rtems_status_code sc = RTEMS_SUCCESSFUL;
58    uint32_t          blk = fat_sector_num_to_block_num (fs_info,
59                                                         sec_num);
60    uint32_t          blk_ofs = fat_sector_offset_to_block_offset (fs_info,
61                                                                   sec_num,
62                                                                   0);
63
64    if (fs_info->c.state == FAT_CACHE_EMPTY || fs_info->c.blk_num != sec_num)
65    {
66        fat_buf_release(fs_info);
67
68        if (op_type == FAT_OP_TYPE_READ)
69            sc = rtems_bdbuf_read(fs_info->vol.dd, blk, &fs_info->c.buf);
70        else
71            sc = rtems_bdbuf_get(fs_info->vol.dd, blk, &fs_info->c.buf);
72        if (sc != RTEMS_SUCCESSFUL)
73            rtems_set_errno_and_return_minus_one(EIO);
74        fs_info->c.blk_num = sec_num;
75        fs_info->c.modified = 0;
76        fs_info->c.state = FAT_CACHE_ACTUAL;
77    }
78    *sec_buf = &fs_info->c.buf->buffer[blk_ofs];
79    return RC_OK;
80}
81
82int
83fat_buf_release(fat_fs_info_t *fs_info)
84{
85    rtems_status_code sc = RTEMS_SUCCESSFUL;
86
87    if (fs_info->c.state == FAT_CACHE_EMPTY)
88        return RC_OK;
89
90    if (fs_info->c.modified)
91    {
92        uint32_t sec_num = fs_info->c.blk_num;
93        bool     sec_of_fat = ((sec_num >= fs_info->vol.fat_loc) &&
94                              (sec_num < fs_info->vol.rdir_loc));
95        uint32_t blk = fat_sector_num_to_block_num(fs_info, sec_num);
96        uint32_t blk_ofs = fat_sector_offset_to_block_offset(fs_info,
97                                                             sec_num,
98                                                             0);
99
100        if (sec_of_fat && !fs_info->vol.mirror)
101            memcpy(fs_info->sec_buf,
102                   fs_info->c.buf->buffer + blk_ofs,
103                   fs_info->vol.bps);
104
105        sc = rtems_bdbuf_release_modified(fs_info->c.buf);
106        if (sc != RTEMS_SUCCESSFUL)
107            rtems_set_errno_and_return_minus_one(EIO);
108        fs_info->c.modified = 0;
109
110        if (sec_of_fat && !fs_info->vol.mirror)
111        {
112            uint8_t i;
113
114            for (i = 1; i < fs_info->vol.fats; i++)
115            {
116                rtems_bdbuf_buffer *bd;
117
118                sec_num = fs_info->c.blk_num + fs_info->vol.fat_length * i,
119                blk = fat_sector_num_to_block_num(fs_info, sec_num);
120                blk_ofs = fat_sector_offset_to_block_offset(fs_info,
121                                                            sec_num,
122                                                            0);
123
124                if (blk_ofs == 0
125                    && fs_info->vol.bps == fs_info->vol.bytes_per_block)
126                {
127                    sc = rtems_bdbuf_get(fs_info->vol.dd, blk, &bd);
128                }
129                else
130                {
131                    sc = rtems_bdbuf_read(fs_info->vol.dd, blk, &bd);
132                }
133                if ( sc != RTEMS_SUCCESSFUL)
134                    rtems_set_errno_and_return_minus_one(ENOMEM);
135                memcpy(bd->buffer + blk_ofs, fs_info->sec_buf, fs_info->vol.bps);
136                sc = rtems_bdbuf_release_modified(bd);
137                if ( sc != RTEMS_SUCCESSFUL)
138                    rtems_set_errno_and_return_minus_one(ENOMEM);
139            }
140        }
141    }
142    else
143    {
144        sc = rtems_bdbuf_release(fs_info->c.buf);
145        if (sc != RTEMS_SUCCESSFUL)
146            rtems_set_errno_and_return_minus_one(EIO);
147    }
148    fs_info->c.state = FAT_CACHE_EMPTY;
149    return RC_OK;
150}
151
152/* _fat_block_read --
153 *     This function reads 'count' bytes from device filesystem is mounted on,
154 *     starts at 'start+offset' position where 'start' computed in sectors
155 *     and 'offset' is offset inside sector (reading may cross sectors
156 *     boundary; in this case assumed we want to read sequential sector(s))
157 *
158 * PARAMETERS:
159 *     fs_info  - FS info
160 *     start    - sector num to start read from
161 *     offset   - offset inside sector 'start'
162 *     count    - count of bytes to read
163 *     buff     - buffer provided by user
164 *
165 * RETURNS:
166 *     bytes read on success, or -1 if error occured
167 *     and errno set appropriately
168 */
169ssize_t
170_fat_block_read(
171    fat_fs_info_t                        *fs_info,
172    uint32_t                              start,
173    uint32_t                              offset,
174    uint32_t                              count,
175    void                                 *buff
176    )
177{
178    int                     rc = RC_OK;
179    ssize_t                 cmpltd = 0;
180    uint32_t                sec_num = start;
181    uint32_t                ofs = offset;
182    uint8_t                *sec_buf;
183    uint32_t                c = 0;
184
185    while (count > 0)
186    {
187        rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_READ, &sec_buf);
188        if (rc != RC_OK)
189            return -1;
190
191        c = MIN(count, (fs_info->vol.bps - ofs));
192        memcpy((buff + cmpltd), (sec_buf + ofs), c);
193
194        count -= c;
195        cmpltd += c;
196        sec_num++;
197        ofs = 0;
198    }
199    return cmpltd;
200}
201
202static ssize_t
203fat_block_write(
204    fat_fs_info_t                        *fs_info,
205    const uint32_t                        start_blk,
206    const uint32_t                        offset,
207    const uint32_t                        count,
208    const void                           *buf)
209{
210    int                 rc             = RC_OK;
211    uint32_t            bytes_to_write = MIN(count, (fs_info->vol.bytes_per_block - offset));
212    uint8_t            *blk_buf;
213    uint32_t            sec_num        = fat_block_num_to_sector_num(fs_info, start_blk);
214
215    if (0 < bytes_to_write)
216    {
217        if (bytes_to_write == fs_info->vol.bytes_per_block)
218        {
219            rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_GET, &blk_buf);
220        }
221        else
222            rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_READ, &blk_buf);
223
224        if (RC_OK == rc)
225        {
226            memcpy(blk_buf + offset, buf, bytes_to_write);
227
228            fat_buf_mark_modified(fs_info);
229        }
230    }
231    if (RC_OK != rc)
232        return rc;
233    else
234        return bytes_to_write;
235}
236
237/* fat_sector_write --
238 *     This function write 'count' bytes to device filesystem is mounted on,
239 *     starts at 'start+offset' position where 'start' computed in sectors
240 *     and 'offset' is offset inside sector (writing may cross sectors
241 *     boundary; in this case assumed we want to write sequential sector(s))
242 *
243 * PARAMETERS:
244 *     fs_info  - FS info
245 *     start    - sector num to start read from
246 *     offset   - offset inside sector 'start'
247 *     count    - count of bytes to write
248 *     buff     - buffer provided by user
249 *
250 * RETURNS:
251 *     bytes written on success, or -1 if error occured
252 *     and errno set appropriately
253 */
254ssize_t
255fat_sector_write(
256    fat_fs_info_t                        *fs_info,
257    uint32_t                              start,
258    uint32_t                              offset,
259    uint32_t                              count,
260    const void                           *buff)
261{
262    int                 rc = RC_OK;
263    ssize_t             cmpltd = 0;
264    uint32_t            sec_num = start;
265    uint32_t            ofs = offset;
266    uint8_t            *sec_buf;
267    uint32_t            c = 0;
268
269    while(count > 0)
270    {
271        c = MIN(count, (fs_info->vol.bps - ofs));
272
273        if (c == fs_info->vol.bytes_per_block)
274            rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_GET, &sec_buf);
275        else
276            rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_READ, &sec_buf);
277        if (rc != RC_OK)
278            return -1;
279
280        memcpy((sec_buf + ofs), (buff + cmpltd), c);
281
282        fat_buf_mark_modified(fs_info);
283
284        count -= c;
285        cmpltd +=c;
286        sec_num++;
287        ofs = 0;
288    }
289    return cmpltd;
290}
291
292static ssize_t
293 fat_block_set (
294     fat_fs_info_t                        *fs_info,
295     const uint32_t                        start_blk,
296     const uint32_t                        offset,
297     const uint32_t                        count,
298     const uint8_t                         pattern)
299{
300    int                 rc             = RC_OK;
301    uint32_t            bytes_to_write = MIN(count, (fs_info->vol.bytes_per_block - offset));
302    uint8_t            *blk_buf;
303    uint32_t            sec_num        = fat_block_num_to_sector_num(fs_info, start_blk);
304
305    if (0 < bytes_to_write)
306    {
307        if (bytes_to_write == fs_info->vol.bytes_per_block)
308        {
309            rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_GET, &blk_buf);
310        }
311        else
312            rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_READ, &blk_buf);
313
314        if (RC_OK == rc)
315        {
316            memset(blk_buf + offset, pattern, bytes_to_write);
317
318            fat_buf_mark_modified(fs_info);
319        }
320    }
321    if (RC_OK != rc)
322        return rc;
323    else
324        return bytes_to_write;
325}
326
327ssize_t
328fat_cluster_set(
329     fat_fs_info_t                        *fs_info,
330     const uint32_t                        start_cln,
331     const uint32_t                        offset,
332     const uint32_t                        count,
333     const uint8_t                         pattern)
334{
335  ssize_t             rc               = RC_OK;
336  uint32_t            bytes_to_write   = MIN(count, (fs_info->vol.bpc - offset));
337  uint32_t            cur_blk          = fat_cluster_num_to_block_num(fs_info, start_cln);
338  uint32_t            blocks_in_offset = offset >> fs_info->vol.bytes_per_block_log2;
339  uint32_t            ofs_blk          = offset - (blocks_in_offset << fs_info->vol.bytes_per_block_log2);
340  ssize_t             bytes_written    = 0;
341  ssize_t             ret;
342
343  cur_blk += blocks_in_offset;
344
345  while (   (RC_OK == rc)
346         && (0 < bytes_to_write))
347  {
348    uint32_t c = MIN(bytes_to_write, (fs_info->vol.bytes_per_block - ofs_blk));
349
350    ret = fat_block_set(
351        fs_info,
352        cur_blk,
353        ofs_blk,
354        c,
355        pattern);
356    if (c != ret)
357      rc = -1;
358    else
359    {
360        bytes_to_write -= ret;
361        bytes_written  += ret;
362        ++cur_blk;
363    }
364    ofs_blk = 0;
365  }
366  if (RC_OK != rc)
367    return rc;
368  else
369    return bytes_written;
370}
371
372/* _fat_block_release --
373 *     This function works around the hack that hold a bdbuf and does
374 *     not release it.
375 *
376 * PARAMETERS:
377 *     fs_info  - FS info
378 *
379 * RETURNS:
380 *     0 on success, or -1 if error occured and errno set appropriately
381 */
382int
383_fat_block_release(fat_fs_info_t *fs_info)
384{
385    return fat_buf_release(fs_info);
386}
387
388/* fat_cluster_write --
389 *     This function write 'count' bytes to device filesystem is mounted on,
390 *     starts at 'start+offset' position where 'start' computed in clusters
391 *     and 'offset' is offset inside cluster.
392 *     Writing will NOT cross cluster boundaries!
393 *
394 * PARAMETERS:
395 *     fs_info            - FS info
396 *     start_cln          - cluster number to start writing to
397 *     offset             - offset inside cluster 'start'
398 *     count              - count of bytes to write
399 *     buff               - buffer provided by user
400 *
401 * RETURNS:
402 *     bytes written on success, or -1 if error occured
403 *     and errno set appropriately
404 */
405ssize_t
406fat_cluster_write(
407    fat_fs_info_t                        *fs_info,
408    const uint32_t                        start_cln,
409    const uint32_t                        offset,
410    const uint32_t                        count,
411    const void                           *buff)
412{
413    ssize_t             rc               = RC_OK;
414    uint32_t            bytes_to_write   = MIN(count, (fs_info->vol.bpc - offset));
415    uint32_t            cur_blk          = fat_cluster_num_to_block_num(fs_info, start_cln);
416    uint32_t            blocks_in_offset = (offset >> fs_info->vol.bytes_per_block_log2);
417    uint32_t            ofs_blk          = offset - (blocks_in_offset << fs_info->vol.bytes_per_block_log2);
418    ssize_t             bytes_written    = 0;
419    uint8_t             *buffer          = (uint8_t*)buff;
420    ssize_t             ret;
421    uint32_t            c;
422
423    cur_blk += blocks_in_offset;
424
425    while (   (RC_OK == rc)
426           && (0 < bytes_to_write))
427    {
428      c = MIN(bytes_to_write, (fs_info->vol.bytes_per_block - ofs_blk));
429
430      ret = fat_block_write(
431          fs_info,
432          cur_blk,
433          ofs_blk,
434          c,
435          &buffer[bytes_written]);
436      if (c != ret)
437        rc = -1;
438      else
439      {
440          bytes_to_write -= ret;
441          bytes_written  += ret;
442          ++cur_blk;
443      }
444      ofs_blk = 0;
445    }
446    if (RC_OK != rc)
447      return rc;
448    else
449      return bytes_written;
450}
451
452static bool is_cluster_aligned(const fat_vol_t *vol, uint32_t sec_num)
453{
454    return (sec_num & (vol->spc - 1)) == 0;
455}
456
457/* fat_init_volume_info --
458 *     Get inforamtion about volume on which filesystem is mounted on
459 *
460 * PARAMETERS:
461 *     fs_info  - FS info
462 *
463 * RETURNS:
464 *     RC_OK on success, or -1 if error occured
465 *     and errno set appropriately
466 */
467int
468fat_init_volume_info(fat_fs_info_t *fs_info, const char *device)
469{
470    rtems_status_code   sc = RTEMS_SUCCESSFUL;
471    int                 rc = RC_OK;
472    fat_vol_t          *vol = &fs_info->vol;
473    uint32_t            data_secs = 0;
474    char                boot_rec[FAT_MAX_BPB_SIZE];
475    char                fs_info_sector[FAT_USEFUL_INFO_SIZE];
476    ssize_t             ret = 0;
477    struct stat         stat_buf;
478    int                 i = 0;
479    rtems_bdbuf_buffer *block = NULL;
480
481    vol->fd = open(device, O_RDWR);
482    if (vol->fd < 0)
483    {
484        rtems_set_errno_and_return_minus_one(ENXIO);
485    }
486
487    rc = fstat(vol->fd, &stat_buf);
488    if (rc != 0)
489    {
490        close(vol->fd);
491        rtems_set_errno_and_return_minus_one(ENXIO);
492    }
493
494    /* Must be a block device. */
495    if (!S_ISBLK(stat_buf.st_mode))
496    {
497        close(vol->fd);
498        rtems_set_errno_and_return_minus_one(ENXIO);
499    }
500
501    /* check that device is registred as block device and lock it */
502    rc = rtems_disk_fd_get_disk_device(vol->fd, &vol->dd);
503    if (rc != 0) {
504        close(vol->fd);
505        rtems_set_errno_and_return_minus_one(ENXIO);
506    }
507
508    /* Read boot record */
509    /* FIXME: Asserts FAT_MAX_BPB_SIZE < bdbuf block size */
510    sc = rtems_bdbuf_read( vol->dd, 0, &block);
511    if (sc != RTEMS_SUCCESSFUL)
512    {
513        close(vol->fd);
514        rtems_set_errno_and_return_minus_one( EIO);
515    }
516
517    memcpy( boot_rec, block->buffer, FAT_MAX_BPB_SIZE);
518
519    sc = rtems_bdbuf_release( block);
520    if (sc != RTEMS_SUCCESSFUL)
521    {
522        close(vol->fd);
523        rtems_set_errno_and_return_minus_one( EIO );
524    }
525
526    /* Evaluate boot record */
527    vol->bps = FAT_GET_BR_BYTES_PER_SECTOR(boot_rec);
528
529    if ( (vol->bps != 512)  &&
530         (vol->bps != 1024) &&
531         (vol->bps != 2048) &&
532         (vol->bps != 4096))
533    {
534        close(vol->fd);
535        rtems_set_errno_and_return_minus_one( EINVAL );
536    }
537    for (vol->sec_mul = 0, i = (vol->bps >> FAT_SECTOR512_BITS); (i & 1) == 0;
538         i >>= 1, vol->sec_mul++);
539    for (vol->sec_log2 = 0, i = vol->bps; (i & 1) == 0;
540         i >>= 1, vol->sec_log2++);
541
542    /* Assign the sector size as bdbuf block size for now.
543     * If possible the bdbuf block size will get increased to the cluster
544     * size at the end of this method for better performance */
545    sc = rtems_bdbuf_set_block_size (vol->dd, vol->bps, true);
546    if (sc != RTEMS_SUCCESSFUL)
547    {
548      close(vol->fd);
549      rtems_set_errno_and_return_minus_one( EINVAL );
550    }
551    vol->bytes_per_block = vol->bps;
552    vol->bytes_per_block_log2 = vol->sec_log2;
553    vol->sectors_per_block = 1;
554
555    vol->spc = FAT_GET_BR_SECTORS_PER_CLUSTER(boot_rec);
556    /*
557     * "sectors per cluster" of zero is invalid
558     * (and would hang the following loop)
559     */
560    if (vol->spc == 0)
561    {
562        close(vol->fd);
563        rtems_set_errno_and_return_minus_one(EINVAL);
564    }
565
566    for (vol->spc_log2 = 0, i = vol->spc; (i & 1) == 0;
567         i >>= 1, vol->spc_log2++);
568
569    /* Sectors per cluster must be a power of two */
570    if (vol->spc != UINT32_C(1) << vol->spc_log2)
571    {
572        close(vol->fd);
573        rtems_set_errno_and_return_minus_one(EINVAL);
574    }
575
576    vol->bpc = ((uint32_t) vol->bps) << vol->spc_log2;
577
578    for (vol->bpc_log2 = 0, i = vol->bpc; (i & 1) == 0;
579         i >>= 1, vol->bpc_log2++);
580
581    vol->fats = FAT_GET_BR_FAT_NUM(boot_rec);
582    vol->fat_loc = FAT_GET_BR_RESERVED_SECTORS_NUM(boot_rec);
583
584    vol->rdir_entrs = FAT_GET_BR_FILES_PER_ROOT_DIR(boot_rec);
585
586    /* calculate the count of sectors occupied by the root directory */
587    vol->rdir_secs = ((vol->rdir_entrs * FAT_DIRENTRY_SIZE) + (vol->bps - 1)) /
588                     vol->bps;
589
590    vol->rdir_size = vol->rdir_secs << vol->sec_log2;
591
592    if ( (FAT_GET_BR_SECTORS_PER_FAT(boot_rec)) != 0)
593        vol->fat_length = FAT_GET_BR_SECTORS_PER_FAT(boot_rec);
594    else
595        vol->fat_length = FAT_GET_BR_SECTORS_PER_FAT32(boot_rec);
596
597    vol->data_fsec = vol->fat_loc + vol->fats * vol->fat_length +
598                     vol->rdir_secs;
599
600    /* for  FAT12/16 root dir starts at(sector) */
601    vol->rdir_loc = vol->fat_loc + vol->fats * vol->fat_length;
602
603    if ( (FAT_GET_BR_TOTAL_SECTORS_NUM16(boot_rec)) != 0)
604        vol->tot_secs = FAT_GET_BR_TOTAL_SECTORS_NUM16(boot_rec);
605    else
606        vol->tot_secs = FAT_GET_BR_TOTAL_SECTORS_NUM32(boot_rec);
607
608    data_secs = vol->tot_secs - vol->data_fsec;
609
610    vol->data_cls = data_secs / vol->spc;
611
612    /* determine FAT type at least */
613    if ( vol->data_cls < FAT_FAT12_MAX_CLN)
614    {
615        vol->type = FAT_FAT12;
616        vol->mask = FAT_FAT12_MASK;
617        vol->eoc_val = FAT_FAT12_EOC;
618    }
619    else
620    {
621        if ( vol->data_cls < FAT_FAT16_MAX_CLN)
622        {
623            vol->type = FAT_FAT16;
624            vol->mask = FAT_FAT16_MASK;
625            vol->eoc_val = FAT_FAT16_EOC;
626        }
627        else if ( vol->data_cls < FAT_FAT32_MASK - 1 )
628        {
629            vol->type = FAT_FAT32;
630            vol->mask = FAT_FAT32_MASK;
631            vol->eoc_val = FAT_FAT32_EOC;
632        }
633        else
634        {
635            close(vol->fd);
636            rtems_set_errno_and_return_minus_one( EINVAL );
637        }
638    }
639
640    if (vol->type == FAT_FAT32)
641    {
642        vol->rdir_cl = FAT_GET_BR_FAT32_ROOT_CLUSTER(boot_rec);
643
644        vol->mirror = FAT_GET_BR_EXT_FLAGS(boot_rec) & FAT_BR_EXT_FLAGS_MIRROR;
645        if (vol->mirror)
646            vol->afat = FAT_GET_BR_EXT_FLAGS(boot_rec) & FAT_BR_EXT_FLAGS_FAT_NUM;
647        else
648            vol->afat = 0;
649
650        vol->info_sec = FAT_GET_BR_FAT32_FS_INFO_SECTOR(boot_rec);
651        if( vol->info_sec == 0 )
652        {
653            close(vol->fd);
654            rtems_set_errno_and_return_minus_one( EINVAL );
655        }
656        else
657        {
658            ret = _fat_block_read(fs_info, vol->info_sec , 0,
659                                  FAT_FSI_LEADSIG_SIZE, fs_info_sector);
660            if ( ret < 0 )
661            {
662                close(vol->fd);
663                return -1;
664            }
665
666            if (FAT_GET_FSINFO_LEAD_SIGNATURE(fs_info_sector) !=
667                FAT_FSINFO_LEAD_SIGNATURE_VALUE)
668            {
669                _fat_block_release(fs_info);
670                close(vol->fd);
671                rtems_set_errno_and_return_minus_one( EINVAL );
672            }
673            else
674            {
675                ret = _fat_block_read(fs_info, vol->info_sec , FAT_FSI_INFO,
676                                      FAT_USEFUL_INFO_SIZE, fs_info_sector);
677                if ( ret < 0 )
678                {
679                    _fat_block_release(fs_info);
680                    close(vol->fd);
681                    return -1;
682                }
683
684                vol->free_cls_in_fs_info =
685                  FAT_GET_FSINFO_FREE_CLUSTER_COUNT(fs_info_sector);
686                vol->free_cls = vol->free_cls_in_fs_info;
687                vol->next_cl_in_fs_info =
688                  FAT_GET_FSINFO_NEXT_FREE_CLUSTER(fs_info_sector);
689                vol->next_cl = vol->next_cl_in_fs_info;
690            }
691        }
692    }
693    else
694    {
695        vol->rdir_cl = 0;
696        vol->mirror = 0;
697        vol->afat = 0;
698        vol->free_cls = FAT_UNDEFINED_VALUE;
699        vol->next_cl = FAT_UNDEFINED_VALUE;
700    }
701
702    _fat_block_release(fs_info);
703
704    vol->afat_loc = vol->fat_loc + vol->fat_length * vol->afat;
705
706    /* set up collection of fat-files fd */
707    fs_info->vhash = calloc(FAT_HASH_SIZE, sizeof(rtems_chain_control));
708    if ( fs_info->vhash == NULL )
709    {
710        close(vol->fd);
711        rtems_set_errno_and_return_minus_one( ENOMEM );
712    }
713
714    for (i = 0; i < FAT_HASH_SIZE; i++)
715        rtems_chain_initialize_empty(fs_info->vhash + i);
716
717    fs_info->rhash = calloc(FAT_HASH_SIZE, sizeof(rtems_chain_control));
718    if ( fs_info->rhash == NULL )
719    {
720        close(vol->fd);
721        free(fs_info->vhash);
722        rtems_set_errno_and_return_minus_one( ENOMEM );
723    }
724    for (i = 0; i < FAT_HASH_SIZE; i++)
725        rtems_chain_initialize_empty(fs_info->rhash + i);
726
727    fs_info->uino_pool_size = FAT_UINO_POOL_INIT_SIZE;
728    fs_info->uino_base = (vol->tot_secs << vol->sec_mul) << 4;
729    fs_info->index = 0;
730    fs_info->uino = (char *)calloc(fs_info->uino_pool_size, sizeof(char));
731    if ( fs_info->uino == NULL )
732    {
733        close(vol->fd);
734        free(fs_info->vhash);
735        free(fs_info->rhash);
736        rtems_set_errno_and_return_minus_one( ENOMEM );
737    }
738    fs_info->sec_buf = (uint8_t *)calloc(vol->bps, sizeof(uint8_t));
739    if (fs_info->sec_buf == NULL)
740    {
741        close(vol->fd);
742        free(fs_info->vhash);
743        free(fs_info->rhash);
744        free(fs_info->uino);
745        rtems_set_errno_and_return_minus_one( ENOMEM );
746    }
747
748    /*
749     * If possible we will use the cluster size as bdbuf block size for faster
750     * file access. This requires that certain sectors are aligned to cluster
751     * borders.
752     */
753    if (is_cluster_aligned(vol, vol->data_fsec)
754        && (FAT_FAT32 == vol->type || is_cluster_aligned(vol, vol->rdir_loc)))
755    {
756        sc = rtems_bdbuf_set_block_size (vol->dd, vol->bpc, true);
757        if (sc == RTEMS_SUCCESSFUL)
758        {
759            vol->bytes_per_block = vol->bpc;
760            vol->bytes_per_block_log2 = vol->bpc_log2;
761            vol->sectors_per_block = vol->spc;
762        }
763    }
764
765    return RC_OK;
766}
767
768/* fat_fat32_update_fsinfo_sector --
769 *     Synchronize fsinfo sector for FAT32 volumes
770 *
771 * PARAMETERS:
772 *     fs_info    - FS info
773 *
774 * RETURNS:
775 *     RC_OK on success, or -1 if error occured (errno set appropriately)
776 */
777static int
778fat_fat32_update_fsinfo_sector(fat_fs_info_t *fs_info)
779{
780    ssize_t ret1 = 0, ret2 = 0;
781
782    if (fs_info->vol.type == FAT_FAT32)
783    {
784        uint32_t free_count = fs_info->vol.free_cls;
785        uint32_t next_free = fs_info->vol.next_cl;
786
787        if (free_count != fs_info->vol.free_cls_in_fs_info)
788        {
789            uint32_t le_free_count = CT_LE_L(free_count);
790
791            fs_info->vol.free_cls_in_fs_info = free_count;
792
793            ret1 = fat_sector_write(fs_info,
794                                    fs_info->vol.info_sec,
795                                    FAT_FSINFO_FREE_CLUSTER_COUNT_OFFSET,
796                                    sizeof(le_free_count),
797                                    &le_free_count);
798        }
799
800        if (next_free != fs_info->vol.next_cl_in_fs_info)
801        {
802            uint32_t le_next_free = CT_LE_L(next_free);
803
804            fs_info->vol.next_cl_in_fs_info = next_free;
805
806            ret2 = fat_sector_write(fs_info,
807                                    fs_info->vol.info_sec,
808                                    FAT_FSINFO_NEXT_FREE_CLUSTER_OFFSET,
809                                    sizeof(le_next_free),
810                                    &le_next_free);
811        }
812    }
813
814    if ( (ret1 < 0) || (ret2 < 0) )
815        return -1;
816
817    return RC_OK;
818}
819
820int
821fat_sync(fat_fs_info_t *fs_info)
822{
823    int rc = RC_OK;
824
825    rc = fat_fat32_update_fsinfo_sector(fs_info);
826    if ( rc != RC_OK )
827        rc = -1;
828
829    fat_buf_release(fs_info);
830
831    if (rtems_bdbuf_syncdev(fs_info->vol.dd) != RTEMS_SUCCESSFUL)
832        rc = -1;
833
834    return rc;
835}
836
837/* fat_shutdown_drive --
838 *     Free all allocated resources and synchronize all necessary data
839 *
840 * PARAMETERS:
841 *     fs_info  - FS info
842 *
843 * RETURNS:
844 *     RC_OK on success, or -1 if error occured
845 *     and errno set appropriately
846 */
847int
848fat_shutdown_drive(fat_fs_info_t *fs_info)
849{
850    int            rc = RC_OK;
851    int            i = 0;
852
853    rc = fat_sync(fs_info);
854    if ( rc != RC_OK )
855        rc = -1;
856
857    for (i = 0; i < FAT_HASH_SIZE; i++)
858    {
859        rtems_chain_node    *node = NULL;
860        rtems_chain_control *the_chain = fs_info->vhash + i;
861
862        while ( (node = rtems_chain_get_unprotected(the_chain)) != NULL )
863            free(node);
864    }
865
866    for (i = 0; i < FAT_HASH_SIZE; i++)
867    {
868        rtems_chain_node    *node = NULL;
869        rtems_chain_control *the_chain = fs_info->rhash + i;
870
871        while ( (node = rtems_chain_get_unprotected(the_chain)) != NULL )
872            free(node);
873    }
874
875    free(fs_info->vhash);
876    free(fs_info->rhash);
877
878    free(fs_info->uino);
879    free(fs_info->sec_buf);
880    close(fs_info->vol.fd);
881
882    if (rc)
883        errno = EIO;
884    return rc;
885}
886
887/* fat_init_clusters_chain --
888 *     Zeroing contents of all clusters in the chain
889 *
890 * PARAMETERS:
891 *     fs_info           - FS info
892 *     start_cluster_num - num of first cluster in the chain
893 *
894 * RETURNS:
895 *     RC_OK on success, or -1 if error occured
896 *     and errno set appropriately
897 */
898int
899fat_init_clusters_chain(
900    fat_fs_info_t                        *fs_info,
901    uint32_t                              start_cln
902    )
903{
904    int                     rc = RC_OK;
905    ssize_t                 ret = 0;
906    uint32_t                cur_cln = start_cln;
907
908    while ((cur_cln & fs_info->vol.mask) < fs_info->vol.eoc_val)
909    {
910        ret = fat_cluster_set(fs_info, cur_cln, 0, fs_info->vol.bpc, 0);
911        if ( ret != fs_info->vol.bpc )
912        {
913            return -1;
914        }
915
916        rc  = fat_get_fat_cluster(fs_info, cur_cln, &cur_cln);
917        if ( rc != RC_OK )
918        {
919            return rc;
920        }
921
922    }
923
924    return rc;
925}
926
927#define FAT_UNIQ_INO_BASE 0x0FFFFF00
928
929#define FAT_UNIQ_INO_IS_BUSY(index, arr) \
930  (((arr)[((index)>>3)]>>((index) & (8-1))) & 0x01)
931
932#define FAT_SET_UNIQ_INO_BUSY(index, arr) \
933  ((arr)[((index)>>3)] |= (0x01<<((index) & (8-1))))
934
935#define FAT_SET_UNIQ_INO_FREE(index, arr) \
936  ((arr)[((index)>>3)] &= (~(0x01<<((index) & (8-1)))))
937
938/* fat_get_unique_ino --
939 *     Allocate unique ino from unique ino pool
940 *
941 * PARAMETERS:
942 *     fs_info  - FS info
943 *
944 * RETURNS:
945 *     unique inode number on success, or 0 if there is no free unique inode
946 *     number in the pool
947 *
948 * ATTENTION:
949 *     0 means FAILED !!!
950 *
951 */
952uint32_t
953fat_get_unique_ino(fat_fs_info_t *fs_info)
954{
955    uint32_t                j = 0;
956    bool                    resrc_unsuff = false;
957
958    while (!resrc_unsuff)
959    {
960        for (j = 0; j < fs_info->uino_pool_size; j++)
961        {
962            if (!FAT_UNIQ_INO_IS_BUSY(fs_info->index, fs_info->uino))
963            {
964                FAT_SET_UNIQ_INO_BUSY(fs_info->index, fs_info->uino);
965                return (fs_info->uino_base + fs_info->index);
966            }
967            fs_info->index++;
968            if (fs_info->index >= fs_info->uino_pool_size)
969                fs_info->index = 0;
970        }
971
972        if ((fs_info->uino_pool_size << 1) < (0x0FFFFFFF - fs_info->uino_base))
973        {
974            fs_info->uino_pool_size <<= 1;
975            fs_info->uino = realloc(fs_info->uino, fs_info->uino_pool_size);
976            if (fs_info->uino != NULL)
977                fs_info->index = fs_info->uino_pool_size;
978            else
979                resrc_unsuff = true;
980        }
981        else
982            resrc_unsuff = true;
983    }
984    return 0;
985}
986
987/* fat_free_unique_ino --
988 *     Return unique ino to unique ino pool
989 *
990 * PARAMETERS:
991 *     fs_info  - FS info
992 *     ino      - inode number to free
993 *
994 * RETURNS:
995 *     None
996 */
997void
998fat_free_unique_ino(
999    fat_fs_info_t                        *fs_info,
1000    uint32_t                              ino
1001    )
1002{
1003    FAT_SET_UNIQ_INO_FREE((ino - fs_info->uino_base), fs_info->uino);
1004}
1005
1006/* fat_ino_is_unique --
1007 *     Test whether ino is from unique ino pool
1008 *
1009 * PARAMETERS:
1010 *     fs_info  - FS info
1011 *     ino   - ino to be tested
1012 *
1013 * RETURNS:
1014 *     true if ino is allocated from unique ino pool, false otherwise
1015 */
1016inline bool
1017fat_ino_is_unique(
1018    fat_fs_info_t                        *fs_info,
1019    uint32_t                              ino
1020    )
1021{
1022
1023    return (ino >= fs_info->uino_base);
1024}
Note: See TracBrowser for help on using the repository browser.