OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [fileio/] [current/] [doc/] [fileio.sgml] - Blame information for rev 794

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
2
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
28
29
 
30
31
File System Support Infrastructure
32
 
33
34
 
35
36
Introduction
37
 
38
39
This document describes the filesystem infrastructure provided in
40
eCos. This is implemented by the FILEIO package and provides POSIX
41
compliant file and IO operations together with the BSD socket
42
API. These APIs are described in the relevant standards and original
43
documentation and will not be described here. See 
44
linkend="posix-standard-support"> for details of which parts of the
45
POSIX standard are supported.
46
47
 
48
49
This document is concerned with the interfaces presented to client
50
filesystems and network protocol stacks.
51
52
 
53
54
The FILEIO infrastructure consist mainly of a set of tables containing
55
pointers to the primary interface functions of a file system. This
56
approach avoids problems of namespace pollution (for example several
57
filesystems can have a function called read(), so long as they are
58
static). The system is also structured to eliminate the need for
59
dynamic memory allocation.
60
61
 
62
63
New filesystems can be written directly to the interfaces described
64
here. Existing filesystems can be ported very easily by the
65
introduction of a thin veneer porting layer that translates FILEIO
66
calls into native filesystem calls.
67
68
 
69
70
The term filesystem should be read fairly loosely in this
71
document. Object accessed through these interfaces could equally be
72
network protocol sockets, device drivers, fifos, message queues or any
73
other object that can present a file-like interface.
74
75
 
76
77
 
78
79
80
 
81
82
File System Table
83
 
84
85
The filesystem table is an array of entries that describe each
86
filesystem implementation that is part of the system image. Each
87
resident filesystem should export an entry to this table using the
88
FSTAB_ENTRY() macro.
89
90
 
91
92
Note
93
94
At present we do not support dynamic addition or removal of table
95
entries. However, an API similar to mount() would
96
allow new entries to be added to the table.
97
98
99
 
100
101
The table entries are described by the following structure:
102
103
 
104
105
struct cyg_fstab_entry
106
{
107
    const char          *name;          // filesystem name
108
    CYG_ADDRWORD        data;           // private data value
109
    cyg_uint32          syncmode;       // synchronization mode
110
 
111
    int     (*mount)    ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
112
    int     (*umount)   ( cyg_mtab_entry *mte );
113
    int     (*open)     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
114
                          int mode,  cyg_file *fte );
115
    int     (*unlink)   ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
116
    int     (*mkdir)    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
117
    int     (*rmdir)    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
118
    int     (*rename)   ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
119
                          cyg_dir dir2, const char *name2 );
120
    int     (*link)     ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
121
                          cyg_dir dir2, const char *name2, int type );
122
    int     (*opendir)  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
123
                          cyg_file *fte );
124
    int     (*chdir)    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
125
                          cyg_dir *dir_out );
126
    int     (*stat)     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
127
                          struct stat *buf);
128
    int     (*getinfo)  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
129
                          int key, char *buf, int len );
130
    int     (*setinfo)  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
131
                          int key, char *buf, int len );
132
};
133
134
 
135
136
The name field points to a string that
137
identifies this filesystem implementation. Typical values might be
138
"romfs", "msdos", "ext2" etc.
139
140
 
141
142
The data field contains any private data
143
that the filesystem needs, perhaps the root of its data structures.
144
145
 
146
147
The syncmode field contains a description of
148
the locking protocol to be used when accessing this filesystem. It
149
will be described in more detail in .
150
151
 
152
153
The remaining fields are pointers to functions that implement
154
filesystem operations that apply to files and directories as whole
155
objects. The operation implemented by each function should be obvious
156
from the names, with a few exceptions:
157
158
 
159
160
The opendir() function pointer opens a directory
161
for reading. See  for details.
162
163
 
164
165
The getinfo() and
166
setinfo() function pointers provide support for
167
various minor control and information functions such as
168
pathconf() and access().
169
170
 
171
172
With the exception of the mount() and
173
umount() functions, all of these functions
174
take three standard arguments, a pointer to a mount table entry (see
175
later) a directory pointer (also see later) and a file name relative
176
to the directory. These should be used by the filesystem to locate the
177
object of interest.
178
179
 
180
181
 
182
183
184
 
185
186
Mount Table
187
 
188
189
The mount table records the filesystems that are actually active.
190
These can be seen as being analogous to mount points in Unix systems.
191
192
 
193
194
There are two sources of mount table entries. Filesystems (or other
195
components) may export static entries to the table using the
196
MTAB_ENTRY() macro. Alternatively, new entries may
197
be installed at run time using the mount()
198
function. Both types of entry may be unmounted with the
199
umount() function.
200
201
 
202
203
A mount table entry has the following structure:
204
205
 
206
207
struct cyg_mtab_entry
208
{
209
    const char          *name;          // name of mount point
210
    const char          *fsname;        // name of implementing filesystem
211
    const char          *devname;       // name of hardware device
212
    CYG_ADDRWORD        data;           // private data value
213
    cyg_bool            valid;          // Valid entry?
214
    cyg_fstab_entry     *fs;            // pointer to fstab entry
215
    cyg_dir             root;           // root directory pointer
216
};
217
218
 
219
220
The name field identifies the mount
221
point. This is used to direct rooted filenames (filenames that
222
begin with "/") to the correct filesystem. When a file
223
name that begins with "/" is submitted, it is matched
224
against the name fields of all valid mount
225
table entries. The entry that yields the longest match terminating
226
before a "/", or end of string, wins and the appropriate
227
function from the filesystem table entry is then passed the remainder
228
of the file name together with a pointer to the table entry and the
229
value of the root field as the directory
230
pointer.
231
232
 
233
234
For example, consider a mount table that contains the following
235
entries:
236
237
 
238
239
        { "/",    "msdos", "/dev/hd0", ... }
240
        { "/fd",  "msdos", "/dev/fd0", ... }
241
        { "/rom", "romfs", "", ... }
242
        { "/tmp", "ramfs", "", ... }
243
        { "/dev", "devfs", "", ... }
244
245
 
246
247
An attempt to open "/tmp/foo" would be directed to the RAM
248
filesystem while an open of "/bar/bundy" would be directed
249
to the hard disc MSDOS filesystem. Opening "/dev/tty0" would
250
be directed to the device management filesystem for lookup in the
251
device table.
252
253
 
254
255
Unrooted file names (those that do not begin with a '/') are passed
256
straight to the filesystem that contains the current directory. The
257
current directory is represented by a pair consisting of a mount table
258
entry and a directory pointer.
259
260
 
261
262
The fsname field points to a string that
263
should match the name field of the
264
implementing filesystem. During initialization the mount table is
265
scanned and the fsname entries looked up in
266
the filesystem table. For each match, the filesystem's _mount_
267
function is called and if successful the mount table entry is marked
268
as valid and the fs pointer installed.
269
270
 
271
272
The devname field contains the name of the
273
device that this filesystem is to use. This may match an entry in the
274
device table (see later) or may be a string that is specific to the
275
filesystem if it has its own internal device drivers.
276
277
 
278
279
The data field is a private data value. This
280
may be installed either statically when the table entry is defined, or
281
may be installed during the mount() operation.
282
283
 
284
285
The valid field indicates whether this mount
286
point has actually been mounted successfully. Entries with a false
287
valid field are ignored when searching for a
288
name match.
289
290
 
291
292
The fs field is installed after a successful
293
mount() operation to point to the implementing
294
filesystem.
295
296
 
297
298
The root field contains a directory pointer
299
value that the filesystem can interpret as the root of its directory
300
tree. This is passed as the dir argument of
301
filesystem functions that operate on rooted filenames. This field must
302
be initialized by the filesystem's mount()
303
function.
304
305
 
306
307
 
308
309
310
 
311
312
File Table
313
 
314
315
Once a file has been opened it is represented by an open file
316
object. These are allocated from an array of available file
317
objects. User code accesses these open file objects via a second array
318
of pointers which is indexed by small integer offsets. This gives the
319
usual Unix file descriptor functionality, complete with the various
320
duplication mechanisms.
321
322
 
323
324
A file table entry has the following structure:
325
326
 
327
328
struct CYG_FILE_TAG
329
{
330
    cyg_uint32                  f_flag;         /* file state                   */
331
    cyg_uint16                  f_ucount;       /* use count                    */
332
    cyg_uint16                  f_type;         /* descriptor type              */
333
    cyg_uint32                  f_syncmode;     /* synchronization protocol     */
334
    struct CYG_FILEOPS_TAG      *f_ops;         /* file operations              */
335
    off_t                       f_offset;       /* current offset               */
336
    CYG_ADDRWORD                f_data;         /* file or socket               */
337
    CYG_ADDRWORD                f_xops;         /* extra type specific ops      */
338
    cyg_mtab_entry              *f_mte;         /* mount table entry            */
339
};
340
341
 
342
343
The f_flag field contains some FILEIO
344
control bits and some bits propagated from the
345
flags argument of the
346
open() call (defined by
347
CYG_FILE_MODE_MASK).
348
349
 
350
351
The f_ucount field contains a use count that
352
controls when a file will be closed. Each duplicate in the file
353
descriptor array counts for one reference here. It is also
354
incremented around each I/O operation to ensure that the file cannot
355
be closed while it has current I/O operations.
356
357
 
358
359
The f_type field indicates the type of the
360
underlying file object. Some of the possible values here are
361
CYG_FILE_TYPE_FILE,
362
CYG_FILE_TYPE_SOCKET or CYG_FILE_TYPE_DEVICE.
363
364
 
365
366
The f_syncmode field is copied from the
367
syncmode field of the implementing
368
filesystem. Its use is described in .
369
370
 
371
372
The f_offset field records the current file
373
position. It is the responsibility of the file operation functions to
374
keep this field up to date.
375
376
 
377
378
The f_data field contains private data
379
placed here by the underlying filesystem. Normally this will be a
380
pointer to, or handle on, the filesystem object that implements this
381
file.
382
383
 
384
385
The f_xops field contains a pointer to any
386
extra type specific operation functions. For example, the socket I/O
387
system installs a pointer to a table of functions that implement the
388
standard socket operations.
389
390
 
391
392
The f_mte field contains a pointer to the
393
parent mount table entry for this file. It is used mainly to implement
394
the synchronization protocol. This may contain a pointer to some other
395
data structure in file objects not derived from a filesystem.
396
397
 
398
399
The f_ops field contains a pointer to a
400
table of file I/O operations. This has the following structure:
401
402
 
403
404
struct CYG_FILEOPS_TAG
405
{
406
        int     (*fo_read)      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
407
        int     (*fo_write)     (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
408
        int     (*fo_lseek)     (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
409
        int     (*fo_ioctl)     (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
410
                                 CYG_ADDRWORD data);
411
        int     (*fo_select)    (struct CYG_FILE_TAG *fp, int which, CYG_ADDRWORD info);
412
        int     (*fo_fsync)     (struct CYG_FILE_TAG *fp, int mode );
413
        int     (*fo_close)     (struct CYG_FILE_TAG *fp);
414
        int     (*fo_fstat)     (struct CYG_FILE_TAG *fp, struct stat *buf );
415
        int     (*fo_getinfo)   (struct CYG_FILE_TAG *fp, int key, char *buf, int len );
416
        int     (*fo_setinfo)   (struct CYG_FILE_TAG *fp, int key, char *buf, int len );
417
};
418
419
 
420
421
It should be obvious from the names of most of these functions what
422
their responsibilities are. The fo_getinfo()
423
and fo_setinfo() function pointers, like their
424
counterparts in the filesystem structure, implement minor control and
425
info functions such as fpathconf().
426
427
 
428
429
The second argument to the fo_read() and
430
fo_write() function pointers is a pointer to a
431
UIO structure:
432
433
 
434
435
struct CYG_UIO_TAG
436
{
437
    struct CYG_IOVEC_TAG *uio_iov;      /* pointer to array of iovecs */
438
    int                  uio_iovcnt;    /* number of iovecs in array */
439
    off_t                uio_offset;    /* offset into file this uio corresponds to */
440
    ssize_t              uio_resid;     /* residual i/o count */
441
    enum cyg_uio_seg     uio_segflg;    /* see above */
442
    enum cyg_uio_rw      uio_rw;        /* see above */
443
};
444
 
445
struct CYG_IOVEC_TAG
446
{
447
    void           *iov_base;           /* Base address. */
448
    ssize_t        iov_len;             /* Length. */
449
};
450
451
 
452
453
This structure encapsulates the parameters of any data transfer
454
operation. It provides support for scatter/gather operations and
455
records the progress of any data transfer. It is also compatible with
456
the I/O operations of any BSD-derived network stacks and filesystems.
457
458
 
459
460
When a file is opened (or a file object created by some other means,
461
such as socket() or accept()) it is the
462
responsibility of the filesystem open operation to initialize all the
463
fields of the object except the f_ucount,
464
f_syncmode and
465
f_mte fields. Since the
466
f_flag field will already contain bits belonging to the FILEIO
467
infrastructure, any changes to it must be made with the appropriate
468
logical operations.
469
470
 
471
472
 
473
474
475
 
476
477
Directories
478
 
479
480
Filesystem operations all take a directory pointer as one of their
481
arguments.  A directory pointer is an opaque handle managed by the
482
filesystem. It should encapsulate a reference to a specific directory
483
within the filesystem. For example, it may be a pointer to the data
484
structure that represents that directory (such as an inode), or a
485
pointer to a pathname for the directory.
486
487
 
488
489
The chdir() filesystem function pointer has two
490
modes of use. When passed a pointer in the
491
dir_out argument, it should locate the named
492
directory and place a directory pointer there. If the
493
dir_out argument is NULL then the
494
dir argument is a previously generated
495
directory pointer that can now be disposed of. When the infrastructure
496
is implementing the chdir() function it makes two
497
calls to filesystem chdir() functions. The first
498
is to get a directory pointer for the new current directory. If this
499
succeeds the second is to dispose of the old current directory
500
pointer.
501
502
 
503
504
The opendir() function is used to open a
505
directory for reading. This results in an open file object that can be
506
read to return a sequence of struct dirent
507
objects. The only operations that are allowed on this file are
508
read, lseek and
509
close. Each read operation on this file should
510
return a single struct dirent object. When
511
the end of the directory is reached, zero should be returned. The only
512
seek operation allowed is a rewind to the start of the directory, by
513
supplying an offset of zero and a whence
514
specifier of SEEK_SET.
515
516
 
517
518
Most of these considerations are invisible to clients of a filesystem
519
since they will access directories via the POSIX
520
opendir(), readdir() and
521
closedir() functions. The  struct
522
dirent object returned by readdir()
523
will always contain d_name as required by
524
POSIX. When CYGPKG_FILEIO_DIRENT_DTYPE is enabled
525
it will also contain d_type, which is not
526
part of POSIX, but often implemented by OSes. Currently only the
527
FATFS, RAMFS, ROMFS and JFFS2 filesystem sets this value. For other
528
filesystems a value of 0 will be returned in the member.
529
 
530
531
Support for the getcwd() function is provided by
532
three mechanisms.  The first is to use the
533
FS_INFO_GETCWD getinfo key on the filesystem to use
534
any internal support that it has for this. If that fails it falls back
535
on one of the two other mechanisms. If
536
CYGPKG_IO_FILEIO_TRACK_CWD is set then the current
537
directory is tracked textually in chdir() and the result of that is
538
reported in getcwd(). Otherwise an attempt is made to traverse the
539
directory tree to its root using ".." entries.
540
541
 
542
543
This last option is complicated and expensive, and relies on the
544
filesystem supporting "." and ".."  entries. This is not always the
545
case, particularly if the filesystem has been ported from a
546
non-UNIX-compatible source. Tracking the pathname textually will
547
usually work, but might not produce optimum results when symbolic
548
links are being used.
549
550
 
551
552
 
553
554
555
 
556
557
Synchronization
558
 
559
560
The FILEIO infrastructure provides a synchronization mechanism for
561
controlling concurrent access to filesystems. This allows existing
562
filesystems to be ported to eCos, even if they do not have their own
563
synchronization mechanisms. It also allows new filesystems to be
564
implemented easily without having to consider the synchronization
565
issues.
566
567
 
568
569
The infrastructure maintains a mutex for each entry in each of
570
the main tables: filesystem table, mount table and file table. For
571
each class of operation each of these mutexes may be locked before the
572
corresponding filesystem operation is invoked.
573
574
 
575
576
The synchronization protocol required by a filesystem is described
577
by the syncmode field of the filesystem
578
table entry. This is a combination of the following flags:
579
580
 
581
582
583
CYG_SYNCMODE_FILE_FILESYSTEM
584
585
586
Lock the filesystem table entry mutex
587
during all filesystem level operations.
588
589
590
591
 
592
593
CYG_SYNCMODE_FILE_MOUNTPOINT
594
595
596
Lock the mount table entry mutex
597
during all filesystem level operations.
598
599
600
601
 
602
603
CYG_SYNCMODE_IO_FILE
604
605
606
Lock the file table entry mutex during all
607
I/O operations.
608
609
610
611
 
612
613
CYG_SYNCMODE_IO_FILESYSTEM
614
615
616
Lock the filesystem table entry mutex during all I/O operations.
617
618
619
620
 
621
622
CYG_SYNCMODE_IO_MOUNTPOINT
623
624
Lock the mount table entry mutex during all I/O operations.
625
626
627
628
 
629
630
CYG_SYNCMODE_SOCK_FILE
631
632
633
Lock the file table entry mutex during all socket operations.
634
635
636
637
 
638
639
CYG_SYNCMODE_SOCK_NETSTACK
640
641
642
Lock the network stack table entry mutex during all socket operations.
643
644
645
646
 
647
648
CYG_SYNCMODE_NONE
649
650
651
Perform no locking at all during any operations.
652
653
654
655
 
656
657
 
658
659
The value of the syncmode field in the
660
filesystem table entry will be copied by the infrastructure to the
661
open file object after a successful open() operation.
662
663
 
664
665
 
666
667
668
 
669
670
Initialization and Mounting
671
 
672
673
As mentioned previously, mount table entries can be sourced from two
674
places. Static entries may be defined by using the
675
MTAB_ENTRY() macro. Such entries will be
676
automatically mounted on system startup.  For each entry in the mount
677
table that has a non-null name field the
678
filesystem table is searched for a match with the
679
fsname field. If a match is found the
680
filesystem's mount entry is called and if
681
successful the mount table entry marked valid and the
682
fs field initialized. The
683
mount() function is responsible for initializing
684
the root field.
685
686
 
687
 
688
689
The size of the mount table is defined by the configuration value
690
CYGNUM_FILEIO_MTAB_MAX. Any entries that have not
691
been statically defined are available for use by dynamic mounts.
692
693
 
694
695
A filesystem may be mounted dynamically by calling mount(). This
696
function has the following prototype:
697
698
 
699
700
int mount( const char *devname,
701
           const char *dir,
702
           const char *fsname);
703
704
 
705
706
The devname argument identifies a device that
707
will be used by this filesystem and will be assigned to the
708
devname field of the mount table entry.
709
710
 
711
712
The dir argument is the mount point name, it
713
will be assigned to the name field of the
714
mount table entry.
715
716
 
717
718
The fsname argument is the name of the
719
implementing filesystem, it will be assigned to the
720
fsname entry of the mount table entry.
721
722
 
723
724
The process of mounting a filesystem dynamically is as follows. First
725
a search is made of the mount table for an entry with a NULL
726
name field to be used for the new mount
727
point. The filesystem table is then searched for an entry whose name
728
matches fsname. If this is successful then
729
the mount table entry is initialized and the filesystem's
730
mount() operation called. If this is successful,
731
the mount table entry is marked valid and the
732
fs field initialized.
733
734
 
735
736
Unmounting a filesystem is done by the umount()
737
function. This can unmount filesystems whether they were mounted
738
statically or dynamically.
739
740
 
741
742
The umount() function has the following prototype:
743
744
 
745
746
int umount( const char *name );
747
748
 
749
750
The mount table is searched for a match between the
751
name argument and the entry
752
name field. When a match is found the
753
filesystem's umount() operation is called and if
754
successful, the mount table entry is invalidated by setting its
755
valid field false and the
756
name field to NULL.
757
758
 
759
763
 
764
765
 
766
767
768
 
769
770
Sockets
771
 
772
773
If a network stack is present, then the FILEIO infrastructure also
774
provides access to the standard BSD socket calls.
775
776
 
777
778
The netstack table contains entries which describe the network
779
protocol stacks that are in the system image. Each resident stack
780
should export an entry to this table using the
781
NSTAB_ENTRY() macro.
782
783
 
784
785
Each table entry has the following structure:
786
787
 
788
789
struct cyg_nstab_entry
790
{
791
    cyg_bool            valid;          // true if stack initialized
792
    cyg_uint32          syncmode;       // synchronization protocol
793
    char                *name;          // stack name
794
    char                *devname;       // hardware device name
795
    CYG_ADDRWORD        data;           // private data value
796
 
797
    int     (*init)( cyg_nstab_entry *nste );
798
    int     (*socket)( cyg_nstab_entry *nste, int domain, int type,
799
                       int protocol, cyg_file *file );
800
};
801
802
 
803
804
This table is analogous to a combination of the filesystem and mount
805
tables.
806
807
 
808
809
The valid field is set
810
true if the stack's init()
811
function returned successfully and the
812
syncmode field contains the
813
CYG_SYNCMODE_SOCK_* bits described above.
814
815
 
816
820
 
821
822
The name field contains the name of the
823
protocol stack.
824
825
 
826
830
 
831
832
The devname field names the device that the stack is using. This may
833
reference a device under "/dev", or may be a name that is only
834
meaningful to the stack itself.
835
836
 
837
841
 
842
843
The init() function pointer is called during
844
system initialization to start the protocol stack running. If it
845
returns non-zero the valid field is set
846
false and the stack will be ignored subsequently.
847
848
 
849
850
The socket() function is called to attempt to create a socket in the
851
stack. When the socket() API function is called the netstack table is
852
scanned and for each valid entry the socket()
853
function pointer is called. If
854
this returns non-zero then the scan continues to the next valid stack,
855
or terminates with an error if the end of the table is reached.
856
857
 
858
859
The result of a successful socket call is an initialized file object
860
with the f_xops field pointing to the
861
following structure:
862
863
 
864
865
struct cyg_sock_ops
866
{
867
    int (*bind)      ( cyg_file *fp, const sockaddr *sa, socklen_t len );
868
    int (*connect)   ( cyg_file *fp, const sockaddr *sa, socklen_t len );
869
    int (*accept)    ( cyg_file *fp, cyg_file *new_fp,
870
                       struct sockaddr *name, socklen_t *anamelen );
871
    int (*listen)    ( cyg_file *fp, int len );
872
    int (*getname)   ( cyg_file *fp, sockaddr *sa, socklen_t *len, int peer );
873
    int (*shutdown)  ( cyg_file *fp, int flags );
874
    int (*getsockopt)( cyg_file *fp, int level, int optname,
875
                       void *optval, socklen_t *optlen);
876
    int (*setsockopt)( cyg_file *fp, int level, int optname,
877
                       const void *optval, socklen_t optlen);
878
    int (*sendmsg)   ( cyg_file *fp, const struct msghdr *m,
879
                       int flags, ssize_t *retsize );
880
    int (*recvmsg)   ( cyg_file *fp, struct msghdr *m,
881
                       socklen_t *namelen, ssize_t *retsize );
882
};
883
884
 
885
886
It should be obvious from the names of these functions which API calls
887
they provide support for. The getname() function
888
pointer provides support for both getsockname()
889
and getpeername() while the
890
sendmsg() and recvmsg()
891
function pointers provide support for send(),
892
sendto(), sendmsg(),
893
recv(), recvfrom() and
894
recvmsg() as appropriate.
895
896
 
897
898
 
899
900
901
 
902
903
Select
904
 
905
906
The infrastructure provides support for implementing a select
907
mechanism. This is modeled on the mechanism in the BSD kernel, but has
908
been modified to make it implementation independent.
909
910
 
911
912
The main part of the mechanism is the select()
913
API call. This processes its arguments and calls the
914
fo_select() function pointer on all file objects
915
referenced by the file descriptor sets passed to it. If the same
916
descriptor appears in more than one descriptor set, the
917
fo_select() function will be called separately
918
for each appearance.
919
920
 
921
922
The which argument of the
923
fo_select() function will either be
924
CYG_FREAD to test for read conditions,
925
CYG_FWRITE to test for write conditions or zero to
926
test for exceptions. For each of these options the function should
927
test whether the condition is satisfied and if so return true. If it
928
is not satisfied then it should call
929
cyg_selrecord() with the
930
info argument that was passed to the function
931
and a pointer to a cyg_selinfo structure.
932
933
 
934
935
The cyg_selinfo structure is used to record information about current
936
select operations. Any object that needs to support select must
937
contain an instance of this structure.  Separate cyg_selinfo
938
structures should be kept for each of the options that the object can
939
select on - read, write or exception.
940
941
 
942
943
If none of the file objects report that the select condition is
944
satisfied, then the select() API function puts
945
the calling thread to sleep waiting either for a condition to become
946
satisfied, or for the optional timeout to expire.
947
948
 
949
950
A selectable object must have some asynchronous activity that may
951
cause a select condition to become true - either via interrupts or the
952
activities of other threads. Whenever a selectable condition is
953
satisfied, the object should call cyg_selwakeup() with a pointer to
954
the appropriate cyg_selinfo structure. If the thread is still waiting,
955
this will cause it to wake up and repeat its poll of the file
956
descriptors. This time around, the object that caused the wakeup
957
should indicate that the select condition is satisfied, and the
958
select() API call will return.
959
960
 
961
962
Note that select() does not exhibit real time
963
behaviour: the iterative poll of the descriptors, and the wakeup
964
mechanism mitigate against this. If real time response to device or
965
socket I/O is required then separate threads should be devoted to each
966
device of interest and should use blocking calls to wait for a
967
condition to become ready.
968
969
 
970
971
 
972
973
974
 
975
976
Devices
977
 
978
979
Devices are accessed by means of a pseudo-filesystem, "devfs", that is
980
mounted on "/dev". Open operations are translated into calls to
981
cyg_io_lookup() and if successful result in a file object whose
982
f_ops functions translate filesystem API functions into calls into
983
the device API.
984
985
 
986
987
 
988
989
990
 
991
992
Writing a New Filesystem
993
 
994
995
To create a new filesystem it is necessary to define the fstab entry
996
and the file IO operations. The easiest way to do this is to copy an
997
existing filesystem: either the test filesystem in the FILEIO package,
998
or the RAM or ROM filesystem packages.
999
1000
 
1001
1002
To make this clearer, the following is a brief tour of the FILEIO
1003
relevant parts of the RAM filesystem.
1004
1005
 
1006
1007
First, it is necessary to provide forward definitions of the functions
1008
that constitute the filesystem interface:
1009
1010
 
1011
1012
//==========================================================================
1013
// Forward definitions
1014
 
1015
// Filesystem operations
1016
static int ramfs_mount    ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
1017
static int ramfs_umount   ( cyg_mtab_entry *mte );
1018
static int ramfs_open     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
1019
                             int mode,  cyg_file *fte );
1020
static int ramfs_unlink   ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
1021
static int ramfs_mkdir    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
1022
static int ramfs_rmdir    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name );
1023
static int ramfs_rename   ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
1024
                             cyg_dir dir2, const char *name2 );
1025
static int ramfs_link     ( cyg_mtab_entry *mte, cyg_dir dir1, const char *name1,
1026
                             cyg_dir dir2, const char *name2, int type );
1027
static int ramfs_opendir  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
1028
                             cyg_file *fte );
1029
static int ramfs_chdir    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
1030
                             cyg_dir *dir_out );
1031
static int ramfs_stat     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
1032
                             struct stat *buf);
1033
static int ramfs_getinfo  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
1034
                             int key, void *buf, int len );
1035
static int ramfs_setinfo  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
1036
                             int key, void *buf, int len );
1037
 
1038
// File operations
1039
static int ramfs_fo_read      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
1040
static int ramfs_fo_write     (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
1041
static int ramfs_fo_lseek     (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
1042
static int ramfs_fo_ioctl     (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
1043
                                CYG_ADDRWORD data);
1044
static int ramfs_fo_fsync     (struct CYG_FILE_TAG *fp, int mode );
1045
static int ramfs_fo_close     (struct CYG_FILE_TAG *fp);
1046
static int ramfs_fo_fstat     (struct CYG_FILE_TAG *fp, struct stat *buf );
1047
static int ramfs_fo_getinfo   (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
1048
static int ramfs_fo_setinfo   (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
1049
 
1050
// Directory operations
1051
static int ramfs_fo_dirread      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
1052
static int ramfs_fo_dirlseek     (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
1053
1054
 
1055
1056
We define all of the fstab entries and all of the file IO
1057
operations. We also define alternatives for the
1058
fo_read and
1059
fo_lseek file IO operations.
1060
1061
 
1062
1063
We can now define the filesystem table entry. There is a macro,
1064
FSTAB_ENTRY to do this:
1065
1066
 
1067
 
1068
1069
//==========================================================================
1070
// Filesystem table entries
1071
 
1072
// -------------------------------------------------------------------------
1073
// Fstab entry.
1074
// This defines the entry in the filesystem table.
1075
// For simplicity we use _FILESYSTEM synchronization for all accesses since
1076
// we should never block in any filesystem operations.
1077
 
1078
FSTAB_ENTRY( ramfs_fste, "ramfs", 0,
1079
             CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
1080
             ramfs_mount,
1081
             ramfs_umount,
1082
             ramfs_open,
1083
             ramfs_unlink,
1084
             ramfs_mkdir,
1085
             ramfs_rmdir,
1086
             ramfs_rename,
1087
             ramfs_link,
1088
             ramfs_opendir,
1089
             ramfs_chdir,
1090
             ramfs_stat,
1091
             ramfs_getinfo,
1092
             ramfs_setinfo);
1093
1094
 
1095
1096
The first argument to this macro gives the fstab entry a name, the
1097
remainder are initializers for the field of the structure.
1098
1099
 
1100
1101
We must also define the file operations table that is installed in all
1102
open file table entries:
1103
1104
 
1105
1106
// -------------------------------------------------------------------------
1107
// File operations.
1108
// This set of file operations are used for normal open files.
1109
 
1110
static cyg_fileops ramfs_fileops =
1111
{
1112
    ramfs_fo_read,
1113
    ramfs_fo_write,
1114
    ramfs_fo_lseek,
1115
    ramfs_fo_ioctl,
1116
    cyg_fileio_seltrue,
1117
    ramfs_fo_fsync,
1118
    ramfs_fo_close,
1119
    ramfs_fo_fstat,
1120
    ramfs_fo_getinfo,
1121
    ramfs_fo_setinfo
1122
};
1123
1124
 
1125
1126
These all point to functions supplied by the filesystem except the
1127
fo_select field which is filled with a
1128
pointer to cyg_fileio_seltrue(). This is provided
1129
by the FILEIO package and is a select function that always returns
1130
true to all operations.
1131
1132
 
1133
1134
Finally, we need to define a set of file operations for use when
1135
reading directories. This table only defines the
1136
fo_read and
1137
fo_lseek operations. The rest are filled
1138
with stub functions supplied by the FILEIO package that just return an
1139
error code.
1140
1141
 
1142
1143
// -------------------------------------------------------------------------
1144
// Directory file operations.
1145
// This set of operations are used for open directories. Most entries
1146
// point to error-returning stub functions. Only the read, lseek and
1147
// close entries are functional.
1148
 
1149
static cyg_fileops ramfs_dirops =
1150
{
1151
    ramfs_fo_dirread,
1152
    (cyg_fileop_write *)cyg_fileio_enosys,
1153
    ramfs_fo_dirlseek,
1154
    (cyg_fileop_ioctl *)cyg_fileio_enosys,
1155
    cyg_fileio_seltrue,
1156
    (cyg_fileop_fsync *)cyg_fileio_enosys,
1157
    ramfs_fo_close,
1158
    (cyg_fileop_fstat *)cyg_fileio_enosys,
1159
    (cyg_fileop_getinfo *)cyg_fileio_enosys,
1160
    (cyg_fileop_setinfo *)cyg_fileio_enosys
1161
};
1162
1163
 
1164
1165
If the filesystem wants to have an instance automatically mounted on
1166
system startup, it must also define a mount table entry. This is done
1167
with the MTAB_ENTRY macro. This is an example from
1168
the test filesystem of how this is used:
1169
1170
 
1171
1172
MTAB_ENTRY( testfs_mte1,
1173
                   "/",
1174
                   "testfs",
1175
                   "",
1176
                   0);
1177
1178
 
1179
1180
The first argument provides a name for the table entry. The following
1181
arguments provide initialization for the
1182
name, fsname,
1183
devname and data
1184
fields respectively.
1185
1186
 
1187
1188
These definitions are adequate to let the new filesystem interact
1189
with the FILEIO package. The new filesystem now needs to be fleshed
1190
out with implementations of the functions defined above. Obviously,
1191
the exact form this takes will depend on what the filesystem is
1192
intended to do. Take a look at the RAM and ROM filesystems for
1193
examples of how this has been done.
1194
1195
 
1196
1197
 
1198
1199
 
1200

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.