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 struct kmem_cache
*node_cache
= NULL
;
65 struct kmem_cache
*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_path
.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
= 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
= 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
);
1055 inode
->i_ctime
= dir
->i_ctime
;
1056 inode_dec_link_count(inode
);
1058 d_delete(dentry
); /* This also frees the inode */
1063 } /* jffs_remove() */
1067 jffs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
1069 struct jffs_raw_inode raw_inode
;
1070 struct jffs_file
*dir_f
;
1071 struct jffs_node
*node
= NULL
;
1072 struct jffs_control
*c
;
1073 struct inode
*inode
;
1075 u16 data
= old_encode_dev(rdev
);
1078 D1(printk("***jffs_mknod()\n"));
1080 if (!old_valid_dev(rdev
))
1083 dir_f
= dir
->i_private
;
1086 D3(printk (KERN_NOTICE
"mknod(): down biglock\n"));
1087 mutex_lock(&c
->fmc
->biglock
);
1089 /* Create and initialize a new node. */
1090 if (!(node
= jffs_alloc_node())) {
1091 D(printk("jffs_mknod(): Allocation failed!\n"));
1093 goto jffs_mknod_err
;
1095 node
->data_offset
= 0;
1096 node
->removed_size
= 0;
1098 /* Initialize the raw inode. */
1099 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1100 raw_inode
.ino
= c
->next_ino
++;
1101 raw_inode
.pino
= dir_f
->ino
;
1102 raw_inode
.version
= 1;
1103 raw_inode
.mode
= mode
;
1104 raw_inode
.uid
= current
->fsuid
;
1105 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
1106 /* raw_inode.gid = current->fsgid; */
1107 raw_inode
.atime
= get_seconds();
1108 raw_inode
.mtime
= raw_inode
.atime
;
1109 raw_inode
.ctime
= raw_inode
.atime
;
1110 raw_inode
.offset
= 0;
1111 raw_inode
.dsize
= 2;
1112 raw_inode
.rsize
= 0;
1113 raw_inode
.nsize
= dentry
->d_name
.len
;
1114 raw_inode
.nlink
= 1;
1115 raw_inode
.spare
= 0;
1116 raw_inode
.rename
= 0;
1117 raw_inode
.deleted
= 0;
1119 /* Write the new node to the flash. */
1120 if ((err
= jffs_write_node(c
, node
, &raw_inode
, dentry
->d_name
.name
,
1121 (unsigned char *)&data
, 0, NULL
)) < 0) {
1122 D(printk("jffs_mknod(): jffs_write_node() failed.\n"));
1124 goto jffs_mknod_err
;
1127 /* Insert the new node into the file system. */
1128 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
1131 goto jffs_mknod_end
;
1134 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
1135 if (inode
== NULL
) {
1137 goto jffs_mknod_end
;
1140 init_special_inode(inode
, mode
, rdev
);
1142 d_instantiate(dentry
, inode
);
1144 goto jffs_mknod_end
;
1148 jffs_free_node(node
);
1152 D3(printk (KERN_NOTICE
"mknod(): up biglock\n"));
1153 mutex_unlock(&c
->fmc
->biglock
);
1156 } /* jffs_mknod() */
1160 jffs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
1162 struct jffs_raw_inode raw_inode
;
1163 struct jffs_control
*c
;
1164 struct jffs_file
*dir_f
;
1165 struct jffs_node
*node
;
1166 struct inode
*inode
;
1168 int symname_len
= strlen(symname
);
1173 int len
= dentry
->d_name
.len
;
1174 char *_name
= kmalloc(len
+ 1, GFP_KERNEL
);
1175 char *_symname
= kmalloc(symname_len
+ 1, GFP_KERNEL
);
1176 memcpy(_name
, dentry
->d_name
.name
, len
);
1178 memcpy(_symname
, symname
, symname_len
);
1179 _symname
[symname_len
] = '\0';
1180 printk("***jffs_symlink(): dir = 0x%p, "
1181 "dentry->dname.name = \"%s\", "
1182 "symname = \"%s\"\n", dir
, _name
, _symname
);
1187 dir_f
= dir
->i_private
;
1188 ASSERT(if (!dir_f
) {
1189 printk(KERN_ERR
"jffs_symlink(): No reference to a "
1190 "jffs_file struct in inode.\n");
1197 /* Create a node and initialize it as much as needed. */
1198 if (!(node
= jffs_alloc_node())) {
1199 D(printk("jffs_symlink(): Allocation failed: node = NULL\n"));
1203 D3(printk (KERN_NOTICE
"symlink(): down biglock\n"));
1204 mutex_lock(&c
->fmc
->biglock
);
1206 node
->data_offset
= 0;
1207 node
->removed_size
= 0;
1209 /* Initialize the raw inode. */
1210 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1211 raw_inode
.ino
= c
->next_ino
++;
1212 raw_inode
.pino
= dir_f
->ino
;
1213 raw_inode
.version
= 1;
1214 raw_inode
.mode
= S_IFLNK
| S_IRWXUGO
;
1215 raw_inode
.uid
= current
->fsuid
;
1216 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
1217 raw_inode
.atime
= get_seconds();
1218 raw_inode
.mtime
= raw_inode
.atime
;
1219 raw_inode
.ctime
= raw_inode
.atime
;
1220 raw_inode
.offset
= 0;
1221 raw_inode
.dsize
= symname_len
;
1222 raw_inode
.rsize
= 0;
1223 raw_inode
.nsize
= dentry
->d_name
.len
;
1224 raw_inode
.nlink
= 1;
1225 raw_inode
.spare
= 0;
1226 raw_inode
.rename
= 0;
1227 raw_inode
.deleted
= 0;
1229 /* Write the new node to the flash. */
1230 if ((err
= jffs_write_node(c
, node
, &raw_inode
, dentry
->d_name
.name
,
1231 (const unsigned char *)symname
, 0, NULL
)) < 0) {
1232 D(printk("jffs_symlink(): jffs_write_node() failed.\n"));
1233 jffs_free_node(node
);
1234 goto jffs_symlink_end
;
1237 /* Insert the new node into the file system. */
1238 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
1240 goto jffs_symlink_end
;
1243 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
1244 if (inode
== NULL
) {
1245 goto jffs_symlink_end
;
1248 inode
->i_op
= &page_symlink_inode_operations
;
1249 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1251 d_instantiate(dentry
, inode
);
1253 D3(printk (KERN_NOTICE
"symlink(): up biglock\n"));
1254 mutex_unlock(&c
->fmc
->biglock
);
1257 } /* jffs_symlink() */
1260 /* Create an inode inside a JFFS directory (dir) and return it.
1262 * By the time this is called, we already have created
1263 * the directory cache entry for the new file, but it
1264 * is so far negative - it has no inode.
1266 * If the create succeeds, we fill in the inode information
1267 * with d_instantiate().
1270 jffs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1271 struct nameidata
*nd
)
1273 struct jffs_raw_inode raw_inode
;
1274 struct jffs_control
*c
;
1275 struct jffs_node
*node
;
1276 struct jffs_file
*dir_f
; /* JFFS representation of the directory. */
1277 struct inode
*inode
;
1282 int len
= dentry
->d_name
.len
;
1283 char *s
= kmalloc(len
+ 1, GFP_KERNEL
);
1284 memcpy(s
, dentry
->d_name
.name
, len
);
1286 printk("jffs_create(): dir: 0x%p, name: \"%s\"\n", dir
, s
);
1290 dir_f
= dir
->i_private
;
1291 ASSERT(if (!dir_f
) {
1292 printk(KERN_ERR
"jffs_create(): No reference to a "
1293 "jffs_file struct in inode.\n");
1300 /* Create a node and initialize as much as needed. */
1301 if (!(node
= jffs_alloc_node())) {
1302 D(printk("jffs_create(): Allocation failed: node == 0\n"));
1306 D3(printk (KERN_NOTICE
"create(): down biglock\n"));
1307 mutex_lock(&c
->fmc
->biglock
);
1309 node
->data_offset
= 0;
1310 node
->removed_size
= 0;
1312 /* Initialize the raw inode. */
1313 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1314 raw_inode
.ino
= c
->next_ino
++;
1315 raw_inode
.pino
= dir_f
->ino
;
1316 raw_inode
.version
= 1;
1317 raw_inode
.mode
= mode
;
1318 raw_inode
.uid
= current
->fsuid
;
1319 raw_inode
.gid
= (dir
->i_mode
& S_ISGID
) ? dir
->i_gid
: current
->fsgid
;
1320 raw_inode
.atime
= get_seconds();
1321 raw_inode
.mtime
= raw_inode
.atime
;
1322 raw_inode
.ctime
= raw_inode
.atime
;
1323 raw_inode
.offset
= 0;
1324 raw_inode
.dsize
= 0;
1325 raw_inode
.rsize
= 0;
1326 raw_inode
.nsize
= dentry
->d_name
.len
;
1327 raw_inode
.nlink
= 1;
1328 raw_inode
.spare
= 0;
1329 raw_inode
.rename
= 0;
1330 raw_inode
.deleted
= 0;
1332 /* Write the new node to the flash. */
1333 if ((err
= jffs_write_node(c
, node
, &raw_inode
,
1334 dentry
->d_name
.name
, NULL
, 0, NULL
)) < 0) {
1335 D(printk("jffs_create(): jffs_write_node() failed.\n"));
1336 jffs_free_node(node
);
1337 goto jffs_create_end
;
1340 /* Insert the new node into the file system. */
1341 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
, dentry
->d_name
.name
,
1343 goto jffs_create_end
;
1346 /* Initialize an inode. */
1347 inode
= jffs_new_inode(dir
, &raw_inode
, &err
);
1348 if (inode
== NULL
) {
1349 goto jffs_create_end
;
1352 inode
->i_op
= &jffs_file_inode_operations
;
1353 inode
->i_fop
= &jffs_file_operations
;
1354 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1355 inode
->i_mapping
->nrpages
= 0;
1357 d_instantiate(dentry
, inode
);
1359 D3(printk (KERN_NOTICE
"create(): up biglock\n"));
1360 mutex_unlock(&c
->fmc
->biglock
);
1363 } /* jffs_create() */
1366 /* Write, append or rewrite data to an existing file. */
1368 jffs_file_write(struct file
*filp
, const char *buf
, size_t count
,
1371 struct jffs_raw_inode raw_inode
;
1372 struct jffs_control
*c
;
1373 struct jffs_file
*f
;
1374 struct jffs_node
*node
;
1375 struct dentry
*dentry
= filp
->f_path
.dentry
;
1376 struct inode
*inode
= dentry
->d_inode
;
1377 int recoverable
= 0;
1379 __u32 thiscount
= count
;
1383 inode
= filp
->f_path
.dentry
->d_inode
;
1385 D2(printk("***jffs_file_write(): inode: 0x%p (ino: %lu), "
1386 "filp: 0x%p, buf: 0x%p, count: %d\n",
1387 inode
, inode
->i_ino
, filp
, buf
, count
));
1390 if (inode
->i_sb
->s_flags
& MS_RDONLY
) {
1391 D(printk("jffs_file_write(): MS_RDONLY\n"));
1398 if (!S_ISREG(inode
->i_mode
)) {
1399 D(printk("jffs_file_write(): inode->i_mode == 0x%08x\n",
1404 if (!(f
= inode
->i_private
)) {
1405 D(printk("jffs_file_write(): inode->i_private = 0x%p\n",
1413 * This will never trigger with sane page sizes. leave it in
1414 * anyway, since I'm thinking about how to merge larger writes
1415 * (the current idea is to poke a thread that does the actual
1416 * I/O and starts by doing a mutex_lock(&inode->i_mutex). then we
1417 * would need to get the page cache pages and have a list of
1418 * I/O requests and do write-merging here.
1421 thiscount
= min(c
->fmc
->max_chunk_size
- sizeof(struct jffs_raw_inode
), count
);
1423 D3(printk (KERN_NOTICE
"file_write(): down biglock\n"));
1424 mutex_lock(&c
->fmc
->biglock
);
1426 /* Urgh. POSIX says we can do short writes if we feel like it.
1427 * In practice, we can't. Nothing will cope. So we loop until
1430 * <_Anarchy_> posix and reality are not interconnected on this issue
1433 /* Things are going to be written so we could allocate and
1434 initialize the necessary data structures now. */
1435 if (!(node
= jffs_alloc_node())) {
1436 D(printk("jffs_file_write(): node == 0\n"));
1441 node
->data_offset
= pos
;
1442 node
->removed_size
= 0;
1444 /* Initialize the raw inode. */
1445 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1446 raw_inode
.ino
= f
->ino
;
1447 raw_inode
.pino
= f
->pino
;
1449 raw_inode
.mode
= f
->mode
;
1451 raw_inode
.uid
= f
->uid
;
1452 raw_inode
.gid
= f
->gid
;
1453 raw_inode
.atime
= get_seconds();
1454 raw_inode
.mtime
= raw_inode
.atime
;
1455 raw_inode
.ctime
= f
->ctime
;
1456 raw_inode
.offset
= pos
;
1457 raw_inode
.dsize
= thiscount
;
1458 raw_inode
.rsize
= 0;
1459 raw_inode
.nsize
= f
->nsize
;
1460 raw_inode
.nlink
= f
->nlink
;
1461 raw_inode
.spare
= 0;
1462 raw_inode
.rename
= 0;
1463 raw_inode
.deleted
= 0;
1465 if (pos
< f
->size
) {
1466 node
->removed_size
= raw_inode
.rsize
= min(thiscount
, (__u32
)(f
->size
- pos
));
1468 /* If this node is going entirely over the top of old data,
1469 we can allow it to go into the reserved space, because
1470 we know that GC can reclaim the space later.
1472 if (pos
+ thiscount
< f
->size
) {
1473 /* If all the data we're overwriting are _real_,
1474 not just holes, then:
1480 /* Write the new node to the flash. */
1481 /* NOTE: We would be quite happy if jffs_write_node() wrote a
1482 smaller node than we were expecting. There's no need for it
1483 to waste the space at the end of the flash just because it's
1484 a little smaller than what we asked for. But that's a whole
1485 new can of worms which I'm not going to open this week.
1488 if ((err
= jffs_write_node(c
, node
, &raw_inode
, f
->name
,
1489 (const unsigned char *)buf
,
1490 recoverable
, f
)) < 0) {
1491 D(printk("jffs_file_write(): jffs_write_node() failed.\n"));
1492 jffs_free_node(node
);
1501 /* Insert the new node into the file system. */
1502 if ((err
= jffs_insert_node(c
, f
, &raw_inode
, NULL
, node
)) < 0) {
1506 D3(printk("jffs_file_write(): new f_pos %ld.\n", (long)pos
));
1508 thiscount
= min(c
->fmc
->max_chunk_size
- sizeof(struct jffs_raw_inode
), count
);
1511 D3(printk (KERN_NOTICE
"file_write(): up biglock\n"));
1512 mutex_unlock(&c
->fmc
->biglock
);
1514 /* Fix things in the real inode. */
1515 if (pos
> inode
->i_size
) {
1516 inode
->i_size
= pos
;
1517 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
1519 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
1520 mark_inode_dirty(inode
);
1521 invalidate_inode_pages(inode
->i_mapping
);
1525 } /* jffs_file_write() */
1528 jffs_prepare_write(struct file
*filp
, struct page
*page
,
1529 unsigned from
, unsigned to
)
1531 /* FIXME: we should detect some error conditions here */
1533 /* Bugger that. We should make sure the page is uptodate */
1534 if (!PageUptodate(page
) && (from
|| to
< PAGE_CACHE_SIZE
))
1535 return jffs_do_readpage_nolock(filp
, page
);
1538 } /* jffs_prepare_write() */
1541 jffs_commit_write(struct file
*filp
, struct page
*page
,
1542 unsigned from
, unsigned to
)
1544 void *addr
= page_address(page
) + from
;
1545 /* XXX: PAGE_CACHE_SHIFT or PAGE_SHIFT */
1546 loff_t pos
= page_offset(page
) + from
;
1548 return jffs_file_write(filp
, addr
, to
-from
, &pos
);
1549 } /* jffs_commit_write() */
1551 /* This is our ioctl() routine. */
1553 jffs_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
1556 struct jffs_control
*c
;
1559 D2(printk("***jffs_ioctl(): cmd = 0x%08x, arg = 0x%08lx\n",
1562 if (!(c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
)) {
1563 printk(KERN_ERR
"JFFS: Bad inode in ioctl() call. "
1564 "(cmd = 0x%08x)\n", cmd
);
1567 D3(printk (KERN_NOTICE
"ioctl(): down biglock\n"));
1568 mutex_lock(&c
->fmc
->biglock
);
1571 case JFFS_PRINT_HASH
:
1572 jffs_print_hash_table(c
);
1574 case JFFS_PRINT_TREE
:
1575 jffs_print_tree(c
->root
, 0);
1577 case JFFS_GET_STATUS
:
1579 struct jffs_flash_status fst
;
1580 struct jffs_fmcontrol
*fmc
= c
->fmc
;
1581 printk("Flash status -- ");
1582 if (!access_ok(VERIFY_WRITE
,
1583 (struct jffs_flash_status __user
*)arg
,
1584 sizeof(struct jffs_flash_status
))) {
1585 D(printk("jffs_ioctl(): Bad arg in "
1586 "JFFS_GET_STATUS ioctl!\n"));
1590 fst
.size
= fmc
->flash_size
;
1591 fst
.used
= fmc
->used_size
;
1592 fst
.dirty
= fmc
->dirty_size
;
1593 fst
.begin
= fmc
->head
->offset
;
1594 fst
.end
= fmc
->tail
->offset
+ fmc
->tail
->size
;
1595 printk("size: %d, used: %d, dirty: %d, "
1596 "begin: %d, end: %d\n",
1597 fst
.size
, fst
.used
, fst
.dirty
,
1598 fst
.begin
, fst
.end
);
1599 if (copy_to_user((struct jffs_flash_status __user
*)arg
,
1601 sizeof(struct jffs_flash_status
))) {
1609 D3(printk (KERN_NOTICE
"ioctl(): up biglock\n"));
1610 mutex_unlock(&c
->fmc
->biglock
);
1612 } /* jffs_ioctl() */
1615 static const struct address_space_operations jffs_address_operations
= {
1616 .readpage
= jffs_readpage
,
1617 .prepare_write
= jffs_prepare_write
,
1618 .commit_write
= jffs_commit_write
,
1621 static int jffs_fsync(struct file
*f
, struct dentry
*d
, int datasync
)
1623 /* We currently have O_SYNC operations at all times.
1630 static const struct file_operations jffs_file_operations
=
1632 .open
= generic_file_open
,
1633 .llseek
= generic_file_llseek
,
1634 .read
= do_sync_read
,
1635 .aio_read
= generic_file_aio_read
,
1636 .write
= do_sync_write
,
1637 .aio_write
= generic_file_aio_write
,
1638 .ioctl
= jffs_ioctl
,
1639 .mmap
= generic_file_readonly_mmap
,
1640 .fsync
= jffs_fsync
,
1641 .sendfile
= generic_file_sendfile
,
1645 static struct inode_operations jffs_file_inode_operations
=
1647 .lookup
= jffs_lookup
, /* lookup */
1648 .setattr
= jffs_setattr
,
1652 static const struct file_operations jffs_dir_operations
=
1654 .readdir
= jffs_readdir
,
1658 static struct inode_operations jffs_dir_inode_operations
=
1660 .create
= jffs_create
,
1661 .lookup
= jffs_lookup
,
1662 .unlink
= jffs_unlink
,
1663 .symlink
= jffs_symlink
,
1664 .mkdir
= jffs_mkdir
,
1665 .rmdir
= jffs_rmdir
,
1666 .mknod
= jffs_mknod
,
1667 .rename
= jffs_rename
,
1668 .setattr
= jffs_setattr
,
1672 /* Initialize an inode for the VFS. */
1674 jffs_read_inode(struct inode
*inode
)
1676 struct jffs_file
*f
;
1677 struct jffs_control
*c
;
1679 D3(printk("jffs_read_inode(): inode->i_ino == %lu\n", inode
->i_ino
));
1682 D(printk("jffs_read_inode(): !inode->i_sb ==> "
1683 "No super block!\n"));
1686 c
= (struct jffs_control
*)inode
->i_sb
->s_fs_info
;
1687 D3(printk (KERN_NOTICE
"read_inode(): down biglock\n"));
1688 mutex_lock(&c
->fmc
->biglock
);
1689 if (!(f
= jffs_find_file(c
, inode
->i_ino
))) {
1690 D(printk("jffs_read_inode(): No such inode (%lu).\n",
1692 D3(printk (KERN_NOTICE
"read_inode(): up biglock\n"));
1693 mutex_unlock(&c
->fmc
->biglock
);
1696 inode
->i_private
= f
;
1697 inode
->i_mode
= f
->mode
;
1698 inode
->i_nlink
= f
->nlink
;
1699 inode
->i_uid
= f
->uid
;
1700 inode
->i_gid
= f
->gid
;
1701 inode
->i_size
= f
->size
;
1702 inode
->i_atime
.tv_sec
= f
->atime
;
1703 inode
->i_mtime
.tv_sec
= f
->mtime
;
1704 inode
->i_ctime
.tv_sec
= f
->ctime
;
1705 inode
->i_atime
.tv_nsec
=
1706 inode
->i_mtime
.tv_nsec
=
1707 inode
->i_ctime
.tv_nsec
= 0;
1709 inode
->i_blocks
= (inode
->i_size
+ 511) >> 9;
1710 if (S_ISREG(inode
->i_mode
)) {
1711 inode
->i_op
= &jffs_file_inode_operations
;
1712 inode
->i_fop
= &jffs_file_operations
;
1713 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1715 else if (S_ISDIR(inode
->i_mode
)) {
1716 inode
->i_op
= &jffs_dir_inode_operations
;
1717 inode
->i_fop
= &jffs_dir_operations
;
1719 else if (S_ISLNK(inode
->i_mode
)) {
1720 inode
->i_op
= &page_symlink_inode_operations
;
1721 inode
->i_mapping
->a_ops
= &jffs_address_operations
;
1724 /* If the node is a device of some sort, then the number of
1725 the device should be read from the flash memory and then
1726 added to the inode's i_rdev member. */
1728 jffs_read_data(f
, (char *)&val
, 0, 2);
1729 init_special_inode(inode
, inode
->i_mode
,
1730 old_decode_dev(val
));
1733 D3(printk (KERN_NOTICE
"read_inode(): up biglock\n"));
1734 mutex_unlock(&c
->fmc
->biglock
);
1739 jffs_delete_inode(struct inode
*inode
)
1741 struct jffs_file
*f
;
1742 struct jffs_control
*c
;
1743 D3(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
1746 truncate_inode_pages(&inode
->i_data
, 0);
1749 inode
->i_blocks
= 0;
1750 inode
->i_private
= NULL
;
1752 if (inode
->i_nlink
== 0) {
1753 c
= (struct jffs_control
*) inode
->i_sb
->s_fs_info
;
1754 f
= (struct jffs_file
*) jffs_find_file (c
, inode
->i_ino
);
1755 jffs_possibly_delete_file(f
);
1763 jffs_write_super(struct super_block
*sb
)
1765 struct jffs_control
*c
= (struct jffs_control
*)sb
->s_fs_info
;
1767 jffs_garbage_collect_trigger(c
);
1771 static int jffs_remount(struct super_block
*sb
, int *flags
, char *data
)
1773 *flags
|= MS_NODIRATIME
;
1777 static struct super_operations jffs_ops
=
1779 .read_inode
= jffs_read_inode
,
1780 .delete_inode
= jffs_delete_inode
,
1781 .put_super
= jffs_put_super
,
1782 .write_super
= jffs_write_super
,
1783 .statfs
= jffs_statfs
,
1784 .remount_fs
= jffs_remount
,
1787 static int jffs_get_sb(struct file_system_type
*fs_type
,
1788 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
1790 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, jffs_fill_super
,
1794 static struct file_system_type jffs_fs_type
= {
1795 .owner
= THIS_MODULE
,
1797 .get_sb
= jffs_get_sb
,
1798 .kill_sb
= kill_block_super
,
1799 .fs_flags
= FS_REQUIRES_DEV
,
1805 printk(KERN_INFO
"JFFS version " JFFS_VERSION_STRING
1806 ", (C) 1999, 2000 Axis Communications AB\n");
1808 #ifdef CONFIG_JFFS_PROC_FS
1809 jffs_proc_root
= proc_mkdir("jffs", proc_root_fs
);
1810 if (!jffs_proc_root
) {
1811 printk(KERN_WARNING
"cannot create /proc/jffs entry\n");
1814 fm_cache
= kmem_cache_create("jffs_fm", sizeof(struct jffs_fm
),
1816 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
,
1822 node_cache
= kmem_cache_create("jffs_node",sizeof(struct jffs_node
),
1824 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
,
1827 kmem_cache_destroy(fm_cache
);
1831 return register_filesystem(&jffs_fs_type
);
1837 unregister_filesystem(&jffs_fs_type
);
1838 kmem_cache_destroy(fm_cache
);
1839 kmem_cache_destroy(node_cache
);
1842 module_init(init_jffs_fs
)
1843 module_exit(exit_jffs_fs
)
1845 MODULE_DESCRIPTION("The Journalling Flash File System");
1846 MODULE_AUTHOR("Axis Communications AB.");
1847 MODULE_LICENSE("GPL");