Add 32-bit versions of open file/close file
[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 <stdio.h>
9 #include <sys/dirent.h>
10 #include "core.h"
11 #include "disk.h"
12
13 /*
14  * Maximum number of open files.  This is *currently* constrained by the
15  * fact that PXE needs to be able to fit all its packet buffers into a
16  * 64K segment; this should be fixed by moving the packet buffers to high
17  * memory.
18  */
19 #define MAX_OPEN_LG2    5
20 #define MAX_OPEN        (1 << MAX_OPEN_LG2)
21
22 #define FILENAME_MAX_LG2 8
23 #define FILENAME_MAX     (1 << FILENAME_MAX_LG2)
24
25 #define CURRENTDIR_MAX  FILENAME_MAX
26
27 #define BLOCK_SIZE(fs)   ((fs)->block_size)
28 #define BLOCK_SHIFT(fs)  ((fs)->block_shift)
29 #define SECTOR_SIZE(fs)  ((fs)->sector_size)
30 #define SECTOR_SHIFT(fs) ((fs)->sector_shift)
31
32 struct fs_info {
33     const struct fs_ops *fs_ops;
34     struct device *fs_dev;
35     void *fs_info;             /* The fs-specific information */
36     int sector_shift, sector_size;
37     int block_shift, block_size;
38     struct inode *root, *cwd;           /* Root and current directories */
39     char cwd_name[CURRENTDIR_MAX];      /* Current directory by name */
40 };
41
42 extern struct fs_info *this_fs;
43
44 struct dirent;                  /* Directory entry structure */
45 struct file;
46 enum fs_flags {
47     FS_NODEV   = 1 << 0,
48     FS_USEMEM  = 1 << 1,        /* If we need a malloc routine, set it */
49     FS_THISIND = 1 << 2,        /* Set cwd based on config file location */
50 };
51
52 struct fs_ops {
53     /* in fact, we use fs_ops structure to find the right fs */
54     const char *fs_name;
55     enum fs_flags fs_flags;
56     
57     int      (*fs_init)(struct fs_info *);
58     void     (*searchdir)(const char *, struct file *);
59     uint32_t (*getfssec)(struct file *, char *, int, bool *);
60     void     (*close_file)(struct file *);
61     void     (*mangle_name)(char *, const char *);
62     size_t   (*realpath)(struct fs_info *, char *, const char *, size_t);
63     int      (*chdir)(struct fs_info *, const char *);
64     int      (*load_config)(void);
65
66     struct inode * (*iget_root)(struct fs_info *);
67     struct inode * (*iget)(const char *, struct inode *);
68     int      (*readlink)(struct inode *, char *);
69
70     /* the _dir_ stuff */
71     struct dirent * (*readdir)(struct file *);
72
73     int      (*next_extent)(struct inode *, uint32_t);
74 };
75
76 enum inode_mode {I_FILE, I_DIR, I_SYMLINK};
77
78 /*
79  * Extent structure: contains the mapping of some chunk of a file
80  * that is contiguous on disk.
81  */
82 struct extent {
83     sector_t    pstart;         /* Physical start sector */
84     uint32_t    lstart;         /* Logical start sector */
85     uint32_t    len;            /* Number of contiguous sectors */
86 };
87
88 /* Special sector numbers used for struct extent.pstart */
89 #define EXTENT_ZERO     ((sector_t)-1) /* All-zero extent */
90 #define EXTENT_VOID     ((sector_t)-2) /* Invalid information */
91
92 #define EXTENT_SPECIAL(x)       ((x) >= EXTENT_VOID)
93
94 /* 
95  * The inode structure, including the detail file information 
96  */
97 struct inode {
98     struct fs_info *fs;  /* The filesystem this inode is associated with */
99     int          refcnt;
100     int          mode;   /* FILE , DIR or SYMLINK */
101     uint32_t     size;
102     uint32_t     blocks; /* How many blocks the file take */
103     uint32_t     ino;    /* Inode number */
104     uint32_t     atime;  /* Access time */
105     uint32_t     mtime;  /* Modify time */
106     uint32_t     ctime;  /* Create time */
107     uint32_t     dtime;  /* Delete time */
108     uint32_t     flags;
109     uint32_t     file_acl;
110     struct extent this_extent, next_extent;
111     char         pvt[0]; /* Private filesystem data */
112 };
113
114 struct open_file_t;
115
116 struct file {
117     struct fs_info *fs;
118     uint32_t file_len;
119     union {
120         /* For the new universal-path_lookup */
121         struct {
122             struct inode *inode;        /* The file-specific information */
123             uint32_t offset;            /* for next read */
124         };
125
126         /* For the old searchdir method */
127         struct {
128             struct open_file_t *open_file;/* The fs-specific open file struct */
129         };
130     };
131 };
132
133
134 enum dev_type {CHS, EDD};
135
136 /*
137  * Struct device contains:
138  *     the pointer points to the disk structure,
139  *     the cache stuff.
140  */
141 struct cache;
142
143 struct device {
144     struct disk *disk;
145
146     /* the cache stuff */
147     char *cache_data;
148     struct cache *cache_head;
149     uint16_t cache_block_size;
150     uint16_t cache_entries;
151     uint32_t cache_size;
152 };
153
154 /*
155  * Our definition of "not whitespace"
156  */
157 static inline bool not_whitespace(char c)
158 {
159   return (unsigned char)c > ' ';
160 }
161
162 /*
163  * Inode allocator/deallocator
164  */
165 struct inode *alloc_inode(struct fs_info *fs, uint32_t ino, size_t data);
166 static inline void free_inode(struct inode * inode)
167 {
168     free(inode);
169 }
170
171 static inline struct inode *get_inode(struct inode *inode)
172 {
173     inode->refcnt++;
174     return inode;
175 }
176 static inline void put_inode(struct inode *inode)
177 {
178     if (! --inode->refcnt)
179         free(inode);
180 }
181
182 static inline void malloc_error(char *obj)
183 {
184         printf("Out of memory: can't allocate memory for %s\n", obj);
185         kaboom();
186 }
187
188 /*
189  * File handle conversion functions
190  */
191 extern struct file files[];
192 static inline uint16_t file_to_handle(struct file *file)
193 {
194     return file ? (file - files)+1 : 0;
195 }
196 static inline struct file *handle_to_file(uint16_t handle)
197 {
198     return handle ? &files[handle-1] : NULL;
199 }
200
201 /* fs.c */
202 void pm_mangle_name(com32sys_t *);
203 void pm_searchdir(com32sys_t *);
204 void mangle_name(char *, const char *);
205 int searchdir(const char *name);
206 void _close_file(struct file *);
207 size_t pmapi_read_file(uint16_t *handle, void *buf, size_t sectors);
208 int open_file(const char *name, struct com32_filedata *filedata);
209 void pm_open_file(com32sys_t *);
210 void close_file(uint16_t handle);
211 void pm_close_file(com32sys_t *);
212
213 /* chdir.c */
214 void pm_realpath(com32sys_t *regs);
215 size_t realpath(char *dst, const char *src, size_t bufsize);
216 int chdir(const char *src);
217
218 /* readdir.c */
219 DIR *opendir(const char *pathname);
220 struct dirent *readdir(DIR *dir);
221 int closedir(DIR *dir);
222
223 /*
224  * Generic functions that filesystem drivers may choose to use
225  */
226
227 /* mangle.c */
228 void generic_mangle_name(char *, const char *);
229
230 /* loadconfig.c */
231 int generic_load_config(void);
232
233 /* close.c */
234 void generic_close_file(struct file *file);
235
236 /* getfssec.c */
237 uint32_t generic_getfssec(struct file *file, char *buf,
238                           int sectors, bool *have_more);
239
240 /* nonextextent.c */
241 int no_next_extent(struct inode *, uint32_t);
242
243 #endif /* FS_H */