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