fileio.h 20 KB

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