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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [jffs/] [intrep.c] - Blame information for rev 1777

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

Line No. Rev Author Line
1 1628 jcastillo
/*
2
 * JFFS -- Journalling Flash File System, Linux implementation.
3
 *
4
 * Copyright (C) 1999, 2000  Finn Hakansson, Axis Communications, Inc.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * $Id: intrep.c,v 1.1 2005-12-20 10:26:13 jcastillo Exp $
12
 *
13
 */
14
 
15
/* This file contains the code for the internal structure of the
16
   Journalling Flash File System, JFFS.  */
17
 
18
/*
19
 * Todo list:
20
 *
21
 * memcpy_to_flash()- and memcpy_from_flash()-functions.
22
 *
23
 * Implementation of hard links.
24
 *
25
 * Organize the source code in a better way. Against the VFS we could
26
 * have jffs_ext.c, and against the block device jffs_int.c.
27
 * A better file-internal organization too.
28
 *
29
 * A better checksum algorithm.
30
 *
31
 * Consider endianness stuff. ntohl() etc.
32
 *
33
 * Check all comments beginning with XXX.
34
 *
35
 * Are we handling the atime, mtime, ctime members of the inode right?
36
 *
37
 * Remove some duplicated code. Take a look at jffs_write_node() and
38
 * jffs_rewrite_data() for instance.
39
 *
40
 * Implement more meaning of the nlink member in various data structures.
41
 * nlink could be used in conjunction with hard links for instance.
42
 *
43
 * Fix the rename stuff. (I.e. if we have two files `a' and `b' and we
44
 * do a `mv b a'.) Half of this is already implemented.
45
 *
46
 */
47
 
48
#include <linux/module.h>
49
#include <linux/types.h>
50
#include <linux/malloc.h>
51
#include <linux/jffs.h>
52
#include <linux/fs.h>
53
#include <linux/stat.h>
54
#include <linux/pagemap.h>
55
#include <linux/locks.h>
56
#include <asm/byteorder.h>
57
 
58
#include "intrep.h"
59
#include "jffs_fm.h"
60
 
61
#if defined(CONFIG_JFFS_FS_VERBOSE) && CONFIG_JFFS_FS_VERBOSE
62
#define D(x) x
63
#else
64
#define D(x)
65
#endif
66
#define D1(x)
67
#define D2(x)
68
#define D3(x)
69
#define ASSERT(x) x
70
 
71
#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
72
long no_jffs_file = 0;
73
long no_jffs_node = 0;
74
long no_jffs_control = 0;
75
long no_jffs_raw_inode = 0;
76
long no_jffs_node_ref = 0;
77
long no_jffs_fm = 0;
78
long no_jffs_fmcontrol = 0;
79
long no_hash = 0;
80
long no_name = 0;
81
#endif
82
 
83
static int jffs_scan_flash(struct jffs_control *c);
84
static int jffs_update_file(struct jffs_file *f, struct jffs_node *node);
85
 
86
#if 0
87
#define _U      01
88
#define _L      02
89
#define _N      04
90
#define _S      010
91
#define _P      020
92
#define _C      040
93
#define _X      0100
94
#define _B      0200
95
 
96
const unsigned char jffs_ctype_[1 + 256] = {
97
        0,
98
        _C,     _C,     _C,     _C,     _C,     _C,     _C,     _C,
99
        _C,     _C|_S,  _C|_S,  _C|_S,  _C|_S,  _C|_S,  _C,     _C,
100
        _C,     _C,     _C,     _C,     _C,     _C,     _C,     _C,
101
        _C,     _C,     _C,     _C,     _C,     _C,     _C,     _C,
102
        _S|_B,  _P,     _P,     _P,     _P,     _P,     _P,     _P,
103
        _P,     _P,     _P,     _P,     _P,     _P,     _P,     _P,
104
        _N,     _N,     _N,     _N,     _N,     _N,     _N,     _N,
105
        _N,     _N,     _P,     _P,     _P,     _P,     _P,     _P,
106
        _P,     _U|_X,  _U|_X,  _U|_X,  _U|_X,  _U|_X,  _U|_X,  _U,
107
        _U,     _U,     _U,     _U,     _U,     _U,     _U,     _U,
108
        _U,     _U,     _U,     _U,     _U,     _U,     _U,     _U,
109
        _U,     _U,     _U,     _P,     _P,     _P,     _P,     _P,
110
        _P,     _L|_X,  _L|_X,  _L|_X,  _L|_X,  _L|_X,  _L|_X,  _L,
111
        _L,     _L,     _L,     _L,     _L,     _L,     _L,     _L,
112
        _L,     _L,     _L,     _L,     _L,     _L,     _L,     _L,
113
        _L,     _L,     _L,     _P,     _P,     _P,     _P,     _C
114
};
115
 
116
#define jffs_isalpha(c)      ((jffs_ctype_+1)[c]&(_U|_L))
117
#define jffs_isupper(c)      ((jffs_ctype_+1)[c]&_U)
118
#define jffs_islower(c)      ((jffs_ctype_+1)[c]&_L)
119
#define jffs_isdigit(c)      ((jffs_ctype_+1)[c]&_N)
120
#define jffs_isxdigit(c)     ((jffs_ctype_+1)[c]&(_X|_N))
121
#define jffs_isspace(c)      ((jffs_ctype_+1)[c]&_S)
122
#define jffs_ispunct(c)      ((jffs_ctype_+1)[c]&_P)
123
#define jffs_isalnum(c)      ((jffs_ctype_+1)[c]&(_U|_L|_N))
124
#define jffs_isprint(c)      ((jffs_ctype_+1)[c]&(_P|_U|_L|_N|_B))
125
#define jffs_isgraph(c)      ((jffs_ctype_+1)[c]&(_P|_U|_L|_N))
126
#define jffs_iscntrl(c)      ((jffs_ctype_+1)[c]&_C)
127
 
128
void
129
jffs_hexdump(const unsigned char* ptr, int size)
130
{
131
        char line[16];
132
        int j = 0;
133
 
134
        while (size > 0) {
135
                int i;
136
 
137
                printk("%p:", ptr);
138
                for (j = 0; j < 16; j++) {
139
                        line[j] = *ptr++;
140
                }
141
                for (i = 0; i < j; i++) {
142
                        if (!(i & 1)) {
143
                                printk(" %.2x", line[i] & 0xff);
144
                        }
145
                        else {
146
                                printk("%.2x", line[i] & 0xff);
147
                        }
148
                }
149
 
150
                /* Print empty space */
151
                for (; i < 16; i++) {
152
                        if (!(i & 1)) {
153
                                printk("   ");
154
                        }
155
                        else {
156
                                printk("  ");
157
                        }
158
                }
159
                printk("  ");
160
 
161
                for (i = 0; i < j; i++) {
162
                        if (jffs_isgraph(line[i])) {
163
                                printk("%c", line[i]);
164
                        }
165
                        else {
166
                                printk(".");
167
                        }
168
                }
169
                printk("\n");
170
                size -= 16;
171
        }
172
}
173
#endif
174
 
175
inline int
176
jffs_min(int a, int b)
177
{
178
        return (a < b ? a : b);
179
}
180
 
181
 
182
inline int
183
jffs_max(int a, int b)
184
{
185
        return (a > b ? a : b);
186
}
187
 
188
 
189
/* This routine calculates checksums in JFFS.  */
190
__u32
191
jffs_checksum(const void *data, int size)
192
{
193
        __u32 sum = 0;
194
        __u8 *ptr = (__u8 *)data;
195
        D3(printk("#csum at 0x%p, {0x%08lx, 0x%08lx, ... }, size: %d",
196
                  data, *(long *)data, *((long *)data + 1), size));
197
        while (size-- > 0) {
198
                sum += *ptr++;
199
        }
200
        D3(printk(", result: 0x%08x\n", sum));
201
        return sum;
202
}
203
 
204
 
205
/* Create and initialize a new struct jffs_file.  */
206
static struct jffs_file *
207
jffs_create_file(struct jffs_control *c,
208
                 const struct jffs_raw_inode *raw_inode)
209
{
210
        struct jffs_file *f;
211
 
212
        if (!(f = (struct jffs_file *)kmalloc(sizeof(struct jffs_file),
213
                                              GFP_KERNEL))) {
214
                D(printk("jffs_create_file(): Failed!\n"));
215
                return 0;
216
        }
217
        DJM(no_jffs_file++);
218
        memset(f, 0, sizeof(struct jffs_file));
219
        f->ino = raw_inode->ino;
220
        f->pino = raw_inode->pino;
221
        f->nlink = raw_inode->nlink;
222
        f->deleted = raw_inode->deleted;
223
        f->c = c;
224
 
225
        return f;
226
}
227
 
228
 
229
/* Build a control block for the file system.  */
230
static struct jffs_control *
231
jffs_create_control(kdev_t dev)
232
{
233
        struct jffs_control *c;
234
        register int s = sizeof(struct jffs_control);
235
        D(char *t = 0);
236
 
237
        D2(printk("jffs_create_control()\n"));
238
 
239
        if (!(c = (struct jffs_control *)kmalloc(s, GFP_KERNEL))) {
240
                goto fail_control;
241
        }
242
        DJM(no_jffs_control++);
243
        c->root = 0;
244
        c->hash_len = JFFS_HASH_SIZE;
245
        s = sizeof(struct jffs_file *) * c->hash_len;
246
        if (!(c->hash = (struct jffs_file **)kmalloc(s, GFP_KERNEL))) {
247
                goto fail_hash;
248
        }
249
        DJM(no_hash++);
250
        memset(c->hash, 0, s);
251
        if (!(c->fmc = jffs_build_begin(c, dev))) {
252
                goto fail_fminit;
253
        }
254
        c->next_ino = JFFS_MIN_INO + 1;
255
        c->rename_lock = 0;
256
        c->rename_wait = (struct wait_queue *)0;
257
        return c;
258
 
259
fail_fminit:
260
        D(t = "c->fmc");
261
fail_hash:
262
        kfree(c);
263
        DJM(no_jffs_control--);
264
        D(t = t ? t : "c->hash");
265
fail_control:
266
        D(t = t ? t : "control");
267
        D(printk("jffs_create_control(): Allocation failed: (%s)\n", t));
268
        return (struct jffs_control *)0;
269
}
270
 
271
 
272
/* Clean up all data structures associated with the file system.  */
273
void
274
jffs_cleanup_control(struct jffs_control *c)
275
{
276
        D2(printk("jffs_cleanup_control()\n"));
277
 
278
        if (!c) {
279
                D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
280
                return;
281
        }
282
 
283
        /* Free all files and nodes.  */
284
        if (c->hash) {
285
                jffs_foreach_file(c, jffs_free_node_list);
286
                kfree(c->hash);
287
                DJM(no_hash--);
288
        }
289
        jffs_cleanup_fmcontrol(c->fmc);
290
        kfree(c);
291
        DJM(no_jffs_control--);
292
        D3(printk("jffs_cleanup_control(): Leaving...\n"));
293
}
294
 
295
 
296
/* This function adds a virtual root node to the in-RAM representation.
297
   Called by jffs_build_fs().  */
298
static int
299
jffs_add_virtual_root(struct jffs_control *c)
300
{
301
        struct jffs_file *root;
302
        struct jffs_node *node;
303
 
304
        D2(printk("jffs_add_virtual_root(): "
305
                  "Creating a virtual root directory.\n"));
306
 
307
        if (!(root = (struct jffs_file *)kmalloc(sizeof(struct jffs_file),
308
                                                 GFP_KERNEL))) {
309
                return -ENOMEM;
310
        }
311
        DJM(no_jffs_file++);
312
        if (!(node = (struct jffs_node *)kmalloc(sizeof(struct jffs_node),
313
                                                 GFP_KERNEL))) {
314
                kfree(root);
315
                DJM(no_jffs_file--);
316
                return -ENOMEM;
317
        }
318
        DJM(no_jffs_node++);
319
        memset(node, 0, sizeof(struct jffs_node));
320
        node->ino = JFFS_MIN_INO;
321
        memset(root, 0, sizeof(struct jffs_file));
322
        root->ino = JFFS_MIN_INO;
323
        root->mode = S_IFDIR | S_IRWXU | S_IRGRP
324
                     | S_IXGRP | S_IROTH | S_IXOTH;
325
        root->atime = root->mtime = root->ctime = CURRENT_TIME;
326
        root->nlink = 1;
327
        root->c = c;
328
        root->version_head = root->version_tail = node;
329
        jffs_insert_file_into_hash(root);
330
        return 0;
331
}
332
 
333
 
334
/* This is where the file system is built and initialized.  */
335
int
336
jffs_build_fs(struct super_block *sb)
337
{
338
        struct jffs_control *c;
339
        int err = 0;
340
 
341
        D2(printk("jffs_build_fs()\n"));
342
 
343
        if (!(c = jffs_create_control(sb->s_dev))) {
344
                return -ENOMEM;
345
        }
346
        c->building_fs = 1;
347
        c->sb = sb;
348
        if ((err = jffs_scan_flash(c)) < 0) {
349
                goto jffs_build_fs_fail;
350
        }
351
 
352
        /* Add a virtual root node if no one exists.  */
353
        if (!jffs_find_file(c, JFFS_MIN_INO)) {
354
                if ((err = jffs_add_virtual_root(c)) < 0) {
355
                        goto jffs_build_fs_fail;
356
                }
357
        }
358
 
359
        /* Remove deleted nodes.  */
360
        if ((err = jffs_foreach_file(c, jffs_possibly_delete_file)) < 0) {
361
                printk(KERN_ERR "JFFS: Failed to remove deleted nodes.\n");
362
                goto jffs_build_fs_fail;
363
        }
364
        /* Remove redundant nodes.  (We are not interested in the
365
           return value in this case.)  */
366
        jffs_foreach_file(c, jffs_remove_redundant_nodes);
367
        /* Try to build a tree from all the nodes.  */
368
        if ((err = jffs_foreach_file(c, jffs_insert_file_into_tree)) < 0) {
369
                printk("JFFS: Failed to build tree.\n");
370
                goto jffs_build_fs_fail;
371
        }
372
        /* Compute the sizes of all files in the filesystem.  Adjust if
373
           necessary.  */
374
        if ((err = jffs_foreach_file(c, jffs_build_file)) < 0) {
375
                printk("JFFS: Failed to build file system.\n");
376
                goto jffs_build_fs_fail;
377
        }
378
        sb->u.generic_sbp = (void *)c;
379
        c->building_fs = 0;
380
 
381
        D1(jffs_print_hash_table(c));
382
        D1(jffs_print_tree(c->root, 0));
383
 
384
        return 0;
385
 
386
jffs_build_fs_fail:
387
        jffs_cleanup_control(c);
388
        return err;
389
} /* jffs_build_fs()  */
390
 
391
 
392
#if defined(JFFS_FLASH_SHORTCUT) && JFFS_FLASH_SHORTCUT
393
 
394
/* Scan the whole flash memory in order to find all nodes in the
395
   file systems.  */
396
static int
397
jffs_scan_flash(struct jffs_control *c)
398
{
399
        char name[JFFS_MAX_NAME_LEN + 2];
400
        struct jffs_raw_inode raw_inode;
401
        struct jffs_node *node = 0;
402
        struct jffs_fmcontrol *fmc = c->fmc;
403
        __u32 checksum;
404
        __u8 tmp_accurate;
405
        __u16 tmp_chksum;
406
        unsigned char *pos = (unsigned char *) fmc->flash_start;
407
        unsigned char *start;
408
        unsigned char *end = (unsigned char *)
409
                             (fmc->flash_start + fmc->flash_size);
410
 
411
        D1(printk("jffs_scan_flash(): start pos = 0x%p, end = 0x%p\n",
412
                  pos, end));
413
 
414
        flash_safe_acquire(fmc->flash_part);
415
 
416
        /* Start the scan.  */
417
        while (pos < end) {
418
 
419
                /* Remember the position from where we started this scan.  */
420
                start = pos;
421
 
422
                switch (*(__u32 *)pos) {
423
                case JFFS_EMPTY_BITMASK:
424
                        /* We have found 0xff on this block.  We have to
425
                           scan the rest of the block to be sure it is
426
                           filled with 0xff.  */
427
                        D1(printk("jffs_scan_flash(): 0xff at pos 0x%p.\n",
428
                                  pos));
429
                        for (; pos < end
430
                               && JFFS_EMPTY_BITMASK == *(__u32 *)pos;
431
                             pos += 4);
432
                        D1(printk("jffs_scan_flash(): 0xff ended at "
433
                                  "pos 0x%p.\n", pos));
434
                        continue;
435
 
436
                case JFFS_DIRTY_BITMASK:
437
                        /* We have found 0x00 on this block.  We have to
438
                           scan as far as possible to find out how much
439
                           is dirty.  */
440
                        D1(printk("jffs_scan_flash(): 0x00 at pos 0x%p.\n",
441
                                  pos));
442
                        for (; pos < end
443
                               && JFFS_DIRTY_BITMASK == *(__u32 *)pos;
444
                             pos += 4);
445
                        D1(printk("jffs_scan_flash(): 0x00 ended at "
446
                                  "pos 0x%p.\n", pos));
447
                        jffs_fmalloced(fmc, (__u32) start,
448
                                       (__u32) (pos - start), 0);
449
                        continue;
450
 
451
                case JFFS_MAGIC_BITMASK:
452
                        /* We have probably found a new raw inode.  */
453
                        break;
454
 
455
                default:
456
                bad_inode:
457
                        /* We're f*cked.  This is not solved yet.  We have
458
                           to scan for the magic pattern.  */
459
                        D1(printk("*************** Dirty flash memory or bad inode: "
460
                                  "hexdump(pos = 0x%p, len = 128):\n",
461
                                  pos));
462
                        D1(jffs_hexdump(pos, 128));
463
                        for (pos += 4; pos < end; pos += 4) {
464
                                switch (*(__u32 *)pos) {
465
                                case JFFS_MAGIC_BITMASK:
466
                                        jffs_fmalloced(fmc, (__u32) start,
467
                                                       (__u32) (pos - start),
468
                                                       0);
469
                                        goto cont_scan;
470
                                default:
471
                                        break;
472
                                }
473
                        }
474
                        cont_scan:
475
                        continue;
476
                }
477
 
478
                /* We have found the beginning of an inode.  Create a
479
                   node for it.  */
480
                if (!node) {
481
                        if (!(node = (struct jffs_node *)
482
                                     kmalloc(sizeof(struct jffs_node),
483
                                             GFP_KERNEL))) {
484
                                flash_safe_release(fmc->flash_part);
485
                                return -ENOMEM;
486
                        }
487
                        DJM(no_jffs_node++);
488
                }
489
 
490
                /* Read the next raw inode.  */
491
                memcpy(&raw_inode, pos, sizeof(struct jffs_raw_inode));
492
 
493
                /* When we compute the checksum for the inode, we never
494
                   count the 'accurate' or the 'checksum' fields.  */
495
                tmp_accurate = raw_inode.accurate;
496
                tmp_chksum = raw_inode.chksum;
497
                raw_inode.accurate = 0;
498
                raw_inode.chksum = 0;
499
                checksum = jffs_checksum(&raw_inode,
500
                                         sizeof(struct jffs_raw_inode));
501
                raw_inode.accurate = tmp_accurate;
502
                raw_inode.chksum = tmp_chksum;
503
 
504
                D3(printk("*** We have found this raw inode at pos 0x%p "
505
                          "on the flash:\n", pos));
506
                D3(jffs_print_raw_inode(&raw_inode));
507
 
508
                if (checksum != raw_inode.chksum) {
509
                        D1(printk("jffs_scan_flash(): Bad checksum: "
510
                                  "checksum = %u, "
511
                                  "raw_inode.chksum = %u\n",
512
                                  checksum, raw_inode.chksum));
513
                        pos += sizeof(struct jffs_raw_inode);
514
                        jffs_fmalloced(fmc, (__u32) start,
515
                                       (__u32) (pos - start), 0);
516
                        /* Reuse this unused struct jffs_node.  */
517
                        continue;
518
                }
519
 
520
                /* Check the raw inode read so far.  Start with the
521
                   maximum length of the filename.  */
522
                if (raw_inode.nsize > JFFS_MAX_NAME_LEN) {
523
                        goto bad_inode;
524
                }
525
                /* The node's data segment should not exceed a
526
                   certain length.  */
527
                if (raw_inode.dsize > fmc->max_chunk_size) {
528
                        goto bad_inode;
529
                }
530
 
531
                pos += sizeof(struct jffs_raw_inode);
532
 
533
                /* This shouldn't be necessary because a node that
534
                   violates the flash boundaries shouldn't be written
535
                   in the first place. */
536
                if (pos >= end) {
537
                        goto check_node;
538
                }
539
 
540
                /* Read the name.  */
541
                *name = 0;
542
                if (raw_inode.nsize) {
543
                        memcpy(name, pos, raw_inode.nsize);
544
                        name[raw_inode.nsize] = '\0';
545
                        pos += raw_inode.nsize
546
                               + JFFS_GET_PAD_BYTES(raw_inode.nsize);
547
                        D3(printk("name == \"%s\"\n", name));
548
                        checksum = jffs_checksum(name, raw_inode.nsize);
549
                        if (checksum != raw_inode.nchksum) {
550
                                D1(printk("jffs_scan_flash(): Bad checksum: "
551
                                          "checksum = %u, "
552
                                          "raw_inode.nchksum = %u\n",
553
                                          checksum, raw_inode.nchksum));
554
                                jffs_fmalloced(fmc, (__u32) start,
555
                                               (__u32) (pos - start), 0);
556
                                /* Reuse this unused struct jffs_node.  */
557
                                continue;
558
                        }
559
                        if (pos >= end) {
560
                                goto check_node;
561
                        }
562
                }
563
 
564
                /* Read the data in order to be sure it matches the
565
                   checksum.  */
566
                checksum = jffs_checksum(pos, raw_inode.dsize);
567
                pos += raw_inode.dsize + JFFS_GET_PAD_BYTES(raw_inode.dsize);
568
 
569
                if (checksum != raw_inode.dchksum) {
570
                        D1(printk("jffs_scan_flash(): Bad checksum: "
571
                                  "checksum = %u, "
572
                                  "raw_inode.dchksum = %u\n",
573
                                  checksum, raw_inode.dchksum));
574
                        jffs_fmalloced(fmc, (__u32) start,
575
                                       (__u32) (pos - start), 0);
576
                        /* Reuse this unused struct jffs_node.  */
577
                        continue;
578
                }
579
 
580
                check_node:
581
 
582
                /* Remember the highest inode number in the whole file
583
                   system.  This information will be used when assigning
584
                   new files new inode numbers.  */
585
                if (c->next_ino <= raw_inode.ino) {
586
                        c->next_ino = raw_inode.ino + 1;
587
                }
588
 
589
                if (raw_inode.accurate) {
590
                        int err;
591
                        node->data_offset = raw_inode.offset;
592
                        node->data_size = raw_inode.dsize;
593
                        node->removed_size = raw_inode.rsize;
594
                        /* Compute the offset to the actual data in the
595
                           on-flash node.  */
596
                        node->fm_offset
597
                        = sizeof(struct jffs_raw_inode)
598
                          + raw_inode.nsize
599
                          + JFFS_GET_PAD_BYTES(raw_inode.nsize);
600
                        node->fm = jffs_fmalloced(fmc, (__u32) start,
601
                                                  (__u32) (pos - start),
602
                                                  node);
603
                        if (!node->fm) {
604
                                D(printk("jffs_scan_flash(): !node->fm\n"));
605
                                kfree(node);
606
                                DJM(no_jffs_node--);
607
                                flash_safe_release(fmc->flash_part);
608
                                return -ENOMEM;
609
                        }
610
                        if ((err = jffs_insert_node(c, 0, &raw_inode,
611
                                                    name, node)) < 0) {
612
                                printk("JFFS: Failed to handle raw inode. "
613
                                       "(err = %d)\n", err);
614
                                break;
615
                        }
616
                        D3(jffs_print_node(node));
617
                        node = 0; /* Don't free the node!  */
618
                }
619
                else {
620
                        jffs_fmalloced(fmc, (__u32) start,
621
                                       (__u32) (pos - start), 0);
622
                        D3(printk("jffs_scan_flash(): Just found an obsolete "
623
                                  "raw_inode. Continuing the scan...\n"));
624
                        /* Reuse this unused struct jffs_node.  */
625
                }
626
        }
627
 
628
        if (node) {
629
                kfree(node);
630
                DJM(no_jffs_node--);
631
        }
632
        jffs_build_end(fmc);
633
        D3(printk("jffs_scan_flash(): Leaving...\n"));
634
        flash_safe_release(fmc->flash_part);
635
        return 0;
636
} /* jffs_scan_flash()  */
637
 
638
#else
639
 
640
/* Scan the whole flash memory in order to find all nodes in the
641
   file systems.  */
642
int
643
jffs_scan_flash(struct jffs_control *c)
644
{
645
        char name[JFFS_MAX_NAME_LEN + 2];
646
        struct jffs_raw_inode raw_inode;
647
        struct jffs_node *node = 0;
648
        struct buffer_head *bh;
649
        kdev_t dev = c->sb->s_dev;
650
        __u32 block = 0;
651
        __u32 last_block = c->fmc->flash_size / BLOCK_SIZE - 1;
652
        __u32 block_offset = 0;
653
        __u32 read_size;
654
        __u32 checksum;
655
        __u32 offset; /* Offset relative to the start of the flash memory.  */
656
        __u8 tmp_accurate;
657
        __u32 tmp_chksum;
658
        __u32 size;
659
 
660
        D(printk("jffs_scan_flash()\n"));
661
 
662
        if (!(bh = bread(dev, block, BLOCK_SIZE))) {
663
                D(printk("jffs_scan_flash(): First bread() failed.\n"));
664
                return -1;
665
        }
666
 
667
        /* Start the scan.  */
668
        while (block <= last_block) {
669
                if (block_offset >= BLOCK_SIZE) {
670
                        brelse(bh);
671
                        if (block == last_block) {
672
                                bh = 0;
673
                                goto end_of_scan;
674
                        }
675
                        if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
676
                                return -1;
677
                        }
678
                        block_offset = 0;
679
                }
680
                offset = block * BLOCK_SIZE + block_offset;
681
                D(printk("jffs_scan_flash(): offset = %u\n", offset));
682
 
683
                switch (*(__u32 *)&bh->b_data[block_offset]) {
684
                case JFFS_EMPTY_BITMASK:
685
                        /* We have found 0xff on this block.  We have to
686
                           scan the rest of the block to be sure it is
687
                           filled with 0xff.  */
688
                        D(printk("jffs_scan_flash(): 0xff on block %u, "
689
                                 "block_offset %u.\n", block, block_offset));
690
                        block_offset += 4;
691
                        while (block <= last_block) {
692
                                for (; block_offset < BLOCK_SIZE;
693
                                     block_offset += 4) {
694
                                        if (JFFS_EMPTY_BITMASK
695
                                            != *(__u32 *)&bh->b_data[block_offset]) {
696
                                                goto ff_scan_end;
697
                                        }
698
                                }
699
                                brelse(bh);
700
                                block_offset = 0;
701
                                if (block >= last_block) {
702
                                        bh = 0;
703
                                        D(printk("jffs_scan_flash(): "
704
                                                 "0xff size: %d\n",
705
                                                 (last_block + 1) * BLOCK_SIZE
706
                                                 - offset));
707
                                        goto end_of_scan;
708
                                }
709
                                if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
710
                                        return -1;
711
                                }
712
                        }
713
                ff_scan_end:
714
                        D(printk("jffs_scan_flash(): 0xff size: %d\n",
715
                                 block * BLOCK_SIZE + block_offset - offset));
716
                        continue;
717
                case JFFS_DIRTY_BITMASK:
718
                        /* We have found 0x00 on this block.  We have to
719
                           scan as far as possible to find out how much
720
                           is dirty.  */
721
                        D(printk("jffs_scan_flash(): 0x00 on block %u, "
722
                                 "block_offset %u.\n",
723
                                 block, block_offset));
724
                        block_offset += 4;
725
                        while (block < last_block) {
726
                                for (; block_offset < BLOCK_SIZE;
727
                                     block_offset += 4) {
728
                                        if (*(__u32 *)&bh->b_data[block_offset]
729
                                            != JFFS_DIRTY_BITMASK) {
730
                                                goto zero_scan_end;
731
                                        }
732
                                }
733
                                brelse(bh);
734
                                if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
735
                                        return -1;
736
                                }
737
                                block_offset = 0;
738
                        }
739
                zero_scan_end:
740
                        D(printk("jffs_scan_flash(): 0x00 size: %d\n",
741
                                 block * BLOCK_SIZE + block_offset - offset));
742
                        jffs_fmalloced(c->fmc, offset,
743
                                       block * BLOCK_SIZE + block_offset
744
                                       - offset, 0);
745
                        continue;
746
                case JFFS_MAGIC_BITMASK:
747
                        /* We have probably found a new raw inode.  */
748
                        break;
749
                default:
750
                        /* We're f*cked.  This is not solved yet.  We have
751
                           to scan for the magic pattern.  */
752
                        D(printk("jffs_scan_flash(): Block #%u at "
753
                                 "block_offset %u is dirty!\n",
754
                                 block, block_offset));
755
                        D(printk("  offset: %u\n", offset));
756
                        D(printk("  data: %u\n",
757
                                 *(__u32 *)&bh->b_data[block_offset]));
758
                        jffs_fmalloced(c->fmc, offset,
759
                                       BLOCK_SIZE - block_offset, 0);
760
                        block_offset = BLOCK_SIZE;
761
                        continue;
762
                }
763
 
764
                /* We have found the beginning of an inode.  Create a
765
                   node for it.  */
766
                if (!node) {
767
                        if (!(node = (struct jffs_node *)
768
                                     kmalloc(sizeof(struct jffs_node),
769
                                             GFP_KERNEL))) {
770
                                brelse(bh);
771
                                return -ENOMEM;
772
                        }
773
                        DJM(no_jffs_node++);
774
                }
775
 
776
                /* Read the next raw inode.  */
777
                read_size = jffs_min(BLOCK_SIZE - block_offset,
778
                                     sizeof(struct jffs_raw_inode));
779
                memcpy(&raw_inode, &bh->b_data[block_offset], read_size);
780
                D(printk("jffs_scan_flash(): block_offset: %u, "
781
                         "read_size: %u\n", block_offset, read_size));
782
                block_offset += read_size;
783
                if (read_size < sizeof(struct jffs_raw_inode)) {
784
                        brelse(bh);
785
                        if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
786
                                return -1;
787
                        }
788
                        block_offset = sizeof(struct jffs_raw_inode) - read_size;
789
                        memcpy((void *)&raw_inode + read_size, bh->b_data,
790
                               block_offset);
791
                }
792
                /* When we compute the checksum for the inode, we never count
793
                   the 'accurate' or the 'checksum' fields.  */
794
                tmp_accurate = raw_inode.accurate;
795
                tmp_chksum = raw_inode.chksum;
796
                raw_inode.accurate = 0;
797
                raw_inode.chksum = 0;
798
                checksum = jffs_checksum(&raw_inode,
799
                                         sizeof(struct jffs_raw_inode));
800
                raw_inode.accurate = tmp_accurate;
801
                raw_inode.chksum = tmp_chksum;
802
 
803
                D(printk("*** We have found this raw inode at pos 0x%08x "
804
                         "on the flash:\n", offset));
805
                jffs_print_raw_inode(&raw_inode);
806
 
807
                if (block_offset == BLOCK_SIZE) {
808
                        brelse(bh);
809
                        if (block == last_block) {
810
                                bh = 0;
811
                                goto check_node;
812
                        }
813
                        if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
814
                                return -1;
815
                        }
816
                        block_offset = 0;
817
                }
818
 
819
                /* Read the name.  */
820
                *name = 0;
821
                if (raw_inode.nsize) {
822
                        read_size = jffs_min(BLOCK_SIZE - block_offset,
823
                                             raw_inode.nsize);
824
                        memcpy(name, &bh->b_data[block_offset], read_size);
825
                        block_offset += read_size;
826
                        if (read_size < raw_inode.nsize) {
827
                                /* We haven't read the whole name.  */
828
                                brelse(bh);
829
                                if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
830
                                        return -1;
831
                                }
832
                                block_offset = raw_inode.nsize - read_size;
833
                                memcpy(&name[read_size], bh->b_data, block_offset);
834
                        }
835
                        block_offset += JFFS_GET_PAD_BYTES(block_offset);
836
                        name[raw_inode.nsize] = '\0';
837
                        checksum += jffs_checksum(name, raw_inode.nsize);
838
                }
839
 
840
                if (block_offset == BLOCK_SIZE) {
841
                        brelse(bh);
842
                        if (block == last_block) {
843
                                bh = 0;
844
                                goto check_node;
845
                        }
846
                        if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
847
                                return -1;
848
                        }
849
                        block_offset = 0;
850
                }
851
 
852
                /* Read the data in order to be sure it matches the
853
                   checksum.  */
854
                if (raw_inode.dsize) {
855
                        __u32 chunk_size = jffs_min(BLOCK_SIZE - block_offset,
856
                                                    raw_inode.dsize);
857
                        __u32 data_read = chunk_size;
858
                        checksum += jffs_checksum(&bh->b_data[block_offset],
859
                                                  data_read);
860
                        block_offset += chunk_size;
861
                        while (data_read < raw_inode.dsize) {
862
                                brelse(bh);
863
                                if (!(bh = bread(dev, ++block, BLOCK_SIZE))) {
864
                                        return -1;
865
                                }
866
                                chunk_size = jffs_min(BLOCK_SIZE,
867
                                                      raw_inode.dsize
868
                                                      - data_read);
869
                                data_read += chunk_size;
870
                                checksum += jffs_checksum(bh->b_data,
871
                                                          chunk_size);
872
                                block_offset = chunk_size;
873
                        }
874
                }
875
                size = sizeof(struct jffs_raw_inode) + raw_inode.nsize
876
                       + raw_inode.dsize;
877
 
878
                block_offset += JFFS_GET_PAD_BYTES(block_offset);
879
 
880
                /* Make sure the checksums are equal.  */
881
                if (checksum != raw_inode.chksum) {
882
                        /* Something was wrong with the node.  The node
883
                           has to be discarded.  */
884
                        D(printk("jffs_scan_flash(): checksum == %u, "
885
                                 "raw_inode.chksum == %u\n",
886
                                 checksum, raw_inode.chksum));
887
                        jffs_fmalloced(c->fmc, offset, size, 0);
888
                        /* Reuse this unused struct jffs_node.  */
889
                        continue;
890
                }
891
 
892
                /* Remember the highest inode number in the whole file
893
                   system.  This information will be used when assigning
894
                   new files new inode numbers.  */
895
                if (c->next_ino <= raw_inode.ino) {
896
                        c->next_ino = raw_inode.ino + 1;
897
                }
898
 
899
                check_node:
900
                if (raw_inode.accurate) {
901
                        node->data_offset = raw_inode.offset;
902
                        node->data_size = raw_inode.dsize;
903
                        node->removed_size = raw_inode.rsize;
904
                        node->fm_offset = sizeof(struct jffs_raw_inode)
905
                                          + raw_inode.nsize
906
                                          + JFFS_GET_PAD_BYTES(raw_inode.nsize);
907
                        node->fm = jffs_fmalloced(c->fmc, offset, size, node);
908
                        if (!node->fm) {
909
                                D(printk("jffs_scan_flash(): !node->fm\n"));
910
                                kfree(node);
911
                                DJM(no_jffs_node--);
912
                                brelse(bh);
913
                                return -ENOMEM;
914
                        }
915
                        jffs_insert_node(c, 0, &raw_inode, name, node);
916
                        jffs_print_node(node);
917
                        node = 0; /* Don't free the node!  */
918
                }
919
                else {
920
                        jffs_fmalloced(c->fmc, offset, size, 0);
921
                        D(printk("jffs_scan_flash(): Just found an obsolete "
922
                                 "raw_inode. Continuing the scan...\n"));
923
                        /* Reuse this unused struct jffs_node.  */
924
                }
925
        }
926
 
927
        end_of_scan:
928
        brelse(bh);
929
        if (node) {
930
                kfree(node);
931
                DJM(no_jffs_node--);
932
        }
933
        jffs_build_end(c->fmc);
934
        D(printk("jffs_scan_flash(): Leaving...\n"));
935
        return 0;
936
} /* jffs_scan_flash()  */
937
 
938
#endif
939
 
940
 
941
/* Insert any kind of node into the file system.  Take care of data
942
   insertions and deletions.  Also remove redundant information. The
943
   memory allocated for the `name' is regarded as "given away" in the
944
   caller's perspective.  */
945
int
946
jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
947
                 const struct jffs_raw_inode *raw_inode,
948
                 const char *name, struct jffs_node *node)
949
{
950
        int update_name = 0;
951
        int insert_into_tree = 0;
952
 
953
        D2(printk("jffs_insert_node(): ino = %u, version = %u, name = \"%s\"\n",
954
                  raw_inode->ino, raw_inode->version,
955
                  ((name && *name) ? name : "")));
956
 
957
        /* If there doesn't exist an associated jffs_file, then
958
           create, initialize and insert one into the file system.  */
959
        if (!f && !(f = jffs_find_file(c, raw_inode->ino))) {
960
                if (!(f = jffs_create_file(c, raw_inode))) {
961
                        return -ENOMEM;
962
                }
963
                jffs_insert_file_into_hash(f);
964
                insert_into_tree = 1;
965
        }
966
 
967
        node->ino = raw_inode->ino;
968
        node->version = raw_inode->version;
969
        node->data_size = raw_inode->dsize;
970
        node->fm_offset = sizeof(struct jffs_raw_inode) + raw_inode->nsize
971
                          + JFFS_GET_PAD_BYTES(raw_inode->nsize);
972
        node->name_size = raw_inode->nsize;
973
 
974
        /* Now insert the node at the correct position into the file's
975
           version list.  */
976
        if (!f->version_head) {
977
                /* This is the first node.  */
978
                f->version_head = node;
979
                f->version_tail = node;
980
                node->version_prev = 0;
981
                node->version_next = 0;
982
                f->highest_version = node->version;
983
                update_name = 1;
984
                f->mode = raw_inode->mode;
985
                f->uid = raw_inode->uid;
986
                f->gid = raw_inode->gid;
987
                f->atime = raw_inode->atime;
988
                f->mtime = raw_inode->mtime;
989
                f->ctime = raw_inode->ctime;
990
                f->deleted = raw_inode->deleted;
991
        }
992
        else if ((f->highest_version < node->version)
993
                 || (node->version == 0)) {
994
                /* Insert at the end of the list.  I.e. this node is the
995
                   oldest one so far.  */
996
                node->version_prev = f->version_tail;
997
                node->version_next = 0;
998
                f->version_tail->version_next = node;
999
                f->version_tail = node;
1000
                f->highest_version = node->version;
1001
                update_name = 1;
1002
                f->pino = raw_inode->pino;
1003
                f->mode = raw_inode->mode;
1004
                f->uid = raw_inode->uid;
1005
                f->gid = raw_inode->gid;
1006
                f->atime = raw_inode->atime;
1007
                f->mtime = raw_inode->mtime;
1008
                f->ctime = raw_inode->ctime;
1009
                f->deleted = raw_inode->deleted;
1010
        }
1011
        else if (f->version_head->version > node->version) {
1012
                /* Insert at the bottom of the list.  */
1013
                node->version_prev = 0;
1014
                node->version_next = f->version_head;
1015
                f->version_head->version_prev = node;
1016
                f->version_head = node;
1017
                if (!f->name) {
1018
                        update_name = 1;
1019
                }
1020
                if (raw_inode->deleted) {
1021
                        f->deleted = raw_inode->deleted;
1022
                }
1023
        }
1024
        else {
1025
                struct jffs_node *n;
1026
                int newer_name = 0;
1027
                /* Search for the insertion position starting from
1028
                   the tail (newest node).  */
1029
                for (n = f->version_tail; n; n = n->version_prev) {
1030
                        if (n->version < node->version) {
1031
                                node->version_prev = n;
1032
                                node->version_next = n->version_next;
1033
                                node->version_next->version_prev = node;
1034
                                n->version_next = node;
1035
                                if (!newer_name) {
1036
                                        update_name = 1;
1037
                                }
1038
                                break;
1039
                        }
1040
                        if (n->name_size) {
1041
                                newer_name = 1;
1042
                        }
1043
                }
1044
        }
1045
 
1046
        /* Perhaps update the name.  */
1047
        if (raw_inode->nsize && update_name && name && *name) {
1048
                if (f->name) {
1049
                        kfree(f->name);
1050
                        DJM(no_name--);
1051
                }
1052
                if (!(f->name = (char *) kmalloc(raw_inode->nsize + 1,
1053
                                                 GFP_KERNEL))) {
1054
                        return -ENOMEM;
1055
                }
1056
                DJM(no_name++);
1057
                memcpy(f->name, name, raw_inode->nsize);
1058
                f->name[raw_inode->nsize] = '\0';
1059
                f->nsize = raw_inode->nsize;
1060
                D3(printk("jffs_insert_node(): Updated the name of "
1061
                          "the file to \"%s\".\n", name));
1062
        }
1063
 
1064
        if (!c->building_fs) {
1065
                D3(printk("jffs_insert_node(): ---------------------------"
1066
                          "------------------------------------------- 1\n"));
1067
                if (insert_into_tree) {
1068
                        jffs_insert_file_into_tree(f);
1069
                }
1070
                if (f->deleted) {
1071
                        /* Mark all versions of the node as obsolete.  */
1072
                        jffs_possibly_delete_file(f);
1073
                }
1074
                else {
1075
                        if (node->data_size || node->removed_size) {
1076
                                jffs_update_file(f, node);
1077
                        }
1078
                        jffs_remove_redundant_nodes(f);
1079
                }
1080
#ifdef USE_GC
1081
                if (!c->fmc->no_call_gc) {
1082
                        jffs_garbage_collect(c);
1083
                }
1084
#endif
1085
                D3(printk("jffs_insert_node(): ---------------------------"
1086
                          "------------------------------------------- 2\n"));
1087
        }
1088
 
1089
        return 0;
1090
} /* jffs_insert_node()  */
1091
 
1092
 
1093
/* Unlink a jffs_node from the version list it is in.  */
1094
static inline void
1095
jffs_unlink_node_from_version_list(struct jffs_file *f,
1096
                                   struct jffs_node *node)
1097
{
1098
        if (node->version_prev) {
1099
                node->version_prev->version_next = node->version_next;
1100
        } else {
1101
                f->version_head = node->version_next;
1102
        }
1103
        if (node->version_next) {
1104
                node->version_next->version_prev = node->version_prev;
1105
        } else {
1106
                f->version_tail = node->version_prev;
1107
        }
1108
}
1109
 
1110
 
1111
/* Unlink a jffs_node from the range list it is in.  */
1112
static inline void
1113
jffs_unlink_node_from_range_list(struct jffs_file *f, struct jffs_node *node)
1114
{
1115
        if (node->range_prev) {
1116
                node->range_prev->range_next = node->range_next;
1117
        }
1118
        else {
1119
                f->range_head = node->range_next;
1120
        }
1121
        if (node->range_next) {
1122
                node->range_next->range_prev = node->range_prev;
1123
        }
1124
        else {
1125
                f->range_tail = node->range_prev;
1126
        }
1127
}
1128
 
1129
 
1130
/* Function used by jffs_remove_redundant_nodes() below.  This function
1131
   classifies what kind of information a node adds to a file.  */
1132
static inline __u8
1133
jffs_classify_node(struct jffs_node *node)
1134
{
1135
        __u8 mod_type = JFFS_MODIFY_INODE;
1136
 
1137
        if (node->name_size) {
1138
                mod_type |= JFFS_MODIFY_NAME;
1139
        }
1140
        if (node->data_size || node->removed_size) {
1141
                mod_type |= JFFS_MODIFY_DATA;
1142
        }
1143
        return mod_type;
1144
}
1145
 
1146
 
1147
/* Remove redundant nodes from a file.  Mark the on-flash memory
1148
   as dirty.  */
1149
int
1150
jffs_remove_redundant_nodes(struct jffs_file *f)
1151
{
1152
        struct jffs_node *newest_node;
1153
        struct jffs_node *cur;
1154
        struct jffs_node *prev;
1155
        __u8 newest_type;
1156
        __u8 mod_type;
1157
        __u8 node_with_name_later = 0;
1158
 
1159
        if (!(newest_node = f->version_tail)) {
1160
                return 0;
1161
        }
1162
 
1163
        /* What does the `newest_node' modify?  */
1164
        newest_type = jffs_classify_node(newest_node);
1165
        node_with_name_later = newest_type & JFFS_MODIFY_NAME;
1166
 
1167
        D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
1168
                  "newest_type: %u\n", f->ino, (f->name ? f->name : ""),
1169
                  newest_type));
1170
 
1171
        /* Traverse the file's nodes and determine which of them that are
1172
           superfluous.  Yeah, this might look very complex at first
1173
           glance but it is actually very simple.  */
1174
        for (cur = newest_node->version_prev; cur; cur = prev) {
1175
                prev = cur->version_prev;
1176
                mod_type = jffs_classify_node(cur);
1177
                if ((mod_type <= JFFS_MODIFY_INODE)
1178
                    || ((newest_type & JFFS_MODIFY_NAME)
1179
                        && (mod_type
1180
                            <= (JFFS_MODIFY_INODE + JFFS_MODIFY_NAME)))
1181
                    || (cur->data_size == 0 && cur->removed_size
1182
                        && !cur->version_prev && node_with_name_later)) {
1183
                        /* Yes, this node is redundant. Remove it.  */
1184
                        D2(printk("jffs_remove_redundant_nodes(): "
1185
                                  "Removing node: ino: %u, version: %u, "
1186
                                  "mod_type: %u\n", cur->ino, cur->version,
1187
                                  mod_type));
1188
                        jffs_unlink_node_from_version_list(f, cur);
1189
                        jffs_fmfree(f->c->fmc, cur->fm, cur);
1190
                        kfree(cur);
1191
                        DJM(no_jffs_node--);
1192
                }
1193
                else {
1194
                        node_with_name_later |= (mod_type & JFFS_MODIFY_NAME);
1195
                }
1196
        }
1197
 
1198
        return 0;
1199
}
1200
 
1201
 
1202
/* Insert a file into the hash table.  */
1203
int
1204
jffs_insert_file_into_hash(struct jffs_file *f)
1205
{
1206
        int i = f->ino % f->c->hash_len;
1207
 
1208
        D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f->ino));
1209
 
1210
        f->hash_next = f->c->hash[i];
1211
        if (f->hash_next) {
1212
                f->hash_next->hash_prev = f;
1213
        }
1214
        f->hash_prev = 0;
1215
        f->c->hash[i] = f;
1216
        return 0;
1217
}
1218
 
1219
 
1220
/* Insert a file into the file system tree.  */
1221
int
1222
jffs_insert_file_into_tree(struct jffs_file *f)
1223
{
1224
        struct jffs_file *parent;
1225
 
1226
        D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
1227
                  (f->name ? f->name : "")));
1228
 
1229
        if (!(parent = jffs_find_file(f->c, f->pino))) {
1230
                if (f->pino == 0) {
1231
                        f->c->root = f;
1232
                        f->parent = 0;
1233
                        f->sibling_prev = 0;
1234
                        f->sibling_next = 0;
1235
                        return 0;
1236
                }
1237
                else {
1238
                        D1(printk("jffs_insert_file_into_tree(): Found "
1239
                                  "inode with no parent and pino == %u\n",
1240
                                  f->pino));
1241
                        return -1;
1242
                }
1243
        }
1244
        f->parent = parent;
1245
        f->sibling_next = parent->children;
1246
        if (f->sibling_next) {
1247
                f->sibling_next->sibling_prev = f;
1248
        }
1249
        f->sibling_prev = 0;
1250
        parent->children = f;
1251
        return 0;
1252
}
1253
 
1254
 
1255
/* Remove a file from the hash table.  */
1256
int
1257
jffs_unlink_file_from_hash(struct jffs_file *f)
1258
{
1259
        D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
1260
                  "ino %u\n", f, f->ino));
1261
 
1262
        if (f->hash_next) {
1263
                f->hash_next->hash_prev = f->hash_prev;
1264
        }
1265
        if (f->hash_prev) {
1266
                f->hash_prev->hash_next = f->hash_next;
1267
        }
1268
        else {
1269
                f->c->hash[f->ino % f->c->hash_len] = f->hash_next;
1270
        }
1271
        return 0;
1272
}
1273
 
1274
 
1275
/* Just remove the file from the parent's children.  Don't free
1276
   any memory.  */
1277
int
1278
jffs_unlink_file_from_tree(struct jffs_file *f)
1279
{
1280
        D3(printk("jffs_unlink_file_from_tree(): ino: %d, name: "
1281
                  "\"%s\"\n", f->ino, (f->name ? f->name : "")));
1282
 
1283
        if (f->sibling_prev) {
1284
                f->sibling_prev->sibling_next = f->sibling_next;
1285
        }
1286
        else {
1287
                f->parent->children = f->sibling_next;
1288
        }
1289
        if (f->sibling_next) {
1290
                f->sibling_next->sibling_prev = f->sibling_prev;
1291
        }
1292
        return 0;
1293
}
1294
 
1295
 
1296
/* Find a file with its inode number.  */
1297
struct jffs_file *
1298
jffs_find_file(struct jffs_control *c, __u32 ino)
1299
{
1300
        struct jffs_file *f;
1301
        int i = ino % c->hash_len;
1302
 
1303
        D3(printk("jffs_find_file(): ino: %u\n", ino));
1304
 
1305
        for (f = c->hash[i]; f && (ino != f->ino); f = f->hash_next);
1306
 
1307
        D3(if (f) {
1308
                printk("jffs_find_file(): Found file with ino "
1309
                       "%u. (name: \"%s\")\n",
1310
                       ino, (f->name ? f->name : ""));
1311
        }
1312
        else {
1313
                printk("jffs_find_file(): Didn't find file "
1314
                         "with ino %u.\n", ino);
1315
        });
1316
 
1317
        return f;
1318
}
1319
 
1320
 
1321
/* Find a file in a directory.  We are comparing the names.  */
1322
struct jffs_file *
1323
jffs_find_child(struct jffs_file *dir, const char *name, int len)
1324
{
1325
        struct jffs_file *f;
1326
 
1327
        D3(printk("jffs_find_child()\n"));
1328
 
1329
        for (f = dir->children; f; f = f->sibling_next) {
1330
                if (f->name
1331
                    && !strncmp(f->name, name, len)
1332
                    && f->name[len] == '\0') {
1333
                        break;
1334
                }
1335
        }
1336
 
1337
        D3(if (f) {
1338
                printk("jffs_find_child(): Found \"%s\".\n", f->name);
1339
        }
1340
        else {
1341
                char *copy = (char *) kmalloc(len + 1, GFP_KERNEL);
1342
                if (copy) {
1343
                        memcpy(copy, name, len);
1344
                        copy[len] = '\0';
1345
                }
1346
                printk("jffs_find_child(): Didn't find the file \"%s\".\n",
1347
                       (copy ? copy : ""));
1348
                if (copy) {
1349
                        kfree(copy);
1350
                }
1351
        });
1352
 
1353
        return f;
1354
}
1355
 
1356
 
1357
#if !defined(JFFS_FLASH_SHORTCUT) || ! JFFS_FLASH_SHORTCUT
1358
 
1359
struct buffer_head *
1360
jffs_get_write_buffer(kdev_t dev, int block)
1361
{
1362
        struct buffer_head *bh;
1363
 
1364
        D3(printk("jffs_get_write_buffer(): block = %u\n", block));
1365
        if (!(bh = bread(dev, block, BLOCK_SIZE))) {
1366
                D(printk("jffs_get_write_buffer(): bread() failed. "
1367
                         "(block == %u)\n", block));
1368
        }
1369
 
1370
        D3(printk("jffs_get_write_buffer(): bh = 0x%08x\n", bh));
1371
        return bh;
1372
}
1373
 
1374
void
1375
jffs_put_write_buffer(struct buffer_head *bh)
1376
{
1377
        D3(printk("jffs_put_write_buffer(): bh = 0x%08x\n", bh));
1378
        mark_buffer_dirty(bh, 1);
1379
        ll_rw_block(WRITE, 1, &bh);
1380
        wait_on_buffer(bh);
1381
        brelse(bh);
1382
}
1383
 
1384
 
1385
/* Structure used by jffs_write_chunk() and jffs_write_node().  */
1386
struct jffs_write_task
1387
{
1388
        struct buffer_head *bh;
1389
        __u32 block;
1390
        __u32 block_offset;
1391
};
1392
 
1393
 
1394
/* Write a chunk of data to the flash memory.  This is a helper routine
1395
   to jffs_write_node().  */
1396
int
1397
jffs_write_chunk(struct jffs_control *c, struct jffs_write_task *wt,
1398
                 const unsigned char *data, __u32 size)
1399
{
1400
        int write_len = 0;
1401
        int len;
1402
        int buf_pos = 0;
1403
 
1404
        D3(printk("jffs_write_chunk(): size = %u\n", size));
1405
 
1406
        ASSERT(if (!wt) {
1407
                printk("jffs_write_chunk(): wt == NULL\n");
1408
                return -1;
1409
        });
1410
 
1411
        if (size == 0) {
1412
                return 0;
1413
        }
1414
 
1415
        if (wt->block_offset == BLOCK_SIZE) {
1416
                if (wt->bh) {
1417
                        jffs_put_write_buffer(wt->bh);
1418
                        wt->bh = 0;
1419
                }
1420
                wt->block++;
1421
                wt->block_offset = 0;
1422
        }
1423
 
1424
        if (!wt->bh
1425
            && !(wt->bh = jffs_get_write_buffer(c->sb->s_dev, wt->block))) {
1426
                return -1;
1427
        }
1428
 
1429
        while (write_len < size) {
1430
                len = jffs_min(size - write_len,
1431
                               BLOCK_SIZE - wt->block_offset);
1432
                memcpy(&wt->bh->b_data[wt->block_offset],
1433
                       &data[buf_pos], len);
1434
                write_len += len;
1435
                wt->block_offset += len;
1436
                D3(printk("  write_len: %u\n", write_len));
1437
                D3(printk("  len: %u\n", len));
1438
                D3(printk("  size: %u\n", size));
1439
                if (write_len < size) {
1440
                        jffs_put_write_buffer(wt->bh);
1441
                        wt->block++;
1442
                        wt->block_offset = 0;
1443
                        wt->bh = 0;
1444
                        if (!(wt->bh = jffs_get_write_buffer(c->sb->s_dev,
1445
                                                             wt->block))) {
1446
                                return write_len;
1447
                        }
1448
                        buf_pos += len;
1449
                }
1450
        }
1451
 
1452
        return write_len;
1453
}
1454
#endif
1455
 
1456
 
1457
#if defined(JFFS_FLASH_SHORTCUT) && JFFS_FLASH_SHORTCUT
1458
 
1459
/* Write a raw inode that takes up a certain amount of space in the flash
1460
   memory.  At the end of the flash device, there is often space that is
1461
   impossible to use.  At these times we want to mark this space as not
1462
   used.  In the cases when the amount of space is greater or equal than
1463
   a struct jffs_raw_inode, we write a "dummy node" that takes up this
1464
   space.  The space after the raw inode, if it exists, is left as it is.
1465
   Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
1466
   we can compute the checksum of it; we don't have to manipulate it any
1467
   further.
1468
 
1469
   If the space left on the device is less than the size of a struct
1470
   jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
1471
   No raw inode is written this time.  */
1472
static int
1473
jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
1474
{
1475
        struct jffs_fmcontrol *fmc = c->fmc;
1476
        int err;
1477
 
1478
        D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
1479
                  "dirty_fm->size = %u\n",
1480
                  dirty_fm->offset, dirty_fm->size));
1481
 
1482
        if (dirty_fm->size >= sizeof(struct jffs_raw_inode)) {
1483
                struct jffs_raw_inode raw_inode;
1484
                memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
1485
                raw_inode.magic = JFFS_MAGIC_BITMASK;
1486
                raw_inode.dsize = dirty_fm->size
1487
                                  - sizeof(struct jffs_raw_inode);
1488
                raw_inode.dchksum = raw_inode.dsize * 0xff;
1489
                raw_inode.chksum
1490
                = jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
1491
 
1492
                if ((err = flash_safe_write(fmc->flash_part,
1493
                                            (unsigned char *)dirty_fm->offset,
1494
                                            (unsigned char *)&raw_inode,
1495
                                            sizeof(struct jffs_raw_inode)))
1496
                    < 0) {
1497
                        return err;
1498
                }
1499
        }
1500
        else {
1501
                flash_safe_acquire(fmc->flash_part);
1502
                flash_memset((unsigned char *) dirty_fm->offset, 0,
1503
                             dirty_fm->size);
1504
                flash_safe_release(fmc->flash_part);
1505
        }
1506
 
1507
        D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1508
        return 0;
1509
}
1510
 
1511
#else
1512
 
1513
 
1514
/* Write a raw inode that takes up a certain amount of space in the flash
1515
   memory.  */
1516
static int
1517
jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
1518
{
1519
        struct jffs_raw_inode raw_inode;
1520
        struct buffer_head *bh;
1521
        __u32 block = dirty_fm->offset / BLOCK_SIZE;
1522
        __u32 block_offset = dirty_fm->offset - block * BLOCK_SIZE;
1523
        kdev_t dev = c->sb->s_dev;
1524
 
1525
        D1(printk("jffs_write_dummy_node(): dirty_fm->offset = %u, "
1526
                  "dirty_fm->size = %u\n",
1527
                  dirty_fm->offset, dirty_fm->size));
1528
 
1529
        if (!(bh = jffs_get_write_buffer(dev, block))) {
1530
                D(printk("jffs_write_dummy_node(): "
1531
                         "Failed to read block.\n"));
1532
                return -1;
1533
        }
1534
 
1535
        memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
1536
        raw_inode.magic = JFFS_MAGIC_BITMASK;
1537
        raw_inode.dsize = dirty_fm->size - sizeof(struct jffs_raw_inode);
1538
        raw_inode.chksum = jffs_checksum(&raw_inode,
1539
                                         sizeof(struct jffs_raw_inode))
1540
                           + raw_inode.dsize * 0xff;
1541
 
1542
        if (BLOCK_SIZE - block_offset < sizeof(struct jffs_raw_inode)) {
1543
                __u32 write_size = BLOCK_SIZE - block_offset;
1544
                memcpy(&bh->b_data[block_offset], &raw_inode, write_size);
1545
                jffs_put_write_buffer(bh);
1546
                bh = jffs_get_write_buffer(dev, ++block);
1547
                memcpy(bh->b_data, (void *)&raw_inode + write_size,
1548
                       sizeof(struct jffs_raw_inode) - write_size);
1549
        }
1550
        else {
1551
                memcpy(&bh->b_data[block_offset], &raw_inode,
1552
                       sizeof(struct jffs_raw_inode));
1553
        }
1554
 
1555
        jffs_put_write_buffer(bh);
1556
        D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1557
        return 0;
1558
}
1559
 
1560
#endif
1561
 
1562
 
1563
#if defined(JFFS_FLASH_SHORTCUT) && JFFS_FLASH_SHORTCUT
1564
 
1565
/* Write a raw inode, possibly its name and possibly some data.  */
1566
int
1567
jffs_write_node(struct jffs_control *c, struct jffs_node *node,
1568
                struct jffs_raw_inode *raw_inode,
1569
                const char *name, const unsigned char *data)
1570
{
1571
        struct jffs_fmcontrol *fmc = c->fmc;
1572
        struct jffs_fm *fm;
1573
        unsigned char *pos;
1574
        int err;
1575
        __u32 total_name_size = raw_inode->nsize
1576
                                + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1577
        __u32 total_data_size = raw_inode->dsize
1578
                                + JFFS_GET_PAD_BYTES(raw_inode->dsize);
1579
        __u32 total_size = sizeof(struct jffs_raw_inode)
1580
                           + total_name_size + total_data_size;
1581
 
1582
        /* Fire the retrorockets and shoot the fruiton torpedoes, sir!  */
1583
 
1584
        ASSERT(if (!node) {
1585
                printk("jffs_write_node(): node == NULL\n");
1586
                return -EINVAL;
1587
        });
1588
        ASSERT(if (raw_inode && raw_inode->nsize && !name) {
1589
                printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
1590
                       raw_inode->nsize);
1591
                return -EINVAL;
1592
        });
1593
 
1594
        D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
1595
                  "version = %u, total_size = %u\n",
1596
                  (name ? name : ""), raw_inode->ino,
1597
                  raw_inode->version, total_size));
1598
 
1599
        /* First try to allocate some flash memory.  */
1600
        if ((err = jffs_fmalloc(fmc, total_size, node, &fm)) < 0) {
1601
                D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1602
                         "failed!\n", fmc, total_size));
1603
                return err;
1604
        }
1605
        else if (!fm->nodes) {
1606
                /* The jffs_fm struct that we got is not good enough.
1607
                   Make that space dirty.  */
1608
                if ((err = jffs_write_dummy_node(c, fm)) < 0) {
1609
                        D(printk("jffs_write_node(): "
1610
                                 "jffs_write_dummy_node(): Failed!\n"));
1611
                        kfree(fm);
1612
                        DJM(no_jffs_fm--);
1613
                        return err;
1614
                }
1615
                /* Get a new one.  */
1616
                if ((err = jffs_fmalloc(fmc, total_size, node, &fm)) < 0) {
1617
                        D(printk("jffs_write_node(): Second "
1618
                                 "jffs_fmalloc(0x%p, %u) failed!\n",
1619
                                 fmc, total_size));
1620
                        return err;
1621
                }
1622
        }
1623
        node->fm = fm;
1624
 
1625
        ASSERT(if (fm->nodes == 0) {
1626
                printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
1627
        });
1628
 
1629
        pos = (unsigned char *) node->fm->offset;
1630
 
1631
        /* Compute the checksum for the data and name chunks.  */
1632
        raw_inode->dchksum = jffs_checksum(data, raw_inode->dsize);
1633
        raw_inode->nchksum = jffs_checksum(name, raw_inode->nsize);
1634
 
1635
        /* The checksum is calculated without the chksum and accurate
1636
           fields so set them to zero first.  */
1637
        raw_inode->accurate = 0;
1638
        raw_inode->chksum = 0;
1639
        raw_inode->chksum = jffs_checksum(raw_inode,
1640
                                          sizeof(struct jffs_raw_inode));
1641
        raw_inode->accurate = 0xff;
1642
 
1643
        D3(printk("jffs_write_node(): About to write this raw inode to the "
1644
                  "flash at pos 0x%p:\n", pos));
1645
        D3(jffs_print_raw_inode(raw_inode));
1646
 
1647
        /* Step 1: Write the raw jffs inode to the flash.  */
1648
        if ((err = flash_safe_write(fmc->flash_part, pos,
1649
                                    (unsigned char *)raw_inode,
1650
                                    sizeof(struct jffs_raw_inode))) < 0) {
1651
                jffs_fmfree_partly(fmc, fm,
1652
                                   total_name_size + total_data_size);
1653
                D1(printk("jffs_write_node(): Failed to write raw_inode.\n"));
1654
                return err;
1655
        }
1656
        pos += sizeof(struct jffs_raw_inode);
1657
 
1658
        /* Step 2: Write the name, if there is any.  */
1659
        if (raw_inode->nsize) {
1660
                if ((err = flash_safe_write(fmc->flash_part, pos,
1661
                                            (unsigned char *)name,
1662
                                            raw_inode->nsize)) < 0) {
1663
                        jffs_fmfree_partly(fmc, fm, total_data_size);
1664
                        D1(printk("jffs_write_node(): Failed to write "
1665
                                  "the name.\n"));
1666
                        return err;
1667
                }
1668
                pos += total_name_size;
1669
        }
1670
 
1671
        /* Step 3: Append the actual data, if any.  */
1672
        if (raw_inode->dsize) {
1673
                if ((err = flash_safe_write(fmc->flash_part, pos, data,
1674
                                            raw_inode->dsize)) < 0) {
1675
                        jffs_fmfree_partly(fmc, fm, 0);
1676
                        D1(printk("jffs_write_node(): Failed to write "
1677
                                  "the data.\n"));
1678
                        return err;
1679
                }
1680
        }
1681
 
1682
        D3(printk("jffs_write_node(): Leaving...\n"));
1683
        return raw_inode->dsize;
1684
} /* jffs_write_node()  */
1685
 
1686
#else
1687
 
1688
/* Write a raw inode, possibly its name and possibly some data.  */
1689
int
1690
jffs_write_node(struct jffs_control *c, struct jffs_node *node,
1691
                struct jffs_raw_inode *raw_inode,
1692
                const char *name, const unsigned char *buf)
1693
{
1694
        struct jffs_write_task wt;
1695
        struct jffs_fm *fm;
1696
        int err;
1697
        __u32 total_size = sizeof(struct jffs_raw_inode)
1698
                           + raw_inode->nsize
1699
                           + JFFS_GET_PAD_BYTES(raw_inode->nsize)
1700
                           + raw_inode->dsize
1701
                           + JFFS_GET_PAD_BYTES(raw_inode->dsize);
1702
 
1703
        /* fire the retrorockets and shoot the fruiton torpedoes, sir! */
1704
 
1705
        D1(printk("jffs_write_node(): ino: %u\n", raw_inode->ino));
1706
 
1707
        ASSERT(if (!node) {
1708
                printk("jffs_write_node(): node == NULL\n");
1709
                return -1;
1710
        });
1711
 
1712
        /* First try to allocate some flash memory.  */
1713
        if ((err = jffs_fmalloc(c->fmc, total_size, node, &fm)) < 0 ) {
1714
                D(printk("jffs_write_node(): jffs_fmalloc(0x%08x, %u) "
1715
                         "failed!\n", c->fmc, total_size));
1716
                return err;
1717
        }
1718
        else if (!fm->nodes) {
1719
                /* The jffs_fm struct that we got is not good enough.  */
1720
                if (jffs_write_dummy_node(c, fm) < 0) {
1721
                        D(printk("jffs_write_node(): "
1722
                                 "jffs_write_dummy_node(): Failed!\n"));
1723
                        kfree(fm);
1724
                        DJM(no_jffs_fm--);
1725
                        return -1;
1726
                }
1727
                /* Get a new one.  */
1728
                if ((err = jffs_fmalloc(c->fmc, total_size, node)) < 0) {
1729
                        D(printk("jffs_write_node(): Second "
1730
                                 "jffs_fmalloc(0x%08x, %u) failed!\n",
1731
                                 c->fmc, total_size));
1732
                        return err;
1733
                }
1734
        }
1735
        node->fm = fm;
1736
 
1737
        ASSERT(if (fm->nodes == 0) {
1738
                printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
1739
        });
1740
 
1741
        wt.bh = 0;
1742
        wt.block = node->fm->offset / BLOCK_SIZE;
1743
        wt.block_offset = node->fm->offset % BLOCK_SIZE;
1744
 
1745
        /* Calculate the checksum for this jffs_raw_node and its name
1746
           and data.  The checksum is performed without the chksum and
1747
           accurate fields so set them to zero first.  */
1748
        raw_inode->accurate = 0;
1749
        raw_inode->chksum = 0;
1750
        raw_inode->chksum = jffs_checksum(raw_inode,
1751
                                          sizeof(struct jffs_raw_inode));
1752
        raw_inode->accurate = 0xff;
1753
        if (raw_inode->nsize) {
1754
                raw_inode->chksum += jffs_checksum(name, raw_inode->nsize);
1755
        }
1756
        if (raw_inode->dsize) {
1757
                raw_inode->chksum += jffs_checksum((void *)buf,
1758
                                                   raw_inode->dsize);
1759
        }
1760
 
1761
        /* Step 1: Write the raw jffs inode to the flash.  */
1762
        if (jffs_write_chunk(c, &wt, (unsigned char *)raw_inode,
1763
                                  sizeof(struct jffs_raw_inode))
1764
            < sizeof(struct jffs_raw_inode)) {
1765
                return -1;
1766
        }
1767
 
1768
        /* Step 2: Write the name, if there is any.  */
1769
        if (raw_inode->nsize && name) {
1770
                if (jffs_write_chunk(c, &wt, name, raw_inode->nsize)
1771
                    < raw_inode->nsize) {
1772
                        return -1;
1773
                }
1774
                /* XXX: Hack! (I'm so lazy.)  */
1775
                if (JFFS_GET_PAD_BYTES(wt.block_offset)) {
1776
                        __u32 ff = 0xffffffff
1777
                        jffs_write_chunk(c, &wt, (unsigned char *)&ff,
1778
                                         JFFS_GET_PAD_BYTES(wt.block_offset));
1779
                }
1780
        }
1781
 
1782
        /* Step 3: Append the actual data, if any.  */
1783
        if (raw_inode->dsize && buf) {
1784
                if (jffs_write_chunk(c, &wt, buf, raw_inode->dsize)
1785
                    < raw_inode->dsize) {
1786
                        return -1;
1787
                }
1788
        }
1789
 
1790
        if (wt.bh) {
1791
                D3(printk("jffs_write_node(): wt.bh != NULL, Final write.\n"));
1792
                jffs_put_write_buffer(wt.bh);
1793
        }
1794
 
1795
        D3(printk("jffs_write_node(): Leaving...\n"));
1796
        return raw_inode->dsize;
1797
}
1798
 
1799
#endif
1800
 
1801
 
1802
#if defined(JFFS_FLASH_SHORTCUT) && JFFS_FLASH_SHORTCUT
1803
 
1804
/* Read data from the node and write it to the buffer.  'node_offset'
1805
   is how much we have read from this particular node before and which
1806
   shouldn't be read again.  'max_size' is how much space there is in
1807
   the buffer.  */
1808
static int
1809
jffs_get_node_data(struct jffs_file *f, struct jffs_node *node, char *buf,
1810
                   __u32 node_offset, __u32 max_size, kdev_t dev)
1811
{
1812
        struct jffs_fmcontrol *fmc = f->c->fmc;
1813
        __u32 pos = node->fm->offset + node->fm_offset + node_offset;
1814
        __u32 avail = node->data_size - node_offset;
1815
        __u32 r;
1816
 
1817
        D2(printk("  jffs_get_node_data(): file: \"%s\", ino: %u, "
1818
                  "version: %u, node_offset: %u\n",
1819
                  f->name, node->ino, node->version, node_offset));
1820
 
1821
        r = jffs_min(avail, max_size);
1822
        flash_safe_read(fmc->flash_part, (unsigned char *) pos,
1823
                        (unsigned char *)buf, r);
1824
 
1825
        D3(printk("  jffs_get_node_data(): Read %u byte%s.\n",
1826
                  r, (r == 1 ? "" : "s")));
1827
 
1828
        return r;
1829
}
1830
 
1831
#else
1832
 
1833
/* Read data from the node and write it to the buffer.  'node_offset'
1834
   is how much we have read from this particular node before and which
1835
   shouldn't be read again.  'max_size' is how much space there is in
1836
   the buffer.  */
1837
static int
1838
jffs_get_node_data(struct jffs_file *f, struct jffs_node *node,
1839
                   char *buf, __u32 node_offset,
1840
                   __u32 max_size, kdev_t dev)
1841
{
1842
        struct buffer_head *bh;
1843
        __u32 first = node->fm->offset + node->fm_offset + node_offset;
1844
        __u32 block = first / BLOCK_SIZE;
1845
        __u32 block_offset = first - block * BLOCK_SIZE;
1846
        __u32 read_len;
1847
        __u32 total_read = 0;
1848
        __u32 avail = node->data_size - node_offset;
1849
 
1850
        D2(printk("jffs_get_node_data(): file: \"%s\", ino: %lu, "
1851
                  "version: %lu, node_offset: %lu\n",
1852
                  f->name, node->ino, node->version, node_offset));
1853
 
1854
        while ((total_read < max_size) && (avail > 0)) {
1855
                read_len = jffs_min(avail, BLOCK_SIZE - block_offset);
1856
                read_len = jffs_min(read_len, max_size - total_read);
1857
                if (!(bh = bread(dev, block, BLOCK_SIZE))) {
1858
                        D(printk("jffs_get_node_data(): bread() failed. "
1859
                                 "(block == %u)\n", block));
1860
                        return -EIO;
1861
                }
1862
                memcpy(&buf[total_read], &bh->b_data[block_offset], read_len);
1863
                brelse(bh);
1864
                block++;
1865
                avail -= read_len;
1866
                total_read += read_len;
1867
                block_offset = 0;
1868
        }
1869
 
1870
        return total_read;
1871
}
1872
 
1873
#endif
1874
 
1875
 
1876
/* Read data from the file's nodes.  Write the data to the buffer
1877
   'buf'.  'read_offset' tells how much data we should skip.  */
1878
int
1879
jffs_read_data(struct jffs_file *f, char *buf, __u32 read_offset, __u32 size)
1880
{
1881
        struct jffs_node *node;
1882
        __u32 read_data = 0; /* Total amount of read data.  */
1883
        __u32 node_offset = 0;
1884
        __u32 pos = 0; /* Number of bytes traversed.  */
1885
 
1886
        D1(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
1887
                  "size = %u\n",
1888
                  (f->name ? f->name : ""), read_offset, size));
1889
 
1890
        if (read_offset >= f->size) {
1891
                D(printk("  f->size: %d\n", f->size));
1892
                return 0;
1893
        }
1894
 
1895
        /* First find the node to read data from.  */
1896
        node = f->range_head;
1897
        while (pos <= read_offset) {
1898
                node_offset = read_offset - pos;
1899
                if (node_offset >= node->data_size) {
1900
                        pos += node->data_size;
1901
                        node = node->range_next;
1902
                }
1903
                else {
1904
                        break;
1905
                }
1906
        }
1907
 
1908
        /* "Cats are living proof that not everything in nature
1909
           has to be useful."
1910
           - Garrison Keilor ('97)  */
1911
 
1912
        /* Fill the buffer.  */
1913
        while (node && (read_data < size)) {
1914
                int r;
1915
                if (!node->fm) {
1916
                        /* This node does not refer to real data.  */
1917
                        r = jffs_min(size - read_data,
1918
                                     node->data_size - node_offset);
1919
                        memset(&buf[read_data], 0, r);
1920
                }
1921
                else if ((r = jffs_get_node_data(f, node, &buf[read_data],
1922
                                                 node_offset,
1923
                                                 size - read_data,
1924
                                                 f->c->sb->s_dev)) < 0) {
1925
                        return r;
1926
                }
1927
                read_data += r;
1928
                node_offset = 0;
1929
                node = node->range_next;
1930
        }
1931
        D3(printk("  jffs_read_data(): Read %u bytes.\n", read_data));
1932
        return read_data;
1933
}
1934
 
1935
 
1936
/* Used for traversing all nodes in the hash table.  */
1937
int
1938
jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *))
1939
{
1940
        struct jffs_file *f;
1941
        struct jffs_file *next_f;
1942
        int pos;
1943
        int r;
1944
        int result = 0;
1945
 
1946
        for (pos = 0; pos < c->hash_len; pos++) {
1947
                for (f = c->hash[pos]; f; f = next_f) {
1948
                        /* We need a reference to the next file in the
1949
                           list because `func' might remove the current
1950
                           file `f'.  */
1951
                        next_f = f->hash_next;
1952
                        if ((r = func(f)) < 0) {
1953
                                return r;
1954
                        }
1955
                        result += r;
1956
                }
1957
        }
1958
 
1959
        return result;
1960
}
1961
 
1962
 
1963
/* Free all memory associated with a file.  */
1964
int
1965
jffs_free_node_list(struct jffs_file *f)
1966
{
1967
        struct jffs_node *node;
1968
        struct jffs_node *p;
1969
 
1970
        D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
1971
                  f->ino, (f->name ? f->name : "")));
1972
        node = f->version_head;
1973
        while (node) {
1974
                p = node;
1975
                node = node->version_next;
1976
                kfree(p);
1977
                DJM(no_jffs_node--);
1978
        }
1979
        return 0;
1980
}
1981
 
1982
 
1983
/* See if a file is deleted. If so, mark that file's nodes as obsolete.  */
1984
int
1985
jffs_possibly_delete_file(struct jffs_file *f)
1986
{
1987
        struct jffs_node *n;
1988
 
1989
        D3(printk("jffs_possibly_delete_file(): ino: %u\n",
1990
                  f->ino));
1991
 
1992
        ASSERT(if (!f) {
1993
                printk(KERN_ERR "jffs_possibly_delete_file(): f == NULL\n");
1994
                return -1;
1995
        });
1996
 
1997
        if (f->deleted) {
1998
                /* First try to remove all older versions.  */
1999
                for (n = f->version_head; n; n = n->version_next) {
2000
                        if (!n->fm) {
2001
                                continue;
2002
                        }
2003
                        if (jffs_fmfree(f->c->fmc, n->fm, n) < 0) {
2004
                                break;
2005
                        }
2006
                }
2007
                /* Unlink the file from the filesystem.  */
2008
                jffs_unlink_file_from_tree(f);
2009
                jffs_unlink_file_from_hash(f);
2010
                jffs_free_node_list(f);
2011
                if (f->name) {
2012
                        kfree(f->name);
2013
                        DJM(no_name--);
2014
                }
2015
                kfree(f);
2016
                DJM(no_jffs_file--);
2017
        }
2018
        return 0;
2019
}
2020
 
2021
 
2022
/* Used in conjunction with jffs_foreach_file() to count the number
2023
   of files in the file system.  */
2024
int
2025
jffs_file_count(struct jffs_file *f)
2026
{
2027
        return 1;
2028
}
2029
 
2030
 
2031
/* Build up a file's range list from scratch by going through the
2032
   version list.  */
2033
int
2034
jffs_build_file(struct jffs_file *f)
2035
{
2036
        struct jffs_node *n;
2037
 
2038
        D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
2039
                  f->ino, (f->name ? f->name : "")));
2040
 
2041
        for (n = f->version_head; n; n = n->version_next) {
2042
                jffs_update_file(f, n);
2043
        }
2044
        return 0;
2045
}
2046
 
2047
 
2048
/* Remove an amount of data from a file. If this amount of data is
2049
   zero, that could mean that a node should be split in two parts.
2050
   We remove or change the appropriate nodes in the lists.
2051
 
2052
   Starting offset of area to be removed is node->data_offset,
2053
   and the length of the area is in node->removed_size.   */
2054
static void
2055
jffs_delete_data(struct jffs_file *f, struct jffs_node *node)
2056
{
2057
        struct jffs_node *n;
2058
        __u32 offset = node->data_offset;
2059
        __u32 remove_size = node->removed_size;
2060
 
2061
        D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
2062
                  offset, remove_size));
2063
 
2064
        if (remove_size == 0
2065
            && f->range_tail
2066
            && f->range_tail->data_offset + f->range_tail->data_size
2067
               == offset) {
2068
                /* A simple append; nothing to remove or no node to split.  */
2069
                return;
2070
        }
2071
 
2072
        /* Find the node where we should begin the removal.  */
2073
        for (n = f->range_head; n; n = n->range_next) {
2074
                if (n->data_offset + n->data_size > offset) {
2075
                        break;
2076
                }
2077
        }
2078
        if (!n) {
2079
                /* If there's no data in the file there's no data to
2080
                   remove either.  */
2081
                return;
2082
        }
2083
 
2084
        if (n->data_offset > offset) {
2085
                /* XXX: Not implemented yet.  */
2086
                printk(KERN_WARNING "JFFS: An unexpected situation "
2087
                       "occurred in jffs_delete_data.\n");
2088
        }
2089
        else if (n->data_offset < offset) {
2090
                /* See if the node has to be split into two parts.  */
2091
                if (n->data_offset + n->data_size < offset + remove_size) {
2092
                        /* Do the split.  */
2093
                        struct jffs_node *new_node;
2094
                        D3(printk("jffs_delete_data(): Split node with "
2095
                                  "version number %u.\n", n->version));
2096
 
2097
                        if (!(new_node = (struct jffs_node *)
2098
                                         kmalloc(sizeof(struct jffs_node),
2099
                                                 GFP_KERNEL))) {
2100
                                D(printk("jffs_delete_data(): -ENOMEM\n"));
2101
                                return;
2102
                        }
2103
                        DJM(no_jffs_node++);
2104
 
2105
                        new_node->ino = n->ino;
2106
                        new_node->version = n->version;
2107
                        new_node->data_offset = offset;
2108
                        new_node->data_size = n->data_size
2109
                                              - (remove_size
2110
                                                 + (offset - n->data_offset));
2111
                        new_node->fm_offset = n->fm_offset + n->data_size
2112
                                              + remove_size;
2113
                        new_node->name_size = n->name_size;
2114
                        new_node->fm = n->fm;
2115
                        new_node->version_prev = n;
2116
                        new_node->version_next = n->version_next;
2117
                        if (new_node->version_next) {
2118
                                new_node->version_next->version_prev
2119
                                = new_node;
2120
                        }
2121
                        else {
2122
                                f->version_tail = new_node;
2123
                        }
2124
                        n->version_next = new_node;
2125
                        new_node->range_prev = n;
2126
                        new_node->range_next = n->range_next;
2127
                        if (new_node->range_next) {
2128
                                new_node->range_next->range_prev = new_node;
2129
                        }
2130
                        else {
2131
                                f->range_tail = new_node;
2132
                        }
2133
                        /* A very interesting can of worms.  */
2134
                        n->range_next = new_node;
2135
                        n->data_size = offset - n->data_offset;
2136
                        jffs_add_node(new_node);
2137
                        n = new_node->range_next;
2138
                        remove_size = 0;
2139
                }
2140
                else {
2141
                        /* No.  No need to split the node.  Just remove
2142
                           the end of the node.  */
2143
                        int r = jffs_min(n->data_offset + n->data_size
2144
                                         - offset, remove_size);
2145
                        n->data_size -= r;
2146
                        remove_size -= r;
2147
                        n = n->range_next;
2148
                }
2149
        }
2150
 
2151
        /* Remove as many nodes as necessary.  */
2152
        while (n && remove_size) {
2153
                if (n->data_size <= remove_size) {
2154
                        struct jffs_node *p = n;
2155
                        remove_size -= n->data_size;
2156
                        n = n->range_next;
2157
                        D3(printk("jffs_delete_data(): Removing node: "
2158
                                  "ino: %u, version: %u\n",
2159
                                  p->ino, p->version));
2160
                        if (p->fm) {
2161
                                jffs_fmfree(f->c->fmc, p->fm, p);
2162
                        }
2163
                        jffs_unlink_node_from_range_list(f, p);
2164
                        jffs_unlink_node_from_version_list(f, p);
2165
                        kfree(p);
2166
                        DJM(no_jffs_node--);
2167
                }
2168
                else {
2169
                        n->data_size -= remove_size;
2170
                        n->fm_offset += remove_size;
2171
                        n->data_offset -= (node->removed_size - remove_size);
2172
                        n = n->range_next;
2173
                        break;
2174
                }
2175
        }
2176
 
2177
        /* Adjust the following nodes' information about offsets etc.  */
2178
        while (n && node->removed_size) {
2179
                n->data_offset -= node->removed_size;
2180
                n = n->range_next;
2181
        }
2182
 
2183
        f->size -= node->removed_size;
2184
        D3(printk("jffs_delete_data(): f->size = %d\n", f->size));
2185
} /* jffs_delete_data()  */
2186
 
2187
 
2188
/* Insert some data into a file.  Prior to the call to this function,
2189
   jffs_delete_data() should be called.  */
2190
static void
2191
jffs_insert_data(struct jffs_file *f, struct jffs_node *node)
2192
{
2193
        D3(printk("jffs_insert_data(): node->data_offset = %u, "
2194
                  "node->data_size = %u, f->size = %u\n",
2195
                  node->data_offset, node->data_size, f->size));
2196
 
2197
        /* Find the position where we should insert data.  */
2198
 
2199
        if (node->data_offset == f->size) {
2200
                /* A simple append.  This is the most common operation.  */
2201
                node->range_next = 0;
2202
                node->range_prev = f->range_tail;
2203
                if (node->range_prev) {
2204
                        node->range_prev->range_next = node;
2205
                }
2206
                f->range_tail = node;
2207
                f->size += node->data_size;
2208
                if (!f->range_head) {
2209
                        f->range_head = node;
2210
                }
2211
        }
2212
        else if (node->data_offset < f->size) {
2213
                /* Trying to insert data into the middle of the file.  This
2214
                   means no problem because jffs_delete_data() has already
2215
                   prepared the range list for us.  */
2216
                struct jffs_node *n;
2217
 
2218
                /* Find the correct place for the insertion and then insert
2219
                   the node.  */
2220
                for (n = f->range_head; n; n = n->range_next) {
2221
                        D1(printk("Cool stuff's happening!\n"));
2222
 
2223
                        if (n->data_offset == node->data_offset) {
2224
                                node->range_prev = n->range_prev;
2225
                                if (node->range_prev) {
2226
                                        node->range_prev->range_next = node;
2227
                                }
2228
                                else {
2229
                                        f->range_head = node;
2230
                                }
2231
                                node->range_next = n;
2232
                                n->range_prev = node;
2233
                                break;
2234
                        }
2235
                        ASSERT(else if (n->data_offset + n->data_size >
2236
                                        node->data_offset) {
2237
                                printk(KERN_ERR "jffs_insert_data(): "
2238
                                       "Couldn't find a place to insert "
2239
                                       "the data!\n");
2240
                                return;
2241
                        });
2242
                }
2243
 
2244
                /* Adjust later nodes' offsets etc.  */
2245
                n = node->range_next;
2246
                while (n) {
2247
                        n->data_offset += node->data_size;
2248
                        n = n->range_next;
2249
                }
2250
                f->size += node->data_size;
2251
        }
2252
        else if (node->data_offset > f->size) {
2253
                /* Not implemented yet.  */
2254
#if 0
2255
                /* Below is some example code for future use if we decide
2256
                   to implement it.  */
2257
                /* This is code that isn't supported by VFS. So there aren't
2258
                   really any reasons to implement it yet.  */
2259
                if (!f->range_head) {
2260
                        if (node->data_offset > f->size) {
2261
                                if (!(nn = jffs_alloc_node())) {
2262
                                        D(printk("jffs_insert_data(): "
2263
                                                 "Allocation failed.\n"));
2264
                                        return;
2265
                                }
2266
                                nn->version = JFFS_MAGIC_BITMASK;
2267
                                nn->data_offset = 0;
2268
                                nn->data_size = node->data_offset;
2269
                                nn->removed_size = 0;
2270
                                nn->fm_offset = 0;
2271
                                nn->name_size = 0;
2272
                                nn->fm = 0; /* This is a virtual data holder.  */
2273
                                nn->version_prev = 0;
2274
                                nn->version_next = 0;
2275
                                nn->range_prev = 0;
2276
                                nn->range_next = 0;
2277
                                nh->range_head = nn;
2278
                                nh->range_tail = nn;
2279
                        }
2280
                }
2281
#endif
2282
        }
2283
 
2284
        D3(printk("jffs_insert_data(): f->size = %d\n", f->size));
2285
}
2286
 
2287
 
2288
/* A new node (with data) has been added to the file and now the range
2289
   list has to be modified.  */
2290
static int
2291
jffs_update_file(struct jffs_file *f, struct jffs_node *node)
2292
{
2293
        D3(printk("jffs_update_file(): ino: %u, version: %u\n",
2294
                  f->ino, node->version));
2295
 
2296
        if (node->data_size == 0) {
2297
                if (node->removed_size == 0) {
2298
                        /* data_offset == X  */
2299
                        /* data_size == 0  */
2300
                        /* remove_size == 0  */
2301
                }
2302
                else {
2303
                        /* data_offset == X  */
2304
                        /* data_size == 0  */
2305
                        /* remove_size != 0  */
2306
                        jffs_delete_data(f, node);
2307
                }
2308
        }
2309
        else {
2310
                /* data_offset == X  */
2311
                /* data_size != 0  */
2312
                /* remove_size == Y  */
2313
                jffs_delete_data(f, node);
2314
                jffs_insert_data(f, node);
2315
        }
2316
        return 0;
2317
}
2318
 
2319
 
2320
/* Print the contents of a node.  */
2321
void
2322
jffs_print_node(struct jffs_node *n)
2323
{
2324
        D(printk("jffs_node: 0x%p\n", n));
2325
        D(printk("{\n"));
2326
        D(printk("        0x%08x, /* version  */\n", n->version));
2327
        D(printk("        0x%08x, /* data_offset  */\n", n->data_offset));
2328
        D(printk("        0x%08x, /* data_size  */\n", n->data_size));
2329
        D(printk("        0x%08x, /* removed_size  */\n", n->removed_size));
2330
        D(printk("        0x%08x, /* fm_offset  */\n", n->fm_offset));
2331
        D(printk("        0x%02x,       /* name_size  */\n", n->name_size));
2332
        D(printk("        0x%p, /* fm,  fm->offset: %u  */\n",
2333
                 n->fm, n->fm->offset));
2334
        D(printk("        0x%p, /* version_prev  */\n", n->version_prev));
2335
        D(printk("        0x%p, /* version_next  */\n", n->version_next));
2336
        D(printk("        0x%p, /* range_prev  */\n", n->range_prev));
2337
        D(printk("        0x%p, /* range_next  */\n", n->range_next));
2338
        D(printk("}\n"));
2339
}
2340
 
2341
 
2342
/* Print the contents of a raw inode.  */
2343
void
2344
jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
2345
{
2346
        D(printk("jffs_raw_inode: inode number: %u\n", raw_inode->ino));
2347
        D(printk("{\n"));
2348
        D(printk("        0x%08x, /* magic  */\n", raw_inode->magic));
2349
        D(printk("        0x%08x, /* ino  */\n", raw_inode->ino));
2350
        D(printk("        0x%08x, /* pino  */\n", raw_inode->pino));
2351
        D(printk("        0x%08x, /* version  */\n", raw_inode->version));
2352
        D(printk("        0x%08x, /* mode  */\n", raw_inode->mode));
2353
        D(printk("        0x%04x,     /* uid  */\n", raw_inode->uid));
2354
        D(printk("        0x%04x,     /* gid  */\n", raw_inode->gid));
2355
        D(printk("        0x%08x, /* atime  */\n", raw_inode->atime));
2356
        D(printk("        0x%08x, /* mtime  */\n", raw_inode->mtime));
2357
        D(printk("        0x%08x, /* ctime  */\n", raw_inode->ctime));
2358
        D(printk("        0x%08x, /* offset  */\n", raw_inode->offset));
2359
        D(printk("        0x%08x, /* dsize  */\n", raw_inode->dsize));
2360
        D(printk("        0x%08x, /* rsize  */\n", raw_inode->rsize));
2361
        D(printk("        0x%02x,       /* nsize  */\n", raw_inode->nsize));
2362
        D(printk("        0x%02x,       /* nlink  */\n", raw_inode->nlink));
2363
        D(printk("        0x%02x,       /* spare  */\n",
2364
                 raw_inode->spare));
2365
        D(printk("        %u,          /* rename  */\n",
2366
                 raw_inode->rename));
2367
        D(printk("        %u,          /* deleted  */\n",
2368
                 raw_inode->deleted));
2369
        D(printk("        0x%02x,       /* accurate  */\n",
2370
                 raw_inode->accurate));
2371
        D(printk("        0x%08x, /* dchksum  */\n", raw_inode->dchksum));
2372
        D(printk("        0x%04x,     /* nchksum  */\n", raw_inode->nchksum));
2373
        D(printk("        0x%04x,     /* chksum  */\n", raw_inode->chksum));
2374
        D(printk("}\n"));
2375
}
2376
 
2377
 
2378
/* Print the contents of a file.  */
2379
int
2380
jffs_print_file(struct jffs_file *f)
2381
{
2382
        D(int i);
2383
        D(printk("jffs_file: 0x%p\n", f));
2384
        D(printk("{\n"));
2385
        D(printk("        0x%08x, /* ino  */\n", f->ino));
2386
        D(printk("        0x%08x, /* pino  */\n", f->pino));
2387
        D(printk("        0x%08x, /* mode  */\n", f->mode));
2388
        D(printk("        0x%04x,     /* uid  */\n", f->uid));
2389
        D(printk("        0x%04x,     /* gid  */\n", f->gid));
2390
        D(printk("        0x%08x, /* atime  */\n", f->atime));
2391
        D(printk("        0x%08x, /* mtime  */\n", f->mtime));
2392
        D(printk("        0x%08x, /* ctime  */\n", f->ctime));
2393
        D(printk("        0x%02x,       /* nsize  */\n", f->nsize));
2394
        D(printk("        0x%02x,       /* nlink  */\n", f->nlink));
2395
        D(printk("        0x%02x,       /* deleted  */\n", f->deleted));
2396
        D(printk("        \"%s\", ", (f->name ? f->name : "")));
2397
        D(for (i = strlen(f->name ? f->name : ""); i < 8; ++i) {
2398
                printk(" ");
2399
        });
2400
        D(printk("/* name  */\n"));
2401
        D(printk("        0x%08x, /* size  */\n", f->size));
2402
        D(printk("        0x%08x, /* highest_version  */\n",
2403
                 f->highest_version));
2404
        D(printk("        0x%p, /* c  */\n", f->c));
2405
        D(printk("        0x%p, /* parent  */\n", f->parent));
2406
        D(printk("        0x%p, /* children  */\n", f->children));
2407
        D(printk("        0x%p, /* sibling_prev  */\n", f->sibling_prev));
2408
        D(printk("        0x%p, /* sibling_next  */\n", f->sibling_next));
2409
        D(printk("        0x%p, /* hash_prev  */\n", f->hash_prev));
2410
        D(printk("        0x%p, /* hash_next  */\n", f->hash_next));
2411
        D(printk("        0x%p, /* range_head  */\n", f->range_head));
2412
        D(printk("        0x%p, /* range_tail  */\n", f->range_tail));
2413
        D(printk("        0x%p, /* version_head  */\n", f->version_head));
2414
        D(printk("        0x%p, /* version_tail  */\n", f->version_tail));
2415
        D(printk("}\n"));
2416
        return 0;
2417
}
2418
 
2419
 
2420
void
2421
jffs_print_hash_table(struct jffs_control *c)
2422
{
2423
        struct jffs_file *f;
2424
        int i;
2425
 
2426
        printk("JFFS: Dumping the file system's hash table...\n");
2427
        for (i = 0; i < c->hash_len; i++) {
2428
                for (f = c->hash[i]; f; f = f->hash_next) {
2429
                        printk("*** c->hash[%u]: \"%s\" "
2430
                               "(ino: %u, pino: %u)\n",
2431
                               i, (f->name ? f->name : ""),
2432
                               f->ino, f->pino);
2433
                }
2434
        }
2435
}
2436
 
2437
 
2438
void
2439
jffs_print_tree(struct jffs_file *first_file, int indent)
2440
{
2441
        struct jffs_file *f;
2442
        char *space;
2443
 
2444
        if (!first_file) {
2445
                return;
2446
        }
2447
 
2448
        if (!(space = (char *) kmalloc(indent + 1, GFP_KERNEL))) {
2449
                printk("jffs_print_tree(): Out of memory!\n");
2450
                return;
2451
        }
2452
 
2453
        memset(space, ' ', indent);
2454
        space[indent] = '\0';
2455
 
2456
        for (f = first_file; f; f = f->sibling_next) {
2457
                printk("%s%s (ino: %u, highest_version: %u, size: %u)\n",
2458
                       space, (f->name ? f->name : "/"),
2459
                       f->ino, f->highest_version, f->size);
2460
                if (S_ISDIR(f->mode)) {
2461
                        jffs_print_tree(f->children, indent + 2);
2462
                }
2463
        }
2464
 
2465
        kfree(space);
2466
}
2467
 
2468
 
2469
#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
2470
void
2471
jffs_print_memory_allocation_statistics(void)
2472
{
2473
        static long printout = 0;
2474
        printk("________ Memory printout #%ld ________\n", ++printout);
2475
        printk("no_jffs_file = %ld\n", no_jffs_file);
2476
        printk("no_jffs_node = %ld\n", no_jffs_node);
2477
        printk("no_jffs_control = %ld\n", no_jffs_control);
2478
        printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode);
2479
        printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref);
2480
        printk("no_jffs_fm = %ld\n", no_jffs_fm);
2481
        printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol);
2482
        printk("no_hash = %ld\n", no_hash);
2483
        printk("no_name = %ld\n", no_name);
2484
        printk("\n");
2485
}
2486
#endif
2487
 
2488
 
2489
#if defined(JFFS_FLASH_SHORTCUT) && JFFS_FLASH_SHORTCUT
2490
 
2491
/* Rewrite `size' bytes, and begin at `node'.  */
2492
int
2493
jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, int size)
2494
{
2495
        struct jffs_control *c = f->c;
2496
        struct jffs_fmcontrol *fmc = c->fmc;
2497
        struct jffs_raw_inode raw_inode;
2498
        struct jffs_node *new_node;
2499
        struct jffs_fm *fm;
2500
        unsigned char *pos;
2501
        unsigned char *pos_dchksum;
2502
        __u32 total_name_size;
2503
        __u32 total_data_size;
2504
        __u32 total_size;
2505
        int err;
2506
 
2507
        D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2508
                  f->ino, (f->name ? f->name : ""), size));
2509
 
2510
        /* Create and initialize the new node.  */
2511
        if (!(new_node = (struct jffs_node *)
2512
                         kmalloc(sizeof(struct jffs_node), GFP_KERNEL))) {
2513
                D(printk("jffs_rewrite_data(): "
2514
                         "Failed to allocate node.\n"));
2515
                return -ENOMEM;
2516
        }
2517
        DJM(no_jffs_node++);
2518
        new_node->data_offset = node->data_offset;
2519
        new_node->data_size = size;
2520
        new_node->removed_size = size;
2521
        total_name_size = f->nsize + JFFS_GET_PAD_BYTES(f->nsize);
2522
        total_data_size = size + JFFS_GET_PAD_BYTES(size);
2523
        total_size = sizeof(struct jffs_raw_inode)
2524
                     + total_name_size + total_data_size;
2525
        new_node->fm_offset = sizeof(struct jffs_raw_inode)
2526
                              + total_name_size;
2527
 
2528
        if ((err = jffs_fmalloc(fmc, total_size, new_node, &fm)) < 0) {
2529
                D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2530
                kfree(new_node);
2531
                DJM(no_jffs_node--);
2532
                return err;
2533
        }
2534
        else if (!fm->nodes) {
2535
                /* The jffs_fm struct that we got is not good enough.  */
2536
                if ((err = jffs_write_dummy_node(c, fm)) < 0) {
2537
                        D(printk("jffs_rewrite_data(): "
2538
                                 "jffs_write_dummy_node() Failed!\n"));
2539
                        kfree(fm);
2540
                        DJM(no_jffs_fm--);
2541
                        return err;
2542
                }
2543
                /* Get a new one.  */
2544
                if ((err = jffs_fmalloc(fmc, total_size, node, &fm)) < 0) {
2545
                        D(printk("jffs_rewrite_data(): Second "
2546
                                 "jffs_fmalloc(0x%p, %u) failed!\n",
2547
                                 fmc, total_size));
2548
                        return err;
2549
                }
2550
        }
2551
        new_node->fm = fm;
2552
 
2553
        ASSERT(if (new_node->fm->nodes == 0) {
2554
                printk(KERN_ERR "jffs_rewrite_data(): "
2555
                       "new_node->fm->nodes == 0\n");
2556
        });
2557
 
2558
        /* Initialize the raw inode.  */
2559
        raw_inode.magic = JFFS_MAGIC_BITMASK;
2560
        raw_inode.ino = f->ino;
2561
        raw_inode.pino = f->pino;
2562
        raw_inode.version = f->highest_version + 1;
2563
        raw_inode.mode = f->mode;
2564
        raw_inode.uid = f->uid;
2565
        raw_inode.gid = f->gid;
2566
        raw_inode.atime = f->atime;
2567
        raw_inode.mtime = f->mtime;
2568
        raw_inode.ctime = f->ctime;
2569
        raw_inode.offset = node->data_offset;
2570
        raw_inode.dsize = size;
2571
        raw_inode.rsize = size;
2572
        raw_inode.nsize = f->nsize;
2573
        raw_inode.nlink = f->nlink;
2574
        raw_inode.spare = 0;
2575
        raw_inode.rename = 0;
2576
        raw_inode.deleted = 0;
2577
        raw_inode.accurate = 0xff;
2578
        raw_inode.dchksum = 0;
2579
        raw_inode.nchksum = 0;
2580
 
2581
        pos = (unsigned char *) new_node->fm->offset;
2582
        pos_dchksum = &pos[JFFS_RAW_INODE_DCHKSUM_OFFSET];
2583
 
2584
        D3(printk("jffs_rewrite_data(): Writing this raw inode "
2585
                  "to pos 0x%p.\n", pos));
2586
        D3(jffs_print_raw_inode(&raw_inode));
2587
 
2588
        if ((err = flash_safe_write(fmc->flash_part, pos,
2589
                                    (unsigned char *) &raw_inode,
2590
                                    sizeof(struct jffs_raw_inode)
2591
                                    - sizeof(__u32)
2592
                                    - sizeof(__u16) - sizeof(__u16))) < 0) {
2593
                D(printk(KERN_WARNING "JFFS: Write error during "
2594
                         "rewrite. (raw inode)\n"));
2595
                jffs_fmfree_partly(fmc, fm,
2596
                                   total_name_size + total_data_size);
2597
                return err;
2598
        }
2599
        pos += sizeof(struct jffs_raw_inode);
2600
 
2601
        /* Write the name to the flash memory.  */
2602
        if (f->nsize) {
2603
                D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
2604
                          "pos 0x%p.\n", f->name, pos));
2605
                if ((err = flash_safe_write(fmc->flash_part, pos,
2606
                                            (unsigned char *)f->name,
2607
                                            f->nsize)) < 0) {
2608
 
2609
                        D(printk(KERN_WARNING "JFFS: Write error during "
2610
                                 "rewrite. (name)\n"));
2611
                        jffs_fmfree_partly(fmc, fm, total_data_size);
2612
                        return err;
2613
                }
2614
                pos += total_name_size;
2615
                raw_inode.nchksum = jffs_checksum(f->name, f->nsize);
2616
        }
2617
 
2618
        /* Write the data.  */
2619
        if (size) {
2620
                int r;
2621
                unsigned char *page;
2622
                __u32 offset = node->data_offset;
2623
 
2624
                if (!(page = (unsigned char *)__get_free_page(GFP_KERNEL))) {
2625
                        jffs_fmfree_partly(fmc, fm, 0);
2626
                        return -1;
2627
                }
2628
 
2629
                while (size) {
2630
                        __u32 s = jffs_min(size, PAGE_SIZE);
2631
                        if ((r = jffs_read_data(f, (char *)page,
2632
                                                offset, s)) < s) {
2633
                                D(printk("jffs_rewrite_data(): "
2634
                                         "jffs_read_data() "
2635
                                         "failed! (r = %d)\n", r));
2636
                                jffs_fmfree_partly(fmc, fm, 0);
2637
                                return -1;
2638
                        }
2639
                        if ((err = flash_safe_write(fmc->flash_part,
2640
                                                    pos, page, r)) < 0) {
2641
                                D(printk(KERN_WARNING "JFFS: Write error "
2642
                                         "during rewrite. (data)\n"));
2643
                                free_page((__u32)page);
2644
                                jffs_fmfree_partly(fmc, fm, 0);
2645
                                return err;
2646
                        }
2647
                        pos += r;
2648
                        size -= r;
2649
                        offset += r;
2650
                        raw_inode.dchksum += jffs_checksum(page, r);
2651
                }
2652
 
2653
                free_page((__u32)page);
2654
        }
2655
 
2656
        raw_inode.accurate = 0;
2657
        raw_inode.chksum = jffs_checksum(&raw_inode,
2658
                                         sizeof(struct jffs_raw_inode)
2659
                                         - sizeof(__u16));
2660
 
2661
        /* Add the checksum.  */
2662
        if ((err
2663
             = flash_safe_write(fmc->flash_part, pos_dchksum,
2664
                                &((unsigned char *)
2665
                                &raw_inode)[JFFS_RAW_INODE_DCHKSUM_OFFSET],
2666
                                sizeof(__u32) + sizeof(__u16)
2667
                                + sizeof(__u16))) < 0) {
2668
                D(printk(KERN_WARNING "JFFS: Write error during "
2669
                         "rewrite. (checksum)\n"));
2670
                jffs_fmfree_partly(fmc, fm, 0);
2671
                return err;
2672
        }
2673
 
2674
        /* Now make the file system aware of the newly written node.  */
2675
        jffs_insert_node(c, f, &raw_inode, f->name, new_node);
2676
 
2677
        D3(printk("jffs_rewrite_data(): Leaving...\n"));
2678
        return 0;
2679
} /* jffs_rewrite_data()  */
2680
 
2681
#else
2682
 
2683
/* Rewrite `size' bytes, and begin at `node'.  */
2684
int
2685
jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, int size)
2686
{
2687
        struct jffs_raw_inode raw_inode;
2688
        struct jffs_node *new_node;
2689
        struct jffs_fm *fm;
2690
        struct buffer_head *bh;
2691
        __u32 chksum;
2692
        __u32 write_size;
2693
        __u32 block_offset;
2694
        __u32 block;
2695
        __u32 copied_data = 0;
2696
        __u32 pos_chksum;
2697
        __u32 block_chksum;
2698
        __u32 total_size;
2699
        kdev_t dev = f->c->sb->s_dev;
2700
        int err;
2701
 
2702
        D(printk("jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2703
                 f->ino, (f->name ? f->name : ""), size));
2704
 
2705
        /* Create and initialize the new node.  */
2706
        if (!(new_node = (struct jffs_node *)
2707
                         kmalloc(sizeof(struct jffs_node), GFP_KERNEL))) {
2708
                D(printk("jffs_rewrite_data(): "
2709
                         "Failed to allocate node.\n"));
2710
                return -ENOMEM;
2711
        }
2712
        DJM(no_jffs_node++);
2713
        new_node->data_offset = node->data_offset;
2714
        new_node->data_size = size;
2715
        new_node->removed_size = size;
2716
        total_size = sizeof(struct jffs_raw_inode)
2717
                     + f->nsize + JFFS_GET_PAD_BYTES(f->nsize)
2718
                     + size + JFFS_GET_PAD_BYTES(size);
2719
        new_node->fm_offset = sizeof(struct jffs_raw_inode)
2720
                              + f->nsize + JFFS_GET_PAD_BYTES(f->nsize);
2721
 
2722
        if ((err = jffs_fmalloc(f->c->fmc, total_size, new_node, &fm)) < 0) {
2723
                D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2724
                kfree(new_node);
2725
                DJM(no_jffs_node--);
2726
                return err;
2727
        }
2728
        else if (!fm->nodes) {
2729
                /* The jffs_fm struct that we got is not good enough.  */
2730
                if ((err = jffs_write_dummy_node(f->c, fm)) < 0) {
2731
                        D(printk("jffs_rewrite_data(): "
2732
                                 "jffs_write_dummy_node() Failed!\n"));
2733
                        kfree(fm);
2734
                        DJM(no_jffs_fm--);
2735
                        return err;
2736
                }
2737
                /* Get a new one.  */
2738
                if ((err = jffs_fmalloc(f->c->fmc, total_size, node, &fm)) < 0) {
2739
                        D(printk("jffs_rewrite_data(): Second "
2740
                                 "jffs_fmalloc(0x%08x, %u) failed!\n",
2741
                                 f->c->fmc, total_size));
2742
                        return err;
2743
                }
2744
        }
2745
        new_node->fm = fm;
2746
 
2747
        ASSERT(if (new_node->fm->nodes == 0) {
2748
                printk(KERN_ERR "jffs_rewrite_data(): "
2749
                       "new_node->fm->nodes == 0\n");
2750
        });
2751
 
2752
        /* Initialize the raw inode.  */
2753
        raw_inode.magic = JFFS_MAGIC_BITMASK;
2754
        raw_inode.ino = f->ino;
2755
        raw_inode.pino = f->pino;
2756
        raw_inode.version = f->highest_version + 1;
2757
        raw_inode.mode = f->mode;
2758
        raw_inode.uid = f->uid;
2759
        raw_inode.gid = f->gid;
2760
        raw_inode.atime = f->atime;
2761
        raw_inode.mtime = f->mtime;
2762
        raw_inode.ctime = f->ctime;
2763
        raw_inode.offset = node->data_offset;
2764
        raw_inode.dsize = size;
2765
        raw_inode.rsize = size;
2766
        raw_inode.nsize = f->nsize;
2767
        raw_inode.nlink = f->nlink;
2768
        raw_inode.deleted = 0;
2769
        raw_inode.accurate = 0;
2770
        raw_inode.chksum = 0;
2771
        chksum = jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
2772
        raw_inode.accurate = 0xff;
2773
        raw_inode.chksum = JFFS_EMPTY_BITMASK;
2774
 
2775
        /* Retrieve the first block to which the new node is going
2776
           to be written.  */
2777
        block = new_node->fm->offset / BLOCK_SIZE;
2778
        block_offset = new_node->fm->offset - block * BLOCK_SIZE;
2779
 
2780
        D(printk("jffs_rewrite_data(): Writing to dev = 0x%04x, block = %u, "
2781
                 "block_offset = %u, block * BLOCK_SIZE + offset = %u\n",
2782
                 dev, block, block_offset, new_node->fm->offset));
2783
 
2784
        if (!(bh = jffs_get_write_buffer(dev, block))) {
2785
                D(printk("jffs_rewrite_data(): Failed to read block.\n"));
2786
                kfree(new_node->fm);
2787
                DJM(no_jffs_fm--);
2788
                kfree(new_node);
2789
                DJM(no_jffs_node--);
2790
                return -1;
2791
        }
2792
 
2793
        /* Write the raw_inode to the flash.  */
2794
        if (BLOCK_SIZE - block_offset < sizeof(struct jffs_raw_inode)) {
2795
                /* Too little space left on this block.  */
2796
                write_size = BLOCK_SIZE - block_offset;
2797
                memcpy(&bh->b_data[block_offset], &raw_inode, write_size);
2798
                jffs_put_write_buffer(bh);
2799
                bh = jffs_get_write_buffer(dev, ++block);
2800
                memcpy(bh->b_data, (void *)&raw_inode + write_size,
2801
                       sizeof(struct jffs_raw_inode) - write_size);
2802
                block_offset = sizeof(struct jffs_raw_inode) - write_size;
2803
                pos_chksum = (block_offset - 4);
2804
                block_chksum = block;
2805
        }
2806
        else {
2807
                memcpy(&bh->b_data[block_offset], &raw_inode,
2808
                       sizeof(struct jffs_raw_inode));
2809
                block_offset += sizeof(struct jffs_raw_inode);
2810
                pos_chksum = (block_offset - 4);
2811
                block_chksum = block;
2812
                if (block_offset == BLOCK_SIZE) {
2813
                        jffs_put_write_buffer(bh);
2814
                        bh = 0;
2815
                        block_offset = 0;
2816
                }
2817
        }
2818
 
2819
        if (!bh && (f->nsize || size)) {
2820
                bh = jffs_get_write_buffer(dev, ++block);
2821
        }
2822
 
2823
        /* Write the name to the flash memory.  */
2824
        if (f->nsize) {
2825
                if (BLOCK_SIZE - block_offset < f->nsize) {
2826
                        write_size = BLOCK_SIZE - block_offset;
2827
                        memcpy(&bh->b_data[block_offset], f->name, write_size);
2828
                        jffs_put_write_buffer(bh);
2829
                        bh = jffs_get_write_buffer(dev, ++block);
2830
                        memcpy(bh->b_data, &f->name[write_size],
2831
                               f->nsize - write_size);
2832
                        block_offset = f->nsize - write_size;
2833
                }
2834
                else {
2835
                        memcpy(&bh->b_data[block_offset], f->name, f->nsize);
2836
                        block_offset += f->nsize;
2837
                        if (block_offset == BLOCK_SIZE) {
2838
                                jffs_put_write_buffer(bh);
2839
                                bh = 0;
2840
                                block_offset = 0;
2841
                        }
2842
                }
2843
                block_offset += JFFS_GET_PAD_BYTES(block_offset);
2844
                chksum += jffs_checksum(f->name, f->nsize);
2845
        }
2846
 
2847
        if (!bh && size) {
2848
                bh = jffs_get_write_buffer(dev, ++block);
2849
        }
2850
 
2851
        /* Write the data.  */
2852
        while (copied_data < size) {
2853
                int r;
2854
                D(printk("jffs_rewrite_data(): copied_data = %u, "
2855
                         "block_offset = %u\n",
2856
                         copied_data, block_offset));
2857
                if (block_offset == BLOCK_SIZE) {
2858
                        jffs_put_write_buffer(bh);
2859
                        bh = jffs_get_write_buffer(dev, ++block);
2860
                        block_offset = 0;
2861
                }
2862
                write_size = jffs_min(size - copied_data,
2863
                                      BLOCK_SIZE - block_offset);
2864
                if ((r = jffs_read_data(f, &bh->b_data[block_offset], copied_data,
2865
                                        write_size)) < write_size) {
2866
                        D(printk("jffs_rewrite_data(): jffs_read_data() "
2867
                                 "failed! (r = %d)\n", r));
2868
                        brelse(bh);
2869
                        return -1;
2870
                }
2871
                chksum += jffs_checksum(&bh->b_data[block_offset], write_size);
2872
                block_offset += r;
2873
                copied_data += r;
2874
        }
2875
 
2876
        if (bh) {
2877
                jffs_put_write_buffer(bh);
2878
        }
2879
 
2880
        /* Add the checksum.  */
2881
        if (!(bh = jffs_get_write_buffer(dev, block_chksum))) {
2882
                D(printk("jffs_rewrite_data(): Failed to read "
2883
                         "chksum block. (%u)\n", block_chksum));
2884
                return -1;
2885
        }
2886
        *(__u32 *)&bh->b_data[pos_chksum] = chksum;
2887
        jffs_put_write_buffer(bh);
2888
        D(printk("jffs_rewrite_data(): Added chksum 0x%08x.\n", chksum));
2889
 
2890
        /* Now make the file system aware of the newly written node.  */
2891
        jffs_insert_node(f->c, f, &raw_inode, 0, new_node);
2892
 
2893
        D(printk("jffs_rewrite_data(): Leaving...\n"));
2894
        return 0;
2895
} /* jffs_rewrite_data()  */
2896
 
2897
#endif
2898
 
2899
 
2900
int
2901
jffs_garbage_collect_next(struct jffs_control *c)
2902
{
2903
        struct jffs_fmcontrol *fmc = c->fmc;
2904
        struct jffs_node *node;
2905
        struct jffs_file *f;
2906
        int size;
2907
        int free_size = fmc->flash_size - (fmc->used_size + fmc->dirty_size);
2908
        __u32 free_chunk_size1 = jffs_free_size1(fmc);
2909
        D2(__u32 free_chunk_size2 = jffs_free_size2(fmc));
2910
 
2911
        /* Get the oldest node in the flash.  */
2912
        node = jffs_get_oldest_node(fmc);
2913
        ASSERT(if (!node) {
2914
                printk(KERN_ERR "JFFS: No oldest node!\n");
2915
                return -1;
2916
        });
2917
 
2918
        /* Find its corresponding file too.  */
2919
        f = jffs_find_file(c, node->ino);
2920
        ASSERT(if (!f) {
2921
                printk(KERN_ERR "JFFS: No file to garbage collect! "
2922
                       "ino = 0x%08x\n", node->ino);
2923
                return -1;
2924
        });
2925
 
2926
        D1(printk("jffs_garbage_collect_next(): \"%s\", "
2927
                  "ino: %u, version: %u\n",
2928
                  (f->name ? f->name : ""), node->ino, node->version));
2929
 
2930
        /* Compute how much we want to rewrite at the moment.  */
2931
        size = sizeof(struct jffs_raw_inode) + f->nsize
2932
               + f->size - node->data_offset;
2933
        D2(printk("  size: %u\n", size));
2934
        D2(printk("  f->nsize: %u\n", f->nsize));
2935
        D2(printk("  f->size: %u\n", f->size));
2936
        D2(printk("  free_chunk_size1: %u\n", free_chunk_size1));
2937
        D2(printk("  free_chunk_size2: %u\n", free_chunk_size2));
2938
        if (size > fmc->max_chunk_size) {
2939
                size = fmc->max_chunk_size;
2940
        }
2941
        if (size > free_chunk_size1) {
2942
 
2943
                if (free_chunk_size1 <
2944
                    (sizeof(struct jffs_raw_inode) + f->nsize + BLOCK_SIZE)) {
2945
                        /* The space left is too small to be of any
2946
                           use really.  */
2947
                        struct jffs_fm *dirty_fm
2948
                        = jffs_fmalloced(fmc,
2949
                                         fmc->tail->offset + fmc->tail->size,
2950
                                         free_chunk_size1, NULL);
2951
                        if (dirty_fm) {
2952
                                return -1;
2953
                        }
2954
                        jffs_write_dummy_node(c, dirty_fm);
2955
                        goto jffs_garbage_collect_next_end;
2956
                }
2957
 
2958
                size = free_chunk_size1;
2959
        }
2960
 
2961
        D2(printk("  size: %u (again)\n", size));
2962
 
2963
        if (free_size - size < fmc->sector_size) {
2964
                /* Just rewrite that node (or even less).  */
2965
                size -= (sizeof(struct jffs_raw_inode) + f->nsize);
2966
                jffs_rewrite_data(f, node, jffs_min(node->data_size, size));
2967
        }
2968
        else {
2969
                size -= (sizeof(struct jffs_raw_inode) + f->nsize);
2970
                jffs_rewrite_data(f, node, size);
2971
        }
2972
 
2973
jffs_garbage_collect_next_end:
2974
        D3(printk("jffs_garbage_collect_next(): Leaving...\n"));
2975
        return 0;
2976
}
2977
 
2978
 
2979
#if defined(JFFS_FLASH_SHORTCUT) && JFFS_FLASH_SHORTCUT
2980
 
2981
/* If an obsolete node is partly going to be erased due to garbage
2982
   collection, the part that isn't going to be erased must be filled
2983
   with zeroes so that the scan of the flash will work smoothly next
2984
   time.
2985
     There are two phases in this procedure: First, the clearing of
2986
   the name and data parts of the node. Second, possibly also clearing
2987
   a part of the raw inode as well.  If the box is power cycled during
2988
   the first phase, only the checksum of this node-to-be-cleared-at-
2989
   the-end will be wrong.  If the box is power cycled during, or after,
2990
   the clearing of the raw inode, the information like the length of
2991
   the name and data parts are zeroed.  The next time the box is
2992
   powered up, the scanning algorithm manages this faulty data too
2993
   because:
2994
 
2995
   - The checksum is invalid and thus the raw inode must be discarded
2996
     in any case.
2997
   - If the lengths of the data part or the name part are zeroed, the
2998
     scanning just continues after the raw inode.  But after the inode
2999
     the scanning procedure just finds zeroes which is the same as
3000
     dirt.
3001
 
3002
   So, in the end, this could never fail. :-)  Even if it does fail,
3003
   the scanning algorithm should manage that too.  */
3004
 
3005
static int
3006
jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
3007
{
3008
        struct jffs_fm *fm;
3009
        struct jffs_fmcontrol *fmc = c->fmc;
3010
        __u32 zero_offset;
3011
        __u32 zero_size;
3012
        __u32 zero_offset_data;
3013
        __u32 zero_size_data;
3014
        __u32 cutting_raw_inode = 0;
3015
 
3016
        if (!(fm = jffs_cut_node(fmc, erase_size))) {
3017
                D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
3018
                return 0;
3019
        }
3020
 
3021
        /* Where and how much shall we clear?  */
3022
        zero_offset = fmc->head->offset + erase_size;
3023
        zero_size = fm->offset + fm->size - zero_offset;
3024
 
3025
        /* Do we have to clear the raw_inode explicitly?  */
3026
        if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
3027
                cutting_raw_inode = sizeof(struct jffs_raw_inode)
3028
                                    - (fm->size - zero_size);
3029
        }
3030
 
3031
        /* First, clear the name and data fields.  */
3032
        zero_offset_data = zero_offset + cutting_raw_inode;
3033
        zero_size_data = zero_size - cutting_raw_inode;
3034
        flash_safe_acquire(fmc->flash_part);
3035
        flash_memset((unsigned char *) zero_offset_data, 0, zero_size_data);
3036
        flash_safe_release(fmc->flash_part);
3037
 
3038
        /* Should we clear a part of the raw inode?  */
3039
        if (cutting_raw_inode) {
3040
                /* I guess it is ok to clear the raw inode in this order.  */
3041
                flash_safe_acquire(fmc->flash_part);
3042
                flash_memset((unsigned char *) zero_offset, 0,
3043
                             cutting_raw_inode);
3044
                flash_safe_release(fmc->flash_part);
3045
        }
3046
 
3047
        return 0;
3048
} /* jffs_clear_end_of_node()  */
3049
 
3050
#else
3051
 
3052
static int
3053
jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
3054
{
3055
        struct jffs_fm *fm;
3056
        __u32 zero_offset;
3057
        __u32 zero_size;
3058
        __u32 first_block;
3059
        __u32 last_block;
3060
        __u32 block_offset;
3061
        __u32 block_clear_size;
3062
        __u32 block;
3063
        int cutting_raw_inode = 0;
3064
        struct buffer_head *bh;
3065
        kdev_t dev;
3066
 
3067
        if (!(fm = jffs_cut_node(c->fmc, erase_size))) {
3068
                D(printk("jffs_clear_end_of_node(): fm == NULL\n"));
3069
                return 0;
3070
        }
3071
 
3072
        /* It is necessary to write zeroes to the flash.
3073
           Find out where and how much to write.  */
3074
        zero_offset = c->fmc->head->offset + erase_size;
3075
        zero_size = fm->offset + fm->size - zero_offset;
3076
        /* Do we have to clear the raw_inode explicitly?  */
3077
        if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
3078
                cutting_raw_inode = sizeof(struct jffs_raw_inode)
3079
                                    - (fm->size - zero_size);
3080
        }
3081
        /* The last block of non-raw inode data should be
3082
           cleared first.  */
3083
        first_block = zero_offset / BLOCK_SIZE;
3084
        last_block = (zero_offset + zero_size) / BLOCK_SIZE;
3085
        block_offset = 0;
3086
        block_clear_size = (zero_offset + zero_size)
3087
                           - last_block * BLOCK_SIZE;
3088
        dev = c->sb->s_dev;
3089
 
3090
        D3(printk("jffs_clear_end_of_node(): zero_offset: 0x%08x\n", zero_offset));
3091
        D3(printk("jffs_clear_end_of_node(): zero_size: 0x%08x\n", zero_size));
3092
        D3(printk("jffs_clear_end_of_node(): first_block: 0x%08x\n", first_block));
3093
        D3(printk("jffs_clear_end_of_node(): last_block: 0x%08x\n", last_block));
3094
        D3(printk("jffs_clear_end_of_node(): block_offset: 0x%08x\n", block_offset));
3095
        D3(printk("jffs_clear_end_of_node(): block_clear_size: 0x%08x\n", block_clear_size));
3096
 
3097
        /* Fill the flash memory with zeroes.  */
3098
        for (block = last_block;
3099
             block >= first_block; block--) {
3100
                if (block == first_block) {
3101
                        block_offset = cutting_raw_inode;
3102
                        block_clear_size -= cutting_raw_inode;
3103
                }
3104
                if (!(bh = jffs_get_write_buffer(dev, block))) {
3105
                        D(printk("jffs_clear_end_of_node(): "
3106
                                 "Failed to get buffer!\n"));
3107
                        return -1;
3108
                }
3109
                memset(&bh->b_data[block_offset], 0, block_clear_size);
3110
                jffs_put_write_buffer(bh);
3111
                block_clear_size = BLOCK_SIZE;
3112
        }
3113
 
3114
        if (cutting_raw_inode) {
3115
                if (!(bh = jffs_get_write_buffer(dev, first_block))) {
3116
                        D(printk("jffs_clear_end_of_node(): Failed to "
3117
                                 "get first buffer!\n"));
3118
                        return -1;
3119
                }
3120
                memset(bh->b_data, 0, cutting_raw_inode);
3121
                jffs_put_write_buffer(bh);
3122
        }
3123
 
3124
        return 0;
3125
} /* jffs_clear_end_of_node()  */
3126
 
3127
#endif
3128
 
3129
 
3130
/* Try to erase as much as possible of the dirt in the flash memory.  */
3131
long
3132
jffs_try_to_erase(struct jffs_control *c)
3133
{
3134
        struct jffs_fmcontrol *fmc = c->fmc;
3135
        long erase_size;
3136
        int err;
3137
        __u32 offset;
3138
 
3139
        D3(printk("jffs_try_to_erase()\n"));
3140
 
3141
        erase_size = jffs_erasable_size(fmc);
3142
 
3143
        D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size));
3144
 
3145
        if (erase_size <= 0) {
3146
                return erase_size;
3147
        }
3148
 
3149
        if (jffs_clear_end_of_node(c, erase_size) < 0) {
3150
                printk(KERN_ERR "JFFS: Clearing of node failed.\n");
3151
                return -1;
3152
        }
3153
 
3154
        offset = fmc->head->offset - fmc->flash_start;
3155
 
3156
        /* Now, let's try to do the erase.  */
3157
        if ((err = flash_erase_region(c->sb->s_dev,
3158
                                      offset, erase_size)) < 0) {
3159
                printk(KERN_ERR "JFFS: Erase of flash failed. "
3160
                       "offset = %u, erase_size = %ld\n",
3161
                       offset, erase_size);
3162
                /* XXX: Here we should allocate this area as dirty
3163
                   with jffs_fmalloced or something similar.  Now
3164
                   we just report the error.  */
3165
                return err;
3166
        }
3167
 
3168
#if 0
3169
        /* Check if the erased sectors really got erased.  */
3170
        {
3171
                __u32 pos;
3172
                __u32 end;
3173
 
3174
                pos = (__u32)flash_get_direct_pointer(c->sb->s_dev, offset);
3175
                end = pos + erase_size;
3176
 
3177
                D2(printk("JFFS: Checking erased sector(s)...\n"));
3178
 
3179
                flash_safe_acquire(fmc->flash_part);
3180
 
3181
                for (; pos < end; pos += 4) {
3182
                        if (*(__u32 *)pos != JFFS_EMPTY_BITMASK) {
3183
                                printk("JFFS: Erase failed! pos = 0x%p\n",
3184
                                       (unsigned char *)pos);
3185
                                jffs_hexdump((unsigned char *)pos,
3186
                                             jffs_min(256, end - pos));
3187
                                err = -1;
3188
                                break;
3189
                        }
3190
                }
3191
 
3192
                flash_safe_release(fmc->flash_part);
3193
 
3194
                if (!err) {
3195
                        D2(printk("JFFS: Erase succeeded.\n"));
3196
                }
3197
                else {
3198
                        /* XXX: Here we should allocate the memory
3199
                           with jffs_fmalloced() in order to prevent
3200
                           JFFS from using this area accidentally.  */
3201
                        return err;
3202
                }
3203
        }
3204
#endif
3205
 
3206
        /* Update the flash memory data structures.  */
3207
        jffs_sync_erase(fmc, erase_size);
3208
 
3209
        return erase_size;
3210
}
3211
 
3212
 
3213
/* There are different criteria that should trigger a garbage collect:
3214
   1. There is too much dirt in the memory.
3215
   2. The free space is becoming small.
3216
   3. There are many versions of a node.
3217
 
3218
   The garbage collect should always be done in a manner that guarantees
3219
   that future garbage collects cannot be locked.  E.g. Rewritten chunks
3220
   should not be too large (span more than one sector in the flash memory
3221
   for exemple).  Of course there is a limit on how intelligent this garbage
3222
   collection can be.  */
3223
int
3224
jffs_garbage_collect(struct jffs_control *c)
3225
{
3226
        struct jffs_fmcontrol *fmc = c->fmc;
3227
        long erased_total = 0;
3228
        long erased;
3229
        int result = 0;
3230
        D1(int i = 1);
3231
 
3232
        D2(printk("***jffs_garbage_collect(): fmc->dirty_size = %u\n",
3233
                  fmc->dirty_size));
3234
        D2(jffs_print_fmcontrol(fmc));
3235
 
3236
        c->fmc->no_call_gc = 1;
3237
 
3238
        /* While there is too much dirt left and it is possible
3239
           to garbage collect, do so.  */
3240
 
3241
        while (fmc->dirty_size >= fmc->sector_size) {
3242
 
3243
                D1(printk("***jffs_garbage_collect(): round #%u, "
3244
                         "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3245
                D2(jffs_print_fmcontrol(fmc));
3246
 
3247
                /* At least one sector should be able to free now.  */
3248
                if ((erased = jffs_try_to_erase(c)) < 0) {
3249
                        printk(KERN_WARNING "JFFS: Error in "
3250
                               "garbage collector.\n");
3251
                        result = erased;
3252
                        goto gc_end;
3253
                }
3254
                else if (erased == 0) {
3255
                        __u32 free_size = fmc->flash_size
3256
                                          - (fmc->used_size
3257
                                             + fmc->dirty_size);
3258
 
3259
                        if (free_size > 0) {
3260
                                /* Let's dare to make a garbage collect.  */
3261
                                if ((result = jffs_garbage_collect_next(c))
3262
                                    < 0) {
3263
                                        printk(KERN_ERR "JFFS: Something "
3264
                                               "has gone seriously wrong "
3265
                                               "with a garbage collect.\n");
3266
                                        goto gc_end;
3267
                                }
3268
                        }
3269
                        else {
3270
                                /* What should we do here?  */
3271
                                D(printk("   jffs_garbage_collect(): "
3272
                                         "erased: %ld, free_size: %u\n",
3273
                                         erased, free_size));
3274
                                result = -1;
3275
                                goto gc_end;
3276
                        }
3277
                }
3278
 
3279
                D1(printk("   jffs_garbage_collect(): erased: %ld\n", erased));
3280
                erased_total += erased;
3281
                DJM(jffs_print_memory_allocation_statistics());
3282
        }
3283
 
3284
 
3285
gc_end:
3286
        c->fmc->no_call_gc = 0;
3287
 
3288
        D3(printk("   jffs_garbage_collect(): Leaving...\n"));
3289
        D1(if (erased_total) {
3290
                printk("erased_total = %ld\n", erased_total);
3291
                jffs_print_fmcontrol(fmc);
3292
        });
3293
        return result;
3294
}

powered by: WebSVN 2.1.0

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