vfs: add the readdir stuff support
[profile/ivi/syslinux.git] / core / include / fs.h
1 #ifndef FS_H
2 #define FS_H
3
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include <string.h>
7 #include <com32.h>
8 #include "core.h"
9 #include "disk.h"
10
11 /*
12  * Maximum number of open files.  This is *currently* constrained by the
13  * fact that PXE needs to be able to fit all its packet buffers into a
14  * 64K segment; this should be fixed by moving the packet buffers to high
15  * memory.
16  */
17 #define MAX_OPEN_LG2    5
18 #define MAX_OPEN        (1 << MAX_OPEN_LG2)
19
20 #define FILENAME_MAX_LG2 8
21 #define FILENAME_MAX     (1 << FILENAME_MAX_LG2)
22
23 #define BLOCK_SIZE(fs)   (1 << fs->block_shift)
24 #define SECTOR_SIZE(fs)  (1 << fs->sector_shift)
25
26 struct fs_info {
27     const struct fs_ops *fs_ops;
28     struct device *fs_dev;
29     void *fs_info;             /* The fs-specific information */
30     int sector_shift;
31     int block_shift;
32 };
33
34 extern struct fs_info *this_fs;
35
36 struct dirent;                  /* Directory entry structure */
37 struct file;
38 enum fs_flags {
39     FS_NODEV   = 1 << 0,
40     FS_USEMEM  = 1 << 1,         /* If we need a malloc routine, set it */
41
42  /* 
43   * Update the this_inode pointer at each part of path searching. This 
44   * flag is just used for FAT and ISO fs for now.
45   */
46     FS_THISIND = 1 << 2,        
47 };
48
49 struct fs_ops {
50     /* in fact, we use fs_ops structure to find the right fs */
51     const char *fs_name;
52     enum fs_flags fs_flags;
53     
54     int      (*fs_init)(struct fs_info *);
55     void     (*searchdir)(char *, struct file *);
56     uint32_t (*getfssec)(struct file *, char *, int, bool *);
57     void     (*close_file)(struct file *);
58     void     (*mangle_name)(char *, const char *);
59     char *   (*unmangle_name)(char *, const char *);
60     int      (*load_config)();
61
62     struct inode * (*iget_root)(void);
63     struct inode * (*iget_current)(void);
64     struct inode * (*iget)(char *, struct inode *);
65     char * (*follow_symlink)(struct inode *, const char *);
66
67     /* the _dir_ stuff */
68     struct dirent * (*readdir)(struct file *);
69 };
70
71 enum inode_mode {I_FILE, I_DIR, I_SYMLINK};
72
73 /* 
74  * The inode structure, including the detail file information 
75  */
76 struct inode {
77     int          mode;   /* FILE , DIR or SYMLINK */
78     uint32_t     size;
79     uint32_t     ino;    /* Inode number */
80     uint32_t     atime;  /* Access time */
81     uint32_t     mtime;  /* Modify time */
82     uint32_t     ctime;  /* Create time */
83     uint32_t     dtime;  /* Delete time */
84     int          blocks; /* How many blocks the file take */
85     uint32_t *   data;   /* The block address array where the file stored */
86     uint32_t     flags;
87     uint32_t     file_acl;
88 };
89
90 extern struct inode *this_inode;
91
92 struct open_file_t;
93
94 struct file {
95     struct fs_info *fs;
96     union {
97         /* For the new universal-path_lookup */
98         struct {
99             struct inode *inode;        /* The file-specific information */
100             uint32_t offset;            /* for next read */
101         };
102
103         /* For the old searhdir method */
104         struct {
105             struct open_file_t *open_file;/* The fs-specific open file struct */
106             uint32_t file_len;
107         };
108     };
109 };
110
111
112 enum dev_type {CHS, EDD};
113
114 /*
115  * Generic functions that filesystem drivers may choose to use
116  */
117 void generic_mangle_name(char *, const char *);
118 #define generic_unmangle_name stpcpy
119
120 /*
121  * Struct device contains:
122  *     the pointer points to the disk structure,
123  *     the cache stuff.
124  */
125 struct device {
126     struct disk *disk;
127
128     /* the cache stuff */
129     char* cache_data;
130     void* cache_head;
131     uint16_t cache_block_size;
132     uint16_t cache_entries;
133     uint32_t cache_size;
134 };
135
136 /*
137  * Our definition of "not whitespace"
138  */
139 static inline bool not_whitespace(char c)
140 {
141   return (unsigned char)c > ' ';
142 }
143
144 static inline void free_inode(struct inode * inode)
145 {
146     if (inode) {
147         if (inode->data)
148             free(inode->data);
149         free(inode);
150     }
151 }
152
153 static inline void malloc_error(char *obj)
154 {
155         printf("Out of memory: can't allocate memory for %s\n", obj);
156 }
157
158 /* 
159  * functions
160  */
161 void mangle_name(com32sys_t *);
162 void searchdir(com32sys_t *);
163 void _close_file(struct file *);
164 inline uint16_t file_to_handle(struct file *);
165 inline struct file *handle_to_file(uint16_t);
166
167 #endif /* FS_H */