MS-DOS File System questions - partitions, mounting, files

Chris Johns chrisj at rtems.org
Wed Oct 15 00:41:57 UTC 2008


Robert S. Grimes wrote:
> I'm trying to use the MSDOS file system on an SD card, and I think I've 
> got the hardware and driver issues sorted out.  Now, I'm pretty much 
> stuck with a lack of understanding on my part on how to use it!  I'm 
> almost certain my problem is my weak understanding of Unix filesystems.
> 
> I start with an SD card formatted in Windows XP as a FAT disk, and place 
> the following contents on it:
> 
>     README.TXT
>     dirone/
>         README1.txt
>     dirtwo/
>         README2.txt
> 
> Here is my code, with comments used for questions:
> 
>     // This stuff (apparently) works - shown for completeness
>     sc = rtems_ide_part_table_initialize( SDCARD_DEVICE_FILE);
>     CHECK_SC( sc, "Initialize IDE partition table");
>     rv = mkdir( SDCARD_MOUNT_POINT, S_IRWXU);

Is SDCARD_MOUNT_POINT defined as "/mnt" ?

>     CHECK_RVSC( rv, "Create mount point");
>     rv = rtems_fsmount(fsTable, sizeof(fsTable) / sizeof(fsTable[0]), NULL);
>     CHECK_RVSC( rv, "Mount file systems");

What is in the fsmount table ?

This looks like it mounts the SD disk ok but I cannot see the options used.

> 
>     // Here I use a function from Sebastian for printing directories -
>     seems correct
>     printf("\nPrint /dev directory\n");
>     printf("Returned %d\n", printDir( "/dev", 0));
>     printf("\nPrint /mnt directory\n");
>     printf("Returned %d\n\n", printDir( "/mnt", 0));
> 

Looks ok.

>     // According to Chris' Wiki entry on MSDOS, I need to do this, but
>     it fails with a "Not supported"
>     // error.  Questions:
>     //
>     //   1. I'm not sure what to do with the mtEntry parameter - is it
>     used for other functions?
>     //   2. I'm not sure about "/mnt/dirone". As I said above, there is
>     a directory on my SD card
>     //      called dirone, but I'm not sure that I'm doing things
>     correctly.  I guess my real question
>     //      is: How do I mount the SD card such that its entire contents
>     are available to my app?
>     rtems_filesystem_mount_table_entry_t* mtEntry;
>     if (mount(&mtEntry, &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
>     "/dev/sdcarda1", "/mnt/dirone") < 0) {
>       printf("mount: mount failed: %s\n", strerror (errno));
>     }

I think this call fails because you have already mounted the SD card under 
/mnt and this call attempts to mount it again on an MSDOS file system and I 
think mounting is not supported on the MSDOS file system. This is the source 
of the not supported error.

You only need to mount the file system once so you either mount with the 
fsmount command or the mount command and not both. There is no correct or 
better solution here so you can decide which one best suites your needs.

> 
>     // Interestingly, this works just fine, even with the previous mount...
>     int readme = open("/mnt/README.TXT", O_RDONLY);
>     char linebuf[32];
>     linebuf[0] = '*';
>     linebuf[1] = 0;
>     rv = read(readme, linebuf, 27);
>     if (rv > 0) {
>       linebuf[rv] = 0;
>       printf("Readme contains: [%s]\n", linebuf);
>     } else {
>       printf("Failed to read file - return code: %d\n", rv);
>     }
>     close(readme);

This shows the SD card is mounted. Great.

> 
>     // This doesn't work fully, though the mkdir sometimes works enough
>     that Windows will see
>     // the new directory; when it does, it is sometimes okay, sometimes
>     corrupted.  Questions:
>     //   1. Is this the correct way to do it?
>     //   2. Is the mount() failure the cause of this one?

The "sometimes works" could be important. More on this in a moment.

The partition needs to be mounted read/write. If mounted read only then these 
actions fail. The fsmount table will hold the options used.

The "sometimes works" could point to the application halting before the cache 
has written all the data to disk. The cache has a hold time where buffers wait 
a configurable period of time before being written to disk. This improves 
performance with the MSDOS file system. It may how-ever not sit well with 
specific applications therefore the 'sync' call is provided. The standard call 
  only covers the open files and not the RTEMS specific cache. The cache also 
needs to be flushed. You can find an example of how do this in the shell in 
the blksync command. The file is cpukit/libmisc/shell/main_blksync.c. The 
device is the device you mounted, ie /dev/sdcarda.

The rule is simple. If your applications needs the data to be on disk then 
sync the file system buffers (sync) and sync the block device cache before it 
terminates.

If you have not waited until the data has been written the disk could be 
corrupted and needs to be fixed or reformatted.

Regards
Chris

>     printf("\nMake a directory and move to it\n");
>     printf("Returned: %d\n", mkdir( "/mnt/testdir3", S_IRWXU));
>     printf("printDir returned %d\n", printDir( "/mnt/testdir3", 0));
>     printf("Change directory returned %d\n", chdir( "/mnt/testdir3"));
> 
>     // This doesn't work, but maybe that is because the mount() fails? 
>     If so, would this be correct
>     // if mount() had succeeded?
>     int writeme = open("newfile.txt", O_WRONLY);
>     printf("\nCreating newfile.txt, returned %d\n", writeme);
>     if (writeme > 0) {
>       sprintf(linebuf, "Hello, world!\n");
>       write(writeme, linebuf, strlen(linebuf)+1);
>       close(writeme);
>     }
> 
>     // Same question as previous open()
>     writeme = open("/mnt/newfile.txt", O_WRONLY);
>     printf("\nCreating /mnt/newfile.txt, returned %d\n", writeme);
>     if (writeme > 0) {
>       sprintf(linebuf, "Hello, world!\n");
>       write(writeme, linebuf, strlen(linebuf)+1);
>       close(writeme);
>     }
> 
> 
> Here is the output from the above code:
> 
>     Print /dev directory
>     <console>
>     <spi0.sdcarda>
>     <sdcarda>
>     <sdcarda1>
>     Returned 0
> 
>     Print /mnt directory
>     <README.TXT>
>     <DIRONE>
>             <README1.TXT>
>     <DIRTWO>
>             <README2.TXT>
>     <TESTDIR3>
>     Returned 0
> 
>     mount: mount failed: Not supported
>     Readme contains: [This is a simple text file.]
> 
>     Make a directory and move to it
>     Returned: -1
>     printDir returned 0
>     Change directory returned 0
> 
>     Creating newfile.txt, returned -1
> 
>     Creating /mnt/newfile.txt, returned -1
> 
> Thanks!
> -Bob
> 
> 
> 
> _______________________________________________
> rtems-users mailing list
> rtems-users at rtems.com
> http://rtems.rtems.org/mailman/listinfo/rtems-users



More information about the users mailing list