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