Merging upstream version 5.01+dfsg.
[syslinux-debian/hramrach.git] / core / include / fs.h
blob5c13f5b590af1532cc283c6db23abdf7065ab541
1 #ifndef FS_H
2 #define FS_H
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 <dprintf.h>
11 #include "core.h"
12 #include "disk.h"
15 * Maximum number of open files. This is *currently* constrained by the
16 * fact that PXE needs to be able to fit all its packet buffers into a
17 * 64K segment; this should be fixed by moving the packet buffers to high
18 * memory.
20 #define MAX_OPEN_LG2 5
21 #define MAX_OPEN (1 << MAX_OPEN_LG2)
23 #define FILENAME_MAX_LG2 8
24 #define FILENAME_MAX (1 << FILENAME_MAX_LG2)
26 #define CURRENTDIR_MAX FILENAME_MAX
28 #define BLOCK_SIZE(fs) ((fs)->block_size)
29 #define BLOCK_SHIFT(fs) ((fs)->block_shift)
30 #define SECTOR_SIZE(fs) ((fs)->sector_size)
31 #define SECTOR_SHIFT(fs) ((fs)->sector_shift)
33 struct fs_info {
34 const struct fs_ops *fs_ops;
35 struct device *fs_dev;
36 void *fs_info; /* The fs-specific information */
37 int sector_shift, sector_size;
38 int block_shift, block_size;
39 struct inode *root, *cwd; /* Root and current directories */
40 char cwd_name[CURRENTDIR_MAX]; /* Current directory by name */
43 extern struct fs_info *this_fs;
45 struct dirent; /* Directory entry structure */
46 struct file;
47 enum fs_flags {
48 FS_NODEV = 1 << 0,
49 FS_USEMEM = 1 << 1, /* If we need a malloc routine, set it */
50 FS_THISIND = 1 << 2, /* Set cwd based on config file location */
53 struct fs_ops {
54 /* in fact, we use fs_ops structure to find the right fs */
55 const char *fs_name;
56 enum fs_flags fs_flags;
58 int (*fs_init)(struct fs_info *);
59 void (*searchdir)(const char *, struct file *);
60 uint32_t (*getfssec)(struct file *, char *, int, bool *);
61 void (*close_file)(struct file *);
62 void (*mangle_name)(char *, const char *);
63 size_t (*realpath)(struct fs_info *, char *, const char *, size_t);
64 int (*chdir)(struct fs_info *, const char *);
65 int (*chdir_start)(void);
66 int (*open_config)(struct com32_filedata *);
68 struct inode * (*iget_root)(struct fs_info *);
69 struct inode * (*iget)(const char *, struct inode *);
70 int (*readlink)(struct inode *, char *);
72 /* the _dir_ stuff */
73 int (*readdir)(struct file *, struct dirent *);
75 int (*next_extent)(struct inode *, uint32_t);
77 int (*copy_super)(void *buf);
81 * Extent structure: contains the mapping of some chunk of a file
82 * that is contiguous on disk.
84 struct extent {
85 sector_t pstart; /* Physical start sector */
86 uint32_t lstart; /* Logical start sector */
87 uint32_t len; /* Number of contiguous sectors */
90 /* Special sector numbers used for struct extent.pstart */
91 #define EXTENT_ZERO ((sector_t)-1) /* All-zero extent */
92 #define EXTENT_VOID ((sector_t)-2) /* Invalid information */
94 #define EXTENT_SPECIAL(x) ((x) >= EXTENT_VOID)
96 /*
97 * The inode structure, including the detail file information
99 struct inode {
100 struct fs_info *fs; /* The filesystem this inode is associated with */
101 struct inode *parent; /* Parent directory, if any */
102 const char *name; /* Name, valid for generic path search only */
103 int refcnt;
104 int mode; /* FILE , DIR or SYMLINK */
105 uint64_t size;
106 uint64_t blocks; /* How many blocks the file take */
107 uint64_t ino; /* Inode number */
108 uint32_t atime; /* Access time */
109 uint32_t mtime; /* Modify time */
110 uint32_t ctime; /* Create time */
111 uint32_t dtime; /* Delete time */
112 uint32_t flags;
113 uint32_t file_acl;
114 struct extent this_extent, next_extent;
115 char pvt[0]; /* Private filesystem data */
118 struct file {
119 struct fs_info *fs;
120 uint32_t offset; /* for next read */
121 struct inode *inode; /* The file-specific information */
125 * Struct device contains:
126 * the pointer points to the disk structure,
127 * the cache stuff.
129 struct cache;
131 struct device {
132 struct disk *disk;
134 /* the cache stuff */
135 char *cache_data;
136 struct cache *cache_head;
137 uint16_t cache_block_size;
138 uint16_t cache_entries;
139 uint32_t cache_size;
143 * Our definition of "not whitespace"
145 static inline bool not_whitespace(char c)
147 return (unsigned char)c > ' ';
151 * Inode allocator/deallocator
153 struct inode *alloc_inode(struct fs_info *fs, uint32_t ino, size_t data);
154 static inline void free_inode(struct inode * inode)
156 free(inode);
159 static inline struct inode *get_inode(struct inode *inode)
161 inode->refcnt++;
162 dprintf("get_inode %p name %s refcnt %d\n",
163 inode, inode->name, inode->refcnt);
164 return inode;
167 void put_inode(struct inode *inode);
169 static inline void malloc_error(char *obj)
171 printf("Out of memory: can't allocate memory for %s\n", obj);
172 kaboom();
176 * File handle conversion functions
178 extern struct file files[];
179 static inline uint16_t file_to_handle(struct file *file)
181 return file ? (file - files)+1 : 0;
183 static inline struct file *handle_to_file(uint16_t handle)
185 return handle ? &files[handle-1] : NULL;
188 extern char *PATH;
190 /* fs.c */
191 void pm_mangle_name(com32sys_t *);
192 void pm_searchdir(com32sys_t *);
193 void mangle_name(char *, const char *);
194 int searchdir(const char *name);
195 void _close_file(struct file *);
196 size_t pmapi_read_file(uint16_t *handle, void *buf, size_t sectors);
197 int open_file(const char *name, struct com32_filedata *filedata);
198 void pm_open_file(com32sys_t *);
199 void close_file(uint16_t handle);
200 void pm_close_file(com32sys_t *);
201 int open_config(void);
203 /* chdir.c */
204 void pm_realpath(com32sys_t *regs);
205 size_t realpath(char *dst, const char *src, size_t bufsize);
206 int chdir(const char *src);
208 /* readdir.c */
209 DIR *opendir(const char *pathname);
210 struct dirent *readdir(DIR *dir);
211 int closedir(DIR *dir);
213 /* getcwd.c */
214 char *core_getcwd(char *buf, size_t size);
217 * Generic functions that filesystem drivers may choose to use
220 /* chdir.c */
221 int generic_chdir_start(void);
223 /* mangle.c */
224 void generic_mangle_name(char *, const char *);
226 /* loadconfig.c */
227 int search_dirs(struct com32_filedata *filedata,
228 const char *search_directores[], const char *filenames[],
229 char *realname);
230 int generic_open_config(struct com32_filedata *filedata);
232 /* close.c */
233 void generic_close_file(struct file *file);
235 /* getfssec.c */
236 uint32_t generic_getfssec(struct file *file, char *buf,
237 int sectors, bool *have_more);
239 /* nonextextent.c */
240 int no_next_extent(struct inode *, uint32_t);
242 #endif /* FS_H */