more changes on original files
[linux-2.4.git] / fs / jffs2 / nodelist.h
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
11  * The contents of this file are subject to the Red Hat eCos Public
12  * License Version 1.1 (the "Licence"); you may not use this file
13  * except in compliance with the Licence.  You may obtain a copy of
14  * the Licence at http://www.redhat.com/
15  *
16  * Software distributed under the Licence is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18  * See the Licence for the specific language governing rights and
19  * limitations under the Licence.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the RHEPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the RHEPL or the GPL.
33  *
34  * $Id: nodelist.h,v 1.46.2.5 2003/11/02 13:54:20 dwmw2 Exp $
35  *
36  */
37
38 #include <linux/config.h>
39 #include <linux/fs.h>
40
41 #include <linux/jffs2_fs_sb.h>
42 #include <linux/jffs2_fs_i.h>
43
44 #ifndef CONFIG_JFFS2_FS_DEBUG
45 #define CONFIG_JFFS2_FS_DEBUG 2
46 #endif
47
48 #if CONFIG_JFFS2_FS_DEBUG > 0
49 #define D1(x) x
50 #else
51 #define D1(x)
52 #endif
53
54 #if CONFIG_JFFS2_FS_DEBUG > 1
55 #define D2(x) x
56 #else
57 #define D2(x)
58 #endif
59
60 /*
61   This is all we need to keep in-core for each raw node during normal
62   operation. As and when we do read_inode on a particular inode, we can
63   scan the nodes which are listed for it and build up a proper map of 
64   which nodes are currently valid. JFFSv1 always used to keep that whole
65   map in core for each inode.
66 */
67 struct jffs2_raw_node_ref
68 {
69         struct jffs2_raw_node_ref *next_in_ino; /* Points to the next raw_node_ref
70                 for this inode. If this is the last, it points to the inode_cache
71                 for this inode instead. The inode_cache will have NULL in the first
72                 word so you know when you've got there :) */
73         struct jffs2_raw_node_ref *next_phys;
74         //      __u32 ino;
75         __u32 flash_offset;
76         __u32 totlen;
77 //      __u16 nodetype;
78         
79         /* flash_offset & 3 always has to be zero, because nodes are
80            always aligned at 4 bytes. So we have a couple of extra bits
81            to play with. So we set the least significant bit to 1 to
82            signify that the node is obsoleted by later nodes.
83         */
84 };
85
86 /* 
87    Used for keeping track of deletion nodes &c, which can only be marked
88    as obsolete when the node which they mark as deleted has actually been 
89    removed from the flash.
90 */
91 struct jffs2_raw_node_ref_list {
92         struct jffs2_raw_node_ref *rew;
93         struct jffs2_raw_node_ref_list *next;
94 };
95
96 /* For each inode in the filesystem, we need to keep a record of
97    nlink, because it would be a PITA to scan the whole directory tree
98    at read_inode() time to calculate it, and to keep sufficient information
99    in the raw_node_ref (basically both parent and child inode number for 
100    dirent nodes) would take more space than this does. We also keep
101    a pointer to the first physical node which is part of this inode, too.
102 */
103 struct jffs2_inode_cache {
104         struct jffs2_scan_info *scan; /* Used during scan to hold
105                 temporary lists of nodes, and later must be set to
106                 NULL to mark the end of the raw_node_ref->next_in_ino
107                 chain. */
108         struct jffs2_inode_cache *next;
109         struct jffs2_raw_node_ref *nodes;
110         __u32 ino;
111         int nlink;
112 };
113
114 struct jffs2_scan_info {
115         struct jffs2_full_dirent *dents;
116         struct jffs2_tmp_dnode_info *tmpnodes;
117 };
118 /*
119   Larger representation of a raw node, kept in-core only when the 
120   struct inode for this particular ino is instantiated.
121 */
122
123 struct jffs2_full_dnode
124 {
125         struct jffs2_raw_node_ref *raw;
126         __u32 ofs; /* Don't really need this, but optimisation */
127         __u32 size;
128         __u32 frags; /* Number of fragments which currently refer
129                         to this node. When this reaches zero, 
130                         the node is obsolete.
131                      */
132 };
133
134 /* 
135    Even larger representation of a raw node, kept in-core only while
136    we're actually building up the original map of which nodes go where,
137    in read_inode()
138 */
139 struct jffs2_tmp_dnode_info
140 {
141         struct jffs2_tmp_dnode_info *next;
142         struct jffs2_full_dnode *fn;
143         __u32 version;
144 };       
145
146 struct jffs2_full_dirent
147 {
148         struct jffs2_raw_node_ref *raw;
149         struct jffs2_full_dirent *next;
150         __u32 version;
151         __u32 ino; /* == zero for unlink */
152         unsigned int nhash;
153         unsigned char type;
154         unsigned char name[0];
155 };
156 /*
157   Fragments - used to build a map of which raw node to obtain 
158   data from for each part of the ino
159 */
160 struct jffs2_node_frag
161 {
162         struct jffs2_node_frag *next;
163         struct jffs2_full_dnode *node; /* NULL for holes */
164         __u32 size;
165         __u32 ofs; /* Don't really need this, but optimisation */
166 };
167
168 struct jffs2_eraseblock
169 {
170         struct list_head list;
171         int bad_count;
172         __u32 offset;           /* of this block in the MTD */
173
174         __u32 used_size;
175         __u32 dirty_size;
176         __u32 free_size;        /* Note that sector_size - free_size
177                                    is the address of the first free space */
178         struct jffs2_raw_node_ref *first_node;
179         struct jffs2_raw_node_ref *last_node;
180
181         struct jffs2_raw_node_ref *gc_node;     /* Next node to be garbage collected */
182
183         /* For deletia. When a dirent node in this eraseblock is
184            deleted by a node elsewhere, that other node can only 
185            be marked as obsolete when this block is actually erased.
186            So we keep a list of the nodes to mark as obsolete when
187            the erase is completed.
188         */
189         // MAYBE        struct jffs2_raw_node_ref_list *deletia;
190 };
191
192 #define ACCT_SANITY_CHECK(c, jeb) do { \
193         if (jeb->used_size + jeb->dirty_size + jeb->free_size != c->sector_size) { \
194                 printk(KERN_NOTICE "Eeep. Space accounting for block at 0x%08x is screwed\n", jeb->offset); \
195                 printk(KERN_NOTICE "free 0x%08x + dirty 0x%08x + used %08x != total %08x\n", \
196                 jeb->free_size, jeb->dirty_size, jeb->used_size, c->sector_size); \
197                 BUG(); \
198         } \
199         if (c->used_size + c->dirty_size + c->free_size + c->erasing_size + c->bad_size != c->flash_size) { \
200                 printk(KERN_NOTICE "Eeep. Space accounting superblock info is screwed\n"); \
201                 printk(KERN_NOTICE "free 0x%08x + dirty 0x%08x + used %08x + erasing %08x + bad %08x != total %08x\n", \
202                 c->free_size, c->dirty_size, c->used_size, c->erasing_size, c->bad_size, c->flash_size); \
203                 BUG(); \
204         } \
205 } while(0)
206
207 #define ACCT_PARANOIA_CHECK(jeb) do { \
208                 __u32 my_used_size = 0; \
209                 struct jffs2_raw_node_ref *ref2 = jeb->first_node; \
210                 while (ref2) { \
211                         if (!(ref2->flash_offset & 1)) \
212                                 my_used_size += ref2->totlen; \
213                         ref2 = ref2->next_phys; \
214                 } \
215                 if (my_used_size != jeb->used_size) { \
216                         printk(KERN_NOTICE "Calculated used size %08x != stored used size %08x\n", my_used_size, jeb->used_size); \
217                         BUG(); \
218                 } \
219         } while(0)
220
221 //REX:
222 #if 0
223 #define ALLOC_NORMAL    0       /* Normal allocation */
224 #define ALLOC_DELETION  1       /* Deletion node. Best to allow it */
225 #define ALLOC_GC        2       /* Space requested for GC. Give it or die */
226
227 #define JFFS2_RESERVED_BLOCKS_BASE 3                                            /* Number of free blocks there must be before we... */
228 #define JFFS2_RESERVED_BLOCKS_WRITE (JFFS2_RESERVED_BLOCKS_BASE + 2)            /* ... allow a normal filesystem write */
229 #define JFFS2_RESERVED_BLOCKS_DELETION (JFFS2_RESERVED_BLOCKS_BASE + 1)         /* ... allow a normal filesystem deletion */
230 #define JFFS2_RESERVED_BLOCKS_GCTRIGGER (JFFS2_RESERVED_BLOCKS_BASE + 3)        /* ... wake up the GC thread */
231 #define JFFS2_RESERVED_BLOCKS_GCBAD (JFFS2_RESERVED_BLOCKS_BASE + 1)            /* ... pick a block from the bad_list to GC */
232 #define JFFS2_RESERVED_BLOCKS_GCMERGE (JFFS2_RESERVED_BLOCKS_BASE)              /* ... merge pages when garbage collecting */
233 #endif
234
235 #define ALLOC_NORMAL    0       /* Normal allocation */
236 #define ALLOC_DELETION  1       /* Deletion node. Best to allow it */
237 #define ALLOC_GC        2       /* Space requested for GC. Give it or die */
238
239 #define JFFS2_RESERVED_BLOCKS_BASE 0                                            /* Number of free blocks there must be before we... */
240 #define JFFS2_RESERVED_BLOCKS_WRITE (JFFS2_RESERVED_BLOCKS_BASE + 1)            /* ... allow a normal filesystem write */
241 #define JFFS2_RESERVED_BLOCKS_DELETION (JFFS2_RESERVED_BLOCKS_BASE + 1)         /* ... allow a normal filesystem deletion */
242 #define JFFS2_RESERVED_BLOCKS_GCTRIGGER (JFFS2_RESERVED_BLOCKS_BASE + 2)        /* ... wake up the GC thread */
243 #define JFFS2_RESERVED_BLOCKS_GCBAD (JFFS2_RESERVED_BLOCKS_BASE + 1)            /* ... pick a block from the bad_list to GC */
244 #define JFFS2_RESERVED_BLOCKS_GCMERGE (JFFS2_RESERVED_BLOCKS_BASE)              /* ... merge pages when garbage collecting */
245
246
247
248 #define PAD(x) (((x)+3)&~3)
249
250 static inline struct jffs2_inode_cache *jffs2_raw_ref_to_ic(struct jffs2_raw_node_ref *raw)
251 {
252         while(raw->next_in_ino) {
253                 raw = raw->next_in_ino;
254         }
255
256         return ((struct jffs2_inode_cache *)raw);
257 }
258
259 /* nodelist.c */
260 D1(void jffs2_print_frag_list(struct jffs2_inode_info *f));
261 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list);
262 void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list);
263 int jffs2_get_inode_nodes(struct jffs2_sb_info *c, ino_t ino, struct jffs2_inode_info *f,
264                           struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
265                           __u32 *highest_version, __u32 *latest_mctime,
266                           __u32 *mctime_ver);
267 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino);
268 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new);
269 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old);
270 void jffs2_free_ino_caches(struct jffs2_sb_info *c);
271 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c);
272
273 /* nodemgmt.c */
274 int jffs2_reserve_space(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len, int prio);
275 int jffs2_reserve_space_gc(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len);
276 int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new, __u32 len, int dirty);
277 void jffs2_complete_reservation(struct jffs2_sb_info *c);
278 void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *raw);
279
280 /* write.c */
281 struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri);
282 struct jffs2_full_dnode *jffs2_write_dnode(struct inode *inode, struct jffs2_raw_inode *ri, const unsigned char *data, __u32 datalen, __u32 flash_ofs,  __u32 *writelen);
283 struct jffs2_full_dirent *jffs2_write_dirent(struct inode *inode, struct jffs2_raw_dirent *rd, const unsigned char *name, __u32 namelen, __u32 flash_ofs,  __u32 *writelen);
284
285 /* readinode.c */
286 void jffs2_truncate_fraglist (struct jffs2_sb_info *c, struct jffs2_node_frag **list, __u32 size);
287 int jffs2_add_full_dnode_to_fraglist(struct jffs2_sb_info *c, struct jffs2_node_frag **list, struct jffs2_full_dnode *fn);
288 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn);
289 void jffs2_read_inode (struct inode *);
290 void jffs2_clear_inode (struct inode *);
291
292 /* malloc.c */
293 void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn);
294 void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd);
295
296 int jffs2_create_slab_caches(void);
297 void jffs2_destroy_slab_caches(void);
298
299 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize);
300 void jffs2_free_full_dirent(struct jffs2_full_dirent *);
301 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void);
302 void jffs2_free_full_dnode(struct jffs2_full_dnode *);
303 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void);
304 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *);
305 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void);
306 void jffs2_free_raw_inode(struct jffs2_raw_inode *);
307 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void);
308 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *);
309 struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void);
310 void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *);
311 struct jffs2_node_frag *jffs2_alloc_node_frag(void);
312 void jffs2_free_node_frag(struct jffs2_node_frag *);
313 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void);
314 void jffs2_free_inode_cache(struct jffs2_inode_cache *);
315
316 /* gc.c */
317 int jffs2_garbage_collect_pass(struct jffs2_sb_info *c);
318
319 /* background.c */
320 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
321 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
322 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
323
324 /* dir.c */
325 extern struct file_operations jffs2_dir_operations;
326 extern struct inode_operations jffs2_dir_inode_operations;
327
328 /* file.c */
329 extern struct file_operations jffs2_file_operations;
330 extern struct inode_operations jffs2_file_inode_operations;
331 extern struct address_space_operations jffs2_file_address_operations;
332 int jffs2_null_fsync(struct file *, struct dentry *, int);
333 int jffs2_setattr (struct dentry *dentry, struct iattr *iattr);
334 int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg);
335 int jffs2_do_readpage_unlock (struct inode *inode, struct page *pg);
336 int jffs2_readpage (struct file *, struct page *);
337 int jffs2_prepare_write (struct file *, struct page *, unsigned, unsigned);
338 int jffs2_commit_write (struct file *, struct page *, unsigned, unsigned);
339
340 /* ioctl.c */
341 int jffs2_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
342
343 /* read.c */
344 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_full_dnode *fd, unsigned char *buf, int ofs, int len);
345
346 /* compr.c */
347 unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out, 
348                              __u32 *datalen, __u32 *cdatalen);
349 int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
350                      unsigned char *data_out, __u32 cdatalen, __u32 datalen);
351
352 /* scan.c */
353 int jffs2_scan_medium(struct jffs2_sb_info *c);
354
355 /* build.c */
356 int jffs2_build_filesystem(struct jffs2_sb_info *c);
357
358 /* symlink.c */
359 extern struct inode_operations jffs2_symlink_inode_operations;
360
361 /* erase.c */
362 void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
363 void jffs2_erase_pending_blocks(struct jffs2_sb_info *c);
364 void jffs2_mark_erased_blocks(struct jffs2_sb_info *c);
365 void jffs2_erase_pending_trigger(struct jffs2_sb_info *c);
366
367 /* compr_zlib.c */
368 int jffs2_zlib_init(void);
369 void jffs2_zlib_exit(void);