1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
12 >Writing a New Filesystem</TITLE
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="File System Support Infrastructure"
23 HREF="fileio.html"><LINK
26 HREF="fileio-devices.html"><LINK
29 HREF="io-pci.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="fileio-devices.html"
84 NAME="FILEIO-WRITING">Chapter 29. Writing a New Filesystem</H1
86 >To create a new filesystem it is necessary to define the fstab entry
87 and the file IO operations. The easiest way to do this is to copy an
88 existing filesystem: either the test filesystem in the FILEIO package,
89 or the RAM or ROM filesystem packages.</P
91 >To make this clearer, the following is a brief tour of the FILEIO
92 relevant parts of the RAM filesystem.</P
94 >First, it is necessary to provide forward definitions of the functions
95 that constitute the filesystem interface:</P
103 CLASS="PROGRAMLISTING"
104 >//==========================================================================
105 // Forward definitions
107 // Filesystem operations
108 static int ramfs_mount ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
109 static int ramfs_umount ( cyg_mtab_entry *mte );
110 static int ramfs_open ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
111 int mode, cyg_file *fte );
112 static int ramfs_unlink ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
113 static int ramfs_mkdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
114 static int ramfs_rmdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
115 static int ramfs_rename ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
116 cyg_dir dir2, const char *name2 );
117 static int ramfs_link ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
118 cyg_dir dir2, const char *name2, int type );
119 static int ramfs_opendir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
121 static int ramfs_chdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
123 static int ramfs_stat ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
125 static int ramfs_getinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
126 int key, void *buf, int len );
127 static int ramfs_setinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
128 int key, void *buf, int len );
131 static int ramfs_fo_read (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
132 static int ramfs_fo_write (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
133 static int ramfs_fo_lseek (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
134 static int ramfs_fo_ioctl (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
136 static int ramfs_fo_fsync (struct CYG_FILE_TAG *fp, int mode );
137 static int ramfs_fo_close (struct CYG_FILE_TAG *fp);
138 static int ramfs_fo_fstat (struct CYG_FILE_TAG *fp, struct stat *buf );
139 static int ramfs_fo_getinfo (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
140 static int ramfs_fo_setinfo (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
142 // Directory operations
143 static int ramfs_fo_dirread (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
144 static int ramfs_fo_dirlseek (struct CYG_FILE_TAG *fp, off_t *pos, int whence );</PRE
149 >We define all of the fstab entries and all of the file IO
150 operations. We also define alternatives for the
162 > file IO operations.</P
164 >We can now define the filesystem table entry. There is a macro,
176 CLASS="PROGRAMLISTING"
177 >//==========================================================================
178 // Filesystem table entries
180 // -------------------------------------------------------------------------
182 // This defines the entry in the filesystem table.
183 // For simplicity we use _FILESYSTEM synchronization for all accesses since
184 // we should never block in any filesystem operations.
186 FSTAB_ENTRY( ramfs_fste, "ramfs", 0,
187 CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
205 >The first argument to this macro gives the fstab entry a name, the
206 remainder are initializers for the field of the structure.</P
208 >We must also define the file operations table that is installed in all
209 open file table entries:</P
217 CLASS="PROGRAMLISTING"
218 >// -------------------------------------------------------------------------
220 // This set of file operations are used for normal open files.
222 static cyg_fileops ramfs_fileops =
239 >These all point to functions supplied by the filesystem except the
245 > field which is filled with a
248 >cyg_fileio_seltrue()</TT
250 by the FILEIO package and is a select function that always returns
251 true to all operations.</P
253 >Finally, we need to define a set of file operations for use when
254 reading directories. This table only defines the
266 > operations. The rest are filled
267 with stub functions supplied by the FILEIO package that just return an
276 CLASS="PROGRAMLISTING"
277 >// -------------------------------------------------------------------------
278 // Directory file operations.
279 // This set of operations are used for open directories. Most entries
280 // point to error-returning stub functions. Only the read, lseek and
281 // close entries are functional.
283 static cyg_fileops ramfs_dirops =
286 (cyg_fileop_write *)cyg_fileio_enosys,
288 (cyg_fileop_ioctl *)cyg_fileio_enosys,
290 (cyg_fileop_fsync *)cyg_fileio_enosys,
292 (cyg_fileop_fstat *)cyg_fileio_enosys,
293 (cyg_fileop_getinfo *)cyg_fileio_enosys,
294 (cyg_fileop_setinfo *)cyg_fileio_enosys
300 >If the filesystem wants to have an instance automatically mounted on
301 system startup, it must also define a mount table entry. This is done
305 > macro. This is an example from
306 the test filesystem of how this is used:</P
314 CLASS="PROGRAMLISTING"
315 >MTAB_ENTRY( testfs_mte1,
324 >The first argument provides a name for the table entry. The following
325 arguments provide initialization for the
348 fields respectively.</P
350 >These definitions are adequate to let the new filesystem interact
351 with the FILEIO package. The new filesystem now needs to be fleshed
352 out with implementations of the functions defined above. Obviously,
353 the exact form this takes will depend on what the filesystem is
354 intended to do. Take a look at the RAM and ROM filesystems for
355 examples of how this has been done.</P
362 SUMMARY="Footer navigation table"
373 HREF="fileio-devices.html"