PATH: use a linked list internally
[syslinux.git] / core / include / fs.h
blobb5c7f0d9dbf807feb12a03b50b4cb08656584497
1 #ifndef FS_H
2 #define FS_H
4 #include <linux/list.h>
5 #include <stddef.h>
6 #include <stdbool.h>
7 #include <string.h>
8 #include <com32.h>
9 #include <stdio.h>
10 #include <sys/dirent.h>
11 #include <dprintf.h>
12 #include "core.h"
13 #include "disk.h"
16 * Maximum number of open files.
18 #define MAX_OPEN_LG2 7
19 #define MAX_OPEN (1 << MAX_OPEN_LG2)
21 #define FILENAME_MAX_LG2 8
22 #define FILENAME_MAX (1 << FILENAME_MAX_LG2)
24 #define CURRENTDIR_MAX FILENAME_MAX
26 #define BLOCK_SIZE(fs) ((fs)->block_size)
27 #define BLOCK_SHIFT(fs) ((fs)->block_shift)
28 #define SECTOR_SIZE(fs) ((fs)->sector_size)
29 #define SECTOR_SHIFT(fs) ((fs)->sector_shift)
31 struct fs_info {
32 const struct fs_ops *fs_ops;
33 struct device *fs_dev;
34 void *fs_info; /* The fs-specific information */
35 int sector_shift, sector_size;
36 int block_shift, block_size;
37 struct inode *root, *cwd; /* Root and current directories */
38 char cwd_name[CURRENTDIR_MAX]; /* Current directory by name */
41 extern struct fs_info *this_fs;
43 struct dirent; /* Directory entry structure */
44 struct file;
45 enum fs_flags {
46 FS_NODEV = 1 << 0,
47 FS_USEMEM = 1 << 1, /* If we need a malloc routine, set it */
48 FS_THISIND = 1 << 2, /* Set cwd based on config file location */
51 struct fs_ops {
52 /* in fact, we use fs_ops structure to find the right fs */
53 const char *fs_name;
54 enum fs_flags fs_flags;
56 int (*fs_init)(struct fs_info *);
57 void (*searchdir)(const char *, int, struct file *);
58 uint32_t (*getfssec)(struct file *, char *, int, bool *);
59 void (*close_file)(struct file *);
60 void (*mangle_name)(char *, const char *);
61 size_t (*realpath)(struct fs_info *, char *, const char *, size_t);
62 int (*chdir)(struct fs_info *, const char *);
63 int (*chdir_start)(void);
64 int (*open_config)(struct com32_filedata *);
66 struct inode * (*iget_root)(struct fs_info *);
67 struct inode * (*iget)(const char *, struct inode *);
68 int (*readlink)(struct inode *, char *);
70 /* the _dir_ stuff */
71 int (*readdir)(struct file *, struct dirent *);
73 int (*next_extent)(struct inode *, uint32_t);
75 int (*copy_super)(void *buf);
79 * Extent structure: contains the mapping of some chunk of a file
80 * that is contiguous on disk.
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 */
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 */
92 #define EXTENT_SPECIAL(x) ((x) >= EXTENT_VOID)
94 /*
95 * The inode structure, including the detail file information
97 struct inode {
98 struct fs_info *fs; /* The filesystem this inode is associated with */
99 struct inode *parent; /* Parent directory, if any */
100 const char *name; /* Name, valid for generic path search only */
101 int refcnt;
102 int mode; /* FILE , DIR or SYMLINK */
103 uint64_t size;
104 uint64_t blocks; /* How many blocks the file take */
105 uint64_t ino; /* Inode number */
106 uint32_t atime; /* Access time */
107 uint32_t mtime; /* Modify time */
108 uint32_t ctime; /* Create time */
109 uint32_t dtime; /* Delete time */
110 uint32_t flags;
111 uint32_t file_acl;
112 struct extent this_extent, next_extent;
113 char pvt[0]; /* Private filesystem data */
116 struct file {
117 struct fs_info *fs;
118 uint32_t offset; /* for next read */
119 struct inode *inode; /* The file-specific information */
123 * Struct device contains:
124 * the pointer points to the disk structure,
125 * the cache stuff.
127 struct cache;
129 struct device {
130 struct disk *disk;
132 /* the cache stuff */
133 char *cache_data;
134 struct cache *cache_head;
135 uint16_t cache_block_size;
136 uint16_t cache_entries;
137 uint32_t cache_size;
141 * Our definition of "not whitespace"
143 static inline bool not_whitespace(char c)
145 return (unsigned char)c > ' ';
149 * Inode allocator/deallocator
151 struct inode *alloc_inode(struct fs_info *fs, uint32_t ino, size_t data);
152 static inline void free_inode(struct inode * inode)
154 free(inode);
157 static inline struct inode *get_inode(struct inode *inode)
159 inode->refcnt++;
160 dprintf("get_inode %p name %s refcnt %d\n",
161 inode, inode->name, inode->refcnt);
162 return inode;
165 void put_inode(struct inode *inode);
167 static inline void malloc_error(char *obj)
169 printf("Out of memory: can't allocate memory for %s\n", obj);
170 kaboom();
174 * File handle conversion functions
176 extern struct file files[];
177 static inline uint16_t file_to_handle(struct file *file)
179 return file ? (file - files)+1 : 0;
181 static inline struct file *handle_to_file(uint16_t handle)
183 return handle ? &files[handle-1] : NULL;
186 struct path_entry {
187 struct list_head list;
188 const char *str;
191 extern struct list_head PATH;
193 extern struct path_entry *path_add(const char *str);
195 /* fs.c */
196 void pm_mangle_name(com32sys_t *);
197 void pm_searchdir(com32sys_t *);
198 void mangle_name(char *, const char *);
199 int searchdir(const char *name, int flags);
200 void _close_file(struct file *);
201 size_t pmapi_read_file(uint16_t *handle, void *buf, size_t sectors);
202 int open_file(const char *name, int flags, struct com32_filedata *filedata);
203 void pm_open_file(com32sys_t *);
204 void close_file(uint16_t handle);
205 void pm_close_file(com32sys_t *);
206 int open_config(void);
208 /* chdir.c */
209 void pm_realpath(com32sys_t *regs);
210 size_t realpath(char *dst, const char *src, size_t bufsize);
211 int chdir(const char *src);
213 /* readdir.c */
214 DIR *opendir(const char *pathname);
215 struct dirent *readdir(DIR *dir);
216 int closedir(DIR *dir);
218 /* getcwd.c */
219 char *core_getcwd(char *buf, size_t size);
222 * Generic functions that filesystem drivers may choose to use
225 /* chdir.c */
226 int generic_chdir_start(void);
228 /* mangle.c */
229 void generic_mangle_name(char *, const char *);
231 /* loadconfig.c */
232 int search_dirs(struct com32_filedata *filedata,
233 const char *search_directores[], const char *filenames[],
234 char *realname);
235 int generic_open_config(struct com32_filedata *filedata);
237 /* close.c */
238 void generic_close_file(struct file *file);
240 /* getfssec.c */
241 uint32_t generic_getfssec(struct file *file, char *buf,
242 int sectors, bool *have_more);
244 /* nonextextent.c */
245 int no_next_extent(struct inode *, uint32_t);
247 #endif /* FS_H */