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