2 * JFFS -- Journalling Flash File System, Linux implementation.
4 * Copyright (C) 1999, 2000 Axis Communications AB.
6 * Created by Finn Hakansson <finn@axis.com>.
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * $Id: inode-v23.c,v 1.70 2001/10/02 09:16:02 dwmw2 Exp $
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
18 * Copyright 2000, 2001 Red Hat, Inc.
21 /* inode.c -- Contains the code that is called from the VFS. */
24 * uid and gid are just 16 bit.
25 * jffs_file_write reads from user-space pointers without xx_from_user
26 * maybe other stuff do to.
29 /* Argh. Some architectures have kernel_thread in asm/processor.h
30 Some have it in unistd.h and you need to define __KERNEL_SYSCALLS__
31 Pass me a baseball bat and the person responsible.
34 #define __KERNEL_SYSCALLS__
35 #include <linux/sched.h>
36 #include <linux/unistd.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/errno.h>
42 #include <linux/slab.h>
43 #include <linux/jffs.h>
45 #include <linux/locks.h>
46 #include <linux/smp_lock.h>
47 #include <linux/ioctl.h>
48 #include <linux/stat.h>
49 #include <linux/blkdev.h>
50 #include <linux/quotaops.h>
51 #include <asm/semaphore.h>
52 #include <asm/byteorder.h>
53 #include <asm/uaccess.h>
57 #if CONFIG_JFFS_PROC_FS
58 #include "jffs_proc.h"
61 static int jffs_remove(struct inode *dir, struct dentry *dentry, int type);
63 static struct super_operations jffs_ops;
64 static struct file_operations jffs_file_operations;
65 static struct inode_operations jffs_file_inode_operations;
66 static struct file_operations jffs_dir_operations;
67 static struct inode_operations jffs_dir_inode_operations;
68 static struct address_space_operations jffs_address_operations;
70 kmem_cache_t *node_cache = NULL;
71 kmem_cache_t *fm_cache = NULL;
73 /* Called by the VFS at mount time to initialize the whole file system. */
74 static struct super_block *
75 jffs_read_super(struct super_block *sb, void *data, int silent)
77 kdev_t dev = sb->s_dev;
78 struct inode *root_inode;
79 struct jffs_control *c;
81 D1(printk(KERN_NOTICE "JFFS: Trying to mount device %s.\n",
84 if (MAJOR(dev) != MTD_BLOCK_MAJOR) {
85 printk(KERN_WARNING "JFFS: Trying to mount a "
90 sb->s_blocksize = PAGE_CACHE_SIZE;
91 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
92 sb->u.generic_sbp = (void *) 0;
93 sb->s_maxbytes = 0xFFFFFFFF;
95 /* Build the file system. */
96 if (jffs_build_fs(sb) < 0) {
101 * set up enough so that we can read an inode
103 sb->s_magic = JFFS_MAGIC_SB_BITMASK;
104 sb->s_op = &jffs_ops;
106 root_inode = iget(sb, JFFS_MIN_INO);
110 /* Get the root directory of this file system. */
111 if (!(sb->s_root = d_alloc_root(root_inode))) {
115 c = (struct jffs_control *) sb->u.generic_sbp;
117 #ifdef CONFIG_JFFS_PROC_FS
118 /* Set up the jffs proc file system. */
119 if (jffs_register_jffs_proc_dir(dev, c) < 0) {
120 printk(KERN_WARNING "JFFS: Failed to initialize the JFFS "
121 "proc file system for device %s.\n",
126 /* Set the Garbage Collection thresholds */
128 /* GC if free space goes below 5% of the total size */
129 c->gc_minfree_threshold = c->fmc->flash_size / 20;
131 if (c->gc_minfree_threshold < c->fmc->sector_size)
132 c->gc_minfree_threshold = c->fmc->sector_size;
134 /* GC if dirty space exceeds 33% of the total size. */
135 c->gc_maxdirty_threshold = c->fmc->flash_size / 3;
137 if (c->gc_maxdirty_threshold < c->fmc->sector_size)
138 c->gc_maxdirty_threshold = c->fmc->sector_size;
141 c->thread_pid = kernel_thread (jffs_garbage_collect_thread,
143 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
144 D1(printk(KERN_NOTICE "JFFS: GC thread pid=%d.\n", (int) c->thread_pid));
146 D1(printk(KERN_NOTICE "JFFS: Successfully mounted device %s.\n",
153 jffs_cleanup_control((struct jffs_control *)sb->u.generic_sbp);
155 printk(KERN_WARNING "JFFS: Failed to mount device %s.\n",
161 /* This function is called when the file system is umounted. */
163 jffs_put_super(struct super_block *sb)
165 struct jffs_control *c = (struct jffs_control *) sb->u.generic_sbp;
166 D1(kdev_t dev = sb->s_dev);
168 D2(printk("jffs_put_super()\n"));
170 #ifdef CONFIG_JFFS_PROC_FS
171 jffs_unregister_jffs_proc_dir(c);
175 D1(printk (KERN_NOTICE "jffs_put_super(): Telling gc thread to die.\n"));
176 send_sig(SIGKILL, c->gc_task, 1);
178 wait_for_completion(&c->gc_thread_comp);
180 D1(printk (KERN_NOTICE "jffs_put_super(): Successfully waited on thread.\n"));
182 jffs_cleanup_control((struct jffs_control *)sb->u.generic_sbp);
183 D1(printk(KERN_NOTICE "JFFS: Successfully unmounted device %s.\n",
188 /* This function is called when user commands like chmod, chgrp and
189 chown are executed. System calls like trunc() results in a call
192 jffs_setattr(struct dentry *dentry, struct iattr *iattr)
194 struct inode *inode = dentry->d_inode;
195 struct jffs_raw_inode raw_inode;
196 struct jffs_control *c;
197 struct jffs_fmcontrol *fmc;
199 struct jffs_node *new_node;
204 if ((res = inode_change_ok(inode, iattr)))
207 c = (struct jffs_control *)inode->i_sb->u.generic_sbp;
210 D3(printk (KERN_NOTICE "notify_change(): down biglock\n"));
213 f = jffs_find_file(c, inode->i_ino);
216 printk("jffs_setattr(): Invalid inode number: %lu\n",
218 D3(printk (KERN_NOTICE "notify_change(): up biglock\n"));
223 D1(printk("***jffs_setattr(): file: \"%s\", ino: %u\n",
226 update_all = iattr->ia_valid & ATTR_FORCE;
228 if ( (update_all || iattr->ia_valid & ATTR_SIZE)
229 && (iattr->ia_size + 128 < f->size) ) {
230 /* We're shrinking the file by more than 128 bytes.
231 We'll be able to GC and recover this space, so
232 allow it to go into the reserved space. */
236 if (!(new_node = jffs_alloc_node())) {
237 D(printk("jffs_setattr(): Allocation failed!\n"));
238 D3(printk (KERN_NOTICE "notify_change(): up biglock\n"));
243 new_node->data_offset = 0;
244 new_node->removed_size = 0;
245 raw_inode.magic = JFFS_MAGIC_BITMASK;
246 raw_inode.ino = f->ino;
247 raw_inode.pino = f->pino;
248 raw_inode.mode = f->mode;
249 raw_inode.uid = f->uid;
250 raw_inode.gid = f->gid;
251 raw_inode.atime = f->atime;
252 raw_inode.mtime = f->mtime;
253 raw_inode.ctime = f->ctime;
255 raw_inode.offset = 0;
258 raw_inode.nsize = f->nsize;
259 raw_inode.nlink = f->nlink;
261 raw_inode.rename = 0;
262 raw_inode.deleted = 0;
264 if (update_all || iattr->ia_valid & ATTR_MODE) {
265 raw_inode.mode = iattr->ia_mode;
266 inode->i_mode = iattr->ia_mode;
268 if (update_all || iattr->ia_valid & ATTR_UID) {
269 raw_inode.uid = iattr->ia_uid;
270 inode->i_uid = iattr->ia_uid;
272 if (update_all || iattr->ia_valid & ATTR_GID) {
273 raw_inode.gid = iattr->ia_gid;
274 inode->i_gid = iattr->ia_gid;
276 if (update_all || iattr->ia_valid & ATTR_SIZE) {
278 D1(printk("jffs_notify_change(): Changing size "
279 "to %lu bytes!\n", (long)iattr->ia_size));
280 raw_inode.offset = iattr->ia_size;
282 /* Calculate how many bytes need to be removed from
284 if (f->size < iattr->ia_size) {
288 len = f->size - iattr->ia_size;
291 raw_inode.rsize = len;
293 /* The updated node will be a removal node, with
294 base at the new size and size of the nbr of bytes
296 new_node->data_offset = iattr->ia_size;
297 new_node->removed_size = len;
298 inode->i_size = iattr->ia_size;
299 inode->i_blocks = (inode->i_size + 511) >> 9;
302 invalidate_inode_pages(inode);
304 inode->i_ctime = CURRENT_TIME;
305 inode->i_mtime = inode->i_ctime;
307 if (update_all || iattr->ia_valid & ATTR_ATIME) {
308 raw_inode.atime = iattr->ia_atime;
309 inode->i_atime = iattr->ia_atime;
311 if (update_all || iattr->ia_valid & ATTR_MTIME) {
312 raw_inode.mtime = iattr->ia_mtime;
313 inode->i_mtime = iattr->ia_mtime;
315 if (update_all || iattr->ia_valid & ATTR_CTIME) {
316 raw_inode.ctime = iattr->ia_ctime;
317 inode->i_ctime = iattr->ia_ctime;
320 /* Write this node to the flash. */
321 if ((res = jffs_write_node(c, new_node, &raw_inode, f->name, 0, recoverable, f)) < 0) {
322 D(printk("jffs_notify_change(): The write failed!\n"));
323 jffs_free_node(new_node);
324 D3(printk (KERN_NOTICE "n_c(): up biglock\n"));
325 up(&c->fmc->biglock);
329 jffs_insert_node(c, f, &raw_inode, 0, new_node);
331 mark_inode_dirty(inode);
332 D3(printk (KERN_NOTICE "n_c(): up biglock\n"));
333 up(&c->fmc->biglock);
335 } /* jffs_notify_change() */
339 jffs_new_inode(const struct inode * dir, struct jffs_raw_inode *raw_inode,
342 struct super_block * sb;
343 struct inode * inode;
344 struct jffs_control *c;
348 inode = new_inode(sb);
354 c = (struct jffs_control *)sb->u.generic_sbp;
356 inode->i_ino = raw_inode->ino;
357 inode->i_mode = raw_inode->mode;
358 inode->i_nlink = raw_inode->nlink;
359 inode->i_uid = raw_inode->uid;
360 inode->i_gid = raw_inode->gid;
362 inode->i_size = raw_inode->dsize;
363 inode->i_atime = raw_inode->atime;
364 inode->i_mtime = raw_inode->mtime;
365 inode->i_ctime = raw_inode->ctime;
366 inode->i_blksize = PAGE_SIZE;
367 inode->i_blocks = (inode->i_size + 511) >> 9;
368 inode->i_version = 0;
370 f = jffs_find_file(c, raw_inode->ino);
372 inode->u.generic_ip = (void *)f;
373 insert_inode_hash(inode);
378 /* Get statistics of the file system. */
380 jffs_statfs(struct super_block *sb, struct statfs *buf)
382 struct jffs_control *c = (struct jffs_control *) sb->u.generic_sbp;
383 struct jffs_fmcontrol *fmc = c->fmc;
385 D2(printk("jffs_statfs()\n"));
387 buf->f_type = JFFS_MAGIC_SB_BITMASK;
388 buf->f_bsize = PAGE_CACHE_SIZE;
389 buf->f_blocks = (fmc->flash_size / PAGE_CACHE_SIZE)
390 - (fmc->min_free_size / PAGE_CACHE_SIZE);
391 buf->f_bfree = (jffs_free_size1(fmc) + jffs_free_size2(fmc) +
392 fmc->dirty_size - fmc->min_free_size)
394 buf->f_bavail = buf->f_bfree;
396 /* Find out how many files there are in the filesystem. */
397 buf->f_files = jffs_foreach_file(c, jffs_file_count);
398 buf->f_ffree = buf->f_bfree;
399 /* buf->f_fsid = 0; */
400 buf->f_namelen = JFFS_MAX_NAME_LEN;
407 jffs_rename(struct inode *old_dir, struct dentry *old_dentry,
408 struct inode *new_dir, struct dentry *new_dentry)
410 struct jffs_raw_inode raw_inode;
411 struct jffs_control *c;
412 struct jffs_file *old_dir_f;
413 struct jffs_file *new_dir_f;
414 struct jffs_file *del_f;
416 struct jffs_node *node;
419 __u32 rename_data = 0;
421 D2(printk("***jffs_rename()\n"));
423 D(printk("jffs_rename(): old_dir: 0x%p, old name: 0x%p, "
424 "new_dir: 0x%p, new name: 0x%p\n",
425 old_dir, old_dentry->d_name.name,
426 new_dir, new_dentry->d_name.name));
428 c = (struct jffs_control *)old_dir->i_sb->u.generic_sbp;
430 printk(KERN_ERR "jffs_rename(): The old_dir inode "
431 "didn't have a reference to a jffs_file struct\n");
436 if (!(old_dir_f = (struct jffs_file *)old_dir->u.generic_ip)) {
437 D(printk("jffs_rename(): Old dir invalid.\n"));
438 goto jffs_rename_end;
441 /* Try to find the file to move. */
443 if (!(f = jffs_find_child(old_dir_f, old_dentry->d_name.name,
444 old_dentry->d_name.len))) {
445 goto jffs_rename_end;
448 /* Find the new directory. */
450 if (!(new_dir_f = (struct jffs_file *)new_dir->u.generic_ip)) {
451 D(printk("jffs_rename(): New dir invalid.\n"));
452 goto jffs_rename_end;
454 D3(printk (KERN_NOTICE "rename(): down biglock\n"));
455 down(&c->fmc->biglock);
456 /* Create a node and initialize as much as needed. */
458 if (!(node = jffs_alloc_node())) {
459 D(printk("jffs_rename(): Allocation failed: node == 0\n"));
460 goto jffs_rename_end;
462 node->data_offset = 0;
463 node->removed_size = 0;
465 /* Initialize the raw inode. */
466 raw_inode.magic = JFFS_MAGIC_BITMASK;
467 raw_inode.ino = f->ino;
468 raw_inode.pino = new_dir_f->ino;
469 /* raw_inode.version = f->highest_version + 1; */
470 raw_inode.mode = f->mode;
471 raw_inode.uid = current->fsuid;
472 raw_inode.gid = current->fsgid;
474 raw_inode.uid = f->uid;
475 raw_inode.gid = f->gid;
477 raw_inode.atime = CURRENT_TIME;
478 raw_inode.mtime = raw_inode.atime;
479 raw_inode.ctime = f->ctime;
480 raw_inode.offset = 0;
483 raw_inode.nsize = new_dentry->d_name.len;
484 raw_inode.nlink = f->nlink;
486 raw_inode.rename = 0;
487 raw_inode.deleted = 0;
489 /* See if there already exists a file with the same name as
491 if ((del_f = jffs_find_child(new_dir_f, new_dentry->d_name.name,
492 new_dentry->d_name.len))) {
493 raw_inode.rename = 1;
494 raw_inode.dsize = sizeof(__u32);
495 rename_data = del_f->ino;
498 /* Write the new node to the flash memory. */
499 if ((result = jffs_write_node(c, node, &raw_inode,
500 new_dentry->d_name.name,
501 (unsigned char*)&rename_data, 0, f)) < 0) {
502 D(printk("jffs_rename(): Failed to write node to flash.\n"));
503 jffs_free_node(node);
504 goto jffs_rename_end;
508 if (raw_inode.rename) {
509 /* The file with the same name must be deleted. */
510 //FIXME deadlock down(&c->fmc->gclock);
511 if ((result = jffs_remove(new_dir, new_dentry,
513 /* This is really bad. */
514 printk(KERN_ERR "JFFS: An error occurred in "
517 // up(&c->fmc->gclock);
520 if (old_dir_f != new_dir_f) {
521 /* Remove the file from its old position in the
523 jffs_unlink_file_from_tree(f);
526 /* Insert the new node into the file system. */
527 if ((result = jffs_insert_node(c, f, &raw_inode,
528 new_dentry->d_name.name, node)) < 0) {
529 D(printk(KERN_ERR "jffs_rename(): jffs_insert_node() "
533 if (old_dir_f != new_dir_f) {
534 /* Insert the file to its new position in the
536 jffs_insert_file_into_tree(f);
539 /* This is a kind of update of the inode we're about to make
540 here. This is what they do in ext2fs. Kind of. */
541 if ((inode = iget(new_dir->i_sb, f->ino))) {
542 inode->i_ctime = CURRENT_TIME;
543 mark_inode_dirty(inode);
548 D3(printk (KERN_NOTICE "rename(): up biglock\n"));
549 up(&c->fmc->biglock);
551 } /* jffs_rename() */
554 /* Read the contents of a directory. Used by programs like `ls'
557 jffs_readdir(struct file *filp, void *dirent, filldir_t filldir)
560 struct dentry *dentry = filp->f_dentry;
561 struct inode *inode = dentry->d_inode;
562 struct jffs_control *c = (struct jffs_control *)inode->i_sb->u.generic_sbp;
565 D3(printk (KERN_NOTICE "readdir(): down biglock\n"));
566 down(&c->fmc->biglock);
568 D2(printk("jffs_readdir(): inode: 0x%p, filp: 0x%p\n", inode, filp));
569 if (filp->f_pos == 0) {
570 D3(printk("jffs_readdir(): \".\" %lu\n", inode->i_ino));
571 if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0) {
572 D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
573 up(&c->fmc->biglock);
578 if (filp->f_pos == 1) {
579 if (inode->i_ino == JFFS_MIN_INO) {
580 ddino = JFFS_MIN_INO;
583 ddino = ((struct jffs_file *)
584 inode->u.generic_ip)->pino;
586 D3(printk("jffs_readdir(): \"..\" %u\n", ddino));
587 if (filldir(dirent, "..", 2, filp->f_pos, ddino, DT_DIR) < 0) {
588 D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
589 up(&c->fmc->biglock);
594 f = ((struct jffs_file *)inode->u.generic_ip)->children;
597 while(f && (f->deleted || j++ < filp->f_pos )) {
602 D3(printk("jffs_readdir(): \"%s\" ino: %u\n",
603 (f->name ? f->name : ""), f->ino));
604 if (filldir(dirent, f->name, f->nsize,
605 filp->f_pos , f->ino, DT_UNKNOWN) < 0) {
606 D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
607 up(&c->fmc->biglock);
613 } while(f && f->deleted);
615 D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
616 up(&c->fmc->biglock);
618 } /* jffs_readdir() */
621 /* Find a file in a directory. If the file exists, return its
622 corresponding dentry. */
623 static struct dentry *
624 jffs_lookup(struct inode *dir, struct dentry *dentry)
628 struct jffs_control *c = (struct jffs_control *)dir->i_sb->u.generic_sbp;
632 struct inode *inode = NULL;
634 len = dentry->d_name.len;
635 name = dentry->d_name.name;
638 char *s = (char *)kmalloc(len + 1, GFP_KERNEL);
639 memcpy(s, name, len);
641 printk("jffs_lookup(): dir: 0x%p, name: \"%s\"\n", dir, s);
645 D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
646 down(&c->fmc->biglock);
649 if (len > JFFS_MAX_NAME_LEN) {
650 goto jffs_lookup_end;
654 if (!(d = (struct jffs_file *)dir->u.generic_ip)) {
655 D(printk("jffs_lookup(): No such inode! (%lu)\n",
657 goto jffs_lookup_end;
660 /* Get the corresponding inode to the file. */
662 /* iget calls jffs_read_inode, so we need to drop the biglock
663 before calling iget. Unfortunately, the GC has a tendency
664 to sneak in here, because iget sometimes calls schedule ().
667 if ((len == 1) && (name[0] == '.')) {
668 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
669 up(&c->fmc->biglock);
670 if (!(inode = iget(dir->i_sb, d->ino))) {
671 D(printk("jffs_lookup(): . iget() ==> NULL\n"));
672 goto jffs_lookup_end_no_biglock;
674 D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
675 down(&c->fmc->biglock);
676 } else if ((len == 2) && (name[0] == '.') && (name[1] == '.')) {
677 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
678 up(&c->fmc->biglock);
679 if (!(inode = iget(dir->i_sb, d->pino))) {
680 D(printk("jffs_lookup(): .. iget() ==> NULL\n"));
681 goto jffs_lookup_end_no_biglock;
683 D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
684 down(&c->fmc->biglock);
685 } else if ((f = jffs_find_child(d, name, len))) {
686 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
687 up(&c->fmc->biglock);
688 if (!(inode = iget(dir->i_sb, f->ino))) {
689 D(printk("jffs_lookup(): iget() ==> NULL\n"));
690 goto jffs_lookup_end_no_biglock;
692 D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
693 down(&c->fmc->biglock);
695 D3(printk("jffs_lookup(): Couldn't find the file. "
696 "f = 0x%p, name = \"%s\", d = 0x%p, d->ino = %u\n",
697 f, name, d, d->ino));
701 d_add(dentry, inode);
702 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
703 up(&c->fmc->biglock);
707 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
708 up(&c->fmc->biglock);
710 jffs_lookup_end_no_biglock:
712 } /* jffs_lookup() */
715 /* Try to read a page of data from a file. */
717 jffs_do_readpage_nolock(struct file *file, struct page *page)
720 unsigned long read_len;
722 struct inode *inode = (struct inode*)page->mapping->host;
723 struct jffs_file *f = (struct jffs_file *)inode->u.generic_ip;
724 struct jffs_control *c = (struct jffs_control *)inode->i_sb->u.generic_sbp;
728 D2(printk("***jffs_readpage(): file = \"%s\", page->index = %lu\n",
729 (f->name ? f->name : ""), (long)page->index));
732 /* Don't LockPage(page), should be locked already */
733 buf = page_address(page);
734 ClearPageUptodate(page);
735 ClearPageError(page);
737 D3(printk (KERN_NOTICE "readpage(): down biglock\n"));
738 down(&c->fmc->biglock);
743 offset = page->index << PAGE_CACHE_SHIFT;
744 if (offset < inode->i_size) {
745 read_len = min_t(long, inode->i_size - offset, PAGE_SIZE);
746 r = jffs_read_data(f, buf, offset, read_len);
750 printk("***jffs_readpage(): Read error! "
751 "Wanted to read %lu bytes but only "
752 "read %d bytes.\n", read_len, r);
758 /* This handles the case of partial or no read in above */
759 if(read_len < PAGE_SIZE)
760 memset(buf + read_len, 0, PAGE_SIZE - read_len);
762 D3(printk (KERN_NOTICE "readpage(): up biglock\n"));
763 up(&c->fmc->biglock);
768 SetPageUptodate(page);
770 flush_dcache_page(page);
774 D3(printk("jffs_readpage(): Leaving...\n"));
777 } /* jffs_do_readpage_nolock() */
779 static int jffs_readpage(struct file *file, struct page *page)
781 int ret = jffs_do_readpage_nolock(file, page);
786 /* Create a new directory. */
788 jffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
790 struct jffs_raw_inode raw_inode;
791 struct jffs_control *c;
792 struct jffs_node *node;
793 struct jffs_file *dir_f;
800 int len = dentry->d_name.len;
801 char *_name = (char *) kmalloc(len + 1, GFP_KERNEL);
802 memcpy(_name, dentry->d_name.name, len);
804 printk("***jffs_mkdir(): dir = 0x%p, name = \"%s\", "
805 "len = %d, mode = 0x%08x\n", dir, _name, len, mode);
809 dir_f = (struct jffs_file *)dir->u.generic_ip;
812 printk(KERN_ERR "jffs_mkdir(): No reference to a "
813 "jffs_file struct in inode.\n");
818 D3(printk (KERN_NOTICE "mkdir(): down biglock\n"));
819 down(&c->fmc->biglock);
821 dir_mode = S_IFDIR | (mode & (S_IRWXUGO|S_ISVTX)
822 & ~current->fs->umask);
823 if (dir->i_mode & S_ISGID) {
827 /* Create a node and initialize it as much as needed. */
828 if (!(node = jffs_alloc_node())) {
829 D(printk("jffs_mkdir(): Allocation failed: node == 0\n"));
833 node->data_offset = 0;
834 node->removed_size = 0;
836 /* Initialize the raw inode. */
837 raw_inode.magic = JFFS_MAGIC_BITMASK;
838 raw_inode.ino = c->next_ino++;
839 raw_inode.pino = dir_f->ino;
840 raw_inode.version = 1;
841 raw_inode.mode = dir_mode;
842 raw_inode.uid = current->fsuid;
843 raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
844 /* raw_inode.gid = current->fsgid; */
845 raw_inode.atime = CURRENT_TIME;
846 raw_inode.mtime = raw_inode.atime;
847 raw_inode.ctime = raw_inode.atime;
848 raw_inode.offset = 0;
851 raw_inode.nsize = dentry->d_name.len;
854 raw_inode.rename = 0;
855 raw_inode.deleted = 0;
857 /* Write the new node to the flash. */
858 if ((result = jffs_write_node(c, node, &raw_inode,
859 dentry->d_name.name, 0, 0, NULL)) < 0) {
860 D(printk("jffs_mkdir(): jffs_write_node() failed.\n"));
861 jffs_free_node(node);
865 /* Insert the new node into the file system. */
866 if ((result = jffs_insert_node(c, 0, &raw_inode, dentry->d_name.name,
871 inode = jffs_new_inode(dir, &raw_inode, &err);
877 inode->i_op = &jffs_dir_inode_operations;
878 inode->i_fop = &jffs_dir_operations;
880 mark_inode_dirty(dir);
881 d_instantiate(dentry, inode);
885 D3(printk (KERN_NOTICE "mkdir(): up biglock\n"));
886 up(&c->fmc->biglock);
891 /* Remove a directory. */
893 jffs_rmdir(struct inode *dir, struct dentry *dentry)
895 struct jffs_control *c = (struct jffs_control *)dir->i_sb->u.generic_sbp;
897 D3(printk("***jffs_rmdir()\n"));
898 D3(printk (KERN_NOTICE "rmdir(): down biglock\n"));
899 down(&c->fmc->biglock);
900 ret = jffs_remove(dir, dentry, S_IFDIR);
901 D3(printk (KERN_NOTICE "rmdir(): up biglock\n"));
902 up(&c->fmc->biglock);
907 /* Remove any kind of file except for directories. */
909 jffs_unlink(struct inode *dir, struct dentry *dentry)
911 struct jffs_control *c = (struct jffs_control *)dir->i_sb->u.generic_sbp;
914 D3(printk("***jffs_unlink()\n"));
915 D3(printk (KERN_NOTICE "unlink(): down biglock\n"));
916 down(&c->fmc->biglock);
917 ret = jffs_remove(dir, dentry, 0);
918 D3(printk (KERN_NOTICE "unlink(): up biglock\n"));
919 up(&c->fmc->biglock);
924 /* Remove a JFFS entry, i.e. plain files, directories, etc. Here we
925 shouldn't test for free space on the device. */
927 jffs_remove(struct inode *dir, struct dentry *dentry, int type)
929 struct jffs_raw_inode raw_inode;
930 struct jffs_control *c;
931 struct jffs_file *dir_f; /* The file-to-remove's parent. */
932 struct jffs_file *del_f; /* The file to remove. */
933 struct jffs_node *del_node;
934 struct inode *inode = 0;
938 int len = dentry->d_name.len;
939 const char *name = dentry->d_name.name;
940 char *_name = (char *) kmalloc(len + 1, GFP_KERNEL);
941 memcpy(_name, name, len);
943 printk("***jffs_remove(): file = \"%s\", ino = %ld\n", _name, dentry->d_inode->i_ino);
947 dir_f = (struct jffs_file *) dir->u.generic_ip;
951 if (!(del_f = jffs_find_child(dir_f, dentry->d_name.name,
952 dentry->d_name.len))) {
953 D(printk("jffs_remove(): jffs_find_child() failed.\n"));
954 goto jffs_remove_end;
958 struct jffs_file *child = del_f->children;
960 if( !child->deleted ) {
962 goto jffs_remove_end;
964 child = child->sibling_next;
967 else if (S_ISDIR(del_f->mode)) {
968 D(printk("jffs_remove(): node is a directory "
969 "but it shouldn't be.\n"));
971 goto jffs_remove_end;
974 inode = dentry->d_inode;
977 if (del_f->ino != inode->i_ino)
978 goto jffs_remove_end;
980 if (!inode->i_nlink) {
981 printk("Deleting nonexistent file inode: %lu, nlink: %d\n",
982 inode->i_ino, inode->i_nlink);
986 /* Create a node for the deletion. */
988 if (!(del_node = jffs_alloc_node())) {
989 D(printk("jffs_remove(): Allocation failed!\n"));
990 goto jffs_remove_end;
992 del_node->data_offset = 0;
993 del_node->removed_size = 0;
995 /* Initialize the raw inode. */
996 raw_inode.magic = JFFS_MAGIC_BITMASK;
997 raw_inode.ino = del_f->ino;
998 raw_inode.pino = del_f->pino;
999 /* raw_inode.version = del_f->highest_version + 1; */
1000 raw_inode.mode = del_f->mode;
1001 raw_inode.uid = current->fsuid;
1002 raw_inode.gid = current->fsgid;
1003 raw_inode.atime = CURRENT_TIME;
1004 raw_inode.mtime = del_f->mtime;
1005 raw_inode.ctime = raw_inode.atime;
1006 raw_inode.offset = 0;
1007 raw_inode.dsize = 0;
1008 raw_inode.rsize = 0;
1009 raw_inode.nsize = 0;
1010 raw_inode.nlink = del_f->nlink;
1011 raw_inode.spare = 0;
1012 raw_inode.rename = 0;
1013 raw_inode.deleted = 1;
1015 /* Write the new node to the flash memory. */
1016 if (jffs_write_node(c, del_node, &raw_inode, 0, 0, 1, del_f) < 0) {
1017 jffs_free_node(del_node);
1019 goto jffs_remove_end;
1022 /* Update the file. This operation will make the file disappear
1023 from the in-memory file system structures. */
1024 jffs_insert_node(c, del_f, &raw_inode, 0, del_node);
1026 dir->i_version = ++event;
1027 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1028 mark_inode_dirty(dir);
1030 inode->i_ctime = dir->i_ctime;
1031 mark_inode_dirty(inode);
1033 d_delete(dentry); /* This also frees the inode */
1038 } /* jffs_remove() */
1042 jffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
1044 struct jffs_raw_inode raw_inode;
1045 struct jffs_file *dir_f;
1046 struct jffs_node *node = 0;
1047 struct jffs_control *c;
1048 struct inode *inode;
1050 kdev_t dev = to_kdev_t(rdev);
1053 D1(printk("***jffs_mknod()\n"));
1055 dir_f = (struct jffs_file *)dir->u.generic_ip;
1058 D3(printk (KERN_NOTICE "mknod(): down biglock\n"));
1059 down(&c->fmc->biglock);
1061 /* Create and initialize a new node. */
1062 if (!(node = jffs_alloc_node())) {
1063 D(printk("jffs_mknod(): Allocation failed!\n"));
1065 goto jffs_mknod_err;
1067 node->data_offset = 0;
1068 node->removed_size = 0;
1070 /* Initialize the raw inode. */
1071 raw_inode.magic = JFFS_MAGIC_BITMASK;
1072 raw_inode.ino = c->next_ino++;
1073 raw_inode.pino = dir_f->ino;
1074 raw_inode.version = 1;
1075 raw_inode.mode = mode;
1076 raw_inode.uid = current->fsuid;
1077 raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1078 /* raw_inode.gid = current->fsgid; */
1079 raw_inode.atime = CURRENT_TIME;
1080 raw_inode.mtime = raw_inode.atime;
1081 raw_inode.ctime = raw_inode.atime;
1082 raw_inode.offset = 0;
1083 raw_inode.dsize = sizeof(kdev_t);
1084 raw_inode.rsize = 0;
1085 raw_inode.nsize = dentry->d_name.len;
1086 raw_inode.nlink = 1;
1087 raw_inode.spare = 0;
1088 raw_inode.rename = 0;
1089 raw_inode.deleted = 0;
1091 /* Write the new node to the flash. */
1092 if ((err = jffs_write_node(c, node, &raw_inode, dentry->d_name.name,
1093 (unsigned char *)&dev, 0, NULL)) < 0) {
1094 D(printk("jffs_mknod(): jffs_write_node() failed.\n"));
1096 goto jffs_mknod_err;
1099 /* Insert the new node into the file system. */
1100 if ((err = jffs_insert_node(c, 0, &raw_inode, dentry->d_name.name,
1103 goto jffs_mknod_end;
1106 inode = jffs_new_inode(dir, &raw_inode, &err);
1107 if (inode == NULL) {
1109 goto jffs_mknod_end;
1112 init_special_inode(inode, mode, rdev);
1114 d_instantiate(dentry, inode);
1116 goto jffs_mknod_end;
1120 jffs_free_node(node);
1124 D3(printk (KERN_NOTICE "mknod(): up biglock\n"));
1125 up(&c->fmc->biglock);
1127 } /* jffs_mknod() */
1131 jffs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1133 struct jffs_raw_inode raw_inode;
1134 struct jffs_control *c;
1135 struct jffs_file *dir_f;
1136 struct jffs_node *node;
1137 struct inode *inode;
1139 int symname_len = strlen(symname);
1143 int len = dentry->d_name.len;
1144 char *_name = (char *)kmalloc(len + 1, GFP_KERNEL);
1145 char *_symname = (char *)kmalloc(symname_len + 1, GFP_KERNEL);
1146 memcpy(_name, dentry->d_name.name, len);
1148 memcpy(_symname, symname, symname_len);
1149 _symname[symname_len] = '\0';
1150 printk("***jffs_symlink(): dir = 0x%p, "
1151 "dentry->dname.name = \"%s\", "
1152 "symname = \"%s\"\n", dir, _name, _symname);
1157 dir_f = (struct jffs_file *)dir->u.generic_ip;
1158 ASSERT(if (!dir_f) {
1159 printk(KERN_ERR "jffs_symlink(): No reference to a "
1160 "jffs_file struct in inode.\n");
1166 /* Create a node and initialize it as much as needed. */
1167 if (!(node = jffs_alloc_node())) {
1168 D(printk("jffs_symlink(): Allocation failed: node = NULL\n"));
1171 D3(printk (KERN_NOTICE "symlink(): down biglock\n"));
1172 down(&c->fmc->biglock);
1174 node->data_offset = 0;
1175 node->removed_size = 0;
1177 /* Initialize the raw inode. */
1178 raw_inode.magic = JFFS_MAGIC_BITMASK;
1179 raw_inode.ino = c->next_ino++;
1180 raw_inode.pino = dir_f->ino;
1181 raw_inode.version = 1;
1182 raw_inode.mode = S_IFLNK | S_IRWXUGO;
1183 raw_inode.uid = current->fsuid;
1184 raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1185 raw_inode.atime = CURRENT_TIME;
1186 raw_inode.mtime = raw_inode.atime;
1187 raw_inode.ctime = raw_inode.atime;
1188 raw_inode.offset = 0;
1189 raw_inode.dsize = symname_len;
1190 raw_inode.rsize = 0;
1191 raw_inode.nsize = dentry->d_name.len;
1192 raw_inode.nlink = 1;
1193 raw_inode.spare = 0;
1194 raw_inode.rename = 0;
1195 raw_inode.deleted = 0;
1197 /* Write the new node to the flash. */
1198 if ((err = jffs_write_node(c, node, &raw_inode, dentry->d_name.name,
1199 (const unsigned char *)symname, 0, NULL)) < 0) {
1200 D(printk("jffs_symlink(): jffs_write_node() failed.\n"));
1201 jffs_free_node(node);
1202 goto jffs_symlink_end;
1205 /* Insert the new node into the file system. */
1206 if ((err = jffs_insert_node(c, 0, &raw_inode, dentry->d_name.name,
1208 goto jffs_symlink_end;
1211 inode = jffs_new_inode(dir, &raw_inode, &err);
1212 if (inode == NULL) {
1213 goto jffs_symlink_end;
1216 inode->i_op = &page_symlink_inode_operations;
1217 inode->i_mapping->a_ops = &jffs_address_operations;
1219 d_instantiate(dentry, inode);
1221 D3(printk (KERN_NOTICE "symlink(): up biglock\n"));
1222 up(&c->fmc->biglock);
1224 } /* jffs_symlink() */
1227 /* Create an inode inside a JFFS directory (dir) and return it.
1229 * By the time this is called, we already have created
1230 * the directory cache entry for the new file, but it
1231 * is so far negative - it has no inode.
1233 * If the create succeeds, we fill in the inode information
1234 * with d_instantiate().
1237 jffs_create(struct inode *dir, struct dentry *dentry, int mode)
1239 struct jffs_raw_inode raw_inode;
1240 struct jffs_control *c;
1241 struct jffs_node *node;
1242 struct jffs_file *dir_f; /* JFFS representation of the directory. */
1243 struct inode *inode;
1247 int len = dentry->d_name.len;
1248 char *s = (char *)kmalloc(len + 1, GFP_KERNEL);
1249 memcpy(s, dentry->d_name.name, len);
1251 printk("jffs_create(): dir: 0x%p, name: \"%s\"\n", dir, s);
1255 dir_f = (struct jffs_file *)dir->u.generic_ip;
1256 ASSERT(if (!dir_f) {
1257 printk(KERN_ERR "jffs_create(): No reference to a "
1258 "jffs_file struct in inode.\n");
1264 /* Create a node and initialize as much as needed. */
1265 if (!(node = jffs_alloc_node())) {
1266 D(printk("jffs_create(): Allocation failed: node == 0\n"));
1269 D3(printk (KERN_NOTICE "create(): down biglock\n"));
1270 down(&c->fmc->biglock);
1272 node->data_offset = 0;
1273 node->removed_size = 0;
1275 /* Initialize the raw inode. */
1276 raw_inode.magic = JFFS_MAGIC_BITMASK;
1277 raw_inode.ino = c->next_ino++;
1278 raw_inode.pino = dir_f->ino;
1279 raw_inode.version = 1;
1280 raw_inode.mode = mode;
1281 raw_inode.uid = current->fsuid;
1282 raw_inode.gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
1283 raw_inode.atime = CURRENT_TIME;
1284 raw_inode.mtime = raw_inode.atime;
1285 raw_inode.ctime = raw_inode.atime;
1286 raw_inode.offset = 0;
1287 raw_inode.dsize = 0;
1288 raw_inode.rsize = 0;
1289 raw_inode.nsize = dentry->d_name.len;
1290 raw_inode.nlink = 1;
1291 raw_inode.spare = 0;
1292 raw_inode.rename = 0;
1293 raw_inode.deleted = 0;
1295 /* Write the new node to the flash. */
1296 if ((err = jffs_write_node(c, node, &raw_inode,
1297 dentry->d_name.name, 0, 0, NULL)) < 0) {
1298 D(printk("jffs_create(): jffs_write_node() failed.\n"));
1299 jffs_free_node(node);
1300 goto jffs_create_end;
1303 /* Insert the new node into the file system. */
1304 if ((err = jffs_insert_node(c, 0, &raw_inode, dentry->d_name.name,
1306 goto jffs_create_end;
1309 /* Initialize an inode. */
1310 inode = jffs_new_inode(dir, &raw_inode, &err);
1311 if (inode == NULL) {
1312 goto jffs_create_end;
1315 inode->i_op = &jffs_file_inode_operations;
1316 inode->i_fop = &jffs_file_operations;
1317 inode->i_mapping->a_ops = &jffs_address_operations;
1318 inode->i_mapping->nrpages = 0;
1320 d_instantiate(dentry, inode);
1322 D3(printk (KERN_NOTICE "create(): up biglock\n"));
1323 up(&c->fmc->biglock);
1325 } /* jffs_create() */
1328 /* Write, append or rewrite data to an existing file. */
1330 jffs_file_write(struct file *filp, const char *buf, size_t count,
1333 struct jffs_raw_inode raw_inode;
1334 struct jffs_control *c;
1335 struct jffs_file *f;
1336 struct jffs_node *node;
1337 struct dentry *dentry = filp->f_dentry;
1338 struct inode *inode = dentry->d_inode;
1339 int recoverable = 0;
1341 __u32 thiscount = count;
1345 inode = filp->f_dentry->d_inode;
1347 D2(printk("***jffs_file_write(): inode: 0x%p (ino: %lu), "
1348 "filp: 0x%p, buf: 0x%p, count: %d\n",
1349 inode, inode->i_ino, filp, buf, count));
1352 if (inode->i_sb->s_flags & MS_RDONLY) {
1353 D(printk("jffs_file_write(): MS_RDONLY\n"));
1360 if (!S_ISREG(inode->i_mode)) {
1361 D(printk("jffs_file_write(): inode->i_mode == 0x%08x\n",
1366 if (!(f = (struct jffs_file *)inode->u.generic_ip)) {
1367 D(printk("jffs_file_write(): inode->u.generic_ip = 0x%p\n",
1368 inode->u.generic_ip));
1375 * This will never trigger with sane page sizes. leave it in
1376 * anyway, since I'm thinking about how to merge larger writes
1377 * (the current idea is to poke a thread that does the actual
1378 * I/O and starts by doing a down(&inode->i_sem). then we
1379 * would need to get the page cache pages and have a list of
1380 * I/O requests and do write-merging here.
1383 thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
1385 D3(printk (KERN_NOTICE "file_write(): down biglock\n"));
1386 down(&c->fmc->biglock);
1388 /* Urgh. POSIX says we can do short writes if we feel like it.
1389 * In practice, we can't. Nothing will cope. So we loop until
1392 * <_Anarchy_> posix and reality are not interconnected on this issue
1395 /* Things are going to be written so we could allocate and
1396 initialize the necessary data structures now. */
1397 if (!(node = jffs_alloc_node())) {
1398 D(printk("jffs_file_write(): node == 0\n"));
1403 node->data_offset = pos;
1404 node->removed_size = 0;
1406 /* Initialize the raw inode. */
1407 raw_inode.magic = JFFS_MAGIC_BITMASK;
1408 raw_inode.ino = f->ino;
1409 raw_inode.pino = f->pino;
1411 raw_inode.mode = f->mode;
1413 raw_inode.uid = f->uid;
1414 raw_inode.gid = f->gid;
1415 raw_inode.atime = CURRENT_TIME;
1416 raw_inode.mtime = raw_inode.atime;
1417 raw_inode.ctime = f->ctime;
1418 raw_inode.offset = pos;
1419 raw_inode.dsize = thiscount;
1420 raw_inode.rsize = 0;
1421 raw_inode.nsize = f->nsize;
1422 raw_inode.nlink = f->nlink;
1423 raw_inode.spare = 0;
1424 raw_inode.rename = 0;
1425 raw_inode.deleted = 0;
1427 if (pos < f->size) {
1428 node->removed_size = raw_inode.rsize = min(thiscount, (__u32)(f->size - pos));
1430 /* If this node is going entirely over the top of old data,
1431 we can allow it to go into the reserved space, because
1432 we know that GC can reclaim the space later.
1434 if (pos + thiscount < f->size) {
1435 /* If all the data we're overwriting are _real_,
1436 not just holes, then:
1442 /* Write the new node to the flash. */
1443 /* NOTE: We would be quite happy if jffs_write_node() wrote a
1444 smaller node than we were expecting. There's no need for it
1445 to waste the space at the end of the flash just because it's
1446 a little smaller than what we asked for. But that's a whole
1447 new can of worms which I'm not going to open this week.
1450 if ((err = jffs_write_node(c, node, &raw_inode, f->name,
1451 (const unsigned char *)buf,
1452 recoverable, f)) < 0) {
1453 D(printk("jffs_file_write(): jffs_write_node() failed.\n"));
1454 jffs_free_node(node);
1463 /* Insert the new node into the file system. */
1464 if ((err = jffs_insert_node(c, f, &raw_inode, 0, node)) < 0) {
1468 D3(printk("jffs_file_write(): new f_pos %ld.\n", (long)pos));
1470 thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
1473 D3(printk (KERN_NOTICE "file_write(): up biglock\n"));
1474 up(&c->fmc->biglock);
1476 /* Fix things in the real inode. */
1477 if (pos > inode->i_size) {
1478 inode->i_size = pos;
1479 inode->i_blocks = (inode->i_size + 511) >> 9;
1481 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1482 mark_inode_dirty(inode);
1483 invalidate_inode_pages(inode);
1487 } /* jffs_file_write() */
1490 jffs_prepare_write(struct file *filp, struct page *page,
1491 unsigned from, unsigned to)
1493 /* FIXME: we should detect some error conditions here */
1495 /* Bugger that. We should make sure the page is uptodate */
1496 if (!Page_Uptodate(page) && (from || to < PAGE_CACHE_SIZE))
1497 return jffs_do_readpage_nolock(filp, page);
1500 } /* jffs_prepare_write() */
1503 jffs_commit_write(struct file *filp, struct page *page,
1504 unsigned from, unsigned to)
1506 void *addr = page_address(page) + from;
1507 /* XXX: PAGE_CACHE_SHIFT or PAGE_SHIFT */
1508 loff_t pos = (page->index<<PAGE_CACHE_SHIFT) + from;
1510 return jffs_file_write(filp, addr, to-from, &pos);
1511 } /* jffs_commit_write() */
1513 /* This is our ioctl() routine. */
1515 jffs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1518 struct jffs_control *c;
1521 D2(printk("***jffs_ioctl(): cmd = 0x%08x, arg = 0x%08lx\n",
1524 if (!(c = (struct jffs_control *)inode->i_sb->u.generic_sbp)) {
1525 printk(KERN_ERR "JFFS: Bad inode in ioctl() call. "
1526 "(cmd = 0x%08x)\n", cmd);
1529 D3(printk (KERN_NOTICE "ioctl(): down biglock\n"));
1530 down(&c->fmc->biglock);
1533 case JFFS_PRINT_HASH:
1534 jffs_print_hash_table(c);
1536 case JFFS_PRINT_TREE:
1537 jffs_print_tree(c->root, 0);
1539 case JFFS_GET_STATUS:
1541 struct jffs_flash_status fst;
1542 struct jffs_fmcontrol *fmc = c->fmc;
1543 printk("Flash status -- ");
1544 if (!access_ok(VERIFY_WRITE,
1545 (struct jffs_flash_status *)arg,
1546 sizeof(struct jffs_flash_status))) {
1547 D(printk("jffs_ioctl(): Bad arg in "
1548 "JFFS_GET_STATUS ioctl!\n"));
1552 fst.size = fmc->flash_size;
1553 fst.used = fmc->used_size;
1554 fst.dirty = fmc->dirty_size;
1555 fst.begin = fmc->head->offset;
1556 fst.end = fmc->tail->offset + fmc->tail->size;
1557 printk("size: %d, used: %d, dirty: %d, "
1558 "begin: %d, end: %d\n",
1559 fst.size, fst.used, fst.dirty,
1560 fst.begin, fst.end);
1561 if (copy_to_user((struct jffs_flash_status *)arg,
1563 sizeof(struct jffs_flash_status))) {
1571 D3(printk (KERN_NOTICE "ioctl(): up biglock\n"));
1572 up(&c->fmc->biglock);
1574 } /* jffs_ioctl() */
1577 static struct address_space_operations jffs_address_operations = {
1578 readpage: jffs_readpage,
1579 prepare_write: jffs_prepare_write,
1580 commit_write: jffs_commit_write,
1583 static int jffs_fsync(struct file *f, struct dentry *d, int datasync)
1585 /* We currently have O_SYNC operations at all times.
1592 extern int generic_file_open(struct inode *, struct file *) __attribute__((weak));
1593 extern loff_t generic_file_llseek(struct file *, loff_t, int) __attribute__((weak));
1595 static struct file_operations jffs_file_operations =
1597 open: generic_file_open,
1598 llseek: generic_file_llseek,
1599 read: generic_file_read,
1600 write: generic_file_write,
1602 mmap: generic_file_mmap,
1607 static struct inode_operations jffs_file_inode_operations =
1609 lookup: jffs_lookup, /* lookup */
1610 setattr: jffs_setattr,
1614 static struct file_operations jffs_dir_operations =
1616 readdir: jffs_readdir,
1620 static struct inode_operations jffs_dir_inode_operations =
1622 create: jffs_create,
1623 lookup: jffs_lookup,
1624 unlink: jffs_unlink,
1625 symlink: jffs_symlink,
1629 rename: jffs_rename,
1630 setattr: jffs_setattr,
1634 /* Initialize an inode for the VFS. */
1636 jffs_read_inode(struct inode *inode)
1638 struct jffs_file *f;
1639 struct jffs_control *c;
1641 D3(printk("jffs_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
1644 D(printk("jffs_read_inode(): !inode->i_sb ==> "
1645 "No super block!\n"));
1648 c = (struct jffs_control *)inode->i_sb->u.generic_sbp;
1649 D3(printk (KERN_NOTICE "read_inode(): down biglock\n"));
1650 down(&c->fmc->biglock);
1651 if (!(f = jffs_find_file(c, inode->i_ino))) {
1652 D(printk("jffs_read_inode(): No such inode (%lu).\n",
1654 D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
1655 up(&c->fmc->biglock);
1658 inode->u.generic_ip = (void *)f;
1659 inode->i_mode = f->mode;
1660 inode->i_nlink = f->nlink;
1661 inode->i_uid = f->uid;
1662 inode->i_gid = f->gid;
1663 inode->i_size = f->size;
1664 inode->i_atime = f->atime;
1665 inode->i_mtime = f->mtime;
1666 inode->i_ctime = f->ctime;
1667 inode->i_blksize = PAGE_SIZE;
1668 inode->i_blocks = (inode->i_size + 511) >> 9;
1669 if (S_ISREG(inode->i_mode)) {
1670 inode->i_op = &jffs_file_inode_operations;
1671 inode->i_fop = &jffs_file_operations;
1672 inode->i_mapping->a_ops = &jffs_address_operations;
1674 else if (S_ISDIR(inode->i_mode)) {
1675 inode->i_op = &jffs_dir_inode_operations;
1676 inode->i_fop = &jffs_dir_operations;
1678 else if (S_ISLNK(inode->i_mode)) {
1679 inode->i_op = &page_symlink_inode_operations;
1680 inode->i_mapping->a_ops = &jffs_address_operations;
1683 /* If the node is a device of some sort, then the number of
1684 the device should be read from the flash memory and then
1685 added to the inode's i_rdev member. */
1687 jffs_read_data(f, (char *)&rdev, 0, sizeof(kdev_t));
1688 init_special_inode(inode, inode->i_mode, kdev_t_to_nr(rdev));
1691 D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
1692 up(&c->fmc->biglock);
1697 jffs_delete_inode(struct inode *inode)
1699 struct jffs_file *f;
1700 struct jffs_control *c;
1701 D3(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
1706 inode->i_blocks = 0;
1707 inode->u.generic_ip = 0;
1709 if (inode->i_nlink == 0) {
1710 c = (struct jffs_control *) inode->i_sb->u.generic_sbp;
1711 f = (struct jffs_file *) jffs_find_file (c, inode->i_ino);
1712 jffs_possibly_delete_file(f);
1720 jffs_write_super(struct super_block *sb)
1722 struct jffs_control *c = (struct jffs_control *)sb->u.generic_sbp;
1724 jffs_garbage_collect_trigger(c);
1727 static struct super_operations jffs_ops =
1729 read_inode: jffs_read_inode,
1730 delete_inode: jffs_delete_inode,
1731 put_super: jffs_put_super,
1732 write_super: jffs_write_super,
1733 statfs: jffs_statfs,
1737 static DECLARE_FSTYPE_DEV(jffs_fs_type, "jffs", jffs_read_super);
1742 printk(KERN_INFO "JFFS version " JFFS_VERSION_STRING
1743 ", (C) 1999, 2000 Axis Communications AB\n");
1745 #ifdef CONFIG_JFFS_PROC_FS
1746 jffs_proc_root = proc_mkdir("jffs", proc_root_fs);
1748 fm_cache = kmem_cache_create("jffs_fm", sizeof(struct jffs_fm),
1749 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
1750 node_cache = kmem_cache_create("jffs_node",sizeof(struct jffs_node),
1751 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
1752 return register_filesystem(&jffs_fs_type);
1758 unregister_filesystem(&jffs_fs_type);
1759 kmem_cache_destroy(fm_cache);
1760 kmem_cache_destroy(node_cache);
1765 module_init(init_jffs_fs)
1766 module_exit(exit_jffs_fs)
1768 MODULE_DESCRIPTION("The Journalling Flash File System");
1769 MODULE_AUTHOR("Axis Communications AB.");
1770 MODULE_LICENSE("GPL");