fileio.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #ifndef CYGONCE_FILEIO_H
  2. #define CYGONCE_FILEIO_H
  3. //=============================================================================
  4. //
  5. // fileio.h
  6. //
  7. // Fileio header
  8. //
  9. //=============================================================================
  10. // ####ECOSGPLCOPYRIGHTBEGIN####
  11. // -------------------------------------------
  12. // This file is part of eCos, the Embedded Configurable Operating System.
  13. // Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  14. //
  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
  18. // version.
  19. //
  20. // eCos is distributed in the hope that it will be useful, but WITHOUT
  21. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  22. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23. // for more details.
  24. //
  25. // You should have received a copy of the GNU General Public License
  26. // along with eCos; if not, write to the Free Software Foundation, Inc.,
  27. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  28. //
  29. // As a special exception, if other files instantiate templates or use
  30. // macros or inline functions from this file, or you compile this file
  31. // and link it with other works to produce a work based on this file,
  32. // this file does not by itself cause the resulting work to be covered by
  33. // the GNU General Public License. However the source code for this file
  34. // must still be made available in accordance with section (3) of the GNU
  35. // General Public License v2.
  36. //
  37. // This exception does not invalidate any other reasons why a work based
  38. // on this file might be covered by the GNU General Public License.
  39. // -------------------------------------------
  40. // ####ECOSGPLCOPYRIGHTEND####
  41. //=============================================================================
  42. //#####DESCRIPTIONBEGIN####
  43. //
  44. // Author(s): nickg
  45. // Contributors: nickg
  46. // Date: 2000-05-25
  47. // Purpose: Fileio header
  48. // Description: This header contains the external definitions of the general file
  49. // IO subsystem for POSIX and EL/IX compatability.
  50. //
  51. // Usage:
  52. // #include <fileio.h>
  53. // ...
  54. //
  55. //
  56. //####DESCRIPTIONEND####
  57. //
  58. //=============================================================================
  59. //#include <pkgconf/hal.h>
  60. //#include <pkgconf/io_fileio.h>
  61. //#include <cyg/infra/cyg_type.h>
  62. //#include <cyg/hal/hal_tables.h>
  63. #ifdef CYGFUN_IO_FILEIO_SELECT
  64. #include <cyg/kernel/kapi.h>
  65. #endif
  66. #include <stddef.h> // NULL, size_t
  67. #include <limits.h>
  68. #include <sys/types.h>
  69. #include <fcntl.h>
  70. //#include "os_sys_stat.h"//#include <sys/stat.h>
  71. //=============================================================================
  72. // forward definitions
  73. struct cyg_mtab_entry;
  74. typedef struct cyg_mtab_entry cyg_mtab_entry;
  75. struct cyg_fstab_entry;
  76. typedef struct cyg_fstab_entry cyg_fstab_entry;
  77. struct CYG_FILEOPS_TAG;
  78. typedef struct CYG_FILEOPS_TAG cyg_fileops;
  79. struct CYG_FILE_TAG;
  80. typedef struct CYG_FILE_TAG cyg_file;
  81. struct CYG_IOVEC_TAG;
  82. typedef struct CYG_IOVEC_TAG cyg_iovec;
  83. struct CYG_UIO_TAG;
  84. typedef struct CYG_UIO_TAG cyg_uio;
  85. struct CYG_SELINFO_TAG;
  86. typedef struct CYG_SELINFO_TAG cyg_selinfo;
  87. //=============================================================================
  88. // Directory pointer
  89. typedef CYG_ADDRWORD cyg_dir;
  90. #define CYG_DIR_NULL 0
  91. //=============================================================================
  92. // Filesystem table entry
  93. typedef int cyg_fsop_mount ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
  94. typedef int cyg_fsop_umount ( cyg_mtab_entry *mte );
  95. typedef int cyg_fsop_open ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  96. int mode, cyg_file *fte );
  97. typedef int cyg_fsop_unlink ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
  98. typedef int cyg_fsop_mkdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
  99. typedef int cyg_fsop_rmdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
  100. typedef int cyg_fsop_rename ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
  101. cyg_dir dir2, const char *name2 );
  102. typedef int cyg_fsop_link ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
  103. cyg_dir dir2, const char *name2, int type );
  104. typedef int cyg_fsop_opendir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  105. cyg_file *fte );
  106. typedef int cyg_fsop_chdir ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  107. cyg_dir *dir_out );
  108. typedef int cyg_fsop_stat ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  109. struct stat *buf);
  110. typedef int cyg_fsop_getinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  111. int key, void *buf, int len );
  112. typedef int cyg_fsop_setinfo ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
  113. int key, void *buf, int len );
  114. struct cyg_fstab_entry
  115. {
  116. const char *name; // filesystem name
  117. CYG_ADDRWORD data; // private data value
  118. //#ifdef CYGOPT_FS_JFFS2_GCTHREAD
  119. // struct rt_mutex syncmode; // synchronization mode
  120. //#endif
  121. cyg_uint32 syncmode; // synchronization mode
  122. cyg_fsop_mount *mount;
  123. cyg_fsop_umount *umount;
  124. cyg_fsop_open *open;
  125. cyg_fsop_unlink *unlink;
  126. cyg_fsop_mkdir *mkdir;
  127. cyg_fsop_rmdir *rmdir;
  128. cyg_fsop_rename *rename;
  129. cyg_fsop_link *link;
  130. cyg_fsop_opendir *opendir;
  131. cyg_fsop_chdir *chdir;
  132. cyg_fsop_stat *stat;
  133. cyg_fsop_getinfo *getinfo;
  134. cyg_fsop_setinfo *setinfo;
  135. } ;// CYG_HAL_TABLE_TYPE; //prife
  136. //-----------------------------------------------------------------------------
  137. // Keys for getinfo() and setinfo()
  138. #define FS_INFO_CONF 1 /* pathconf() */
  139. #define FS_INFO_ACCESS 2 /* access() */
  140. #define FS_INFO_GETCWD 3 /* getcwd() */
  141. #define FS_INFO_SYNC 4 /* cyg_fs_fssync() */
  142. #define FS_INFO_ATTRIB 5 /* cyg_fs_(get|set)_attrib() */
  143. #ifdef CYGSEM_FILEIO_INFO_DISK_USAGE
  144. #define FS_INFO_DISK_USAGE 6 /* get_disk_usage() */
  145. #endif
  146. #define FS_INFO_CHMOD 7 /* chmod() */
  147. //-----------------------------------------------------------------------------
  148. // Types for link()
  149. #define CYG_FSLINK_HARD 1 /* form a hard link */
  150. #define CYG_FSLINK_SOFT 2 /* form a soft link */
  151. //-----------------------------------------------------------------------------
  152. // getinfo() and setinfo() buffers structures.
  153. struct cyg_getcwd_info
  154. {
  155. char *buf; /* buffer for cwd string */
  156. size_t size; /* size of buffer */
  157. };
  158. struct cyg_fs_disk_usage{
  159. cyg_uint64 total_blocks;
  160. cyg_uint64 free_blocks;
  161. cyg_uint32 block_size;
  162. };
  163. typedef cyg_uint32 cyg_fs_attrib_t;
  164. //-----------------------------------------------------------------------------
  165. // Macro to define an initialized fstab entry
  166. #define FSTAB_ENTRY( _l, _name, _data, _syncmode, _mount, _umount, \
  167. _open, _unlink, _mkdir, _rmdir, _rename, _link, \
  168. _opendir, _chdir, _stat, _getinfo, _setinfo) \
  169. struct cyg_fstab_entry _l /*CYG_HAL_TABLE_ENTRY(fstab) prife*/ = \
  170. { \
  171. _name, \
  172. _data, \
  173. _syncmode, \
  174. _mount, \
  175. _umount, \
  176. _open, \
  177. _unlink, \
  178. _mkdir, \
  179. _rmdir, \
  180. _rename, \
  181. _link, \
  182. _opendir, \
  183. _chdir, \
  184. _stat, \
  185. _getinfo, \
  186. _setinfo \
  187. };
  188. //=============================================================================
  189. // Mount table entry
  190. struct cyg_mtab_entry
  191. {
  192. const char *name; // name of mount point
  193. const char *fsname; // name of implementing filesystem
  194. const char *devname; // name of hardware device
  195. CYG_ADDRWORD data; // private data value
  196. // The following are filled in after a successful mount operation
  197. cyg_bool valid; // Valid entry?
  198. cyg_fstab_entry *fs; // pointer to fstab entry
  199. cyg_dir root; // root directory pointer
  200. } ; // CYG_HAL_TABLE_TYPE; // prife
  201. // This macro defines an initialized mtab entry
  202. #define MTAB_ENTRY( _l, _name, _fsname, _devname, _data ) \
  203. struct cyg_mtab_entry _l /*CYG_HAL_TABLE_ENTRY(mtab) prife */ = \
  204. { \
  205. _name, \
  206. _fsname, \
  207. _devname, \
  208. _data, \
  209. false, \
  210. NULL, \
  211. CYG_DIR_NULL \
  212. };
  213. //=============================================================================
  214. // IO vector descriptors
  215. struct CYG_IOVEC_TAG
  216. {
  217. void *iov_base; /* Base address. */
  218. ssize_t iov_len; /* Length. */
  219. };
  220. enum cyg_uio_rw { UIO_READ, UIO_WRITE };
  221. /* Segment flag values. */
  222. enum cyg_uio_seg
  223. {
  224. UIO_USERSPACE, /* from user data space */
  225. UIO_SYSSPACE /* from system space */
  226. };
  227. struct CYG_UIO_TAG
  228. {
  229. struct CYG_IOVEC_TAG *uio_iov; /* pointer to array of iovecs */
  230. int uio_iovcnt; /* number of iovecs in array */
  231. off_t uio_offset; /* offset into file this uio corresponds to */
  232. ssize_t uio_resid; /* residual i/o count */
  233. enum cyg_uio_seg uio_segflg; /* see above */
  234. enum cyg_uio_rw uio_rw; /* see above */
  235. };
  236. // Limits
  237. #define UIO_SMALLIOV 8 /* 8 on stack, else malloc */
  238. //=============================================================================
  239. // Description of open file
  240. typedef int cyg_fileop_readwrite (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  241. typedef cyg_fileop_readwrite cyg_fileop_read;
  242. typedef cyg_fileop_readwrite cyg_fileop_write;
  243. typedef int cyg_fileop_lseek (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
  244. typedef int cyg_fileop_ioctl (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
  245. CYG_ADDRWORD data);
  246. typedef cyg_bool cyg_fileop_select (struct CYG_FILE_TAG *fp, int which, CYG_ADDRWORD info);
  247. typedef int cyg_fileop_fsync (struct CYG_FILE_TAG *fp, int mode );
  248. typedef int cyg_fileop_close (struct CYG_FILE_TAG *fp);
  249. typedef int cyg_fileop_fstat (struct CYG_FILE_TAG *fp, struct stat *buf );
  250. typedef int cyg_fileop_getinfo (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
  251. typedef int cyg_fileop_setinfo (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
  252. struct CYG_FILEOPS_TAG
  253. {
  254. cyg_fileop_read *fo_read;
  255. cyg_fileop_write *fo_write;
  256. cyg_fileop_lseek *fo_lseek;
  257. cyg_fileop_ioctl *fo_ioctl;
  258. cyg_fileop_select *fo_select;
  259. cyg_fileop_fsync *fo_fsync;
  260. cyg_fileop_close *fo_close;
  261. cyg_fileop_fstat *fo_fstat;
  262. cyg_fileop_getinfo *fo_getinfo;
  263. cyg_fileop_setinfo *fo_setinfo;
  264. };
  265. struct CYG_FILE_TAG
  266. {
  267. cyg_uint32 f_flag; /* file state */
  268. cyg_uint16 f_ucount; /* use count */
  269. cyg_uint16 f_type; /* descriptor type */
  270. cyg_uint32 f_syncmode; /* synchronization protocol */
  271. struct CYG_FILEOPS_TAG *f_ops; /* file operations */
  272. off_t f_offset; /* current offset */
  273. CYG_ADDRWORD f_data; /* file or socket */
  274. CYG_ADDRWORD f_xops; /* extra type specific ops */
  275. cyg_mtab_entry *f_mte; /* mount table entry */
  276. };
  277. //-----------------------------------------------------------------------------
  278. // File flags
  279. // Allocation here is that bits 0..15 are copies of bits from the open
  280. // flags, bits 16..23 are extra bits that are visible to filesystems but
  281. // are not derived from the open call, and bits 24..31 are reserved for
  282. // the fileio infrastructure.
  283. #define CYG_FREAD O_RDONLY
  284. #define CYG_FWRITE O_WRONLY
  285. #define CYG_FNONBLOCK O_NONBLOCK
  286. #define CYG_FAPPEND O_APPEND
  287. #define CYG_FASYNC 0x00010000
  288. #define CYG_FDIR 0x00020000
  289. #define CYG_FLOCKED 0x01000000 // Set if file is locked
  290. #define CYG_FLOCK 0x02000000 // Lock during file ops
  291. #define CYG_FALLOC 0x80000000 // File is "busy", i.e. allocated
  292. // Mask for open mode bits stored in file object
  293. #define CYG_FILE_MODE_MASK (CYG_FREAD|CYG_FWRITE|CYG_FNONBLOCK|CYG_FAPPEND)
  294. //-----------------------------------------------------------------------------
  295. // Type of file
  296. #define CYG_FILE_TYPE_FILE 1 /* file */
  297. #define CYG_FILE_TYPE_SOCKET 2 /* communications endpoint */
  298. #define CYG_FILE_TYPE_DEVICE 3 /* device */
  299. //-----------------------------------------------------------------------------
  300. // Keys for getinfo() and setinfo()
  301. #define FILE_INFO_CONF 1 /* fpathconf() */
  302. //-----------------------------------------------------------------------------
  303. // Modes for fsync()
  304. #define CYG_FSYNC 1
  305. #define CYG_FDATASYNC 2
  306. //-----------------------------------------------------------------------------
  307. // Get/set info buffer structures
  308. // This is used for pathconf() and fpathconf()
  309. struct cyg_pathconf_info
  310. {
  311. int name; // POSIX defined variable name
  312. long value; // Returned variable value
  313. };
  314. //=============================================================================
  315. // Synchronization modes
  316. // These values are filled into the syncmode fields of the above structures
  317. // and define the synchronization protocol used when accessing the object in
  318. // question.
  319. #define CYG_SYNCMODE_NONE (0) // no locking required
  320. #define CYG_SYNCMODE_FILE_FILESYSTEM 0x0002 // lock fs during file ops
  321. #define CYG_SYNCMODE_FILE_MOUNTPOINT 0x0004 // lock mte during file ops
  322. #define CYG_SYNCMODE_IO_FILE 0x0010 // lock file during io ops
  323. #define CYG_SYNCMODE_IO_FILESYSTEM 0x0020 // lock fs during io ops
  324. #define CYG_SYNCMODE_IO_MOUNTPOINT 0x0040 // lock mte during io ops
  325. #define CYG_SYNCMODE_SOCK_FILE 0x0100 // lock socket during socket ops
  326. #define CYG_SYNCMODE_SOCK_NETSTACK 0x0800 // lock netstack during socket ops
  327. #define CYG_SYNCMODE_IO_SHIFT (4) // shift for IO to file bits
  328. #define CYG_SYNCMODE_SOCK_SHIFT (8) // shift for sock to file bits
  329. //=============================================================================
  330. // Mount and umount functions
  331. __externC int mount( const char *devname,
  332. const char *dir,
  333. const char *fsname);
  334. __externC int umount( const char *name);
  335. //=============================================================================
  336. // Get/Set info functions
  337. __externC int cyg_fs_getinfo( const char *path, int key, void *buf, int len );
  338. __externC int cyg_fs_setinfo( const char *path, int key, void *buf, int len );
  339. __externC int cyg_fs_fgetinfo( int fd, int key, void *buf, int len );
  340. __externC int cyg_fs_fsetinfo( int fd, int key, void *buf, int len );
  341. #ifdef CYGFUN_IO_FILEIO_SELECT
  342. //=============================================================================
  343. // Select support
  344. //-----------------------------------------------------------------------------
  345. // Data structure for embedding in client data structures. A pointer to this
  346. // must be passed to cyg_selrecord() and cyg_selwakeup().
  347. struct CYG_SELINFO_TAG
  348. {
  349. CYG_ADDRWORD si_info; // info passed through from fo_select()
  350. cyg_flag_value_t si_waitFlag; // select wait flags
  351. };
  352. //-----------------------------------------------------------------------------
  353. // Select support functions.
  354. // cyg_selinit() is used to initialize a selinfo structure.
  355. __externC void cyg_selinit( struct CYG_SELINFO_TAG *sip );
  356. // cyg_selrecord() is called when a client device needs to register
  357. // the current thread for selection.
  358. __externC void cyg_selrecord( CYG_ADDRWORD info, struct CYG_SELINFO_TAG *sip );
  359. // cyg_selwakeup() is called when the client device matches the select
  360. // criterion, and needs to wake up a selector.
  361. __externC void cyg_selwakeup( struct CYG_SELINFO_TAG *sip );
  362. #endif
  363. //=============================================================================
  364. // Timestamp support
  365. // Provides the current time as a time_t timestamp for use in filesystem
  366. // data strucures.
  367. __externC time_t jffs2_get_timestamp(void);
  368. //=============================================================================
  369. // Miscellaneous functions.
  370. // Provide a function to synchronize an individual file system. (ie write
  371. // file and directory information to disk)
  372. __externC int cyg_fs_fssync(const char *path);
  373. // Functions to set and get attributes of a file, eg FAT attributes
  374. // like hidden and system.
  375. __externC int cyg_fs_set_attrib( const char *fname,
  376. const cyg_fs_attrib_t new_attrib );
  377. __externC int cyg_fs_get_attrib( const char *fname,
  378. cyg_fs_attrib_t * const file_attrib );
  379. // Functions to lock and unlock a filesystem. These are normally used
  380. // internally by the fileio layer, but the file system might need to
  381. // use them when it needs to lock itself, eg when performing garbage
  382. // collect.
  383. __externC void cyg_fs_lock( cyg_mtab_entry *mte, cyg_uint32 syncmode );
  384. __externC void cyg_fs_unlock( cyg_mtab_entry *mte, cyg_uint32 syncmode );
  385. // To be able to lock the filesystem you need the mte. This function
  386. // allows the table of mounted filesystems to be searched to find an
  387. // mte which uses the give filesystem root.
  388. __externC cyg_mtab_entry * cyg_fs_root_lookup( cyg_dir *root );
  389. //=============================================================================
  390. // Default functions.
  391. // Cast to the appropriate type, these functions can be put into any of
  392. // the operation table slots to provide the defined error code.
  393. __externC int cyg_fileio_enosys(void);
  394. __externC int cyg_fileio_erofs(void);
  395. __externC int cyg_fileio_enoerr(void);
  396. __externC int cyg_fileio_enotdir(void);
  397. __externC cyg_fileop_select cyg_fileio_seltrue;
  398. //-----------------------------------------------------------------------------
  399. #endif // ifndef CYGONCE_FILEIO_H
  400. // End of fileio.h