Commit 419ee448 authored by Jeff Garzik's avatar Jeff Garzik

Remove JFFS (version 1), as scheduled.

Unmaintained for years, few if any users.
Signed-off-by: default avatarJeff Garzik <jeff@garzik.org>
parent 8a03d9a4
......@@ -306,13 +306,6 @@ Who: Len Brown <len.brown@intel.com>
---------------------------
What: JFFS (version 1)
When: 2.6.21
Why: Unmaintained for years, superceded by JFFS2 for years.
Who: Jeff Garzik <jeff@garzik.org>
---------------------------
What: sk98lin network driver
When: July 2007
Why: In kernel tree version of driver is unmaintained. Sk98lin driver
......
......@@ -1189,32 +1189,6 @@ config EFS_FS
To compile the EFS file system support as a module, choose M here: the
module will be called efs.
config JFFS_FS
tristate "Journalling Flash File System (JFFS) support"
depends on MTD && BLOCK && BROKEN
help
JFFS is the Journalling Flash File System developed by Axis
Communications in Sweden, aimed at providing a crash/powerdown-safe
file system for disk-less embedded devices. Further information is
available at (<http://developer.axis.com/software/jffs/>).
NOTE: This filesystem is deprecated and is scheduled for removal in
2.6.21. See Documentation/feature-removal-schedule.txt
config JFFS_FS_VERBOSE
int "JFFS debugging verbosity (0 = quiet, 3 = noisy)"
depends on JFFS_FS
default "0"
help
Determines the verbosity level of the JFFS debugging messages.
config JFFS_PROC_FS
bool "JFFS stats available in /proc filesystem"
depends on JFFS_FS && PROC_FS
help
Enabling this option will cause statistics from mounted JFFS file systems
to be made available to the user in the /proc/fs/jffs/ directory.
config JFFS2_FS
tristate "Journalling Flash File System v2 (JFFS2) support"
select CRC32
......
......@@ -94,7 +94,6 @@ obj-$(CONFIG_HPFS_FS) += hpfs/
obj-$(CONFIG_NTFS_FS) += ntfs/
obj-$(CONFIG_UFS_FS) += ufs/
obj-$(CONFIG_EFS_FS) += efs/
obj-$(CONFIG_JFFS_FS) += jffs/
obj-$(CONFIG_JFFS2_FS) += jffs2/
obj-$(CONFIG_AFFS_FS) += affs/
obj-$(CONFIG_ROMFS_FS) += romfs/
......
#
# Makefile for the linux Journalling Flash FileSystem (JFFS) routines.
#
# $Id: Makefile,v 1.11 2001/09/25 20:59:41 dwmw2 Exp $
#
obj-$(CONFIG_JFFS_FS) += jffs.o
jffs-y := jffs_fm.o intrep.o inode-v23.o
jffs-$(CONFIG_JFFS_PROC_FS) += jffs_proc.o
jffs-objs := $(jffs-y)
/*
* JFFS -- Journalling Flash File System, Linux implementation.
*
* Copyright (C) 1999, 2000 Axis Communications AB.
*
* Created by Finn Hakansson <finn@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: inode-v23.c,v 1.70 2001/10/02 09:16:02 dwmw2 Exp $
*
* Ported to Linux 2.3.x and MTD:
* Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
*
* Copyright 2000, 2001 Red Hat, Inc.
*/
/* inode.c -- Contains the code that is called from the VFS. */
/* TODO-ALEX:
* uid and gid are just 16 bit.
* jffs_file_write reads from user-space pointers without xx_from_user
* maybe other stuff do to.
*/
#include <linux/time.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/jffs.h>
#include <linux/fs.h>
#include <linux/smp_lock.h>
#include <linux/ioctl.h>
#include <linux/stat.h>
#include <linux/blkdev.h>
#include <linux/quotaops.h>
#include <linux/highmem.h>
#include <linux/vfs.h>
#include <linux/mutex.h>
#include <asm/byteorder.h>
#include <asm/uaccess.h>
#include "jffs_fm.h"
#include "intrep.h"
#ifdef CONFIG_JFFS_PROC_FS
#include "jffs_proc.h"
#endif
static int jffs_remove(struct inode *dir, struct dentry *dentry, int type);
static const struct super_operations jffs_ops;
static const struct file_operations jffs_file_operations;
static const struct inode_operations jffs_file_inode_operations;
static const struct file_operations jffs_dir_operations;
static const struct inode_operations jffs_dir_inode_operations;
static const struct address_space_operations jffs_address_operations;
struct kmem_cache *node_cache = NULL;
struct kmem_cache *fm_cache = NULL;
/* Called by the VFS at mount time to initialize the whole file system. */
static int jffs_fill_super(struct super_block *sb, void *data, int silent)
{
struct inode *root_inode;
struct jffs_control *c;
sb->s_flags |= MS_NODIRATIME;
D1(printk(KERN_NOTICE "JFFS: Trying to mount device %s.\n",
sb->s_id));
if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
printk(KERN_WARNING "JFFS: Trying to mount a "
"non-mtd device.\n");
return -EINVAL;
}
sb->s_blocksize = PAGE_CACHE_SIZE;
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
sb->s_fs_info = (void *) 0;
sb->s_maxbytes = 0xFFFFFFFF;
/* Build the file system. */
if (jffs_build_fs(sb) < 0) {
goto jffs_sb_err1;
}
/*
* set up enough so that we can read an inode
*/
sb->s_magic = JFFS_MAGIC_SB_BITMASK;
sb->s_op = &jffs_ops;
root_inode = iget(sb, JFFS_MIN_INO);
if (!root_inode)
goto jffs_sb_err2;
/* Get the root directory of this file system. */
if (!(sb->s_root = d_alloc_root(root_inode))) {
goto jffs_sb_err3;
}
c = (struct jffs_control *) sb->s_fs_info;
#ifdef CONFIG_JFFS_PROC_FS
/* Set up the jffs proc file system. */
if (jffs_register_jffs_proc_dir(MINOR(sb->s_dev), c) < 0) {
printk(KERN_WARNING "JFFS: Failed to initialize the JFFS "
"proc file system for device %s.\n",
sb->s_id);
}
#endif
/* Set the Garbage Collection thresholds */
/* GC if free space goes below 5% of the total size */
c->gc_minfree_threshold = c->fmc->flash_size / 20;
if (c->gc_minfree_threshold < c->fmc->sector_size)
c->gc_minfree_threshold = c->fmc->sector_size;
/* GC if dirty space exceeds 33% of the total size. */
c->gc_maxdirty_threshold = c->fmc->flash_size / 3;
if (c->gc_maxdirty_threshold < c->fmc->sector_size)
c->gc_maxdirty_threshold = c->fmc->sector_size;
c->thread_pid = kernel_thread (jffs_garbage_collect_thread,
(void *) c,
CLONE_KERNEL);
D1(printk(KERN_NOTICE "JFFS: GC thread pid=%d.\n", (int) c->thread_pid));
D1(printk(KERN_NOTICE "JFFS: Successfully mounted device %s.\n",
sb->s_id));
return 0;
jffs_sb_err3:
iput(root_inode);
jffs_sb_err2:
jffs_cleanup_control((struct jffs_control *)sb->s_fs_info);
jffs_sb_err1:
printk(KERN_WARNING "JFFS: Failed to mount device %s.\n",
sb->s_id);
return -EINVAL;
}
/* This function is called when the file system is umounted. */
static void
jffs_put_super(struct super_block *sb)
{
struct jffs_control *c = (struct jffs_control *) sb->s_fs_info;
D2(printk("jffs_put_super()\n"));
#ifdef CONFIG_JFFS_PROC_FS
jffs_unregister_jffs_proc_dir(c);
#endif
if (c->gc_task) {
D1(printk (KERN_NOTICE "jffs_put_super(): Telling gc thread to die.\n"));
send_sig(SIGKILL, c->gc_task, 1);
}
wait_for_completion(&c->gc_thread_comp);
D1(printk (KERN_NOTICE "jffs_put_super(): Successfully waited on thread.\n"));
jffs_cleanup_control((struct jffs_control *)sb->s_fs_info);
D1(printk(KERN_NOTICE "JFFS: Successfully unmounted device %s.\n",
sb->s_id));
}
/* This function is called when user commands like chmod, chgrp and
chown are executed. System calls like trunc() results in a call
to this function. */
static int
jffs_setattr(struct dentry *dentry, struct iattr *iattr)
{
struct inode *inode = dentry->d_inode;
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_fmcontrol *fmc;
struct jffs_file *f;
struct jffs_node *new_node;
int update_all;
int res = 0;
int recoverable = 0;
lock_kernel();
if ((res = inode_change_ok(inode, iattr)))
goto out;
c = (struct jffs_control *)inode->i_sb->s_fs_info;
fmc = c->fmc;
D3(printk (KERN_NOTICE "notify_change(): down biglock\n"));
mutex_lock(&fmc->biglock);
f = jffs_find_file(c, inode->i_ino);
ASSERT(if (!f) {
printk("jffs_setattr(): Invalid inode number: %lu\n",
inode->i_ino);
D3(printk (KERN_NOTICE "notify_change(): up biglock\n"));
mutex_unlock(&fmc->biglock);
res = -EINVAL;
goto out;
});
D1(printk("***jffs_setattr(): file: \"%s\", ino: %u\n",
f->name, f->ino));
update_all = iattr->ia_valid & ATTR_FORCE;
if ( (update_all || iattr->ia_valid & ATTR_SIZE)
&& (iattr->ia_size + 128 < f->size) ) {
/* We're shrinking the file by more than 128 bytes.
We'll be able to GC and recover this space, so
allow it to go into the reserved space. */
recoverable = 1;
}
if (!(new_node = jffs_alloc_node())) {
D(printk("jffs_setattr(): Allocation failed!\n"));
D3(printk (KERN_NOTICE "notify_change(): up biglock\n"));
mutex_unlock(&fmc->biglock);
res = -ENOMEM;
goto out;
}
new_node->data_offset = 0;
new_node->removed_size = 0;
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = f->ino;
raw_inode.pino = f->pino;
raw_inode.mode = f->mode;
raw_inode.uid = f->uid;
raw_inode.gid = f->gid;
raw_inode.atime = f->atime;
raw_inode.mtime = f->mtime;
raw_inode.ctime = f->ctime;
raw_inode.dsize = 0;
raw_inode.offset = 0;
raw_inode.rsize = 0;
raw_inode.dsize = 0;
raw_inode.nsize = f->nsize;
raw_inode.nlink = f->nlink;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
if (update_all || iattr->ia_valid & ATTR_MODE) {
raw_inode.mode = iattr->ia_mode;
inode->i_mode = iattr->ia_mode;
}
if (update_all || iattr->ia_valid & ATTR_UID) {
raw_inode.uid = iattr->ia_uid;
inode->i_uid = iattr->ia_uid;
}
if (update_all || iattr->ia_valid & ATTR_GID) {
raw_inode.gid = iattr->ia_gid;
inode->i_gid = iattr->ia_gid;
}
if (update_all || iattr->ia_valid & ATTR_SIZE) {
int len;
D1(printk("jffs_notify_change(): Changing size "
"to %lu bytes!\n", (long)iattr->ia_size));
raw_inode.offset = iattr->ia_size;
/* Calculate how many bytes need to be removed from
the end. */
if (f->size < iattr->ia_size) {
len = 0;
}
else {
len = f->size - iattr->ia_size;
}
raw_inode.rsize = len;
/* The updated node will be a removal node, with
base at the new size and size of the nbr of bytes
to be removed. */
new_node->data_offset = iattr->ia_size;
new_node->removed_size = len;
inode->i_size = iattr->ia_size;
inode->i_blocks = (inode->i_size + 511) >> 9;
if (len) {
invalidate_mapping_pages(inode->i_mapping, 0, -1);
}
inode->i_ctime = CURRENT_TIME_SEC;
inode->i_mtime = inode->i_ctime;
}
if (update_all || iattr->ia_valid & ATTR_ATIME) {
raw_inode.atime = iattr->ia_atime.tv_sec;
inode->i_atime = iattr->ia_atime;
}
if (update_all || iattr->ia_valid & ATTR_MTIME) {
raw_inode.mtime = iattr->ia_mtime.tv_sec;
inode->i_mtime = iattr->ia_mtime;
}
if (update_all || iattr->ia_valid & ATTR_CTIME) {
raw_inode.ctime = iattr->ia_ctime.tv_sec;
inode->i_ctime = iattr->ia_ctime;
}
/* Write this node to the flash. */
if ((res = jffs_write_node(c, new_node, &raw_inode, f->name, NULL, recoverable, f)) < 0) {
D(printk("jffs_notify_change(): The write failed!\n"));
jffs_free_node(new_node);
D3(printk (KERN_NOTICE "n_c(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
goto out;
}
jffs_insert_node(c, f, &raw_inode, NULL, new_node);
mark_inode_dirty(inode);
D3(printk (KERN_NOTICE "n_c(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
out:
unlock_kernel();
return res;
} /* jffs_notify_change() */
static struct inode *
jffs_new_inode(const struct inode * dir, struct jffs_raw_inode *raw_inode,
int * err)
{
struct super_block * sb;
struct inode * inode;
struct jffs_control *c;
struct jffs_file *f;
sb = dir->i_sb;
inode = new_inode(sb);
if (!inode) {
*err = -ENOMEM;
return NULL;
}
c = (struct jffs_control *)sb->s_fs_info;
inode->i_ino = raw_inode->ino;
inode->i_mode = raw_inode->mode;
inode->i_nlink = raw_inode->nlink;
inode->i_uid = raw_inode->uid;
inode->i_gid = raw_inode->gid;
inode->i_size = raw_inode->dsize;
inode->i_atime.tv_sec = raw_inode->atime;
inode->i_mtime.tv_sec = raw_inode->mtime;
inode->i_ctime.tv_sec = raw_inode->ctime;
inode->i_ctime.tv_nsec = 0;
inode->i_mtime.tv_nsec = 0;
inode->i_atime.tv_nsec = 0;
inode->i_blocks = (inode->i_size + 511) >> 9;
f = jffs_find_file(c, raw_inode->ino);
inode->i_private = (void *)f;
insert_inode_hash(inode);
return inode;
}
/* Get statistics of the file system. */
static int
jffs_statfs(struct dentry *dentry, struct kstatfs *buf)
{
struct jffs_control *c = (struct jffs_control *) dentry->d_sb->s_fs_info;
struct jffs_fmcontrol *fmc;
lock_kernel();
fmc = c->fmc;
D2(printk("jffs_statfs()\n"));
buf->f_type = JFFS_MAGIC_SB_BITMASK;
buf->f_bsize = PAGE_CACHE_SIZE;
buf->f_blocks = (fmc->flash_size / PAGE_CACHE_SIZE)
- (fmc->min_free_size / PAGE_CACHE_SIZE);
buf->f_bfree = (jffs_free_size1(fmc) + jffs_free_size2(fmc) +
fmc->dirty_size - fmc->min_free_size)
>> PAGE_CACHE_SHIFT;
buf->f_bavail = buf->f_bfree;
/* Find out how many files there are in the filesystem. */
buf->f_files = jffs_foreach_file(c, jffs_file_count);
buf->f_ffree = buf->f_bfree;
/* buf->f_fsid = 0; */
buf->f_namelen = JFFS_MAX_NAME_LEN;
unlock_kernel();
return 0;
}
/* Rename a file. */
static int
jffs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_file *old_dir_f;
struct jffs_file *new_dir_f;
struct jffs_file *del_f;
struct jffs_file *f;
struct jffs_node *node;
struct inode *inode;
int result = 0;
__u32 rename_data = 0;
D2(printk("***jffs_rename()\n"));
D(printk("jffs_rename(): old_dir: 0x%p, old name: 0x%p, "
"new_dir: 0x%p, new name: 0x%p\n",
old_dir, old_dentry->d_name.name,
new_dir, new_dentry->d_name.name));
lock_kernel();
c = (struct jffs_control *)old_dir->i_sb->s_fs_info;
ASSERT(if (!c) {
printk(KERN_ERR "jffs_rename(): The old_dir inode "
"didn't have a reference to a jffs_file struct\n");
unlock_kernel();
return -EIO;
});
result = -ENOTDIR;
if (!(old_dir_f = old_dir->i_private)) {
D(printk("jffs_rename(): Old dir invalid.\n"));
goto jffs_rename_end;
}
/* Try to find the file to move. */
result = -ENOENT;
if (!(f = jffs_find_child(old_dir_f, old_dentry->d_name.name,
old_dentry->d_name.len))) {
goto jffs_rename_end;
}
/* Find the new directory. */
result = -ENOTDIR;
if (!(new_dir_f = new_dir->i_private)) {
D(printk("jffs_rename(): New dir invalid.\n"));
goto jffs_rename_end;
}
D3(printk (KERN_NOTICE "rename(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
/* Create a node and initialize as much as needed. */
result = -ENOMEM;
if (!(node = jffs_alloc_node())) {
D(printk("jffs_rename(): Allocation failed: node == 0\n"));
goto jffs_rename_end;
}
node->data_offset = 0;
node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = f->ino;
raw_inode.pino = new_dir_f->ino;
/* raw_inode.version = f->highest_version + 1; */
raw_inode.mode = f->mode;
raw_inode.uid = current->fsuid;
raw_inode.gid = current->fsgid;
#if 0
raw_inode.uid = f->uid;
raw_inode.gid = f->gid;
#endif
raw_inode.atime = get_seconds();
raw_inode.mtime = raw_inode.atime;
raw_inode.ctime = f->ctime;
raw_inode.offset = 0;
raw_inode.dsize = 0;
raw_inode.rsize = 0;
raw_inode.nsize = new_dentry->d_name.len;
raw_inode.nlink = f->nlink;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
/* See if there already exists a file with the same name as
new_name. */
if ((del_f = jffs_find_child(new_dir_f, new_dentry->d_name.name,
new_dentry->d_name.len))) {
raw_inode.rename = 1;
raw_inode.dsize = sizeof(__u32);
rename_data = del_f->ino;
}
/* Write the new node to the flash memory. */
if ((result = jffs_write_node(c, node, &raw_inode,
new_dentry->d_name.name,
(unsigned char*)&rename_data, 0, f)) < 0) {
D(printk("jffs_rename(): Failed to write node to flash.\n"));
jffs_free_node(node);
goto jffs_rename_end;
}
raw_inode.dsize = 0;
if (raw_inode.rename) {
/* The file with the same name must be deleted. */
//FIXME deadlock down(&c->fmc->gclock);
if ((result = jffs_remove(new_dir, new_dentry,
del_f->mode)) < 0) {
/* This is really bad. */
printk(KERN_ERR "JFFS: An error occurred in "
"rename().\n");
}
// up(&c->fmc->gclock);
}
if (old_dir_f != new_dir_f) {
/* Remove the file from its old position in the
filesystem tree. */
jffs_unlink_file_from_tree(f);
}
/* Insert the new node into the file system. */
if ((result = jffs_insert_node(c, f, &raw_inode,
new_dentry->d_name.name, node)) < 0) {
D(printk(KERN_ERR "jffs_rename(): jffs_insert_node() "
"failed!\n"));
}
if (old_dir_f != new_dir_f) {
/* Insert the file to its new position in the
file system. */
jffs_insert_file_into_tree(f);
}
/* This is a kind of update of the inode we're about to make
here. This is what they do in ext2fs. Kind of. */
if ((inode = iget(new_dir->i_sb, f->ino))) {
inode->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
iput(inode);
}
jffs_rename_end:
D3(printk (KERN_NOTICE "rename(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return result;
} /* jffs_rename() */
/* Read the contents of a directory. Used by programs like `ls'
for instance. */
static int
jffs_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
struct jffs_file *f;
struct dentry *dentry = filp->f_path.dentry;
struct inode *inode = dentry->d_inode;
struct jffs_control *c = (struct jffs_control *)inode->i_sb->s_fs_info;
int j;
int ddino;
lock_kernel();
D3(printk (KERN_NOTICE "readdir(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
D2(printk("jffs_readdir(): inode: 0x%p, filp: 0x%p\n", inode, filp));
if (filp->f_pos == 0) {
D3(printk("jffs_readdir(): \".\" %lu\n", inode->i_ino));
if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0) {
D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return 0;
}
filp->f_pos = 1;
}
if (filp->f_pos == 1) {
if (inode->i_ino == JFFS_MIN_INO) {
ddino = JFFS_MIN_INO;
}
else {
ddino = ((struct jffs_file *)
inode->i_private)->pino;
}
D3(printk("jffs_readdir(): \"..\" %u\n", ddino));
if (filldir(dirent, "..", 2, filp->f_pos, ddino, DT_DIR) < 0) {
D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return 0;
}
filp->f_pos++;
}
f = ((struct jffs_file *)inode->i_private)->children;
j = 2;
while(f && (f->deleted || j++ < filp->f_pos )) {
f = f->sibling_next;
}
while (f) {
D3(printk("jffs_readdir(): \"%s\" ino: %u\n",
(f->name ? f->name : ""), f->ino));
if (filldir(dirent, f->name, f->nsize,
filp->f_pos , f->ino, DT_UNKNOWN) < 0) {
D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return 0;
}
filp->f_pos++;
do {
f = f->sibling_next;
} while(f && f->deleted);
}
D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return filp->f_pos;
} /* jffs_readdir() */
/* Find a file in a directory. If the file exists, return its
corresponding dentry. */
static struct dentry *
jffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
{
struct jffs_file *d;
struct jffs_file *f;
struct jffs_control *c = (struct jffs_control *)dir->i_sb->s_fs_info;
int len;
int r = 0;
const char *name;
struct inode *inode = NULL;
len = dentry->d_name.len;
name = dentry->d_name.name;
lock_kernel();
D3({
char *s = kmalloc(len + 1, GFP_KERNEL);
memcpy(s, name, len);
s[len] = '\0';
printk("jffs_lookup(): dir: 0x%p, name: \"%s\"\n", dir, s);
kfree(s);
});
D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
r = -ENAMETOOLONG;
if (len > JFFS_MAX_NAME_LEN) {
goto jffs_lookup_end;
}
r = -EACCES;
if (!(d = (struct jffs_file *)dir->i_private)) {
D(printk("jffs_lookup(): No such inode! (%lu)\n",
dir->i_ino));
goto jffs_lookup_end;
}
/* Get the corresponding inode to the file. */
/* iget calls jffs_read_inode, so we need to drop the biglock
before calling iget. Unfortunately, the GC has a tendency
to sneak in here, because iget sometimes calls schedule ().
*/
if ((len == 1) && (name[0] == '.')) {
D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
if (!(inode = iget(dir->i_sb, d->ino))) {
D(printk("jffs_lookup(): . iget() ==> NULL\n"));
goto jffs_lookup_end_no_biglock;
}
D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
} else if ((len == 2) && (name[0] == '.') && (name[1] == '.')) {
D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
if (!(inode = iget(dir->i_sb, d->pino))) {
D(printk("jffs_lookup(): .. iget() ==> NULL\n"));
goto jffs_lookup_end_no_biglock;
}
D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
} else if ((f = jffs_find_child(d, name, len))) {
D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
if (!(inode = iget(dir->i_sb, f->ino))) {
D(printk("jffs_lookup(): iget() ==> NULL\n"));
goto jffs_lookup_end_no_biglock;
}
D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
} else {
D3(printk("jffs_lookup(): Couldn't find the file. "
"f = 0x%p, name = \"%s\", d = 0x%p, d->ino = %u\n",
f, name, d, d->ino));
inode = NULL;
}
d_add(dentry, inode);
D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return NULL;
jffs_lookup_end:
D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
jffs_lookup_end_no_biglock:
unlock_kernel();
return ERR_PTR(r);
} /* jffs_lookup() */
/* Try to read a page of data from a file. */
static int
jffs_do_readpage_nolock(struct file *file, struct page *page)
{
void *buf;
unsigned long read_len;
int result;
struct inode *inode = (struct inode*)page->mapping->host;
struct jffs_file *f = (struct jffs_file *)inode->i_private;
struct jffs_control *c = (struct jffs_control *)inode->i_sb->s_fs_info;
int r;
loff_t offset;
D2(printk("***jffs_readpage(): file = \"%s\", page->index = %lu\n",
(f->name ? f->name : ""), (long)page->index));
get_page(page);
/* Don't SetPageLocked(page), should be locked already */
ClearPageUptodate(page);
ClearPageError(page);
D3(printk (KERN_NOTICE "readpage(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
read_len = 0;
result = 0;
offset = page_offset(page);
kmap(page);
buf = page_address(page);
if (offset < inode->i_size) {
read_len = min_t(long, inode->i_size - offset, PAGE_SIZE);
r = jffs_read_data(f, buf, offset, read_len);
if (r != read_len) {
result = -EIO;
D(
printk("***jffs_readpage(): Read error! "
"Wanted to read %lu bytes but only "
"read %d bytes.\n", read_len, r);
);
}
}
/* This handles the case of partial or no read in above */
if(read_len < PAGE_SIZE)
memset(buf + read_len, 0, PAGE_SIZE - read_len);
flush_dcache_page(page);
kunmap(page);
D3(printk (KERN_NOTICE "readpage(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
if (result) {
SetPageError(page);
}else {
SetPageUptodate(page);
}
page_cache_release(page);
D3(printk("jffs_readpage(): Leaving...\n"));
return result;
} /* jffs_do_readpage_nolock() */
static int jffs_readpage(struct file *file, struct page *page)
{
int ret = jffs_do_readpage_nolock(file, page);
unlock_page(page);
return ret;
}
/* Create a new directory. */
static int
jffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_node *node;
struct jffs_file *dir_f;
struct inode *inode;
int dir_mode;
int result = 0;
int err;
D1({
int len = dentry->d_name.len;
char *_name = kmalloc(len + 1, GFP_KERNEL);
memcpy(_name, dentry->d_name.name, len);
_name[len] = '\0';
printk("***jffs_mkdir(): dir = 0x%p, name = \"%s\", "
"len = %d, mode = 0x%08x\n", dir, _name, len, mode);
kfree(_name);
});
lock_kernel();
dir_f = dir->i_private;
ASSERT(if (!dir_f) {
printk(KERN_ERR "jffs_mkdir(): No reference to a "
"jffs_file struct in inode.\n");
unlock_kernel();
return -EIO;
});
c = dir_f->c;
D3(printk (KERN_NOTICE "mkdir(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
dir_mode = S_IFDIR | (mode & (S_IRWXUGO|S_ISVTX)
& ~current->fs->umask);
if (dir->i_mode & S_ISGID) {
dir_mode |= S_ISGID;
}
/* Create a node and initialize it as much as needed. */
if (!(node = jffs_alloc_node())) {
D(printk("jffs_mkdir(): Allocation failed: node == 0\n"));
result = -ENOMEM;
goto jffs_mkdir_end;
}
node->data_offset = 0;
node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = c->next_ino++;
raw_inode.pino = dir_f->ino;
raw_inode.version = 1;
raw_inode.mode = dir_mode;
raw_inode.uid = current->fsuid;
raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
/* raw_inode.gid = current->fsgid; */
raw_inode.atime = get_seconds();
raw_inode.mtime = raw_inode.atime;
raw_inode.ctime = raw_inode.atime;
raw_inode.offset = 0;
raw_inode.dsize = 0;
raw_inode.rsize = 0;
raw_inode.nsize = dentry->d_name.len;
raw_inode.nlink = 1;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
/* Write the new node to the flash. */
if ((result = jffs_write_node(c, node, &raw_inode,
dentry->d_name.name, NULL, 0, NULL)) < 0) {
D(printk("jffs_mkdir(): jffs_write_node() failed.\n"));
jffs_free_node(node);
goto jffs_mkdir_end;
}
/* Insert the new node into the file system. */
if ((result = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
node)) < 0) {
goto jffs_mkdir_end;
}
inode = jffs_new_inode(dir, &raw_inode, &err);
if (inode == NULL) {
result = err;
goto jffs_mkdir_end;
}
inode->i_op = &jffs_dir_inode_operations;
inode->i_fop = &jffs_dir_operations;
mark_inode_dirty(dir);
d_instantiate(dentry, inode);
result = 0;
jffs_mkdir_end:
D3(printk (KERN_NOTICE "mkdir(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return result;
} /* jffs_mkdir() */
/* Remove a directory. */
static int
jffs_rmdir(struct inode *dir, struct dentry *dentry)
{
struct jffs_control *c = (struct jffs_control *)dir->i_sb->s_fs_info;
int ret;
D3(printk("***jffs_rmdir()\n"));
D3(printk (KERN_NOTICE "rmdir(): down biglock\n"));
lock_kernel();
mutex_lock(&c->fmc->biglock);
ret = jffs_remove(dir, dentry, S_IFDIR);
D3(printk (KERN_NOTICE "rmdir(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return ret;
}
/* Remove any kind of file except for directories. */
static int
jffs_unlink(struct inode *dir, struct dentry *dentry)
{
struct jffs_control *c = (struct jffs_control *)dir->i_sb->s_fs_info;
int ret;
lock_kernel();
D3(printk("***jffs_unlink()\n"));
D3(printk (KERN_NOTICE "unlink(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
ret = jffs_remove(dir, dentry, 0);
D3(printk (KERN_NOTICE "unlink(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return ret;
}
/* Remove a JFFS entry, i.e. plain files, directories, etc. Here we
shouldn't test for free space on the device. */
static int
jffs_remove(struct inode *dir, struct dentry *dentry, int type)
{
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_file *dir_f; /* The file-to-remove's parent. */
struct jffs_file *del_f; /* The file to remove. */
struct jffs_node *del_node;
struct inode *inode = NULL;
int result = 0;
D1({
int len = dentry->d_name.len;
const char *name = dentry->d_name.name;
char *_name = kmalloc(len + 1, GFP_KERNEL);
memcpy(_name, name, len);
_name[len] = '\0';
printk("***jffs_remove(): file = \"%s\", ino = %ld\n", _name, dentry->d_inode->i_ino);
kfree(_name);
});
dir_f = dir->i_private;
c = dir_f->c;
result = -ENOENT;
if (!(del_f = jffs_find_child(dir_f, dentry->d_name.name,
dentry->d_name.len))) {
D(printk("jffs_remove(): jffs_find_child() failed.\n"));
goto jffs_remove_end;
}
if (S_ISDIR(type)) {
struct jffs_file *child = del_f->children;
while(child) {
if( !child->deleted ) {
result = -ENOTEMPTY;
goto jffs_remove_end;
}
child = child->sibling_next;
}
}
else if (S_ISDIR(del_f->mode)) {
D(printk("jffs_remove(): node is a directory "
"but it shouldn't be.\n"));
result = -EPERM;
goto jffs_remove_end;
}
inode = dentry->d_inode;
result = -EIO;
if (del_f->ino != inode->i_ino)
goto jffs_remove_end;
if (!inode->i_nlink) {
printk("Deleting nonexistent file inode: %lu, nlink: %d\n",
inode->i_ino, inode->i_nlink);
inode->i_nlink=1;
}
/* Create a node for the deletion. */
result = -ENOMEM;
if (!(del_node = jffs_alloc_node())) {
D(printk("jffs_remove(): Allocation failed!\n"));
goto jffs_remove_end;
}
del_node->data_offset = 0;
del_node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = del_f->ino;
raw_inode.pino = del_f->pino;
/* raw_inode.version = del_f->highest_version + 1; */
raw_inode.mode = del_f->mode;
raw_inode.uid = current->fsuid;
raw_inode.gid = current->fsgid;
raw_inode.atime = get_seconds();
raw_inode.mtime = del_f->mtime;
raw_inode.ctime = raw_inode.atime;
raw_inode.offset = 0;
raw_inode.dsize = 0;
raw_inode.rsize = 0;
raw_inode.nsize = 0;
raw_inode.nlink = del_f->nlink;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 1;
/* Write the new node to the flash memory. */
if (jffs_write_node(c, del_node, &raw_inode, NULL, NULL, 1, del_f) < 0) {
jffs_free_node(del_node);
result = -EIO;
goto jffs_remove_end;
}
/* Update the file. This operation will make the file disappear
from the in-memory file system structures. */
jffs_insert_node(c, del_f, &raw_inode, NULL, del_node);
dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
mark_inode_dirty(dir);
inode->i_ctime = dir->i_ctime;
inode_dec_link_count(inode);
d_delete(dentry); /* This also frees the inode */
result = 0;
jffs_remove_end:
return result;
} /* jffs_remove() */
static int
jffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
{
struct jffs_raw_inode raw_inode;
struct jffs_file *dir_f;
struct jffs_node *node = NULL;
struct jffs_control *c;
struct inode *inode;
int result = 0;
u16 data = old_encode_dev(rdev);
int err;
D1(printk("***jffs_mknod()\n"));
if (!old_valid_dev(rdev))
return -EINVAL;
lock_kernel();
dir_f = dir->i_private;
c = dir_f->c;
D3(printk (KERN_NOTICE "mknod(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
/* Create and initialize a new node. */
if (!(node = jffs_alloc_node())) {
D(printk("jffs_mknod(): Allocation failed!\n"));
result = -ENOMEM;
goto jffs_mknod_err;
}
node->data_offset = 0;
node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = c->next_ino++;
raw_inode.pino = dir_f->ino;
raw_inode.version = 1;
raw_inode.mode = mode;
raw_inode.uid = current->fsuid;
raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
/* raw_inode.gid = current->fsgid; */
raw_inode.atime = get_seconds();
raw_inode.mtime = raw_inode.atime;
raw_inode.ctime = raw_inode.atime;
raw_inode.offset = 0;
raw_inode.dsize = 2;
raw_inode.rsize = 0;
raw_inode.nsize = dentry->d_name.len;
raw_inode.nlink = 1;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
/* Write the new node to the flash. */
if ((err = jffs_write_node(c, node, &raw_inode, dentry->d_name.name,
(unsigned char *)&data, 0, NULL)) < 0) {
D(printk("jffs_mknod(): jffs_write_node() failed.\n"));
result = err;
goto jffs_mknod_err;
}
/* Insert the new node into the file system. */
if ((err = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
node)) < 0) {
result = err;
goto jffs_mknod_end;
}
inode = jffs_new_inode(dir, &raw_inode, &err);
if (inode == NULL) {
result = err;
goto jffs_mknod_end;
}
init_special_inode(inode, mode, rdev);
d_instantiate(dentry, inode);
goto jffs_mknod_end;
jffs_mknod_err:
if (node) {
jffs_free_node(node);
}
jffs_mknod_end:
D3(printk (KERN_NOTICE "mknod(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return result;
} /* jffs_mknod() */
static int
jffs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
{
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_file *dir_f;
struct jffs_node *node;
struct inode *inode;
int symname_len = strlen(symname);
int err;
lock_kernel();
D1({
int len = dentry->d_name.len;
char *_name = kmalloc(len + 1, GFP_KERNEL);
char *_symname = kmalloc(symname_len + 1, GFP_KERNEL);
memcpy(_name, dentry->d_name.name, len);
_name[len] = '\0';
memcpy(_symname, symname, symname_len);
_symname[symname_len] = '\0';
printk("***jffs_symlink(): dir = 0x%p, "
"dentry->dname.name = \"%s\", "
"symname = \"%s\"\n", dir, _name, _symname);
kfree(_name);
kfree(_symname);
});
dir_f = dir->i_private;
ASSERT(if (!dir_f) {
printk(KERN_ERR "jffs_symlink(): No reference to a "
"jffs_file struct in inode.\n");
unlock_kernel();
return -EIO;
});
c = dir_f->c;
/* Create a node and initialize it as much as needed. */
if (!(node = jffs_alloc_node())) {
D(printk("jffs_symlink(): Allocation failed: node = NULL\n"));
unlock_kernel();
return -ENOMEM;
}
D3(printk (KERN_NOTICE "symlink(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
node->data_offset = 0;
node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = c->next_ino++;
raw_inode.pino = dir_f->ino;
raw_inode.version = 1;
raw_inode.mode = S_IFLNK | S_IRWXUGO;
raw_inode.uid = current->fsuid;
raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
raw_inode.atime = get_seconds();
raw_inode.mtime = raw_inode.atime;
raw_inode.ctime = raw_inode.atime;
raw_inode.offset = 0;
raw_inode.dsize = symname_len;
raw_inode.rsize = 0;
raw_inode.nsize = dentry->d_name.len;
raw_inode.nlink = 1;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
/* Write the new node to the flash. */
if ((err = jffs_write_node(c, node, &raw_inode, dentry->d_name.name,
(const unsigned char *)symname, 0, NULL)) < 0) {
D(printk("jffs_symlink(): jffs_write_node() failed.\n"));
jffs_free_node(node);
goto jffs_symlink_end;
}
/* Insert the new node into the file system. */
if ((err = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
node)) < 0) {
goto jffs_symlink_end;
}
inode = jffs_new_inode(dir, &raw_inode, &err);
if (inode == NULL) {
goto jffs_symlink_end;
}
err = 0;
inode->i_op = &page_symlink_inode_operations;
inode->i_mapping->a_ops = &jffs_address_operations;
d_instantiate(dentry, inode);
jffs_symlink_end:
D3(printk (KERN_NOTICE "symlink(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return err;
} /* jffs_symlink() */
/* Create an inode inside a JFFS directory (dir) and return it.
*
* By the time this is called, we already have created
* the directory cache entry for the new file, but it
* is so far negative - it has no inode.
*
* If the create succeeds, we fill in the inode information
* with d_instantiate().
*/
static int
jffs_create(struct inode *dir, struct dentry *dentry, int mode,
struct nameidata *nd)
{
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_node *node;
struct jffs_file *dir_f; /* JFFS representation of the directory. */
struct inode *inode;
int err;
lock_kernel();
D1({
int len = dentry->d_name.len;
char *s = kmalloc(len + 1, GFP_KERNEL);
memcpy(s, dentry->d_name.name, len);
s[len] = '\0';
printk("jffs_create(): dir: 0x%p, name: \"%s\"\n", dir, s);
kfree(s);
});
dir_f = dir->i_private;
ASSERT(if (!dir_f) {
printk(KERN_ERR "jffs_create(): No reference to a "
"jffs_file struct in inode.\n");
unlock_kernel();
return -EIO;
});
c = dir_f->c;
/* Create a node and initialize as much as needed. */
if (!(node = jffs_alloc_node())) {
D(printk("jffs_create(): Allocation failed: node == 0\n"));
unlock_kernel();
return -ENOMEM;
}
D3(printk (KERN_NOTICE "create(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
node->data_offset = 0;
node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = c->next_ino++;
raw_inode.pino = dir_f->ino;
raw_inode.version = 1;
raw_inode.mode = mode;
raw_inode.uid = current->fsuid;
raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
raw_inode.atime = get_seconds();
raw_inode.mtime = raw_inode.atime;
raw_inode.ctime = raw_inode.atime;
raw_inode.offset = 0;
raw_inode.dsize = 0;
raw_inode.rsize = 0;
raw_inode.nsize = dentry->d_name.len;
raw_inode.nlink = 1;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
/* Write the new node to the flash. */
if ((err = jffs_write_node(c, node, &raw_inode,
dentry->d_name.name, NULL, 0, NULL)) < 0) {
D(printk("jffs_create(): jffs_write_node() failed.\n"));
jffs_free_node(node);
goto jffs_create_end;
}
/* Insert the new node into the file system. */
if ((err = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
node)) < 0) {
goto jffs_create_end;
}
/* Initialize an inode. */
inode = jffs_new_inode(dir, &raw_inode, &err);
if (inode == NULL) {
goto jffs_create_end;
}
err = 0;
inode->i_op = &jffs_file_inode_operations;
inode->i_fop = &jffs_file_operations;
inode->i_mapping->a_ops = &jffs_address_operations;
inode->i_mapping->nrpages = 0;
d_instantiate(dentry, inode);
jffs_create_end:
D3(printk (KERN_NOTICE "create(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
unlock_kernel();
return err;
} /* jffs_create() */
/* Write, append or rewrite data to an existing file. */
static ssize_t
jffs_file_write(struct file *filp, const char *buf, size_t count,
loff_t *ppos)
{
struct jffs_raw_inode raw_inode;
struct jffs_control *c;
struct jffs_file *f;
struct jffs_node *node;
struct dentry *dentry = filp->f_path.dentry;
struct inode *inode = dentry->d_inode;
int recoverable = 0;
size_t written = 0;
__u32 thiscount = count;
loff_t pos = *ppos;
int err;
inode = filp->f_path.dentry->d_inode;
D2(printk("***jffs_file_write(): inode: 0x%p (ino: %lu), "
"filp: 0x%p, buf: 0x%p, count: %d\n",
inode, inode->i_ino, filp, buf, count));
#if 0
if (inode->i_sb->s_flags & MS_RDONLY) {
D(printk("jffs_file_write(): MS_RDONLY\n"));
err = -EROFS;
goto out_isem;
}
#endif
err = -EINVAL;
if (!S_ISREG(inode->i_mode)) {
D(printk("jffs_file_write(): inode->i_mode == 0x%08x\n",
inode->i_mode));
goto out_isem;
}
if (!(f = inode->i_private)) {
D(printk("jffs_file_write(): inode->i_private = 0x%p\n",
inode->i_private));
goto out_isem;
}
c = f->c;
/*
* This will never trigger with sane page sizes. leave it in
* anyway, since I'm thinking about how to merge larger writes
* (the current idea is to poke a thread that does the actual
* I/O and starts by doing a mutex_lock(&inode->i_mutex). then we
* would need to get the page cache pages and have a list of
* I/O requests and do write-merging here.
* -- prumpf
*/
thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
D3(printk (KERN_NOTICE "file_write(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
/* Urgh. POSIX says we can do short writes if we feel like it.
* In practice, we can't. Nothing will cope. So we loop until
* we're done.
*
* <_Anarchy_> posix and reality are not interconnected on this issue
*/
while (count) {
/* Things are going to be written so we could allocate and
initialize the necessary data structures now. */
if (!(node = jffs_alloc_node())) {
D(printk("jffs_file_write(): node == 0\n"));
err = -ENOMEM;
goto out;
}
node->data_offset = pos;
node->removed_size = 0;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = f->ino;
raw_inode.pino = f->pino;
raw_inode.mode = f->mode;
raw_inode.uid = f->uid;
raw_inode.gid = f->gid;
raw_inode.atime = get_seconds();
raw_inode.mtime = raw_inode.atime;
raw_inode.ctime = f->ctime;
raw_inode.offset = pos;
raw_inode.dsize = thiscount;
raw_inode.rsize = 0;
raw_inode.nsize = f->nsize;
raw_inode.nlink = f->nlink;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = 0;
if (pos < f->size) {
node->removed_size = raw_inode.rsize = min(thiscount, (__u32)(f->size - pos));
/* If this node is going entirely over the top of old data,
we can allow it to go into the reserved space, because
we know that GC can reclaim the space later.
*/
if (pos + thiscount < f->size) {
/* If all the data we're overwriting are _real_,
not just holes, then:
recoverable = 1;
*/
}
}
/* Write the new node to the flash. */
/* NOTE: We would be quite happy if jffs_write_node() wrote a
smaller node than we were expecting. There's no need for it
to waste the space at the end of the flash just because it's
a little smaller than what we asked for. But that's a whole
new can of worms which I'm not going to open this week.
-- dwmw2.
*/
if ((err = jffs_write_node(c, node, &raw_inode, f->name,
(const unsigned char *)buf,
recoverable, f)) < 0) {
D(printk("jffs_file_write(): jffs_write_node() failed.\n"));
jffs_free_node(node);
goto out;
}
written += err;
buf += err;
count -= err;
pos += err;
/* Insert the new node into the file system. */
if ((err = jffs_insert_node(c, f, &raw_inode, NULL, node)) < 0) {
goto out;
}
D3(printk("jffs_file_write(): new f_pos %ld.\n", (long)pos));
thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
}
out:
D3(printk (KERN_NOTICE "file_write(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
/* Fix things in the real inode. */
if (pos > inode->i_size) {
inode->i_size = pos;
inode->i_blocks = (inode->i_size + 511) >> 9;
}
inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
invalidate_mapping_pages(inode->i_mapping, 0, -1);
out_isem:
return err;
} /* jffs_file_write() */
static int
jffs_prepare_write(struct file *filp, struct page *page,
unsigned from, unsigned to)
{
/* FIXME: we should detect some error conditions here */
/* Bugger that. We should make sure the page is uptodate */
if (!PageUptodate(page) && (from || to < PAGE_CACHE_SIZE))
return jffs_do_readpage_nolock(filp, page);
return 0;
} /* jffs_prepare_write() */
static int
jffs_commit_write(struct file *filp, struct page *page,
unsigned from, unsigned to)
{
void *addr = page_address(page) + from;
/* XXX: PAGE_CACHE_SHIFT or PAGE_SHIFT */
loff_t pos = page_offset(page) + from;
return jffs_file_write(filp, addr, to-from, &pos);
} /* jffs_commit_write() */
/* This is our ioctl() routine. */
static int
jffs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
unsigned long arg)
{
struct jffs_control *c;
int ret = 0;
D2(printk("***jffs_ioctl(): cmd = 0x%08x, arg = 0x%08lx\n",
cmd, arg));
if (!(c = (struct jffs_control *)inode->i_sb->s_fs_info)) {
printk(KERN_ERR "JFFS: Bad inode in ioctl() call. "
"(cmd = 0x%08x)\n", cmd);
return -EIO;
}
D3(printk (KERN_NOTICE "ioctl(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
switch (cmd) {
case JFFS_PRINT_HASH:
jffs_print_hash_table(c);
break;
case JFFS_PRINT_TREE:
jffs_print_tree(c->root, 0);
break;
case JFFS_GET_STATUS:
{
struct jffs_flash_status fst;
struct jffs_fmcontrol *fmc = c->fmc;
printk("Flash status -- ");
if (!access_ok(VERIFY_WRITE,
(struct jffs_flash_status __user *)arg,
sizeof(struct jffs_flash_status))) {
D(printk("jffs_ioctl(): Bad arg in "
"JFFS_GET_STATUS ioctl!\n"));
ret = -EFAULT;
break;
}
fst.size = fmc->flash_size;
fst.used = fmc->used_size;
fst.dirty = fmc->dirty_size;
fst.begin = fmc->head->offset;
fst.end = fmc->tail->offset + fmc->tail->size;
printk("size: %d, used: %d, dirty: %d, "
"begin: %d, end: %d\n",
fst.size, fst.used, fst.dirty,
fst.begin, fst.end);
if (copy_to_user((struct jffs_flash_status __user *)arg,
&fst,
sizeof(struct jffs_flash_status))) {
ret = -EFAULT;
}
}
break;
default:
ret = -ENOTTY;
}
D3(printk (KERN_NOTICE "ioctl(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
return ret;
} /* jffs_ioctl() */
static const struct address_space_operations jffs_address_operations = {
.readpage = jffs_readpage,
.prepare_write = jffs_prepare_write,
.commit_write = jffs_commit_write,
};
static int jffs_fsync(struct file *f, struct dentry *d, int datasync)
{
/* We currently have O_SYNC operations at all times.
Do nothing.
*/
return 0;
}
static const struct file_operations jffs_file_operations =
{
.open = generic_file_open,
.llseek = generic_file_llseek,
.read = do_sync_read,
.aio_read = generic_file_aio_read,
.write = do_sync_write,
.aio_write = generic_file_aio_write,
.ioctl = jffs_ioctl,
.mmap = generic_file_readonly_mmap,
.fsync = jffs_fsync,
.sendfile = generic_file_sendfile,
};
static const struct inode_operations jffs_file_inode_operations =
{
.lookup = jffs_lookup, /* lookup */
.setattr = jffs_setattr,
};
static const struct file_operations jffs_dir_operations =
{
.readdir = jffs_readdir,
};
static const struct inode_operations jffs_dir_inode_operations =
{
.create = jffs_create,
.lookup = jffs_lookup,
.unlink = jffs_unlink,
.symlink = jffs_symlink,
.mkdir = jffs_mkdir,
.rmdir = jffs_rmdir,
.mknod = jffs_mknod,
.rename = jffs_rename,
.setattr = jffs_setattr,
};
/* Initialize an inode for the VFS. */
static void
jffs_read_inode(struct inode *inode)
{
struct jffs_file *f;
struct jffs_control *c;
D3(printk("jffs_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
if (!inode->i_sb) {
D(printk("jffs_read_inode(): !inode->i_sb ==> "
"No super block!\n"));
return;
}
c = (struct jffs_control *)inode->i_sb->s_fs_info;
D3(printk (KERN_NOTICE "read_inode(): down biglock\n"));
mutex_lock(&c->fmc->biglock);
if (!(f = jffs_find_file(c, inode->i_ino))) {
D(printk("jffs_read_inode(): No such inode (%lu).\n",
inode->i_ino));
D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
return;
}
inode->i_private = f;
inode->i_mode = f->mode;
inode->i_nlink = f->nlink;
inode->i_uid = f->uid;
inode->i_gid = f->gid;
inode->i_size = f->size;
inode->i_atime.tv_sec = f->atime;
inode->i_mtime.tv_sec = f->mtime;
inode->i_ctime.tv_sec = f->ctime;
inode->i_atime.tv_nsec =
inode->i_mtime.tv_nsec =
inode->i_ctime.tv_nsec = 0;
inode->i_blocks = (inode->i_size + 511) >> 9;
if (S_ISREG(inode->i_mode)) {
inode->i_op = &jffs_file_inode_operations;
inode->i_fop = &jffs_file_operations;
inode->i_mapping->a_ops = &jffs_address_operations;
}
else if (S_ISDIR(inode->i_mode)) {
inode->i_op = &jffs_dir_inode_operations;
inode->i_fop = &jffs_dir_operations;
}
else if (S_ISLNK(inode->i_mode)) {
inode->i_op = &page_symlink_inode_operations;
inode->i_mapping->a_ops = &jffs_address_operations;
}
else {
/* If the node is a device of some sort, then the number of
the device should be read from the flash memory and then
added to the inode's i_rdev member. */
u16 val;
jffs_read_data(f, (char *)&val, 0, 2);
init_special_inode(inode, inode->i_mode,
old_decode_dev(val));
}
D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
mutex_unlock(&c->fmc->biglock);
}
static void
jffs_delete_inode(struct inode *inode)
{
struct jffs_file *f;
struct jffs_control *c;
D3(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
inode->i_ino));
truncate_inode_pages(&inode->i_data, 0);
lock_kernel();
inode->i_size = 0;
inode->i_blocks = 0;
inode->i_private = NULL;
clear_inode(inode);
if (inode->i_nlink == 0) {
c = (struct jffs_control *) inode->i_sb->s_fs_info;
f = (struct jffs_file *) jffs_find_file (c, inode->i_ino);
jffs_possibly_delete_file(f);
}
unlock_kernel();
}
static void
jffs_write_super(struct super_block *sb)
{
struct jffs_control *c = (struct jffs_control *)sb->s_fs_info;
lock_kernel();
jffs_garbage_collect_trigger(c);
unlock_kernel();
}
static int jffs_remount(struct super_block *sb, int *flags, char *data)
{
*flags |= MS_NODIRATIME;
return 0;
}
static const struct super_operations jffs_ops =
{
.read_inode = jffs_read_inode,
.delete_inode = jffs_delete_inode,
.put_super = jffs_put_super,
.write_super = jffs_write_super,
.statfs = jffs_statfs,
.remount_fs = jffs_remount,
};
static int jffs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data, jffs_fill_super,
mnt);
}
static struct file_system_type jffs_fs_type = {
.owner = THIS_MODULE,
.name = "jffs",
.get_sb = jffs_get_sb,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
static int __init
init_jffs_fs(void)
{
printk(KERN_INFO "JFFS version " JFFS_VERSION_STRING
", (C) 1999, 2000 Axis Communications AB\n");
#ifdef CONFIG_JFFS_PROC_FS
jffs_proc_root = proc_mkdir("jffs", proc_root_fs);
if (!jffs_proc_root) {
printk(KERN_WARNING "cannot create /proc/jffs entry\n");
}
#endif
fm_cache = kmem_cache_create("jffs_fm", sizeof(struct jffs_fm),
0,
SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
NULL, NULL);
if (!fm_cache) {
return -ENOMEM;
}
node_cache = kmem_cache_create("jffs_node",sizeof(struct jffs_node),
0,
SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
NULL, NULL);
if (!node_cache) {
kmem_cache_destroy(fm_cache);
return -ENOMEM;
}
return register_filesystem(&jffs_fs_type);
}
static void __exit
exit_jffs_fs(void)
{
unregister_filesystem(&jffs_fs_type);
kmem_cache_destroy(fm_cache);
kmem_cache_destroy(node_cache);
}
module_init(init_jffs_fs)
module_exit(exit_jffs_fs)
MODULE_DESCRIPTION("The Journalling Flash File System");
MODULE_AUTHOR("Axis Communications AB.");
MODULE_LICENSE("GPL");
/*
* JFFS -- Journaling Flash File System, Linux implementation.
*
* Copyright (C) 1999, 2000 Axis Communications, Inc.
*
* Created by Finn Hakansson <finn@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: intrep.c,v 1.102 2001/09/23 23:28:36 dwmw2 Exp $
*
* Ported to Linux 2.3.x and MTD:
* Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
*
*/
/* This file contains the code for the internal structure of the
Journaling Flash File System, JFFS. */
/*
* Todo list:
*
* memcpy_to_flash() and memcpy_from_flash() functions.
*
* Implementation of hard links.
*
* Organize the source code in a better way. Against the VFS we could
* have jffs_ext.c, and against the block device jffs_int.c.
* A better file-internal organization too.
*
* A better checksum algorithm.
*
* Consider endianness stuff. ntohl() etc.
*
* Are we handling the atime, mtime, ctime members of the inode right?
*
* Remove some duplicated code. Take a look at jffs_write_node() and
* jffs_rewrite_data() for instance.
*
* Implement more meaning of the nlink member in various data structures.
* nlink could be used in conjunction with hard links for instance.
*
* Better memory management. Allocate data structures in larger chunks
* if possible.
*
* If too much meta data is stored, a garbage collect should be issued.
* We have experienced problems with too much meta data with for instance
* log files.
*
* Improve the calls to jffs_ioctl(). We would like to retrieve more
* information to be able to debug (or to supervise) JFFS during run-time.
*
*/
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/jffs.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/pagemap.h>
#include <linux/mutex.h>
#include <asm/byteorder.h>
#include <linux/smp_lock.h>
#include <linux/time.h>
#include <linux/ctype.h>
#include <linux/freezer.h>
#include "intrep.h"
#include "jffs_fm.h"
long no_jffs_node = 0;
static long no_jffs_file = 0;
#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
long no_jffs_control = 0;
long no_jffs_raw_inode = 0;
long no_jffs_node_ref = 0;
long no_jffs_fm = 0;
long no_jffs_fmcontrol = 0;
long no_hash = 0;
long no_name = 0;
#endif
static int jffs_scan_flash(struct jffs_control *c);
static int jffs_update_file(struct jffs_file *f, struct jffs_node *node);
static int jffs_build_file(struct jffs_file *f);
static int jffs_free_file(struct jffs_file *f);
static int jffs_free_node_list(struct jffs_file *f);
static int jffs_garbage_collect_now(struct jffs_control *c);
static int jffs_insert_file_into_hash(struct jffs_file *f);
static int jffs_remove_redundant_nodes(struct jffs_file *f);
/* Is there enough space on the flash? */
static inline int JFFS_ENOUGH_SPACE(struct jffs_control *c, __u32 space)
{
struct jffs_fmcontrol *fmc = c->fmc;
while (1) {
if ((fmc->flash_size - (fmc->used_size + fmc->dirty_size))
>= fmc->min_free_size + space) {
return 1;
}
if (fmc->dirty_size < fmc->sector_size)
return 0;
if (jffs_garbage_collect_now(c)) {
D1(printk("JFFS_ENOUGH_SPACE: jffs_garbage_collect_now() failed.\n"));
return 0;
}
}
}
#if CONFIG_JFFS_FS_VERBOSE > 0
static __u8
flash_read_u8(struct mtd_info *mtd, loff_t from)
{
size_t retlen;
__u8 ret;
int res;
res = MTD_READ(mtd, from, 1, &retlen, &ret);
if (retlen != 1) {
printk("Didn't read a byte in flash_read_u8(). Returned %d\n", res);
return 0;
}
return ret;
}
static void
jffs_hexdump(struct mtd_info *mtd, loff_t pos, int size)
{
char line[16];
int j = 0;
while (size > 0) {
int i;
printk("%ld:", (long) pos);
for (j = 0; j < 16; j++) {
line[j] = flash_read_u8(mtd, pos++);
}
for (i = 0; i < j; i++) {
if (!(i & 1)) {
printk(" %.2x", line[i] & 0xff);
}
else {
printk("%.2x", line[i] & 0xff);
}
}
/* Print empty space */
for (; i < 16; i++) {
if (!(i & 1)) {
printk(" ");
}
else {
printk(" ");
}
}
printk(" ");
for (i = 0; i < j; i++) {
if (isgraph(line[i])) {
printk("%c", line[i]);
}
else {
printk(".");
}
}
printk("\n");
size -= 16;
}
}
/* Print the contents of a node. */
static void
jffs_print_node(struct jffs_node *n)
{
D(printk("jffs_node: 0x%p\n", n));
D(printk("{\n"));
D(printk(" 0x%08x, /* version */\n", n->version));
D(printk(" 0x%08x, /* data_offset */\n", n->data_offset));
D(printk(" 0x%08x, /* data_size */\n", n->data_size));
D(printk(" 0x%08x, /* removed_size */\n", n->removed_size));
D(printk(" 0x%08x, /* fm_offset */\n", n->fm_offset));
D(printk(" 0x%02x, /* name_size */\n", n->name_size));
D(printk(" 0x%p, /* fm, fm->offset: %u */\n",
n->fm, (n->fm ? n->fm->offset : 0)));
D(printk(" 0x%p, /* version_prev */\n", n->version_prev));
D(printk(" 0x%p, /* version_next */\n", n->version_next));
D(printk(" 0x%p, /* range_prev */\n", n->range_prev));
D(printk(" 0x%p, /* range_next */\n", n->range_next));
D(printk("}\n"));
}
#endif
/* Print the contents of a raw inode. */
static void
jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
{
D(printk("jffs_raw_inode: inode number: %u\n", raw_inode->ino));
D(printk("{\n"));
D(printk(" 0x%08x, /* magic */\n", raw_inode->magic));
D(printk(" 0x%08x, /* ino */\n", raw_inode->ino));
D(printk(" 0x%08x, /* pino */\n", raw_inode->pino));
D(printk(" 0x%08x, /* version */\n", raw_inode->version));
D(printk(" 0x%08x, /* mode */\n", raw_inode->mode));
D(printk(" 0x%04x, /* uid */\n", raw_inode->uid));
D(printk(" 0x%04x, /* gid */\n", raw_inode->gid));
D(printk(" 0x%08x, /* atime */\n", raw_inode->atime));
D(printk(" 0x%08x, /* mtime */\n", raw_inode->mtime));
D(printk(" 0x%08x, /* ctime */\n", raw_inode->ctime));
D(printk(" 0x%08x, /* offset */\n", raw_inode->offset));
D(printk(" 0x%08x, /* dsize */\n", raw_inode->dsize));
D(printk(" 0x%08x, /* rsize */\n", raw_inode->rsize));
D(printk(" 0x%02x, /* nsize */\n", raw_inode->nsize));
D(printk(" 0x%02x, /* nlink */\n", raw_inode->nlink));
D(printk(" 0x%02x, /* spare */\n",
raw_inode->spare));
D(printk(" %u, /* rename */\n",
raw_inode->rename));
D(printk(" %u, /* deleted */\n",
raw_inode->deleted));
D(printk(" 0x%02x, /* accurate */\n",
raw_inode->accurate));
D(printk(" 0x%08x, /* dchksum */\n", raw_inode->dchksum));
D(printk(" 0x%04x, /* nchksum */\n", raw_inode->nchksum));
D(printk(" 0x%04x, /* chksum */\n", raw_inode->chksum));
D(printk("}\n"));
}
#define flash_safe_acquire(arg)
#define flash_safe_release(arg)
static int
flash_safe_read(struct mtd_info *mtd, loff_t from,
u_char *buf, size_t count)
{
size_t retlen;
int res;
D3(printk(KERN_NOTICE "flash_safe_read(%p, %08x, %p, %08x)\n",
mtd, (unsigned int) from, buf, count));
res = mtd->read(mtd, from, count, &retlen, buf);
if (retlen != count) {
panic("Didn't read all bytes in flash_safe_read(). Returned %d\n", res);
}
return res?res:retlen;
}
static __u32
flash_read_u32(struct mtd_info *mtd, loff_t from)
{
size_t retlen;
__u32 ret;
int res;
res = mtd->read(mtd, from, 4, &retlen, (unsigned char *)&ret);
if (retlen != 4) {
printk("Didn't read all bytes in flash_read_u32(). Returned %d\n", res);
return 0;
}
return ret;
}
static int
flash_safe_write(struct mtd_info *mtd, loff_t to,
const u_char *buf, size_t count)
{
size_t retlen;
int res;
D3(printk(KERN_NOTICE "flash_safe_write(%p, %08x, %p, %08x)\n",
mtd, (unsigned int) to, buf, count));
res = mtd->write(mtd, to, count, &retlen, buf);
if (retlen != count) {
printk("Didn't write all bytes in flash_safe_write(). Returned %d\n", res);
}
return res?res:retlen;
}
static int
flash_safe_writev(struct mtd_info *mtd, const struct kvec *vecs,
unsigned long iovec_cnt, loff_t to)
{
size_t retlen, retlen_a;
int i;
int res;
D3(printk(KERN_NOTICE "flash_safe_writev(%p, %08x, %p)\n",
mtd, (unsigned int) to, vecs));
if (mtd->writev) {
res = mtd->writev(mtd, vecs, iovec_cnt, to, &retlen);
return res ? res : retlen;
}
/* Not implemented writev. Repeatedly use write - on the not so
unreasonable assumption that the mtd driver doesn't care how
many write cycles we use. */
res=0;
retlen=0;
for (i=0; !res && i<iovec_cnt; i++) {
res = mtd->write(mtd, to, vecs[i].iov_len, &retlen_a,
vecs[i].iov_base);
if (retlen_a != vecs[i].iov_len) {
printk("Didn't write all bytes in flash_safe_writev(). Returned %d\n", res);
if (i != iovec_cnt-1)
return -EIO;
}
/* If res is non-zero, retlen_a is undefined, but we don't
care because in that case it's not going to be
returned anyway.
*/
to += retlen_a;
retlen += retlen_a;
}
return res?res:retlen;
}
static int
flash_memset(struct mtd_info *mtd, loff_t to,
const u_char c, size_t size)
{
static unsigned char pattern[64];
int i;
/* fill up pattern */
for(i = 0; i < 64; i++)
pattern[i] = c;
/* write as many 64-byte chunks as we can */
while (size >= 64) {
flash_safe_write(mtd, to, pattern, 64);
size -= 64;
to += 64;
}
/* and the rest */
if(size)
flash_safe_write(mtd, to, pattern, size);
return size;
}
static void
intrep_erase_callback(struct erase_info *done)
{
wait_queue_head_t *wait_q;
wait_q = (wait_queue_head_t *)done->priv;
wake_up(wait_q);
}
static int
flash_erase_region(struct mtd_info *mtd, loff_t start,
size_t size)
{
struct erase_info *erase;
DECLARE_WAITQUEUE(wait, current);
wait_queue_head_t wait_q;
erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
if (!erase)
return -ENOMEM;
init_waitqueue_head(&wait_q);
erase->mtd = mtd;
erase->callback = intrep_erase_callback;
erase->addr = start;
erase->len = size;
erase->priv = (u_long)&wait_q;
/* FIXME: Use TASK_INTERRUPTIBLE and deal with being interrupted */
set_current_state(TASK_UNINTERRUPTIBLE);
add_wait_queue(&wait_q, &wait);
if (mtd->erase(mtd, erase) < 0) {
set_current_state(TASK_RUNNING);
remove_wait_queue(&wait_q, &wait);
kfree(erase);
printk(KERN_WARNING "flash: erase of region [0x%lx, 0x%lx] "
"totally failed\n", (long)start, (long)start + size);
return -1;
}
schedule(); /* Wait for flash to finish. */
remove_wait_queue(&wait_q, &wait);
kfree(erase);
return 0;
}
/* This routine calculates checksums in JFFS. */
static __u32
jffs_checksum(const void *data, int size)
{
__u32 sum = 0;
__u8 *ptr = (__u8 *)data;
while (size-- > 0) {
sum += *ptr++;
}
D3(printk(", result: 0x%08x\n", sum));
return sum;
}
static int
jffs_checksum_flash(struct mtd_info *mtd, loff_t start, int size, __u32 *result)
{
__u32 sum = 0;
loff_t ptr = start;
__u8 *read_buf;
int i, length;
/* Allocate read buffer */
read_buf = kmalloc(sizeof(__u8) * 4096, GFP_KERNEL);
if (!read_buf) {
printk(KERN_NOTICE "kmalloc failed in jffs_checksum_flash()\n");
return -ENOMEM;
}
/* Loop until checksum done */
while (size) {
/* Get amount of data to read */
if (size < 4096)
length = size;
else
length = 4096;
/* Perform flash read */
D3(printk(KERN_NOTICE "jffs_checksum_flash\n"));
flash_safe_read(mtd, ptr, &read_buf[0], length);
/* Compute checksum */
for (i=0; i < length ; i++)
sum += read_buf[i];
/* Update pointer and size */
size -= length;
ptr += length;
}
/* Free read buffer */
kfree(read_buf);
/* Return result */
D3(printk("checksum result: 0x%08x\n", sum));
*result = sum;
return 0;
}
static __inline__ void jffs_fm_write_lock(struct jffs_fmcontrol *fmc)
{
// down(&fmc->wlock);
}
static __inline__ void jffs_fm_write_unlock(struct jffs_fmcontrol *fmc)
{
// up(&fmc->wlock);
}
/* Create and initialize a new struct jffs_file. */
static struct jffs_file *
jffs_create_file(struct jffs_control *c,
const struct jffs_raw_inode *raw_inode)
{
struct jffs_file *f;
if (!(f = kzalloc(sizeof(*f), GFP_KERNEL))) {
D(printk("jffs_create_file(): Failed!\n"));
return NULL;
}
no_jffs_file++;
f->ino = raw_inode->ino;
f->pino = raw_inode->pino;
f->nlink = raw_inode->nlink;
f->deleted = raw_inode->deleted;
f->c = c;
return f;
}
/* Build a control block for the file system. */
static struct jffs_control *
jffs_create_control(struct super_block *sb)
{
struct jffs_control *c;
register int s = sizeof(struct jffs_control);
int i;
D(char *t = 0);
D2(printk("jffs_create_control()\n"));
if (!(c = kmalloc(s, GFP_KERNEL))) {
goto fail_control;
}
DJM(no_jffs_control++);
c->root = NULL;
c->gc_task = NULL;
c->hash_len = JFFS_HASH_SIZE;
s = sizeof(struct list_head) * c->hash_len;
if (!(c->hash = kmalloc(s, GFP_KERNEL))) {
goto fail_hash;
}
DJM(no_hash++);
for (i = 0; i < c->hash_len; i++)
INIT_LIST_HEAD(&c->hash[i]);
if (!(c->fmc = jffs_build_begin(c, MINOR(sb->s_dev)))) {
goto fail_fminit;
}
c->next_ino = JFFS_MIN_INO + 1;
c->delete_list = (struct jffs_delete_list *) 0;
return c;
fail_fminit:
D(t = "c->fmc");
fail_hash:
kfree(c);
DJM(no_jffs_control--);
D(t = t ? t : "c->hash");
fail_control:
D(t = t ? t : "control");
D(printk("jffs_create_control(): Allocation failed: (%s)\n", t));
return (struct jffs_control *)0;
}
/* Clean up all data structures associated with the file system. */
void
jffs_cleanup_control(struct jffs_control *c)
{
D2(printk("jffs_cleanup_control()\n"));
if (!c) {
D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
return;
}
while (c->delete_list) {
struct jffs_delete_list *delete_list_element;
delete_list_element = c->delete_list;
c->delete_list = c->delete_list->next;
kfree(delete_list_element);
}
/* Free all files and nodes. */
if (c->hash) {
jffs_foreach_file(c, jffs_free_node_list);
jffs_foreach_file(c, jffs_free_file);
kfree(c->hash);
DJM(no_hash--);
}
jffs_cleanup_fmcontrol(c->fmc);
kfree(c);
DJM(no_jffs_control--);
D3(printk("jffs_cleanup_control(): Leaving...\n"));
}
/* This function adds a virtual root node to the in-RAM representation.
Called by jffs_build_fs(). */
static int
jffs_add_virtual_root(struct jffs_control *c)
{
struct jffs_file *root;
struct jffs_node *node;
D2(printk("jffs_add_virtual_root(): "
"Creating a virtual root directory.\n"));
if (!(root = kzalloc(sizeof(struct jffs_file), GFP_KERNEL))) {
return -ENOMEM;
}
no_jffs_file++;
if (!(node = jffs_alloc_node())) {
kfree(root);
no_jffs_file--;
return -ENOMEM;
}
DJM(no_jffs_node++);
memset(node, 0, sizeof(struct jffs_node));
node->ino = JFFS_MIN_INO;
root->ino = JFFS_MIN_INO;
root->mode = S_IFDIR | S_IRWXU | S_IRGRP
| S_IXGRP | S_IROTH | S_IXOTH;
root->atime = root->mtime = root->ctime = get_seconds();
root->nlink = 1;
root->c = c;
root->version_head = root->version_tail = node;
jffs_insert_file_into_hash(root);
return 0;
}
/* This is where the file system is built and initialized. */
int
jffs_build_fs(struct super_block *sb)
{
struct jffs_control *c;
int err = 0;
D2(printk("jffs_build_fs()\n"));
if (!(c = jffs_create_control(sb))) {
return -ENOMEM;
}
c->building_fs = 1;
c->sb = sb;
if ((err = jffs_scan_flash(c)) < 0) {
if(err == -EAGAIN){
/* scan_flash() wants us to try once more. A flipping
bits sector was detect in the middle of the scan flash.
Clean up old allocated memory before going in.
*/
D1(printk("jffs_build_fs: Cleaning up all control structures,"
" reallocating them and trying mount again.\n"));
jffs_cleanup_control(c);
if (!(c = jffs_create_control(sb))) {
return -ENOMEM;
}
c->building_fs = 1;
c->sb = sb;
if ((err = jffs_scan_flash(c)) < 0) {
goto jffs_build_fs_fail;
}
}else{
goto jffs_build_fs_fail;
}
}
/* Add a virtual root node if no one exists. */
if (!jffs_find_file(c, JFFS_MIN_INO)) {
if ((err = jffs_add_virtual_root(c)) < 0) {
goto jffs_build_fs_fail;
}
}
while (c->delete_list) {
struct jffs_file *f;
struct jffs_delete_list *delete_list_element;
if ((f = jffs_find_file(c, c->delete_list->ino))) {
f->deleted = 1;
}
delete_list_element = c->delete_list;
c->delete_list = c->delete_list->next;
kfree(delete_list_element);
}
/* Remove deleted nodes. */
if ((err = jffs_foreach_file(c, jffs_possibly_delete_file)) < 0) {
printk(KERN_ERR "JFFS: Failed to remove deleted nodes.\n");
goto jffs_build_fs_fail;
}
/* Remove redundant nodes. (We are not interested in the
return value in this case.) */
jffs_foreach_file(c, jffs_remove_redundant_nodes);
/* Try to build a tree from all the nodes. */
if ((err = jffs_foreach_file(c, jffs_insert_file_into_tree)) < 0) {
printk("JFFS: Failed to build tree.\n");
goto jffs_build_fs_fail;
}
/* Compute the sizes of all files in the filesystem. Adjust if
necessary. */
if ((err = jffs_foreach_file(c, jffs_build_file)) < 0) {
printk("JFFS: Failed to build file system.\n");
goto jffs_build_fs_fail;
}
sb->s_fs_info = (void *)c;
c->building_fs = 0;
D1(jffs_print_hash_table(c));
D1(jffs_print_tree(c->root, 0));
return 0;
jffs_build_fs_fail:
jffs_cleanup_control(c);
return err;
} /* jffs_build_fs() */
/*
This checks for sectors that were being erased in their previous
lifetimes and for some reason or the other (power fail etc.),
the erase cycles never completed.
As the flash array would have reverted back to read status,
these sectors are detected by the symptom of the "flipping bits",
i.e. bits being read back differently from the same location in
flash if read multiple times.
The only solution to this is to re-erase the entire
sector.
Unfortunately detecting "flipping bits" is not a simple exercise
as a bit may be read back at 1 or 0 depending on the alignment
of the stars in the universe.
The level of confidence is in direct proportion to the number of
scans done. By power fail testing I (Vipin) have been able to
proove that reading twice is not enough.
Maybe 4 times? Change NUM_REREADS to a higher number if you want
a (even) higher degree of confidence in your mount process.
A higher number would of course slow down your mount.
*/
static int check_partly_erased_sectors(struct jffs_fmcontrol *fmc){
#define NUM_REREADS 4 /* see note above */
#define READ_AHEAD_BYTES 4096 /* must be a multiple of 4,
usually set to kernel page size */
__u8 *read_buf1;
__u8 *read_buf2;
int err = 0;
int retlen;
int i;
int cnt;
__u32 offset;
loff_t pos = 0;
loff_t end = fmc->flash_size;
/* Allocate read buffers */
read_buf1 = kmalloc(sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
if (!read_buf1)
return -ENOMEM;
read_buf2 = kmalloc(sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
if (!read_buf2) {
kfree(read_buf1);
return -ENOMEM;
}
CHECK_NEXT:
while(pos < end){
D1(printk("check_partly_erased_sector():checking sector which contains"
" offset 0x%x for flipping bits..\n", (__u32)pos));
retlen = flash_safe_read(fmc->mtd, pos,
&read_buf1[0], READ_AHEAD_BYTES);
retlen &= ~3;
for(cnt = 0; cnt < NUM_REREADS; cnt++){
(void)flash_safe_read(fmc->mtd, pos,
&read_buf2[0], READ_AHEAD_BYTES);
for (i=0 ; i < retlen ; i+=4) {
/* buffers MUST match, double word for word! */
if(*((__u32 *) &read_buf1[i]) !=
*((__u32 *) &read_buf2[i])
){
/* flipping bits detected, time to erase sector */
/* This will help us log some statistics etc. */
D1(printk("Flipping bits detected in re-read round:%i of %i\n",
cnt, NUM_REREADS));
D1(printk("check_partly_erased_sectors:flipping bits detected"
" @offset:0x%x(0x%x!=0x%x)\n",
(__u32)pos+i, *((__u32 *) &read_buf1[i]),
*((__u32 *) &read_buf2[i])));
/* calculate start of present sector */
offset = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
D1(printk("check_partly_erased_sector():erasing sector starting 0x%x.\n",
offset));
if (flash_erase_region(fmc->mtd,
offset, fmc->sector_size) < 0) {
printk(KERN_ERR "JFFS: Erase of flash failed. "
"offset = %u, erase_size = %d\n",
offset , fmc->sector_size);
err = -EIO;
goto returnBack;
}else{
D1(printk("JFFS: Erase of flash sector @0x%x successful.\n",
offset));
/* skip ahead to the next sector */
pos = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
pos += fmc->sector_size;
goto CHECK_NEXT;
}
}
}
}
pos += READ_AHEAD_BYTES;
}
returnBack:
kfree(read_buf1);
kfree(read_buf2);
D2(printk("check_partly_erased_sector():Done checking all sectors till offset 0x%x for flipping bits.\n",
(__u32)pos));
return err;
}/* end check_partly_erased_sectors() */
/* Scan the whole flash memory in order to find all nodes in the
file systems. */
static int
jffs_scan_flash(struct jffs_control *c)
{
char name[JFFS_MAX_NAME_LEN + 2];
struct jffs_raw_inode raw_inode;
struct jffs_node *node = NULL;
struct jffs_fmcontrol *fmc = c->fmc;
__u32 checksum;
__u8 tmp_accurate;
__u16 tmp_chksum;
__u32 deleted_file;
loff_t pos = 0;
loff_t start;
loff_t test_start;
loff_t end = fmc->flash_size;
__u8 *read_buf;
int i, len, retlen;
__u32 offset;
__u32 free_chunk_size1;
__u32 free_chunk_size2;
#define NUMFREEALLOWED 2 /* 2 chunks of at least erase size space allowed */
int num_free_space = 0; /* Flag err if more than TWO
free blocks found. This is NOT allowed
by the current jffs design.
*/
int num_free_spc_not_accp = 0; /* For debugging purposed keep count
of how much free space was rejected and
marked dirty
*/
D1(printk("jffs_scan_flash(): start pos = 0x%lx, end = 0x%lx\n",
(long)pos, (long)end));
flash_safe_acquire(fmc->mtd);
/*
check and make sure that any sector does not suffer
from the "partly erased, bit flipping syndrome" (TM Vipin :)
If so, offending sectors will be erased.
*/
if(check_partly_erased_sectors(fmc) < 0){
flash_safe_release(fmc->mtd);
return -EIO; /* bad, bad, bad error. Cannot continue.*/
}
/* Allocate read buffer */
read_buf = kmalloc(sizeof(__u8) * 4096, GFP_KERNEL);
if (!read_buf) {
flash_safe_release(fmc->mtd);
return -ENOMEM;
}
/* Start the scan. */
while (pos < end) {
deleted_file = 0;
/* Remember the position from where we started this scan. */
start = pos;
switch (flash_read_u32(fmc->mtd, pos)) {
case JFFS_EMPTY_BITMASK:
/* We have found 0xffffffff at this position. We have to
scan the rest of the flash till the end or till
something else than 0xffffffff is found.
Keep going till we do not find JFFS_EMPTY_BITMASK
anymore */
D1(printk("jffs_scan_flash(): 0xffffffff at pos 0x%lx.\n",
(long)pos));
while(pos < end){
len = end - pos < 4096 ? end - pos : 4096;
retlen = flash_safe_read(fmc->mtd, pos,
&read_buf[0], len);
retlen &= ~3;
for (i=0 ; i < retlen ; i+=4, pos += 4) {
if(*((__u32 *) &read_buf[i]) !=
JFFS_EMPTY_BITMASK)
break;
}
if (i == retlen)
continue;
else
break;
}
D1(printk("jffs_scan_flash():0xffffffff ended at pos 0x%lx.\n",
(long)pos));
/* If some free space ends in the middle of a sector,
treat it as dirty rather than clean.
This is to handle the case where one thread
allocated space for a node, but didn't get to
actually _write_ it before power was lost, leaving
a gap in the log. Shifting all node writes into
a single kernel thread will fix the original problem.
*/
if ((__u32) pos % fmc->sector_size) {
/* If there was free space in previous
sectors, don't mark that dirty too -
only from the beginning of this sector
(or from start)
*/
test_start = pos & ~(fmc->sector_size-1); /* end of last sector */
if (start < test_start) {
/* free space started in the previous sector! */
if((num_free_space < NUMFREEALLOWED) &&
((unsigned int)(test_start - start) >= fmc->sector_size)){
/*
Count it in if we are still under NUMFREEALLOWED *and* it is
at least 1 erase sector in length. This will keep us from
picking any little ole' space as "free".
*/
D1(printk("Reducing end of free space to 0x%x from 0x%x\n",
(unsigned int)test_start, (unsigned int)pos));
D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
(unsigned int) start,
(unsigned int)(test_start - start)));
/* below, space from "start" to "pos" will be marked dirty. */
start = test_start;
/* Being in here means that we have found at least an entire
erase sector size of free space ending on a sector boundary.
Keep track of free spaces accepted.
*/
num_free_space++;
}else{
num_free_spc_not_accp++;
D1(printk("Free space (#%i) found but *Not* accepted: Starting"
" 0x%x for 0x%x bytes\n",
num_free_spc_not_accp, (unsigned int)start,
(unsigned int)((unsigned int)(pos & ~(fmc->sector_size-1)) - (unsigned int)start)));
}
}
if((((__u32)(pos - start)) != 0)){
D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
(unsigned int) start, (unsigned int) (pos - start)));
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
}else{
/* "Flipping bits" detected. This means that our scan for them
did not catch this offset. See check_partly_erased_sectors() for
more info.
*/
D1(printk("jffs_scan_flash():wants to allocate dirty flash "
"space for 0 bytes.\n"));
D1(printk("jffs_scan_flash(): Flipping bits! We will free "
"all allocated memory, erase this sector and remount\n"));
/* calculate start of present sector */
offset = (((__u32)pos)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
D1(printk("jffs_scan_flash():erasing sector starting 0x%x.\n",
offset));
if (flash_erase_region(fmc->mtd,
offset, fmc->sector_size) < 0) {
printk(KERN_ERR "JFFS: Erase of flash failed. "
"offset = %u, erase_size = %d\n",
offset , fmc->sector_size);
flash_safe_release(fmc->mtd);
kfree(read_buf);
return -1; /* bad, bad, bad! */
}
flash_safe_release(fmc->mtd);
kfree(read_buf);
return -EAGAIN; /* erased offending sector. Try mount one more time please. */
}
}else{
/* Being in here means that we have found free space that ends on an erase sector
boundary.
Count it in if we are still under NUMFREEALLOWED *and* it is at least 1 erase
sector in length. This will keep us from picking any little ole' space as "free".
*/
if((num_free_space < NUMFREEALLOWED) &&
((unsigned int)(pos - start) >= fmc->sector_size)){
/* We really don't do anything to mark space as free, except *not*
mark it dirty and just advance the "pos" location pointer.
It will automatically be picked up as free space.
*/
num_free_space++;
D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
(unsigned int) start, (unsigned int) (pos - start)));
}else{
num_free_spc_not_accp++;
D1(printk("Free space (#%i) found but *Not* accepted: Starting "
"0x%x for 0x%x bytes\n", num_free_spc_not_accp,
(unsigned int) start,
(unsigned int) (pos - start)));
/* Mark this space as dirty. We already have our free space. */
D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
(unsigned int) start, (unsigned int) (pos - start)));
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
}
}
if(num_free_space > NUMFREEALLOWED){
printk(KERN_WARNING "jffs_scan_flash(): Found free space "
"number %i. Only %i free space is allowed.\n",
num_free_space, NUMFREEALLOWED);
}
continue;
case JFFS_DIRTY_BITMASK:
/* We have found 0x00000000 at this position. Scan as far
as possible to find out how much is dirty. */
D1(printk("jffs_scan_flash(): 0x00000000 at pos 0x%lx.\n",
(long)pos));
for (; pos < end
&& JFFS_DIRTY_BITMASK == flash_read_u32(fmc->mtd, pos);
pos += 4);
D1(printk("jffs_scan_flash(): 0x00 ended at "
"pos 0x%lx.\n", (long)pos));
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
continue;
case JFFS_MAGIC_BITMASK:
/* We have probably found a new raw inode. */
break;
default:
bad_inode:
/* We're f*cked. This is not solved yet. We have
to scan for the magic pattern. */
D1(printk("*************** Dirty flash memory or "
"bad inode: "
"hexdump(pos = 0x%lx, len = 128):\n",
(long)pos));
D1(jffs_hexdump(fmc->mtd, pos, 128));
for (pos += 4; pos < end; pos += 4) {
switch (flash_read_u32(fmc->mtd, pos)) {
case JFFS_MAGIC_BITMASK:
case JFFS_EMPTY_BITMASK:
/* handle these in the main switch() loop */
goto cont_scan;
default:
break;
}
}
cont_scan:
/* First, mark as dirty the region
which really does contain crap. */
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start),
NULL);
continue;
}/* switch */
/* We have found the beginning of an inode. Create a
node for it unless there already is one available. */
if (!node) {
if (!(node = jffs_alloc_node())) {
/* Free read buffer */
kfree(read_buf);
/* Release the flash device */
flash_safe_release(fmc->mtd);
return -ENOMEM;
}
DJM(no_jffs_node++);
}
/* Read the next raw inode. */
flash_safe_read(fmc->mtd, pos, (u_char *) &raw_inode,
sizeof(struct jffs_raw_inode));
/* When we compute the checksum for the inode, we never
count the 'accurate' or the 'checksum' fields. */
tmp_accurate = raw_inode.accurate;
tmp_chksum = raw_inode.chksum;
raw_inode.accurate = 0;
raw_inode.chksum = 0;
checksum = jffs_checksum(&raw_inode,
sizeof(struct jffs_raw_inode));
raw_inode.accurate = tmp_accurate;
raw_inode.chksum = tmp_chksum;
D3(printk("*** We have found this raw inode at pos 0x%lx "
"on the flash:\n", (long)pos));
D3(jffs_print_raw_inode(&raw_inode));
if (checksum != raw_inode.chksum) {
D1(printk("jffs_scan_flash(): Bad checksum: "
"checksum = %u, "
"raw_inode.chksum = %u\n",
checksum, raw_inode.chksum));
pos += sizeof(struct jffs_raw_inode);
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
/* Reuse this unused struct jffs_node. */
continue;
}
/* Check the raw inode read so far. Start with the
maximum length of the filename. */
if (raw_inode.nsize > JFFS_MAX_NAME_LEN) {
printk(KERN_WARNING "jffs_scan_flash: Found a "
"JFFS node with name too large\n");
goto bad_inode;
}
if (raw_inode.rename && raw_inode.dsize != sizeof(__u32)) {
printk(KERN_WARNING "jffs_scan_flash: Found a "
"rename node with dsize %u.\n",
raw_inode.dsize);
jffs_print_raw_inode(&raw_inode);
goto bad_inode;
}
/* The node's data segment should not exceed a
certain length. */
if (raw_inode.dsize > fmc->max_chunk_size) {
printk(KERN_WARNING "jffs_scan_flash: Found a "
"JFFS node with dsize (0x%x) > max_chunk_size (0x%x)\n",
raw_inode.dsize, fmc->max_chunk_size);
goto bad_inode;
}
pos += sizeof(struct jffs_raw_inode);
/* This shouldn't be necessary because a node that
violates the flash boundaries shouldn't be written
in the first place. */
if (pos >= end) {
goto check_node;
}
/* Read the name. */
*name = 0;
if (raw_inode.nsize) {
flash_safe_read(fmc->mtd, pos, name, raw_inode.nsize);
name[raw_inode.nsize] = '\0';
pos += raw_inode.nsize
+ JFFS_GET_PAD_BYTES(raw_inode.nsize);
D3(printk("name == \"%s\"\n", name));
checksum = jffs_checksum(name, raw_inode.nsize);
if (checksum != raw_inode.nchksum) {
D1(printk("jffs_scan_flash(): Bad checksum: "
"checksum = %u, "
"raw_inode.nchksum = %u\n",
checksum, raw_inode.nchksum));
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
/* Reuse this unused struct jffs_node. */
continue;
}
if (pos >= end) {
goto check_node;
}
}
/* Read the data, if it exists, in order to be sure it
matches the checksum. */
if (raw_inode.dsize) {
if (raw_inode.rename) {
deleted_file = flash_read_u32(fmc->mtd, pos);
}
if (jffs_checksum_flash(fmc->mtd, pos, raw_inode.dsize, &checksum)) {
printk("jffs_checksum_flash() failed to calculate a checksum\n");
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
/* Reuse this unused struct jffs_node. */
continue;
}
pos += raw_inode.dsize
+ JFFS_GET_PAD_BYTES(raw_inode.dsize);
if (checksum != raw_inode.dchksum) {
D1(printk("jffs_scan_flash(): Bad checksum: "
"checksum = %u, "
"raw_inode.dchksum = %u\n",
checksum, raw_inode.dchksum));
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
/* Reuse this unused struct jffs_node. */
continue;
}
}
check_node:
/* Remember the highest inode number in the whole file
system. This information will be used when assigning
new files new inode numbers. */
if (c->next_ino <= raw_inode.ino) {
c->next_ino = raw_inode.ino + 1;
}
if (raw_inode.accurate) {
int err;
node->data_offset = raw_inode.offset;
node->data_size = raw_inode.dsize;
node->removed_size = raw_inode.rsize;
/* Compute the offset to the actual data in the
on-flash node. */
node->fm_offset
= sizeof(struct jffs_raw_inode)
+ raw_inode.nsize
+ JFFS_GET_PAD_BYTES(raw_inode.nsize);
node->fm = jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start),
node);
if (!node->fm) {
D(printk("jffs_scan_flash(): !node->fm\n"));
jffs_free_node(node);
DJM(no_jffs_node--);
/* Free read buffer */
kfree(read_buf);
/* Release the flash device */
flash_safe_release(fmc->mtd);
return -ENOMEM;
}
if ((err = jffs_insert_node(c, NULL, &raw_inode,
name, node)) < 0) {
printk("JFFS: Failed to handle raw inode. "
"(err = %d)\n", err);
break;
}
if (raw_inode.rename) {
struct jffs_delete_list *dl
= (struct jffs_delete_list *)
kmalloc(sizeof(struct jffs_delete_list),
GFP_KERNEL);
if (!dl) {
D(printk("jffs_scan_flash: !dl\n"));
jffs_free_node(node);
DJM(no_jffs_node--);
/* Release the flash device */
flash_safe_release(fmc->flash_part);
/* Free read buffer */
kfree(read_buf);
return -ENOMEM;
}
dl->ino = deleted_file;
dl->next = c->delete_list;
c->delete_list = dl;
node->data_size = 0;
}
D3(jffs_print_node(node));
node = NULL; /* Don't free the node! */
}
else {
jffs_fmalloced(fmc, (__u32) start,
(__u32) (pos - start), NULL);
D3(printk("jffs_scan_flash(): Just found an obsolete "
"raw_inode. Continuing the scan...\n"));
/* Reuse this unused struct jffs_node. */
}
}
if (node) {
jffs_free_node(node);
DJM(no_jffs_node--);
}
jffs_build_end(fmc);
/* Free read buffer */
kfree(read_buf);
if(!num_free_space){
printk(KERN_WARNING "jffs_scan_flash(): Did not find even a single "
"chunk of free space. This is BAD!\n");
}
/* Return happy */
D3(printk("jffs_scan_flash(): Leaving...\n"));
flash_safe_release(fmc->mtd);
/* This is to trap the "free size accounting screwed error. */
free_chunk_size1 = jffs_free_size1(fmc);
free_chunk_size2 = jffs_free_size2(fmc);
if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
printk(KERN_WARNING "jffs_scan_falsh():Free size accounting screwed\n");
printk(KERN_WARNING "jfffs_scan_flash():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);
return -1; /* Do NOT mount f/s so that we can inspect what happened.
Mounting this screwed up f/s will screw us up anyway.
*/
}
return 0; /* as far as we are concerned, we are happy! */
} /* jffs_scan_flash() */
/* Insert any kind of node into the file system. Take care of data
insertions and deletions. Also remove redundant information. The
memory allocated for the `name' is regarded as "given away" in the
caller's perspective. */
int
jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
const struct jffs_raw_inode *raw_inode,
const char *name, struct jffs_node *node)
{
int update_name = 0;
int insert_into_tree = 0;
D2(printk("jffs_insert_node(): ino = %u, version = %u, "
"name = \"%s\", deleted = %d\n",
raw_inode->ino, raw_inode->version,
((name && *name) ? name : ""), raw_inode->deleted));
/* If there doesn't exist an associated jffs_file, then
create, initialize and insert one into the file system. */
if (!f && !(f = jffs_find_file(c, raw_inode->ino))) {
if (!(f = jffs_create_file(c, raw_inode))) {
return -ENOMEM;
}
jffs_insert_file_into_hash(f);
insert_into_tree = 1;
}
node->ino = raw_inode->ino;
node->version = raw_inode->version;
node->data_size = raw_inode->dsize;
node->fm_offset = sizeof(struct jffs_raw_inode) + raw_inode->nsize
+ JFFS_GET_PAD_BYTES(raw_inode->nsize);
node->name_size = raw_inode->nsize;
/* Now insert the node at the correct position into the file's
version list. */
if (!f->version_head) {
/* This is the first node. */
f->version_head = node;
f->version_tail = node;
node->version_prev = NULL;
node->version_next = NULL;
f->highest_version = node->version;
update_name = 1;
f->mode = raw_inode->mode;
f->uid = raw_inode->uid;
f->gid = raw_inode->gid;
f->atime = raw_inode->atime;
f->mtime = raw_inode->mtime;
f->ctime = raw_inode->ctime;
}
else if ((f->highest_version < node->version)
|| (node->version == 0)) {
/* Insert at the end of the list. I.e. this node is the
newest one so far. */
node->version_prev = f->version_tail;
node->version_next = NULL;
f->version_tail->version_next = node;
f->version_tail = node;
f->highest_version = node->version;
update_name = 1;
f->pino = raw_inode->pino;
f->mode = raw_inode->mode;
f->uid = raw_inode->uid;
f->gid = raw_inode->gid;
f->atime = raw_inode->atime;
f->mtime = raw_inode->mtime;
f->ctime = raw_inode->ctime;
}
else if (f->version_head->version > node->version) {
/* Insert at the bottom of the list. */
node->version_prev = NULL;
node->version_next = f->version_head;
f->version_head->version_prev = node;
f->version_head = node;
if (!f->name) {
update_name = 1;
}
}
else {
struct jffs_node *n;
int newer_name = 0;
/* Search for the insertion position starting from
the tail (newest node). */
for (n = f->version_tail; n; n = n->version_prev) {
if (n->version < node->version) {
node->version_prev = n;
node->version_next = n->version_next;
node->version_next->version_prev = node;
n->version_next = node;
if (!newer_name) {
update_name = 1;
}
break;
}
if (n->name_size) {
newer_name = 1;
}
}
}
/* Deletion is irreversible. If any 'deleted' node is ever
written, the file is deleted */
if (raw_inode->deleted)
f->deleted = raw_inode->deleted;
/* Perhaps update the name. */
if (raw_inode->nsize && update_name && name && *name && (name != f->name)) {
if (f->name) {
kfree(f->name);
DJM(no_name--);
}
if (!(f->name = kmalloc(raw_inode->nsize + 1,
GFP_KERNEL))) {
return -ENOMEM;
}
DJM(no_name++);
memcpy(f->name, name, raw_inode->nsize);
f->name[raw_inode->nsize] = '\0';
f->nsize = raw_inode->nsize;
D3(printk("jffs_insert_node(): Updated the name of "
"the file to \"%s\".\n", name));
}
if (!c->building_fs) {
D3(printk("jffs_insert_node(): ---------------------------"
"------------------------------------------- 1\n"));
if (insert_into_tree) {
jffs_insert_file_into_tree(f);
}
/* Once upon a time, we would call jffs_possibly_delete_file()
here. That causes an oops if someone's still got the file
open, so now we only do it in jffs_delete_inode()
-- dwmw2
*/
if (node->data_size || node->removed_size) {
jffs_update_file(f, node);
}
jffs_remove_redundant_nodes(f);
jffs_garbage_collect_trigger(c);
D3(printk("jffs_insert_node(): ---------------------------"
"------------------------------------------- 2\n"));
}
return 0;
} /* jffs_insert_node() */
/* Unlink a jffs_node from the version list it is in. */
static inline void
jffs_unlink_node_from_version_list(struct jffs_file *f,
struct jffs_node *node)
{
if (node->version_prev) {
node->version_prev->version_next = node->version_next;
} else {
f->version_head = node->version_next;
}
if (node->version_next) {
node->version_next->version_prev = node->version_prev;
} else {
f->version_tail = node->version_prev;
}
}
/* Unlink a jffs_node from the range list it is in. */
static inline void
jffs_unlink_node_from_range_list(struct jffs_file *f, struct jffs_node *node)
{
if (node->range_prev) {
node->range_prev->range_next = node->range_next;
}
else {
f->range_head = node->range_next;
}
if (node->range_next) {
node->range_next->range_prev = node->range_prev;
}
else {
f->range_tail = node->range_prev;
}
}
/* Function used by jffs_remove_redundant_nodes() below. This function
classifies what kind of information a node adds to a file. */
static inline __u8
jffs_classify_node(struct jffs_node *node)
{
__u8 mod_type = JFFS_MODIFY_INODE;
if (node->name_size) {
mod_type |= JFFS_MODIFY_NAME;
}
if (node->data_size || node->removed_size) {
mod_type |= JFFS_MODIFY_DATA;
}
return mod_type;
}
/* Remove redundant nodes from a file. Mark the on-flash memory
as dirty. */
static int
jffs_remove_redundant_nodes(struct jffs_file *f)
{
struct jffs_node *newest_node;
struct jffs_node *cur;
struct jffs_node *prev;
__u8 newest_type;
__u8 mod_type;
__u8 node_with_name_later = 0;
if (!(newest_node = f->version_tail)) {
return 0;
}
/* What does the `newest_node' modify? */
newest_type = jffs_classify_node(newest_node);
node_with_name_later = newest_type & JFFS_MODIFY_NAME;
D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
"newest_type: %u\n", f->ino, (f->name ? f->name : ""),
newest_type));
/* Traverse the file's nodes and determine which of them that are
superfluous. Yeah, this might look very complex at first
glance but it is actually very simple. */
for (cur = newest_node->version_prev; cur; cur = prev) {
prev = cur->version_prev;
mod_type = jffs_classify_node(cur);
if ((mod_type <= JFFS_MODIFY_INODE)
|| ((newest_type & JFFS_MODIFY_NAME)
&& (mod_type
<= (JFFS_MODIFY_INODE + JFFS_MODIFY_NAME)))
|| (cur->data_size == 0 && cur->removed_size
&& !cur->version_prev && node_with_name_later)) {
/* Yes, this node is redundant. Remove it. */
D2(printk("jffs_remove_redundant_nodes(): "
"Removing node: ino: %u, version: %u, "
"mod_type: %u\n", cur->ino, cur->version,
mod_type));
jffs_unlink_node_from_version_list(f, cur);
jffs_fmfree(f->c->fmc, cur->fm, cur);
jffs_free_node(cur);
DJM(no_jffs_node--);
}
else {
node_with_name_later |= (mod_type & JFFS_MODIFY_NAME);
}
}
return 0;
}
/* Insert a file into the hash table. */
static int
jffs_insert_file_into_hash(struct jffs_file *f)
{
int i = f->ino % f->c->hash_len;
D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f->ino));
list_add(&f->hash, &f->c->hash[i]);
return 0;
}
/* Insert a file into the file system tree. */
int
jffs_insert_file_into_tree(struct jffs_file *f)
{
struct jffs_file *parent;
D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
(f->name ? f->name : "")));
if (!(parent = jffs_find_file(f->c, f->pino))) {
if (f->pino == 0) {
f->c->root = f;
f->parent = NULL;
f->sibling_prev = NULL;
f->sibling_next = NULL;
return 0;
}
else {
D1(printk("jffs_insert_file_into_tree(): Found "
"inode with no parent and pino == %u\n",
f->pino));
return -1;
}
}
f->parent = parent;
f->sibling_next = parent->children;
if (f->sibling_next) {
f->sibling_next->sibling_prev = f;
}
f->sibling_prev = NULL;
parent->children = f;
return 0;
}
/* Remove a file from the hash table. */
static int
jffs_unlink_file_from_hash(struct jffs_file *f)
{
D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
"ino %u\n", f, f->ino));
list_del(&f->hash);
return 0;
}
/* Just remove the file from the parent's children. Don't free
any memory. */
int
jffs_unlink_file_from_tree(struct jffs_file *f)
{
D3(printk("jffs_unlink_file_from_tree(): ino: %d, pino: %d, name: "
"\"%s\"\n", f->ino, f->pino, (f->name ? f->name : "")));
if (f->sibling_prev) {
f->sibling_prev->sibling_next = f->sibling_next;
}
else if (f->parent) {
D3(printk("f->parent=%p\n", f->parent));
f->parent->children = f->sibling_next;
}
if (f->sibling_next) {
f->sibling_next->sibling_prev = f->sibling_prev;
}
return 0;
}
/* Find a file with its inode number. */
struct jffs_file *
jffs_find_file(struct jffs_control *c, __u32 ino)
{
struct jffs_file *f;
int i = ino % c->hash_len;
D3(printk("jffs_find_file(): ino: %u\n", ino));
list_for_each_entry(f, &c->hash[i], hash) {
if (ino != f->ino)
continue;
D3(printk("jffs_find_file(): Found file with ino "
"%u. (name: \"%s\")\n",
ino, (f->name ? f->name : ""));
);
return f;
}
D3(printk("jffs_find_file(): Didn't find file "
"with ino %u.\n", ino);
);
return NULL;
}
/* Find a file in a directory. We are comparing the names. */
struct jffs_file *
jffs_find_child(struct jffs_file *dir, const char *name, int len)
{
struct jffs_file *f;
D3(printk("jffs_find_child()\n"));
for (f = dir->children; f; f = f->sibling_next) {
if (!f->deleted && f->name
&& !strncmp(f->name, name, len)
&& f->name[len] == '\0') {
break;
}
}
D3(if (f) {
printk("jffs_find_child(): Found \"%s\".\n", f->name);
}
else {
char *copy = kmalloc(len + 1, GFP_KERNEL);
if (copy) {
memcpy(copy, name, len);
copy[len] = '\0';
}
printk("jffs_find_child(): Didn't find the file \"%s\".\n",
(copy ? copy : ""));
kfree(copy);
});
return f;
}
/* Write a raw inode that takes up a certain amount of space in the flash
memory. At the end of the flash device, there is often space that is
impossible to use. At these times we want to mark this space as not
used. In the cases when the amount of space is greater or equal than
a struct jffs_raw_inode, we write a "dummy node" that takes up this
space. The space after the raw inode, if it exists, is left as it is.
Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
we can compute the checksum of it; we don't have to manipulate it any
further.
If the space left on the device is less than the size of a struct
jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
No raw inode is written this time. */
static int
jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
{
struct jffs_fmcontrol *fmc = c->fmc;
int err;
D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
"dirty_fm->size = %u\n",
dirty_fm->offset, dirty_fm->size));
if (dirty_fm->size >= sizeof(struct jffs_raw_inode)) {
struct jffs_raw_inode raw_inode;
memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.dsize = dirty_fm->size
- sizeof(struct jffs_raw_inode);
raw_inode.dchksum = raw_inode.dsize * 0xff;
raw_inode.chksum
= jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
if ((err = flash_safe_write(fmc->mtd,
dirty_fm->offset,
(u_char *)&raw_inode,
sizeof(struct jffs_raw_inode)))
< 0) {
printk(KERN_ERR "JFFS: jffs_write_dummy_node: "
"flash_safe_write failed!\n");
return err;
}
}
else {
flash_safe_acquire(fmc->mtd);
flash_memset(fmc->mtd, dirty_fm->offset, 0, dirty_fm->size);
flash_safe_release(fmc->mtd);
}
D3(printk("jffs_write_dummy_node(): Leaving...\n"));
return 0;
}
/* Write a raw inode, possibly its name and possibly some data. */
int
jffs_write_node(struct jffs_control *c, struct jffs_node *node,
struct jffs_raw_inode *raw_inode,
const char *name, const unsigned char *data,
int recoverable,
struct jffs_file *f)
{
struct jffs_fmcontrol *fmc = c->fmc;
struct jffs_fm *fm;
struct kvec node_iovec[4];
unsigned long iovec_cnt;
__u32 pos;
int err;
__u32 slack = 0;
__u32 total_name_size = raw_inode->nsize
+ JFFS_GET_PAD_BYTES(raw_inode->nsize);
__u32 total_data_size = raw_inode->dsize
+ JFFS_GET_PAD_BYTES(raw_inode->dsize);
__u32 total_size = sizeof(struct jffs_raw_inode)
+ total_name_size + total_data_size;
/* If this node isn't something that will eventually let
GC free even more space, then don't allow it unless
there's at least max_chunk_size space still available
*/
if (!recoverable)
slack = fmc->max_chunk_size;
/* Fire the retrorockets and shoot the fruiton torpedoes, sir! */
ASSERT(if (!node) {
printk("jffs_write_node(): node == NULL\n");
return -EINVAL;
});
ASSERT(if (raw_inode && raw_inode->nsize && !name) {
printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
raw_inode->nsize);
return -EINVAL;
});
D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
"total_size = %u\n",
(name ? name : ""), raw_inode->ino,
total_size));
jffs_fm_write_lock(fmc);
retry:
fm = NULL;
err = 0;
while (!fm) {
/* Deadlocks suck. */
while(fmc->free_size < fmc->min_free_size + total_size + slack) {
jffs_fm_write_unlock(fmc);
if (!JFFS_ENOUGH_SPACE(c, total_size + slack))
return -ENOSPC;
jffs_fm_write_lock(fmc);
}
/* First try to allocate some flash memory. */
err = jffs_fmalloc(fmc, total_size, node, &fm);
if (err == -ENOSPC) {
/* Just out of space. GC and try again */
if (fmc->dirty_size < fmc->sector_size) {
D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
"failed, no dirty space to GC\n", fmc,
total_size));
return err;
}
D1(printk(KERN_INFO "jffs_write_node(): Calling jffs_garbage_collect_now()\n"));
jffs_fm_write_unlock(fmc);
if ((err = jffs_garbage_collect_now(c))) {
D(printk("jffs_write_node(): jffs_garbage_collect_now() failed\n"));
return err;
}
jffs_fm_write_lock(fmc);
continue;
}
if (err < 0) {
jffs_fm_write_unlock(fmc);
D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
"failed!\n", fmc, total_size));
return err;
}
if (!fm->nodes) {
/* The jffs_fm struct that we got is not good enough.
Make that space dirty and try again */
if ((err = jffs_write_dummy_node(c, fm)) < 0) {
kfree(fm);
DJM(no_jffs_fm--);
jffs_fm_write_unlock(fmc);
D(printk("jffs_write_node(): "
"jffs_write_dummy_node(): Failed!\n"));
return err;
}
fm = NULL;
}
} /* while(!fm) */
node->fm = fm;
ASSERT(if (fm->nodes == 0) {
printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
});
pos = node->fm->offset;
/* Increment the version number here. We can't let the caller
set it beforehand, because we might have had to do GC on a node
of this file - and we'd end up reusing version numbers.
*/
if (f) {
raw_inode->version = f->highest_version + 1;
D1(printk (KERN_NOTICE "jffs_write_node(): setting version of %s to %d\n", f->name, raw_inode->version));
/* if the file was deleted, set the deleted bit in the raw inode */
if (f->deleted)
raw_inode->deleted = 1;
}
/* Compute the checksum for the data and name chunks. */
raw_inode->dchksum = jffs_checksum(data, raw_inode->dsize);
raw_inode->nchksum = jffs_checksum(name, raw_inode->nsize);
/* The checksum is calculated without the chksum and accurate
fields so set them to zero first. */
raw_inode->accurate = 0;
raw_inode->chksum = 0;
raw_inode->chksum = jffs_checksum(raw_inode,
sizeof(struct jffs_raw_inode));
raw_inode->accurate = 0xff;
D3(printk("jffs_write_node(): About to write this raw inode to the "
"flash at pos 0x%lx:\n", (long)pos));
D3(jffs_print_raw_inode(raw_inode));
/* The actual raw JFFS node */
node_iovec[0].iov_base = (void *) raw_inode;
node_iovec[0].iov_len = (size_t) sizeof(struct jffs_raw_inode);
iovec_cnt = 1;
/* Get name and size if there is one */
if (raw_inode->nsize) {
node_iovec[iovec_cnt].iov_base = (void *) name;
node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->nsize;
iovec_cnt++;
if (JFFS_GET_PAD_BYTES(raw_inode->nsize)) {
static unsigned char allff[3]={255,255,255};
/* Add some extra padding if necessary */
node_iovec[iovec_cnt].iov_base = allff;
node_iovec[iovec_cnt].iov_len =
JFFS_GET_PAD_BYTES(raw_inode->nsize);
iovec_cnt++;
}
}
/* Get data and size if there is any */
if (raw_inode->dsize) {
node_iovec[iovec_cnt].iov_base = (void *) data;
node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->dsize;
iovec_cnt++;
/* No need to pad this because we're not actually putting
anything after it.
*/
}
if ((err = flash_safe_writev(fmc->mtd, node_iovec, iovec_cnt,
pos)) < 0) {
jffs_fmfree_partly(fmc, fm, 0);
jffs_fm_write_unlock(fmc);
printk(KERN_ERR "JFFS: jffs_write_node: Failed to write, "
"requested %i, wrote %i\n", total_size, err);
goto retry;
}
if (raw_inode->deleted)
f->deleted = 1;
jffs_fm_write_unlock(fmc);
D3(printk("jffs_write_node(): Leaving...\n"));
return raw_inode->dsize;
} /* jffs_write_node() */
/* Read data from the node and write it to the buffer. 'node_offset'
is how much we have read from this particular node before and which
shouldn't be read again. 'max_size' is how much space there is in
the buffer. */
static int
jffs_get_node_data(struct jffs_file *f, struct jffs_node *node,
unsigned char *buf,__u32 node_offset, __u32 max_size)
{
struct jffs_fmcontrol *fmc = f->c->fmc;
__u32 pos = node->fm->offset + node->fm_offset + node_offset;
__u32 avail = node->data_size - node_offset;
__u32 r;
D2(printk(" jffs_get_node_data(): file: \"%s\", ino: %u, "
"version: %u, node_offset: %u\n",
f->name, node->ino, node->version, node_offset));
r = min(avail, max_size);
D3(printk(KERN_NOTICE "jffs_get_node_data\n"));
flash_safe_read(fmc->mtd, pos, buf, r);
D3(printk(" jffs_get_node_data(): Read %u byte%s.\n",
r, (r == 1 ? "" : "s")));
return r;
}
/* Read data from the file's nodes. Write the data to the buffer
'buf'. 'read_offset' tells how much data we should skip. */
int
jffs_read_data(struct jffs_file *f, unsigned char *buf, __u32 read_offset,
__u32 size)
{
struct jffs_node *node;
__u32 read_data = 0; /* Total amount of read data. */
__u32 node_offset = 0;
__u32 pos = 0; /* Number of bytes traversed. */
D2(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
"size = %u\n",
(f->name ? f->name : ""), read_offset, size));
if (read_offset >= f->size) {
D(printk(" f->size: %d\n", f->size));
return 0;
}
/* First find the node to read data from. */
node = f->range_head;
while (pos <= read_offset) {
node_offset = read_offset - pos;
if (node_offset >= node->data_size) {
pos += node->data_size;
node = node->range_next;
}
else {
break;
}
}
/* "Cats are living proof that not everything in nature
has to be useful."
- Garrison Keilor ('97) */
/* Fill the buffer. */
while (node && (read_data < size)) {
int r;
if (!node->fm) {
/* This node does not refer to real data. */
r = min(size - read_data,
node->data_size - node_offset);
memset(&buf[read_data], 0, r);
}
else if ((r = jffs_get_node_data(f, node, &buf[read_data],
node_offset,
size - read_data)) < 0) {
return r;
}
read_data += r;
node_offset = 0;
node = node->range_next;
}
D3(printk(" jffs_read_data(): Read %u bytes.\n", read_data));
return read_data;
}
/* Used for traversing all nodes in the hash table. */
int
jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *))
{
int pos;
int r;
int result = 0;
for (pos = 0; pos < c->hash_len; pos++) {
struct jffs_file *f, *next;
/* We must do _safe, because 'func' might remove the
current file 'f' from the list. */
list_for_each_entry_safe(f, next, &c->hash[pos], hash) {
r = func(f);
if (r < 0)
return r;
result += r;
}
}
return result;
}
/* Free all nodes associated with a file. */
static int
jffs_free_node_list(struct jffs_file *f)
{
struct jffs_node *node;
struct jffs_node *p;
D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
f->ino, (f->name ? f->name : "")));
node = f->version_head;
while (node) {
p = node;
node = node->version_next;
jffs_free_node(p);
DJM(no_jffs_node--);
}
return 0;
}
/* Free a file and its name. */
static int
jffs_free_file(struct jffs_file *f)
{
D3(printk("jffs_free_file: f #%u, \"%s\"\n",
f->ino, (f->name ? f->name : "")));
if (f->name) {
kfree(f->name);
DJM(no_name--);
}
kfree(f);
no_jffs_file--;
return 0;
}
static long
jffs_get_file_count(void)
{
return no_jffs_file;
}
/* See if a file is deleted. If so, mark that file's nodes as obsolete. */
int
jffs_possibly_delete_file(struct jffs_file *f)
{
struct jffs_node *n;
D3(printk("jffs_possibly_delete_file(): ino: %u\n",
f->ino));
ASSERT(if (!f) {
printk(KERN_ERR "jffs_possibly_delete_file(): f == NULL\n");
return -1;
});
if (f->deleted) {
/* First try to remove all older versions. Commence with
the oldest node. */
for (n = f->version_head; n; n = n->version_next) {
if (!n->fm) {
continue;
}
if (jffs_fmfree(f->c->fmc, n->fm, n) < 0) {
break;
}
}
/* Unlink the file from the filesystem. */
if (!f->c->building_fs) {
jffs_unlink_file_from_tree(f);
}
jffs_unlink_file_from_hash(f);
jffs_free_node_list(f);
jffs_free_file(f);
}
return 0;
}
/* Used in conjunction with jffs_foreach_file() to count the number
of files in the file system. */
int
jffs_file_count(struct jffs_file *f)
{
return 1;
}
/* Build up a file's range list from scratch by going through the
version list. */
static int
jffs_build_file(struct jffs_file *f)
{
struct jffs_node *n;
D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
f->ino, (f->name ? f->name : "")));
for (n = f->version_head; n; n = n->version_next) {
jffs_update_file(f, n);
}
return 0;
}
/* Remove an amount of data from a file. If this amount of data is
zero, that could mean that a node should be split in two parts.
We remove or change the appropriate nodes in the lists.
Starting offset of area to be removed is node->data_offset,
and the length of the area is in node->removed_size. */
static int
jffs_delete_data(struct jffs_file *f, struct jffs_node *node)
{
struct jffs_node *n;
__u32 offset = node->data_offset;
__u32 remove_size = node->removed_size;
D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
offset, remove_size));
if (remove_size == 0
&& f->range_tail
&& f->range_tail->data_offset + f->range_tail->data_size
== offset) {
/* A simple append; nothing to remove or no node to split. */
return 0;
}
/* Find the node where we should begin the removal. */
for (n = f->range_head; n; n = n->range_next) {
if (n->data_offset + n->data_size > offset) {
break;
}
}
if (!n) {
/* If there's no data in the file there's no data to
remove either. */
return 0;
}
if (n->data_offset > offset) {
/* XXX: Not implemented yet. */
printk(KERN_WARNING "JFFS: An unexpected situation "
"occurred in jffs_delete_data.\n");
}
else if (n->data_offset < offset) {
/* See if the node has to be split into two parts. */
if (n->data_offset + n->data_size > offset + remove_size) {
/* Do the split. */
struct jffs_node *new_node;
D3(printk("jffs_delete_data(): Split node with "
"version number %u.\n", n->version));
if (!(new_node = jffs_alloc_node())) {
D(printk("jffs_delete_data(): -ENOMEM\n"));
return -ENOMEM;
}
DJM(no_jffs_node++);
new_node->ino = n->ino;
new_node->version = n->version;
new_node->data_offset = offset;
new_node->data_size = n->data_size - (remove_size + (offset - n->data_offset));
new_node->fm_offset = n->fm_offset + (remove_size + (offset - n->data_offset));
new_node->name_size = n->name_size;
new_node->fm = n->fm;
new_node->version_prev = n;
new_node->version_next = n->version_next;
if (new_node->version_next) {
new_node->version_next->version_prev
= new_node;
}
else {
f->version_tail = new_node;
}
n->version_next = new_node;
new_node->range_prev = n;
new_node->range_next = n->range_next;
if (new_node->range_next) {
new_node->range_next->range_prev = new_node;
}
else {
f->range_tail = new_node;
}
/* A very interesting can of worms. */
n->range_next = new_node;
n->data_size = offset - n->data_offset;
if (new_node->fm)
jffs_add_node(new_node);
else {
D1(printk(KERN_WARNING "jffs_delete_data(): Splitting an empty node (file hold).\n!"));
D1(printk(KERN_WARNING "FIXME: Did dwmw2 do the right thing here?\n"));
}
n = new_node->range_next;
remove_size = 0;
}
else {
/* No. No need to split the node. Just remove
the end of the node. */
int r = min(n->data_offset + n->data_size
- offset, remove_size);
n->data_size -= r;
remove_size -= r;
n = n->range_next;
}
}
/* Remove as many nodes as necessary. */
while (n && remove_size) {
if (n->data_size <= remove_size) {
struct jffs_node *p = n;
remove_size -= n->data_size;
n = n->range_next;
D3(printk("jffs_delete_data(): Removing node: "
"ino: %u, version: %u%s\n",
p->ino, p->version,
(p->fm ? "" : " (virtual)")));
if (p->fm) {
jffs_fmfree(f->c->fmc, p->fm, p);
}
jffs_unlink_node_from_range_list(f, p);
jffs_unlink_node_from_version_list(f, p);
jffs_free_node(p);
DJM(no_jffs_node--);
}
else {
n->data_size -= remove_size;
n->fm_offset += remove_size;
n->data_offset -= (node->removed_size - remove_size);
n = n->range_next;
break;
}
}
/* Adjust the following nodes' information about offsets etc. */
while (n && node->removed_size) {
n->data_offset -= node->removed_size;
n = n->range_next;
}
if (node->removed_size > (f->size - node->data_offset)) {
/* It's possible that the removed_size is in fact
* greater than the amount of data we actually thought
* were present in the first place - some of the nodes
* which this node originally obsoleted may already have
* been deleted from the flash by subsequent garbage
* collection.
*
* If this is the case, don't let f->size go negative.
* Bad things would happen :)
*/
f->size = node->data_offset;
} else {
f->size -= node->removed_size;
}
D3(printk("jffs_delete_data(): f->size = %d\n", f->size));
return 0;
} /* jffs_delete_data() */
/* Insert some data into a file. Prior to the call to this function,
jffs_delete_data should be called. */
static int
jffs_insert_data(struct jffs_file *f, struct jffs_node *node)
{
D3(printk("jffs_insert_data(): node->data_offset = %u, "
"node->data_size = %u, f->size = %u\n",
node->data_offset, node->data_size, f->size));
/* Find the position where we should insert data. */
retry:
if (node->data_offset == f->size) {
/* A simple append. This is the most common operation. */
node->range_next = NULL;
node->range_prev = f->range_tail;
if (node->range_prev) {
node->range_prev->range_next = node;
}
f->range_tail = node;
f->size += node->data_size;
if (!f->range_head) {
f->range_head = node;
}
}
else if (node->data_offset < f->size) {
/* Trying to insert data into the middle of the file. This
means no problem because jffs_delete_data() has already
prepared the range list for us. */
struct jffs_node *n;
/* Find the correct place for the insertion and then insert
the node. */
for (n = f->range_head; n; n = n->range_next) {
D2(printk("Cool stuff's happening!\n"));
if (n->data_offset == node->data_offset) {
node->range_prev = n->range_prev;
if (node->range_prev) {
node->range_prev->range_next = node;
}
else {
f->range_head = node;
}
node->range_next = n;
n->range_prev = node;
break;
}
ASSERT(else if (n->data_offset + n->data_size >
node->data_offset) {
printk(KERN_ERR "jffs_insert_data(): "
"Couldn't find a place to insert "
"the data!\n");
return -1;
});
}
/* Adjust later nodes' offsets etc. */
n = node->range_next;
while (n) {
n->data_offset += node->data_size;
n = n->range_next;
}
f->size += node->data_size;
}
else if (node->data_offset > f->size) {
/* Okay. This is tricky. This means that we want to insert
data at a place that is beyond the limits of the file as
it is constructed right now. This is actually a common
event that for instance could occur during the mounting
of the file system if a large file have been truncated,
rewritten and then only partially garbage collected. */
struct jffs_node *n;
/* We need a place holder for the data that is missing in
front of this insertion. This "virtual node" will not
be associated with any space on the flash device. */
struct jffs_node *virtual_node;
if (!(virtual_node = jffs_alloc_node())) {
return -ENOMEM;
}
D(printk("jffs_insert_data: Inserting a virtual node.\n"));
D(printk(" node->data_offset = %u\n", node->data_offset));
D(printk(" f->size = %u\n", f->size));
virtual_node->ino = node->ino;
virtual_node->version = node->version;
virtual_node->removed_size = 0;
virtual_node->fm_offset = 0;
virtual_node->name_size = 0;
virtual_node->fm = NULL; /* This is a virtual data holder. */
virtual_node->version_prev = NULL;
virtual_node->version_next = NULL;
virtual_node->range_next = NULL;
/* Are there any data at all in the file yet? */
if (f->range_head) {
virtual_node->data_offset
= f->range_tail->data_offset
+ f->range_tail->data_size;
virtual_node->data_size
= node->data_offset - virtual_node->data_offset;
virtual_node->range_prev = f->range_tail;
f->range_tail->range_next = virtual_node;
}
else {
virtual_node->data_offset = 0;
virtual_node->data_size = node->data_offset;
virtual_node->range_prev = NULL;
f->range_head = virtual_node;
}
f->range_tail = virtual_node;
f->size += virtual_node->data_size;
/* Insert this virtual node in the version list as well. */
for (n = f->version_head; n ; n = n->version_next) {
if (n->version == virtual_node->version) {
virtual_node->version_prev = n->version_prev;
n->version_prev = virtual_node;
if (virtual_node->version_prev) {
virtual_node->version_prev
->version_next = virtual_node;
}
else {
f->version_head = virtual_node;
}
virtual_node->version_next = n;
break;
}
}
D(jffs_print_node(virtual_node));
/* Make a new try to insert the node. */
goto retry;
}
D3(printk("jffs_insert_data(): f->size = %d\n", f->size));
return 0;
}
/* A new node (with data) has been added to the file and now the range
list has to be modified. */
static int
jffs_update_file(struct jffs_file *f, struct jffs_node *node)
{
int err;
D3(printk("jffs_update_file(): ino: %u, version: %u\n",
f->ino, node->version));
if (node->data_size == 0) {
if (node->removed_size == 0) {
/* data_offset == X */
/* data_size == 0 */
/* remove_size == 0 */
}
else {
/* data_offset == X */
/* data_size == 0 */
/* remove_size != 0 */
if ((err = jffs_delete_data(f, node)) < 0) {
return err;
}
}
}
else {
/* data_offset == X */
/* data_size != 0 */
/* remove_size == Y */
if ((err = jffs_delete_data(f, node)) < 0) {
return err;
}
if ((err = jffs_insert_data(f, node)) < 0) {
return err;
}
}
return 0;
}
/* Print the contents of a file. */
#if 0
int
jffs_print_file(struct jffs_file *f)
{
D(int i);
D(printk("jffs_file: 0x%p\n", f));
D(printk("{\n"));
D(printk(" 0x%08x, /* ino */\n", f->ino));
D(printk(" 0x%08x, /* pino */\n", f->pino));
D(printk(" 0x%08x, /* mode */\n", f->mode));
D(printk(" 0x%04x, /* uid */\n", f->uid));
D(printk(" 0x%04x, /* gid */\n", f->gid));
D(printk(" 0x%08x, /* atime */\n", f->atime));
D(printk(" 0x%08x, /* mtime */\n", f->mtime));
D(printk(" 0x%08x, /* ctime */\n", f->ctime));
D(printk(" 0x%02x, /* nsize */\n", f->nsize));
D(printk(" 0x%02x, /* nlink */\n", f->nlink));
D(printk(" 0x%02x, /* deleted */\n", f->deleted));
D(printk(" \"%s\", ", (f->name ? f->name : "")));
D(for (i = strlen(f->name ? f->name : ""); i < 8; ++i) {
printk(" ");
});
D(printk("/* name */\n"));
D(printk(" 0x%08x, /* size */\n", f->size));
D(printk(" 0x%08x, /* highest_version */\n",
f->highest_version));
D(printk(" 0x%p, /* c */\n", f->c));
D(printk(" 0x%p, /* parent */\n", f->parent));
D(printk(" 0x%p, /* children */\n", f->children));
D(printk(" 0x%p, /* sibling_prev */\n", f->sibling_prev));
D(printk(" 0x%p, /* sibling_next */\n", f->sibling_next));
D(printk(" 0x%p, /* hash_prev */\n", f->hash.prev));
D(printk(" 0x%p, /* hash_next */\n", f->hash.next));
D(printk(" 0x%p, /* range_head */\n", f->range_head));
D(printk(" 0x%p, /* range_tail */\n", f->range_tail));
D(printk(" 0x%p, /* version_head */\n", f->version_head));
D(printk(" 0x%p, /* version_tail */\n", f->version_tail));
D(printk("}\n"));
return 0;
}
#endif /* 0 */
void
jffs_print_hash_table(struct jffs_control *c)
{
int i;
printk("JFFS: Dumping the file system's hash table...\n");
for (i = 0; i < c->hash_len; i++) {
struct jffs_file *f;
list_for_each_entry(f, &c->hash[i], hash) {
printk("*** c->hash[%u]: \"%s\" "
"(ino: %u, pino: %u)\n",
i, (f->name ? f->name : ""),
f->ino, f->pino);
}
}
}
void
jffs_print_tree(struct jffs_file *first_file, int indent)
{
struct jffs_file *f;
char *space;
int dir;
if (!first_file) {
return;
}
if (!(space = kmalloc(indent + 1, GFP_KERNEL))) {
printk("jffs_print_tree(): Out of memory!\n");
return;
}
memset(space, ' ', indent);
space[indent] = '\0';
for (f = first_file; f; f = f->sibling_next) {
dir = S_ISDIR(f->mode);
printk("%s%s%s (ino: %u, highest_version: %u, size: %u)\n",
space, (f->name ? f->name : ""), (dir ? "/" : ""),
f->ino, f->highest_version, f->size);
if (dir) {
jffs_print_tree(f->children, indent + 2);
}
}
kfree(space);
}
#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
void
jffs_print_memory_allocation_statistics(void)
{
static long printout;
printk("________ Memory printout #%ld ________\n", ++printout);
printk("no_jffs_file = %ld\n", no_jffs_file);
printk("no_jffs_node = %ld\n", no_jffs_node);
printk("no_jffs_control = %ld\n", no_jffs_control);
printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode);
printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref);
printk("no_jffs_fm = %ld\n", no_jffs_fm);
printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol);
printk("no_hash = %ld\n", no_hash);
printk("no_name = %ld\n", no_name);
printk("\n");
}
#endif
/* Rewrite `size' bytes, and begin at `node'. */
static int
jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, __u32 size)
{
struct jffs_control *c = f->c;
struct jffs_fmcontrol *fmc = c->fmc;
struct jffs_raw_inode raw_inode;
struct jffs_node *new_node;
struct jffs_fm *fm;
__u32 pos;
__u32 pos_dchksum;
__u32 total_name_size;
__u32 total_data_size;
__u32 total_size;
int err;
D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
f->ino, (f->name ? f->name : "(null)"), size));
/* Create and initialize the new node. */
if (!(new_node = jffs_alloc_node())) {
D(printk("jffs_rewrite_data(): "
"Failed to allocate node.\n"));
return -ENOMEM;
}
DJM(no_jffs_node++);
new_node->data_offset = node->data_offset;
new_node->removed_size = size;
total_name_size = JFFS_PAD(f->nsize);
total_data_size = JFFS_PAD(size);
total_size = sizeof(struct jffs_raw_inode)
+ total_name_size + total_data_size;
new_node->fm_offset = sizeof(struct jffs_raw_inode)
+ total_name_size;
retry:
jffs_fm_write_lock(fmc);
err = 0;
if ((err = jffs_fmalloc(fmc, total_size, new_node, &fm)) < 0) {
DJM(no_jffs_node--);
jffs_fm_write_unlock(fmc);
D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
jffs_free_node(new_node);
return err;
}
else if (!fm->nodes) {
/* The jffs_fm struct that we got is not big enough. */
/* This should never happen, because we deal with this case
in jffs_garbage_collect_next().*/
printk(KERN_WARNING "jffs_rewrite_data(): Allocated node is too small (%d bytes of %d)\n", fm->size, total_size);
if ((err = jffs_write_dummy_node(c, fm)) < 0) {
D(printk("jffs_rewrite_data(): "
"jffs_write_dummy_node() Failed!\n"));
} else {
err = -ENOSPC;
}
DJM(no_jffs_fm--);
jffs_fm_write_unlock(fmc);
kfree(fm);
return err;
}
new_node->fm = fm;
/* Initialize the raw inode. */
raw_inode.magic = JFFS_MAGIC_BITMASK;
raw_inode.ino = f->ino;
raw_inode.pino = f->pino;
raw_inode.version = f->highest_version + 1;
raw_inode.mode = f->mode;
raw_inode.uid = f->uid;
raw_inode.gid = f->gid;
raw_inode.atime = f->atime;
raw_inode.mtime = f->mtime;
raw_inode.ctime = f->ctime;
raw_inode.offset = node->data_offset;
raw_inode.dsize = size;
raw_inode.rsize = size;
raw_inode.nsize = f->nsize;
raw_inode.nlink = f->nlink;
raw_inode.spare = 0;
raw_inode.rename = 0;
raw_inode.deleted = f->deleted;
raw_inode.accurate = 0xff;
raw_inode.dchksum = 0;
raw_inode.nchksum = 0;
pos = new_node->fm->offset;
pos_dchksum = pos +JFFS_RAW_INODE_DCHKSUM_OFFSET;
D3(printk("jffs_rewrite_data(): Writing this raw inode "
"to pos 0x%ul.\n", pos));
D3(jffs_print_raw_inode(&raw_inode));
if ((err = flash_safe_write(fmc->mtd, pos,
(u_char *) &raw_inode,
sizeof(struct jffs_raw_inode)
- sizeof(__u32)
- sizeof(__u16) - sizeof(__u16))) < 0) {
jffs_fmfree_partly(fmc, fm,
total_name_size + total_data_size);
jffs_fm_write_unlock(fmc);
printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
"rewrite. (raw inode)\n");
printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
"rewrite. (raw inode)\n");
goto retry;
}
pos += sizeof(struct jffs_raw_inode);
/* Write the name to the flash memory. */
if (f->nsize) {
D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
"pos 0x%ul.\n", f->name, (unsigned int) pos));
if ((err = flash_safe_write(fmc->mtd, pos,
(u_char *)f->name,
f->nsize)) < 0) {
jffs_fmfree_partly(fmc, fm, total_data_size);
jffs_fm_write_unlock(fmc);
printk(KERN_ERR "JFFS: jffs_rewrite_data: Write "
"error during rewrite. (name)\n");
printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
"rewrite. (name)\n");
goto retry;
}
pos += total_name_size;
raw_inode.nchksum = jffs_checksum(f->name, f->nsize);
}
/* Write the data. */
if (size) {
int r;
unsigned char *page;
__u32 offset = node->data_offset;
if (!(page = (unsigned char *)__get_free_page(GFP_KERNEL))) {
jffs_fmfree_partly(fmc, fm, 0);
return -1;
}
while (size) {
__u32 s = min(size, (__u32)PAGE_SIZE);
if ((r = jffs_read_data(f, (char *)page,
offset, s)) < s) {
free_page((unsigned long)page);
jffs_fmfree_partly(fmc, fm, 0);
jffs_fm_write_unlock(fmc);
printk(KERN_ERR "JFFS: jffs_rewrite_data: "
"jffs_read_data() "
"failed! (r = %d)\n", r);
return -1;
}
if ((err = flash_safe_write(fmc->mtd,
pos, page, r)) < 0) {
free_page((unsigned long)page);
jffs_fmfree_partly(fmc, fm, 0);
jffs_fm_write_unlock(fmc);
printk(KERN_ERR "JFFS: jffs_rewrite_data: "
"Write error during rewrite. "
"(data)\n");
goto retry;
}
pos += r;
size -= r;
offset += r;
raw_inode.dchksum += jffs_checksum(page, r);
}
free_page((unsigned long)page);
}
raw_inode.accurate = 0;
raw_inode.chksum = jffs_checksum(&raw_inode,
sizeof(struct jffs_raw_inode)
- sizeof(__u16));
/* Add the checksum. */
if ((err
= flash_safe_write(fmc->mtd, pos_dchksum,
&((u_char *)
&raw_inode)[JFFS_RAW_INODE_DCHKSUM_OFFSET],
sizeof(__u32) + sizeof(__u16)
+ sizeof(__u16))) < 0) {
jffs_fmfree_partly(fmc, fm, 0);
jffs_fm_write_unlock(fmc);
printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
"rewrite. (checksum)\n");
goto retry;
}
/* Now make the file system aware of the newly written node. */
jffs_insert_node(c, f, &raw_inode, f->name, new_node);
jffs_fm_write_unlock(fmc);
D3(printk("jffs_rewrite_data(): Leaving...\n"));
return 0;
} /* jffs_rewrite_data() */
/* jffs_garbage_collect_next implements one step in the garbage collect
process and is often called multiple times at each occasion of a
garbage collect. */
static int
jffs_garbage_collect_next(struct jffs_control *c)
{
struct jffs_fmcontrol *fmc = c->fmc;
struct jffs_node *node;
struct jffs_file *f;
int err = 0;
__u32 size;
__u32 data_size;
__u32 total_name_size;
__u32 extra_available;
__u32 space_needed;
__u32 free_chunk_size1 = jffs_free_size1(fmc);
D2(__u32 free_chunk_size2 = jffs_free_size2(fmc));
/* Get the oldest node in the flash. */
node = jffs_get_oldest_node(fmc);
ASSERT(if (!node) {
printk(KERN_ERR "JFFS: jffs_garbage_collect_next: "
"No oldest node found!\n");
err = -1;
goto jffs_garbage_collect_next_end;
});
/* Find its corresponding file too. */
f = jffs_find_file(c, node->ino);
if (!f) {
printk (KERN_ERR "JFFS: jffs_garbage_collect_next: "
"No file to garbage collect! "
"(ino = 0x%08x)\n", node->ino);
/* FIXME: Free the offending node and recover. */
err = -1;
goto jffs_garbage_collect_next_end;
}
/* We always write out the name. Theoretically, we don't need
to, but for now it's easier - because otherwise we'd have
to keep track of how many times the current name exists on
the flash and make sure it never reaches zero.
The current approach means that would be possible to cause
the GC to end up eating its tail by writing lots of nodes
with no name for it to garbage-collect. Hence the change in
inode.c to write names with _every_ node.
It sucks, but it _should_ work.
*/
total_name_size = JFFS_PAD(f->nsize);
D1(printk("jffs_garbage_collect_next(): \"%s\", "
"ino: %u, version: %u, location 0x%x, dsize %u\n",
(f->name ? f->name : ""), node->ino, node->version,
node->fm->offset, node->data_size));
/* Compute how many data it's possible to rewrite at the moment. */
data_size = f->size - node->data_offset;
/* And from that, the total size of the chunk we want to write */
size = sizeof(struct jffs_raw_inode) + total_name_size
+ data_size + JFFS_GET_PAD_BYTES(data_size);
/* If that's more than max_chunk_size, reduce it accordingly */
if (size > fmc->max_chunk_size) {
size = fmc->max_chunk_size;
data_size = size - sizeof(struct jffs_raw_inode)
- total_name_size;
}
/* If we're asking to take up more space than free_chunk_size1
but we _could_ fit in it, shrink accordingly.
*/
if (size > free_chunk_size1) {
if (free_chunk_size1 <
(sizeof(struct jffs_raw_inode) + total_name_size + BLOCK_SIZE)){
/* The space left is too small to be of any
use really. */
struct jffs_fm *dirty_fm
= jffs_fmalloced(fmc,
fmc->tail->offset + fmc->tail->size,
free_chunk_size1, NULL);
if (!dirty_fm) {
printk(KERN_ERR "JFFS: "
"jffs_garbage_collect_next: "
"Failed to allocate `dirty' "
"flash memory!\n");
err = -1;
goto jffs_garbage_collect_next_end;
}
D1(printk("Dirtying end of flash - too small\n"));
jffs_write_dummy_node(c, dirty_fm);
err = 0;
goto jffs_garbage_collect_next_end;
}
D1(printk("Reducing size of new node from %d to %d to avoid "
" exceeding free_chunk_size1\n",
size, free_chunk_size1));
size = free_chunk_size1;
data_size = size - sizeof(struct jffs_raw_inode)
- total_name_size;
}
/* Calculate the amount of space needed to hold the nodes
which are remaining in the tail */
space_needed = fmc->min_free_size - (node->fm->offset % fmc->sector_size);
/* From that, calculate how much 'extra' space we can use to
increase the size of the node we're writing from the size
of the node we're obsoleting
*/
if (space_needed > fmc->free_size) {
/* If we've gone below min_free_size for some reason,
don't fuck up. This is why we have
min_free_size > sector_size. Whinge about it though,
just so I can convince myself my maths is right.
*/
D1(printk(KERN_WARNING "jffs_garbage_collect_next(): "
"space_needed %d exceeded free_size %d\n",
space_needed, fmc->free_size));
extra_available = 0;
} else {
extra_available = fmc->free_size - space_needed;
}
/* Check that we don't use up any more 'extra' space than
what's available */
if (size > JFFS_PAD(node->data_size) + total_name_size +
sizeof(struct jffs_raw_inode) + extra_available) {
D1(printk("Reducing size of new node from %d to %ld to avoid "
"catching our tail\n", size,
(long) (JFFS_PAD(node->data_size) + JFFS_PAD(node->name_size) +
sizeof(struct jffs_raw_inode) + extra_available)));
D1(printk("space_needed = %d, extra_available = %d\n",
space_needed, extra_available));
size = JFFS_PAD(node->data_size) + total_name_size +
sizeof(struct jffs_raw_inode) + extra_available;
data_size = size - sizeof(struct jffs_raw_inode)
- total_name_size;
};
D2(printk(" total_name_size: %u\n", total_name_size));
D2(printk(" data_size: %u\n", data_size));
D2(printk(" size: %u\n", size));
D2(printk(" f->nsize: %u\n", f->nsize));
D2(printk(" f->size: %u\n", f->size));
D2(printk(" node->data_offset: %u\n", node->data_offset));
D2(printk(" free_chunk_size1: %u\n", free_chunk_size1));
D2(printk(" free_chunk_size2: %u\n", free_chunk_size2));
D2(printk(" node->fm->offset: 0x%08x\n", node->fm->offset));
if ((err = jffs_rewrite_data(f, node, data_size))) {
printk(KERN_WARNING "jffs_rewrite_data() failed: %d\n", err);
return err;
}
jffs_garbage_collect_next_end:
D3(printk("jffs_garbage_collect_next: Leaving...\n"));
return err;
} /* jffs_garbage_collect_next */
/* If an obsolete node is partly going to be erased due to garbage
collection, the part that isn't going to be erased must be filled
with zeroes so that the scan of the flash will work smoothly next
time. (The data in the file could for instance be a JFFS image
which could cause enormous confusion during a scan of the flash
device if we didn't do this.)
There are two phases in this procedure: First, the clearing of
the name and data parts of the node. Second, possibly also clearing
a part of the raw inode as well. If the box is power cycled during
the first phase, only the checksum of this node-to-be-cleared-at-
the-end will be wrong. If the box is power cycled during, or after,
the clearing of the raw inode, the information like the length of
the name and data parts are zeroed. The next time the box is
powered up, the scanning algorithm manages this faulty data too
because:
- The checksum is invalid and thus the raw inode must be discarded
in any case.
- If the lengths of the data part or the name part are zeroed, the
scanning just continues after the raw inode. But after the inode
the scanning procedure just finds zeroes which is the same as
dirt.
So, in the end, this could never fail. :-) Even if it does fail,
the scanning algorithm should manage that too. */
static int
jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
{
struct jffs_fm *fm;
struct jffs_fmcontrol *fmc = c->fmc;
__u32 zero_offset;
__u32 zero_size;
__u32 zero_offset_data;
__u32 zero_size_data;
__u32 cutting_raw_inode = 0;
if (!(fm = jffs_cut_node(fmc, erase_size))) {
D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
return 0;
}
/* Where and how much shall we clear? */
zero_offset = fmc->head->offset + erase_size;
zero_size = fm->offset + fm->size - zero_offset;
/* Do we have to clear the raw_inode explicitly? */
if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
cutting_raw_inode = sizeof(struct jffs_raw_inode)
- (fm->size - zero_size);
}
/* First, clear the name and data fields. */
zero_offset_data = zero_offset + cutting_raw_inode;
zero_size_data = zero_size - cutting_raw_inode;
flash_safe_acquire(fmc->mtd);
flash_memset(fmc->mtd, zero_offset_data, 0, zero_size_data);
flash_safe_release(fmc->mtd);
/* Should we clear a part of the raw inode? */
if (cutting_raw_inode) {
/* I guess it is ok to clear the raw inode in this order. */
flash_safe_acquire(fmc->mtd);
flash_memset(fmc->mtd, zero_offset, 0,
cutting_raw_inode);
flash_safe_release(fmc->mtd);
}
return 0;
} /* jffs_clear_end_of_node() */
/* Try to erase as much as possible of the dirt in the flash memory. */
static long
jffs_try_to_erase(struct jffs_control *c)
{
struct jffs_fmcontrol *fmc = c->fmc;
long erase_size;
int err;
__u32 offset;
D3(printk("jffs_try_to_erase()\n"));
erase_size = jffs_erasable_size(fmc);
D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size));
if (erase_size == 0) {
return 0;
}
else if (erase_size < 0) {
printk(KERN_ERR "JFFS: jffs_try_to_erase: "
"jffs_erasable_size returned %ld.\n", erase_size);
return erase_size;
}
if ((err = jffs_clear_end_of_node(c, erase_size)) < 0) {
printk(KERN_ERR "JFFS: jffs_try_to_erase: "
"Clearing of node failed.\n");
return err;
}
offset = fmc->head->offset;
/* Now, let's try to do the erase. */
if ((err = flash_erase_region(fmc->mtd,
offset, erase_size)) < 0) {
printk(KERN_ERR "JFFS: Erase of flash failed. "
"offset = %u, erase_size = %ld\n",
offset, erase_size);
/* XXX: Here we should allocate this area as dirty
with jffs_fmalloced or something similar. Now
we just report the error. */
return err;
}
#if 0
/* Check if the erased sectors really got erased. */
{
__u32 pos;
__u32 end;
pos = (__u32)flash_get_direct_pointer(to_kdev_t(c->sb->s_dev), offset);
end = pos + erase_size;
D2(printk("JFFS: Checking erased sector(s)...\n"));
flash_safe_acquire(fmc->mtd);
for (; pos < end; pos += 4) {
if (*(__u32 *)pos != JFFS_EMPTY_BITMASK) {
printk("JFFS: Erase failed! pos = 0x%lx\n",
(long)pos);
jffs_hexdump(fmc->mtd, pos,
jffs_min(256, end - pos));
err = -1;
break;
}
}
flash_safe_release(fmc->mtd);
if (!err) {
D2(printk("JFFS: Erase succeeded.\n"));
}
else {
/* XXX: Here we should allocate the memory
with jffs_fmalloced() in order to prevent
JFFS from using this area accidentally. */
return err;
}
}
#endif
/* Update the flash memory data structures. */
jffs_sync_erase(fmc, erase_size);
return erase_size;
}
/* There are different criteria that should trigger a garbage collect:
1. There is too much dirt in the memory.
2. The free space is becoming small.
3. There are many versions of a node.
The garbage collect should always be done in a manner that guarantees
that future garbage collects cannot be locked. E.g. Rewritten chunks
should not be too large (span more than one sector in the flash memory
for exemple). Of course there is a limit on how intelligent this garbage
collection can be. */
static int
jffs_garbage_collect_now(struct jffs_control *c)
{
struct jffs_fmcontrol *fmc = c->fmc;
long erased = 0;
int result = 0;
D1(int i = 1);
D2(printk("***jffs_garbage_collect_now(): fmc->dirty_size = %u, fmc->free_size = 0x%x\n, fcs1=0x%x, fcs2=0x%x",
fmc->dirty_size, fmc->free_size, jffs_free_size1(fmc), jffs_free_size2(fmc)));
D2(jffs_print_fmcontrol(fmc));
// down(&fmc->gclock);
/* If it is possible to garbage collect, do so. */
while (erased == 0) {
D1(printk("***jffs_garbage_collect_now(): round #%u, "
"fmc->dirty_size = %u\n", i++, fmc->dirty_size));
D2(jffs_print_fmcontrol(fmc));
if ((erased = jffs_try_to_erase(c)) < 0) {
printk(KERN_WARNING "JFFS: Error in "
"garbage collector.\n");
result = erased;
goto gc_end;
}
if (erased)
break;
if (fmc->free_size == 0) {
/* Argh */
printk(KERN_ERR "jffs_garbage_collect_now(): free_size == 0. This is BAD.\n");
result = -ENOSPC;
break;
}
if (fmc->dirty_size < fmc->sector_size) {
/* Actually, we _may_ have been able to free some,
* if there are many overlapping nodes which aren't
* actually marked dirty because they still have
* some valid data in each.
*/
result = -ENOSPC;
break;
}
/* Let's dare to make a garbage collect. */
if ((result = jffs_garbage_collect_next(c)) < 0) {
printk(KERN_ERR "JFFS: Something "
"has gone seriously wrong "
"with a garbage collect.\n");
goto gc_end;
}
D1(printk(" jffs_garbage_collect_now(): erased: %ld\n", erased));
DJM(jffs_print_memory_allocation_statistics());
}
gc_end:
// up(&fmc->gclock);
D3(printk(" jffs_garbage_collect_now(): Leaving...\n"));
D1(if (erased) {
printk("jffs_g_c_now(): erased = %ld\n", erased);
jffs_print_fmcontrol(fmc);
});
if (!erased && !result)
return -ENOSPC;
return result;
} /* jffs_garbage_collect_now() */
/* Determine if it is reasonable to start garbage collection.
We start a gc pass if either:
- The number of free bytes < MIN_FREE_BYTES && at least one
block is dirty, OR
- The number of dirty bytes > MAX_DIRTY_BYTES
*/
static inline int thread_should_wake (struct jffs_control *c)
{
D1(printk (KERN_NOTICE "thread_should_wake(): free=%d, dirty=%d, blocksize=%d.\n",
c->fmc->free_size, c->fmc->dirty_size, c->fmc->sector_size));
/* If there's not enough dirty space to free a block, there's no point. */
if (c->fmc->dirty_size < c->fmc->sector_size) {
D2(printk(KERN_NOTICE "thread_should_wake(): Not waking. Insufficient dirty space\n"));
return 0;
}
#if 1
/* If there is too much RAM used by the various structures, GC */
if (jffs_get_node_inuse() > (c->fmc->used_size/c->fmc->max_chunk_size * 5 + jffs_get_file_count() * 2 + 50)) {
/* FIXME: Provide proof that this test can be satisfied. We
don't want a filesystem doing endless GC just because this
condition cannot ever be false.
*/
D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to number of nodes\n"));
return 1;
}
#endif
/* If there are fewer free bytes than the threshold, GC */
if (c->fmc->free_size < c->gc_minfree_threshold) {
D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to insufficent free space\n"));
return 1;
}
/* If there are more dirty bytes than the threshold, GC */
if (c->fmc->dirty_size > c->gc_maxdirty_threshold) {
D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to excessive dirty space\n"));
return 1;
}
/* FIXME: What about the "There are many versions of a node" condition? */
return 0;
}
void jffs_garbage_collect_trigger(struct jffs_control *c)
{
/* NOTE: We rely on the fact that we have the BKL here.
* Otherwise, the gc_task could go away between the check
* and the wake_up_process()
*/
if (c->gc_task && thread_should_wake(c))
send_sig(SIGHUP, c->gc_task, 1);
}
/* Kernel threads take (void *) as arguments. Thus we pass
the jffs_control data as a (void *) and then cast it. */
int
jffs_garbage_collect_thread(void *ptr)
{
struct jffs_control *c = (struct jffs_control *) ptr;
struct jffs_fmcontrol *fmc = c->fmc;
long erased;
int result = 0;
D1(int i = 1);
daemonize("jffs_gcd");
c->gc_task = current;
lock_kernel();
init_completion(&c->gc_thread_comp); /* barrier */
spin_lock_irq(&current->sighand->siglock);
siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
recalc_sigpending();
spin_unlock_irq(&current->sighand->siglock);
D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): Starting infinite loop.\n"));
for (;;) {
/* See if we need to start gc. If we don't, go to sleep.
Current implementation is a BAD THING(tm). If we try
to unmount the FS, the unmount operation will sleep waiting
for this thread to exit. We need to arrange to send it a
sig before the umount process sleeps.
*/
if (!thread_should_wake(c))
set_current_state (TASK_INTERRUPTIBLE);
schedule(); /* Yes, we do this even if we want to go
on immediately - we're a low priority
background task. */
/* Put_super will send a SIGKILL and then wait on the sem.
*/
while (signal_pending(current)) {
siginfo_t info;
unsigned long signr = 0;
if (try_to_freeze())
continue;
spin_lock_irq(&current->sighand->siglock);
signr = dequeue_signal(current, &current->blocked, &info);
spin_unlock_irq(&current->sighand->siglock);
switch(signr) {
case SIGSTOP:
D1(printk("jffs_garbage_collect_thread(): SIGSTOP received.\n"));
set_current_state(TASK_STOPPED);
schedule();
break;
case SIGKILL:
D1(printk("jffs_garbage_collect_thread(): SIGKILL received.\n"));
c->gc_task = NULL;
complete_and_exit(&c->gc_thread_comp, 0);
}
}
D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): collecting.\n"));
D3(printk (KERN_NOTICE "g_c_thread(): down biglock\n"));
mutex_lock(&fmc->biglock);
D1(printk("***jffs_garbage_collect_thread(): round #%u, "
"fmc->dirty_size = %u\n", i++, fmc->dirty_size));
D2(jffs_print_fmcontrol(fmc));
if ((erased = jffs_try_to_erase(c)) < 0) {
printk(KERN_WARNING "JFFS: Error in "
"garbage collector: %ld.\n", erased);
}
if (erased)
goto gc_end;
if (fmc->free_size == 0) {
/* Argh. Might as well commit suicide. */
printk(KERN_ERR "jffs_garbage_collect_thread(): free_size == 0. This is BAD.\n");
send_sig(SIGQUIT, c->gc_task, 1);
// panic()
goto gc_end;
}
/* Let's dare to make a garbage collect. */
if ((result = jffs_garbage_collect_next(c)) < 0) {
printk(KERN_ERR "JFFS: Something "
"has gone seriously wrong "
"with a garbage collect: %d\n", result);
}
gc_end:
D3(printk (KERN_NOTICE "g_c_thread(): up biglock\n"));
mutex_unlock(&fmc->biglock);
} /* for (;;) */
} /* jffs_garbage_collect_thread() */
/*
* JFFS -- Journaling Flash File System, Linux implementation.
*
* Copyright (C) 1999, 2000 Axis Communications AB.
*
* Created by Finn Hakansson <finn@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: intrep.h,v 1.14 2001/09/23 23:28:37 dwmw2 Exp $
*
*/
#ifndef __LINUX_JFFS_INTREP_H__
#define __LINUX_JFFS_INTREP_H__
#include "jffs_fm.h"
struct jffs_node *jffs_alloc_node(void);
void jffs_free_node(struct jffs_node *n);
int jffs_get_node_inuse(void);
void jffs_cleanup_control(struct jffs_control *c);
int jffs_build_fs(struct super_block *sb);
int jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
const struct jffs_raw_inode *raw_inode,
const char *name, struct jffs_node *node);
struct jffs_file *jffs_find_file(struct jffs_control *c, __u32 ino);
struct jffs_file *jffs_find_child(struct jffs_file *dir, const char *name, int len);
void jffs_free_node(struct jffs_node *node);
int jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *));
int jffs_possibly_delete_file(struct jffs_file *f);
int jffs_insert_file_into_tree(struct jffs_file *f);
int jffs_unlink_file_from_tree(struct jffs_file *f);
int jffs_file_count(struct jffs_file *f);
int jffs_write_node(struct jffs_control *c, struct jffs_node *node,
struct jffs_raw_inode *raw_inode,
const char *name, const unsigned char *buf,
int recoverable, struct jffs_file *f);
int jffs_read_data(struct jffs_file *f, unsigned char *buf, __u32 read_offset, __u32 size);
/* Garbage collection stuff. */
int jffs_garbage_collect_thread(void *c);
void jffs_garbage_collect_trigger(struct jffs_control *c);
/* For debugging purposes. */
#if 0
int jffs_print_file(struct jffs_file *f);
#endif /* 0 */
void jffs_print_hash_table(struct jffs_control *c);
void jffs_print_tree(struct jffs_file *first_file, int indent);
#endif /* __LINUX_JFFS_INTREP_H__ */
/*
* JFFS -- Journaling Flash File System, Linux implementation.
*
* Copyright (C) 1999, 2000 Axis Communications AB.
*
* Created by Finn Hakansson <finn@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: jffs_fm.c,v 1.27 2001/09/20 12:29:47 dwmw2 Exp $
*
* Ported to Linux 2.3.x and MTD:
* Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
*
*/
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/blkdev.h>
#include <linux/jffs.h>
#include "jffs_fm.h"
#include "intrep.h"
#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
#endif
static struct jffs_fm *jffs_alloc_fm(void);
static void jffs_free_fm(struct jffs_fm *n);
extern struct kmem_cache *fm_cache;
extern struct kmem_cache *node_cache;
#if CONFIG_JFFS_FS_VERBOSE > 0
void
jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
{
D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
D(printk("{\n"));
D(printk(" %u, /* flash_size */\n", fmc->flash_size));
D(printk(" %u, /* used_size */\n", fmc->used_size));
D(printk(" %u, /* dirty_size */\n", fmc->dirty_size));
D(printk(" %u, /* free_size */\n", fmc->free_size));
D(printk(" %u, /* sector_size */\n", fmc->sector_size));
D(printk(" %u, /* min_free_size */\n", fmc->min_free_size));
D(printk(" %u, /* max_chunk_size */\n", fmc->max_chunk_size));
D(printk(" 0x%p, /* mtd */\n", fmc->mtd));
D(printk(" 0x%p, /* head */ "
"(head->offset = 0x%08x)\n",
fmc->head, (fmc->head ? fmc->head->offset : 0)));
D(printk(" 0x%p, /* tail */ "
"(tail->offset + tail->size = 0x%08x)\n",
fmc->tail,
(fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
D(printk(" 0x%p, /* head_extra */\n", fmc->head_extra));
D(printk(" 0x%p, /* tail_extra */\n", fmc->tail_extra));
D(printk("}\n"));
}
#endif /* CONFIG_JFFS_FS_VERBOSE > 0 */
#if CONFIG_JFFS_FS_VERBOSE > 2
static void
jffs_print_fm(struct jffs_fm *fm)
{
D(printk("struct jffs_fm: 0x%p\n", fm));
D(printk("{\n"));
D(printk(" 0x%08x, /* offset */\n", fm->offset));
D(printk(" %u, /* size */\n", fm->size));
D(printk(" 0x%p, /* prev */\n", fm->prev));
D(printk(" 0x%p, /* next */\n", fm->next));
D(printk(" 0x%p, /* nodes */\n", fm->nodes));
D(printk("}\n"));
}
#endif /* CONFIG_JFFS_FS_VERBOSE > 2 */
#if 0
void
jffs_print_node_ref(struct jffs_node_ref *ref)
{
D(printk("struct jffs_node_ref: 0x%p\n", ref));
D(printk("{\n"));
D(printk(" 0x%p, /* node */\n", ref->node));
D(printk(" 0x%p, /* next */\n", ref->next));
D(printk("}\n"));
}
#endif /* 0 */
/* This function creates a new shiny flash memory control structure. */
struct jffs_fmcontrol *
jffs_build_begin(struct jffs_control *c, int unit)
{
struct jffs_fmcontrol *fmc;
struct mtd_info *mtd;
D3(printk("jffs_build_begin()\n"));
fmc = kmalloc(sizeof(*fmc), GFP_KERNEL);
if (!fmc) {
D(printk("jffs_build_begin(): Allocation of "
"struct jffs_fmcontrol failed!\n"));
return (struct jffs_fmcontrol *)0;
}
DJM(no_jffs_fmcontrol++);
mtd = get_mtd_device(NULL, unit);
if (IS_ERR(mtd)) {
kfree(fmc);
DJM(no_jffs_fmcontrol--);
return NULL;
}
/* Retrieve the size of the flash memory. */
fmc->flash_size = mtd->size;
D3(printk(" fmc->flash_size = %d bytes\n", fmc->flash_size));
fmc->used_size = 0;
fmc->dirty_size = 0;
fmc->free_size = mtd->size;
fmc->sector_size = mtd->erasesize;
fmc->max_chunk_size = fmc->sector_size >> 1;
/* min_free_size:
1 sector, obviously.
+ 1 x max_chunk_size, for when a nodes overlaps the end of a sector
+ 1 x max_chunk_size again, which ought to be enough to handle
the case where a rename causes a name to grow, and GC has
to write out larger nodes than the ones it's obsoleting.
We should fix it so it doesn't have to write the name
_every_ time. Later.
+ another 2 sectors because people keep getting GC stuck and
we don't know why. This scares me - I want formal proof
of correctness of whatever number we put here. dwmw2.
*/
fmc->min_free_size = fmc->sector_size << 2;
fmc->mtd = mtd;
fmc->c = c;
fmc->head = NULL;
fmc->tail = NULL;
fmc->head_extra = NULL;
fmc->tail_extra = NULL;
mutex_init(&fmc->biglock);
return fmc;
}
/* When the flash memory scan has completed, this function should be called
before use of the control structure. */
void
jffs_build_end(struct jffs_fmcontrol *fmc)
{
D3(printk("jffs_build_end()\n"));
if (!fmc->head) {
fmc->head = fmc->head_extra;
fmc->tail = fmc->tail_extra;
}
else if (fmc->head_extra) {
fmc->tail_extra->next = fmc->head;
fmc->head->prev = fmc->tail_extra;
fmc->head = fmc->head_extra;
}
fmc->head_extra = NULL; /* These two instructions should be omitted. */
fmc->tail_extra = NULL;
D3(jffs_print_fmcontrol(fmc));
}
/* Call this function when the file system is unmounted. This function
frees all memory used by this module. */
void
jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
{
if (fmc) {
struct jffs_fm *next = fmc->head;
while (next) {
struct jffs_fm *cur = next;
next = next->next;
jffs_free_fm(cur);
}
put_mtd_device(fmc->mtd);
kfree(fmc);
DJM(no_jffs_fmcontrol--);
}
}
/* This function returns the size of the first chunk of free space on the
flash memory. This function will return something nonzero if the flash
memory contains any free space. */
__u32
jffs_free_size1(struct jffs_fmcontrol *fmc)
{
__u32 head;
__u32 tail;
__u32 end = fmc->flash_size;
if (!fmc->head) {
/* There is nothing on the flash. */
return fmc->flash_size;
}
/* Compute the beginning and ending of the contents of the flash. */
head = fmc->head->offset;
tail = fmc->tail->offset + fmc->tail->size;
if (tail == end) {
tail = 0;
}
ASSERT(else if (tail > end) {
printk(KERN_WARNING "jffs_free_size1(): tail > end\n");
tail = 0;
});
if (head <= tail) {
return end - tail;
}
else {
return head - tail;
}
}
/* This function will return something nonzero in case there are two free
areas on the flash. Like this:
+----------------+------------------+----------------+
| FREE 1 | USED / DIRTY | FREE 2 |
+----------------+------------------+----------------+
fmc->head -----^
fmc->tail ------------------------^
The value returned, will be the size of the first empty area on the
flash, in this case marked "FREE 1". */
__u32
jffs_free_size2(struct jffs_fmcontrol *fmc)
{
if (fmc->head) {
__u32 head = fmc->head->offset;
__u32 tail = fmc->tail->offset + fmc->tail->size;
if (tail == fmc->flash_size) {
tail = 0;
}
if (tail >= head) {
return head;
}
}
return 0;
}
/* Allocate a chunk of flash memory. If there is enough space on the
device, a reference to the associated node is stored in the jffs_fm
struct. */
int
jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
struct jffs_fm **result)
{
struct jffs_fm *fm;
__u32 free_chunk_size1;
__u32 free_chunk_size2;
D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
"node = 0x%p\n", fmc, size, node));
*result = NULL;
if (!(fm = jffs_alloc_fm())) {
D(printk("jffs_fmalloc(): kmalloc() failed! (fm)\n"));
return -ENOMEM;
}
free_chunk_size1 = jffs_free_size1(fmc);
free_chunk_size2 = jffs_free_size2(fmc);
if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
printk(KERN_WARNING "Free size accounting screwed\n");
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);
}
D3(printk("jffs_fmalloc(): free_chunk_size1 = %u, "
"free_chunk_size2 = %u\n",
free_chunk_size1, free_chunk_size2));
if (size <= free_chunk_size1) {
if (!(fm->nodes = (struct jffs_node_ref *)
kmalloc(sizeof(struct jffs_node_ref),
GFP_KERNEL))) {
D(printk("jffs_fmalloc(): kmalloc() failed! "
"(node_ref)\n"));
jffs_free_fm(fm);
return -ENOMEM;
}
DJM(no_jffs_node_ref++);
fm->nodes->node = node;
fm->nodes->next = NULL;
if (fmc->tail) {
fm->offset = fmc->tail->offset + fmc->tail->size;
if (fm->offset == fmc->flash_size) {
fm->offset = 0;
}
ASSERT(else if (fm->offset > fmc->flash_size) {
printk(KERN_WARNING "jffs_fmalloc(): "
"offset > flash_end\n");
fm->offset = 0;
});
}
else {
/* There don't have to be files in the file
system yet. */
fm->offset = 0;
}
fm->size = size;
fmc->free_size -= size;
fmc->used_size += size;
}
else if (size > free_chunk_size2) {
printk(KERN_WARNING "JFFS: Tried to allocate a too "
"large flash memory chunk. (size = %u)\n", size);
jffs_free_fm(fm);
return -ENOSPC;
}
else {
fm->offset = fmc->tail->offset + fmc->tail->size;
fm->size = free_chunk_size1;
fm->nodes = NULL;
fmc->free_size -= fm->size;
fmc->dirty_size += fm->size; /* Changed by simonk. This seemingly fixes a
bug that caused infinite garbage collection.
It previously set fmc->dirty_size to size (which is the
size of the requested chunk).
*/
}
fm->next = NULL;
if (!fmc->head) {
fm->prev = NULL;
fmc->head = fm;
fmc->tail = fm;
}
else {
fm->prev = fmc->tail;
fmc->tail->next = fm;
fmc->tail = fm;
}
D3(jffs_print_fmcontrol(fmc));
D3(jffs_print_fm(fm));
*result = fm;
return 0;
}
/* The on-flash space is not needed anymore by the passed node. Remove
the reference to the node from the node list. If the data chunk in
the flash memory isn't used by any more nodes anymore (fm->nodes == 0),
then mark that chunk as dirty. */
int
jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, struct jffs_node *node)
{
struct jffs_node_ref *ref;
struct jffs_node_ref *prev;
ASSERT(int del = 0);
D2(printk("jffs_fmfree(): node->ino = %u, node->version = %u\n",
node->ino, node->version));
ASSERT(if (!fmc || !fm || !fm->nodes) {
printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
"fm->nodes: 0x%p\n",
fmc, fm, (fm ? fm->nodes : NULL));
return -1;
});
/* Find the reference to the node that is going to be removed
and remove it. */
for (ref = fm->nodes, prev = NULL; ref; ref = ref->next) {
if (ref->node == node) {
if (prev) {
prev->next = ref->next;
}
else {
fm->nodes = ref->next;
}
kfree(ref);
DJM(no_jffs_node_ref--);
ASSERT(del = 1);
break;
}
prev = ref;
}
/* If the data chunk in the flash memory isn't used anymore
just mark it as obsolete. */
if (!fm->nodes) {
/* No node uses this chunk so let's remove it. */
fmc->used_size -= fm->size;
fmc->dirty_size += fm->size;
#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
if (jffs_mark_obsolete(fmc, fm->offset) < 0) {
D1(printk("jffs_fmfree(): Failed to mark an on-flash "
"node obsolete!\n"));
return -1;
}
#endif
}
ASSERT(if (!del) {
printk(KERN_WARNING "***jffs_fmfree(): "
"Didn't delete any node reference!\n");
});
return 0;
}
/* This allocation function is used during the initialization of
the file system. */
struct jffs_fm *
jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
struct jffs_node *node)
{
struct jffs_fm *fm;
D3(printk("jffs_fmalloced()\n"));
if (!(fm = jffs_alloc_fm())) {
D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
fmc, offset, size, node));
return NULL;
}
fm->offset = offset;
fm->size = size;
fm->prev = NULL;
fm->next = NULL;
fm->nodes = NULL;
if (node) {
/* `node' exists and it should be associated with the
jffs_fm structure `fm'. */
if (!(fm->nodes = (struct jffs_node_ref *)
kmalloc(sizeof(struct jffs_node_ref),
GFP_KERNEL))) {
D(printk("jffs_fmalloced(): !fm->nodes\n"));
jffs_free_fm(fm);
return NULL;
}
DJM(no_jffs_node_ref++);
fm->nodes->node = node;
fm->nodes->next = NULL;
fmc->used_size += size;
fmc->free_size -= size;
}
else {
/* If there is no node, then this is just a chunk of dirt. */
fmc->dirty_size += size;
fmc->free_size -= size;
}
if (fmc->head_extra) {
fm->prev = fmc->tail_extra;
fmc->tail_extra->next = fm;
fmc->tail_extra = fm;
}
else if (!fmc->head) {
fmc->head = fm;
fmc->tail = fm;
}
else if (fmc->tail->offset + fmc->tail->size < offset) {
fmc->head_extra = fm;
fmc->tail_extra = fm;
}
else {
fm->prev = fmc->tail;
fmc->tail->next = fm;
fmc->tail = fm;
}
D3(jffs_print_fmcontrol(fmc));
D3(jffs_print_fm(fm));
return fm;
}
/* Add a new node to an already existing jffs_fm struct. */
int
jffs_add_node(struct jffs_node *node)
{
struct jffs_node_ref *ref;
D3(printk("jffs_add_node(): ino = %u\n", node->ino));
ref = kmalloc(sizeof(*ref), GFP_KERNEL);
if (!ref)
return -ENOMEM;
DJM(no_jffs_node_ref++);
ref->node = node;
ref->next = node->fm->nodes;
node->fm->nodes = ref;
return 0;
}
/* Free a part of some allocated space. */
void
jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
{
D1(printk("***jffs_fmfree_partly(): fm = 0x%p, fm->nodes = 0x%p, "
"fm->nodes->node->ino = %u, size = %u\n",
fm, (fm ? fm->nodes : 0),
(!fm ? 0 : (!fm->nodes ? 0 : fm->nodes->node->ino)), size));
if (fm->nodes) {
kfree(fm->nodes);
DJM(no_jffs_node_ref--);
fm->nodes = NULL;
}
fmc->used_size -= fm->size;
if (fm == fmc->tail) {
fm->size -= size;
fmc->free_size += size;
}
fmc->dirty_size += fm->size;
}
/* Find the jffs_fm struct that contains the end of the data chunk that
begins at the logical beginning of the flash memory and spans `size'
bytes. If we want to erase a sector of the flash memory, we use this
function to find where the sector limit cuts a chunk of data. */
struct jffs_fm *
jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
{
struct jffs_fm *fm;
__u32 pos = 0;
if (size == 0) {
return NULL;
}
ASSERT(if (!fmc) {
printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
return NULL;
});
fm = fmc->head;
while (fm) {
pos += fm->size;
if (pos < size) {
fm = fm->next;
}
else if (pos > size) {
break;
}
else {
fm = NULL;
break;
}
}
return fm;
}
/* Move the head of the fmc structures and delete the obsolete parts. */
void
jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
{
struct jffs_fm *fm;
struct jffs_fm *del;
ASSERT(if (!fmc) {
printk(KERN_ERR "jffs_sync_erase(): fmc == NULL\n");
return;
});
fmc->dirty_size -= erased_size;
fmc->free_size += erased_size;
for (fm = fmc->head; fm && (erased_size > 0);) {
if (erased_size >= fm->size) {
erased_size -= fm->size;
del = fm;
fm = fm->next;
fm->prev = NULL;
fmc->head = fm;
jffs_free_fm(del);
}
else {
fm->size -= erased_size;
fm->offset += erased_size;
break;
}
}
}
/* Return the oldest used node in the flash memory. */
struct jffs_node *
jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
{
struct jffs_fm *fm;
struct jffs_node_ref *nref;
struct jffs_node *node = NULL;
ASSERT(if (!fmc) {
printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
return NULL;
});
for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);
if (!fm) {
return NULL;
}
/* The oldest node is the last one in the reference list. This list
shouldn't be too long; just one or perhaps two elements. */
for (nref = fm->nodes; nref; nref = nref->next) {
node = nref->node;
}
D2(printk("jffs_get_oldest_node(): ino = %u, version = %u\n",
(node ? node->ino : 0), (node ? node->version : 0)));
return node;
}
#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
/* Mark an on-flash node as obsolete.
Note that this is just an optimization that isn't necessary for the
filesystem to work. */
static int
jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
{
/* The `accurate_pos' holds the position of the accurate byte
in the jffs_raw_inode structure that we are going to mark
as obsolete. */
__u32 accurate_pos = fm_offset + JFFS_RAW_INODE_ACCURATE_OFFSET;
unsigned char zero = 0x00;
size_t len;
D3(printk("jffs_mark_obsolete(): accurate_pos = %u\n", accurate_pos));
ASSERT(if (!fmc) {
printk(KERN_ERR "jffs_mark_obsolete(): fmc == NULL\n");
return -1;
});
/* Write 0x00 to the raw inode's accurate member. Don't care
about the return value. */
MTD_WRITE(fmc->mtd, accurate_pos, 1, &len, &zero);
return 0;
}
#endif /* JFFS_MARK_OBSOLETE */
/* check if it's possible to erase the wanted range, and if not, return
* the range that IS erasable, or a negative error code.
*/
static long
jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
{
u_long ssize;
/* assume that sector size for a partition is constant even
* if it spans more than one chip (you usually put the same
* type of chips in a system)
*/
ssize = mtd->erasesize;
if (offset % ssize) {
printk(KERN_WARNING "jffs_flash_erasable_size() given non-aligned offset %x (erasesize %lx)\n", offset, ssize);
/* The offset is not sector size aligned. */
return -1;
}
else if (offset > mtd->size) {
printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size);
return -2;
}
else if (offset + size > mtd->size) {
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);
return -3;
}
return (size / ssize) * ssize;
}
/* How much dirty flash memory is possible to erase at the moment? */
long
jffs_erasable_size(struct jffs_fmcontrol *fmc)
{
struct jffs_fm *fm;
__u32 size = 0;
long ret;
ASSERT(if (!fmc) {
printk(KERN_ERR "jffs_erasable_size(): fmc = NULL\n");
return -1;
});
if (!fmc->head) {
/* The flash memory is totally empty. No nodes. No dirt.
Just return. */
return 0;
}
/* Calculate how much space that is dirty. */
for (fm = fmc->head; fm && !fm->nodes; fm = fm->next) {
if (size && fm->offset == 0) {
/* We have reached the beginning of the flash. */
break;
}
size += fm->size;
}
/* Someone's signature contained this:
There's a fine line between fishing and just standing on
the shore like an idiot... */
ret = jffs_flash_erasable_size(fmc->mtd, fmc->head->offset, size);
ASSERT(if (ret < 0) {
printk("jffs_erasable_size: flash_erasable_size() "
"returned something less than zero (%ld).\n", ret);
printk("jffs_erasable_size: offset = 0x%08x\n",
fmc->head->offset);
});
/* If there is dirt on the flash (which is the reason to why
this function was called in the first place) but no space is
possible to erase right now, the initial part of the list of
jffs_fm structs, that hold place for dirty space, could perhaps
be shortened. The list's initial "dirty" elements are merged
into just one large dirty jffs_fm struct. This operation must
only be performed if nothing is possible to erase. Otherwise,
jffs_clear_end_of_node() won't work as expected. */
if (ret == 0) {
struct jffs_fm *head = fmc->head;
struct jffs_fm *del;
/* While there are two dirty nodes beside each other.*/
while (head->nodes == 0
&& head->next
&& head->next->nodes == 0) {
del = head->next;
head->size += del->size;
head->next = del->next;
if (del->next) {
del->next->prev = head;
}
jffs_free_fm(del);
}
}
return (ret >= 0 ? ret : 0);
}
static struct jffs_fm *jffs_alloc_fm(void)
{
struct jffs_fm *fm;
fm = kmem_cache_alloc(fm_cache,GFP_KERNEL);
DJM(if (fm) no_jffs_fm++;);
return fm;
}
static void jffs_free_fm(struct jffs_fm *n)
{
kmem_cache_free(fm_cache,n);
DJM(no_jffs_fm--);
}
struct jffs_node *jffs_alloc_node(void)
{
struct jffs_node *n;
n = (struct jffs_node *)kmem_cache_alloc(node_cache,GFP_KERNEL);
if(n != NULL)
no_jffs_node++;
return n;
}
void jffs_free_node(struct jffs_node *n)
{
kmem_cache_free(node_cache,n);
no_jffs_node--;
}
int jffs_get_node_inuse(void)
{
return no_jffs_node;
}
/*
* JFFS -- Journaling Flash File System, Linux implementation.
*
* Copyright (C) 1999, 2000 Axis Communications AB.
*
* Created by Finn Hakansson <finn@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: jffs_fm.h,v 1.13 2001/01/11 12:03:25 dwmw2 Exp $
*
* Ported to Linux 2.3.x and MTD:
* Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
*
*/
#ifndef __LINUX_JFFS_FM_H__
#define __LINUX_JFFS_FM_H__
#include <linux/types.h>
#include <linux/jffs.h>
#include <linux/mtd/mtd.h>
#include <linux/mutex.h>
/* The alignment between two nodes in the flash memory. */
#define JFFS_ALIGN_SIZE 4
/* Mark the on-flash space as obsolete when appropriate. */
#define JFFS_MARK_OBSOLETE 0
#ifndef CONFIG_JFFS_FS_VERBOSE
#define CONFIG_JFFS_FS_VERBOSE 1
#endif
#if CONFIG_JFFS_FS_VERBOSE > 0
#define D(x) x
#define D1(x) D(x)
#else
#define D(x)
#define D1(x)
#endif
#if CONFIG_JFFS_FS_VERBOSE > 1
#define D2(x) D(x)
#else
#define D2(x)
#endif
#if CONFIG_JFFS_FS_VERBOSE > 2
#define D3(x) D(x)
#else
#define D3(x)
#endif
#define ASSERT(x) x
/* How many padding bytes should be inserted between two chunks of data
on the flash? */
#define JFFS_GET_PAD_BYTES(size) ( (JFFS_ALIGN_SIZE-1) & -(__u32)(size) )
#define JFFS_PAD(size) ( (size + (JFFS_ALIGN_SIZE-1)) & ~(JFFS_ALIGN_SIZE-1) )
struct jffs_node_ref
{
struct jffs_node *node;
struct jffs_node_ref *next;
};
/* The struct jffs_fm represents a chunk of data in the flash memory. */
struct jffs_fm
{
__u32 offset;
__u32 size;
struct jffs_fm *prev;
struct jffs_fm *next;
struct jffs_node_ref *nodes; /* USED if != 0. */
};
struct jffs_fmcontrol
{
__u32 flash_size;
__u32 used_size;
__u32 dirty_size;
__u32 free_size;
__u32 sector_size;
__u32 min_free_size; /* The minimum free space needed to be able
to perform garbage collections. */
__u32 max_chunk_size; /* The maximum size of a chunk of data. */
struct mtd_info *mtd;
struct jffs_control *c;
struct jffs_fm *head;
struct jffs_fm *tail;
struct jffs_fm *head_extra;
struct jffs_fm *tail_extra;
struct mutex biglock;
};
/* Notice the two members head_extra and tail_extra in the jffs_control
structure above. Those are only used during the scanning of the flash
memory; while the file system is being built. If the data in the flash
memory is organized like
+----------------+------------------+----------------+
| USED / DIRTY | FREE | USED / DIRTY |
+----------------+------------------+----------------+
then the scan is split in two parts. The first scanned part of the
flash memory is organized through the members head and tail. The
second scanned part is organized with head_extra and tail_extra. When
the scan is completed, the two lists are merged together. The jffs_fm
struct that head_extra references is the logical beginning of the
flash memory so it will be referenced by the head member. */
struct jffs_fmcontrol *jffs_build_begin(struct jffs_control *c, int unit);
void jffs_build_end(struct jffs_fmcontrol *fmc);
void jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc);
int jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size,
struct jffs_node *node, struct jffs_fm **result);
int jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm,
struct jffs_node *node);
__u32 jffs_free_size1(struct jffs_fmcontrol *fmc);
__u32 jffs_free_size2(struct jffs_fmcontrol *fmc);
void jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size);
struct jffs_fm *jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size);
struct jffs_node *jffs_get_oldest_node(struct jffs_fmcontrol *fmc);
long jffs_erasable_size(struct jffs_fmcontrol *fmc);
struct jffs_fm *jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset,
__u32 size, struct jffs_node *node);
int jffs_add_node(struct jffs_node *node);
void jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm,
__u32 size);
#if CONFIG_JFFS_FS_VERBOSE > 0
void jffs_print_fmcontrol(struct jffs_fmcontrol *fmc);
#endif
#if 0
void jffs_print_node_ref(struct jffs_node_ref *ref);
#endif /* 0 */
#endif /* __LINUX_JFFS_FM_H__ */
/*
* JFFS -- Journaling Flash File System, Linux implementation.
*
* Copyright (C) 2000 Axis Communications AB.
*
* Created by Simon Kagstrom <simonk@axis.com>.
*
* $Id: jffs_proc.c,v 1.5 2001/06/02 14:34:55 dwmw2 Exp $
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Overview:
* This file defines JFFS partition entries in the proc file system.
*
* TODO:
* Create some more proc files for different kinds of info, i.e. statistics
* about written and read bytes, number of calls to different routines,
* reports about failures.
*/
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/jffs.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#include <linux/types.h>
#include "jffs_fm.h"
#include "jffs_proc.h"
/*
* Structure for a JFFS partition in the system
*/
struct jffs_partition_dir {
struct jffs_control *c;
struct proc_dir_entry *part_root;
struct proc_dir_entry *part_info;
struct proc_dir_entry *part_layout;
struct jffs_partition_dir *next;
};
/*
* Structure for top-level entry in '/proc/fs' directory
*/
struct proc_dir_entry *jffs_proc_root;
/*
* Linked list of 'jffs_partition_dirs' to help us track
* the mounted JFFS partitions in the system
*/
static struct jffs_partition_dir *jffs_part_dirs;
/*
* Read functions for entries
*/
static int jffs_proc_info_read(char *page, char **start, off_t off,
int count, int *eof, void *data);
static int jffs_proc_layout_read (char *page, char **start, off_t off,
int count, int *eof, void *data);
/*
* Register a JFFS partition directory (called upon mount)
*/
int jffs_register_jffs_proc_dir(int mtd, struct jffs_control *c)
{
struct jffs_partition_dir *part_dir;
struct proc_dir_entry *part_info = NULL;
struct proc_dir_entry *part_layout = NULL;
struct proc_dir_entry *part_root = NULL;
char name[10];
sprintf(name, "%d", mtd);
/* Allocate structure for local JFFS partition table */
part_dir = (struct jffs_partition_dir *)
kmalloc(sizeof (struct jffs_partition_dir), GFP_KERNEL);
if (!part_dir)
goto out;
/* Create entry for this partition */
part_root = proc_mkdir(name, jffs_proc_root);
if (!part_root)
goto out1;
/* Create entry for 'info' file */
part_info = create_proc_entry ("info", 0, part_root);
if (!part_info)
goto out2;
part_info->read_proc = jffs_proc_info_read;
part_info->data = (void *) c;
/* Create entry for 'layout' file */
part_layout = create_proc_entry ("layout", 0, part_root);
if (!part_layout)
goto out3;
part_layout->read_proc = jffs_proc_layout_read;
part_layout->data = (void *) c;
/* Fill in structure for table and insert in the list */
part_dir->c = c;
part_dir->part_root = part_root;
part_dir->part_info = part_info;
part_dir->part_layout = part_layout;
part_dir->next = jffs_part_dirs;
jffs_part_dirs = part_dir;
/* Return happy */
return 0;
out3:
remove_proc_entry("info", part_root);
out2:
remove_proc_entry(name, jffs_proc_root);
out1:
kfree(part_dir);
out:
return -ENOMEM;
}
/*
* Unregister a JFFS partition directory (called at umount)
*/
int jffs_unregister_jffs_proc_dir(struct jffs_control *c)
{
struct jffs_partition_dir *part_dir = jffs_part_dirs;
struct jffs_partition_dir *prev_part_dir = NULL;
while (part_dir) {
if (part_dir->c == c) {
/* Remove entries for partition */
remove_proc_entry (part_dir->part_info->name,
part_dir->part_root);
remove_proc_entry (part_dir->part_layout->name,
part_dir->part_root);
remove_proc_entry (part_dir->part_root->name,
jffs_proc_root);
/* Remove entry from list */
if (prev_part_dir)
prev_part_dir->next = part_dir->next;
else
jffs_part_dirs = part_dir->next;
/*
* Check to see if this is the last one
* and remove the entry from '/proc/fs'
* if it is.
*/
if (jffs_part_dirs == part_dir->next)
remove_proc_entry ("jffs", proc_root_fs);
/* Free memory for entry */
kfree(part_dir);
/* Return happy */
return 0;
}
/* Move to next entry */
prev_part_dir = part_dir;
part_dir = part_dir->next;
}
/* Return unhappy */
return -1;
}
/*
* Read a JFFS partition's `info' file
*/
static int jffs_proc_info_read (char *page, char **start, off_t off,
int count, int *eof, void *data)
{
struct jffs_control *c = (struct jffs_control *) data;
int len = 0;
/* Get information on the parition */
len += sprintf (page,
"partition size: %08lX (%u)\n"
"sector size: %08lX (%u)\n"
"used size: %08lX (%u)\n"
"dirty size: %08lX (%u)\n"
"free size: %08lX (%u)\n\n",
(unsigned long) c->fmc->flash_size, c->fmc->flash_size,
(unsigned long) c->fmc->sector_size, c->fmc->sector_size,
(unsigned long) c->fmc->used_size, c->fmc->used_size,
(unsigned long) c->fmc->dirty_size, c->fmc->dirty_size,
(unsigned long) (c->fmc->flash_size -
(c->fmc->used_size + c->fmc->dirty_size)),
c->fmc->flash_size - (c->fmc->used_size + c->fmc->dirty_size));
/* We're done */
*eof = 1;
/* Return length */
return len;
}
/*
* Read a JFFS partition's `layout' file
*/
static int jffs_proc_layout_read (char *page, char **start, off_t off,
int count, int *eof, void *data)
{
struct jffs_control *c = (struct jffs_control *) data;
struct jffs_fm *fm = NULL;
struct jffs_fm *last_fm = NULL;
int len = 0;
/* Get the first item in the list */
fm = c->fmc->head;
/* Print free space */
if (fm && fm->offset) {
len += sprintf (page, "00000000 %08lX free\n",
(unsigned long) fm->offset);
}
/* Loop through all of the flash control structures */
while (fm && (len < (off + count))) {
if (fm->nodes) {
len += sprintf (page + len,
"%08lX %08lX ino=%08lX, ver=%08lX\n",
(unsigned long) fm->offset,
(unsigned long) fm->size,
(unsigned long) fm->nodes->node->ino,
(unsigned long) fm->nodes->node->version);
}
else {
len += sprintf (page + len,
"%08lX %08lX dirty\n",
(unsigned long) fm->offset,
(unsigned long) fm->size);
}
last_fm = fm;
fm = fm->next;
}
/* Print free space */
if ((len < (off + count)) && last_fm
&& (last_fm->offset < c->fmc->flash_size)) {
len += sprintf (page + len,
"%08lX %08lX free\n",
(unsigned long) last_fm->offset +
last_fm->size,
(unsigned long) (c->fmc->flash_size -
(last_fm->offset + last_fm->size)));
}
/* We're done */
*eof = 1;
/* Return length */
return len;
}
/*
* JFFS -- Journaling Flash File System, Linux implementation.
*
* Copyright (C) 2000 Axis Communications AB.
*
* Created by Simon Kagstrom <simonk@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: jffs_proc.h,v 1.2 2000/11/15 22:04:12 sjhill Exp $
*/
/* jffs_proc.h defines a structure for inclusion in the proc-file system. */
#ifndef __LINUX_JFFS_PROC_H__
#define __LINUX_JFFS_PROC_H__
#include <linux/proc_fs.h>
/* The proc_dir_entry for jffs (defined in jffs_proc.c). */
extern struct proc_dir_entry *jffs_proc_root;
int jffs_register_jffs_proc_dir(int mtd, struct jffs_control *c);
int jffs_unregister_jffs_proc_dir(struct jffs_control *c);
#endif /* __LINUX_JFFS_PROC_H__ */
/*
* JFFS -- Journalling Flash File System, Linux implementation.
*
* Copyright (C) 1999, 2000 Axis Communications AB.
*
* Created by Finn Hakansson <finn@axis.com>.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Id: jffs.h,v 1.20 2001/09/18 21:33:37 dwmw2 Exp $
*
* Ported to Linux 2.3.x and MTD:
* Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
*
*/
#ifndef __LINUX_JFFS_H__
#define __LINUX_JFFS_H__
#include <linux/types.h>
#include <linux/completion.h>
#define JFFS_VERSION_STRING "1.0"
/* This is a magic number that is used as an identification number for
this file system. It is written to the super_block structure. */
#define JFFS_MAGIC_SB_BITMASK 0x07c0 /* 1984 */
/* This is a magic number that every on-flash raw inode begins with. */
#define JFFS_MAGIC_BITMASK 0x34383931 /* "1984" */
/* These two bitmasks are the valid ones for the flash memories we have
for the moment. */
#define JFFS_EMPTY_BITMASK 0xffffffff
#define JFFS_DIRTY_BITMASK 0x00000000
/* This is the inode number of the root node. */
#define JFFS_MIN_INO 1
/* How many slots in the file hash table should we have? */
#define JFFS_HASH_SIZE 40
/* Don't use more than 254 bytes as the maximum allowed length of a file's
name due to errors that could occur during the scanning of the flash
memory. In fact, a name length of 255 or 0xff, could be the result of
an uncompleted write. For instance, if a raw inode is written to the
flash memory and there is a power lossage just before the length of
the name is written, the length 255 would be interpreted as an illegal
value. */
#define JFFS_MAX_NAME_LEN 254
/* Commands for ioctl(). */
#define JFFS_IOCTL_MAGIC 't'
#define JFFS_PRINT_HASH _IO(JFFS_IOCTL_MAGIC, 90)
#define JFFS_PRINT_TREE _IO(JFFS_IOCTL_MAGIC, 91)
#define JFFS_GET_STATUS _IO(JFFS_IOCTL_MAGIC, 92)
/* XXX: This is something that we should try to get rid of in the future. */
#define JFFS_MODIFY_INODE 0x01
#define JFFS_MODIFY_NAME 0x02
#define JFFS_MODIFY_DATA 0x04
#define JFFS_MODIFY_EXIST 0x08
struct jffs_control;
/* The JFFS raw inode structure: Used for storage on physical media. */
/* Perhaps the uid, gid, atime, mtime and ctime members should have
more space due to future changes in the Linux kernel. Anyhow, since
a user of this filesystem probably have to fix a large number of
other things, we have decided to not be forward compatible. */
struct jffs_raw_inode
{
__u32 magic; /* A constant magic number. */
__u32 ino; /* Inode number. */
__u32 pino; /* Parent's inode number. */
__u32 version; /* Version number. */
__u32 mode; /* The file's type or mode. */
__u16 uid; /* The file's owner. */
__u16 gid; /* The file's group. */
__u32 atime; /* Last access time. */
__u32 mtime; /* Last modification time. */
__u32 ctime; /* Creation time. */
__u32 offset; /* Where to begin to write. */
__u32 dsize; /* Size of the node's data. */
__u32 rsize; /* How much are going to be replaced? */
__u8 nsize; /* Name length. */
__u8 nlink; /* Number of links. */
__u8 spare : 6; /* For future use. */
__u8 rename : 1; /* Rename to a name of an already existing file? */
__u8 deleted : 1; /* Has this file been deleted? */
__u8 accurate; /* The inode is obsolete if accurate == 0. */
__u32 dchksum; /* Checksum for the data. */
__u16 nchksum; /* Checksum for the name. */
__u16 chksum; /* Checksum for the raw inode. */
};
/* Define the offset of the accurate byte in struct jffs_raw_inode. */
#define JFFS_RAW_INODE_ACCURATE_OFFSET (sizeof(struct jffs_raw_inode) \
- 2 * sizeof(__u32) - sizeof(__u8))
/* Define the offset of the chksum member in struct jffs_raw_inode. */
#define JFFS_RAW_INODE_CHKSUM_OFFSET (sizeof(struct jffs_raw_inode) \
- sizeof(__u16))
/* Define the offset of the dchksum member in struct jffs_raw_inode. */
#define JFFS_RAW_INODE_DCHKSUM_OFFSET (sizeof(struct jffs_raw_inode) \
- sizeof(__u16) - sizeof(__u16) \
- sizeof(__u32))
/* The RAM representation of the node. The names of pointers to
jffs_nodes are very often just called `n' in the source code. */
struct jffs_node
{
__u32 ino; /* Inode number. */
__u32 version; /* Version number. */
__u32 data_offset; /* Logic location of the data to insert. */
__u32 data_size; /* The amount of data this node inserts. */
__u32 removed_size; /* The amount of data that this node removes. */
__u32 fm_offset; /* Physical location of the data in the actual
flash memory data chunk. */
__u8 name_size; /* Size of the name. */
struct jffs_fm *fm; /* Physical memory information. */
struct jffs_node *version_prev;
struct jffs_node *version_next;
struct jffs_node *range_prev;
struct jffs_node *range_next;
};
/* The RAM representation of a file (plain files, directories,
links, etc.). Pointers to jffs_files are normally named `f'
in the JFFS source code. */
struct jffs_file
{
__u32 ino; /* Inode number. */
__u32 pino; /* Parent's inode number. */
__u32 mode; /* file_type, mode */
__u16 uid; /* owner */
__u16 gid; /* group */
__u32 atime; /* Last access time. */
__u32 mtime; /* Last modification time. */
__u32 ctime; /* Creation time. */
__u8 nsize; /* Name length. */
__u8 nlink; /* Number of links. */
__u8 deleted; /* Has this file been deleted? */
char *name; /* The name of this file; NULL-terminated. */
__u32 size; /* The total size of the file's data. */
__u32 highest_version; /* The highest version number of this file. */
struct jffs_control *c;
struct jffs_file *parent; /* Reference to the parent directory. */
struct jffs_file *children; /* Always NULL for plain files. */
struct jffs_file *sibling_prev; /* Siblings in the same directory. */
struct jffs_file *sibling_next;
struct list_head hash; /* hash list. */
struct jffs_node *range_head; /* The final data. */
struct jffs_node *range_tail; /* The first data. */
struct jffs_node *version_head; /* The youngest node. */
struct jffs_node *version_tail; /* The oldest node. */
};
/* This is just a definition of a simple list used for keeping track of
files deleted due to a rename. This list is only used during the
mounting of the file system and only if there have been rename operations
earlier. */
struct jffs_delete_list
{
__u32 ino;
struct jffs_delete_list *next;
};
/* A struct for the overall file system control. Pointers to
jffs_control structs are named `c' in the source code. */
struct jffs_control
{
struct super_block *sb; /* Reference to the VFS super block. */
struct jffs_file *root; /* The root directory file. */
struct list_head *hash; /* Hash table for finding files by ino. */
struct jffs_fmcontrol *fmc; /* Flash memory control structure. */
__u32 hash_len; /* The size of the hash table. */
__u32 next_ino; /* Next inode number to use for new files. */
__u16 building_fs; /* Is the file system being built right now? */
struct jffs_delete_list *delete_list; /* Track deleted files. */
pid_t thread_pid; /* GC thread's PID */
struct task_struct *gc_task; /* GC task struct */
struct completion gc_thread_comp; /* GC thread exit mutex */
__u32 gc_minfree_threshold; /* GC trigger thresholds */
__u32 gc_maxdirty_threshold;
};
/* Used to inform about flash status. */
struct jffs_flash_status
{
__u32 size;
__u32 used;
__u32 dirty;
__u32 begin;
__u32 end;
};
/* This stuff could be used for finding memory leaks. */
#define JFFS_MEMORY_DEBUG 0
extern long no_jffs_node;
#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
extern long no_jffs_control;
extern long no_jffs_raw_inode;
extern long no_jffs_node_ref;
extern long no_jffs_fm;
extern long no_jffs_fmcontrol;
extern long no_hash;
extern long no_name;
#define DJM(x) x
#else
#define DJM(x)
#endif
#endif /* __LINUX_JFFS_H__ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment