2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001, 2002 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
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/
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.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
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.
34 * $Id: nodelist.c,v 1.30.2.6 2003/02/24 21:49:33 dwmw2 Exp $
38 #include <linux/kernel.h>
39 #include <linux/jffs2.h>
41 #include <linux/mtd/mtd.h>
44 void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
46 struct jffs2_full_dirent **prev = list;
47 D1(printk(KERN_DEBUG "jffs2_add_fd_to_list( %p, %p (->%p))\n", new, list, *list));
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);
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);
66 prev = &((*prev)->next);
73 printk(KERN_DEBUG "Dirent \"%s\" (hash 0x%08x, ino #%u\n", (*list)->name, (*list)->nhash, (*list)->ino);
74 list = &(*list)->next;
78 /* Put a new tmp_dnode_info into the list, keeping the list in
79 order of increasing version
81 void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list)
83 struct jffs2_tmp_dnode_info **prev = list;
85 while ((*prev) && (*prev)->version < tn->version) {
86 prev = &((*prev)->next);
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 */
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,
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;
104 union jffs2_node_union node;
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);
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));
121 err = c->mtd->read(c->mtd, (ref->flash_offset & ~3), min(ref->totlen, sizeof(node)), &retlen, (void *)&node);
123 printk(KERN_WARNING "error %d reading node at 0x%08x in get_inode_nodes()\n", err, (ref->flash_offset) & ~3);
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");
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");
143 if (node.d.version > *highest_version)
144 *highest_version = node.d.version;
145 if (ref->flash_offset & 1) {
149 fd = jffs2_alloc_full_dirent(node.d.nsize+1);
154 memset(fd,0,sizeof(struct jffs2_full_dirent) + node.d.nsize+1);
156 fd->version = node.d.version;
157 fd->ino = node.d.ino;
158 fd->type = node.d.type;
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;
166 /* memcpy as much of the name as possible from the raw
167 dirent we've already read from the flash
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))));
172 /* Do we need to copy any more of the name directly
175 if (node.d.nsize + sizeof(struct jffs2_raw_dirent) > retlen) {
176 int already = retlen - sizeof(struct jffs2_raw_dirent);
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)
184 printk(KERN_WARNING "Read remainder of name in jffs2_get_inode_nodes(): error %d\n", err);
185 jffs2_free_full_dirent(fd);
189 fd->nhash = full_name_hash(fd->name, node.d.nsize);
191 /* Wheee. We now have a complete jffs2_full_dirent structure, with
192 the name in it and everything. Link it into the list
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);
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");
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));
209 if (ref->flash_offset & 1) {
210 D1(printk(KERN_DEBUG "obsoleted\n"));
214 tn = jffs2_alloc_tmp_dnode_info();
216 D1(printk(KERN_DEBUG "alloc tn failed\n"));
221 tn->fn = jffs2_alloc_full_dnode();
223 D1(printk(KERN_DEBUG "alloc fn failed\n"));
225 jffs2_free_tmp_dnode_info(tn);
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;
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);
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);
246 case JFFS2_FEATURE_ROCOMPAT:
247 printk(KERN_NOTICE "Unknown ROCOMPAT nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
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);
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);
264 jffs2_free_tmp_dnode_info_list(ret_tn);
265 jffs2_free_full_dirent_list(ret_fd);
269 struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, uint32_t ino)
271 struct jffs2_inode_cache *ret;
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) {
280 spin_unlock(&c->inocache_lock);
282 if (ret && ret->ino != ino)
285 D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
289 void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
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);
295 prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
297 while ((*prev) && (*prev)->ino < new->ino) {
298 prev = &(*prev)->next;
302 spin_unlock(&c->inocache_lock);
305 void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
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);
311 prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
313 while ((*prev) && (*prev)->ino < old->ino) {
314 prev = &(*prev)->next;
316 if ((*prev) == old) {
319 spin_unlock(&c->inocache_lock);
322 void jffs2_free_ino_caches(struct jffs2_sb_info *c)
325 struct jffs2_inode_cache *this, *next;
327 for (i=0; i<INOCACHE_HASHSIZE; i++) {
328 this = c->inocache_list[i];
331 D2(printk(KERN_DEBUG "jffs2_free_ino_caches: Freeing ino #%u at %p\n", this->ino, this));
332 jffs2_free_inode_cache(this);
335 c->inocache_list[i] = NULL;
339 void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
342 struct jffs2_raw_node_ref *this, *next;
344 for (i=0; i<c->nr_blocks; i++) {
345 this = c->blocks[i].first_node;
347 next = this->next_phys;
348 jffs2_free_raw_node_ref(this);
351 c->blocks[i].first_node = c->blocks[i].last_node = NULL;