OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [fs/] [jffs2/] [v2_0/] [src/] [nodelist.c] - Blame information for rev 565

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
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
 * For licensing information, see the file 'LICENCE' in this directory.
9
 *
10
 * $Id: nodelist.c,v 1.1.1.1 2004-02-14 13:29:19 phoenix Exp $
11
 *
12
 */
13
 
14
#include <linux/kernel.h>
15
#include <linux/sched.h>
16
#include <linux/fs.h>
17
#include <linux/mtd/mtd.h>
18
#include <linux/rbtree.h>
19
#include <linux/crc32.h>
20
#include <linux/slab.h>
21
#include <linux/pagemap.h>
22
#include "nodelist.h"
23
 
24
void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
25
{
26
        struct jffs2_full_dirent **prev = list;
27
        D1(printk(KERN_DEBUG "jffs2_add_fd_to_list( %p, %p (->%p))\n", new, list, *list));
28
 
29
        while ((*prev) && (*prev)->nhash <= new->nhash) {
30
                if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
31
                        /* Duplicate. Free one */
32
                        if (new->version < (*prev)->version) {
33
                                D1(printk(KERN_DEBUG "Eep! Marking new dirent node obsolete\n"));
34
                                D1(printk(KERN_DEBUG "New dirent is \"%s\"->ino #%u. Old is \"%s\"->ino #%u\n", new->name, new->ino, (*prev)->name, (*prev)->ino));
35
                                jffs2_mark_node_obsolete(c, new->raw);
36
                                jffs2_free_full_dirent(new);
37
                        } else {
38
                                D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) obsolete\n", (*prev)->ino));
39
                                new->next = (*prev)->next;
40
                                jffs2_mark_node_obsolete(c, ((*prev)->raw));
41
                                jffs2_free_full_dirent(*prev);
42
                                *prev = new;
43
                        }
44
                        goto out;
45
                }
46
                prev = &((*prev)->next);
47
        }
48
        new->next = *prev;
49
        *prev = new;
50
 
51
 out:
52
        D2(while(*list) {
53
                printk(KERN_DEBUG "Dirent \"%s\" (hash 0x%08x, ino #%u\n", (*list)->name, (*list)->nhash, (*list)->ino);
54
                list = &(*list)->next;
55
        });
56
}
57
 
58
/* Put a new tmp_dnode_info into the list, keeping the list in
59
   order of increasing version
60
*/
61
void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list)
62
{
63
        struct jffs2_tmp_dnode_info **prev = list;
64
 
65
        while ((*prev) && (*prev)->version < tn->version) {
66
                prev = &((*prev)->next);
67
        }
68
        tn->next = (*prev);
69
        *prev = tn;
70
}
71
 
72
static void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn)
73
{
74
        struct jffs2_tmp_dnode_info *next;
75
 
76
        while (tn) {
77
                next = tn;
78
                tn = tn->next;
79
                jffs2_free_full_dnode(next->fn);
80
                jffs2_free_tmp_dnode_info(next);
81
        }
82
}
83
 
84
static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
85
{
86
        struct jffs2_full_dirent *next;
87
 
88
        while (fd) {
89
                next = fd->next;
90
                jffs2_free_full_dirent(fd);
91
                fd = next;
92
        }
93
}
94
 
95
 
96
/* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
97
   with this ino, returning the former in order of version */
98
 
99
int jffs2_get_inode_nodes(struct jffs2_sb_info *c, ino_t ino, struct jffs2_inode_info *f,
100
                          struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
101
                          uint32_t *highest_version, uint32_t *latest_mctime,
102
                          uint32_t *mctime_ver)
103
{
104
        struct jffs2_raw_node_ref *ref = f->inocache->nodes;
105
        struct jffs2_tmp_dnode_info *tn, *ret_tn = NULL;
106
        struct jffs2_full_dirent *fd, *ret_fd = NULL;
107
 
108
        union jffs2_node_union node;
109
        size_t retlen;
110
        int err;
111
 
112
        *mctime_ver = 0;
113
 
114
        D1(printk(KERN_DEBUG "jffs2_get_inode_nodes(): ino #%lu\n", ino));
115
        if (!f->inocache->nodes) {
116
                printk(KERN_WARNING "Eep. no nodes for ino #%lu\n", (unsigned long)ino);
117
        }
118
 
119
        spin_lock(&c->erase_completion_lock);
120
 
121
        for (ref = f->inocache->nodes; ref && ref->next_in_ino; ref = ref->next_in_ino) {
122
                /* Work out whether it's a data node or a dirent node */
123
                if (ref_obsolete(ref)) {
124
                        /* FIXME: On NAND flash we may need to read these */
125
                        D1(printk(KERN_DEBUG "node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref)));
126
                        continue;
127
                }
128
                /* We can hold a pointer to a non-obsolete node without the spinlock,
129
                   but _obsolete_ nodes may disappear at any time, if the block
130
                   they're in gets erased */
131
                spin_unlock(&c->erase_completion_lock);
132
 
133
                cond_resched();
134
 
135
                /* FIXME: point() */
136
                err = jffs2_flash_read(c, (ref_offset(ref)), min_t(uint32_t, ref->totlen, sizeof(node)), &retlen, (void *)&node);
137
                if (err) {
138
                        printk(KERN_WARNING "error %d reading node at 0x%08x in get_inode_nodes()\n", err, ref_offset(ref));
139
                        goto free_out;
140
                }
141
 
142
 
143
                        /* Check we've managed to read at least the common node header */
144
                if (retlen < min_t(uint32_t, ref->totlen, sizeof(node.u))) {
145
                        printk(KERN_WARNING "short read in get_inode_nodes()\n");
146
                        err = -EIO;
147
                        goto free_out;
148
                }
149
 
150
                switch (je16_to_cpu(node.u.nodetype)) {
151
                case JFFS2_NODETYPE_DIRENT:
152
                        D1(printk(KERN_DEBUG "Node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref)));
153
                        if (ref_flags(ref) == REF_UNCHECKED) {
154
                                printk(KERN_WARNING "BUG: Dirent node at 0x%08x never got checked? How?\n", ref_offset(ref));
155
                                BUG();
156
                        }
157
                        if (retlen < sizeof(node.d)) {
158
                                printk(KERN_WARNING "short read in get_inode_nodes()\n");
159
                                err = -EIO;
160
                                goto free_out;
161
                        }
162
                        if (je32_to_cpu(node.d.version) > *highest_version)
163
                                *highest_version = je32_to_cpu(node.d.version);
164
                        if (ref_obsolete(ref)) {
165
                                /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
166
                                printk(KERN_ERR "Dirent node at 0x%08x became obsolete while we weren't looking\n",
167
                                       ref_offset(ref));
168
                                BUG();
169
                        }
170
                        fd = jffs2_alloc_full_dirent(node.d.nsize+1);
171
                        if (!fd) {
172
                                err = -ENOMEM;
173
                                goto free_out;
174
                        }
175
                        memset(fd,0,sizeof(struct jffs2_full_dirent) + node.d.nsize+1);
176
                        fd->raw = ref;
177
                        fd->version = je32_to_cpu(node.d.version);
178
                        fd->ino = je32_to_cpu(node.d.ino);
179
                        fd->type = node.d.type;
180
 
181
                        /* Pick out the mctime of the latest dirent */
182
                        if(fd->version > *mctime_ver) {
183
                                *mctime_ver = fd->version;
184
                                *latest_mctime = je32_to_cpu(node.d.mctime);
185
                        }
186
 
187
                        /* memcpy as much of the name as possible from the raw
188
                           dirent we've already read from the flash
189
                        */
190
                        if (retlen > sizeof(struct jffs2_raw_dirent))
191
                                memcpy(&fd->name[0], &node.d.name[0], min_t(uint32_t, node.d.nsize, (retlen-sizeof(struct jffs2_raw_dirent))));
192
 
193
                        /* Do we need to copy any more of the name directly
194
                           from the flash?
195
                        */
196
                        if (node.d.nsize + sizeof(struct jffs2_raw_dirent) > retlen) {
197
                                /* FIXME: point() */
198
                                int already = retlen - sizeof(struct jffs2_raw_dirent);
199
 
200
                                err = jffs2_flash_read(c, (ref_offset(ref)) + retlen,
201
                                                   node.d.nsize - already, &retlen, &fd->name[already]);
202
                                if (!err && retlen != node.d.nsize - already)
203
                                        err = -EIO;
204
 
205
                                if (err) {
206
                                        printk(KERN_WARNING "Read remainder of name in jffs2_get_inode_nodes(): error %d\n", err);
207
                                        jffs2_free_full_dirent(fd);
208
                                        goto free_out;
209
                                }
210
                        }
211
                        fd->nhash = full_name_hash(fd->name, node.d.nsize);
212
                        fd->next = NULL;
213
                                /* Wheee. We now have a complete jffs2_full_dirent structure, with
214
                                   the name in it and everything. Link it into the list
215
                                */
216
                        D1(printk(KERN_DEBUG "Adding fd \"%s\", ino #%u\n", fd->name, fd->ino));
217
                        jffs2_add_fd_to_list(c, fd, &ret_fd);
218
                        break;
219
 
220
                case JFFS2_NODETYPE_INODE:
221
                        D1(printk(KERN_DEBUG "Node at %08x (%d) is a data node\n", ref_offset(ref), ref_flags(ref)));
222
                        if (retlen < sizeof(node.i)) {
223
                                printk(KERN_WARNING "read too short for dnode\n");
224
                                err = -EIO;
225
                                goto free_out;
226
                        }
227
                        if (je32_to_cpu(node.i.version) > *highest_version)
228
                                *highest_version = je32_to_cpu(node.i.version);
229
                        D1(printk(KERN_DEBUG "version %d, highest_version now %d\n", je32_to_cpu(node.i.version), *highest_version));
230
 
231
                        if (ref_obsolete(ref)) {
232
                                /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
233
                                printk(KERN_ERR "Inode node at 0x%08x became obsolete while we weren't looking\n",
234
                                       ref_offset(ref));
235
                                BUG();
236
                        }
237
 
238
                        /* If we've never checked the CRCs on this node, check them now. */
239
                        if (ref_flags(ref) == REF_UNCHECKED) {
240
                                uint32_t crc;
241
                                struct jffs2_eraseblock *jeb;
242
 
243
                                crc = crc32(0, &node, sizeof(node.i)-8);
244
                                if (crc != je32_to_cpu(node.i.node_crc)) {
245
                                        printk(KERN_NOTICE "jffs2_get_inode_nodes(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
246
                                               ref_offset(ref), je32_to_cpu(node.i.node_crc), crc);
247
                                        jffs2_mark_node_obsolete(c, ref);
248
                                        spin_lock(&c->erase_completion_lock);
249
                                        continue;
250
                                }
251
 
252
                                if (node.i.compr != JFFS2_COMPR_ZERO && je32_to_cpu(node.i.csize)) {
253
                                        unsigned char *buf=NULL;
254
                                        uint32_t pointed = 0;
255
#ifndef __ECOS
256
                                        if (c->mtd->point) {
257
                                                err = c->mtd->point (c->mtd, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize),
258
                                                                     &retlen, &buf);
259
                                                if (!err && retlen < je32_to_cpu(node.i.csize)) {
260
                                                        D1(printk(KERN_DEBUG "MTD point returned len too short: 0x%zx\n", retlen));
261
                                                        c->mtd->unpoint(c->mtd, buf, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize));
262
                                                } else if (err){
263
                                                        D1(printk(KERN_DEBUG "MTD point failed %d\n", err));
264
                                                } else
265
                                                        pointed = 1; /* succefully pointed to device */
266
                                        }
267
#endif                                  
268
                                        if(!pointed){
269
                                                buf = kmalloc(je32_to_cpu(node.i.csize), GFP_KERNEL);
270
                                                if (!buf)
271
                                                        return -ENOMEM;
272
 
273
                                                err = jffs2_flash_read(c, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize),
274
                                                                       &retlen, buf);
275
                                                if (!err && retlen != je32_to_cpu(node.i.csize))
276
                                                        err = -EIO;
277
                                                if (err) {
278
                                                        kfree(buf);
279
                                                        return err;
280
                                                }
281
                                        }
282
                                        crc = crc32(0, buf, je32_to_cpu(node.i.csize));
283
                                        if(!pointed)
284
                                                kfree(buf);
285
#ifndef __ECOS
286
                                        else
287
                                                c->mtd->unpoint(c->mtd, buf, ref_offset(ref) + sizeof(node.i), je32_to_cpu(node.i.csize));
288
#endif
289
 
290
                                        if (crc != je32_to_cpu(node.i.data_crc)) {
291
                                                printk(KERN_NOTICE "jffs2_get_inode_nodes(): Data CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
292
                                                       ref_offset(ref), je32_to_cpu(node.i.data_crc), crc);
293
                                                jffs2_mark_node_obsolete(c, ref);
294
                                                spin_lock(&c->erase_completion_lock);
295
                                                continue;
296
                                        }
297
 
298
                                }
299
 
300
                                /* Mark the node as having been checked and fix the accounting accordingly */
301
                                spin_lock(&c->erase_completion_lock);
302
                                jeb = &c->blocks[ref->flash_offset / c->sector_size];
303
                                jeb->used_size += ref->totlen;
304
                                jeb->unchecked_size -= ref->totlen;
305
                                c->used_size += ref->totlen;
306
                                c->unchecked_size -= ref->totlen;
307
 
308
                                /* If node covers at least a whole page, or if it starts at the
309
                                   beginning of a page and runs to the end of the file, or if
310
                                   it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
311
 
312
                                   If it's actually overlapped, it'll get made NORMAL (or OBSOLETE)
313
                                   when the overlapping node(s) get added to the tree anyway.
314
                                */
315
                                if ((je32_to_cpu(node.i.dsize) >= PAGE_CACHE_SIZE) ||
316
                                    ( ((je32_to_cpu(node.i.offset)&(PAGE_CACHE_SIZE-1))==0) &&
317
                                      (je32_to_cpu(node.i.dsize)+je32_to_cpu(node.i.offset) ==  je32_to_cpu(node.i.isize)))) {
318
                                        D1(printk(KERN_DEBUG "Marking node at 0x%08x REF_PRISTINE\n", ref_offset(ref)));
319
                                        ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
320
                                } else {
321
                                        D1(printk(KERN_DEBUG "Marking node at 0x%08x REF_NORMAL\n", ref_offset(ref)));
322
                                        ref->flash_offset = ref_offset(ref) | REF_NORMAL;
323
                                }
324
                                spin_unlock(&c->erase_completion_lock);
325
                        }
326
 
327
                        tn = jffs2_alloc_tmp_dnode_info();
328
                        if (!tn) {
329
                                D1(printk(KERN_DEBUG "alloc tn failed\n"));
330
                                err = -ENOMEM;
331
                                goto free_out;
332
                        }
333
 
334
                        tn->fn = jffs2_alloc_full_dnode();
335
                        if (!tn->fn) {
336
                                D1(printk(KERN_DEBUG "alloc fn failed\n"));
337
                                err = -ENOMEM;
338
                                jffs2_free_tmp_dnode_info(tn);
339
                                goto free_out;
340
                        }
341
                        tn->version = je32_to_cpu(node.i.version);
342
                        tn->fn->ofs = je32_to_cpu(node.i.offset);
343
                        /* There was a bug where we wrote hole nodes out with
344
                           csize/dsize swapped. Deal with it */
345
                        if (node.i.compr == JFFS2_COMPR_ZERO && !je32_to_cpu(node.i.dsize) && je32_to_cpu(node.i.csize))
346
                                tn->fn->size = je32_to_cpu(node.i.csize);
347
                        else // normal case...
348
                                tn->fn->size = je32_to_cpu(node.i.dsize);
349
                        tn->fn->raw = ref;
350
                        D1(printk(KERN_DEBUG "dnode @%08x: ver %u, offset %04x, dsize %04x\n",
351
                                  ref_offset(ref), je32_to_cpu(node.i.version),
352
                                  je32_to_cpu(node.i.offset), je32_to_cpu(node.i.dsize)));
353
                        jffs2_add_tn_to_list(tn, &ret_tn);
354
                        break;
355
 
356
                default:
357
                        if (ref_flags(ref) == REF_UNCHECKED) {
358
                                struct jffs2_eraseblock *jeb;
359
 
360
                                printk(KERN_ERR "Eep. Unknown node type %04x at %08x was marked REF_UNCHECKED\n",
361
                                       je16_to_cpu(node.u.nodetype), ref_offset(ref));
362
 
363
                                /* Mark the node as having been checked and fix the accounting accordingly */
364
                                spin_lock(&c->erase_completion_lock);
365
                                jeb = &c->blocks[ref->flash_offset / c->sector_size];
366
                                jeb->used_size += ref->totlen;
367
                                jeb->unchecked_size -= ref->totlen;
368
                                c->used_size += ref->totlen;
369
                                c->unchecked_size -= ref->totlen;
370
 
371
                                mark_ref_normal(ref);
372
                                spin_unlock(&c->erase_completion_lock);
373
                        }
374
                        node.u.nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(node.u.nodetype));
375
                        if (crc32(0, &node, sizeof(struct jffs2_unknown_node)-4) != je32_to_cpu(node.u.hdr_crc)) {
376
                                /* Hmmm. This should have been caught at scan time. */
377
                                printk(KERN_ERR "Node header CRC failed at %08x. But it must have been OK earlier.\n",
378
                                       ref_offset(ref));
379
                                printk(KERN_ERR "Node was: { %04x, %04x, %08x, %08x }\n",
380
                                       je16_to_cpu(node.u.magic), je16_to_cpu(node.u.nodetype), je32_to_cpu(node.u.totlen),
381
                                       je32_to_cpu(node.u.hdr_crc));
382
                                jffs2_mark_node_obsolete(c, ref);
383
                        } else switch(je16_to_cpu(node.u.nodetype) & JFFS2_COMPAT_MASK) {
384
                        case JFFS2_FEATURE_INCOMPAT:
385
                                printk(KERN_NOTICE "Unknown INCOMPAT nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
386
                                /* EEP */
387
                                BUG();
388
                                break;
389
                        case JFFS2_FEATURE_ROCOMPAT:
390
                                printk(KERN_NOTICE "Unknown ROCOMPAT nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
391
                                if (!(c->flags & JFFS2_SB_FLAG_RO))
392
                                        BUG();
393
                                break;
394
                        case JFFS2_FEATURE_RWCOMPAT_COPY:
395
                                printk(KERN_NOTICE "Unknown RWCOMPAT_COPY nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
396
                                break;
397
                        case JFFS2_FEATURE_RWCOMPAT_DELETE:
398
                                printk(KERN_NOTICE "Unknown RWCOMPAT_DELETE nodetype %04X at %08x\n", je16_to_cpu(node.u.nodetype), ref_offset(ref));
399
                                jffs2_mark_node_obsolete(c, ref);
400
                                break;
401
                        }
402
 
403
                }
404
                spin_lock(&c->erase_completion_lock);
405
 
406
        }
407
        spin_unlock(&c->erase_completion_lock);
408
        *tnp = ret_tn;
409
        *fdp = ret_fd;
410
 
411
        return 0;
412
 
413
 free_out:
414
        jffs2_free_tmp_dnode_info_list(ret_tn);
415
        jffs2_free_full_dirent_list(ret_fd);
416
        return err;
417
}
418
 
419
void jffs2_set_inocache_state(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, int state)
420
{
421
        spin_lock(&c->inocache_lock);
422
        ic->state = state;
423
        wake_up(&c->inocache_wq);
424
        spin_unlock(&c->inocache_lock);
425
}
426
 
427
/* During mount, this needs no locking. During normal operation, its
428
   callers want to do other stuff while still holding the inocache_lock.
429
   Rather than introducing special case get_ino_cache functions or
430
   callbacks, we just let the caller do the locking itself. */
431
 
432
struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, int ino)
433
{
434
        struct jffs2_inode_cache *ret;
435
 
436
        D2(printk(KERN_DEBUG "jffs2_get_ino_cache(): ino %u\n", ino));
437
 
438
        ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
439
        while (ret && ret->ino < ino) {
440
                ret = ret->next;
441
        }
442
 
443
        if (ret && ret->ino != ino)
444
                ret = NULL;
445
 
446
        D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
447
        return ret;
448
}
449
 
450
void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
451
{
452
        struct jffs2_inode_cache **prev;
453
        D2(printk(KERN_DEBUG "jffs2_add_ino_cache: Add %p (ino #%u)\n", new, new->ino));
454
        spin_lock(&c->inocache_lock);
455
 
456
        prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
457
 
458
        while ((*prev) && (*prev)->ino < new->ino) {
459
                prev = &(*prev)->next;
460
        }
461
        new->next = *prev;
462
        *prev = new;
463
 
464
        spin_unlock(&c->inocache_lock);
465
}
466
 
467
void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
468
{
469
        struct jffs2_inode_cache **prev;
470
        D2(printk(KERN_DEBUG "jffs2_del_ino_cache: Del %p (ino #%u)\n", old, old->ino));
471
        spin_lock(&c->inocache_lock);
472
 
473
        prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
474
 
475
        while ((*prev) && (*prev)->ino < old->ino) {
476
                prev = &(*prev)->next;
477
        }
478
        if ((*prev) == old) {
479
                *prev = old->next;
480
        }
481
 
482
        spin_unlock(&c->inocache_lock);
483
}
484
 
485
void jffs2_free_ino_caches(struct jffs2_sb_info *c)
486
{
487
        int i;
488
        struct jffs2_inode_cache *this, *next;
489
 
490
        for (i=0; i<INOCACHE_HASHSIZE; i++) {
491
                this = c->inocache_list[i];
492
                while (this) {
493
                        next = this->next;
494
                        D2(printk(KERN_DEBUG "jffs2_free_ino_caches: Freeing ino #%u at %p\n", this->ino, this));
495
                        jffs2_free_inode_cache(this);
496
                        this = next;
497
                }
498
                c->inocache_list[i] = NULL;
499
        }
500
}
501
 
502
void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
503
{
504
        int i;
505
        struct jffs2_raw_node_ref *this, *next;
506
 
507
        for (i=0; i<c->nr_blocks; i++) {
508
                this = c->blocks[i].first_node;
509
                while(this) {
510
                        next = this->next_phys;
511
                        jffs2_free_raw_node_ref(this);
512
                        this = next;
513
                }
514
                c->blocks[i].first_node = c->blocks[i].last_node = NULL;
515
        }
516
}
517
 
518
struct jffs2_node_frag *jffs2_lookup_node_frag(struct rb_root *fragtree, uint32_t offset)
519
{
520
        /* The common case in lookup is that there will be a node
521
           which precisely matches. So we go looking for that first */
522
        struct rb_node *next;
523
        struct jffs2_node_frag *prev = NULL;
524
        struct jffs2_node_frag *frag = NULL;
525
 
526
        D2(printk(KERN_DEBUG "jffs2_lookup_node_frag(%p, %d)\n", fragtree, offset));
527
 
528
        next = fragtree->rb_node;
529
 
530
        while(next) {
531
                frag = rb_entry(next, struct jffs2_node_frag, rb);
532
 
533
                D2(printk(KERN_DEBUG "Considering frag %d-%d (%p). left %p, right %p\n",
534
                          frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right));
535
                if (frag->ofs + frag->size <= offset) {
536
                        D2(printk(KERN_DEBUG "Going right from frag %d-%d, before the region we care about\n",
537
                                  frag->ofs, frag->ofs+frag->size));
538
                        /* Remember the closest smaller match on the way down */
539
                        if (!prev || frag->ofs > prev->ofs)
540
                                prev = frag;
541
                        next = frag->rb.rb_right;
542
                } else if (frag->ofs > offset) {
543
                        D2(printk(KERN_DEBUG "Going left from frag %d-%d, after the region we care about\n",
544
                                  frag->ofs, frag->ofs+frag->size));
545
                        next = frag->rb.rb_left;
546
                } else {
547
                        D2(printk(KERN_DEBUG "Returning frag %d,%d, matched\n",
548
                                  frag->ofs, frag->ofs+frag->size));
549
                        return frag;
550
                }
551
        }
552
 
553
        /* Exact match not found. Go back up looking at each parent,
554
           and return the closest smaller one */
555
 
556
        if (prev)
557
                D2(printk(KERN_DEBUG "No match. Returning frag %d,%d, closest previous\n",
558
                          prev->ofs, prev->ofs+prev->size));
559
        else
560
                D2(printk(KERN_DEBUG "Returning NULL, empty fragtree\n"));
561
 
562
        return prev;
563
}
564
 
565
/* Pass 'c' argument to indicate that nodes should be marked obsolete as
566
   they're killed. */
567
void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
568
{
569
        struct jffs2_node_frag *frag;
570
        struct jffs2_node_frag *parent;
571
 
572
        if (!root->rb_node)
573
                return;
574
 
575
        frag = (rb_entry(root->rb_node, struct jffs2_node_frag, rb));
576
 
577
        while(frag) {
578
                if (frag->rb.rb_left) {
579
                        D2(printk(KERN_DEBUG "Going left from frag (%p) %d-%d\n",
580
                                  frag, frag->ofs, frag->ofs+frag->size));
581
                        frag = frag_left(frag);
582
                        continue;
583
                }
584
                if (frag->rb.rb_right) {
585
                        D2(printk(KERN_DEBUG "Going right from frag (%p) %d-%d\n",
586
                                  frag, frag->ofs, frag->ofs+frag->size));
587
                        frag = frag_right(frag);
588
                        continue;
589
                }
590
 
591
                D2(printk(KERN_DEBUG "jffs2_kill_fragtree: frag at 0x%x-0x%x: node %p, frags %d--\n",
592
                          frag->ofs, frag->ofs+frag->size, frag->node,
593
                          frag->node?frag->node->frags:0));
594
 
595
                if (frag->node && !(--frag->node->frags)) {
596
                        /* Not a hole, and it's the final remaining frag
597
                           of this node. Free the node */
598
                        if (c)
599
                                jffs2_mark_node_obsolete(c, frag->node->raw);
600
 
601
                        jffs2_free_full_dnode(frag->node);
602
                }
603
                parent = frag_parent(frag);
604
                if (parent) {
605
                        if (frag_left(parent) == frag)
606
                                parent->rb.rb_left = NULL;
607
                        else
608
                                parent->rb.rb_right = NULL;
609
                }
610
 
611
                jffs2_free_node_frag(frag);
612
                frag = parent;
613
        }
614
}
615
 
616
void jffs2_fragtree_insert(struct jffs2_node_frag *newfrag, struct jffs2_node_frag *base)
617
{
618
        struct rb_node *parent = &base->rb;
619
        struct rb_node **link = &parent;
620
 
621
        D2(printk(KERN_DEBUG "jffs2_fragtree_insert(%p; %d-%d, %p)\n", newfrag,
622
                  newfrag->ofs, newfrag->ofs+newfrag->size, base));
623
 
624
        while (*link) {
625
                parent = *link;
626
                base = rb_entry(parent, struct jffs2_node_frag, rb);
627
 
628
                D2(printk(KERN_DEBUG "fragtree_insert considering frag at 0x%x\n", base->ofs));
629
                if (newfrag->ofs > base->ofs)
630
                        link = &base->rb.rb_right;
631
                else if (newfrag->ofs < base->ofs)
632
                        link = &base->rb.rb_left;
633
                else {
634
                        printk(KERN_CRIT "Duplicate frag at %08x (%p,%p)\n", newfrag->ofs, newfrag, base);
635
                        BUG();
636
                }
637
        }
638
 
639
        rb_link_node(&newfrag->rb, &base->rb, link);
640
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.