1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001, 2002 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.c,v 1.30.2.6 2003/02/24 21:49:33 dwmw2 Exp $
35  *
36  */
37 
38 #include <linux/kernel.h>
39 #include <linux/jffs2.h>
40 #include <linux/fs.h>
41 #include <linux/mtd/mtd.h>
42 #include "nodelist.h"
43 
jffs2_add_fd_to_list(struct jffs2_sb_info * c,struct jffs2_full_dirent * new,struct jffs2_full_dirent ** list)44 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
45 {
46 	struct jffs2_full_dirent **prev = list;
47 	D1(printk(KERN_DEBUG "jffs2_add_fd_to_list( %p, %p (->%p))\n", new, list, *list));
48 
49 	while ((*prev) && (*prev)->nhash <= new->nhash) {
50 		if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
51 			/* Duplicate. Free one */
52 			if (new->version < (*prev)->version) {
53 				D1(printk(KERN_DEBUG "Eep! Marking new dirent node obsolete\n"));
54 				D1(printk(KERN_DEBUG "New dirent is \"%s\"->ino #%u. Old is \"%s\"->ino #%u\n", new->name, new->ino, (*prev)->name, (*prev)->ino));
55 				jffs2_mark_node_obsolete(c, new->raw);
56 				jffs2_free_full_dirent(new);
57 			} else {
58 				D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) obsolete\n", (*prev)->ino));
59 				new->next = (*prev)->next;
60 				jffs2_mark_node_obsolete(c, ((*prev)->raw));
61 				jffs2_free_full_dirent(*prev);
62 				*prev = new;
63 			}
64 			goto out;
65 		}
66 		prev = &((*prev)->next);
67 	}
68 	new->next = *prev;
69 	*prev = new;
70 
71  out:
72 	D2(while(*list) {
73 		printk(KERN_DEBUG "Dirent \"%s\" (hash 0x%08x, ino #%u\n", (*list)->name, (*list)->nhash, (*list)->ino);
74 		list = &(*list)->next;
75 	});
76 }
77 
78 /* Put a new tmp_dnode_info into the list, keeping the list in
79    order of increasing version
80 */
jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info * tn,struct jffs2_tmp_dnode_info ** list)81 void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list)
82 {
83 	struct jffs2_tmp_dnode_info **prev = list;
84 
85 	while ((*prev) && (*prev)->version < tn->version) {
86 		prev = &((*prev)->next);
87 	}
88 	tn->next = (*prev);
89         *prev = tn;
90 }
91 
92 /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
93    with this ino, returning the former in order of version */
94 
jffs2_get_inode_nodes(struct jffs2_sb_info * c,ino_t ino,struct jffs2_inode_info * f,struct jffs2_tmp_dnode_info ** tnp,struct jffs2_full_dirent ** fdp,__u32 * highest_version,__u32 * latest_mctime,__u32 * mctime_ver)95 int jffs2_get_inode_nodes(struct jffs2_sb_info *c, ino_t ino, struct jffs2_inode_info *f,
96 			  struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
97 			  __u32 *highest_version, __u32 *latest_mctime,
98 			  __u32 *mctime_ver)
99 {
100 	struct jffs2_raw_node_ref *ref = f->inocache->nodes;
101 	struct jffs2_tmp_dnode_info *tn, *ret_tn = NULL;
102 	struct jffs2_full_dirent *fd, *ret_fd = NULL;
103 
104 	union jffs2_node_union node;
105 	size_t retlen;
106 	int err;
107 
108 	*mctime_ver = 0;
109 
110 	D1(printk(KERN_DEBUG "jffs2_get_inode_nodes(): ino #%lu\n", ino));
111 	if (!f->inocache->nodes) {
112 		printk(KERN_WARNING "Eep. no nodes for ino #%lu\n", ino);
113 	}
114 	for (ref = f->inocache->nodes; ref && ref->next_in_ino; ref = ref->next_in_ino) {
115 		/* Work out whether it's a data node or a dirent node */
116 		if (ref->flash_offset & 1) {
117 			/* FIXME: On NAND flash we may need to read these */
118 			D1(printk(KERN_DEBUG "node at 0x%08x is obsoleted. Ignoring.\n", ref->flash_offset &~3));
119 			continue;
120 		}
121 		err = c->mtd->read(c->mtd, (ref->flash_offset & ~3), min(ref->totlen, sizeof(node)), &retlen, (void *)&node);
122 		if (err) {
123 			printk(KERN_WARNING "error %d reading node at 0x%08x in get_inode_nodes()\n", err, (ref->flash_offset) & ~3);
124 			goto free_out;
125 		}
126 
127 
128 			/* Check we've managed to read at least the common node header */
129 		if (retlen < min(ref->totlen, sizeof(node.u))) {
130 			printk(KERN_WARNING "short read in get_inode_nodes()\n");
131 			err = -EIO;
132 			goto free_out;
133 		}
134 
135 		switch (node.u.nodetype) {
136 		case JFFS2_NODETYPE_DIRENT:
137 			D1(printk(KERN_DEBUG "Node at %08x is a dirent node\n", ref->flash_offset &~3));
138 			if (retlen < sizeof(node.d)) {
139 				printk(KERN_WARNING "short read in get_inode_nodes()\n");
140 				err = -EIO;
141 				goto free_out;
142 			}
143 			if (node.d.version > *highest_version)
144 				*highest_version = node.d.version;
145 			if (ref->flash_offset & 1) {
146 				/* Obsoleted */
147 				continue;
148 			}
149 			fd = jffs2_alloc_full_dirent(node.d.nsize+1);
150 			if (!fd) {
151 				err = -ENOMEM;
152 				goto free_out;
153 			}
154 			memset(fd,0,sizeof(struct jffs2_full_dirent) + node.d.nsize+1);
155 			fd->raw = ref;
156 			fd->version = node.d.version;
157 			fd->ino = node.d.ino;
158 			fd->type = node.d.type;
159 
160 			/* Pick out the mctime of the latest dirent */
161 			if(fd->version > *mctime_ver) {
162 				*mctime_ver = fd->version;
163 				*latest_mctime = node.d.mctime;
164 			}
165 
166 			/* memcpy as much of the name as possible from the raw
167 			   dirent we've already read from the flash
168 			*/
169 			if (retlen > sizeof(struct jffs2_raw_dirent))
170 				memcpy(&fd->name[0], &node.d.name[0], min((__u32)node.d.nsize, (retlen-sizeof(struct jffs2_raw_dirent))));
171 
172 			/* Do we need to copy any more of the name directly
173 			   from the flash?
174 			*/
175 			if (node.d.nsize + sizeof(struct jffs2_raw_dirent) > retlen) {
176 				int already = retlen - sizeof(struct jffs2_raw_dirent);
177 
178 				err = c->mtd->read(c->mtd, (ref->flash_offset & ~3) + retlen,
179 						   node.d.nsize - already, &retlen, &fd->name[already]);
180 				if (!err && retlen != node.d.nsize - already)
181 					err = -EIO;
182 
183 				if (err) {
184 					printk(KERN_WARNING "Read remainder of name in jffs2_get_inode_nodes(): error %d\n", err);
185 					jffs2_free_full_dirent(fd);
186 					goto free_out;
187 				}
188 			}
189 			fd->nhash = full_name_hash(fd->name, node.d.nsize);
190 			fd->next = NULL;
191 				/* Wheee. We now have a complete jffs2_full_dirent structure, with
192 				   the name in it and everything. Link it into the list
193 				*/
194 			D1(printk(KERN_DEBUG "Adding fd \"%s\", ino #%u\n", fd->name, fd->ino));
195 			jffs2_add_fd_to_list(c, fd, &ret_fd);
196 			break;
197 
198 		case JFFS2_NODETYPE_INODE:
199 			D1(printk(KERN_DEBUG "Node at %08x is a data node\n", ref->flash_offset &~3));
200 			if (retlen < sizeof(node.i)) {
201 				printk(KERN_WARNING "read too short for dnode\n");
202 				err = -EIO;
203 				goto free_out;
204 			}
205 			if (node.i.version > *highest_version)
206 				*highest_version = node.i.version;
207 			D1(printk(KERN_DEBUG "version %d, highest_version now %d\n", node.i.version, *highest_version));
208 
209 			if (ref->flash_offset & 1) {
210 				D1(printk(KERN_DEBUG "obsoleted\n"));
211 				/* Obsoleted */
212 				continue;
213 			}
214 			tn = jffs2_alloc_tmp_dnode_info();
215 			if (!tn) {
216 				D1(printk(KERN_DEBUG "alloc tn failed\n"));
217 				err = -ENOMEM;
218 				goto free_out;
219 			}
220 
221 			tn->fn = jffs2_alloc_full_dnode();
222 			if (!tn->fn) {
223 				D1(printk(KERN_DEBUG "alloc fn failed\n"));
224 				err = -ENOMEM;
225 				jffs2_free_tmp_dnode_info(tn);
226 				goto free_out;
227 			}
228 			tn->version = node.i.version;
229 			tn->fn->ofs = node.i.offset;
230 			/* There was a bug where we wrote hole nodes out with
231 			   csize/dsize swapped. Deal with it */
232 			if (node.i.compr == JFFS2_COMPR_ZERO && !node.i.dsize && node.i.csize)
233 				tn->fn->size = node.i.csize;
234 			else // normal case...
235 				tn->fn->size = node.i.dsize;
236 			tn->fn->raw = ref;
237 			D1(printk(KERN_DEBUG "dnode @%08x: ver %u, offset %04x, dsize %04x\n", ref->flash_offset &~3, node.i.version, node.i.offset, node.i.dsize));
238 			jffs2_add_tn_to_list(tn, &ret_tn);
239 			break;
240 
241 		default:
242 			switch(node.u.nodetype & JFFS2_COMPAT_MASK) {
243 			case JFFS2_FEATURE_INCOMPAT:
244 				printk(KERN_NOTICE "Unknown INCOMPAT nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
245 				break;
246 			case JFFS2_FEATURE_ROCOMPAT:
247 				printk(KERN_NOTICE "Unknown ROCOMPAT nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
248 				break;
249 			case JFFS2_FEATURE_RWCOMPAT_COPY:
250 				printk(KERN_NOTICE "Unknown RWCOMPAT_COPY nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
251 				break;
252 			case JFFS2_FEATURE_RWCOMPAT_DELETE:
253 				printk(KERN_NOTICE "Unknown RWCOMPAT_DELETE nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
254 				break;
255 			}
256 		}
257 	}
258 	*tnp = ret_tn;
259 	*fdp = ret_fd;
260 
261 	return 0;
262 
263  free_out:
264 	jffs2_free_tmp_dnode_info_list(ret_tn);
265 	jffs2_free_full_dirent_list(ret_fd);
266 	return err;
267 }
268 
jffs2_get_ino_cache(struct jffs2_sb_info * c,uint32_t ino)269 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
270 {
271 	struct jffs2_inode_cache *ret;
272 
273 	D2(printk(KERN_DEBUG "jffs2_get_ino_cache(): ino %u\n", ino));
274 	spin_lock (&c->inocache_lock);
275 	ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
276 	while (ret && ret->ino < ino) {
277 		ret = ret->next;
278 	}
279 
280 	spin_unlock(&c->inocache_lock);
281 
282 	if (ret && ret->ino != ino)
283 		ret = NULL;
284 
285 	D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
286 	return ret;
287 }
288 
jffs2_add_ino_cache(struct jffs2_sb_info * c,struct jffs2_inode_cache * new)289 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
290 {
291 	struct jffs2_inode_cache **prev;
292 	D2(printk(KERN_DEBUG "jffs2_add_ino_cache: Add %p (ino #%u)\n", new, new->ino));
293 	spin_lock(&c->inocache_lock);
294 
295 	prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
296 
297 	while ((*prev) && (*prev)->ino < new->ino) {
298 		prev = &(*prev)->next;
299 	}
300 	new->next = *prev;
301 	*prev = new;
302 	spin_unlock(&c->inocache_lock);
303 }
304 
jffs2_del_ino_cache(struct jffs2_sb_info * c,struct jffs2_inode_cache * old)305 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
306 {
307 	struct jffs2_inode_cache **prev;
308 	D2(printk(KERN_DEBUG "jffs2_del_ino_cache: Del %p (ino #%u)\n", old, old->ino));
309 	spin_lock(&c->inocache_lock);
310 
311 	prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
312 
313 	while ((*prev) && (*prev)->ino < old->ino) {
314 		prev = &(*prev)->next;
315 	}
316 	if ((*prev) == old) {
317 		*prev = old->next;
318 	}
319 	spin_unlock(&c->inocache_lock);
320 }
321 
jffs2_free_ino_caches(struct jffs2_sb_info * c)322 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
323 {
324 	int i;
325 	struct jffs2_inode_cache *this, *next;
326 
327 	for (i=0; i<INOCACHE_HASHSIZE; i++) {
328 		this = c->inocache_list[i];
329 		while (this) {
330 			next = this->next;
331 			D2(printk(KERN_DEBUG "jffs2_free_ino_caches: Freeing ino #%u at %p\n", this->ino, this));
332 			jffs2_free_inode_cache(this);
333 			this = next;
334 		}
335 		c->inocache_list[i] = NULL;
336 	}
337 }
338 
jffs2_free_raw_node_refs(struct jffs2_sb_info * c)339 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
340 {
341 	int i;
342 	struct jffs2_raw_node_ref *this, *next;
343 
344 	for (i=0; i<c->nr_blocks; i++) {
345 		this = c->blocks[i].first_node;
346 		while(this) {
347 			next = this->next_phys;
348 			jffs2_free_raw_node_ref(this);
349 			this = next;
350 		}
351 		c->blocks[i].first_node = c->blocks[i].last_node = NULL;
352 	}
353 }
354 
355