inode-v23.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,848 行 · 第 1/4 页

C
1,848
字号
/* * 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 <asm/semaphore.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"#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 address_space_operations jffs_address_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 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 voidjffs_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 intjffs_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"));	down(&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"));		up(&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"));		up(&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_inode_pages(inode->i_mapping);		}		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.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"));		up(&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"));	up(&c->fmc->biglock);out:	unlock_kernel();	return res;} /* 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;	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_blksize = PAGE_SIZE;	inode->i_blocks = (inode->i_size + 511) >> 9;	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 kstatfs *buf){	struct jffs_control *c = (struct jffs_control *) 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.  */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));	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 = (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;	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?