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