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 #include <linux/time.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/errno.h>
35 #include <linux/slab.h>
36 #include <linux/jffs.h>
38 #include <linux/smp_lock.h>
39 #include <linux/ioctl.h>
40 #include <linux/stat.h>
41 #include <linux/blkdev.h>
42 #include <linux/quotaops.h>
43 #include <linux/highmem.h>
44 #include <linux/vfs.h>
45 #include <linux/mutex.h>
46 #include <asm/byteorder.h>
47 #include <asm/uaccess.h>
51 #ifdef CONFIG_JFFS_PROC_FS
52 #include "jffs_proc.h"
55 static int jffs_remove(struct inode
*dir
, struct dentry
*dentry
, int type
);
57 static struct super_operations jffs_ops
;
58 static const struct file_operations jffs_file_operations
;
59 static struct inode_operations jffs_file_inode_operations
;
60 static const struct file_operations jffs_dir_operations
;
61 static struct inode_operations jffs_dir_inode_operations
;
62 static const struct address_space_operations jffs_address_operations
;
64 kmem_cache_t
*node_cache
= NULL
;
65 kmem_cache_t
*fm_cache
= NULL
;
67 /* Called by the VFS at mount time to initialize the whole file system. */
68 static int jffs_fill_super(struct super_block
*sb
, void *data
, int silent
)
70 struct inode
*root_inode
;
71 struct jffs_control
*c
;
73 sb
->s_flags
|= MS_NODIRATIME
;
75 D1(printk(KERN_NOTICE
"JFFS: Trying to mount device %s.\n",
78 if (MAJOR(sb
->s_dev
) != MTD_BLOCK_MAJOR
) {
79 printk(KERN_WARNING
"JFFS: Trying to mount a "
84 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
85 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
86 sb
->s_fs_info
= (void *) 0;
87 sb
->s_maxbytes
= 0xFFFFFFFF;
89 /* Build the file system. */
90 if (jffs_build_fs(sb
) < 0) {
95 * set up enough so that we can read an inode
97 sb
->s_magic
= JFFS_MAGIC_SB_BITMASK
;
100 root_inode
= iget(sb
, JFFS_MIN_INO
);
104 /* Get the root directory of this file system. */
105 if (!(sb
->s_root
= d_alloc_root(root_inode
))) {
109 c
= (struct jffs_control
*) sb
->s_fs_info
;
111 #ifdef CONFIG_JFFS_PROC_FS
112 /* Set up the jffs proc file system. */
113 if (jffs_register_jffs_proc_dir(MINOR(sb
->s_dev
), c
) < 0) {
114 printk(KERN_WARNING
"JFFS: Failed to initialize the JFFS "
115 "proc file system for device %s.\n",
120 /* Set the Garbage Collection thresholds */
122 /* GC if free space goes below 5% of the total size */
123 c
->gc_minfree_threshold
= c
->fmc
->flash_size
/ 20;
125 if (c
->gc_minfree_threshold
< c
->fmc
->sector_size
)
126 c
->gc_minfree_threshold
= c
->fmc
->sector_size
;
128 /* GC if dirty space exceeds 33% of the total size. */
129 c
->gc_maxdirty_threshold
= c
->fmc
->flash_size
/ 3;
131 if (c
->gc_maxdirty_threshold
< c
->fmc
->sector_size
)
132 c
->gc_maxdirty_threshold
= c
->fmc
->sector_size
;
135 c
->thread_pid
= kernel_thread (jffs_garbage_collect_thread
,
138 D1(printk(KERN_NOTICE
"JFFS: GC thread pid=%d.\n", (int) c
->thread_pid
));
140 D1(printk(KERN_NOTICE
"JFFS: Successfully mounted device %s.\n",
147 jffs_cleanup_control((struct jffs_control
*)sb
->s_fs_info
);
149 printk(KERN_WARNING
"JFFS: Failed to mount device %s.\n",
155 /* This function is called when the file system is umounted. */
157 jffs_put_super(struct super_block
*sb
)
159 struct jffs_control
*c
= (struct jffs_control
*) sb
->s_fs_info
;
161 D2(printk("jffs_put_super()\n"));
163 #ifdef CONFIG_JFFS_PROC_FS
164 jffs_unregister_jffs_proc_dir(c
);
168 D1(printk (KERN_NOTICE
"jffs_put_super(): Telling gc thread to die.\n"));
169 send_sig(SIGKILL
, c
->gc_task
, 1);
171 wait_for_completion(&c
->gc_thread_comp
);
173 D1(printk (KERN_NOTICE
"jffs_put_super(): Successfully waited on thread.\n"));
175 jffs_cleanup_control((struct jffs_control
*)sb
->s_fs_info
);
176 D1(printk(KERN_NOTICE
"JFFS: Successfully unmounted device %s.\n",
181 /* This function is called when user commands like chmod, chgrp and
182 chown are executed. System calls like trunc() results in a call
185 jffs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
187 struct inode
*inode
= dentry
->d_inode
;
188 struct jffs_raw_inode raw_inode
;
189 struct jffs_control
*c
;
190 struct jffs_fmcontrol
*fmc
;
192 struct jffs_node
*new_node
;
199 if ((res
= inode_change_ok(inode
, iattr
)))
202 c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
;
205 D3(printk (KERN_NOTICE
"notify_change(): down biglock\n"));
206 mutex_lock(&fmc
->biglock
);
208 f
= jffs_find_file(c
, inode
->i_ino
);
211 printk("jffs_setattr(): Invalid inode number: %lu\n",
213 D3(printk (KERN_NOTICE
"notify_change(): up biglock\n"));
214 mutex_unlock(&fmc
->biglock
);
219 D1(printk("***jffs_setattr(): file: \"%s\", ino: %u\n",
222 update_all
= iattr
->ia_valid
& ATTR_FORCE
;
224 if ( (update_all
|| iattr
->ia_valid
& ATTR_SIZE
)
225 && (iattr
->ia_size
+ 128 < f
->size
) ) {
226 /* We're shrinking the file by more than 128 bytes.
227 We'll be able to GC and recover this space, so
228 allow it to go into the reserved space. */
232 if (!(new_node
= jffs_alloc_node())) {
233 D(printk("jffs_setattr(): Allocation failed!\n"));
234 D3(printk (KERN_NOTICE
"notify_change(): up biglock\n"));
235 mutex_unlock(&fmc
->biglock
);
240 new_node
->data_offset
= 0;
241 new_node
->removed_size
= 0;
242 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
243 raw_inode
.ino
= f
->ino
;
244 raw_inode
.pino
= f
->pino
;
245 raw_inode
.mode
= f
->mode
;
246 raw_inode
.uid
= f
->uid
;
247 raw_inode
.gid
= f
->gid
;
248 raw_inode
.atime
= f
->atime
;
249 raw_inode
.mtime
= f
->mtime
;
250 raw_inode
.ctime
= f
->ctime
;
252 raw_inode
.offset
= 0;
255 raw_inode
.nsize
= f
->nsize
;
256 raw_inode
.nlink
= f
->nlink
;
258 raw_inode
.rename
= 0;
259 raw_inode
.deleted
= 0;
261 if (update_all
|| iattr
->ia_valid
& ATTR_MODE
) {
262 raw_inode
.mode
= iattr
->ia_mode
;
263 inode
->i_mode
= iattr
->ia_mode
;
265 if (update_all
|| iattr
->ia_valid
& ATTR_UID
) {
266 raw_inode
.uid
= iattr
->ia_uid
;
267 inode
->i_uid
= iattr
->ia_uid
;
269 if (update_all
|| iattr
->ia_valid
& ATTR_GID
) {
270 raw_inode
.gid
= iattr
->ia_gid
;
271 inode
->i_gid
= iattr
->ia_gid
;
273 if (update_all
|| iattr
->ia_valid
& ATTR_SIZE
) {
275 D1(printk("jffs_notify_change(): Changing size "
276 "to %lu bytes!\n", (long)iattr
->ia_size
));
277 raw_inode
.offset
= iattr
->ia_size
;
279 /* Calculate how many bytes need to be removed from
281 if (f
->size
< iattr
->ia_size
) {
285 len
= f
->size
- iattr
->ia_size
;
288 raw_inode
.rsize
= len
;
290 /* The updated node will be a removal node, with
291 base at the new size and size of the nbr of bytes
293 new_node
->data_offset
= iattr
->ia_size
;
294 new_node
->removed_size
= len
;
295 inode
->i_size
= iattr
->ia_size
;
296 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
299 invalidate_inode_pages(inode
->i_mapping
);
301 inode
->i_ctime
= CURRENT_TIME_SEC
;
302 inode
->i_mtime
= inode
->i_ctime
;
304 if (update_all
|| iattr
->ia_valid
& ATTR_ATIME
) {
305 raw_inode
.atime
= iattr
->ia_atime
.tv_sec
;
306 inode
->i_atime
= iattr
->ia_atime
;
308 if (update_all
|| iattr
->ia_valid
& ATTR_MTIME
) {
309 raw_inode
.mtime
= iattr
->ia_mtime
.tv_sec
;
310 inode
->i_mtime
= iattr
->ia_mtime
;
312 if (update_all
|| iattr
->ia_valid
& ATTR_CTIME
) {
313 raw_inode
.ctime
= iattr
->ia_ctime
.tv_sec
;
314 inode
->i_ctime
= iattr
->ia_ctime
;
317 /* Write this node to the flash. */
318 if ((res
= jffs_write_node(c
, new_node
, &raw_inode
, f
->name
, NULL
, recoverable
, f
)) < 0) {
319 D(printk("jffs_notify_change(): The write failed!\n"));
320 jffs_free_node(new_node
);
321 D3(printk (KERN_NOTICE
"n_c(): up biglock\n"));
322 mutex_unlock(&c
->fmc
->biglock
);
326 jffs_insert_node(c
, f
, &raw_inode
, NULL
, new_node
);
328 mark_inode_dirty(inode
);
329 D3(printk (KERN_NOTICE
"n_c(): up biglock\n"));
330 mutex_unlock(&c
->fmc
->biglock
);
334 } /* jffs_notify_change() */
337 static struct inode
*
338 jffs_new_inode(const struct inode
* dir
, struct jffs_raw_inode
*raw_inode
,
341 struct super_block
* sb
;
342 struct inode
* inode
;
343 struct jffs_control
*c
;
347 inode
= new_inode(sb
);
353 c
= (struct jffs_control
*)sb
->s_fs_info
;
355 inode
->i_ino
= raw_inode
->ino
;
356 inode
->i_mode
= raw_inode
->mode
;
357 inode
->i_nlink
= raw_inode
->nlink
;
358 inode
->i_uid
= raw_inode
->uid
;
359 inode
->i_gid
= raw_inode
->gid
;
360 inode
->i_size
= raw_inode
->dsize
;
361 inode
->i_atime
.tv_sec
= raw_inode
->atime
;
362 inode
->i_mtime
.tv_sec
= raw_inode
->mtime
;
363 inode
->i_ctime
.tv_sec
= raw_inode
->ctime
;
364 inode
->i_ctime
.tv_nsec
= 0;
365 inode
->i_mtime
.tv_nsec
= 0;
366 inode
->i_atime
.tv_nsec
= 0;
367 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
369 f
= jffs_find_file(c
, raw_inode
->ino
);
371 inode
->i_private
= (void *)f
;
372 insert_inode_hash(inode
);
377 /* Get statistics of the file system. */
379 jffs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
381 struct jffs_control
*c
= (struct jffs_control
*) dentry
->d_sb
->s_fs_info
;
382 struct jffs_fmcontrol
*fmc
;
388 D2(printk("jffs_statfs()\n"));
390 buf
->f_type
= JFFS_MAGIC_SB_BITMASK
;
391 buf
->f_bsize
= PAGE_CACHE_SIZE
;
392 buf
->f_blocks
= (fmc
->flash_size
/ PAGE_CACHE_SIZE
)
393 - (fmc
->min_free_size
/ PAGE_CACHE_SIZE
);
394 buf
->f_bfree
= (jffs_free_size1(fmc
) + jffs_free_size2(fmc
) +
395 fmc
->dirty_size
- fmc
->min_free_size
)
397 buf
->f_bavail
= buf
->f_bfree
;
399 /* Find out how many files there are in the filesystem. */
400 buf
->f_files
= jffs_foreach_file(c
, jffs_file_count
);
401 buf
->f_ffree
= buf
->f_bfree
;
402 /* buf->f_fsid = 0; */
403 buf
->f_namelen
= JFFS_MAX_NAME_LEN
;
413 jffs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
414 struct inode
*new_dir
, struct dentry
*new_dentry
)
416 struct jffs_raw_inode raw_inode
;
417 struct jffs_control
*c
;
418 struct jffs_file
*old_dir_f
;
419 struct jffs_file
*new_dir_f
;
420 struct jffs_file
*del_f
;
422 struct jffs_node
*node
;
425 __u32 rename_data
= 0;
427 D2(printk("***jffs_rename()\n"));
429 D(printk("jffs_rename(): old_dir: 0x%p, old name: 0x%p, "
430 "new_dir: 0x%p, new name: 0x%p\n",
431 old_dir
, old_dentry
->d_name
.name
,
432 new_dir
, new_dentry
->d_name
.name
));
435 c
= (struct jffs_control
*)old_dir
->i_sb
->s_fs_info
;
437 printk(KERN_ERR
"jffs_rename(): The old_dir inode "
438 "didn't have a reference to a jffs_file struct\n");
444 if (!(old_dir_f
= old_dir
->i_private
)) {
445 D(printk("jffs_rename(): Old dir invalid.\n"));
446 goto jffs_rename_end
;
449 /* Try to find the file to move. */
451 if (!(f
= jffs_find_child(old_dir_f
, old_dentry
->d_name
.name
,
452 old_dentry
->d_name
.len
))) {
453 goto jffs_rename_end
;
456 /* Find the new directory. */
458 if (!(new_dir_f
= new_dir
->i_private
)) {
459 D(printk("jffs_rename(): New dir invalid.\n"));
460 goto jffs_rename_end
;
462 D3(printk (KERN_NOTICE
"rename(): down biglock\n"));
463 mutex_lock(&c
->fmc
->biglock
);
464 /* Create a node and initialize as much as needed. */
466 if (!(node
= jffs_alloc_node())) {
467 D(printk("jffs_rename(): Allocation failed: node == 0\n"));
468 goto jffs_rename_end
;
470 node
->data_offset
= 0;
471 node
->removed_size
= 0;
473 /* Initialize the raw inode. */
474 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
475 raw_inode
.ino
= f
->ino
;
476 raw_inode
.pino
= new_dir_f
->ino
;
477 /* raw_inode.version = f->highest_version + 1; */
478 raw_inode
.mode
= f
->mode
;
479 raw_inode
.uid
= current
->fsuid
;
480 raw_inode
.gid
= current
->fsgid
;
482 raw_inode
.uid
= f
->uid
;
483 raw_inode
.gid
= f
->gid
;
485 raw_inode
.atime
= get_seconds();
486 raw_inode
.mtime
= raw_inode
.atime
;
487 raw_inode
.ctime
= f
->ctime
;
488 raw_inode
.offset
= 0;
491 raw_inode
.nsize
= new_dentry
->d_name
.len
;
492 raw_inode
.nlink
= f
->nlink
;
494 raw_inode
.rename
= 0;
495 raw_inode
.deleted
= 0;
497 /* See if there already exists a file with the same name as
499 if ((del_f
= jffs_find_child(new_dir_f
, new_dentry
->d_name
.name
,
500 new_dentry
->d_name
.len
))) {
501 raw_inode
.rename
= 1;
502 raw_inode
.dsize
= sizeof(__u32
);
503 rename_data
= del_f
->ino
;
506 /* Write the new node to the flash memory. */
507 if ((result
= jffs_write_node(c
, node
, &raw_inode
,
508 new_dentry
->d_name
.name
,
509 (unsigned char*)&rename_data
, 0, f
)) < 0) {
510 D(printk("jffs_rename(): Failed to write node to flash.\n"));
511 jffs_free_node(node
);
512 goto jffs_rename_end
;
516 if (raw_inode
.rename
) {
517 /* The file with the same name must be deleted. */
518 //FIXME deadlock down(&c->fmc->gclock);
519 if ((result
= jffs_remove(new_dir
, new_dentry
,
521 /* This is really bad. */
522 printk(KERN_ERR
"JFFS: An error occurred in "
525 // up(&c->fmc->gclock);
528 if (old_dir_f
!= new_dir_f
) {
529 /* Remove the file from its old position in the
531 jffs_unlink_file_from_tree(f
);
534 /* Insert the new node into the file system. */
535 if ((result
= jffs_insert_node(c
, f
, &raw_inode
,
536 new_dentry
->d_name
.name
, node
)) < 0) {
537 D(printk(KERN_ERR
"jffs_rename(): jffs_insert_node() "
541 if (old_dir_f
!= new_dir_f
) {
542 /* Insert the file to its new position in the
544 jffs_insert_file_into_tree(f
);
547 /* This is a kind of update of the inode we're about to make
548 here. This is what they do in ext2fs. Kind of. */
549 if ((inode
= iget(new_dir
->i_sb
, f
->ino
))) {
550 inode
->i_ctime
= CURRENT_TIME_SEC
;
551 mark_inode_dirty(inode
);
556 D3(printk (KERN_NOTICE
"rename(): up biglock\n"));
557 mutex_unlock(&c
->fmc
->biglock
);
560 } /* jffs_rename() */
563 /* Read the contents of a directory. Used by programs like `ls'
566 jffs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
569 struct dentry
*dentry
= filp
->f_dentry
;
570 struct inode
*inode
= dentry
->d_inode
;
571 struct jffs_control
*c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
;
575 D3(printk (KERN_NOTICE
"readdir(): down biglock\n"));
576 mutex_lock(&c
->fmc
->biglock
);
578 D2(printk("jffs_readdir(): inode: 0x%p, filp: 0x%p\n", inode
, filp
));
579 if (filp
->f_pos
== 0) {
580 D3(printk("jffs_readdir(): \".\" %lu\n", inode
->i_ino
));
581 if (filldir(dirent
, ".", 1, filp
->f_pos
, inode
->i_ino
, DT_DIR
) < 0) {
582 D3(printk (KERN_NOTICE
"readdir(): up biglock\n"));
583 mutex_unlock(&c
->fmc
->biglock
);
589 if (filp
->f_pos
== 1) {
590 if (inode
->i_ino
== JFFS_MIN_INO
) {
591 ddino
= JFFS_MIN_INO
;
594 ddino
= ((struct jffs_file
*)
595 inode
->i_private
)->pino
;
597 D3(printk("jffs_readdir(): \"..\" %u\n", ddino
));
598 if (filldir(dirent
, "..", 2, filp
->f_pos
, ddino
, DT_DIR
) < 0) {
599 D3(printk (KERN_NOTICE
"readdir(): up biglock\n"));
600 mutex_unlock(&c
->fmc
->biglock
);
606 f
= ((struct jffs_file
*)inode
->i_private
)->children
;
609 while(f
&& (f
->deleted
|| j
++ < filp
->f_pos
)) {
614 D3(printk("jffs_readdir(): \"%s\" ino: %u\n",
615 (f
->name
? f
->name
: ""), f
->ino
));
616 if (filldir(dirent
, f
->name
, f
->nsize
,
617 filp
->f_pos
, f
->ino
, DT_UNKNOWN
) < 0) {
618 D3(printk (KERN_NOTICE
"readdir(): up biglock\n"));
619 mutex_unlock(&c
->fmc
->biglock
);
626 } while(f
&& f
->deleted
);
628 D3(printk (KERN_NOTICE
"readdir(): up biglock\n"));
629 mutex_unlock(&c
->fmc
->biglock
);
632 } /* jffs_readdir() */
635 /* Find a file in a directory. If the file exists, return its
636 corresponding dentry. */
637 static struct dentry
*
638 jffs_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
642 struct jffs_control
*c
= (struct jffs_control
*)dir
->i_sb
->s_fs_info
;
646 struct inode
*inode
= NULL
;
648 len
= dentry
->d_name
.len
;
649 name
= dentry
->d_name
.name
;
654 char *s
= kmalloc(len
+ 1, GFP_KERNEL
);
655 memcpy(s
, name
, len
);
657 printk("jffs_lookup(): dir: 0x%p, name: \"%s\"\n", dir
, s
);
661 D3(printk (KERN_NOTICE
"lookup(): down biglock\n"));
662 mutex_lock(&c
->fmc
->biglock
);
665 if (len
> JFFS_MAX_NAME_LEN
) {
666 goto jffs_lookup_end
;
670 if (!(d
= (struct jffs_file
*)dir
->i_private
)) {
671 D(printk("jffs_lookup(): No such inode! (%lu)\n",
673 goto jffs_lookup_end
;
676 /* Get the corresponding inode to the file. */
678 /* iget calls jffs_read_inode, so we need to drop the biglock
679 before calling iget. Unfortunately, the GC has a tendency
680 to sneak in here, because iget sometimes calls schedule ().
683 if ((len
== 1) && (name
[0] == '.')) {
684 D3(printk (KERN_NOTICE
"lookup(): up biglock\n"));
685 mutex_unlock(&c
->fmc
->biglock
);
686 if (!(inode
= iget(dir
->i_sb
, d
->ino
))) {
687 D(printk("jffs_lookup(): . iget() ==> NULL\n"));
688 goto jffs_lookup_end_no_biglock
;
690 D3(printk (KERN_NOTICE
"lookup(): down biglock\n"));
691 mutex_lock(&c
->fmc
->biglock
);
692 } else if ((len
== 2) && (name
[0] == '.') && (name
[1] == '.')) {
693 D3(printk (KERN_NOTICE
"lookup(): up biglock\n"));
694 mutex_unlock(&c
->fmc
->biglock
);
695 if (!(inode
= iget(dir
->i_sb
, d
->pino
))) {
696 D(printk("jffs_lookup(): .. iget() ==> NULL\n"));
697 goto jffs_lookup_end_no_biglock
;
699 D3(printk (KERN_NOTICE
"lookup(): down biglock\n"));
700 mutex_lock(&c
->fmc
->biglock
);
701 } else if ((f
= jffs_find_child(d
, name
, len
))) {
702 D3(printk (KERN_NOTICE
"lookup(): up biglock\n"));
703 mutex_unlock(&c
->fmc
->biglock
);
704 if (!(inode
= iget(dir
->i_sb
, f
->ino
))) {
705 D(printk("jffs_lookup(): iget() ==> NULL\n"));
706 goto jffs_lookup_end_no_biglock
;
708 D3(printk (KERN_NOTICE
"lookup(): down biglock\n"));
709 mutex_lock(&c
->fmc
->biglock
);
711 D3(printk("jffs_lookup(): Couldn't find the file. "
712 "f = 0x%p, name = \"%s\", d = 0x%p, d->ino = %u\n",
713 f
, name
, d
, d
->ino
));
717 d_add(dentry
, inode
);
718 D3(printk (KERN_NOTICE
"lookup(): up biglock\n"));
719 mutex_unlock(&c
->fmc
->biglock
);
724 D3(printk (KERN_NOTICE
"lookup(): up biglock\n"));
725 mutex_unlock(&c
->fmc
->biglock
);
727 jffs_lookup_end_no_biglock
:
730 } /* jffs_lookup() */
733 /* Try to read a page of data from a file. */
735 jffs_do_readpage_nolock(struct file
*file
, struct page
*page
)
738 unsigned long read_len
;
740 struct inode
*inode
= (struct inode
*)page
->mapping
->host
;
741 struct jffs_file
*f
= (struct jffs_file
*)inode
->i_private
;
742 struct jffs_control
*c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
;
746 D2(printk("***jffs_readpage(): file = \"%s\", page->index = %lu\n",
747 (f
->name
? f
->name
: ""), (long)page
->index
));
750 /* Don't SetPageLocked(page), should be locked already */
751 ClearPageUptodate(page
);
752 ClearPageError(page
);
754 D3(printk (KERN_NOTICE
"readpage(): down biglock\n"));
755 mutex_lock(&c
->fmc
->biglock
);
759 offset
= page_offset(page
);
762 buf
= page_address(page
);
763 if (offset
< inode
->i_size
) {
764 read_len
= min_t(long, inode
->i_size
- offset
, PAGE_SIZE
);
765 r
= jffs_read_data(f
, buf
, offset
, read_len
);
769 printk("***jffs_readpage(): Read error! "
770 "Wanted to read %lu bytes but only "
771 "read %d bytes.\n", read_len
, r
);
777 /* This handles the case of partial or no read in above */
778 if(read_len
< PAGE_SIZE
)
779 memset(buf
+ read_len
, 0, PAGE_SIZE
- read_len
);
780 flush_dcache_page(page
);
783 D3(printk (KERN_NOTICE
"readpage(): up biglock\n"));
784 mutex_unlock(&c
->fmc
->biglock
);
789 SetPageUptodate(page
);
792 page_cache_release(page
);
794 D3(printk("jffs_readpage(): Leaving...\n"));
797 } /* jffs_do_readpage_nolock() */
799 static int jffs_readpage(struct file
*file
, struct page
*page
)
801 int ret
= jffs_do_readpage_nolock(file
, page
);
806 /* Create a new directory. */
808 jffs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
810 struct jffs_raw_inode raw_inode
;
811 struct jffs_control
*c
;
812 struct jffs_node
*node
;
813 struct jffs_file
*dir_f
;
820 int len
= dentry
->d_name
.len
;
821 char *_name
= (char *) kmalloc(len
+ 1, GFP_KERNEL
);
822 memcpy(_name
, dentry
->d_name
.name
, len
);
824 printk("***jffs_mkdir(): dir = 0x%p, name = \"%s\", "
825 "len = %d, mode = 0x%08x\n", dir
, _name
, len
, mode
);
830 dir_f
= dir
->i_private
;
833 printk(KERN_ERR
"jffs_mkdir(): No reference to a "
834 "jffs_file struct in inode.\n");
840 D3(printk (KERN_NOTICE
"mkdir(): down biglock\n"));
841 mutex_lock(&c
->fmc
->biglock
);
843 dir_mode
= S_IFDIR
| (mode
& (S_IRWXUGO
|S_ISVTX
)
844 & ~current
->fs
->umask
);
845 if (dir
->i_mode
& S_ISGID
) {
849 /* Create a node and initialize it as much as needed. */
850 if (!(node
= jffs_alloc_node())) {
851 D(printk("jffs_mkdir(): Allocation failed: node == 0\n"));
855 node
->data_offset
= 0;
856 node
->removed_size
= 0;
858 /* Initialize the raw inode. */
859 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
860 raw_inode
.ino
= c
->next_ino
++;
861 raw_inode
.pino
= dir_f
->ino
;
862 raw_inode
.version
= 1;
863 raw_inode
.mode
= dir_mode
;
864 raw_inode
.uid
= current
->fsuid
;
865 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
866 /* raw_inode.gid = current->fsgid; */
867 raw_inode
.atime
= get_seconds();
868 raw_inode
.mtime
= raw_inode
.atime
;
869 raw_inode
.ctime
= raw_inode
.atime
;
870 raw_inode
.offset
= 0;
873 raw_inode
.nsize
= dentry
->d_name
.len
;
876 raw_inode
.rename
= 0;
877 raw_inode
.deleted
= 0;
879 /* Write the new node to the flash. */
880 if ((result
= jffs_write_node(c
, node
, &raw_inode
,
881 dentry
->d_name
.name
, NULL
, 0, NULL
)) < 0) {
882 D(printk("jffs_mkdir(): jffs_write_node() failed.\n"));
883 jffs_free_node(node
);
887 /* Insert the new node into the file system. */
888 if ((result
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
893 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
899 inode
->i_op
= &jffs_dir_inode_operations
;
900 inode
->i_fop
= &jffs_dir_operations
;
902 mark_inode_dirty(dir
);
903 d_instantiate(dentry
, inode
);
907 D3(printk (KERN_NOTICE
"mkdir(): up biglock\n"));
908 mutex_unlock(&c
->fmc
->biglock
);
914 /* Remove a directory. */
916 jffs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
918 struct jffs_control
*c
= (struct jffs_control
*)dir
->i_sb
->s_fs_info
;
920 D3(printk("***jffs_rmdir()\n"));
921 D3(printk (KERN_NOTICE
"rmdir(): down biglock\n"));
923 mutex_lock(&c
->fmc
->biglock
);
924 ret
= jffs_remove(dir
, dentry
, S_IFDIR
);
925 D3(printk (KERN_NOTICE
"rmdir(): up biglock\n"));
926 mutex_unlock(&c
->fmc
->biglock
);
932 /* Remove any kind of file except for directories. */
934 jffs_unlink(struct inode
*dir
, struct dentry
*dentry
)
936 struct jffs_control
*c
= (struct jffs_control
*)dir
->i_sb
->s_fs_info
;
940 D3(printk("***jffs_unlink()\n"));
941 D3(printk (KERN_NOTICE
"unlink(): down biglock\n"));
942 mutex_lock(&c
->fmc
->biglock
);
943 ret
= jffs_remove(dir
, dentry
, 0);
944 D3(printk (KERN_NOTICE
"unlink(): up biglock\n"));
945 mutex_unlock(&c
->fmc
->biglock
);
951 /* Remove a JFFS entry, i.e. plain files, directories, etc. Here we
952 shouldn't test for free space on the device. */
954 jffs_remove(struct inode
*dir
, struct dentry
*dentry
, int type
)
956 struct jffs_raw_inode raw_inode
;
957 struct jffs_control
*c
;
958 struct jffs_file
*dir_f
; /* The file-to-remove's parent. */
959 struct jffs_file
*del_f
; /* The file to remove. */
960 struct jffs_node
*del_node
;
961 struct inode
*inode
= NULL
;
965 int len
= dentry
->d_name
.len
;
966 const char *name
= dentry
->d_name
.name
;
967 char *_name
= (char *) kmalloc(len
+ 1, GFP_KERNEL
);
968 memcpy(_name
, name
, len
);
970 printk("***jffs_remove(): file = \"%s\", ino = %ld\n", _name
, dentry
->d_inode
->i_ino
);
974 dir_f
= dir
->i_private
;
978 if (!(del_f
= jffs_find_child(dir_f
, dentry
->d_name
.name
,
979 dentry
->d_name
.len
))) {
980 D(printk("jffs_remove(): jffs_find_child() failed.\n"));
981 goto jffs_remove_end
;
985 struct jffs_file
*child
= del_f
->children
;
987 if( !child
->deleted
) {
989 goto jffs_remove_end
;
991 child
= child
->sibling_next
;
994 else if (S_ISDIR(del_f
->mode
)) {
995 D(printk("jffs_remove(): node is a directory "
996 "but it shouldn't be.\n"));
998 goto jffs_remove_end
;
1001 inode
= dentry
->d_inode
;
1004 if (del_f
->ino
!= inode
->i_ino
)
1005 goto jffs_remove_end
;
1007 if (!inode
->i_nlink
) {
1008 printk("Deleting nonexistent file inode: %lu, nlink: %d\n",
1009 inode
->i_ino
, inode
->i_nlink
);
1013 /* Create a node for the deletion. */
1015 if (!(del_node
= jffs_alloc_node())) {
1016 D(printk("jffs_remove(): Allocation failed!\n"));
1017 goto jffs_remove_end
;
1019 del_node
->data_offset
= 0;
1020 del_node
->removed_size
= 0;
1022 /* Initialize the raw inode. */
1023 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1024 raw_inode
.ino
= del_f
->ino
;
1025 raw_inode
.pino
= del_f
->pino
;
1026 /* raw_inode.version = del_f->highest_version + 1; */
1027 raw_inode
.mode
= del_f
->mode
;
1028 raw_inode
.uid
= current
->fsuid
;
1029 raw_inode
.gid
= current
->fsgid
;
1030 raw_inode
.atime
= get_seconds();
1031 raw_inode
.mtime
= del_f
->mtime
;
1032 raw_inode
.ctime
= raw_inode
.atime
;
1033 raw_inode
.offset
= 0;
1034 raw_inode
.dsize
= 0;
1035 raw_inode
.rsize
= 0;
1036 raw_inode
.nsize
= 0;
1037 raw_inode
.nlink
= del_f
->nlink
;
1038 raw_inode
.spare
= 0;
1039 raw_inode
.rename
= 0;
1040 raw_inode
.deleted
= 1;
1042 /* Write the new node to the flash memory. */
1043 if (jffs_write_node(c
, del_node
, &raw_inode
, NULL
, NULL
, 1, del_f
) < 0) {
1044 jffs_free_node(del_node
);
1046 goto jffs_remove_end
;
1049 /* Update the file. This operation will make the file disappear
1050 from the in-memory file system structures. */
1051 jffs_insert_node(c
, del_f
, &raw_inode
, NULL
, del_node
);
1053 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME_SEC
;
1054 mark_inode_dirty(dir
);
1056 inode
->i_ctime
= dir
->i_ctime
;
1057 mark_inode_dirty(inode
);
1059 d_delete(dentry
); /* This also frees the inode */
1064 } /* jffs_remove() */
1068 jffs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
1070 struct jffs_raw_inode raw_inode
;
1071 struct jffs_file
*dir_f
;
1072 struct jffs_node
*node
= NULL
;
1073 struct jffs_control
*c
;
1074 struct inode
*inode
;
1076 u16 data
= old_encode_dev(rdev
);
1079 D1(printk("***jffs_mknod()\n"));
1081 if (!old_valid_dev(rdev
))
1084 dir_f
= dir
->i_private
;
1087 D3(printk (KERN_NOTICE
"mknod(): down biglock\n"));
1088 mutex_lock(&c
->fmc
->biglock
);
1090 /* Create and initialize a new node. */
1091 if (!(node
= jffs_alloc_node())) {
1092 D(printk("jffs_mknod(): Allocation failed!\n"));
1094 goto jffs_mknod_err
;
1096 node
->data_offset
= 0;
1097 node
->removed_size
= 0;
1099 /* Initialize the raw inode. */
1100 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1101 raw_inode
.ino
= c
->next_ino
++;
1102 raw_inode
.pino
= dir_f
->ino
;
1103 raw_inode
.version
= 1;
1104 raw_inode
.mode
= mode
;
1105 raw_inode
.uid
= current
->fsuid
;
1106 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
1107 /* raw_inode.gid = current->fsgid; */
1108 raw_inode
.atime
= get_seconds();
1109 raw_inode
.mtime
= raw_inode
.atime
;
1110 raw_inode
.ctime
= raw_inode
.atime
;
1111 raw_inode
.offset
= 0;
1112 raw_inode
.dsize
= 2;
1113 raw_inode
.rsize
= 0;
1114 raw_inode
.nsize
= dentry
->d_name
.len
;
1115 raw_inode
.nlink
= 1;
1116 raw_inode
.spare
= 0;
1117 raw_inode
.rename
= 0;
1118 raw_inode
.deleted
= 0;
1120 /* Write the new node to the flash. */
1121 if ((err
= jffs_write_node(c
, node
, &raw_inode
, dentry
->d_name
.name
,
1122 (unsigned char *)&data
, 0, NULL
)) < 0) {
1123 D(printk("jffs_mknod(): jffs_write_node() failed.\n"));
1125 goto jffs_mknod_err
;
1128 /* Insert the new node into the file system. */
1129 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
1132 goto jffs_mknod_end
;
1135 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
1136 if (inode
== NULL
) {
1138 goto jffs_mknod_end
;
1141 init_special_inode(inode
, mode
, rdev
);
1143 d_instantiate(dentry
, inode
);
1145 goto jffs_mknod_end
;
1149 jffs_free_node(node
);
1153 D3(printk (KERN_NOTICE
"mknod(): up biglock\n"));
1154 mutex_unlock(&c
->fmc
->biglock
);
1157 } /* jffs_mknod() */
1161 jffs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1163 struct jffs_raw_inode raw_inode
;
1164 struct jffs_control
*c
;
1165 struct jffs_file
*dir_f
;
1166 struct jffs_node
*node
;
1167 struct inode
*inode
;
1169 int symname_len
= strlen(symname
);
1174 int len
= dentry
->d_name
.len
;
1175 char *_name
= kmalloc(len
+ 1, GFP_KERNEL
);
1176 char *_symname
= kmalloc(symname_len
+ 1, GFP_KERNEL
);
1177 memcpy(_name
, dentry
->d_name
.name
, len
);
1179 memcpy(_symname
, symname
, symname_len
);
1180 _symname
[symname_len
] = '\0';
1181 printk("***jffs_symlink(): dir = 0x%p, "
1182 "dentry->dname.name = \"%s\", "
1183 "symname = \"%s\"\n", dir
, _name
, _symname
);
1188 dir_f
= dir
->i_private
;
1189 ASSERT(if (!dir_f
) {
1190 printk(KERN_ERR
"jffs_symlink(): No reference to a "
1191 "jffs_file struct in inode.\n");
1198 /* Create a node and initialize it as much as needed. */
1199 if (!(node
= jffs_alloc_node())) {
1200 D(printk("jffs_symlink(): Allocation failed: node = NULL\n"));
1204 D3(printk (KERN_NOTICE
"symlink(): down biglock\n"));
1205 mutex_lock(&c
->fmc
->biglock
);
1207 node
->data_offset
= 0;
1208 node
->removed_size
= 0;
1210 /* Initialize the raw inode. */
1211 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1212 raw_inode
.ino
= c
->next_ino
++;
1213 raw_inode
.pino
= dir_f
->ino
;
1214 raw_inode
.version
= 1;
1215 raw_inode
.mode
= S_IFLNK
| S_IRWXUGO
;
1216 raw_inode
.uid
= current
->fsuid
;
1217 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
1218 raw_inode
.atime
= get_seconds();
1219 raw_inode
.mtime
= raw_inode
.atime
;
1220 raw_inode
.ctime
= raw_inode
.atime
;
1221 raw_inode
.offset
= 0;
1222 raw_inode
.dsize
= symname_len
;
1223 raw_inode
.rsize
= 0;
1224 raw_inode
.nsize
= dentry
->d_name
.len
;
1225 raw_inode
.nlink
= 1;
1226 raw_inode
.spare
= 0;
1227 raw_inode
.rename
= 0;
1228 raw_inode
.deleted
= 0;
1230 /* Write the new node to the flash. */
1231 if ((err
= jffs_write_node(c
, node
, &raw_inode
, dentry
->d_name
.name
,
1232 (const unsigned char *)symname
, 0, NULL
)) < 0) {
1233 D(printk("jffs_symlink(): jffs_write_node() failed.\n"));
1234 jffs_free_node(node
);
1235 goto jffs_symlink_end
;
1238 /* Insert the new node into the file system. */
1239 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
1241 goto jffs_symlink_end
;
1244 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
1245 if (inode
== NULL
) {
1246 goto jffs_symlink_end
;
1249 inode
->i_op
= &page_symlink_inode_operations
;
1250 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1252 d_instantiate(dentry
, inode
);
1254 D3(printk (KERN_NOTICE
"symlink(): up biglock\n"));
1255 mutex_unlock(&c
->fmc
->biglock
);
1258 } /* jffs_symlink() */
1261 /* Create an inode inside a JFFS directory (dir) and return it.
1263 * By the time this is called, we already have created
1264 * the directory cache entry for the new file, but it
1265 * is so far negative - it has no inode.
1267 * If the create succeeds, we fill in the inode information
1268 * with d_instantiate().
1271 jffs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1272 struct nameidata
*nd
)
1274 struct jffs_raw_inode raw_inode
;
1275 struct jffs_control
*c
;
1276 struct jffs_node
*node
;
1277 struct jffs_file
*dir_f
; /* JFFS representation of the directory. */
1278 struct inode
*inode
;
1283 int len
= dentry
->d_name
.len
;
1284 char *s
= kmalloc(len
+ 1, GFP_KERNEL
);
1285 memcpy(s
, dentry
->d_name
.name
, len
);
1287 printk("jffs_create(): dir: 0x%p, name: \"%s\"\n", dir
, s
);
1291 dir_f
= dir
->i_private
;
1292 ASSERT(if (!dir_f
) {
1293 printk(KERN_ERR
"jffs_create(): No reference to a "
1294 "jffs_file struct in inode.\n");
1301 /* Create a node and initialize as much as needed. */
1302 if (!(node
= jffs_alloc_node())) {
1303 D(printk("jffs_create(): Allocation failed: node == 0\n"));
1307 D3(printk (KERN_NOTICE
"create(): down biglock\n"));
1308 mutex_lock(&c
->fmc
->biglock
);
1310 node
->data_offset
= 0;
1311 node
->removed_size
= 0;
1313 /* Initialize the raw inode. */
1314 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1315 raw_inode
.ino
= c
->next_ino
++;
1316 raw_inode
.pino
= dir_f
->ino
;
1317 raw_inode
.version
= 1;
1318 raw_inode
.mode
= mode
;
1319 raw_inode
.uid
= current
->fsuid
;
1320 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
1321 raw_inode
.atime
= get_seconds();
1322 raw_inode
.mtime
= raw_inode
.atime
;
1323 raw_inode
.ctime
= raw_inode
.atime
;
1324 raw_inode
.offset
= 0;
1325 raw_inode
.dsize
= 0;
1326 raw_inode
.rsize
= 0;
1327 raw_inode
.nsize
= dentry
->d_name
.len
;
1328 raw_inode
.nlink
= 1;
1329 raw_inode
.spare
= 0;
1330 raw_inode
.rename
= 0;
1331 raw_inode
.deleted
= 0;
1333 /* Write the new node to the flash. */
1334 if ((err
= jffs_write_node(c
, node
, &raw_inode
,
1335 dentry
->d_name
.name
, NULL
, 0, NULL
)) < 0) {
1336 D(printk("jffs_create(): jffs_write_node() failed.\n"));
1337 jffs_free_node(node
);
1338 goto jffs_create_end
;
1341 /* Insert the new node into the file system. */
1342 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
1344 goto jffs_create_end
;
1347 /* Initialize an inode. */
1348 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
1349 if (inode
== NULL
) {
1350 goto jffs_create_end
;
1353 inode
->i_op
= &jffs_file_inode_operations
;
1354 inode
->i_fop
= &jffs_file_operations
;
1355 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1356 inode
->i_mapping
->nrpages
= 0;
1358 d_instantiate(dentry
, inode
);
1360 D3(printk (KERN_NOTICE
"create(): up biglock\n"));
1361 mutex_unlock(&c
->fmc
->biglock
);
1364 } /* jffs_create() */
1367 /* Write, append or rewrite data to an existing file. */
1369 jffs_file_write(struct file
*filp
, const char *buf
, size_t count
,
1372 struct jffs_raw_inode raw_inode
;
1373 struct jffs_control
*c
;
1374 struct jffs_file
*f
;
1375 struct jffs_node
*node
;
1376 struct dentry
*dentry
= filp
->f_dentry
;
1377 struct inode
*inode
= dentry
->d_inode
;
1378 int recoverable
= 0;
1380 __u32 thiscount
= count
;
1384 inode
= filp
->f_dentry
->d_inode
;
1386 D2(printk("***jffs_file_write(): inode: 0x%p (ino: %lu), "
1387 "filp: 0x%p, buf: 0x%p, count: %d\n",
1388 inode
, inode
->i_ino
, filp
, buf
, count
));
1391 if (inode
->i_sb
->s_flags
& MS_RDONLY
) {
1392 D(printk("jffs_file_write(): MS_RDONLY\n"));
1399 if (!S_ISREG(inode
->i_mode
)) {
1400 D(printk("jffs_file_write(): inode->i_mode == 0x%08x\n",
1405 if (!(f
= inode
->i_private
)) {
1406 D(printk("jffs_file_write(): inode->i_private = 0x%p\n",
1414 * This will never trigger with sane page sizes. leave it in
1415 * anyway, since I'm thinking about how to merge larger writes
1416 * (the current idea is to poke a thread that does the actual
1417 * I/O and starts by doing a mutex_lock(&inode->i_mutex). then we
1418 * would need to get the page cache pages and have a list of
1419 * I/O requests and do write-merging here.
1422 thiscount
= min(c
->fmc
->max_chunk_size
- sizeof(struct jffs_raw_inode
), count
);
1424 D3(printk (KERN_NOTICE
"file_write(): down biglock\n"));
1425 mutex_lock(&c
->fmc
->biglock
);
1427 /* Urgh. POSIX says we can do short writes if we feel like it.
1428 * In practice, we can't. Nothing will cope. So we loop until
1431 * <_Anarchy_> posix and reality are not interconnected on this issue
1434 /* Things are going to be written so we could allocate and
1435 initialize the necessary data structures now. */
1436 if (!(node
= jffs_alloc_node())) {
1437 D(printk("jffs_file_write(): node == 0\n"));
1442 node
->data_offset
= pos
;
1443 node
->removed_size
= 0;
1445 /* Initialize the raw inode. */
1446 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1447 raw_inode
.ino
= f
->ino
;
1448 raw_inode
.pino
= f
->pino
;
1450 raw_inode
.mode
= f
->mode
;
1452 raw_inode
.uid
= f
->uid
;
1453 raw_inode
.gid
= f
->gid
;
1454 raw_inode
.atime
= get_seconds();
1455 raw_inode
.mtime
= raw_inode
.atime
;
1456 raw_inode
.ctime
= f
->ctime
;
1457 raw_inode
.offset
= pos
;
1458 raw_inode
.dsize
= thiscount
;
1459 raw_inode
.rsize
= 0;
1460 raw_inode
.nsize
= f
->nsize
;
1461 raw_inode
.nlink
= f
->nlink
;
1462 raw_inode
.spare
= 0;
1463 raw_inode
.rename
= 0;
1464 raw_inode
.deleted
= 0;
1466 if (pos
< f
->size
) {
1467 node
->removed_size
= raw_inode
.rsize
= min(thiscount
, (__u32
)(f
->size
- pos
));
1469 /* If this node is going entirely over the top of old data,
1470 we can allow it to go into the reserved space, because
1471 we know that GC can reclaim the space later.
1473 if (pos
+ thiscount
< f
->size
) {
1474 /* If all the data we're overwriting are _real_,
1475 not just holes, then:
1481 /* Write the new node to the flash. */
1482 /* NOTE: We would be quite happy if jffs_write_node() wrote a
1483 smaller node than we were expecting. There's no need for it
1484 to waste the space at the end of the flash just because it's
1485 a little smaller than what we asked for. But that's a whole
1486 new can of worms which I'm not going to open this week.
1489 if ((err
= jffs_write_node(c
, node
, &raw_inode
, f
->name
,
1490 (const unsigned char *)buf
,
1491 recoverable
, f
)) < 0) {
1492 D(printk("jffs_file_write(): jffs_write_node() failed.\n"));
1493 jffs_free_node(node
);
1502 /* Insert the new node into the file system. */
1503 if ((err
= jffs_insert_node(c
, f
, &raw_inode
, NULL
, node
)) < 0) {
1507 D3(printk("jffs_file_write(): new f_pos %ld.\n", (long)pos
));
1509 thiscount
= min(c
->fmc
->max_chunk_size
- sizeof(struct jffs_raw_inode
), count
);
1512 D3(printk (KERN_NOTICE
"file_write(): up biglock\n"));
1513 mutex_unlock(&c
->fmc
->biglock
);
1515 /* Fix things in the real inode. */
1516 if (pos
> inode
->i_size
) {
1517 inode
->i_size
= pos
;
1518 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
1520 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
1521 mark_inode_dirty(inode
);
1522 invalidate_inode_pages(inode
->i_mapping
);
1526 } /* jffs_file_write() */
1529 jffs_prepare_write(struct file
*filp
, struct page
*page
,
1530 unsigned from
, unsigned to
)
1532 /* FIXME: we should detect some error conditions here */
1534 /* Bugger that. We should make sure the page is uptodate */
1535 if (!PageUptodate(page
) && (from
|| to
< PAGE_CACHE_SIZE
))
1536 return jffs_do_readpage_nolock(filp
, page
);
1539 } /* jffs_prepare_write() */
1542 jffs_commit_write(struct file
*filp
, struct page
*page
,
1543 unsigned from
, unsigned to
)
1545 void *addr
= page_address(page
) + from
;
1546 /* XXX: PAGE_CACHE_SHIFT or PAGE_SHIFT */
1547 loff_t pos
= page_offset(page
) + from
;
1549 return jffs_file_write(filp
, addr
, to
-from
, &pos
);
1550 } /* jffs_commit_write() */
1552 /* This is our ioctl() routine. */
1554 jffs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
1557 struct jffs_control
*c
;
1560 D2(printk("***jffs_ioctl(): cmd = 0x%08x, arg = 0x%08lx\n",
1563 if (!(c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
)) {
1564 printk(KERN_ERR
"JFFS: Bad inode in ioctl() call. "
1565 "(cmd = 0x%08x)\n", cmd
);
1568 D3(printk (KERN_NOTICE
"ioctl(): down biglock\n"));
1569 mutex_lock(&c
->fmc
->biglock
);
1572 case JFFS_PRINT_HASH
:
1573 jffs_print_hash_table(c
);
1575 case JFFS_PRINT_TREE
:
1576 jffs_print_tree(c
->root
, 0);
1578 case JFFS_GET_STATUS
:
1580 struct jffs_flash_status fst
;
1581 struct jffs_fmcontrol
*fmc
= c
->fmc
;
1582 printk("Flash status -- ");
1583 if (!access_ok(VERIFY_WRITE
,
1584 (struct jffs_flash_status __user
*)arg
,
1585 sizeof(struct jffs_flash_status
))) {
1586 D(printk("jffs_ioctl(): Bad arg in "
1587 "JFFS_GET_STATUS ioctl!\n"));
1591 fst
.size
= fmc
->flash_size
;
1592 fst
.used
= fmc
->used_size
;
1593 fst
.dirty
= fmc
->dirty_size
;
1594 fst
.begin
= fmc
->head
->offset
;
1595 fst
.end
= fmc
->tail
->offset
+ fmc
->tail
->size
;
1596 printk("size: %d, used: %d, dirty: %d, "
1597 "begin: %d, end: %d\n",
1598 fst
.size
, fst
.used
, fst
.dirty
,
1599 fst
.begin
, fst
.end
);
1600 if (copy_to_user((struct jffs_flash_status __user
*)arg
,
1602 sizeof(struct jffs_flash_status
))) {
1610 D3(printk (KERN_NOTICE
"ioctl(): up biglock\n"));
1611 mutex_unlock(&c
->fmc
->biglock
);
1613 } /* jffs_ioctl() */
1616 static const struct address_space_operations jffs_address_operations
= {
1617 .readpage
= jffs_readpage
,
1618 .prepare_write
= jffs_prepare_write
,
1619 .commit_write
= jffs_commit_write
,
1622 static int jffs_fsync(struct file
*f
, struct dentry
*d
, int datasync
)
1624 /* We currently have O_SYNC operations at all times.
1631 static const struct file_operations jffs_file_operations
=
1633 .open
= generic_file_open
,
1634 .llseek
= generic_file_llseek
,
1635 .read
= generic_file_read
,
1636 .write
= generic_file_write
,
1637 .ioctl
= jffs_ioctl
,
1638 .mmap
= generic_file_readonly_mmap
,
1639 .fsync
= jffs_fsync
,
1640 .sendfile
= generic_file_sendfile
,
1644 static struct inode_operations jffs_file_inode_operations
=
1646 .lookup
= jffs_lookup
, /* lookup */
1647 .setattr
= jffs_setattr
,
1651 static const struct file_operations jffs_dir_operations
=
1653 .readdir
= jffs_readdir
,
1657 static struct inode_operations jffs_dir_inode_operations
=
1659 .create
= jffs_create
,
1660 .lookup
= jffs_lookup
,
1661 .unlink
= jffs_unlink
,
1662 .symlink
= jffs_symlink
,
1663 .mkdir
= jffs_mkdir
,
1664 .rmdir
= jffs_rmdir
,
1665 .mknod
= jffs_mknod
,
1666 .rename
= jffs_rename
,
1667 .setattr
= jffs_setattr
,
1671 /* Initialize an inode for the VFS. */
1673 jffs_read_inode(struct inode
*inode
)
1675 struct jffs_file
*f
;
1676 struct jffs_control
*c
;
1678 D3(printk("jffs_read_inode(): inode->i_ino == %lu\n", inode
->i_ino
));
1681 D(printk("jffs_read_inode(): !inode->i_sb ==> "
1682 "No super block!\n"));
1685 c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
;
1686 D3(printk (KERN_NOTICE
"read_inode(): down biglock\n"));
1687 mutex_lock(&c
->fmc
->biglock
);
1688 if (!(f
= jffs_find_file(c
, inode
->i_ino
))) {
1689 D(printk("jffs_read_inode(): No such inode (%lu).\n",
1691 D3(printk (KERN_NOTICE
"read_inode(): up biglock\n"));
1692 mutex_unlock(&c
->fmc
->biglock
);
1695 inode
->i_private
= f
;
1696 inode
->i_mode
= f
->mode
;
1697 inode
->i_nlink
= f
->nlink
;
1698 inode
->i_uid
= f
->uid
;
1699 inode
->i_gid
= f
->gid
;
1700 inode
->i_size
= f
->size
;
1701 inode
->i_atime
.tv_sec
= f
->atime
;
1702 inode
->i_mtime
.tv_sec
= f
->mtime
;
1703 inode
->i_ctime
.tv_sec
= f
->ctime
;
1704 inode
->i_atime
.tv_nsec
=
1705 inode
->i_mtime
.tv_nsec
=
1706 inode
->i_ctime
.tv_nsec
= 0;
1708 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
1709 if (S_ISREG(inode
->i_mode
)) {
1710 inode
->i_op
= &jffs_file_inode_operations
;
1711 inode
->i_fop
= &jffs_file_operations
;
1712 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1714 else if (S_ISDIR(inode
->i_mode
)) {
1715 inode
->i_op
= &jffs_dir_inode_operations
;
1716 inode
->i_fop
= &jffs_dir_operations
;
1718 else if (S_ISLNK(inode
->i_mode
)) {
1719 inode
->i_op
= &page_symlink_inode_operations
;
1720 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1723 /* If the node is a device of some sort, then the number of
1724 the device should be read from the flash memory and then
1725 added to the inode's i_rdev member. */
1727 jffs_read_data(f
, (char *)&val
, 0, 2);
1728 init_special_inode(inode
, inode
->i_mode
,
1729 old_decode_dev(val
));
1732 D3(printk (KERN_NOTICE
"read_inode(): up biglock\n"));
1733 mutex_unlock(&c
->fmc
->biglock
);
1738 jffs_delete_inode(struct inode
*inode
)
1740 struct jffs_file
*f
;
1741 struct jffs_control
*c
;
1742 D3(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
1745 truncate_inode_pages(&inode
->i_data
, 0);
1748 inode
->i_blocks
= 0;
1749 inode
->i_private
= NULL
;
1751 if (inode
->i_nlink
== 0) {
1752 c
= (struct jffs_control
*) inode
->i_sb
->s_fs_info
;
1753 f
= (struct jffs_file
*) jffs_find_file (c
, inode
->i_ino
);
1754 jffs_possibly_delete_file(f
);
1762 jffs_write_super(struct super_block
*sb
)
1764 struct jffs_control
*c
= (struct jffs_control
*)sb
->s_fs_info
;
1766 jffs_garbage_collect_trigger(c
);
1770 static int jffs_remount(struct super_block
*sb
, int *flags
, char *data
)
1772 *flags
|= MS_NODIRATIME
;
1776 static struct super_operations jffs_ops
=
1778 .read_inode
= jffs_read_inode
,
1779 .delete_inode
= jffs_delete_inode
,
1780 .put_super
= jffs_put_super
,
1781 .write_super
= jffs_write_super
,
1782 .statfs
= jffs_statfs
,
1783 .remount_fs
= jffs_remount
,
1786 static int jffs_get_sb(struct file_system_type
*fs_type
,
1787 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
1789 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, jffs_fill_super
,
1793 static struct file_system_type jffs_fs_type
= {
1794 .owner
= THIS_MODULE
,
1796 .get_sb
= jffs_get_sb
,
1797 .kill_sb
= kill_block_super
,
1798 .fs_flags
= FS_REQUIRES_DEV
,
1804 printk(KERN_INFO
"JFFS version " JFFS_VERSION_STRING
1805 ", (C) 1999, 2000 Axis Communications AB\n");
1807 #ifdef CONFIG_JFFS_PROC_FS
1808 jffs_proc_root
= proc_mkdir("jffs", proc_root_fs
);
1809 if (!jffs_proc_root
) {
1810 printk(KERN_WARNING
"cannot create /proc/jffs entry\n");
1813 fm_cache
= kmem_cache_create("jffs_fm", sizeof(struct jffs_fm
),
1815 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
,
1821 node_cache
= kmem_cache_create("jffs_node",sizeof(struct jffs_node
),
1823 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
,
1826 kmem_cache_destroy(fm_cache
);
1830 return register_filesystem(&jffs_fs_type
);
1836 unregister_filesystem(&jffs_fs_type
);
1837 kmem_cache_destroy(fm_cache
);
1838 kmem_cache_destroy(node_cache
);
1841 module_init(init_jffs_fs
)
1842 module_exit(exit_jffs_fs
)
1844 MODULE_DESCRIPTION("The Journalling Flash File System");
1845 MODULE_AUTHOR("Axis Communications AB.");
1846 MODULE_LICENSE("GPL");