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