📄 inode-v22.c
字号:
/* * 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-v22.c,v 1.67 2001/09/23 23:28:36 dwmw2 Exp $ * * Ported to Linux 2.2.x by Sebastien Cote: * Copyright (C) 2000 Matrox Electronic Systems * * Copyright 2000, 2001 Red Hat, Inc. *//* inode.c -- Contains the code that is called from the VFS. *//* Argh. Some architectures have kernel_thread in asm/processor.h Some have it in unistd.h and you need to define __KERNEL_SYSCALLS__ Pass me a baseball bat and the person responsible. dwmw2*/#define __KERNEL_SYSCALLS__#include <linux/sched.h>#include <linux/unistd.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/locks.h>#include <linux/smp_lock.h>#include <linux/sched.h>#include <linux/ioctl.h>#include <linux/stat.h>#include <linux/blkdev.h>#include <linux/quotaops.h>#include <asm/semaphore.h>#include <asm/byteorder.h>#include <asm/uaccess.h>#if CONFIG_JFFS_PROC_FS#include <linux/proc_fs.h>#endif#include "jffs_fm.h"#include "intrep.h"#if CONFIG_JFFS_PROC_FS#include "jffs_proc.h"#endifstatic int jffs_remove(struct inode *dir, struct dentry *dentry, int type);static struct super_operations jffs_ops;static struct file_operations jffs_file_operations;static struct inode_operations jffs_file_inode_operations;static struct file_operations jffs_dir_operations;static struct inode_operations jffs_dir_inode_operations;static struct inode_operations jffs_symlink_inode_operations;kmem_cache_t *node_cache = NULL;kmem_cache_t *fm_cache = NULL;/* Called by the VFS at mount time to initialize the whole file system. */static struct super_block *jffs_read_super(struct super_block *sb, void *data, int silent){ kdev_t dev = sb->s_dev; struct inode *root_inode; struct jffs_control *c; D1(printk(KERN_NOTICE "JFFS: Trying to mount device %s.\n", kdevname(dev))); if (MAJOR(dev)!=MTD_BLOCK_MAJOR) { printk(KERN_WARNING "JFFS: Trying to mount non-mtd device.\n"); return 0; } MOD_INC_USE_COUNT; lock_super(sb); sb->s_blocksize = PAGE_CACHE_SIZE; sb->s_blocksize_bits = PAGE_CACHE_SHIFT; sb->u.generic_sbp = (void *) 0; /* 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; unlock_super(sb); 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, NULL))) { goto jffs_sb_err3; } c = (struct jffs_control *) sb->u.generic_sbp; #ifdef CONFIG_JFFS_PROC_FS /* Set up the jffs proc file system. */ if (jffs_register_jffs_proc_dir(dev, c) < 0) { printk(KERN_WARNING "JFFS: Failed to initialize the JFFS " "proc file system for device %s.\n", kdevname(dev)); }#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_FS | CLONE_FILES); D1(printk(KERN_NOTICE "JFFS: GC thread pid=%d.\n", (int) c->thread_pid)); D1(printk(KERN_NOTICE "JFFS: Successfully mounted device %s.\n", kdevname(dev))); return sb; jffs_sb_err3: iput(root_inode); jffs_sb_err2: jffs_cleanup_control((struct jffs_control *)sb->u.generic_sbp); jffs_sb_err1: unlock_super(sb); MOD_DEC_USE_COUNT; printk(KERN_WARNING "JFFS: Failed to mount device %s.\n", kdevname(dev)); return 0;}/* This function is called when the file system is umounted. */static voidjffs_put_super(struct super_block *sb){ struct jffs_control *c = (struct jffs_control *) sb->u.generic_sbp; D1(kdev_t dev = sb->s_dev); 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); } down (&c->gc_thread_sem); D1(printk (KERN_NOTICE "jffs_put_super(): Successfully waited on thread.\n")); sb->s_dev = 0; jffs_cleanup_control((struct jffs_control *)sb->u.generic_sbp); MOD_DEC_USE_COUNT; D1(printk(KERN_NOTICE "JFFS: Successfully unmounted device %s.\n", kdevname(dev)));}/* 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 intjffs_notify_change(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; int recoverable = 0; c = (struct jffs_control *)inode->i_sb->u.generic_sbp; fmc = c->fmc; if ((res = inode_change_ok(inode, iattr))) return res; D3(printk (KERN_NOTICE "notify_change(): down biglock\n")); down(&fmc->biglock); f = jffs_find_file(c, inode->i_ino); ASSERT(if (!f) { printk("jffs_notify_change(): Invalid inode number: %lu\n", inode->i_ino); D3(printk (KERN_NOTICE "notify_change(): up biglock\n")); up(&fmc->biglock); return -EINVAL; }); D1(printk("***jffs_notify_change(): 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_notify_change(): Allocation failed!\n")); D3(printk (KERN_NOTICE "notify_change(): up biglock\n")); up(&fmc->biglock); return -ENOMEM; } 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.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.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_inode_pages(inode); } inode->i_ctime = CURRENT_TIME; inode->i_mtime = inode->i_ctime; } if (update_all || iattr->ia_valid & ATTR_ATIME) { raw_inode.atime = iattr->ia_atime; inode->i_atime = iattr->ia_atime; } if (update_all || iattr->ia_valid & ATTR_MTIME) { raw_inode.mtime = iattr->ia_mtime; inode->i_mtime = iattr->ia_mtime; } if (update_all || iattr->ia_valid & ATTR_CTIME) { raw_inode.ctime = iattr->ia_ctime; inode->i_ctime = iattr->ia_ctime; } /* Write this node to the flash. */ if ((res = jffs_write_node(c, new_node, &raw_inode, f->name, 0, 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")); up(&c->fmc->biglock); return res; } jffs_insert_node(c, f, &raw_inode, 0, new_node); mark_inode_dirty(inode); D3(printk (KERN_NOTICE "n_c(): up biglock\n")); up(&c->fmc->biglock); return 0;} /* jffs_notify_change() */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; inode = get_empty_inode(); if (!inode) { *err = -ENOMEM; return NULL; } sb = dir->i_sb; c = (struct jffs_control *)sb->u.generic_sbp; inode->i_sb = sb; inode->i_dev = sb->s_dev; 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_rdev = 0; inode->i_size = raw_inode->dsize; inode->i_atime = raw_inode->atime; inode->i_mtime = raw_inode->mtime; inode->i_ctime = raw_inode->ctime; inode->i_blksize = PAGE_SIZE; inode->i_blocks = (inode->i_size + 511) >> 9; inode->i_version = 0; inode->i_flags = sb->s_flags; f = jffs_find_file(c, raw_inode->ino); inode->u.generic_ip = (void *)f; insert_inode_hash(inode); return inode;}/* Get statistics of the file system. */intjffs_statfs(struct super_block *sb, struct statfs *buf, int bufsiz){ struct statfs tmp; struct jffs_control *c = (struct jffs_control *) sb->u.generic_sbp; struct jffs_fmcontrol *fmc = c->fmc; D2(printk("jffs_statfs()\n")); tmp.f_type = JFFS_MAGIC_SB_BITMASK; tmp.f_bsize = PAGE_CACHE_SIZE; tmp.f_blocks = (fmc->flash_size / PAGE_CACHE_SIZE) - (fmc->min_free_size / PAGE_CACHE_SIZE); tmp.f_bfree = (jffs_free_size1(fmc) + jffs_free_size2(fmc) + fmc->dirty_size - fmc->min_free_size) >> PAGE_CACHE_SHIFT; tmp.f_bavail = tmp.f_bfree; /* Find out how many files there are in the filesystem. */ tmp.f_files = jffs_foreach_file(c, jffs_file_count); tmp.f_ffree = tmp.f_bfree; /* tmp.f_fsid = 0; */ tmp.f_namelen = JFFS_MAX_NAME_LEN; return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;}/* Rename a file. */intjffs_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)); if (!new_dir->i_nlink) { D(printk("jffs_rename(): new_dir->i_nlink is zero\n")); return -ENOENT; } c = (struct jffs_control *)old_dir->i_sb->u.generic_sbp; ASSERT(if (!c) { printk(KERN_ERR "jffs_rename(): The old_dir inode " "didn't have a reference to a jffs_file struct\n"); return -EIO; }); result = -ENOTDIR; if (!(old_dir_f = (struct jffs_file *)old_dir->u.generic_ip)) { 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 = (struct jffs_file *)new_dir->u.generic_ip)) { D(printk("jffs_rename(): New dir invalid.\n")); goto jffs_rename_end; } D3(printk (KERN_NOTICE "rename(): down biglock\n")); down(&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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -