home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mint095b.zoo / doc / filesys.doc < prev    next >
Text File  |  1992-07-28  |  35KB  |  723 lines

  1. MiNT File Systems
  2.  
  3. MiNT allows loadable file systems, which means that it should be quite
  4. easy to implement networked file systems, dynamically re-sizable ram
  5. disks, or other nifty things (for example, Stephen Henson's minix.xfs
  6. file system allows access to Minix partitions from TOS). Writing
  7. these is not difficult, but there are a lot of data structures that
  8. must be understood first. (These data structures are given in the
  9. file "filesys.h".)
  10.  
  11. A note on conventions: a declaration like:
  12.     short foo P_((char *bar, long baz));
  13. means that "foo" is a function that returns a 16 bit integer, and that
  14. expects a pointer to a character and a 32 bit integer as its two
  15. arguments. "ushort" is an unsigned 16 bit integer; "ulong" is an
  16. unsigned 32 bit integer.
  17.  
  18. File Cookies
  19.  
  20. Files and directories are represented in the kernel by "cookies".
  21. The contents of the cookie are mostly file system dependent, i.e. the
  22. kernel interprets only the "fs" and "dev" field of the cookie, and the
  23. contents of the other fields may be used by a file system as it sees fit.
  24.  
  25. A file cookie has the following structure:
  26.  
  27. typedef struct f_cookie {
  28.     FILESYS *fs;        /* file system that knows about this cookie */
  29.     ushort    dev;        /* device info (e.g. Rwabs device number) */
  30.     ushort    aux;        /* extra data that the file system may want */
  31.     long    index;        /* this+dev uniquely identifies a file */
  32. } fcookie;
  33.  
  34. (The "FILESYS" data type is defined below.)
  35. The interpretation of the "aux" field is entirely file system dependent. The
  36. "index" field is not presently used by the kernel, but file systems
  37. should (if possible) make this field uniquely identify a file or directory
  38. on a device.
  39.  
  40. File System Structure
  41.  
  42. This is the structure that tells the kernel about the file system,
  43. and gives the entry points for routines which the kernel can call in order
  44. to manipulate files and directories. Note that actual input/output
  45. operations are performed by a device driver; most file systems have just
  46. one associated device driver, but some may have more. See the section on
  47. device drivers for more information on these.
  48.  
  49. Unless otherwise specified, all of the functions should return 0 for
  50. success and an appropriate (long negative) error code for failure. Also,
  51. note that it is the kernel's responsibility to do all access checking;
  52. the file system may assume that the file's permissions have been checked
  53. and are compatible with the current process' uid and the operation
  54. selected.
  55.  
  56. Parameters are passed to file system driver functions on the stack.
  57. The file system drivers should preserve registers d2-d7 and a2-a7,
  58. and return any results in register d0. Note that this may differ from
  59. your compiler's default conventions (for example, Alcyon C preserves
  60. only registers d3-d7 and a3-a7); in this case, an assembly language
  61. wrapper will be necessary.
  62.  
  63. typedef struct filesys {
  64.     struct    filesys    *next;
  65.  
  66. This is a link to the next file system in the kernel's list. It will be
  67. filled in by the kernel; the file system should leave it as NULL.
  68.  
  69.     long    fsflags;
  70. These flags give some information about the file system. Currently, three
  71. flags are defined:
  72.  
  73. #define FS_KNOPARSE     0x01    /* kernel shouldn't do parsing */
  74. #define FS_CASESENSITIVE 0x02    /* file names are case sensitive */
  75. #define FS_NOXBIT     0x04    /* if a file can be read, it can be executed */
  76.  
  77. Other bits may be defined in future releases of MiNT; for now all other
  78. bits in this flag should be 0. Most file systems will have only the
  79. FS_NOXBIT flag; networked file systems may have FS_KNOPARSE for
  80. reasons of efficiency, and file systems that must be compatible with Unix
  81. or similar specifications may be case sensitive and hence have FS_CASESENSITIVE
  82. set.
  83.  
  84.     long    (*root) P_((short drv, fcookie *fc));
  85. This is the entry point for a routine to find a file cookie for the root
  86. directory of BIOS device "drv" (an integer in the range 0-31 inclusive).
  87. This function is called by the kernel when initializing a drive; the kernel
  88. will query each file system in turn for a file cookie representing the
  89. root directory of the drive. If the file system recognizes the data on
  90. the drive as being valid for a file system that it recognizes, it should
  91. fill in the cookie pointed to by "fc" and return 0. Otherwise, it should
  92. return a negative error code (EDRIVE is a good choice) to indicate that the
  93. drive must belong to another file system. Note that this function is called
  94. at boot up time and also at any time when media change is detected on a drive.
  95.  
  96.     long    (*lookup) P_((fcookie *dir, char *name, fcookie *fc));
  97. Translate a file name into a cookie. "dir" is the cookie for a directory,
  98. returned by a previous call to (*lookup) or (*root). "name" is either the
  99. name of a file in that directory (if fsflags & FS_KNOPARSE == 0) or a path
  100. name relative to that directory (if fsflags & FS_KNOPARSE == FS_KNOPARSE).
  101. If the file is not found, an appropriate error code (like EFILNF) should be
  102. returned. If the file is found, the cookie "*fc" should be filled in with
  103. appropriate data, and either 0 or EMOUNT returned. EMOUNT should be returned
  104. only if "name" is ".." and "dir" represents the root directory of a drive;
  105. 0 should be returned otherwise. Note that a lookup call with a null name
  106. or with "." should always succeed and return a cookie representing the
  107. directory itself. Also note that symbolic links should *never* be followed.
  108.  
  109.     long    (*creat) P_((fcookie *dir, char *name, ushort mode,
  110.                 short attrib, fcookie *fc)
  111. Create a new file named "name" in the directory whose cookie is "dir".
  112. "mode" gives the file's type and access permissions, as follows:
  113.     /* file types */
  114.     #define S_IFMT    0170000        /* mask to select file type */
  115.     #define S_IFCHR    0020000        /* BIOS special file */
  116.     #define S_IFDIR    0040000        /* directory file */
  117.     #define S_IFREG 0100000        /* regular file */
  118.     #define S_IFIFO 0120000        /* FIFO */
  119.     #define S_IMEM    0140000        /* memory region or process */
  120.     #define S_IFLNK    0160000        /* symbolic link */
  121.  
  122.     /* special bits: setuid, setgid, sticky bit */
  123.     #define S_ISUID    04000        /* change euid when executing this file */
  124.     #define S_ISGID 02000        /* change egid when executing this file */
  125.     #define S_ISVTX    01000        /* not implemented */
  126.  
  127.     /* file access modes for user, group, and other*/
  128.     #define S_IRUSR    0400        /* read access for user */
  129.     #define S_IWUSR 0200        /* write access for user */
  130.     #define S_IXUSR 0100        /* execute access for user */
  131.     #define S_IRGRP 0040        /* ditto for group... */
  132.     #define S_IWGRP    0020
  133.     #define S_IXGRP    0010
  134.     #define S_IROTH    0004        /* ditto for everyone else */
  135.     #define S_IWOTH    0002
  136.     #define S_IXOTH    0001
  137.  
  138. "attrib" gives the standard TOS attribute byte. This is slightly redundant
  139. with "mode" (i.e. the FA_RDONLY bit should agree with the settings of
  140. S_IWUSR, S_IWGRP, and S_IWOTH, and FA_DIR should be set if and only if
  141. the file's format is S_IFDIR) but is provided for convenience for standard
  142. DOS compatible file systems.
  143.  
  144. The kernel will make the "creat" call only after using "lookup" in an attempt
  145. to find the file. The file system should create the file (if possible) and
  146. set the cookie pointed to by "fc" to represent the newly created file.
  147. If an error of any sort occurs, an appropriate error number is returned,
  148. otherwise 0 is returned. Also note that the kernel will not try to
  149. create directories this way; it will use "mkdir" (q.v.) instead.
  150.  
  151.     DEVDRV *(*getdev) P_((fcookie *fc, long *devspecial))
  152. Get the device driver which should be used to do i/o on the file whose
  153. cookie is "fc". If an error occurs, a NULL pointer should be returned
  154. and an error code placed in the long pointed to by "devspecial"; otherwise,
  155. "devspecial" should be set to a device-driver specific value which will
  156. be placed by the kernel in the "devinfo" field of the FILEPTR structure
  157. passed to the device driver's open routine. (The interpretation of
  158. this value is a matter for the file system and the device driver, the
  159. kernel doesn't care.)
  160.  
  161. If the call to (*getdev) succeeds, a pointer to a device driver structure
  162. (see below) is returned; if it fails, a NULL pointer should be returned and
  163. an appropriate error number placed in *devspecial.
  164.  
  165.     long    (*getxattr) P_((fcookie *file, XATTR *xattr));
  166. Get a file's attributes. The XATTR structure pointed to by "xattr" should
  167. be filled in with the data for the file or directory represented by
  168. the cookie "*file", and 0 returned. If a fatal error occurs (e.g. media
  169. change) an error code is returned instead. The XATTR structure is defined
  170. as follows:
  171.  
  172. /* structure for getxattr */
  173.     typedef struct xattr {
  174.     ushort    mode;
  175. file types and permissions; same as the mode passed to (*creat) (see above)
  176.     long    index;
  177. file index; this should if possible be a unique number for the file, so that
  178. no two files on the same physical drive could have the same index. It is
  179. not mandatory that this match the "index" field of the fcookie structure
  180. for the file.
  181.     ushort    dev;
  182. physical device on which the file is located; normally set to file->dev
  183.     ushort    reserved1;
  184. set to 0
  185.     ushort    nlink;
  186. number of hard links to the file; normally 1
  187.     ushort    uid;
  188. a number representing the user that owns the file
  189.     ushort    gid;
  190. the group ownership of the file
  191.     long    size;
  192. length of the file, in bytes
  193.     short    mtime, mdate;
  194. last modification time and date of the file, in standard GEMDOS format
  195.     short    atime, adate;
  196. last access time and date for the file, in standard GEMDOS format; if the
  197. file system does not keep separate record of these, they should be the same
  198. as mtime and mdate
  199.     short    ctime, cdate;
  200. file creation time and date, in standard GEMDOS format; if the file system
  201. does not keep separate record of these, they should be the same as mtime
  202. and mdate
  203.     short    attr;
  204. TOS attribute byte for the file in the lower 8 bits; the upper 8 should
  205. be 0
  206.     short    reserved2;
  207. reserved, set to 0
  208.     long    reserved3[2];
  209. reserved, set both long words to 0
  210.     } XATTR;
  211.  
  212.     long    (*chattr) P_((fcookie *file, short attr));
  213. Change the TOS attributes of the file whose cookie is "*file" to "attr".
  214. Only the lower 8 bits of "attr" should be considered significant, for now.
  215. The kernel will not allow changes if the file's current attributes include
  216. the FA_DIR bit or the FA_LABEL bit. Not all filesystems will support all
  217. TOS attribute bits, but FA_RDONLY should probably be supported if possible;
  218. usually setting the FA_RDONLY bit should be equivalent to turning off all
  219. write permissions to the file.
  220.  
  221.     long    (*chown) P_((fcookie *file, short uid, short gid));
  222. Change a file's user and group ownership to "uid" and "gid" respectively.
  223. The kernel checks access permissions before making this call, so file
  224. systems do not have to. If the file system does not support a concept
  225. of ownership, or does not allow changes to ownership, it should return
  226. EINVFN.
  227.  
  228.     long    (*chmode) P_((fcookie *file, ushort mode));
  229. Change a file's access permissions. "mode" is similar to the field in
  230. the XATTR structure or the value passed to creat, except that _only_
  231. the permission bits are significant; (mode & S_IFMT) will always be 0.
  232. In the event that the file system supports only a subset of permissions
  233. (e.g. the TOS file system can only control write access to the file)
  234. then it may consider only the relevant bits of "mode".
  235.  
  236.     long    (*mkdir) P_((fcookie *dir, char *name, ushort mode));
  237. Make a new subdirectory called "name" of the directory whose cookie is
  238. "*dir". The new directory should have the file permissions given by
  239. "mode & ~S_IFMT". Note that the file system should do all appropriate
  240. initializations for the new directory, including making entries for
  241. "." and "..". Note also that the kernel verifies that "mode & S_IFMT"
  242. is S_IFDIR before making this call.
  243.  
  244.     long    (*rmdir) P_((fcookie *dir, char *name));
  245. Delete the subdirectory called "name" of the directory whose cookie is
  246. "*dir". It is also a good idea to allow removal of symbolic links via
  247. this call, since a symbolic link to a directory looks like a directory
  248. to a normal TOS program.
  249.  
  250.     long    (*remove) P_((fcookie *dir, char *name));
  251. Delete the file called "name" in the directory "*dir". This function should
  252. act like the Unix "unlink" call, i.e. if the file has more than 1 hard
  253. link to it, only this particular link to the file should be removed and
  254. the file contents should not be affected. Directories should not be removed
  255. by this function. Symbolic links definitely should be removed by this
  256. function; whether other types of special files are removed by this function
  257. is up to the file system.
  258.  
  259.     long    (*getname) P_((fcookie *relto, fcookie *dir, char *pathname));
  260. This is analogous to the "getcwd()" operation in Unix. It should get the name
  261. of the directory whose cookie is "*dir", expressed as a path relative
  262. to the directory whose cookie is "*relto"; normally, this is the root
  263. directory, but the file system should not assume this. The resulting path
  264. is placed in the array pointed to by *pathname, which is PATH_MAX bytes
  265. long. If *relto and *dir are the same directory, then an empty string
  266. should be placed in *pathname.
  267.  
  268. Example: if "*relto" is the directory "\FOO", and "*dir" is the directory
  269. "\FOO\BAR\SUB", then after the call "pathname" should contain "\BAR\SUB".
  270.  
  271.     long    (*rename) P_((fcookie *olddir, char *oldname,
  272.                 fcookie *newdir, char *newname));
  273. Rename the file with name "oldname" contained in the directory whose cookie
  274. is "*olddir" to the name "newname" in the directory whose cookie is "*newdir".
  275. The file system need not actually support cross-directory renames, or
  276. indeed any sort of renames at all; if no renames at all are supported, EINVFN
  277. should be returned.
  278.  
  279.     long    (*opendir) P_((DIR *dirh, short tosflag));
  280. Open a directory for reading. "dirh" is a pointer to a structure as defined
  281. below; the file cookie for the directory being opened may be found there.
  282. "tosflag" is a copy of "dirh->flags" and is in a sense redundant. The
  283. file system should initialize the "fsstuff" and "index" fields of "*dirh"
  284. to whatever it needs for carrying out a successful search.
  285.  
  286. /* structure for opendir/readdir/closedir */
  287. typedef struct dirstruct {
  288.     fcookie fc;        /* cookie for this directory */
  289.     ushort    index;        /* index of the current entry */
  290.     ushort    flags;        /* flags (e.g. tos or not) */
  291. #define TOS_SEARCH    0x01
  292. /* if TOS_SEARCH is set, this call originated from a TOS Fsfirst() system
  293.  * call -- if possible, the returned names should be acceptable to a
  294.  * "naive" TOS program
  295.  */
  296.     char    fsstuff[60];    /* anything else the file system wants */
  297. } DIR;
  298.  
  299.     long    (*readdir) P_((DIR *dirh, char *name, short namelen, fcookie *fc));
  300. Read the next name from the directory whose DIR structure (see above)
  301. is "*dirh". The name should be copied into "name" (if dirh->flags & TOS_SEARCH
  302. is nonzero) or "name+4" if dirh->flags & TOS_SEARCH is 0; in the latter case,
  303. the first 4 bytes of "name" should be a unique index for the file. "namelen"
  304. is the total size of the buffer for "name"; if the next file name (plus index,
  305. if appopriate, and including the trailing 0) is too long for the buffer, as
  306. much of it as will fit should be copied in and ENAMETOOLONG returned. If no
  307. more file names remain unread in the directory, ENMFIL should be returned
  308. and "name" left unchanged. Otherwise, 0 should be returned.
  309. Note that volume labels should not be read by this function; only
  310. "readlabel" (q.v.) should see these.
  311.  
  312.     long    (*rewinddir) P_((DIR *dirh));
  313. Reset the file system specific fields of "*dirh" so that the next call to
  314. "readdir" on this directory will return the first file name in the directory.
  315.  
  316.     long    (*closedir) P_((DIR *dirh));
  317. Called by the kernel when the directory "*dirh" is not going to be searched
  318. any more; if the file system needs to clean up any structures or free memory
  319. allocated for the search it can do so here.
  320.  
  321.     long    (*pathconf) P_((fcookie *dir, short which));
  322. Get path configuration information for the directory whose cookie is
  323. "*dir". "which" indicates what kind of information should be returned,
  324. as follows:
  325.  
  326. /* The requests for pathconf() */
  327. #define DP_IOPEN    0    /* internal limit on # of open files */
  328. #define DP_MAXLINKS    1    /* max number of hard links to a file */
  329. #define DP_PATHMAX    2    /* max path name length */
  330. #define DP_NAMEMAX    3    /* max length of an individual file name */
  331. #define DP_ATOMIC    4    /* # of bytes that can be written atomically */
  332. #define DP_TRUNC    5    /* file name truncation behavior */
  333. /* possible return values for DP_TRUNC */
  334. #    define    DP_NOTRUNC    0    /* long names cause an error */
  335. #    define    DP_AUTOTRUNC    1    /* long names are truncated */
  336. #    define    DP_DOSTRUNC    2    /* DOS 8+3 rules are used */
  337. #define DP_CASE        6    /* file name case conversion */
  338. /* possible values returned for DP_CASE */
  339. #    define    DP_CASESENS    0    /* case sensitive */
  340. #    define    DP_CASECONV    1    /* case always converted */
  341. #    define    DP_CASEINSENS    2    /* case insensitive, preserved */
  342.  
  343. #define DP_MAXREQ    6    /* highest legal request */
  344. /* Dpathconf and Sysconf return this when a value is not limited
  345.    (or is limited only by available memory) */
  346. #define UNLIMITED    0x7fffffffL
  347.  
  348.     long    (*dfree) P_((fcookie *dir, long *buf));
  349. Determine bytes used and free on the disk that the directory whose cookie
  350. is "*dir" is contained on. "buf" points to the same kind of buffer that
  351. the "Dfree" system call uses; see the documentation for that call.
  352.  
  353.     long    (*writelabel) P_((fcookie *dir, char *name));
  354. Create a volume label with the indicated name on the drive which contains
  355. the directory whose cookie is "*dir". If a label already exists, the file
  356. system may either fail the call with EACCDN or re-write the label. If the
  357. file system doesn't support the notion of labels, it should return EINVFN.
  358.  
  359.     long    (*readlabel) P_((fcookie *dir, char *name, short namelen));
  360. Read the volume label for the disk whose root directory has the cookie
  361. "*dir" into the buffer "name", which is "namelen" bytes long. If the
  362. volume label (including trailing 0) won't fit, return ENAMETOOLONG. If
  363. *dir is not the cookie of a root directory, or if no volume label
  364. exists (perhaps because the file system doesn't support them), return
  365. EFILNF.
  366.  
  367.     long    (*symlink) P_((fcookie *dir, char *name, char *to));
  368. Create a symbolic link called "name" in the directory whose cookie is
  369. "*dir". The link should contain the 0-terminated string "to". If the file
  370. system doesn't support symbolic links, it should return EINVFN.
  371.  
  372.     long    (*readlink) P_((fcookie *file, char *buf, short buflen));
  373. Read the contents of the symbolic link whose cookie is "*file" into the
  374. buffer "buf", which is "buflen" bytes long. If the contents (including the
  375. trailing 0) won't fit, return ENAMETOOLONG; if the file system doesn't
  376. do symbolic links, return EINVFN.
  377.  
  378.     long    (*hardlink) P_((fcookie *fromdir, char *fromname,
  379.                 fcookie *todir, char *toname));
  380. Create a hard link called "toname" in the directory whose cookie is
  381. "*todir" for the file named "fromname" in the directory whose cookie is
  382. "*fromdir". If the file system doesn't do hard links, return EINVFN.
  383.  
  384.     long    (*fscntl) P_((fcookie *dir, char *name, short cmd, long arg));
  385. Perform an operation on the file whose name is "name", in the directory with
  386. cookie "*dir". "cmd" and "arg" specify the operation, and are file system
  387. specific. See the documentation for Dcntl() for more details. Most file
  388. systems will just return EINVFN for any values of "cmd" and "arg"; this call
  389. is here so that you can provide users with a way to manipulate various special
  390. features of your file system.
  391.  
  392.     long    (*dskchng) P_((short drv));
  393. Check for media change. "drv" is the BIOS device number on which the
  394. kernel thinks there has been a change. This function is called only
  395. when the kernel detects what the BIOS claims is a definite disk change
  396. (i.e. Mediach returning 2 or Mediach returning 1 and Rwabs returning -14).
  397. This may be the result of a program trying to force a media change; if the
  398. file system agrees that a change has occured, it should perform any
  399. appropriate actions (e.g. invalidating buffers) and return 1; the kernel
  400. will then invalidate any open files or directories on the device and
  401. re-check what file system the device belongs  If no change, in fact, occured,
  402. a 0 should be returned to tell the kernel not to worry.
  403.  
  404.     long    zero;
  405. A long word present to allow for future expansion; this must always be set
  406. to 0 by file systems (for now).
  407. } FILESYS;
  408.  
  409. typedef struct fileptr {
  410.     short    links;        /* number of copies of this descriptor */
  411.     ushort    flags;        /* file open mode and other file flags */
  412. #define O_RWMODE      0x03    /* isolates file read/write mode */
  413. #    define O_RDONLY    0x00
  414. #    define O_WRONLY    0x01
  415. #    define O_RDWR    0x02
  416. #    define O_EXEC    0x03    /* execute file; used by kernel only */
  417.  
  418. #define O_APPEND    0x08    /* all writes go to the end of the file */
  419.  
  420. #define O_SHMODE    0x70    /* isolates file sharing mode */
  421. #    define O_COMPAT    0x00    /* compatibility mode */
  422. #    define O_DENYRW    0x10    /* deny both read and write access */
  423. #    define O_DENYW    0x20    /* deny write access to others */
  424. #    define O_DENYR    0x30    /* deny read access to others */
  425. #    define O_DENYNONE 0x40    /* don't deny any access to others */
  426.  
  427. #define O_NOINHERIT    0x80    /* children can't access via this file descriptor */
  428. #define O_NDELAY    0x100    /* don't block for i/o on this file */
  429. #define O_CREAT        0x200    /* create file if it doesn't exist */
  430. #define O_TRUNC        0x400    /* truncate file to 0 bytes if it does exist */
  431. #define O_EXCL        0x800    /* fail open if file exists */
  432. #define O_TTY        0x2000    /* file is a terminal */
  433. #define O_HEAD        0x4000    /* file is a pseudo-terminal "master" */
  434. #define O_LOCK        0x8000    /* file has been locked */
  435.  
  436. The "flags" is constructed by or'ing together exactly one read/write mode,
  437. one sharing mode, and any number of the other bits. Device drivers can
  438. ignore the O_CREAT flag, since file creation is handled by the kernel.
  439. The O_TRUNC flag, however, should be respected; the file should be
  440. truncated to 0 length if this flag is set.
  441.  
  442.     long    pos;        /* position in file */
  443. The kernel doesn't actually use this field, except to initialize it to
  444. 0; it is recommended that device drivers that allow seeking should use it
  445. to store the current position in the file (relative to the start of
  446. the file). Other device drivers may use it for other purposes.
  447.     long    devinfo;    /* device driver specific info */
  448. This field is passed back to the kernel from the file system from the
  449. "getdev" call; its interpretation is file system specific, except that
  450. if this is a terminal device (i.e. the O_TTY bit is set in "flags") then
  451. this must be a pointer to a struct tty for this terminal.
  452.     fcookie    fc;        /* file system cookie for this file */
  453. This is the cookie for the file, as returned by the file system "lookup"
  454. function during opening of the file.
  455.     struct devdrv *dev; /* device driver that knows how to deal with this */
  456. This is the device driver returned by the "getdev" call.
  457.     struct fileptr *next; /* link to next fileptr for this file */
  458. This field may be used by device drivers to keep a linked list of file
  459. pointers that refer to the same physical file, for example, in order to
  460. implement file sharing or locking code.
  461. } FILEPTR;
  462.  
  463.  
  464. The Device Driver Structure
  465.  
  466. All of the functions in the device driver structure, like those in the
  467. file system structure, are called in supervisor mode and with the
  468. GCC calling conventions. They must preserve registers d2-d7 and a2-a7,
  469. and results are to be returned in register d0. The BIOS, XBIOS,
  470. GEMDOS, AES, and VDI must not be called directly from the device
  471. driver; but GEMDOS and BIOS functions may be called indirectly via
  472. the tables found in the kerinfo structure.
  473.  
  474. typedef struct devdrv {
  475.     long (*open)    P_((FILEPTR *f));
  476. This routine is called by the kernel during a file "open", after it has
  477. constructed a FILEPTR for the file being opened and determined the device
  478. driver. The device driver should check the contents of the FILEPTR and
  479. make any changes or initializations necessary. If for some reason the open
  480. call should be failed, an appropriate error code must be returned (in which
  481. case the kernel will free the FILEPTR structure automatically). For example,
  482. if the file sharing mode in f->flags is not compatible with the sharing
  483. mode of another open FILEPTR referring to the same physical file, EACCDN should
  484. be returned.
  485.  
  486.     long (*write)    P_((FILEPTR *f, char *buf, long bytes));
  487. Write "bytes" bytes from the buffer pointed to by "buf" to the file with
  488. FILEPTR "f". Return the number of bytes actually written. If the file
  489. pointer has the O_APPEND bit set, the kernel will automatically perform
  490. an "lseek" to the end of the file before calling the "write" function.
  491. If the device driver cannot ensure the atomicity of the "lseek" + "write"
  492. combination, it should take whatever steps are necessary here to ensure
  493. that files with O_APPEND really do have all writes go to the end of the
  494. file.
  495.  
  496.     long (*read)    P_((FILEPTR *f, char *buf, long bytes));
  497. Read "bytes" bytes from the file with FILEPTR "f" into the buffer pointed
  498. to by "buf". Return the number of bytes actually read.
  499.  
  500.     long (*lseek)    P_((FILEPTR *f, long where, short whence));
  501. Seek to a new position in the file. "where" is the new position; "whence"
  502. says what "where" is relative to, as follows:
  503. /* lseek() origins */
  504. #define    SEEK_SET    0        /* from beginning of file */
  505. #define    SEEK_CUR    1        /* from current location */
  506. #define    SEEK_END    2        /* from end of file */
  507.  
  508.     long (*ioctl)    P_((FILEPTR *f, short mode, void *buf));
  509. Perform a device specific function. "mode" is the function desired. All devices
  510. should support the FIONREAD and FIONWRITE functions, and the file locking
  511. Fcntl functions if appropriate (see the documentation for the GEMDOS
  512. Fcntl function).
  513.  
  514.     long (*datime)    P_((FILEPTR *f, short *timeptr, short rwflag));
  515. Get or set the date/time of the file. "timeptr" is a pointer to two words,
  516. the first of which is the time and the second of which is the date.
  517. If "rwflag" is 0, the time and date of the file should be placed into timeptr.
  518. If "rwflag" is nonzero, then the time and date of the file should be set to
  519. agree with the time and date pointed to by timeptr.
  520.  
  521.     long (*close)    P_((FILEPTR *f, short pid));
  522. Called every time an open file is closed. Note that the file is "really"
  523. being closed if f->links == 0, otherwise, the FILEPTR is still being used
  524. by some process. However, if the device driver supports file locking
  525. then all locks held on the file by process pid should be released on
  526. any close, even if f->links > 0. Some things to watch out for:
  527. (1) "pid" is not necessarily the current process; some system calls
  528.     (e.g. Fmidipipe, Pexec) can sometimes close files in a process
  529.     other than the current one.
  530. (2) Device drivers should set the O_LOCK bit on f->flag when the F_SETLK
  531.     ioctl is made; they can then test for this bit when the file is being
  532.     closed, and remove locks only if O_LOCK is set. Note that all locks
  533.     held by process pid and referring to the same physical file as "f" may
  534.     be removed if O_LOCK is set, not just the locks that were associated with
  535.     the particular FILEPTR "f". If the FILEPTR has never had any lock Fcntl()
  536.     calls made on it, locks on the associated physical file need not be (and
  537.     should not be) removed when it is closed.
  538.  
  539.     long (*select)    P_((FILEPTR *f, long proc, short mode));
  540. Called by Fselect() when "f" is one of the file handles a user has chosen to
  541. do a select on. If mode is O_RDONLY, the select is for reading; if
  542. it is O_WRONLY, it is for writing (if it is for both reading and writing,
  543. the function will be called twice). The select function should return
  544. 1 if the device is ready for reading or writing (i.e. if a read or write
  545. call to the device will not block); otherwise, it should take whatever
  546. steps are necessary to arrange to wake up the process whose PROC structure is
  547. pointed to by "proc" when the appropriate I/O on the device becomes possible.
  548. Normally, this will be done by calling the "wakeselect" function (as passed
  549. by the kernel in "struct kerinfo") with "proc" as its parameter.
  550.  
  551.     void (*unselect) P_((FILEPTR *f, long proc, short mode));
  552. Called when the kernel is returning from an Fselect that had previously
  553. selected this file or device; the device driver should no longer notify
  554. "proc" when I/O is possible for this file or device. "mode" is the same
  555. mode as was passed to the select() function (see above), i.e. either
  556. O_RDONLY or O_WRONLY; as with select(), unselect() will be called twice
  557. if both input and output were selected for.
  558.  
  559.     long reserved[3];
  560. Reserved longwords for future expansion. These must be set to 0.
  561. } DEVDRV;
  562.  
  563.  
  564.  
  565. How the File System Is Booted
  566.  
  567. A loadable file system is an ordinary TOS executable file with an
  568. extension of ".xfs". MiNT searches its current directory (normally the
  569. root directory of the boot disk) when it is starting for all such
  570. files, and loads them with Pexec mode 3. It then does a jump to
  571. subroutine call to the first instruction of the loaded program,
  572. passing on the stack a pointer to a structure of type "struct kerinfo"
  573. (see below) which describes the version of MiNT and provides entry points
  574. for various utility functions.
  575.  
  576. The file system should *not* set up a stack or shrink its basepage
  577. (so the ordinary C startup code is not necessary). MiNT has already provided
  578. a stack of about 8K or so, and has shrunk the basepage to the bare minimum.
  579. The file system initialization point (like all file system functions) is
  580. called in supervisor mode; it must never switch back to user mode. It
  581. may use registers d0, d1, a0, and a1 as scratch registers; all other
  582. registers must be preserved.
  583.  
  584. What the file system initialization code *should* do is to check that an
  585. appropriate version of MiNT is running and to otherwise check the system
  586. configuration to see if it is appropriate for the file system driver.
  587. If so, a pointer to a FILESYS structure should be returned; if not, a NULL
  588. pointer should be returned.
  589.  
  590. Note that it is not necessary to actually check during initialization for
  591. the presence of disks with the appropriate file system types; MiNT will
  592. call the file system "root" function for each drive in the system, and so
  593. such checks should be done in the "root" function.
  594.  
  595. If the file system driver wishes to add new drives to the system,
  596. it should update the drive configuration variable stored at 0x4c2 to
  597. reflect the presence of these new drives.
  598.  
  599. File system drivers should *not* make any calls to the BIOS or GEMDOS
  600. directly; all such calls should be made through the vectors provided by
  601. the kernel as part of the struct kerinfo. File system drivers should
  602. never call the AES, VDI, or XBIOS.
  603.  
  604. All functions made visible to file systems through the kerinfo
  605. structure should be called using the GCC/Lattice C calling conventions,
  606. i.e.:
  607. (1) parameters are passed on the stack, aligned on 16 bit boundaries
  608. (2) registers d0-d1 and a0-a1 are scratch registers and may be modified
  609.     by the called functions
  610. (3) if the function returns a value, it will be returned in register
  611.     d0
  612.  
  613. /*
  614.  * this is the structure passed to loaded file systems to tell them
  615.  * about the kernel
  616.  */
  617.  
  618. typedef long (*Func)();
  619.  
  620. struct kerinfo {
  621.     short    maj_version;    /* kernel version number */
  622.     short    min_version;    /* minor kernel version number */
  623.     ushort    default_mode;    /* default file access permissions */
  624.     short    reserved1;    /* room for expansion */
  625.  
  626. /* OS functions */
  627. /* NOTE: these tables are definitely READ ONLY!!!! */
  628.     Func    *bios_tab;    /* pointer to the BIOS entry points */
  629.     Func    *dos_tab;    /* pointer to the GEMDOS entry points */
  630.  
  631. /* media change vector: call this if a device driver detects a disk
  632.  * change during a read or write operation. The parameter is the BIOS device
  633.  * number of the disk that changed.
  634.  */
  635.     void    (*drvchng) P_((ushort));
  636.  
  637. /* Debugging stuff */
  638.     void    (*trace) P_((char *, ...));    /* informational messages */
  639.     void    (*debug) P_((char *, ...));    /* error messages */
  640.     void    (*alert) P_((char *, ...));    /* really serious errors */
  641.     void    (*fatal) P_((char *, ...));    /* fatal errors */
  642.  
  643. /* memory allocation functions */
  644. /* kmalloc and kfree should be used for most purposes, and act like malloc
  645.  * and free respectively. umalloc and ufree may be used to allocate/free memory
  646.  * that is attached to the current process, and which is freed automatically
  647.  * when the process exits; this is generally not of much use to a file system
  648.  * driver
  649.  */
  650.     void *    (*kmalloc) P_((long));
  651.     void    (*kfree) P_((void *));
  652.     void *    (*umalloc) P_((long));
  653.     void    (*ufree) P_((void *));
  654.  
  655. /* utility functions for string manipulation */
  656.  
  657. /* strnicmp, stricmp: like strncmp and strcmp, respectively, except
  658.  * that case is ignored
  659.  */
  660.     short    (*strnicmp) P_((char *, char *, short));
  661.     short    (*stricmp) P_((char *, char *));
  662.  
  663. /* strlwr: convert a string to lower case. Returns the address of the string */
  664.     char *    (*strlwr) P_((char *));
  665. /* strupr: convert a string to upper case. Returns the address of the string */
  666.     char *    (*strupr) P_((char *));
  667.  
  668. /* sprintf: like the C library sprintf call, but using this one means you
  669.  * can avoid linking another one. Note: floating point formats are not
  670.  * supported! Also: this sprintf will put at most SPRINTF_MAX characters
  671.  * into the output string.
  672.  */
  673.     short    (*sprintf) P_((char *, const char *, ...));
  674.  
  675. /* utility functions for manipulating time */
  676. /* convert "ms" milliseconds into a DOS time (in td[0]) and date (in td[1]) */
  677.     void    (*millis_time) P_((ulong ms, short *td));
  678.  
  679. /* convert a DOS style time and date into a Unix style time; returns the
  680.  * Unix time
  681.  */
  682.     long    (*unixtim) P_((ushort time, ushort date));
  683.  
  684. /* convert a Unix time into a DOS time (in the high word of the returned
  685.  * value) and date (in the low word)
  686.  */
  687.     long    (*dostim) P_((long));
  688.  
  689. /* utility functions for dealing with pauses */
  690. /* go to sleep temporarily for about "n" milliseconds (the exact time
  691.  * slept may vary
  692.  */
  693.     void    (*nap) P_((ushort n));
  694.  
  695. /* wait on system queue "que" until a condition occurs */
  696.     void    (*sleep) P_((short que, long cond));
  697. /* wake all processes on queue "que" that are waiting for condition "cond" */
  698.     void    (*wake) P_((short que, long cond));
  699.  
  700. /* wake a process that is doing a select(); "param" should be the process
  701.  * code passed to select()
  702.  */
  703.     void    (*wakeselect) P_((long param));
  704.  
  705. /* file system utility functions */
  706. /* "list" is a list of open files; "f" is a new file that is being opened.
  707.  * If the file sharing mode of "f" conflicts with any of the FILEPTRs
  708.  * in the list, then this returns 1, otherwise 0.
  709.  */
  710.     short    (*denyshare) P_((FILEPTR *list, FILEPTR *f));
  711.  
  712. /* denylock() checks a list of locks to see if the new lock conflicts
  713.  * with any one in the list. If so, the first conflicting lock
  714.  * is returned; otherwise, a NULL is returned.
  715.  * This function is only available if maj_version > 0 or min_version >= 94.
  716.  * Otherwise, it will be a null pointer.
  717.  */
  718.     LOCK *    (*denylock) P_((LOCK *list, LOCK *new));
  719.  
  720. /* reserved for future use */
  721.     long    res2[9];
  722. };
  723.