1 #ifndef CYGONCE_FILEIO_H
2 #define CYGONCE_FILEIO_H
3 //=============================================================================
9 //=============================================================================
10 //####ECOSGPLCOPYRIGHTBEGIN####
11 // -------------------------------------------
12 // This file is part of eCos, the Embedded Configurable Operating System.
13 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
15 // eCos is free software; you can redistribute it and/or modify it under
16 // the terms of the GNU General Public License as published by the Free
17 // Software Foundation; either version 2 or (at your option) any later version.
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 // You should have received a copy of the GNU General Public License along
25 // with eCos; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 // As a special exception, if other files instantiate templates or use macros
29 // or inline functions from this file, or you compile this file and link it
30 // with other works to produce a work based on this file, this file does not
31 // by itself cause the resulting work to be covered by the GNU General Public
32 // License. However the source code for this file must still be made available
33 // in accordance with section (3) of the GNU General Public License.
35 // This exception does not invalidate any other reasons why a work based on
36 // this file might be covered by the GNU General Public License.
38 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
39 // at http://sources.redhat.com/ecos/ecos-license/
40 // -------------------------------------------
41 //####ECOSGPLCOPYRIGHTEND####
42 //=============================================================================
43 //#####DESCRIPTIONBEGIN####
46 // Contributors: nickg
48 // Purpose: Fileio header
49 // Description: This header contains the external definitions of the general file
50 // IO subsystem for POSIX and EL/IX compatability.
53 // #include <fileio.h>
57 //####DESCRIPTIONEND####
59 //=============================================================================
61 #include <pkgconf/hal.h>
62 #include <pkgconf/io_fileio.h>
64 #include <cyg/infra/cyg_type.h>
65 #include <cyg/hal/hal_tables.h>
66 #ifdef CYGFUN_IO_FILEIO_SELECT
67 #include <cyg/kernel/kapi.h>
70 #include <stddef.h> // NULL, size_t
72 #include <sys/types.h>
76 //=============================================================================
77 // forward definitions
79 struct cyg_mtab_entry;
80 typedef struct cyg_mtab_entry cyg_mtab_entry;
82 struct cyg_fstab_entry;
83 typedef struct cyg_fstab_entry cyg_fstab_entry;
85 struct CYG_FILEOPS_TAG;
86 typedef struct CYG_FILEOPS_TAG cyg_fileops;
89 typedef struct CYG_FILE_TAG cyg_file;
92 typedef struct CYG_IOVEC_TAG cyg_iovec;
95 typedef struct CYG_UIO_TAG cyg_uio;
97 struct CYG_SELINFO_TAG;
98 typedef struct CYG_SELINFO_TAG cyg_selinfo;
100 //=============================================================================
103 typedef CYG_ADDRWORD cyg_dir;
105 #define CYG_DIR_NULL 0
107 //=============================================================================
108 // Filesystem table entry
110 typedef int cyg_fsop_mount ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
111 typedef int cyg_fsop_umount ( cyg_mtab_entry *mte );
112 typedef int cyg_fsop_open ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
113 int mode, cyg_file *fte );
114 typedef int cyg_fsop_unlink ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
115 typedef int cyg_fsop_mkdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
116 typedef int cyg_fsop_rmdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
117 typedef int cyg_fsop_rename ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
118 cyg_dir dir2, const char *name2 );
119 typedef int cyg_fsop_link ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
120 cyg_dir dir2, const char *name2, int type );
121 typedef int cyg_fsop_opendir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
123 typedef int cyg_fsop_chdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
125 typedef int cyg_fsop_stat ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
127 typedef int cyg_fsop_getinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
128 int key, void *buf, int len );
129 typedef int cyg_fsop_setinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
130 int key, void *buf, int len );
133 struct cyg_fstab_entry
135 const char *name; // filesystem name
136 CYG_ADDRWORD data; // private data value
137 cyg_uint32 syncmode; // synchronization mode
139 cyg_fsop_mount *mount;
140 cyg_fsop_umount *umount;
142 cyg_fsop_unlink *unlink;
143 cyg_fsop_mkdir *mkdir;
144 cyg_fsop_rmdir *rmdir;
145 cyg_fsop_rename *rename;
147 cyg_fsop_opendir *opendir;
148 cyg_fsop_chdir *chdir;
150 cyg_fsop_getinfo *getinfo;
151 cyg_fsop_setinfo *setinfo;
152 } CYG_HAL_TABLE_TYPE;
154 //-----------------------------------------------------------------------------
155 // Keys for getinfo() and setinfo()
157 #define FS_INFO_CONF 1 /* pathconf() */
158 #define FS_INFO_ACCESS 2 /* access() */
159 #define FS_INFO_GETCWD 3 /* getcwd() */
160 #define FS_INFO_SYNC 4 /* cyg_fs_fssync() */
161 #define FS_INFO_ATTRIB 5 /* cyg_fs_(get|set)_attrib() */
162 #ifdef CYGSEM_FILEIO_INFO_DISK_USAGE
163 #define FS_INFO_DISK_USAGE 6 /* get_disk_usage() */
165 //-----------------------------------------------------------------------------
168 #define CYG_FSLINK_HARD 1 /* form a hard link */
169 #define CYG_FSLINK_SOFT 2 /* form a soft link */
171 //-----------------------------------------------------------------------------
172 // getinfo() and setinfo() buffers structures.
174 struct cyg_getcwd_info
176 char *buf; /* buffer for cwd string */
177 size_t size; /* size of buffer */
180 struct cyg_fs_disk_usage{
181 cyg_uint64 total_blocks;
182 cyg_uint64 free_blocks;
183 cyg_uint32 block_size;
186 typedef cyg_uint32 cyg_fs_attrib_t;
188 //-----------------------------------------------------------------------------
189 // Macro to define an initialized fstab entry
191 #define FSTAB_ENTRY( _l, _name, _data, _syncmode, _mount, _umount, \
192 _open, _unlink, _mkdir, _rmdir, _rename, _link, \
193 _opendir, _chdir, _stat, _getinfo, _setinfo) \
194 struct cyg_fstab_entry _l CYG_HAL_TABLE_ENTRY(fstab) = \
214 //=============================================================================
217 struct cyg_mtab_entry
219 const char *name; // name of mount point
220 const char *fsname; // name of implementing filesystem
221 const char *devname; // name of hardware device
222 CYG_ADDRWORD data; // private data value
224 // The following are filled in after a successful mount operation
225 cyg_bool valid; // Valid entry?
226 cyg_fstab_entry *fs; // pointer to fstab entry
227 cyg_dir root; // root directory pointer
228 } CYG_HAL_TABLE_TYPE;
231 // This macro defines an initialized mtab entry
233 #define MTAB_ENTRY( _l, _name, _fsname, _devname, _data ) \
234 struct cyg_mtab_entry _l CYG_HAL_TABLE_ENTRY(mtab) = \
245 //=============================================================================
246 // IO vector descriptors
250 void *iov_base; /* Base address. */
251 ssize_t iov_len; /* Length. */
254 enum cyg_uio_rw { UIO_READ, UIO_WRITE };
256 /* Segment flag values. */
259 UIO_USERSPACE, /* from user data space */
260 UIO_SYSSPACE /* from system space */
265 struct CYG_IOVEC_TAG *uio_iov; /* pointer to array of iovecs */
266 int uio_iovcnt; /* number of iovecs in array */
267 off_t uio_offset; /* offset into file this uio corresponds to */
268 ssize_t uio_resid; /* residual i/o count */
269 enum cyg_uio_seg uio_segflg; /* see above */
270 enum cyg_uio_rw uio_rw; /* see above */
274 #define UIO_SMALLIOV 8 /* 8 on stack, else malloc */
276 //=============================================================================
277 // Description of open file
279 typedef int cyg_fileop_readwrite (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
280 typedef cyg_fileop_readwrite cyg_fileop_read;
281 typedef cyg_fileop_readwrite cyg_fileop_write;
282 typedef int cyg_fileop_lseek (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
283 typedef int cyg_fileop_ioctl (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
285 typedef cyg_bool cyg_fileop_select (struct CYG_FILE_TAG *fp, int which, CYG_ADDRWORD info);
286 typedef int cyg_fileop_fsync (struct CYG_FILE_TAG *fp, int mode );
287 typedef int cyg_fileop_close (struct CYG_FILE_TAG *fp);
288 typedef int cyg_fileop_fstat (struct CYG_FILE_TAG *fp, struct stat *buf );
289 typedef int cyg_fileop_getinfo (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
290 typedef int cyg_fileop_setinfo (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
292 struct CYG_FILEOPS_TAG
294 cyg_fileop_read *fo_read;
295 cyg_fileop_write *fo_write;
296 cyg_fileop_lseek *fo_lseek;
297 cyg_fileop_ioctl *fo_ioctl;
298 cyg_fileop_select *fo_select;
299 cyg_fileop_fsync *fo_fsync;
300 cyg_fileop_close *fo_close;
301 cyg_fileop_fstat *fo_fstat;
302 cyg_fileop_getinfo *fo_getinfo;
303 cyg_fileop_setinfo *fo_setinfo;
308 cyg_uint32 f_flag; /* file state */
309 cyg_uint16 f_ucount; /* use count */
310 cyg_uint16 f_type; /* descriptor type */
311 cyg_uint32 f_syncmode; /* synchronization protocol */
312 struct CYG_FILEOPS_TAG *f_ops; /* file operations */
313 off_t f_offset; /* current offset */
314 CYG_ADDRWORD f_data; /* file or socket */
315 CYG_ADDRWORD f_xops; /* extra type specific ops */
316 cyg_mtab_entry *f_mte; /* mount table entry */
319 //-----------------------------------------------------------------------------
322 // Allocation here is that bits 0..15 are copies of bits from the open
323 // flags, bits 16..23 are extra bits that are visible to filesystems but
324 // are not derived from the open call, and bits 24..31 are reserved for
325 // the fileio infrastructure.
326 #define CYG_FREAD O_RDONLY
327 #define CYG_FWRITE O_WRONLY
328 #define CYG_FNONBLOCK O_NONBLOCK
329 #define CYG_FAPPEND O_APPEND
330 #define CYG_FASYNC 0x00010000
331 #define CYG_FDIR 0x00020000
333 #define CYG_FLOCKED 0x01000000 // Set if file is locked
334 #define CYG_FLOCK 0x02000000 // Lock during file ops
335 #define CYG_FALLOC 0x80000000 // File is "busy", i.e. allocated
337 // Mask for open mode bits stored in file object
338 #define CYG_FILE_MODE_MASK (CYG_FREAD|CYG_FWRITE|CYG_FNONBLOCK|CYG_FAPPEND)
340 //-----------------------------------------------------------------------------
343 #define CYG_FILE_TYPE_FILE 1 /* file */
344 #define CYG_FILE_TYPE_SOCKET 2 /* communications endpoint */
345 #define CYG_FILE_TYPE_DEVICE 3 /* device */
347 //-----------------------------------------------------------------------------
348 // Keys for getinfo() and setinfo()
350 #define FILE_INFO_CONF 1 /* fpathconf() */
352 //-----------------------------------------------------------------------------
356 #define CYG_FDATASYNC 2
358 //-----------------------------------------------------------------------------
359 // Get/set info buffer structures
361 // This is used for pathconf() and fpathconf()
362 struct cyg_pathconf_info
364 int name; // POSIX defined variable name
365 long value; // Returned variable value
368 //=============================================================================
369 // Synchronization modes
370 // These values are filled into the syncmode fields of the above structures
371 // and define the synchronization protocol used when accessing the object in
374 #define CYG_SYNCMODE_NONE (0) // no locking required
376 #define CYG_SYNCMODE_FILE_FILESYSTEM 0x0002 // lock fs during file ops
377 #define CYG_SYNCMODE_FILE_MOUNTPOINT 0x0004 // lock mte during file ops
378 #define CYG_SYNCMODE_IO_FILE 0x0010 // lock file during io ops
379 #define CYG_SYNCMODE_IO_FILESYSTEM 0x0020 // lock fs during io ops
380 #define CYG_SYNCMODE_IO_MOUNTPOINT 0x0040 // lock mte during io ops
381 #define CYG_SYNCMODE_SOCK_FILE 0x0100 // lock socket during socket ops
382 #define CYG_SYNCMODE_SOCK_NETSTACK 0x0800 // lock netstack during socket ops
384 #define CYG_SYNCMODE_IO_SHIFT (4) // shift for IO to file bits
385 #define CYG_SYNCMODE_SOCK_SHIFT (8) // shift for sock to file bits
387 //=============================================================================
388 // Mount and umount functions
390 __externC int mount( const char *devname,
394 __externC int umount( const char *name);
396 //=============================================================================
397 // Get/Set info functions
399 __externC int cyg_fs_getinfo( const char *path, int key, void *buf, int len );
400 __externC int cyg_fs_setinfo( const char *path, int key, void *buf, int len );
401 __externC int cyg_fs_fgetinfo( int fd, int key, void *buf, int len );
402 __externC int cyg_fs_fsetinfo( int fd, int key, void *buf, int len );
404 #ifdef CYGFUN_IO_FILEIO_SELECT
405 //=============================================================================
408 //-----------------------------------------------------------------------------
409 // Data structure for embedding in client data structures. A pointer to this
410 // must be passed to cyg_selrecord() and cyg_selwakeup().
412 struct CYG_SELINFO_TAG
414 CYG_ADDRWORD si_info; // info passed through from fo_select()
415 cyg_flag_value_t si_waitFlag; // select wait flags
418 //-----------------------------------------------------------------------------
419 // Select support functions.
421 // cyg_selinit() is used to initialize a selinfo structure.
422 __externC void cyg_selinit( struct CYG_SELINFO_TAG *sip );
424 // cyg_selrecord() is called when a client device needs to register
425 // the current thread for selection.
426 __externC void cyg_selrecord( CYG_ADDRWORD info, struct CYG_SELINFO_TAG *sip );
428 // cyg_selwakeup() is called when the client device matches the select
429 // criterion, and needs to wake up a selector.
430 __externC void cyg_selwakeup( struct CYG_SELINFO_TAG *sip );
432 //=============================================================================
435 // Provides the current time as a time_t timestamp for use in filesystem
438 __externC time_t cyg_timestamp(void);
440 //=============================================================================
441 // Miscellaneous functions.
443 // Provide a function to synchronize an individual file system. (ie write
444 // file and directory information to disk)
445 __externC int cyg_fs_fssync(const char *path);
447 // Functions to set and get attributes of a file, eg FAT attributes
448 // like hidden and system.
449 __externC int cyg_fs_set_attrib( const char *fname,
450 const cyg_fs_attrib_t new_attrib );
451 __externC int cyg_fs_get_attrib( const char *fname,
452 cyg_fs_attrib_t * const file_attrib );
454 // Functions to lock and unlock a filesystem. These are normally used
455 // internally by the fileio layer, but the file system might need to
456 // use them when it needs to lock itself, eg when performing garbage
458 __externC void cyg_fs_lock( cyg_mtab_entry *mte, cyg_uint32 syncmode );
460 __externC void cyg_fs_unlock( cyg_mtab_entry *mte, cyg_uint32 syncmode );
462 // To be able to lock the filesystem you need the mte. This function
463 // allows the table of mounted filesystems to be searched to find an
464 // mte which uses the give filesystem root.
466 __externC cyg_mtab_entry * cyg_fs_root_lookup( cyg_dir *root );
468 //=============================================================================
469 // Default functions.
470 // Cast to the appropriate type, these functions can be put into any of
471 // the operation table slots to provide the defined error code.
473 __externC int cyg_fileio_enosys(void);
474 __externC int cyg_fileio_erofs(void);
475 __externC int cyg_fileio_enoerr(void);
476 __externC int cyg_fileio_enotdir(void);
477 __externC cyg_fileop_select cyg_fileio_seltrue;
479 //-----------------------------------------------------------------------------
480 #endif // ifndef CYGONCE_FILEIO_H