⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inode1.c

📁 elinux jffs初始版本 具体了解JFFS的文件系统!
💻 C
📖 第 1 页 / 共 4 页
字号:
		c->fmc->no_call_gc = 0;
	}

	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_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;
		inode->i_dirt = 1;
		iput(inode);
	}

jffs_rename_end:
	iput(old_dir);
	iput(new_dir);
	c->rename_lock = 0;
	wake_up(&c->rename_wait);
	return result;
} /* jffs_rename()  */


/* Read the contents of a directory.  Used by programs like `ls'
   for instance.  */
static int
jffs_readdir(struct inode *dir, struct file *filp, void *dirent, filldir_t filldir)
{
	struct jffs_file *f;
	int j;
	int ddino;

	D2(printk("jffs_readdir(): dir: 0x%p, filp: 0x%p\n", dir, filp));

	if (!dir || !S_ISDIR(dir->i_mode)) {
		D(printk("jffs_readdir(): 'dir' is NULL or not a dir!\n"));
		return -EBADF;
	}

	switch (filp->f_pos)
	{
	case 0:
		D3(printk("jffs_readdir(): \".\" %lu\n", dir->i_ino));
		if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino) < 0) {
			return 0;
		}
		filp->f_pos = 1;
	case 1:
		if (dir->i_ino == JFFS_MIN_INO) {
			ddino = dir->i_sb->s_covered->i_ino;
		}
		else {
			ddino = ((struct jffs_file *)dir->u.generic_ip)->pino;
		}
		D3(printk("jffs_readdir(): \"..\" %u\n", ddino));
		if (filldir(dirent, "..", 2, filp->f_pos, ddino) < 0)
			return 0;
		filp->f_pos++;
	default:
		f = ((struct jffs_file *)dir->u.generic_ip)->children;
		for (j = 2; (j < filp->f_pos) && f; j++) {
			f = f->sibling_next;
		}
		for (; f ; f = f->sibling_next) {
			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) < 0)
				return 0;
			filp->f_pos++;
		}
	}
	return filp->f_pos;
} /* jffs_readdir()  */


/* Find a file in a directory. If the file exists, return its
   corresponding inode in the argument `result'.  */
static int
jffs_lookup(struct inode *dir, const char *name, int len,
            struct inode **result)
{
	struct jffs_file *d;
	struct jffs_file *f;
	int r = 0;

	D3({
		char *s = (char *)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);
	});

	*result = (struct inode *)0;
	if (!dir) {
		return -ENOENT;
	}
	if (!S_ISDIR(dir->i_mode)) {
		r = -ENOTDIR;
		goto jffs_lookup_end;
	}
	if (len > JFFS_MAX_NAME_LEN) {
		r = -ENAMETOOLONG;
		goto jffs_lookup_end;
	}

	if (!(d = (struct jffs_file *)dir->u.generic_ip)) {
		D(printk("jffs_lookup(): No such inode! (%lu)\n", dir->i_ino));
		r = -ENOENT;
		goto jffs_lookup_end;
	}

	/* Get the corresponding inode to the file.  */
	if ((len == 1) && (name[0] == '.')) {
		if (!(*result = iget(dir->i_sb, d->ino))) {
			D(printk("jffs_lookup(): . iget() ==> NULL\n"));
			r = -ENOENT;
		}
	}
	else if ((len == 2) && (name[0] == '.') && (name[1] == '.')) {
		if (!(*result = iget(dir->i_sb, d->pino))) {
			D(printk("jffs_lookup(): .. iget() ==> NULL\n"));
			r = -ENOENT;
		}
	}
	else if ((f = jffs_find_child(d, name, len))) {
		if (!(*result = iget(dir->i_sb, f->ino))) {
			D(printk("jffs_lookup(): iget() ==> NULL\n"));
			r = -ENOENT;
		}
	}
	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));
		r = -ENOENT;
	}

jffs_lookup_end:
	iput(dir);
	return r;
} /* jffs_lookup()  */


/* Try to read a page of data from a file.  */
static int
jffs_readpage(struct inode *inode, struct page *page)
{
	unsigned long buf;
	unsigned long read_len;
	int result = -EIO;
	struct jffs_file *f = (struct jffs_file *)inode->u.generic_ip;
	int r;

	D2(printk("***jffs_readpage(): file = \"%s\", page->offset = %lu\n",
		  (f->name ? f->name : ""), page->offset));

	page->count++;
	set_bit(PG_locked, &page->flags);
	buf = page_address(page);
	clear_bit(PG_uptodate, &page->flags);
	clear_bit(PG_error, &page->flags);

	if (page->offset < inode->i_size) {
		read_len = jffs_min(inode->i_size - page->offset, PAGE_SIZE);
		r = jffs_read_data(f, (char *)buf, page->offset, read_len);
		if (r == read_len) {
			if (read_len < PAGE_SIZE) {
				memset((void *)(buf + read_len), 0,
				       PAGE_SIZE - read_len);
			}
			set_bit(PG_uptodate, &page->flags);
			result = 0;
		}
		D(else {
			printk("***jffs_readpage(): Read error! "
			       "Wanted to read %lu bytes but only "
			       "read %d bytes.\n", read_len, r);
		});
	}
	if (result) {
		set_bit(PG_error, &page->flags);
		memset((void *)buf, 0, PAGE_SIZE);
	}

	clear_bit(PG_locked, &page->flags);
	wake_up(&page->wait);
	free_page(buf);

	D3(printk("jffs_readpage(): Leaving...\n"));

	return result;
} /* jffs_readpage()  */


/* Create a new directory.  */
static int
jffs_mkdir(struct inode *dir, const char *name, int len, int mode)
{
	struct jffs_raw_inode raw_inode;
	struct jffs_control *c;
	struct jffs_node *node;
	struct jffs_file *dir_f;
	int dir_mode;
	int result = 0;

	D1({
		char *_name = (char *) kmalloc(len + 1, GFP_KERNEL);
		memcpy(_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);
	});

	if (!dir) {
		return -ENOENT;
	}
	if (len > JFFS_MAX_NAME_LEN) {
		result = -ENAMETOOLONG;
		goto jffs_mkdir_end;
	}

	dir_f = (struct jffs_file *)dir->u.generic_ip;
	ASSERT(if (!dir_f) {
		printk(KERN_ERR "jffs_mkdir(): No reference to a "
		       "jffs_file struct in inode.\n");
		result = -1;
		goto jffs_mkdir_end;
	});

	c = dir_f->c;

	if (!JFFS_ENOUGH_SPACE(c->fmc)) {
		D1(printk("jffs_mkdir(): Free size = %u\n",
			  jffs_free_size1(c->fmc) + jffs_free_size2(c->fmc)));
		D(printk(KERN_NOTICE "JFFS: No space left on device\n"));
		result = -ENOSPC;
		goto jffs_mkdir_end;
	}

	/* If there already exists a file or directory with the same name,
	   then this operation should fail. I originally thought that VFS
	   should take care of this issue.  */
	if (jffs_find_child(dir_f, name, len)) {
		D(printk("jffs_mkdir(): There already exists a file or "
			 "directory with the same name!\n"));
		result = -EEXIST;
		goto jffs_mkdir_end;
	}

	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 = (struct jffs_node *) kmalloc(sizeof(struct jffs_node),
						  GFP_KERNEL))) {
		D(printk("jffs_mkdir(): Allocation failed: node == 0\n"));
		result = -ENOMEM;
		goto jffs_mkdir_end;
	}
	DJM(no_jffs_node++);
	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 = current->fsgid;
	raw_inode.atime = CURRENT_TIME;
	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 = 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 (jffs_write_node(c, node, &raw_inode, name, 0) < 0) {
		D(printk("jffs_mkdir(): jffs_write_node() failed.\n"));
		kfree(node);
		DJM(no_jffs_node--);
		result = -1;
		goto jffs_mkdir_end;
	}

	/* Insert the new node into the file system.  */
	result = jffs_insert_node(c, 0, &raw_inode, name, node);

jffs_mkdir_end:
	iput(dir);
	return result;
} /* jffs_mkdir()  */


/* Remove a directory.  */
static int
jffs_rmdir(struct inode *dir, const char *name, int len)
{
	D3(printk("***jffs_rmdir()\n"));
	return jffs_remove(dir, name, len, S_IFDIR, 1);
}


/* Remove any kind of file except for directories.  */
static int
jffs_unlink(struct inode *dir, const char *name, int len)
{
	D3(printk("***jffs_unlink()\n"));
	return jffs_remove(dir, name, len, 0, 1);
}


/* 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, const char *name, int len, int type,
	    int must_iput)
{
	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 = 0;
	int result = 0;

	D1({
		char *_name = (char *) kmalloc(len + 1, GFP_KERNEL);
		memcpy(_name, name, len);
		_name[len] = '\0';
		printk("***jffs_remove(): file = \"%s\"\n", _name);
		kfree(_name);
	});

	if (!dir) {
		return -ENOENT;
	}
	if (len > JFFS_MAX_NAME_LEN) {
		result = -ENAMETOOLONG;
		goto jffs_remove_end;
	}

	dir_f = (struct jffs_file *) dir->u.generic_ip;
	c = dir_f->c;

	if (!(del_f = jffs_find_child(dir_f, name, len))) {
		D(printk("jffs_remove(): jffs_find_child() failed.\n"));
		result = -ENOENT;
		goto jffs_remove_end;
	}

	if (S_ISDIR(type)) {
		if (!S_ISDIR(del_f->mode)) {
			result = -ENOTDIR;
			goto jffs_remove_end;
		}
		if (del_f->children) {
			result = -ENOTEMPTY;
			goto jffs_remove_end;
		}
        }
	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;
	}

	if (!(inode = iget(dir->i_sb, del_f->ino))) {
		printk(KERN_ERR "JFFS: Unlink failed.\n");
		result = -ENOENT;
		goto jffs_remove_end;
	}

	/* Create a node for the deletion.  */
	if (!(del_node = (struct jffs_node *)
			 kmalloc(sizeof(struct jffs_node), GFP_KERNEL))) {
		D(printk("jffs_remove(): Allocation failed!\n"));
		result = -ENOMEM;
		goto jffs_remove_end;
	}
	DJM(no_jffs_node++);
	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 = CURRENT_TIME;
	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, 0, 0) < 0) {

⌨️ 快捷键说明

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