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 #define ALLOC_NORMAL	0	/* Normal allocation */
222 #define ALLOC_DELETION	1	/* Deletion node. Best to allow it */
223 #define ALLOC_GC	2	/* Space requested for GC. Give it or die */
224 
225 #define JFFS2_RESERVED_BLOCKS_BASE 3						/* Number of free blocks there must be before we... */
226 #define JFFS2_RESERVED_BLOCKS_WRITE (JFFS2_RESERVED_BLOCKS_BASE + 2)		/* ... allow a normal filesystem write */
227 #define JFFS2_RESERVED_BLOCKS_DELETION (JFFS2_RESERVED_BLOCKS_BASE + 1)		/* ... allow a normal filesystem deletion */
228 #define JFFS2_RESERVED_BLOCKS_GCTRIGGER (JFFS2_RESERVED_BLOCKS_BASE + 3)	/* ... wake up the GC thread */
229 #define JFFS2_RESERVED_BLOCKS_GCBAD (JFFS2_RESERVED_BLOCKS_BASE + 1)		/* ... pick a block from the bad_list to GC */
230 #define JFFS2_RESERVED_BLOCKS_GCMERGE (JFFS2_RESERVED_BLOCKS_BASE)		/* ... merge pages when garbage collecting */
231 
232 
233 #define PAD(x) (((x)+3)&~3)
234 
jffs2_raw_ref_to_ic(struct jffs2_raw_node_ref * raw)235 static inline struct jffs2_inode_cache *jffs2_raw_ref_to_ic(struct jffs2_raw_node_ref *raw)
236 {
237 	while(raw->next_in_ino) {
238 		raw = raw->next_in_ino;
239 	}
240 
241 	return ((struct jffs2_inode_cache *)raw);
242 }
243 
244 /* nodelist.c */
245 D1(void jffs2_print_frag_list(struct jffs2_inode_info *f));
246 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list);
247 void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list);
248 int jffs2_get_inode_nodes(struct jffs2_sb_info *c, ino_t ino, struct jffs2_inode_info *f,
249 			  struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
250 			  __u32 *highest_version, __u32 *latest_mctime,
251 			  __u32 *mctime_ver);
252 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino);
253 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new);
254 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old);
255 void jffs2_free_ino_caches(struct jffs2_sb_info *c);
256 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c);
257 
258 /* nodemgmt.c */
259 int jffs2_reserve_space(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len, int prio);
260 int jffs2_reserve_space_gc(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len);
261 int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new, __u32 len, int dirty);
262 void jffs2_complete_reservation(struct jffs2_sb_info *c);
263 void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *raw);
264 
265 /* write.c */
266 struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri);
267 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);
268 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);
269 
270 /* readinode.c */
271 void jffs2_truncate_fraglist (struct jffs2_sb_info *c, struct jffs2_node_frag **list, __u32 size);
272 int jffs2_add_full_dnode_to_fraglist(struct jffs2_sb_info *c, struct jffs2_node_frag **list, struct jffs2_full_dnode *fn);
273 int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn);
274 void jffs2_read_inode (struct inode *);
275 void jffs2_clear_inode (struct inode *);
276 
277 /* malloc.c */
278 void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn);
279 void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd);
280 
281 int jffs2_create_slab_caches(void);
282 void jffs2_destroy_slab_caches(void);
283 
284 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize);
285 void jffs2_free_full_dirent(struct jffs2_full_dirent *);
286 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void);
287 void jffs2_free_full_dnode(struct jffs2_full_dnode *);
288 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void);
289 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *);
290 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void);
291 void jffs2_free_raw_inode(struct jffs2_raw_inode *);
292 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void);
293 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *);
294 struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void);
295 void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *);
296 struct jffs2_node_frag *jffs2_alloc_node_frag(void);
297 void jffs2_free_node_frag(struct jffs2_node_frag *);
298 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void);
299 void jffs2_free_inode_cache(struct jffs2_inode_cache *);
300 
301 /* gc.c */
302 int jffs2_garbage_collect_pass(struct jffs2_sb_info *c);
303 
304 /* background.c */
305 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
306 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
307 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
308 
309 /* dir.c */
310 extern struct file_operations jffs2_dir_operations;
311 extern struct inode_operations jffs2_dir_inode_operations;
312 
313 /* file.c */
314 extern struct file_operations jffs2_file_operations;
315 extern struct inode_operations jffs2_file_inode_operations;
316 extern struct address_space_operations jffs2_file_address_operations;
317 int jffs2_null_fsync(struct file *, struct dentry *, int);
318 int jffs2_setattr (struct dentry *dentry, struct iattr *iattr);
319 int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg);
320 int jffs2_do_readpage_unlock (struct inode *inode, struct page *pg);
321 int jffs2_readpage (struct file *, struct page *);
322 int jffs2_prepare_write (struct file *, struct page *, unsigned, unsigned);
323 int jffs2_commit_write (struct file *, struct page *, unsigned, unsigned);
324 
325 /* ioctl.c */
326 int jffs2_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
327 
328 /* read.c */
329 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_full_dnode *fd, unsigned char *buf, int ofs, int len);
330 
331 /* compr.c */
332 unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out,
333 			     __u32 *datalen, __u32 *cdatalen);
334 int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in,
335 		     unsigned char *data_out, __u32 cdatalen, __u32 datalen);
336 
337 /* scan.c */
338 int jffs2_scan_medium(struct jffs2_sb_info *c);
339 
340 /* build.c */
341 int jffs2_build_filesystem(struct jffs2_sb_info *c);
342 
343 /* symlink.c */
344 extern struct inode_operations jffs2_symlink_inode_operations;
345 
346 /* erase.c */
347 void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
348 void jffs2_erase_pending_blocks(struct jffs2_sb_info *c);
349 void jffs2_mark_erased_blocks(struct jffs2_sb_info *c);
350 void jffs2_erase_pending_trigger(struct jffs2_sb_info *c);
351 
352 /* compr_zlib.c */
353 int jffs2_zlib_init(void);
354 void jffs2_zlib_exit(void);
355