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