aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/9p/fid.c3
-rw-r--r--fs/9p/mux.c5
-rw-r--r--fs/9p/v9fs.c9
-rw-r--r--fs/9p/v9fs.h9
-rw-r--r--fs/9p/v9fs_vfs.h2
-rw-r--r--fs/9p/vfs_addr.c2
-rw-r--r--fs/9p/vfs_dentry.c26
-rw-r--r--fs/9p/vfs_file.c18
-rw-r--r--fs/9p/vfs_inode.c20
-rw-r--r--fs/Kconfig26
-rw-r--r--fs/Makefile1
-rw-r--r--fs/affs/affs.h1
-rw-r--r--fs/affs/inode.c17
-rw-r--r--fs/affs/super.c1
-rw-r--r--fs/autofs4/autofs_i.h4
-rw-r--r--fs/autofs4/inode.c9
-rw-r--r--fs/autofs4/root.c194
-rw-r--r--fs/autofs4/waitq.c12
-rw-r--r--fs/binfmt_elf.c3
-rw-r--r--fs/block_dev.c7
-rw-r--r--fs/buffer.c23
-rw-r--r--fs/char_dev.c8
-rw-r--r--fs/cifs/README2
-rw-r--r--fs/cifs/cifssmb.c4
-rw-r--r--fs/debugfs/file.c12
-rw-r--r--fs/debugfs/inode.c82
-rw-r--r--fs/ecryptfs/ecryptfs_kernel.h1
-rw-r--r--fs/ecryptfs/keystore.c26
-rw-r--r--fs/ecryptfs/main.c5
-rw-r--r--fs/ecryptfs/messaging.c3
-rw-r--r--fs/ext2/balloc.c2
-rw-r--r--fs/ext3/balloc.c2
-rw-r--r--fs/ext4/balloc.c2
-rw-r--r--fs/ext4/extents.c14
-rw-r--r--fs/fat/inode.c4
-rw-r--r--fs/jffs/Makefile11
-rw-r--r--fs/jffs/inode-v23.c1847
-rw-r--r--fs/jffs/intrep.c3449
-rw-r--r--fs/jffs/intrep.h58
-rw-r--r--fs/jffs/jffs_fm.c798
-rw-r--r--fs/jffs/jffs_fm.h149
-rw-r--r--fs/jffs/jffs_proc.c261
-rw-r--r--fs/jffs/jffs_proc.h28
-rw-r--r--fs/jffs2/build.c22
-rw-r--r--fs/jffs2/jffs2_fs_sb.h12
-rw-r--r--fs/jffs2/scan.c10
-rw-r--r--fs/jffs2/wbuf.c203
-rw-r--r--fs/libfs.c5
-rw-r--r--fs/lockd/svc.c2
-rw-r--r--fs/namei.c3
-rw-r--r--fs/nfsd/nfs2acl.c17
-rw-r--r--fs/nfsd/nfs4acl.c491
-rw-r--r--fs/nfsd/nfs4callback.c7
-rw-r--r--fs/nfsd/nfs4xdr.c55
-rw-r--r--fs/nfsd/vfs.c5
-rw-r--r--fs/ocfs2/namei.c2
-rw-r--r--fs/partitions/check.c9
-rw-r--r--fs/pipe.c7
-rw-r--r--fs/proc/base.c2
-rw-r--r--fs/stack.c14
-rw-r--r--fs/sysfs/file.c2
-rw-r--r--fs/xfs/linux-2.6/xfs_super.c2
62 files changed, 829 insertions, 7201 deletions
diff --git a/fs/9p/fid.c b/fs/9p/fid.c
index a9b6301a04fc..90419715c7e9 100644
--- a/fs/9p/fid.c
+++ b/fs/9p/fid.c
@@ -136,7 +136,8 @@ struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry)
136} 136}
137 137
138/** 138/**
139 * v9fs_fid_clone - lookup the fid for a dentry, clone a private copy and release it 139 * v9fs_fid_clone - lookup the fid for a dentry, clone a private copy and
140 * release it
140 * @dentry: dentry to look for fid in 141 * @dentry: dentry to look for fid in
141 * 142 *
142 * find a fid in the dentry and then clone to a new private fid 143 * find a fid in the dentry and then clone to a new private fid
diff --git a/fs/9p/mux.c b/fs/9p/mux.c
index 147ceef8e537..c783874a9caf 100644
--- a/fs/9p/mux.c
+++ b/fs/9p/mux.c
@@ -256,7 +256,7 @@ static void v9fs_mux_poll_stop(struct v9fs_mux_data *m)
256 vpt->muxnum--; 256 vpt->muxnum--;
257 if (!vpt->muxnum) { 257 if (!vpt->muxnum) {
258 dprintk(DEBUG_MUX, "destroy proc %p\n", vpt); 258 dprintk(DEBUG_MUX, "destroy proc %p\n", vpt);
259 send_sig(SIGKILL, vpt->task, 1); 259 kthread_stop(vpt->task);
260 vpt->task = NULL; 260 vpt->task = NULL;
261 v9fs_mux_poll_task_num--; 261 v9fs_mux_poll_task_num--;
262 } 262 }
@@ -438,11 +438,8 @@ static int v9fs_poll_proc(void *a)
438 438
439 vpt = a; 439 vpt = a;
440 dprintk(DEBUG_MUX, "start %p %p\n", current, vpt); 440 dprintk(DEBUG_MUX, "start %p %p\n", current, vpt);
441 allow_signal(SIGKILL);
442 while (!kthread_should_stop()) { 441 while (!kthread_should_stop()) {
443 set_current_state(TASK_INTERRUPTIBLE); 442 set_current_state(TASK_INTERRUPTIBLE);
444 if (signal_pending(current))
445 break;
446 443
447 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) { 444 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
448 v9fs_poll_mux(m); 445 v9fs_poll_mux(m);
diff --git a/fs/9p/v9fs.c b/fs/9p/v9fs.c
index d9b561ba5e58..6ad6f192b6e4 100644
--- a/fs/9p/v9fs.c
+++ b/fs/9p/v9fs.c
@@ -53,6 +53,8 @@ enum {
53 Opt_uname, Opt_remotename, 53 Opt_uname, Opt_remotename,
54 /* Options that take no arguments */ 54 /* Options that take no arguments */
55 Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd, 55 Opt_legacy, Opt_nodevmap, Opt_unix, Opt_tcp, Opt_fd,
56 /* Cache options */
57 Opt_cache_loose,
56 /* Error token */ 58 /* Error token */
57 Opt_err 59 Opt_err
58}; 60};
@@ -76,6 +78,8 @@ static match_table_t tokens = {
76 {Opt_fd, "fd"}, 78 {Opt_fd, "fd"},
77 {Opt_legacy, "noextend"}, 79 {Opt_legacy, "noextend"},
78 {Opt_nodevmap, "nodevmap"}, 80 {Opt_nodevmap, "nodevmap"},
81 {Opt_cache_loose, "cache=loose"},
82 {Opt_cache_loose, "loose"},
79 {Opt_err, NULL} 83 {Opt_err, NULL}
80}; 84};
81 85
@@ -106,6 +110,7 @@ static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
106 v9ses->debug = 0; 110 v9ses->debug = 0;
107 v9ses->rfdno = ~0; 111 v9ses->rfdno = ~0;
108 v9ses->wfdno = ~0; 112 v9ses->wfdno = ~0;
113 v9ses->cache = 0;
109 114
110 if (!options) 115 if (!options)
111 return; 116 return;
@@ -121,7 +126,6 @@ static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
121 "integer field, but no integer?\n"); 126 "integer field, but no integer?\n");
122 continue; 127 continue;
123 } 128 }
124
125 } 129 }
126 switch (token) { 130 switch (token) {
127 case Opt_port: 131 case Opt_port:
@@ -169,6 +173,9 @@ static void v9fs_parse_options(char *options, struct v9fs_session_info *v9ses)
169 case Opt_nodevmap: 173 case Opt_nodevmap:
170 v9ses->nodev = 1; 174 v9ses->nodev = 1;
171 break; 175 break;
176 case Opt_cache_loose:
177 v9ses->cache = CACHE_LOOSE;
178 break;
172 default: 179 default:
173 continue; 180 continue;
174 } 181 }
diff --git a/fs/9p/v9fs.h b/fs/9p/v9fs.h
index c134d104cb28..820bf5ca35d8 100644
--- a/fs/9p/v9fs.h
+++ b/fs/9p/v9fs.h
@@ -47,7 +47,7 @@ struct v9fs_session_info {
47 unsigned int afid; /* authentication fid */ 47 unsigned int afid; /* authentication fid */
48 unsigned int rfdno; /* read file descriptor number */ 48 unsigned int rfdno; /* read file descriptor number */
49 unsigned int wfdno; /* write file descriptor number */ 49 unsigned int wfdno; /* write file descriptor number */
50 50 unsigned int cache; /* cache mode */
51 51
52 char *name; /* user name to mount as */ 52 char *name; /* user name to mount as */
53 char *remotename; /* name of remote hierarchy being mounted */ 53 char *remotename; /* name of remote hierarchy being mounted */
@@ -73,6 +73,13 @@ enum {
73 PROTO_FD, 73 PROTO_FD,
74}; 74};
75 75
76/* possible values of ->cache */
77/* eventually support loose, tight, time, session, default always none */
78enum {
79 CACHE_NONE, /* default */
80 CACHE_LOOSE, /* no consistency */
81};
82
76extern struct dentry *v9fs_debugfs_root; 83extern struct dentry *v9fs_debugfs_root;
77 84
78int v9fs_session_init(struct v9fs_session_info *, const char *, char *); 85int v9fs_session_init(struct v9fs_session_info *, const char *, char *);
diff --git a/fs/9p/v9fs_vfs.h b/fs/9p/v9fs_vfs.h
index 450b0c1b385e..8ada4c5c5d70 100644
--- a/fs/9p/v9fs_vfs.h
+++ b/fs/9p/v9fs_vfs.h
@@ -40,8 +40,10 @@
40extern struct file_system_type v9fs_fs_type; 40extern struct file_system_type v9fs_fs_type;
41extern const struct address_space_operations v9fs_addr_operations; 41extern const struct address_space_operations v9fs_addr_operations;
42extern const struct file_operations v9fs_file_operations; 42extern const struct file_operations v9fs_file_operations;
43extern const struct file_operations v9fs_cached_file_operations;
43extern const struct file_operations v9fs_dir_operations; 44extern const struct file_operations v9fs_dir_operations;
44extern struct dentry_operations v9fs_dentry_operations; 45extern struct dentry_operations v9fs_dentry_operations;
46extern struct dentry_operations v9fs_cached_dentry_operations;
45 47
46struct inode *v9fs_get_inode(struct super_block *sb, int mode); 48struct inode *v9fs_get_inode(struct super_block *sb, int mode);
47ino_t v9fs_qid2ino(struct v9fs_qid *qid); 49ino_t v9fs_qid2ino(struct v9fs_qid *qid);
diff --git a/fs/9p/vfs_addr.c b/fs/9p/vfs_addr.c
index cc24abf232d5..bed48fa96521 100644
--- a/fs/9p/vfs_addr.c
+++ b/fs/9p/vfs_addr.c
@@ -63,6 +63,8 @@ static int v9fs_vfs_readpage(struct file *filp, struct page *page)
63 int total = 0; 63 int total = 0;
64 int result = 0; 64 int result = 0;
65 65
66 dprintk(DEBUG_VFS, "\n");
67
66 buffer = kmap(page); 68 buffer = kmap(page);
67 do { 69 do {
68 if (count < rsize) 70 if (count < rsize)
diff --git a/fs/9p/vfs_dentry.c b/fs/9p/vfs_dentry.c
index 062daa6000ab..ddffd8aa902d 100644
--- a/fs/9p/vfs_dentry.c
+++ b/fs/9p/vfs_dentry.c
@@ -53,10 +53,31 @@
53static int v9fs_dentry_delete(struct dentry *dentry) 53static int v9fs_dentry_delete(struct dentry *dentry)
54{ 54{
55 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry); 55 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
56
56 return 1; 57 return 1;
57} 58}
58 59
59/** 60/**
61 * v9fs_cached_dentry_delete - called when dentry refcount equals 0
62 * @dentry: dentry in question
63 *
64 * Only return 1 if our inode is invalid. Only non-synthetic files
65 * (ones without mtime == 0) should be calling this function.
66 *
67 */
68
69static int v9fs_cached_dentry_delete(struct dentry *dentry)
70{
71 struct inode *inode = dentry->d_inode;
72 dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
73
74 if(!inode)
75 return 1;
76
77 return 0;
78}
79
80/**
60 * v9fs_dentry_release - called when dentry is going to be freed 81 * v9fs_dentry_release - called when dentry is going to be freed
61 * @dentry: dentry that is being release 82 * @dentry: dentry that is being release
62 * 83 *
@@ -87,6 +108,11 @@ void v9fs_dentry_release(struct dentry *dentry)
87 } 108 }
88} 109}
89 110
111struct dentry_operations v9fs_cached_dentry_operations = {
112 .d_delete = v9fs_cached_dentry_delete,
113 .d_release = v9fs_dentry_release,
114};
115
90struct dentry_operations v9fs_dentry_operations = { 116struct dentry_operations v9fs_dentry_operations = {
91 .d_delete = v9fs_dentry_delete, 117 .d_delete = v9fs_dentry_delete,
92 .d_release = v9fs_dentry_release, 118 .d_release = v9fs_dentry_release,
diff --git a/fs/9p/vfs_file.c b/fs/9p/vfs_file.c
index 6c78343cf690..653dfa5b2531 100644
--- a/fs/9p/vfs_file.c
+++ b/fs/9p/vfs_file.c
@@ -79,6 +79,13 @@ int v9fs_file_open(struct inode *inode, struct file *file)
79 vfid->filp = file; 79 vfid->filp = file;
80 kfree(fcall); 80 kfree(fcall);
81 81
82 if((vfid->qid.version) && (v9ses->cache)) {
83 dprintk(DEBUG_VFS, "cached");
84 /* enable cached file options */
85 if(file->f_op == &v9fs_file_operations)
86 file->f_op = &v9fs_cached_file_operations;
87 }
88
82 return 0; 89 return 0;
83 90
84Clunk_Fid: 91Clunk_Fid:
@@ -238,6 +245,17 @@ v9fs_file_write(struct file *filp, const char __user * data,
238 return total; 245 return total;
239} 246}
240 247
248const struct file_operations v9fs_cached_file_operations = {
249 .llseek = generic_file_llseek,
250 .read = do_sync_read,
251 .aio_read = generic_file_aio_read,
252 .write = v9fs_file_write,
253 .open = v9fs_file_open,
254 .release = v9fs_dir_release,
255 .lock = v9fs_file_lock,
256 .mmap = generic_file_mmap,
257};
258
241const struct file_operations v9fs_file_operations = { 259const struct file_operations v9fs_file_operations = {
242 .llseek = generic_file_llseek, 260 .llseek = generic_file_llseek,
243 .read = v9fs_file_read, 261 .read = v9fs_file_read,
diff --git a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
index 5cf22134826b..124a085d1f2e 100644
--- a/fs/9p/vfs_inode.c
+++ b/fs/9p/vfs_inode.c
@@ -504,7 +504,10 @@ v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
504 goto error; 504 goto error;
505 } 505 }
506 506
507 dentry->d_op = &v9fs_dentry_operations; 507 if(v9ses->cache)
508 dentry->d_op = &v9fs_cached_dentry_operations;
509 else
510 dentry->d_op = &v9fs_dentry_operations;
508 d_instantiate(dentry, inode); 511 d_instantiate(dentry, inode);
509 512
510 if (nd && nd->flags & LOOKUP_OPEN) { 513 if (nd && nd->flags & LOOKUP_OPEN) {
@@ -589,7 +592,10 @@ static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
589 goto error; 592 goto error;
590 } 593 }
591 594
592 dentry->d_op = &v9fs_dentry_operations; 595 if(v9ses->cache)
596 dentry->d_op = &v9fs_cached_dentry_operations;
597 else
598 dentry->d_op = &v9fs_dentry_operations;
593 d_instantiate(dentry, inode); 599 d_instantiate(dentry, inode);
594 return 0; 600 return 0;
595 601
@@ -626,7 +632,6 @@ static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
626 632
627 sb = dir->i_sb; 633 sb = dir->i_sb;
628 v9ses = v9fs_inode2v9ses(dir); 634 v9ses = v9fs_inode2v9ses(dir);
629 dentry->d_op = &v9fs_dentry_operations;
630 dirfid = v9fs_fid_lookup(dentry->d_parent); 635 dirfid = v9fs_fid_lookup(dentry->d_parent);
631 636
632 if(IS_ERR(dirfid)) 637 if(IS_ERR(dirfid))
@@ -697,6 +702,10 @@ static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
697 702
698 fid->qid = fcall->params.rstat.stat.qid; 703 fid->qid = fcall->params.rstat.stat.qid;
699 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb); 704 v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb);
705 if((fid->qid.version)&&(v9ses->cache))
706 dentry->d_op = &v9fs_cached_dentry_operations;
707 else
708 dentry->d_op = &v9fs_dentry_operations;
700 709
701 d_add(dentry, inode); 710 d_add(dentry, inode);
702 kfree(fcall); 711 kfree(fcall);
@@ -1184,7 +1193,10 @@ static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1184 goto free_vfid; 1193 goto free_vfid;
1185 } 1194 }
1186 1195
1187 dentry->d_op = &v9fs_dentry_operations; 1196 if(v9ses->cache)
1197 dentry->d_op = &v9fs_cached_dentry_operations;
1198 else
1199 dentry->d_op = &v9fs_dentry_operations;
1188 d_instantiate(dentry, inode); 1200 d_instantiate(dentry, inode);
1189 return 0; 1201 return 0;
1190 1202
diff --git a/fs/Kconfig b/fs/Kconfig
index a722b5a3f752..3c4886b849f5 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -1189,32 +1189,6 @@ config EFS_FS
1189 To compile the EFS file system support as a module, choose M here: the 1189 To compile the EFS file system support as a module, choose M here: the
1190 module will be called efs. 1190 module will be called efs.
1191 1191
1192config JFFS_FS
1193 tristate "Journalling Flash File System (JFFS) support"
1194 depends on MTD && BLOCK && BROKEN
1195 help
1196 JFFS is the Journalling Flash File System developed by Axis
1197 Communications in Sweden, aimed at providing a crash/powerdown-safe
1198 file system for disk-less embedded devices. Further information is
1199 available at (<http://developer.axis.com/software/jffs/>).
1200
1201 NOTE: This filesystem is deprecated and is scheduled for removal in
1202 2.6.21. See Documentation/feature-removal-schedule.txt
1203
1204config JFFS_FS_VERBOSE
1205 int "JFFS debugging verbosity (0 = quiet, 3 = noisy)"
1206 depends on JFFS_FS
1207 default "0"
1208 help
1209 Determines the verbosity level of the JFFS debugging messages.
1210
1211config JFFS_PROC_FS
1212 bool "JFFS stats available in /proc filesystem"
1213 depends on JFFS_FS && PROC_FS
1214 help
1215 Enabling this option will cause statistics from mounted JFFS file systems
1216 to be made available to the user in the /proc/fs/jffs/ directory.
1217
1218config JFFS2_FS 1192config JFFS2_FS
1219 tristate "Journalling Flash File System v2 (JFFS2) support" 1193 tristate "Journalling Flash File System v2 (JFFS2) support"
1220 select CRC32 1194 select CRC32
diff --git a/fs/Makefile b/fs/Makefile
index b9ffa63f77fc..9edf4112bee0 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -94,7 +94,6 @@ obj-$(CONFIG_HPFS_FS) += hpfs/
94obj-$(CONFIG_NTFS_FS) += ntfs/ 94obj-$(CONFIG_NTFS_FS) += ntfs/
95obj-$(CONFIG_UFS_FS) += ufs/ 95obj-$(CONFIG_UFS_FS) += ufs/
96obj-$(CONFIG_EFS_FS) += efs/ 96obj-$(CONFIG_EFS_FS) += efs/
97obj-$(CONFIG_JFFS_FS) += jffs/
98obj-$(CONFIG_JFFS2_FS) += jffs2/ 97obj-$(CONFIG_JFFS2_FS) += jffs2/
99obj-$(CONFIG_AFFS_FS) += affs/ 98obj-$(CONFIG_AFFS_FS) += affs/
100obj-$(CONFIG_ROMFS_FS) += romfs/ 99obj-$(CONFIG_ROMFS_FS) += romfs/
diff --git a/fs/affs/affs.h b/fs/affs/affs.h
index 7db2d287e9f3..232c69493683 100644
--- a/fs/affs/affs.h
+++ b/fs/affs/affs.h
@@ -171,6 +171,7 @@ extern unsigned long affs_parent_ino(struct inode *dir);
171extern struct inode *affs_new_inode(struct inode *dir); 171extern struct inode *affs_new_inode(struct inode *dir);
172extern int affs_notify_change(struct dentry *dentry, struct iattr *attr); 172extern int affs_notify_change(struct dentry *dentry, struct iattr *attr);
173extern void affs_put_inode(struct inode *inode); 173extern void affs_put_inode(struct inode *inode);
174extern void affs_drop_inode(struct inode *inode);
174extern void affs_delete_inode(struct inode *inode); 175extern void affs_delete_inode(struct inode *inode);
175extern void affs_clear_inode(struct inode *inode); 176extern void affs_clear_inode(struct inode *inode);
176extern void affs_read_inode(struct inode *inode); 177extern void affs_read_inode(struct inode *inode);
diff --git a/fs/affs/inode.c b/fs/affs/inode.c
index fce6848a4641..c5b9d73c084a 100644
--- a/fs/affs/inode.c
+++ b/fs/affs/inode.c
@@ -243,12 +243,17 @@ affs_put_inode(struct inode *inode)
243{ 243{
244 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink); 244 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
245 affs_free_prealloc(inode); 245 affs_free_prealloc(inode);
246 if (atomic_read(&inode->i_count) == 1) { 246}
247 mutex_lock(&inode->i_mutex); 247
248 if (inode->i_size != AFFS_I(inode)->mmu_private) 248void
249 affs_truncate(inode); 249affs_drop_inode(struct inode *inode)
250 mutex_unlock(&inode->i_mutex); 250{
251 } 251 mutex_lock(&inode->i_mutex);
252 if (inode->i_size != AFFS_I(inode)->mmu_private)
253 affs_truncate(inode);
254 mutex_unlock(&inode->i_mutex);
255
256 generic_drop_inode(inode);
252} 257}
253 258
254void 259void
diff --git a/fs/affs/super.c b/fs/affs/super.c
index a324045d8554..c3986a1911b0 100644
--- a/fs/affs/super.c
+++ b/fs/affs/super.c
@@ -118,6 +118,7 @@ static const struct super_operations affs_sops = {
118 .read_inode = affs_read_inode, 118 .read_inode = affs_read_inode,
119 .write_inode = affs_write_inode, 119 .write_inode = affs_write_inode,
120 .put_inode = affs_put_inode, 120 .put_inode = affs_put_inode,
121 .drop_inode = affs_drop_inode,
121 .delete_inode = affs_delete_inode, 122 .delete_inode = affs_delete_inode,
122 .clear_inode = affs_clear_inode, 123 .clear_inode = affs_clear_inode,
123 .put_super = affs_put_super, 124 .put_super = affs_put_super,
diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
index 6b4cec3f272f..d85f42fa9206 100644
--- a/fs/autofs4/autofs_i.h
+++ b/fs/autofs4/autofs_i.h
@@ -52,6 +52,8 @@ struct autofs_info {
52 52
53 int flags; 53 int flags;
54 54
55 struct list_head rehash;
56
55 struct autofs_sb_info *sbi; 57 struct autofs_sb_info *sbi;
56 unsigned long last_used; 58 unsigned long last_used;
57 atomic_t count; 59 atomic_t count;
@@ -110,6 +112,8 @@ struct autofs_sb_info {
110 struct mutex wq_mutex; 112 struct mutex wq_mutex;
111 spinlock_t fs_lock; 113 spinlock_t fs_lock;
112 struct autofs_wait_queue *queues; /* Wait queue pointer */ 114 struct autofs_wait_queue *queues; /* Wait queue pointer */
115 spinlock_t rehash_lock;
116 struct list_head rehash_list;
113}; 117};
114 118
115static inline struct autofs_sb_info *autofs4_sbi(struct super_block *sb) 119static inline struct autofs_sb_info *autofs4_sbi(struct super_block *sb)
diff --git a/fs/autofs4/inode.c b/fs/autofs4/inode.c
index 5e458e096ef6..26063dc84a2a 100644
--- a/fs/autofs4/inode.c
+++ b/fs/autofs4/inode.c
@@ -48,6 +48,8 @@ struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
48 ino->dentry = NULL; 48 ino->dentry = NULL;
49 ino->size = 0; 49 ino->size = 0;
50 50
51 INIT_LIST_HEAD(&ino->rehash);
52
51 ino->last_used = jiffies; 53 ino->last_used = jiffies;
52 atomic_set(&ino->count, 0); 54 atomic_set(&ino->count, 0);
53 55
@@ -158,14 +160,13 @@ void autofs4_kill_sb(struct super_block *sb)
158 if (!sbi) 160 if (!sbi)
159 goto out_kill_sb; 161 goto out_kill_sb;
160 162
161 sb->s_fs_info = NULL; 163 if (!sbi->catatonic)
162
163 if ( !sbi->catatonic )
164 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */ 164 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
165 165
166 /* Clean up and release dangling references */ 166 /* Clean up and release dangling references */
167 autofs4_force_release(sbi); 167 autofs4_force_release(sbi);
168 168
169 sb->s_fs_info = NULL;
169 kfree(sbi); 170 kfree(sbi);
170 171
171out_kill_sb: 172out_kill_sb:
@@ -336,6 +337,8 @@ int autofs4_fill_super(struct super_block *s, void *data, int silent)
336 mutex_init(&sbi->wq_mutex); 337 mutex_init(&sbi->wq_mutex);
337 spin_lock_init(&sbi->fs_lock); 338 spin_lock_init(&sbi->fs_lock);
338 sbi->queues = NULL; 339 sbi->queues = NULL;
340 spin_lock_init(&sbi->rehash_lock);
341 INIT_LIST_HEAD(&sbi->rehash_list);
339 s->s_blocksize = 1024; 342 s->s_blocksize = 1024;
340 s->s_blocksize_bits = 10; 343 s->s_blocksize_bits = 10;
341 s->s_magic = AUTOFS_SUPER_MAGIC; 344 s->s_magic = AUTOFS_SUPER_MAGIC;
diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c
index 47fee96c2182..b4631046867e 100644
--- a/fs/autofs4/root.c
+++ b/fs/autofs4/root.c
@@ -263,7 +263,7 @@ static int try_to_fill_dentry(struct dentry *dentry, int flags)
263 */ 263 */
264 status = d_invalidate(dentry); 264 status = d_invalidate(dentry);
265 if (status != -EBUSY) 265 if (status != -EBUSY)
266 return -ENOENT; 266 return -EAGAIN;
267 } 267 }
268 268
269 DPRINTK("dentry=%p %.*s ino=%p", 269 DPRINTK("dentry=%p %.*s ino=%p",
@@ -413,7 +413,16 @@ static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
413 */ 413 */
414 status = try_to_fill_dentry(dentry, flags); 414 status = try_to_fill_dentry(dentry, flags);
415 if (status == 0) 415 if (status == 0)
416 return 1; 416 return 1;
417
418 /*
419 * A status of EAGAIN here means that the dentry has gone
420 * away while waiting for an expire to complete. If we are
421 * racing with expire lookup will wait for it so this must
422 * be a revalidate and we need to send it to lookup.
423 */
424 if (status == -EAGAIN)
425 return 0;
417 426
418 return status; 427 return status;
419 } 428 }
@@ -459,9 +468,18 @@ void autofs4_dentry_release(struct dentry *de)
459 de->d_fsdata = NULL; 468 de->d_fsdata = NULL;
460 469
461 if (inf) { 470 if (inf) {
471 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
472
462 inf->dentry = NULL; 473 inf->dentry = NULL;
463 inf->inode = NULL; 474 inf->inode = NULL;
464 475
476 if (sbi) {
477 spin_lock(&sbi->rehash_lock);
478 if (!list_empty(&inf->rehash))
479 list_del(&inf->rehash);
480 spin_unlock(&sbi->rehash_lock);
481 }
482
465 autofs4_free_ino(inf); 483 autofs4_free_ino(inf);
466 } 484 }
467} 485}
@@ -478,10 +496,80 @@ static struct dentry_operations autofs4_dentry_operations = {
478 .d_release = autofs4_dentry_release, 496 .d_release = autofs4_dentry_release,
479}; 497};
480 498
499static struct dentry *autofs4_lookup_unhashed(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
500{
501 unsigned int len = name->len;
502 unsigned int hash = name->hash;
503 const unsigned char *str = name->name;
504 struct list_head *p, *head;
505
506 spin_lock(&dcache_lock);
507 spin_lock(&sbi->rehash_lock);
508 head = &sbi->rehash_list;
509 list_for_each(p, head) {
510 struct autofs_info *ino;
511 struct dentry *dentry;
512 struct qstr *qstr;
513
514 ino = list_entry(p, struct autofs_info, rehash);
515 dentry = ino->dentry;
516
517 spin_lock(&dentry->d_lock);
518
519 /* Bad luck, we've already been dentry_iput */
520 if (!dentry->d_inode)
521 goto next;
522
523 qstr = &dentry->d_name;
524
525 if (dentry->d_name.hash != hash)
526 goto next;
527 if (dentry->d_parent != parent)
528 goto next;
529
530 if (qstr->len != len)
531 goto next;
532 if (memcmp(qstr->name, str, len))
533 goto next;
534
535 if (d_unhashed(dentry)) {
536 struct autofs_info *ino = autofs4_dentry_ino(dentry);
537 struct inode *inode = dentry->d_inode;
538
539 list_del_init(&ino->rehash);
540 dget(dentry);
541 /*
542 * Make the rehashed dentry negative so the VFS
543 * behaves as it should.
544 */
545 if (inode) {
546 dentry->d_inode = NULL;
547 list_del_init(&dentry->d_alias);
548 spin_unlock(&dentry->d_lock);
549 spin_unlock(&sbi->rehash_lock);
550 spin_unlock(&dcache_lock);
551 iput(inode);
552 return dentry;
553 }
554 spin_unlock(&dentry->d_lock);
555 spin_unlock(&sbi->rehash_lock);
556 spin_unlock(&dcache_lock);
557 return dentry;
558 }
559next:
560 spin_unlock(&dentry->d_lock);
561 }
562 spin_unlock(&sbi->rehash_lock);
563 spin_unlock(&dcache_lock);
564
565 return NULL;
566}
567
481/* Lookups in the root directory */ 568/* Lookups in the root directory */
482static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 569static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
483{ 570{
484 struct autofs_sb_info *sbi; 571 struct autofs_sb_info *sbi;
572 struct dentry *unhashed;
485 int oz_mode; 573 int oz_mode;
486 574
487 DPRINTK("name = %.*s", 575 DPRINTK("name = %.*s",
@@ -497,25 +585,46 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
497 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", 585 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
498 current->pid, process_group(current), sbi->catatonic, oz_mode); 586 current->pid, process_group(current), sbi->catatonic, oz_mode);
499 587
500 /* 588 unhashed = autofs4_lookup_unhashed(sbi, dentry->d_parent, &dentry->d_name);
501 * Mark the dentry incomplete, but add it. This is needed so 589 if (!unhashed) {
502 * that the VFS layer knows about the dentry, and we can count 590 /*
503 * on catching any lookups through the revalidate. 591 * Mark the dentry incomplete, but add it. This is needed so
504 * 592 * that the VFS layer knows about the dentry, and we can count
505 * Let all the hard work be done by the revalidate function that 593 * on catching any lookups through the revalidate.
506 * needs to be able to do this anyway.. 594 *
507 * 595 * Let all the hard work be done by the revalidate function that
508 * We need to do this before we release the directory semaphore. 596 * needs to be able to do this anyway..
509 */ 597 *
510 dentry->d_op = &autofs4_root_dentry_operations; 598 * We need to do this before we release the directory semaphore.
599 */
600 dentry->d_op = &autofs4_root_dentry_operations;
601
602 dentry->d_fsdata = NULL;
603 d_add(dentry, NULL);
604 } else {
605 struct autofs_info *ino = autofs4_dentry_ino(unhashed);
606 DPRINTK("rehash %p with %p", dentry, unhashed);
607 /*
608 * If we are racing with expire the request might not
609 * be quite complete but the directory has been removed
610 * so it must have been successful, so just wait for it.
611 */
612 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
613 DPRINTK("wait for incomplete expire %p name=%.*s",
614 unhashed, unhashed->d_name.len,
615 unhashed->d_name.name);
616 autofs4_wait(sbi, unhashed, NFY_NONE);
617 DPRINTK("request completed");
618 }
619 d_rehash(unhashed);
620 dentry = unhashed;
621 }
511 622
512 if (!oz_mode) { 623 if (!oz_mode) {
513 spin_lock(&dentry->d_lock); 624 spin_lock(&dentry->d_lock);
514 dentry->d_flags |= DCACHE_AUTOFS_PENDING; 625 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
515 spin_unlock(&dentry->d_lock); 626 spin_unlock(&dentry->d_lock);
516 } 627 }
517 dentry->d_fsdata = NULL;
518 d_add(dentry, NULL);
519 628
520 if (dentry->d_op && dentry->d_op->d_revalidate) { 629 if (dentry->d_op && dentry->d_op->d_revalidate) {
521 mutex_unlock(&dir->i_mutex); 630 mutex_unlock(&dir->i_mutex);
@@ -534,6 +643,8 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
534 if (sigismember (sigset, SIGKILL) || 643 if (sigismember (sigset, SIGKILL) ||
535 sigismember (sigset, SIGQUIT) || 644 sigismember (sigset, SIGQUIT) ||
536 sigismember (sigset, SIGINT)) { 645 sigismember (sigset, SIGINT)) {
646 if (unhashed)
647 dput(unhashed);
537 return ERR_PTR(-ERESTARTNOINTR); 648 return ERR_PTR(-ERESTARTNOINTR);
538 } 649 }
539 } 650 }
@@ -544,12 +655,33 @@ static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, s
544 655
545 /* 656 /*
546 * If this dentry is unhashed, then we shouldn't honour this 657 * If this dentry is unhashed, then we shouldn't honour this
547 * lookup even if the dentry is positive. Returning ENOENT here 658 * lookup. Returning ENOENT here doesn't do the right thing
548 * doesn't do the right thing for all system calls, but it should 659 * for all system calls, but it should be OK for the operations
549 * be OK for the operations we permit from an autofs. 660 * we permit from an autofs.
550 */ 661 */
551 if (dentry->d_inode && d_unhashed(dentry)) 662 if (dentry->d_inode && d_unhashed(dentry)) {
552 return ERR_PTR(-ENOENT); 663 /*
664 * A user space application can (and has done in the past)
665 * remove and re-create this directory during the callback.
666 * This can leave us with an unhashed dentry, but a
667 * successful mount! So we need to perform another
668 * cached lookup in case the dentry now exists.
669 */
670 struct dentry *parent = dentry->d_parent;
671 struct dentry *new = d_lookup(parent, &dentry->d_name);
672 if (new != NULL)
673 dentry = new;
674 else
675 dentry = ERR_PTR(-ENOENT);
676
677 if (unhashed)
678 dput(unhashed);
679
680 return dentry;
681 }
682
683 if (unhashed)
684 return dentry;
553 685
554 return NULL; 686 return NULL;
555} 687}
@@ -611,9 +743,10 @@ static int autofs4_dir_symlink(struct inode *dir,
611 * Normal filesystems would do a "d_delete()" to tell the VFS dcache 743 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
612 * that the file no longer exists. However, doing that means that the 744 * that the file no longer exists. However, doing that means that the
613 * VFS layer can turn the dentry into a negative dentry. We don't want 745 * VFS layer can turn the dentry into a negative dentry. We don't want
614 * this, because since the unlink is probably the result of an expire. 746 * this, because the unlink is probably the result of an expire.
615 * We simply d_drop it, which allows the dentry lookup to remount it 747 * We simply d_drop it and add it to a rehash candidates list in the
616 * if necessary. 748 * super block, which allows the dentry lookup to reuse it retaining
749 * the flags, such as expire in progress, in case we're racing with expire.
617 * 750 *
618 * If a process is blocked on the dentry waiting for the expire to finish, 751 * If a process is blocked on the dentry waiting for the expire to finish,
619 * it will invalidate the dentry and try to mount with a new one. 752 * it will invalidate the dentry and try to mount with a new one.
@@ -642,7 +775,14 @@ static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
642 775
643 dir->i_mtime = CURRENT_TIME; 776 dir->i_mtime = CURRENT_TIME;
644 777
645 d_drop(dentry); 778 spin_lock(&dcache_lock);
779 spin_lock(&sbi->rehash_lock);
780 list_add(&ino->rehash, &sbi->rehash_list);
781 spin_unlock(&sbi->rehash_lock);
782 spin_lock(&dentry->d_lock);
783 __d_drop(dentry);
784 spin_unlock(&dentry->d_lock);
785 spin_unlock(&dcache_lock);
646 786
647 return 0; 787 return 0;
648} 788}
@@ -653,6 +793,9 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
653 struct autofs_info *ino = autofs4_dentry_ino(dentry); 793 struct autofs_info *ino = autofs4_dentry_ino(dentry);
654 struct autofs_info *p_ino; 794 struct autofs_info *p_ino;
655 795
796 DPRINTK("dentry %p, removing %.*s",
797 dentry, dentry->d_name.len, dentry->d_name.name);
798
656 if (!autofs4_oz_mode(sbi)) 799 if (!autofs4_oz_mode(sbi))
657 return -EACCES; 800 return -EACCES;
658 801
@@ -661,6 +804,9 @@ static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
661 spin_unlock(&dcache_lock); 804 spin_unlock(&dcache_lock);
662 return -ENOTEMPTY; 805 return -ENOTEMPTY;
663 } 806 }
807 spin_lock(&sbi->rehash_lock);
808 list_add(&ino->rehash, &sbi->rehash_list);
809 spin_unlock(&sbi->rehash_lock);
664 spin_lock(&dentry->d_lock); 810 spin_lock(&dentry->d_lock);
665 __d_drop(dentry); 811 __d_drop(dentry);
666 spin_unlock(&dentry->d_lock); 812 spin_unlock(&dentry->d_lock);
diff --git a/fs/autofs4/waitq.c b/fs/autofs4/waitq.c
index 1e4a539f4417..0d041a9cb348 100644
--- a/fs/autofs4/waitq.c
+++ b/fs/autofs4/waitq.c
@@ -84,7 +84,11 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
84 struct autofs_wait_queue *wq, 84 struct autofs_wait_queue *wq,
85 int type) 85 int type)
86{ 86{
87 union autofs_packet_union pkt; 87 union {
88 struct autofs_packet_hdr hdr;
89 union autofs_packet_union v4_pkt;
90 union autofs_v5_packet_union v5_pkt;
91 } pkt;
88 size_t pktsz; 92 size_t pktsz;
89 93
90 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d", 94 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
@@ -98,7 +102,7 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
98 /* Kernel protocol v4 missing and expire packets */ 102 /* Kernel protocol v4 missing and expire packets */
99 case autofs_ptype_missing: 103 case autofs_ptype_missing:
100 { 104 {
101 struct autofs_packet_missing *mp = &pkt.missing; 105 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
102 106
103 pktsz = sizeof(*mp); 107 pktsz = sizeof(*mp);
104 108
@@ -110,7 +114,7 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
110 } 114 }
111 case autofs_ptype_expire_multi: 115 case autofs_ptype_expire_multi:
112 { 116 {
113 struct autofs_packet_expire_multi *ep = &pkt.expire_multi; 117 struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
114 118
115 pktsz = sizeof(*ep); 119 pktsz = sizeof(*ep);
116 120
@@ -129,7 +133,7 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
129 case autofs_ptype_missing_direct: 133 case autofs_ptype_missing_direct:
130 case autofs_ptype_expire_direct: 134 case autofs_ptype_expire_direct:
131 { 135 {
132 struct autofs_v5_packet *packet = &pkt.v5_packet; 136 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
133 137
134 pktsz = sizeof(*packet); 138 pktsz = sizeof(*packet);
135 139
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 669dbe5b0317..51db1182b27e 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -76,7 +76,8 @@ static struct linux_binfmt elf_format = {
76 .load_binary = load_elf_binary, 76 .load_binary = load_elf_binary,
77 .load_shlib = load_elf_library, 77 .load_shlib = load_elf_library,
78 .core_dump = elf_core_dump, 78 .core_dump = elf_core_dump,
79 .min_coredump = ELF_EXEC_PAGESIZE 79 .min_coredump = ELF_EXEC_PAGESIZE,
80 .hasvdso = 1
80}; 81};
81 82
82#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE) 83#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 0c59b703e9d5..575076c018f4 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -1101,6 +1101,13 @@ static int __blkdev_get(struct block_device *bdev, mode_t mode, unsigned flags,
1101 int for_part); 1101 int for_part);
1102static int __blkdev_put(struct block_device *bdev, int for_part); 1102static int __blkdev_put(struct block_device *bdev, int for_part);
1103 1103
1104/*
1105 * bd_mutex locking:
1106 *
1107 * mutex_lock(part->bd_mutex)
1108 * mutex_lock_nested(whole->bd_mutex, 1)
1109 */
1110
1104static int do_open(struct block_device *bdev, struct file *file, int for_part) 1111static int do_open(struct block_device *bdev, struct file *file, int for_part)
1105{ 1112{
1106 struct module *owner = NULL; 1113 struct module *owner = NULL;
diff --git a/fs/buffer.c b/fs/buffer.c
index f99c509697cd..e8504b65176c 100644
--- a/fs/buffer.c
+++ b/fs/buffer.c
@@ -1743,7 +1743,6 @@ recover:
1743 SetPageError(page); 1743 SetPageError(page);
1744 BUG_ON(PageWriteback(page)); 1744 BUG_ON(PageWriteback(page));
1745 set_page_writeback(page); 1745 set_page_writeback(page);
1746 unlock_page(page);
1747 do { 1746 do {
1748 struct buffer_head *next = bh->b_this_page; 1747 struct buffer_head *next = bh->b_this_page;
1749 if (buffer_async_write(bh)) { 1748 if (buffer_async_write(bh)) {
@@ -1753,6 +1752,7 @@ recover:
1753 } 1752 }
1754 bh = next; 1753 bh = next;
1755 } while (bh != head); 1754 } while (bh != head);
1755 unlock_page(page);
1756 goto done; 1756 goto done;
1757} 1757}
1758 1758
@@ -2248,7 +2248,6 @@ int nobh_prepare_write(struct page *page, unsigned from, unsigned to,
2248 int i; 2248 int i;
2249 int ret = 0; 2249 int ret = 0;
2250 int is_mapped_to_disk = 1; 2250 int is_mapped_to_disk = 1;
2251 int dirtied_it = 0;
2252 2251
2253 if (PageMappedToDisk(page)) 2252 if (PageMappedToDisk(page))
2254 return 0; 2253 return 0;
@@ -2285,14 +2284,10 @@ int nobh_prepare_write(struct page *page, unsigned from, unsigned to,
2285 continue; 2284 continue;
2286 if (buffer_new(&map_bh) || !buffer_mapped(&map_bh)) { 2285 if (buffer_new(&map_bh) || !buffer_mapped(&map_bh)) {
2287 kaddr = kmap_atomic(page, KM_USER0); 2286 kaddr = kmap_atomic(page, KM_USER0);
2288 if (block_start < from) { 2287 if (block_start < from)
2289 memset(kaddr+block_start, 0, from-block_start); 2288 memset(kaddr+block_start, 0, from-block_start);
2290 dirtied_it = 1; 2289 if (block_end > to)
2291 }
2292 if (block_end > to) {
2293 memset(kaddr + to, 0, block_end - to); 2290 memset(kaddr + to, 0, block_end - to);
2294 dirtied_it = 1;
2295 }
2296 flush_dcache_page(page); 2291 flush_dcache_page(page);
2297 kunmap_atomic(kaddr, KM_USER0); 2292 kunmap_atomic(kaddr, KM_USER0);
2298 continue; 2293 continue;
@@ -2347,17 +2342,6 @@ int nobh_prepare_write(struct page *page, unsigned from, unsigned to,
2347 2342
2348 if (is_mapped_to_disk) 2343 if (is_mapped_to_disk)
2349 SetPageMappedToDisk(page); 2344 SetPageMappedToDisk(page);
2350 SetPageUptodate(page);
2351
2352 /*
2353 * Setting the page dirty here isn't necessary for the prepare_write
2354 * function - commit_write will do that. But if/when this function is
2355 * used within the pagefault handler to ensure that all mmapped pages
2356 * have backing space in the filesystem, we will need to dirty the page
2357 * if its contents were altered.
2358 */
2359 if (dirtied_it)
2360 set_page_dirty(page);
2361 2345
2362 return 0; 2346 return 0;
2363 2347
@@ -2387,6 +2371,7 @@ int nobh_commit_write(struct file *file, struct page *page,
2387 struct inode *inode = page->mapping->host; 2371 struct inode *inode = page->mapping->host;
2388 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; 2372 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2389 2373
2374 SetPageUptodate(page);
2390 set_page_dirty(page); 2375 set_page_dirty(page);
2391 if (pos > inode->i_size) { 2376 if (pos > inode->i_size) {
2392 i_size_write(inode, pos); 2377 i_size_write(inode, pos);
diff --git a/fs/char_dev.c b/fs/char_dev.c
index e6194e2b9bb9..78ced721554d 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -6,6 +6,7 @@
6 6
7#include <linux/init.h> 7#include <linux/init.h>
8#include <linux/fs.h> 8#include <linux/fs.h>
9#include <linux/kdev_t.h>
9#include <linux/slab.h> 10#include <linux/slab.h>
10#include <linux/string.h> 11#include <linux/string.h>
11 12
@@ -108,12 +109,7 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
108 /* temporary */ 109 /* temporary */
109 if (major == 0) { 110 if (major == 0) {
110 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) { 111 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
111 /* 112 if (is_lanana_major(i))
112 * Disallow the LANANA-assigned LOCAL/EXPERIMENTAL
113 * majors
114 */
115 if ((60 <= i && i <= 63) || (120 <= i && i <= 127) ||
116 (240 <= i && i <= 254))
117 continue; 113 continue;
118 if (chrdevs[i] == NULL) 114 if (chrdevs[i] == NULL)
119 break; 115 break;
diff --git a/fs/cifs/README b/fs/cifs/README
index 432e515431c4..080c5eba112b 100644
--- a/fs/cifs/README
+++ b/fs/cifs/README
@@ -1,5 +1,5 @@
1The CIFS VFS support for Linux supports many advanced network filesystem 1The CIFS VFS support for Linux supports many advanced network filesystem
2features such as heirarchical dfs like namespace, hardlinks, locking and more. 2features such as hierarchical dfs like namespace, hardlinks, locking and more.
3It was designed to comply with the SNIA CIFS Technical Reference (which 3It was designed to comply with the SNIA CIFS Technical Reference (which
4supersedes the 1992 X/Open SMB Standard) as well as to perform best practice 4supersedes the 1992 X/Open SMB Standard) as well as to perform best practice
5practical interoperability with Windows 2000, Windows XP, Samba and equivalent 5practical interoperability with Windows 2000, Windows XP, Samba and equivalent
diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c
index b8e91470c27f..24364106b8f9 100644
--- a/fs/cifs/cifssmb.c
+++ b/fs/cifs/cifssmb.c
@@ -2824,10 +2824,10 @@ GetExtAttrOut:
2824 2824
2825 2825
2826/* security id for everyone */ 2826/* security id for everyone */
2827const static struct cifs_sid sid_everyone = 2827static const struct cifs_sid sid_everyone =
2828 {1, 1, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0}}; 2828 {1, 1, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0}};
2829/* group users */ 2829/* group users */
2830const static struct cifs_sid sid_user = 2830static const struct cifs_sid sid_user =
2831 {1, 2 , {0, 0, 0, 0, 0, 5}, {32, 545, 0, 0}}; 2831 {1, 2 , {0, 0, 0, 0, 0, 5}, {32, 545, 0, 0}};
2832 2832
2833/* Convert CIFS ACL to POSIX form */ 2833/* Convert CIFS ACL to POSIX form */
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 8d130cc85322..682f928b7f4d 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -16,6 +16,7 @@
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/fs.h> 17#include <linux/fs.h>
18#include <linux/pagemap.h> 18#include <linux/pagemap.h>
19#include <linux/namei.h>
19#include <linux/debugfs.h> 20#include <linux/debugfs.h>
20 21
21static ssize_t default_read_file(struct file *file, char __user *buf, 22static ssize_t default_read_file(struct file *file, char __user *buf,
@@ -44,6 +45,17 @@ const struct file_operations debugfs_file_operations = {
44 .open = default_open, 45 .open = default_open,
45}; 46};
46 47
48static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
49{
50 nd_set_link(nd, dentry->d_inode->i_private);
51 return NULL;
52}
53
54const struct inode_operations debugfs_link_operations = {
55 .readlink = generic_readlink,
56 .follow_link = debugfs_follow_link,
57};
58
47static void debugfs_u8_set(void *data, u64 val) 59static void debugfs_u8_set(void *data, u64 val)
48{ 60{
49 *(u8 *)data = val; 61 *(u8 *)data = val;
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
index c692487346ea..7b324cfebcb1 100644
--- a/fs/debugfs/inode.c
+++ b/fs/debugfs/inode.c
@@ -25,11 +25,13 @@
25#include <linux/namei.h> 25#include <linux/namei.h>
26#include <linux/debugfs.h> 26#include <linux/debugfs.h>
27#include <linux/fsnotify.h> 27#include <linux/fsnotify.h>
28#include <linux/string.h>
28 29
29#define DEBUGFS_MAGIC 0x64626720 30#define DEBUGFS_MAGIC 0x64626720
30 31
31/* declared over in file.c */ 32/* declared over in file.c */
32extern struct file_operations debugfs_file_operations; 33extern struct file_operations debugfs_file_operations;
34extern struct inode_operations debugfs_link_operations;
33 35
34static struct vfsmount *debugfs_mount; 36static struct vfsmount *debugfs_mount;
35static int debugfs_mount_count; 37static int debugfs_mount_count;
@@ -51,6 +53,9 @@ static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t d
51 case S_IFREG: 53 case S_IFREG:
52 inode->i_fop = &debugfs_file_operations; 54 inode->i_fop = &debugfs_file_operations;
53 break; 55 break;
56 case S_IFLNK:
57 inode->i_op = &debugfs_link_operations;
58 break;
54 case S_IFDIR: 59 case S_IFDIR:
55 inode->i_op = &simple_dir_inode_operations; 60 inode->i_op = &simple_dir_inode_operations;
56 inode->i_fop = &simple_dir_operations; 61 inode->i_fop = &simple_dir_operations;
@@ -96,6 +101,12 @@ static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
96 return res; 101 return res;
97} 102}
98 103
104static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode)
105{
106 mode = (mode & S_IALLUGO) | S_IFLNK;
107 return debugfs_mknod(dir, dentry, mode, 0);
108}
109
99static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode) 110static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode)
100{ 111{
101 int res; 112 int res;
@@ -158,10 +169,17 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
158 mutex_lock(&parent->d_inode->i_mutex); 169 mutex_lock(&parent->d_inode->i_mutex);
159 *dentry = lookup_one_len(name, parent, strlen(name)); 170 *dentry = lookup_one_len(name, parent, strlen(name));
160 if (!IS_ERR(*dentry)) { 171 if (!IS_ERR(*dentry)) {
161 if ((mode & S_IFMT) == S_IFDIR) 172 switch (mode & S_IFMT) {
173 case S_IFDIR:
162 error = debugfs_mkdir(parent->d_inode, *dentry, mode); 174 error = debugfs_mkdir(parent->d_inode, *dentry, mode);
163 else 175 break;
176 case S_IFLNK:
177 error = debugfs_link(parent->d_inode, *dentry, mode);
178 break;
179 default:
164 error = debugfs_create(parent->d_inode, *dentry, mode); 180 error = debugfs_create(parent->d_inode, *dentry, mode);
181 break;
182 }
165 dput(*dentry); 183 dput(*dentry);
166 } else 184 } else
167 error = PTR_ERR(*dentry); 185 error = PTR_ERR(*dentry);
@@ -194,9 +212,7 @@ static int debugfs_create_by_name(const char *name, mode_t mode,
194 * you are responsible here.) If an error occurs, %NULL will be returned. 212 * you are responsible here.) If an error occurs, %NULL will be returned.
195 * 213 *
196 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 214 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
197 * returned. It is not wise to check for this value, but rather, check for 215 * returned.
198 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
199 * code.
200 */ 216 */
201struct dentry *debugfs_create_file(const char *name, mode_t mode, 217struct dentry *debugfs_create_file(const char *name, mode_t mode,
202 struct dentry *parent, void *data, 218 struct dentry *parent, void *data,
@@ -246,9 +262,7 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);
246 * you are responsible here.) If an error occurs, %NULL will be returned. 262 * you are responsible here.) If an error occurs, %NULL will be returned.
247 * 263 *
248 * If debugfs is not enabled in the kernel, the value -%ENODEV will be 264 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
249 * returned. It is not wise to check for this value, but rather, check for 265 * returned.
250 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
251 * code.
252 */ 266 */
253struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) 267struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
254{ 268{
@@ -259,6 +273,47 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
259EXPORT_SYMBOL_GPL(debugfs_create_dir); 273EXPORT_SYMBOL_GPL(debugfs_create_dir);
260 274
261/** 275/**
276 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
277 * @name: a pointer to a string containing the name of the symbolic link to
278 * create.
279 * @parent: a pointer to the parent dentry for this symbolic link. This
280 * should be a directory dentry if set. If this paramater is NULL,
281 * then the symbolic link will be created in the root of the debugfs
282 * filesystem.
283 * @target: a pointer to a string containing the path to the target of the
284 * symbolic link.
285 *
286 * This function creates a symbolic link with the given name in debugfs that
287 * links to the given target path.
288 *
289 * This function will return a pointer to a dentry if it succeeds. This
290 * pointer must be passed to the debugfs_remove() function when the symbolic
291 * link is to be removed (no automatic cleanup happens if your module is
292 * unloaded, you are responsible here.) If an error occurs, %NULL will be
293 * returned.
294 *
295 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
296 * returned.
297 */
298struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
299 const char *target)
300{
301 struct dentry *result;
302 char *link;
303
304 link = kstrdup(target, GFP_KERNEL);
305 if (!link)
306 return NULL;
307
308 result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
309 NULL);
310 if (!result)
311 kfree(link);
312 return result;
313}
314EXPORT_SYMBOL_GPL(debugfs_create_symlink);
315
316/**
262 * debugfs_remove - removes a file or directory from the debugfs filesystem 317 * debugfs_remove - removes a file or directory from the debugfs filesystem
263 * @dentry: a pointer to a the dentry of the file or directory to be 318 * @dentry: a pointer to a the dentry of the file or directory to be
264 * removed. 319 * removed.
@@ -287,15 +342,22 @@ void debugfs_remove(struct dentry *dentry)
287 if (debugfs_positive(dentry)) { 342 if (debugfs_positive(dentry)) {
288 if (dentry->d_inode) { 343 if (dentry->d_inode) {
289 dget(dentry); 344 dget(dentry);
290 if (S_ISDIR(dentry->d_inode->i_mode)) { 345 switch (dentry->d_inode->i_mode & S_IFMT) {
346 case S_IFDIR:
291 ret = simple_rmdir(parent->d_inode, dentry); 347 ret = simple_rmdir(parent->d_inode, dentry);
292 if (ret) 348 if (ret)
293 printk(KERN_ERR 349 printk(KERN_ERR
294 "DebugFS rmdir on %s failed : " 350 "DebugFS rmdir on %s failed : "
295 "directory not empty.\n", 351 "directory not empty.\n",
296 dentry->d_name.name); 352 dentry->d_name.name);
297 } else 353 break;
354 case S_IFLNK:
355 kfree(dentry->d_inode->i_private);
356 /* fall through */
357 default:
298 simple_unlink(parent->d_inode, dentry); 358 simple_unlink(parent->d_inode, dentry);
359 break;
360 }
299 if (!ret) 361 if (!ret)
300 d_delete(dentry); 362 d_delete(dentry);
301 dput(dentry); 363 dput(dentry);
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
index b3609b7cdf11..403e3bad1455 100644
--- a/fs/ecryptfs/ecryptfs_kernel.h
+++ b/fs/ecryptfs/ecryptfs_kernel.h
@@ -467,6 +467,7 @@ extern struct kmem_cache *ecryptfs_header_cache_1;
467extern struct kmem_cache *ecryptfs_header_cache_2; 467extern struct kmem_cache *ecryptfs_header_cache_2;
468extern struct kmem_cache *ecryptfs_xattr_cache; 468extern struct kmem_cache *ecryptfs_xattr_cache;
469extern struct kmem_cache *ecryptfs_lower_page_cache; 469extern struct kmem_cache *ecryptfs_lower_page_cache;
470extern struct kmem_cache *ecryptfs_key_record_cache;
470 471
471int ecryptfs_interpose(struct dentry *hidden_dentry, 472int ecryptfs_interpose(struct dentry *hidden_dentry,
472 struct dentry *this_dentry, struct super_block *sb, 473 struct dentry *this_dentry, struct super_block *sb,
diff --git a/fs/ecryptfs/keystore.c b/fs/ecryptfs/keystore.c
index 81156e95ef8e..b550dea8eee6 100644
--- a/fs/ecryptfs/keystore.c
+++ b/fs/ecryptfs/keystore.c
@@ -1638,6 +1638,8 @@ out:
1638 return rc; 1638 return rc;
1639} 1639}
1640 1640
1641struct kmem_cache *ecryptfs_key_record_cache;
1642
1641/** 1643/**
1642 * ecryptfs_generate_key_packet_set 1644 * ecryptfs_generate_key_packet_set
1643 * @dest: Virtual address from which to write the key record set 1645 * @dest: Virtual address from which to write the key record set
@@ -1664,50 +1666,55 @@ ecryptfs_generate_key_packet_set(char *dest_base,
1664 &ecryptfs_superblock_to_private( 1666 &ecryptfs_superblock_to_private(
1665 ecryptfs_dentry->d_sb)->mount_crypt_stat; 1667 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1666 size_t written; 1668 size_t written;
1667 struct ecryptfs_key_record key_rec; 1669 struct ecryptfs_key_record *key_rec;
1668 int rc = 0; 1670 int rc = 0;
1669 1671
1670 (*len) = 0; 1672 (*len) = 0;
1673 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1674 if (!key_rec) {
1675 rc = -ENOMEM;
1676 goto out;
1677 }
1671 if (mount_crypt_stat->global_auth_tok) { 1678 if (mount_crypt_stat->global_auth_tok) {
1672 auth_tok = mount_crypt_stat->global_auth_tok; 1679 auth_tok = mount_crypt_stat->global_auth_tok;
1673 if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 1680 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1674 rc = write_tag_3_packet((dest_base + (*len)), 1681 rc = write_tag_3_packet((dest_base + (*len)),
1675 max, auth_tok, 1682 max, auth_tok,
1676 crypt_stat, &key_rec, 1683 crypt_stat, key_rec,
1677 &written); 1684 &written);
1678 if (rc) { 1685 if (rc) {
1679 ecryptfs_printk(KERN_WARNING, "Error " 1686 ecryptfs_printk(KERN_WARNING, "Error "
1680 "writing tag 3 packet\n"); 1687 "writing tag 3 packet\n");
1681 goto out; 1688 goto out_free;
1682 } 1689 }
1683 (*len) += written; 1690 (*len) += written;
1684 /* Write auth tok signature packet */ 1691 /* Write auth tok signature packet */
1685 rc = write_tag_11_packet( 1692 rc = write_tag_11_packet(
1686 (dest_base + (*len)), 1693 (dest_base + (*len)),
1687 (max - (*len)), 1694 (max - (*len)),
1688 key_rec.sig, ECRYPTFS_SIG_SIZE, &written); 1695 key_rec->sig, ECRYPTFS_SIG_SIZE, &written);
1689 if (rc) { 1696 if (rc) {
1690 ecryptfs_printk(KERN_ERR, "Error writing " 1697 ecryptfs_printk(KERN_ERR, "Error writing "
1691 "auth tok signature packet\n"); 1698 "auth tok signature packet\n");
1692 goto out; 1699 goto out_free;
1693 } 1700 }
1694 (*len) += written; 1701 (*len) += written;
1695 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1702 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1696 rc = write_tag_1_packet(dest_base + (*len), 1703 rc = write_tag_1_packet(dest_base + (*len),
1697 max, auth_tok, 1704 max, auth_tok,
1698 crypt_stat,mount_crypt_stat, 1705 crypt_stat,mount_crypt_stat,
1699 &key_rec, &written); 1706 key_rec, &written);
1700 if (rc) { 1707 if (rc) {
1701 ecryptfs_printk(KERN_WARNING, "Error " 1708 ecryptfs_printk(KERN_WARNING, "Error "
1702 "writing tag 1 packet\n"); 1709 "writing tag 1 packet\n");
1703 goto out; 1710 goto out_free;
1704 } 1711 }
1705 (*len) += written; 1712 (*len) += written;
1706 } else { 1713 } else {
1707 ecryptfs_printk(KERN_WARNING, "Unsupported " 1714 ecryptfs_printk(KERN_WARNING, "Unsupported "
1708 "authentication token type\n"); 1715 "authentication token type\n");
1709 rc = -EINVAL; 1716 rc = -EINVAL;
1710 goto out; 1717 goto out_free;
1711 } 1718 }
1712 } else 1719 } else
1713 BUG(); 1720 BUG();
@@ -1717,6 +1724,9 @@ ecryptfs_generate_key_packet_set(char *dest_base,
1717 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 1724 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1718 rc = -EIO; 1725 rc = -EIO;
1719 } 1726 }
1727
1728out_free:
1729 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
1720out: 1730out:
1721 if (rc) 1731 if (rc)
1722 (*len) = 0; 1732 (*len) = 0;
diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
index 26fe405a5763..80044d196fe0 100644
--- a/fs/ecryptfs/main.c
+++ b/fs/ecryptfs/main.c
@@ -651,6 +651,11 @@ static struct ecryptfs_cache_info {
651 .name = "ecryptfs_lower_page_cache", 651 .name = "ecryptfs_lower_page_cache",
652 .size = PAGE_CACHE_SIZE, 652 .size = PAGE_CACHE_SIZE,
653 }, 653 },
654 {
655 .cache = &ecryptfs_key_record_cache,
656 .name = "ecryptfs_key_record_cache",
657 .size = sizeof(struct ecryptfs_key_record),
658 },
654}; 659};
655 660
656static void ecryptfs_free_kmem_caches(void) 661static void ecryptfs_free_kmem_caches(void)
diff --git a/fs/ecryptfs/messaging.c b/fs/ecryptfs/messaging.c
index 47d7e7b611f7..3baf253be95a 100644
--- a/fs/ecryptfs/messaging.c
+++ b/fs/ecryptfs/messaging.c
@@ -169,7 +169,8 @@ int ecryptfs_process_helo(unsigned int transport, uid_t uid, pid_t pid)
169 if (!new_id) { 169 if (!new_id) {
170 rc = -ENOMEM; 170 rc = -ENOMEM;
171 ecryptfs_printk(KERN_ERR, "Failed to allocate memory; unable " 171 ecryptfs_printk(KERN_ERR, "Failed to allocate memory; unable "
172 "to register daemon [%d] for user\n", pid, uid); 172 "to register daemon [%d] for user [%d]\n",
173 pid, uid);
173 goto unlock; 174 goto unlock;
174 } 175 }
175 if (!ecryptfs_find_daemon_id(uid, &old_id)) { 176 if (!ecryptfs_find_daemon_id(uid, &old_id)) {
diff --git a/fs/ext2/balloc.c b/fs/ext2/balloc.c
index b1981d0e95ad..baf71dd721fa 100644
--- a/fs/ext2/balloc.c
+++ b/fs/ext2/balloc.c
@@ -29,7 +29,7 @@
29 * The file system contains group descriptors which are located after the 29 * The file system contains group descriptors which are located after the
30 * super block. Each descriptor contains the number of the bitmap block and 30 * super block. Each descriptor contains the number of the bitmap block and
31 * the free blocks count in the block. The descriptors are loaded in memory 31 * the free blocks count in the block. The descriptors are loaded in memory
32 * when a file system is mounted (see ext2_read_super). 32 * when a file system is mounted (see ext2_fill_super).
33 */ 33 */
34 34
35 35
diff --git a/fs/ext3/balloc.c b/fs/ext3/balloc.c
index 22161740ba29..ca8aee6efe37 100644
--- a/fs/ext3/balloc.c
+++ b/fs/ext3/balloc.c
@@ -32,7 +32,7 @@
32 * The file system contains group descriptors which are located after the 32 * The file system contains group descriptors which are located after the
33 * super block. Each descriptor contains the number of the bitmap block and 33 * super block. Each descriptor contains the number of the bitmap block and
34 * the free blocks count in the block. The descriptors are loaded in memory 34 * the free blocks count in the block. The descriptors are loaded in memory
35 * when a file system is mounted (see ext3_read_super). 35 * when a file system is mounted (see ext3_fill_super).
36 */ 36 */
37 37
38 38
diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
index c4dd1103ccf1..8a23483ca8d0 100644
--- a/fs/ext4/balloc.c
+++ b/fs/ext4/balloc.c
@@ -50,7 +50,7 @@ void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
50 * The file system contains group descriptors which are located after the 50 * The file system contains group descriptors which are located after the
51 * super block. Each descriptor contains the number of the bitmap block and 51 * super block. Each descriptor contains the number of the bitmap block and
52 * the free blocks count in the block. The descriptors are loaded in memory 52 * the free blocks count in the block. The descriptors are loaded in memory
53 * when a file system is mounted (see ext4_read_super). 53 * when a file system is mounted (see ext4_fill_super).
54 */ 54 */
55 55
56 56
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index dc2724fa7622..7916b50f9a13 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -222,7 +222,7 @@ static int ext4_ext_space_block(struct inode *inode)
222 222
223 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 223 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
224 / sizeof(struct ext4_extent); 224 / sizeof(struct ext4_extent);
225#ifdef AGRESSIVE_TEST 225#ifdef AGGRESSIVE_TEST
226 if (size > 6) 226 if (size > 6)
227 size = 6; 227 size = 6;
228#endif 228#endif
@@ -235,7 +235,7 @@ static int ext4_ext_space_block_idx(struct inode *inode)
235 235
236 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 236 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
237 / sizeof(struct ext4_extent_idx); 237 / sizeof(struct ext4_extent_idx);
238#ifdef AGRESSIVE_TEST 238#ifdef AGGRESSIVE_TEST
239 if (size > 5) 239 if (size > 5)
240 size = 5; 240 size = 5;
241#endif 241#endif
@@ -249,7 +249,7 @@ static int ext4_ext_space_root(struct inode *inode)
249 size = sizeof(EXT4_I(inode)->i_data); 249 size = sizeof(EXT4_I(inode)->i_data);
250 size -= sizeof(struct ext4_extent_header); 250 size -= sizeof(struct ext4_extent_header);
251 size /= sizeof(struct ext4_extent); 251 size /= sizeof(struct ext4_extent);
252#ifdef AGRESSIVE_TEST 252#ifdef AGGRESSIVE_TEST
253 if (size > 3) 253 if (size > 3)
254 size = 3; 254 size = 3;
255#endif 255#endif
@@ -263,7 +263,7 @@ static int ext4_ext_space_root_idx(struct inode *inode)
263 size = sizeof(EXT4_I(inode)->i_data); 263 size = sizeof(EXT4_I(inode)->i_data);
264 size -= sizeof(struct ext4_extent_header); 264 size -= sizeof(struct ext4_extent_header);
265 size /= sizeof(struct ext4_extent_idx); 265 size /= sizeof(struct ext4_extent_idx);
266#ifdef AGRESSIVE_TEST 266#ifdef AGGRESSIVE_TEST
267 if (size > 4) 267 if (size > 4)
268 size = 4; 268 size = 4;
269#endif 269#endif
@@ -1118,7 +1118,7 @@ ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1118 */ 1118 */
1119 if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN) 1119 if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN)
1120 return 0; 1120 return 0;
1121#ifdef AGRESSIVE_TEST 1121#ifdef AGGRESSIVE_TEST
1122 if (le16_to_cpu(ex1->ee_len) >= 4) 1122 if (le16_to_cpu(ex1->ee_len) >= 4)
1123 return 0; 1123 return 0;
1124#endif 1124#endif
@@ -1891,8 +1891,8 @@ void ext4_ext_init(struct super_block *sb)
1891 1891
1892 if (test_opt(sb, EXTENTS)) { 1892 if (test_opt(sb, EXTENTS)) {
1893 printk("EXT4-fs: file extents enabled"); 1893 printk("EXT4-fs: file extents enabled");
1894#ifdef AGRESSIVE_TEST 1894#ifdef AGGRESSIVE_TEST
1895 printk(", agressive tests"); 1895 printk(", aggressive tests");
1896#endif 1896#endif
1897#ifdef CHECK_BINSEARCH 1897#ifdef CHECK_BINSEARCH
1898 printk(", check binsearch"); 1898 printk(", check binsearch");
diff --git a/fs/fat/inode.c b/fs/fat/inode.c
index 761073544217..9bfe607c892e 100644
--- a/fs/fat/inode.c
+++ b/fs/fat/inode.c
@@ -173,10 +173,12 @@ static ssize_t fat_direct_IO(int rw, struct kiocb *iocb,
173 * 173 *
174 * But we must fill the remaining area or hole by nul for 174 * But we must fill the remaining area or hole by nul for
175 * updating ->mmu_private. 175 * updating ->mmu_private.
176 *
177 * Return 0, and fallback to normal buffered write.
176 */ 178 */
177 loff_t size = offset + iov_length(iov, nr_segs); 179 loff_t size = offset + iov_length(iov, nr_segs);
178 if (MSDOS_I(inode)->mmu_private < size) 180 if (MSDOS_I(inode)->mmu_private < size)
179 return -EINVAL; 181 return 0;
180 } 182 }
181 183
182 /* 184 /*
diff --git a/fs/jffs/Makefile b/fs/jffs/Makefile
deleted file mode 100644
index 9c1c0bb59696..000000000000
--- a/fs/jffs/Makefile
+++ /dev/null
@@ -1,11 +0,0 @@
1#
2# Makefile for the linux Journalling Flash FileSystem (JFFS) routines.
3#
4# $Id: Makefile,v 1.11 2001/09/25 20:59:41 dwmw2 Exp $
5#
6
7obj-$(CONFIG_JFFS_FS) += jffs.o
8
9jffs-y := jffs_fm.o intrep.o inode-v23.o
10jffs-$(CONFIG_JFFS_PROC_FS) += jffs_proc.o
11jffs-objs := $(jffs-y)
diff --git a/fs/jffs/inode-v23.c b/fs/jffs/inode-v23.c
deleted file mode 100644
index 9602b925da08..000000000000
--- a/fs/jffs/inode-v23.c
+++ /dev/null
@@ -1,1847 +0,0 @@
1/*
2 * JFFS -- Journalling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 1999, 2000 Axis Communications AB.
5 *
6 * Created by Finn Hakansson <finn@axis.com>.
7 *
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.
12 *
13 * $Id: inode-v23.c,v 1.70 2001/10/02 09:16:02 dwmw2 Exp $
14 *
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
17 *
18 * Copyright 2000, 2001 Red Hat, Inc.
19 */
20
21/* inode.c -- Contains the code that is called from the VFS. */
22
23/* TODO-ALEX:
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.
27 */
28
29#include <linux/time.h>
30
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>
37#include <linux/fs.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>
48
49#include "jffs_fm.h"
50#include "intrep.h"
51#ifdef CONFIG_JFFS_PROC_FS
52#include "jffs_proc.h"
53#endif
54
55static int jffs_remove(struct inode *dir, struct dentry *dentry, int type);
56
57static const struct super_operations jffs_ops;
58static const struct file_operations jffs_file_operations;
59static const struct inode_operations jffs_file_inode_operations;
60static const struct file_operations jffs_dir_operations;
61static const struct inode_operations jffs_dir_inode_operations;
62static const struct address_space_operations jffs_address_operations;
63
64struct kmem_cache *node_cache = NULL;
65struct kmem_cache *fm_cache = NULL;
66
67/* Called by the VFS at mount time to initialize the whole file system. */
68static int jffs_fill_super(struct super_block *sb, void *data, int silent)
69{
70 struct inode *root_inode;
71 struct jffs_control *c;
72
73 sb->s_flags |= MS_NODIRATIME;
74
75 D1(printk(KERN_NOTICE "JFFS: Trying to mount device %s.\n",
76 sb->s_id));
77
78 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
79 printk(KERN_WARNING "JFFS: Trying to mount a "
80 "non-mtd device.\n");
81 return -EINVAL;
82 }
83
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;
88
89 /* Build the file system. */
90 if (jffs_build_fs(sb) < 0) {
91 goto jffs_sb_err1;
92 }
93
94 /*
95 * set up enough so that we can read an inode
96 */
97 sb->s_magic = JFFS_MAGIC_SB_BITMASK;
98 sb->s_op = &jffs_ops;
99
100 root_inode = iget(sb, JFFS_MIN_INO);
101 if (!root_inode)
102 goto jffs_sb_err2;
103
104 /* Get the root directory of this file system. */
105 if (!(sb->s_root = d_alloc_root(root_inode))) {
106 goto jffs_sb_err3;
107 }
108
109 c = (struct jffs_control *) sb->s_fs_info;
110
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",
116 sb->s_id);
117 }
118#endif
119
120 /* Set the Garbage Collection thresholds */
121
122 /* GC if free space goes below 5% of the total size */
123 c->gc_minfree_threshold = c->fmc->flash_size / 20;
124
125 if (c->gc_minfree_threshold < c->fmc->sector_size)
126 c->gc_minfree_threshold = c->fmc->sector_size;
127
128 /* GC if dirty space exceeds 33% of the total size. */
129 c->gc_maxdirty_threshold = c->fmc->flash_size / 3;
130
131 if (c->gc_maxdirty_threshold < c->fmc->sector_size)
132 c->gc_maxdirty_threshold = c->fmc->sector_size;
133
134
135 c->thread_pid = kernel_thread (jffs_garbage_collect_thread,
136 (void *) c,
137 CLONE_KERNEL);
138 D1(printk(KERN_NOTICE "JFFS: GC thread pid=%d.\n", (int) c->thread_pid));
139
140 D1(printk(KERN_NOTICE "JFFS: Successfully mounted device %s.\n",
141 sb->s_id));
142 return 0;
143
144jffs_sb_err3:
145 iput(root_inode);
146jffs_sb_err2:
147 jffs_cleanup_control((struct jffs_control *)sb->s_fs_info);
148jffs_sb_err1:
149 printk(KERN_WARNING "JFFS: Failed to mount device %s.\n",
150 sb->s_id);
151 return -EINVAL;
152}
153
154
155/* This function is called when the file system is umounted. */
156static void
157jffs_put_super(struct super_block *sb)
158{
159 struct jffs_control *c = (struct jffs_control *) sb->s_fs_info;
160
161 D2(printk("jffs_put_super()\n"));
162
163#ifdef CONFIG_JFFS_PROC_FS
164 jffs_unregister_jffs_proc_dir(c);
165#endif
166
167 if (c->gc_task) {
168 D1(printk (KERN_NOTICE "jffs_put_super(): Telling gc thread to die.\n"));
169 send_sig(SIGKILL, c->gc_task, 1);
170 }
171 wait_for_completion(&c->gc_thread_comp);
172
173 D1(printk (KERN_NOTICE "jffs_put_super(): Successfully waited on thread.\n"));
174
175 jffs_cleanup_control((struct jffs_control *)sb->s_fs_info);
176 D1(printk(KERN_NOTICE "JFFS: Successfully unmounted device %s.\n",
177 sb->s_id));
178}
179
180
181/* This function is called when user commands like chmod, chgrp and
182 chown are executed. System calls like trunc() results in a call
183 to this function. */
184static int
185jffs_setattr(struct dentry *dentry, struct iattr *iattr)
186{
187 struct inode *inode = dentry->d_inode;
188 struct jffs_raw_inode raw_inode;
189 struct jffs_control *c;
190 struct jffs_fmcontrol *fmc;
191 struct jffs_file *f;
192 struct jffs_node *new_node;
193 int update_all;
194 int res = 0;
195 int recoverable = 0;
196
197 lock_kernel();
198
199 if ((res = inode_change_ok(inode, iattr)))
200 goto out;
201
202 c = (struct jffs_control *)inode->i_sb->s_fs_info;
203 fmc = c->fmc;
204
205 D3(printk (KERN_NOTICE "notify_change(): down biglock\n"));
206 mutex_lock(&fmc->biglock);
207
208 f = jffs_find_file(c, inode->i_ino);
209
210 ASSERT(if (!f) {
211 printk("jffs_setattr(): Invalid inode number: %lu\n",
212 inode->i_ino);
213 D3(printk (KERN_NOTICE "notify_change(): up biglock\n"));
214 mutex_unlock(&fmc->biglock);
215 res = -EINVAL;
216 goto out;
217 });
218
219 D1(printk("***jffs_setattr(): file: \"%s\", ino: %u\n",
220 f->name, f->ino));
221
222 update_all = iattr->ia_valid & ATTR_FORCE;
223
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. */
229 recoverable = 1;
230 }
231
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);
236 res = -ENOMEM;
237 goto out;
238 }
239
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;
251 raw_inode.dsize = 0;
252 raw_inode.offset = 0;
253 raw_inode.rsize = 0;
254 raw_inode.dsize = 0;
255 raw_inode.nsize = f->nsize;
256 raw_inode.nlink = f->nlink;
257 raw_inode.spare = 0;
258 raw_inode.rename = 0;
259 raw_inode.deleted = 0;
260
261 if (update_all || iattr->ia_valid & ATTR_MODE) {
262 raw_inode.mode = iattr->ia_mode;
263 inode->i_mode = iattr->ia_mode;
264 }
265 if (update_all || iattr->ia_valid & ATTR_UID) {
266 raw_inode.uid = iattr->ia_uid;
267 inode->i_uid = iattr->ia_uid;
268 }
269 if (update_all || iattr->ia_valid & ATTR_GID) {
270 raw_inode.gid = iattr->ia_gid;
271 inode->i_gid = iattr->ia_gid;
272 }
273 if (update_all || iattr->ia_valid & ATTR_SIZE) {
274 int len;
275 D1(printk("jffs_notify_change(): Changing size "
276 "to %lu bytes!\n", (long)iattr->ia_size));
277 raw_inode.offset = iattr->ia_size;
278
279 /* Calculate how many bytes need to be removed from
280 the end. */
281 if (f->size < iattr->ia_size) {
282 len = 0;
283 }
284 else {
285 len = f->size - iattr->ia_size;
286 }
287
288 raw_inode.rsize = len;
289
290 /* The updated node will be a removal node, with
291 base at the new size and size of the nbr of bytes
292 to be removed. */
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;
297
298 if (len) {
299 invalidate_mapping_pages(inode->i_mapping, 0, -1);
300 }
301 inode->i_ctime = CURRENT_TIME_SEC;
302 inode->i_mtime = inode->i_ctime;
303 }
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;
307 }
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;
311 }
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;
315 }
316
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);
323 goto out;
324 }
325
326 jffs_insert_node(c, f, &raw_inode, NULL, new_node);
327
328 mark_inode_dirty(inode);
329 D3(printk (KERN_NOTICE "n_c(): up biglock\n"));
330 mutex_unlock(&c->fmc->biglock);
331out:
332 unlock_kernel();
333 return res;
334} /* jffs_notify_change() */
335
336
337static struct inode *
338jffs_new_inode(const struct inode * dir, struct jffs_raw_inode *raw_inode,
339 int * err)
340{
341 struct super_block * sb;
342 struct inode * inode;
343 struct jffs_control *c;
344 struct jffs_file *f;
345
346 sb = dir->i_sb;
347 inode = new_inode(sb);
348 if (!inode) {
349 *err = -ENOMEM;
350 return NULL;
351 }
352
353 c = (struct jffs_control *)sb->s_fs_info;
354
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;
368
369 f = jffs_find_file(c, raw_inode->ino);
370
371 inode->i_private = (void *)f;
372 insert_inode_hash(inode);
373
374 return inode;
375}
376
377/* Get statistics of the file system. */
378static int
379jffs_statfs(struct dentry *dentry, struct kstatfs *buf)
380{
381 struct jffs_control *c = (struct jffs_control *) dentry->d_sb->s_fs_info;
382 struct jffs_fmcontrol *fmc;
383
384 lock_kernel();
385
386 fmc = c->fmc;
387
388 D2(printk("jffs_statfs()\n"));
389
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)
396 >> PAGE_CACHE_SHIFT;
397 buf->f_bavail = buf->f_bfree;
398
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;
404
405 unlock_kernel();
406
407 return 0;
408}
409
410
411/* Rename a file. */
412static int
413jffs_rename(struct inode *old_dir, struct dentry *old_dentry,
414 struct inode *new_dir, struct dentry *new_dentry)
415{
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;
421 struct jffs_file *f;
422 struct jffs_node *node;
423 struct inode *inode;
424 int result = 0;
425 __u32 rename_data = 0;
426
427 D2(printk("***jffs_rename()\n"));
428
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));
433
434 lock_kernel();
435 c = (struct jffs_control *)old_dir->i_sb->s_fs_info;
436 ASSERT(if (!c) {
437 printk(KERN_ERR "jffs_rename(): The old_dir inode "
438 "didn't have a reference to a jffs_file struct\n");
439 unlock_kernel();
440 return -EIO;
441 });
442
443 result = -ENOTDIR;
444 if (!(old_dir_f = old_dir->i_private)) {
445 D(printk("jffs_rename(): Old dir invalid.\n"));
446 goto jffs_rename_end;
447 }
448
449 /* Try to find the file to move. */
450 result = -ENOENT;
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;
454 }
455
456 /* Find the new directory. */
457 result = -ENOTDIR;
458 if (!(new_dir_f = new_dir->i_private)) {
459 D(printk("jffs_rename(): New dir invalid.\n"));
460 goto jffs_rename_end;
461 }
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. */
465 result = -ENOMEM;
466 if (!(node = jffs_alloc_node())) {
467 D(printk("jffs_rename(): Allocation failed: node == 0\n"));
468 goto jffs_rename_end;
469 }
470 node->data_offset = 0;
471 node->removed_size = 0;
472
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;
481#if 0
482 raw_inode.uid = f->uid;
483 raw_inode.gid = f->gid;
484#endif
485 raw_inode.atime = get_seconds();
486 raw_inode.mtime = raw_inode.atime;
487 raw_inode.ctime = f->ctime;
488 raw_inode.offset = 0;
489 raw_inode.dsize = 0;
490 raw_inode.rsize = 0;
491 raw_inode.nsize = new_dentry->d_name.len;
492 raw_inode.nlink = f->nlink;
493 raw_inode.spare = 0;
494 raw_inode.rename = 0;
495 raw_inode.deleted = 0;
496
497 /* See if there already exists a file with the same name as
498 new_name. */
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;
504 }
505
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;
513 }
514 raw_inode.dsize = 0;
515
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,
520 del_f->mode)) < 0) {
521 /* This is really bad. */
522 printk(KERN_ERR "JFFS: An error occurred in "
523 "rename().\n");
524 }
525 // up(&c->fmc->gclock);
526 }
527
528 if (old_dir_f != new_dir_f) {
529 /* Remove the file from its old position in the
530 filesystem tree. */
531 jffs_unlink_file_from_tree(f);
532 }
533
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() "
538 "failed!\n"));
539 }
540
541 if (old_dir_f != new_dir_f) {
542 /* Insert the file to its new position in the
543 file system. */
544 jffs_insert_file_into_tree(f);
545 }
546
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);
552 iput(inode);
553 }
554
555jffs_rename_end:
556 D3(printk (KERN_NOTICE "rename(): up biglock\n"));
557 mutex_unlock(&c->fmc->biglock);
558 unlock_kernel();
559 return result;
560} /* jffs_rename() */
561
562
563/* Read the contents of a directory. Used by programs like `ls'
564 for instance. */
565static int
566jffs_readdir(struct file *filp, void *dirent, filldir_t filldir)
567{
568 struct jffs_file *f;
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;
572 int j;
573 int ddino;
574 lock_kernel();
575 D3(printk (KERN_NOTICE "readdir(): down biglock\n"));
576 mutex_lock(&c->fmc->biglock);
577
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);
584 unlock_kernel();
585 return 0;
586 }
587 filp->f_pos = 1;
588 }
589 if (filp->f_pos == 1) {
590 if (inode->i_ino == JFFS_MIN_INO) {
591 ddino = JFFS_MIN_INO;
592 }
593 else {
594 ddino = ((struct jffs_file *)
595 inode->i_private)->pino;
596 }
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);
601 unlock_kernel();
602 return 0;
603 }
604 filp->f_pos++;
605 }
606 f = ((struct jffs_file *)inode->i_private)->children;
607
608 j = 2;
609 while(f && (f->deleted || j++ < filp->f_pos )) {
610 f = f->sibling_next;
611 }
612
613 while (f) {
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);
620 unlock_kernel();
621 return 0;
622 }
623 filp->f_pos++;
624 do {
625 f = f->sibling_next;
626 } while(f && f->deleted);
627 }
628 D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
629 mutex_unlock(&c->fmc->biglock);
630 unlock_kernel();
631 return filp->f_pos;
632} /* jffs_readdir() */
633
634
635/* Find a file in a directory. If the file exists, return its
636 corresponding dentry. */
637static struct dentry *
638jffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
639{
640 struct jffs_file *d;
641 struct jffs_file *f;
642 struct jffs_control *c = (struct jffs_control *)dir->i_sb->s_fs_info;
643 int len;
644 int r = 0;
645 const char *name;
646 struct inode *inode = NULL;
647
648 len = dentry->d_name.len;
649 name = dentry->d_name.name;
650
651 lock_kernel();
652
653 D3({
654 char *s = kmalloc(len + 1, GFP_KERNEL);
655 memcpy(s, name, len);
656 s[len] = '\0';
657 printk("jffs_lookup(): dir: 0x%p, name: \"%s\"\n", dir, s);
658 kfree(s);
659 });
660
661 D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
662 mutex_lock(&c->fmc->biglock);
663
664 r = -ENAMETOOLONG;
665 if (len > JFFS_MAX_NAME_LEN) {
666 goto jffs_lookup_end;
667 }
668
669 r = -EACCES;
670 if (!(d = (struct jffs_file *)dir->i_private)) {
671 D(printk("jffs_lookup(): No such inode! (%lu)\n",
672 dir->i_ino));
673 goto jffs_lookup_end;
674 }
675
676 /* Get the corresponding inode to the file. */
677
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 ().
681 */
682
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;
689 }
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;
698 }
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;
707 }
708 D3(printk (KERN_NOTICE "lookup(): down biglock\n"));
709 mutex_lock(&c->fmc->biglock);
710 } else {
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));
714 inode = NULL;
715 }
716
717 d_add(dentry, inode);
718 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
719 mutex_unlock(&c->fmc->biglock);
720 unlock_kernel();
721 return NULL;
722
723jffs_lookup_end:
724 D3(printk (KERN_NOTICE "lookup(): up biglock\n"));
725 mutex_unlock(&c->fmc->biglock);
726
727jffs_lookup_end_no_biglock:
728 unlock_kernel();
729 return ERR_PTR(r);
730} /* jffs_lookup() */
731
732
733/* Try to read a page of data from a file. */
734static int
735jffs_do_readpage_nolock(struct file *file, struct page *page)
736{
737 void *buf;
738 unsigned long read_len;
739 int result;
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;
743 int r;
744 loff_t offset;
745
746 D2(printk("***jffs_readpage(): file = \"%s\", page->index = %lu\n",
747 (f->name ? f->name : ""), (long)page->index));
748
749 get_page(page);
750 /* Don't SetPageLocked(page), should be locked already */
751 ClearPageUptodate(page);
752 ClearPageError(page);
753
754 D3(printk (KERN_NOTICE "readpage(): down biglock\n"));
755 mutex_lock(&c->fmc->biglock);
756
757 read_len = 0;
758 result = 0;
759 offset = page_offset(page);
760
761 kmap(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);
766 if (r != read_len) {
767 result = -EIO;
768 D(
769 printk("***jffs_readpage(): Read error! "
770 "Wanted to read %lu bytes but only "
771 "read %d bytes.\n", read_len, r);
772 );
773 }
774
775 }
776
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);
781 kunmap(page);
782
783 D3(printk (KERN_NOTICE "readpage(): up biglock\n"));
784 mutex_unlock(&c->fmc->biglock);
785
786 if (result) {
787 SetPageError(page);
788 }else {
789 SetPageUptodate(page);
790 }
791
792 page_cache_release(page);
793
794 D3(printk("jffs_readpage(): Leaving...\n"));
795
796 return result;
797} /* jffs_do_readpage_nolock() */
798
799static int jffs_readpage(struct file *file, struct page *page)
800{
801 int ret = jffs_do_readpage_nolock(file, page);
802 unlock_page(page);
803 return ret;
804}
805
806/* Create a new directory. */
807static int
808jffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
809{
810 struct jffs_raw_inode raw_inode;
811 struct jffs_control *c;
812 struct jffs_node *node;
813 struct jffs_file *dir_f;
814 struct inode *inode;
815 int dir_mode;
816 int result = 0;
817 int err;
818
819 D1({
820 int len = dentry->d_name.len;
821 char *_name = kmalloc(len + 1, GFP_KERNEL);
822 memcpy(_name, dentry->d_name.name, len);
823 _name[len] = '\0';
824 printk("***jffs_mkdir(): dir = 0x%p, name = \"%s\", "
825 "len = %d, mode = 0x%08x\n", dir, _name, len, mode);
826 kfree(_name);
827 });
828
829 lock_kernel();
830 dir_f = dir->i_private;
831
832 ASSERT(if (!dir_f) {
833 printk(KERN_ERR "jffs_mkdir(): No reference to a "
834 "jffs_file struct in inode.\n");
835 unlock_kernel();
836 return -EIO;
837 });
838
839 c = dir_f->c;
840 D3(printk (KERN_NOTICE "mkdir(): down biglock\n"));
841 mutex_lock(&c->fmc->biglock);
842
843 dir_mode = S_IFDIR | (mode & (S_IRWXUGO|S_ISVTX)
844 & ~current->fs->umask);
845 if (dir->i_mode & S_ISGID) {
846 dir_mode |= S_ISGID;
847 }
848
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"));
852 result = -ENOMEM;
853 goto jffs_mkdir_end;
854 }
855 node->data_offset = 0;
856 node->removed_size = 0;
857
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;
871 raw_inode.dsize = 0;
872 raw_inode.rsize = 0;
873 raw_inode.nsize = dentry->d_name.len;
874 raw_inode.nlink = 1;
875 raw_inode.spare = 0;
876 raw_inode.rename = 0;
877 raw_inode.deleted = 0;
878
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);
884 goto jffs_mkdir_end;
885 }
886
887 /* Insert the new node into the file system. */
888 if ((result = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
889 node)) < 0) {
890 goto jffs_mkdir_end;
891 }
892
893 inode = jffs_new_inode(dir, &raw_inode, &err);
894 if (inode == NULL) {
895 result = err;
896 goto jffs_mkdir_end;
897 }
898
899 inode->i_op = &jffs_dir_inode_operations;
900 inode->i_fop = &jffs_dir_operations;
901
902 mark_inode_dirty(dir);
903 d_instantiate(dentry, inode);
904
905 result = 0;
906jffs_mkdir_end:
907 D3(printk (KERN_NOTICE "mkdir(): up biglock\n"));
908 mutex_unlock(&c->fmc->biglock);
909 unlock_kernel();
910 return result;
911} /* jffs_mkdir() */
912
913
914/* Remove a directory. */
915static int
916jffs_rmdir(struct inode *dir, struct dentry *dentry)
917{
918 struct jffs_control *c = (struct jffs_control *)dir->i_sb->s_fs_info;
919 int ret;
920 D3(printk("***jffs_rmdir()\n"));
921 D3(printk (KERN_NOTICE "rmdir(): down biglock\n"));
922 lock_kernel();
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);
927 unlock_kernel();
928 return ret;
929}
930
931
932/* Remove any kind of file except for directories. */
933static int
934jffs_unlink(struct inode *dir, struct dentry *dentry)
935{
936 struct jffs_control *c = (struct jffs_control *)dir->i_sb->s_fs_info;
937 int ret;
938
939 lock_kernel();
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);
946 unlock_kernel();
947 return ret;
948}
949
950
951/* Remove a JFFS entry, i.e. plain files, directories, etc. Here we
952 shouldn't test for free space on the device. */
953static int
954jffs_remove(struct inode *dir, struct dentry *dentry, int type)
955{
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;
962 int result = 0;
963
964 D1({
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);
969 _name[len] = '\0';
970 printk("***jffs_remove(): file = \"%s\", ino = %ld\n", _name, dentry->d_inode->i_ino);
971 kfree(_name);
972 });
973
974 dir_f = dir->i_private;
975 c = dir_f->c;
976
977 result = -ENOENT;
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;
982 }
983
984 if (S_ISDIR(type)) {
985 struct jffs_file *child = del_f->children;
986 while(child) {
987 if( !child->deleted ) {
988 result = -ENOTEMPTY;
989 goto jffs_remove_end;
990 }
991 child = child->sibling_next;
992 }
993 }
994 else if (S_ISDIR(del_f->mode)) {
995 D(printk("jffs_remove(): node is a directory "
996 "but it shouldn't be.\n"));
997 result = -EPERM;
998 goto jffs_remove_end;
999 }
1000
1001 inode = dentry->d_inode;
1002
1003 result = -EIO;
1004 if (del_f->ino != inode->i_ino)
1005 goto jffs_remove_end;
1006
1007 if (!inode->i_nlink) {
1008 printk("Deleting nonexistent file inode: %lu, nlink: %d\n",
1009 inode->i_ino, inode->i_nlink);
1010 inode->i_nlink=1;
1011 }
1012
1013 /* Create a node for the deletion. */
1014 result = -ENOMEM;
1015 if (!(del_node = jffs_alloc_node())) {
1016 D(printk("jffs_remove(): Allocation failed!\n"));
1017 goto jffs_remove_end;
1018 }
1019 del_node->data_offset = 0;
1020 del_node->removed_size = 0;
1021
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;
1041
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);
1045 result = -EIO;
1046 goto jffs_remove_end;
1047 }
1048
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);
1052
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);
1057
1058 d_delete(dentry); /* This also frees the inode */
1059
1060 result = 0;
1061jffs_remove_end:
1062 return result;
1063} /* jffs_remove() */
1064
1065
1066static int
1067jffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1068{
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;
1074 int result = 0;
1075 u16 data = old_encode_dev(rdev);
1076 int err;
1077
1078 D1(printk("***jffs_mknod()\n"));
1079
1080 if (!old_valid_dev(rdev))
1081 return -EINVAL;
1082 lock_kernel();
1083 dir_f = dir->i_private;
1084 c = dir_f->c;
1085
1086 D3(printk (KERN_NOTICE "mknod(): down biglock\n"));
1087 mutex_lock(&c->fmc->biglock);
1088
1089 /* Create and initialize a new node. */
1090 if (!(node = jffs_alloc_node())) {
1091 D(printk("jffs_mknod(): Allocation failed!\n"));
1092 result = -ENOMEM;
1093 goto jffs_mknod_err;
1094 }
1095 node->data_offset = 0;
1096 node->removed_size = 0;
1097
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;
1118
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"));
1123 result = err;
1124 goto jffs_mknod_err;
1125 }
1126
1127 /* Insert the new node into the file system. */
1128 if ((err = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
1129 node)) < 0) {
1130 result = err;
1131 goto jffs_mknod_end;
1132 }
1133
1134 inode = jffs_new_inode(dir, &raw_inode, &err);
1135 if (inode == NULL) {
1136 result = err;
1137 goto jffs_mknod_end;
1138 }
1139
1140 init_special_inode(inode, mode, rdev);
1141
1142 d_instantiate(dentry, inode);
1143
1144 goto jffs_mknod_end;
1145
1146jffs_mknod_err:
1147 if (node) {
1148 jffs_free_node(node);
1149 }
1150
1151jffs_mknod_end:
1152 D3(printk (KERN_NOTICE "mknod(): up biglock\n"));
1153 mutex_unlock(&c->fmc->biglock);
1154 unlock_kernel();
1155 return result;
1156} /* jffs_mknod() */
1157
1158
1159static int
1160jffs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1161{
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;
1167
1168 int symname_len = strlen(symname);
1169 int err;
1170
1171 lock_kernel();
1172 D1({
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);
1177 _name[len] = '\0';
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);
1183 kfree(_name);
1184 kfree(_symname);
1185 });
1186
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");
1191 unlock_kernel();
1192 return -EIO;
1193 });
1194
1195 c = dir_f->c;
1196
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"));
1200 unlock_kernel();
1201 return -ENOMEM;
1202 }
1203 D3(printk (KERN_NOTICE "symlink(): down biglock\n"));
1204 mutex_lock(&c->fmc->biglock);
1205
1206 node->data_offset = 0;
1207 node->removed_size = 0;
1208
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;
1228
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;
1235 }
1236
1237 /* Insert the new node into the file system. */
1238 if ((err = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
1239 node)) < 0) {
1240 goto jffs_symlink_end;
1241 }
1242
1243 inode = jffs_new_inode(dir, &raw_inode, &err);
1244 if (inode == NULL) {
1245 goto jffs_symlink_end;
1246 }
1247 err = 0;
1248 inode->i_op = &page_symlink_inode_operations;
1249 inode->i_mapping->a_ops = &jffs_address_operations;
1250
1251 d_instantiate(dentry, inode);
1252 jffs_symlink_end:
1253 D3(printk (KERN_NOTICE "symlink(): up biglock\n"));
1254 mutex_unlock(&c->fmc->biglock);
1255 unlock_kernel();
1256 return err;
1257} /* jffs_symlink() */
1258
1259
1260/* Create an inode inside a JFFS directory (dir) and return it.
1261 *
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.
1265 *
1266 * If the create succeeds, we fill in the inode information
1267 * with d_instantiate().
1268 */
1269static int
1270jffs_create(struct inode *dir, struct dentry *dentry, int mode,
1271 struct nameidata *nd)
1272{
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;
1278 int err;
1279
1280 lock_kernel();
1281 D1({
1282 int len = dentry->d_name.len;
1283 char *s = kmalloc(len + 1, GFP_KERNEL);
1284 memcpy(s, dentry->d_name.name, len);
1285 s[len] = '\0';
1286 printk("jffs_create(): dir: 0x%p, name: \"%s\"\n", dir, s);
1287 kfree(s);
1288 });
1289
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");
1294 unlock_kernel();
1295 return -EIO;
1296 });
1297
1298 c = dir_f->c;
1299
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"));
1303 unlock_kernel();
1304 return -ENOMEM;
1305 }
1306 D3(printk (KERN_NOTICE "create(): down biglock\n"));
1307 mutex_lock(&c->fmc->biglock);
1308
1309 node->data_offset = 0;
1310 node->removed_size = 0;
1311
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;
1331
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;
1338 }
1339
1340 /* Insert the new node into the file system. */
1341 if ((err = jffs_insert_node(c, NULL, &raw_inode, dentry->d_name.name,
1342 node)) < 0) {
1343 goto jffs_create_end;
1344 }
1345
1346 /* Initialize an inode. */
1347 inode = jffs_new_inode(dir, &raw_inode, &err);
1348 if (inode == NULL) {
1349 goto jffs_create_end;
1350 }
1351 err = 0;
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;
1356
1357 d_instantiate(dentry, inode);
1358 jffs_create_end:
1359 D3(printk (KERN_NOTICE "create(): up biglock\n"));
1360 mutex_unlock(&c->fmc->biglock);
1361 unlock_kernel();
1362 return err;
1363} /* jffs_create() */
1364
1365
1366/* Write, append or rewrite data to an existing file. */
1367static ssize_t
1368jffs_file_write(struct file *filp, const char *buf, size_t count,
1369 loff_t *ppos)
1370{
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;
1378 size_t written = 0;
1379 __u32 thiscount = count;
1380 loff_t pos = *ppos;
1381 int err;
1382
1383 inode = filp->f_path.dentry->d_inode;
1384
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));
1388
1389#if 0
1390 if (inode->i_sb->s_flags & MS_RDONLY) {
1391 D(printk("jffs_file_write(): MS_RDONLY\n"));
1392 err = -EROFS;
1393 goto out_isem;
1394 }
1395#endif
1396 err = -EINVAL;
1397
1398 if (!S_ISREG(inode->i_mode)) {
1399 D(printk("jffs_file_write(): inode->i_mode == 0x%08x\n",
1400 inode->i_mode));
1401 goto out_isem;
1402 }
1403
1404 if (!(f = inode->i_private)) {
1405 D(printk("jffs_file_write(): inode->i_private = 0x%p\n",
1406 inode->i_private));
1407 goto out_isem;
1408 }
1409
1410 c = f->c;
1411
1412 /*
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.
1419 * -- prumpf
1420 */
1421 thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
1422
1423 D3(printk (KERN_NOTICE "file_write(): down biglock\n"));
1424 mutex_lock(&c->fmc->biglock);
1425
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
1428 * we're done.
1429 *
1430 * <_Anarchy_> posix and reality are not interconnected on this issue
1431 */
1432 while (count) {
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"));
1437 err = -ENOMEM;
1438 goto out;
1439 }
1440
1441 node->data_offset = pos;
1442 node->removed_size = 0;
1443
1444 /* Initialize the raw inode. */
1445 raw_inode.magic = JFFS_MAGIC_BITMASK;
1446 raw_inode.ino = f->ino;
1447 raw_inode.pino = f->pino;
1448
1449 raw_inode.mode = f->mode;
1450
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;
1464
1465 if (pos < f->size) {
1466 node->removed_size = raw_inode.rsize = min(thiscount, (__u32)(f->size - pos));
1467
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.
1471 */
1472 if (pos + thiscount < f->size) {
1473 /* If all the data we're overwriting are _real_,
1474 not just holes, then:
1475 recoverable = 1;
1476 */
1477 }
1478 }
1479
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.
1486 -- dwmw2.
1487 */
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);
1493 goto out;
1494 }
1495
1496 written += err;
1497 buf += err;
1498 count -= err;
1499 pos += err;
1500
1501 /* Insert the new node into the file system. */
1502 if ((err = jffs_insert_node(c, f, &raw_inode, NULL, node)) < 0) {
1503 goto out;
1504 }
1505
1506 D3(printk("jffs_file_write(): new f_pos %ld.\n", (long)pos));
1507
1508 thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
1509 }
1510 out:
1511 D3(printk (KERN_NOTICE "file_write(): up biglock\n"));
1512 mutex_unlock(&c->fmc->biglock);
1513
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;
1518 }
1519 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
1520 mark_inode_dirty(inode);
1521 invalidate_mapping_pages(inode->i_mapping, 0, -1);
1522
1523 out_isem:
1524 return err;
1525} /* jffs_file_write() */
1526
1527static int
1528jffs_prepare_write(struct file *filp, struct page *page,
1529 unsigned from, unsigned to)
1530{
1531 /* FIXME: we should detect some error conditions here */
1532
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);
1536
1537 return 0;
1538} /* jffs_prepare_write() */
1539
1540static int
1541jffs_commit_write(struct file *filp, struct page *page,
1542 unsigned from, unsigned to)
1543{
1544 void *addr = page_address(page) + from;
1545 /* XXX: PAGE_CACHE_SHIFT or PAGE_SHIFT */
1546 loff_t pos = page_offset(page) + from;
1547
1548 return jffs_file_write(filp, addr, to-from, &pos);
1549} /* jffs_commit_write() */
1550
1551/* This is our ioctl() routine. */
1552static int
1553jffs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1554 unsigned long arg)
1555{
1556 struct jffs_control *c;
1557 int ret = 0;
1558
1559 D2(printk("***jffs_ioctl(): cmd = 0x%08x, arg = 0x%08lx\n",
1560 cmd, arg));
1561
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);
1565 return -EIO;
1566 }
1567 D3(printk (KERN_NOTICE "ioctl(): down biglock\n"));
1568 mutex_lock(&c->fmc->biglock);
1569
1570 switch (cmd) {
1571 case JFFS_PRINT_HASH:
1572 jffs_print_hash_table(c);
1573 break;
1574 case JFFS_PRINT_TREE:
1575 jffs_print_tree(c->root, 0);
1576 break;
1577 case JFFS_GET_STATUS:
1578 {
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"));
1587 ret = -EFAULT;
1588 break;
1589 }
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,
1600 &fst,
1601 sizeof(struct jffs_flash_status))) {
1602 ret = -EFAULT;
1603 }
1604 }
1605 break;
1606 default:
1607 ret = -ENOTTY;
1608 }
1609 D3(printk (KERN_NOTICE "ioctl(): up biglock\n"));
1610 mutex_unlock(&c->fmc->biglock);
1611 return ret;
1612} /* jffs_ioctl() */
1613
1614
1615static const struct address_space_operations jffs_address_operations = {
1616 .readpage = jffs_readpage,
1617 .prepare_write = jffs_prepare_write,
1618 .commit_write = jffs_commit_write,
1619};
1620
1621static int jffs_fsync(struct file *f, struct dentry *d, int datasync)
1622{
1623 /* We currently have O_SYNC operations at all times.
1624 Do nothing.
1625 */
1626 return 0;
1627}
1628
1629
1630static const struct file_operations jffs_file_operations =
1631{
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,
1642};
1643
1644
1645static const struct inode_operations jffs_file_inode_operations =
1646{
1647 .lookup = jffs_lookup, /* lookup */
1648 .setattr = jffs_setattr,
1649};
1650
1651
1652static const struct file_operations jffs_dir_operations =
1653{
1654 .readdir = jffs_readdir,
1655};
1656
1657
1658static const struct inode_operations jffs_dir_inode_operations =
1659{
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,
1669};
1670
1671
1672/* Initialize an inode for the VFS. */
1673static void
1674jffs_read_inode(struct inode *inode)
1675{
1676 struct jffs_file *f;
1677 struct jffs_control *c;
1678
1679 D3(printk("jffs_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
1680
1681 if (!inode->i_sb) {
1682 D(printk("jffs_read_inode(): !inode->i_sb ==> "
1683 "No super block!\n"));
1684 return;
1685 }
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",
1691 inode->i_ino));
1692 D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
1693 mutex_unlock(&c->fmc->biglock);
1694 return;
1695 }
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;
1708
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;
1714 }
1715 else if (S_ISDIR(inode->i_mode)) {
1716 inode->i_op = &jffs_dir_inode_operations;
1717 inode->i_fop = &jffs_dir_operations;
1718 }
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;
1722 }
1723 else {
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. */
1727 u16 val;
1728 jffs_read_data(f, (char *)&val, 0, 2);
1729 init_special_inode(inode, inode->i_mode,
1730 old_decode_dev(val));
1731 }
1732
1733 D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
1734 mutex_unlock(&c->fmc->biglock);
1735}
1736
1737
1738static void
1739jffs_delete_inode(struct inode *inode)
1740{
1741 struct jffs_file *f;
1742 struct jffs_control *c;
1743 D3(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
1744 inode->i_ino));
1745
1746 truncate_inode_pages(&inode->i_data, 0);
1747 lock_kernel();
1748 inode->i_size = 0;
1749 inode->i_blocks = 0;
1750 inode->i_private = NULL;
1751 clear_inode(inode);
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);
1756 }
1757
1758 unlock_kernel();
1759}
1760
1761
1762static void
1763jffs_write_super(struct super_block *sb)
1764{
1765 struct jffs_control *c = (struct jffs_control *)sb->s_fs_info;
1766 lock_kernel();
1767 jffs_garbage_collect_trigger(c);
1768 unlock_kernel();
1769}
1770
1771static int jffs_remount(struct super_block *sb, int *flags, char *data)
1772{
1773 *flags |= MS_NODIRATIME;
1774 return 0;
1775}
1776
1777static const struct super_operations jffs_ops =
1778{
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,
1785};
1786
1787static int jffs_get_sb(struct file_system_type *fs_type,
1788 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1789{
1790 return get_sb_bdev(fs_type, flags, dev_name, data, jffs_fill_super,
1791 mnt);
1792}
1793
1794static struct file_system_type jffs_fs_type = {
1795 .owner = THIS_MODULE,
1796 .name = "jffs",
1797 .get_sb = jffs_get_sb,
1798 .kill_sb = kill_block_super,
1799 .fs_flags = FS_REQUIRES_DEV,
1800};
1801
1802static int __init
1803init_jffs_fs(void)
1804{
1805 printk(KERN_INFO "JFFS version " JFFS_VERSION_STRING
1806 ", (C) 1999, 2000 Axis Communications AB\n");
1807
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");
1812 }
1813#endif
1814 fm_cache = kmem_cache_create("jffs_fm", sizeof(struct jffs_fm),
1815 0,
1816 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
1817 NULL, NULL);
1818 if (!fm_cache) {
1819 return -ENOMEM;
1820 }
1821
1822 node_cache = kmem_cache_create("jffs_node",sizeof(struct jffs_node),
1823 0,
1824 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
1825 NULL, NULL);
1826 if (!node_cache) {
1827 kmem_cache_destroy(fm_cache);
1828 return -ENOMEM;
1829 }
1830
1831 return register_filesystem(&jffs_fs_type);
1832}
1833
1834static void __exit
1835exit_jffs_fs(void)
1836{
1837 unregister_filesystem(&jffs_fs_type);
1838 kmem_cache_destroy(fm_cache);
1839 kmem_cache_destroy(node_cache);
1840}
1841
1842module_init(init_jffs_fs)
1843module_exit(exit_jffs_fs)
1844
1845MODULE_DESCRIPTION("The Journalling Flash File System");
1846MODULE_AUTHOR("Axis Communications AB.");
1847MODULE_LICENSE("GPL");
diff --git a/fs/jffs/intrep.c b/fs/jffs/intrep.c
deleted file mode 100644
index 6dd18911b44c..000000000000
--- a/fs/jffs/intrep.c
+++ /dev/null
@@ -1,3449 +0,0 @@
1/*
2 * JFFS -- Journaling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 1999, 2000 Axis Communications, Inc.
5 *
6 * Created by Finn Hakansson <finn@axis.com>.
7 *
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.
12 *
13 * $Id: intrep.c,v 1.102 2001/09/23 23:28:36 dwmw2 Exp $
14 *
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
17 *
18 */
19
20/* This file contains the code for the internal structure of the
21 Journaling Flash File System, JFFS. */
22
23/*
24 * Todo list:
25 *
26 * memcpy_to_flash() and memcpy_from_flash() functions.
27 *
28 * Implementation of hard links.
29 *
30 * Organize the source code in a better way. Against the VFS we could
31 * have jffs_ext.c, and against the block device jffs_int.c.
32 * A better file-internal organization too.
33 *
34 * A better checksum algorithm.
35 *
36 * Consider endianness stuff. ntohl() etc.
37 *
38 * Are we handling the atime, mtime, ctime members of the inode right?
39 *
40 * Remove some duplicated code. Take a look at jffs_write_node() and
41 * jffs_rewrite_data() for instance.
42 *
43 * Implement more meaning of the nlink member in various data structures.
44 * nlink could be used in conjunction with hard links for instance.
45 *
46 * Better memory management. Allocate data structures in larger chunks
47 * if possible.
48 *
49 * If too much meta data is stored, a garbage collect should be issued.
50 * We have experienced problems with too much meta data with for instance
51 * log files.
52 *
53 * Improve the calls to jffs_ioctl(). We would like to retrieve more
54 * information to be able to debug (or to supervise) JFFS during run-time.
55 *
56 */
57
58#include <linux/types.h>
59#include <linux/slab.h>
60#include <linux/jffs.h>
61#include <linux/fs.h>
62#include <linux/stat.h>
63#include <linux/pagemap.h>
64#include <linux/mutex.h>
65#include <asm/byteorder.h>
66#include <linux/smp_lock.h>
67#include <linux/time.h>
68#include <linux/ctype.h>
69#include <linux/freezer.h>
70
71#include "intrep.h"
72#include "jffs_fm.h"
73
74long no_jffs_node = 0;
75static long no_jffs_file = 0;
76#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
77long no_jffs_control = 0;
78long no_jffs_raw_inode = 0;
79long no_jffs_node_ref = 0;
80long no_jffs_fm = 0;
81long no_jffs_fmcontrol = 0;
82long no_hash = 0;
83long no_name = 0;
84#endif
85
86static int jffs_scan_flash(struct jffs_control *c);
87static int jffs_update_file(struct jffs_file *f, struct jffs_node *node);
88static int jffs_build_file(struct jffs_file *f);
89static int jffs_free_file(struct jffs_file *f);
90static int jffs_free_node_list(struct jffs_file *f);
91static int jffs_garbage_collect_now(struct jffs_control *c);
92static int jffs_insert_file_into_hash(struct jffs_file *f);
93static int jffs_remove_redundant_nodes(struct jffs_file *f);
94
95/* Is there enough space on the flash? */
96static inline int JFFS_ENOUGH_SPACE(struct jffs_control *c, __u32 space)
97{
98 struct jffs_fmcontrol *fmc = c->fmc;
99
100 while (1) {
101 if ((fmc->flash_size - (fmc->used_size + fmc->dirty_size))
102 >= fmc->min_free_size + space) {
103 return 1;
104 }
105 if (fmc->dirty_size < fmc->sector_size)
106 return 0;
107
108 if (jffs_garbage_collect_now(c)) {
109 D1(printk("JFFS_ENOUGH_SPACE: jffs_garbage_collect_now() failed.\n"));
110 return 0;
111 }
112 }
113}
114
115#if CONFIG_JFFS_FS_VERBOSE > 0
116static __u8
117flash_read_u8(struct mtd_info *mtd, loff_t from)
118{
119 size_t retlen;
120 __u8 ret;
121 int res;
122
123 res = MTD_READ(mtd, from, 1, &retlen, &ret);
124 if (retlen != 1) {
125 printk("Didn't read a byte in flash_read_u8(). Returned %d\n", res);
126 return 0;
127 }
128
129 return ret;
130}
131
132static void
133jffs_hexdump(struct mtd_info *mtd, loff_t pos, int size)
134{
135 char line[16];
136 int j = 0;
137
138 while (size > 0) {
139 int i;
140
141 printk("%ld:", (long) pos);
142 for (j = 0; j < 16; j++) {
143 line[j] = flash_read_u8(mtd, pos++);
144 }
145 for (i = 0; i < j; i++) {
146 if (!(i & 1)) {
147 printk(" %.2x", line[i] & 0xff);
148 }
149 else {
150 printk("%.2x", line[i] & 0xff);
151 }
152 }
153
154 /* Print empty space */
155 for (; i < 16; i++) {
156 if (!(i & 1)) {
157 printk(" ");
158 }
159 else {
160 printk(" ");
161 }
162 }
163 printk(" ");
164
165 for (i = 0; i < j; i++) {
166 if (isgraph(line[i])) {
167 printk("%c", line[i]);
168 }
169 else {
170 printk(".");
171 }
172 }
173 printk("\n");
174 size -= 16;
175 }
176}
177
178/* Print the contents of a node. */
179static void
180jffs_print_node(struct jffs_node *n)
181{
182 D(printk("jffs_node: 0x%p\n", n));
183 D(printk("{\n"));
184 D(printk(" 0x%08x, /* version */\n", n->version));
185 D(printk(" 0x%08x, /* data_offset */\n", n->data_offset));
186 D(printk(" 0x%08x, /* data_size */\n", n->data_size));
187 D(printk(" 0x%08x, /* removed_size */\n", n->removed_size));
188 D(printk(" 0x%08x, /* fm_offset */\n", n->fm_offset));
189 D(printk(" 0x%02x, /* name_size */\n", n->name_size));
190 D(printk(" 0x%p, /* fm, fm->offset: %u */\n",
191 n->fm, (n->fm ? n->fm->offset : 0)));
192 D(printk(" 0x%p, /* version_prev */\n", n->version_prev));
193 D(printk(" 0x%p, /* version_next */\n", n->version_next));
194 D(printk(" 0x%p, /* range_prev */\n", n->range_prev));
195 D(printk(" 0x%p, /* range_next */\n", n->range_next));
196 D(printk("}\n"));
197}
198
199#endif
200
201/* Print the contents of a raw inode. */
202static void
203jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
204{
205 D(printk("jffs_raw_inode: inode number: %u\n", raw_inode->ino));
206 D(printk("{\n"));
207 D(printk(" 0x%08x, /* magic */\n", raw_inode->magic));
208 D(printk(" 0x%08x, /* ino */\n", raw_inode->ino));
209 D(printk(" 0x%08x, /* pino */\n", raw_inode->pino));
210 D(printk(" 0x%08x, /* version */\n", raw_inode->version));
211 D(printk(" 0x%08x, /* mode */\n", raw_inode->mode));
212 D(printk(" 0x%04x, /* uid */\n", raw_inode->uid));
213 D(printk(" 0x%04x, /* gid */\n", raw_inode->gid));
214 D(printk(" 0x%08x, /* atime */\n", raw_inode->atime));
215 D(printk(" 0x%08x, /* mtime */\n", raw_inode->mtime));
216 D(printk(" 0x%08x, /* ctime */\n", raw_inode->ctime));
217 D(printk(" 0x%08x, /* offset */\n", raw_inode->offset));
218 D(printk(" 0x%08x, /* dsize */\n", raw_inode->dsize));
219 D(printk(" 0x%08x, /* rsize */\n", raw_inode->rsize));
220 D(printk(" 0x%02x, /* nsize */\n", raw_inode->nsize));
221 D(printk(" 0x%02x, /* nlink */\n", raw_inode->nlink));
222 D(printk(" 0x%02x, /* spare */\n",
223 raw_inode->spare));
224 D(printk(" %u, /* rename */\n",
225 raw_inode->rename));
226 D(printk(" %u, /* deleted */\n",
227 raw_inode->deleted));
228 D(printk(" 0x%02x, /* accurate */\n",
229 raw_inode->accurate));
230 D(printk(" 0x%08x, /* dchksum */\n", raw_inode->dchksum));
231 D(printk(" 0x%04x, /* nchksum */\n", raw_inode->nchksum));
232 D(printk(" 0x%04x, /* chksum */\n", raw_inode->chksum));
233 D(printk("}\n"));
234}
235
236#define flash_safe_acquire(arg)
237#define flash_safe_release(arg)
238
239
240static int
241flash_safe_read(struct mtd_info *mtd, loff_t from,
242 u_char *buf, size_t count)
243{
244 size_t retlen;
245 int res;
246
247 D3(printk(KERN_NOTICE "flash_safe_read(%p, %08x, %p, %08x)\n",
248 mtd, (unsigned int) from, buf, count));
249
250 res = mtd->read(mtd, from, count, &retlen, buf);
251 if (retlen != count) {
252 panic("Didn't read all bytes in flash_safe_read(). Returned %d\n", res);
253 }
254 return res?res:retlen;
255}
256
257
258static __u32
259flash_read_u32(struct mtd_info *mtd, loff_t from)
260{
261 size_t retlen;
262 __u32 ret;
263 int res;
264
265 res = mtd->read(mtd, from, 4, &retlen, (unsigned char *)&ret);
266 if (retlen != 4) {
267 printk("Didn't read all bytes in flash_read_u32(). Returned %d\n", res);
268 return 0;
269 }
270
271 return ret;
272}
273
274
275static int
276flash_safe_write(struct mtd_info *mtd, loff_t to,
277 const u_char *buf, size_t count)
278{
279 size_t retlen;
280 int res;
281
282 D3(printk(KERN_NOTICE "flash_safe_write(%p, %08x, %p, %08x)\n",
283 mtd, (unsigned int) to, buf, count));
284
285 res = mtd->write(mtd, to, count, &retlen, buf);
286 if (retlen != count) {
287 printk("Didn't write all bytes in flash_safe_write(). Returned %d\n", res);
288 }
289 return res?res:retlen;
290}
291
292
293static int
294flash_safe_writev(struct mtd_info *mtd, const struct kvec *vecs,
295 unsigned long iovec_cnt, loff_t to)
296{
297 size_t retlen, retlen_a;
298 int i;
299 int res;
300
301 D3(printk(KERN_NOTICE "flash_safe_writev(%p, %08x, %p)\n",
302 mtd, (unsigned int) to, vecs));
303
304 if (mtd->writev) {
305 res = mtd->writev(mtd, vecs, iovec_cnt, to, &retlen);
306 return res ? res : retlen;
307 }
308 /* Not implemented writev. Repeatedly use write - on the not so
309 unreasonable assumption that the mtd driver doesn't care how
310 many write cycles we use. */
311 res=0;
312 retlen=0;
313
314 for (i=0; !res && i<iovec_cnt; i++) {
315 res = mtd->write(mtd, to, vecs[i].iov_len, &retlen_a,
316 vecs[i].iov_base);
317 if (retlen_a != vecs[i].iov_len) {
318 printk("Didn't write all bytes in flash_safe_writev(). Returned %d\n", res);
319 if (i != iovec_cnt-1)
320 return -EIO;
321 }
322 /* If res is non-zero, retlen_a is undefined, but we don't
323 care because in that case it's not going to be
324 returned anyway.
325 */
326 to += retlen_a;
327 retlen += retlen_a;
328 }
329 return res?res:retlen;
330}
331
332
333static int
334flash_memset(struct mtd_info *mtd, loff_t to,
335 const u_char c, size_t size)
336{
337 static unsigned char pattern[64];
338 int i;
339
340 /* fill up pattern */
341
342 for(i = 0; i < 64; i++)
343 pattern[i] = c;
344
345 /* write as many 64-byte chunks as we can */
346
347 while (size >= 64) {
348 flash_safe_write(mtd, to, pattern, 64);
349 size -= 64;
350 to += 64;
351 }
352
353 /* and the rest */
354
355 if(size)
356 flash_safe_write(mtd, to, pattern, size);
357
358 return size;
359}
360
361
362static void
363intrep_erase_callback(struct erase_info *done)
364{
365 wait_queue_head_t *wait_q;
366
367 wait_q = (wait_queue_head_t *)done->priv;
368
369 wake_up(wait_q);
370}
371
372
373static int
374flash_erase_region(struct mtd_info *mtd, loff_t start,
375 size_t size)
376{
377 struct erase_info *erase;
378 DECLARE_WAITQUEUE(wait, current);
379 wait_queue_head_t wait_q;
380
381 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
382 if (!erase)
383 return -ENOMEM;
384
385 init_waitqueue_head(&wait_q);
386
387 erase->mtd = mtd;
388 erase->callback = intrep_erase_callback;
389 erase->addr = start;
390 erase->len = size;
391 erase->priv = (u_long)&wait_q;
392
393 /* FIXME: Use TASK_INTERRUPTIBLE and deal with being interrupted */
394 set_current_state(TASK_UNINTERRUPTIBLE);
395 add_wait_queue(&wait_q, &wait);
396
397 if (mtd->erase(mtd, erase) < 0) {
398 set_current_state(TASK_RUNNING);
399 remove_wait_queue(&wait_q, &wait);
400 kfree(erase);
401
402 printk(KERN_WARNING "flash: erase of region [0x%lx, 0x%lx] "
403 "totally failed\n", (long)start, (long)start + size);
404
405 return -1;
406 }
407
408 schedule(); /* Wait for flash to finish. */
409 remove_wait_queue(&wait_q, &wait);
410
411 kfree(erase);
412
413 return 0;
414}
415
416/* This routine calculates checksums in JFFS. */
417static __u32
418jffs_checksum(const void *data, int size)
419{
420 __u32 sum = 0;
421 __u8 *ptr = (__u8 *)data;
422 while (size-- > 0) {
423 sum += *ptr++;
424 }
425 D3(printk(", result: 0x%08x\n", sum));
426 return sum;
427}
428
429
430static int
431jffs_checksum_flash(struct mtd_info *mtd, loff_t start, int size, __u32 *result)
432{
433 __u32 sum = 0;
434 loff_t ptr = start;
435 __u8 *read_buf;
436 int i, length;
437
438 /* Allocate read buffer */
439 read_buf = kmalloc(sizeof(__u8) * 4096, GFP_KERNEL);
440 if (!read_buf) {
441 printk(KERN_NOTICE "kmalloc failed in jffs_checksum_flash()\n");
442 return -ENOMEM;
443 }
444 /* Loop until checksum done */
445 while (size) {
446 /* Get amount of data to read */
447 if (size < 4096)
448 length = size;
449 else
450 length = 4096;
451
452 /* Perform flash read */
453 D3(printk(KERN_NOTICE "jffs_checksum_flash\n"));
454 flash_safe_read(mtd, ptr, &read_buf[0], length);
455
456 /* Compute checksum */
457 for (i=0; i < length ; i++)
458 sum += read_buf[i];
459
460 /* Update pointer and size */
461 size -= length;
462 ptr += length;
463 }
464
465 /* Free read buffer */
466 kfree(read_buf);
467
468 /* Return result */
469 D3(printk("checksum result: 0x%08x\n", sum));
470 *result = sum;
471 return 0;
472}
473
474static __inline__ void jffs_fm_write_lock(struct jffs_fmcontrol *fmc)
475{
476 // down(&fmc->wlock);
477}
478
479static __inline__ void jffs_fm_write_unlock(struct jffs_fmcontrol *fmc)
480{
481 // up(&fmc->wlock);
482}
483
484
485/* Create and initialize a new struct jffs_file. */
486static struct jffs_file *
487jffs_create_file(struct jffs_control *c,
488 const struct jffs_raw_inode *raw_inode)
489{
490 struct jffs_file *f;
491
492 if (!(f = kzalloc(sizeof(*f), GFP_KERNEL))) {
493 D(printk("jffs_create_file(): Failed!\n"));
494 return NULL;
495 }
496 no_jffs_file++;
497 f->ino = raw_inode->ino;
498 f->pino = raw_inode->pino;
499 f->nlink = raw_inode->nlink;
500 f->deleted = raw_inode->deleted;
501 f->c = c;
502
503 return f;
504}
505
506
507/* Build a control block for the file system. */
508static struct jffs_control *
509jffs_create_control(struct super_block *sb)
510{
511 struct jffs_control *c;
512 register int s = sizeof(struct jffs_control);
513 int i;
514 D(char *t = 0);
515
516 D2(printk("jffs_create_control()\n"));
517
518 if (!(c = kmalloc(s, GFP_KERNEL))) {
519 goto fail_control;
520 }
521 DJM(no_jffs_control++);
522 c->root = NULL;
523 c->gc_task = NULL;
524 c->hash_len = JFFS_HASH_SIZE;
525 s = sizeof(struct list_head) * c->hash_len;
526 if (!(c->hash = kmalloc(s, GFP_KERNEL))) {
527 goto fail_hash;
528 }
529 DJM(no_hash++);
530 for (i = 0; i < c->hash_len; i++)
531 INIT_LIST_HEAD(&c->hash[i]);
532 if (!(c->fmc = jffs_build_begin(c, MINOR(sb->s_dev)))) {
533 goto fail_fminit;
534 }
535 c->next_ino = JFFS_MIN_INO + 1;
536 c->delete_list = (struct jffs_delete_list *) 0;
537 return c;
538
539fail_fminit:
540 D(t = "c->fmc");
541fail_hash:
542 kfree(c);
543 DJM(no_jffs_control--);
544 D(t = t ? t : "c->hash");
545fail_control:
546 D(t = t ? t : "control");
547 D(printk("jffs_create_control(): Allocation failed: (%s)\n", t));
548 return (struct jffs_control *)0;
549}
550
551
552/* Clean up all data structures associated with the file system. */
553void
554jffs_cleanup_control(struct jffs_control *c)
555{
556 D2(printk("jffs_cleanup_control()\n"));
557
558 if (!c) {
559 D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
560 return;
561 }
562
563 while (c->delete_list) {
564 struct jffs_delete_list *delete_list_element;
565 delete_list_element = c->delete_list;
566 c->delete_list = c->delete_list->next;
567 kfree(delete_list_element);
568 }
569
570 /* Free all files and nodes. */
571 if (c->hash) {
572 jffs_foreach_file(c, jffs_free_node_list);
573 jffs_foreach_file(c, jffs_free_file);
574 kfree(c->hash);
575 DJM(no_hash--);
576 }
577 jffs_cleanup_fmcontrol(c->fmc);
578 kfree(c);
579 DJM(no_jffs_control--);
580 D3(printk("jffs_cleanup_control(): Leaving...\n"));
581}
582
583
584/* This function adds a virtual root node to the in-RAM representation.
585 Called by jffs_build_fs(). */
586static int
587jffs_add_virtual_root(struct jffs_control *c)
588{
589 struct jffs_file *root;
590 struct jffs_node *node;
591
592 D2(printk("jffs_add_virtual_root(): "
593 "Creating a virtual root directory.\n"));
594
595 if (!(root = kzalloc(sizeof(struct jffs_file), GFP_KERNEL))) {
596 return -ENOMEM;
597 }
598 no_jffs_file++;
599 if (!(node = jffs_alloc_node())) {
600 kfree(root);
601 no_jffs_file--;
602 return -ENOMEM;
603 }
604 DJM(no_jffs_node++);
605 memset(node, 0, sizeof(struct jffs_node));
606 node->ino = JFFS_MIN_INO;
607 root->ino = JFFS_MIN_INO;
608 root->mode = S_IFDIR | S_IRWXU | S_IRGRP
609 | S_IXGRP | S_IROTH | S_IXOTH;
610 root->atime = root->mtime = root->ctime = get_seconds();
611 root->nlink = 1;
612 root->c = c;
613 root->version_head = root->version_tail = node;
614 jffs_insert_file_into_hash(root);
615 return 0;
616}
617
618
619/* This is where the file system is built and initialized. */
620int
621jffs_build_fs(struct super_block *sb)
622{
623 struct jffs_control *c;
624 int err = 0;
625
626 D2(printk("jffs_build_fs()\n"));
627
628 if (!(c = jffs_create_control(sb))) {
629 return -ENOMEM;
630 }
631 c->building_fs = 1;
632 c->sb = sb;
633 if ((err = jffs_scan_flash(c)) < 0) {
634 if(err == -EAGAIN){
635 /* scan_flash() wants us to try once more. A flipping
636 bits sector was detect in the middle of the scan flash.
637 Clean up old allocated memory before going in.
638 */
639 D1(printk("jffs_build_fs: Cleaning up all control structures,"
640 " reallocating them and trying mount again.\n"));
641 jffs_cleanup_control(c);
642 if (!(c = jffs_create_control(sb))) {
643 return -ENOMEM;
644 }
645 c->building_fs = 1;
646 c->sb = sb;
647
648 if ((err = jffs_scan_flash(c)) < 0) {
649 goto jffs_build_fs_fail;
650 }
651 }else{
652 goto jffs_build_fs_fail;
653 }
654 }
655
656 /* Add a virtual root node if no one exists. */
657 if (!jffs_find_file(c, JFFS_MIN_INO)) {
658 if ((err = jffs_add_virtual_root(c)) < 0) {
659 goto jffs_build_fs_fail;
660 }
661 }
662
663 while (c->delete_list) {
664 struct jffs_file *f;
665 struct jffs_delete_list *delete_list_element;
666
667 if ((f = jffs_find_file(c, c->delete_list->ino))) {
668 f->deleted = 1;
669 }
670 delete_list_element = c->delete_list;
671 c->delete_list = c->delete_list->next;
672 kfree(delete_list_element);
673 }
674
675 /* Remove deleted nodes. */
676 if ((err = jffs_foreach_file(c, jffs_possibly_delete_file)) < 0) {
677 printk(KERN_ERR "JFFS: Failed to remove deleted nodes.\n");
678 goto jffs_build_fs_fail;
679 }
680 /* Remove redundant nodes. (We are not interested in the
681 return value in this case.) */
682 jffs_foreach_file(c, jffs_remove_redundant_nodes);
683 /* Try to build a tree from all the nodes. */
684 if ((err = jffs_foreach_file(c, jffs_insert_file_into_tree)) < 0) {
685 printk("JFFS: Failed to build tree.\n");
686 goto jffs_build_fs_fail;
687 }
688 /* Compute the sizes of all files in the filesystem. Adjust if
689 necessary. */
690 if ((err = jffs_foreach_file(c, jffs_build_file)) < 0) {
691 printk("JFFS: Failed to build file system.\n");
692 goto jffs_build_fs_fail;
693 }
694 sb->s_fs_info = (void *)c;
695 c->building_fs = 0;
696
697 D1(jffs_print_hash_table(c));
698 D1(jffs_print_tree(c->root, 0));
699
700 return 0;
701
702jffs_build_fs_fail:
703 jffs_cleanup_control(c);
704 return err;
705} /* jffs_build_fs() */
706
707
708/*
709 This checks for sectors that were being erased in their previous
710 lifetimes and for some reason or the other (power fail etc.),
711 the erase cycles never completed.
712 As the flash array would have reverted back to read status,
713 these sectors are detected by the symptom of the "flipping bits",
714 i.e. bits being read back differently from the same location in
715 flash if read multiple times.
716 The only solution to this is to re-erase the entire
717 sector.
718 Unfortunately detecting "flipping bits" is not a simple exercise
719 as a bit may be read back at 1 or 0 depending on the alignment
720 of the stars in the universe.
721 The level of confidence is in direct proportion to the number of
722 scans done. By power fail testing I (Vipin) have been able to
723 proove that reading twice is not enough.
724 Maybe 4 times? Change NUM_REREADS to a higher number if you want
725 a (even) higher degree of confidence in your mount process.
726 A higher number would of course slow down your mount.
727*/
728static int check_partly_erased_sectors(struct jffs_fmcontrol *fmc){
729
730#define NUM_REREADS 4 /* see note above */
731#define READ_AHEAD_BYTES 4096 /* must be a multiple of 4,
732 usually set to kernel page size */
733
734 __u8 *read_buf1;
735 __u8 *read_buf2;
736
737 int err = 0;
738 int retlen;
739 int i;
740 int cnt;
741 __u32 offset;
742 loff_t pos = 0;
743 loff_t end = fmc->flash_size;
744
745
746 /* Allocate read buffers */
747 read_buf1 = kmalloc(sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
748 if (!read_buf1)
749 return -ENOMEM;
750
751 read_buf2 = kmalloc(sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
752 if (!read_buf2) {
753 kfree(read_buf1);
754 return -ENOMEM;
755 }
756
757 CHECK_NEXT:
758 while(pos < end){
759
760 D1(printk("check_partly_erased_sector():checking sector which contains"
761 " offset 0x%x for flipping bits..\n", (__u32)pos));
762
763 retlen = flash_safe_read(fmc->mtd, pos,
764 &read_buf1[0], READ_AHEAD_BYTES);
765 retlen &= ~3;
766
767 for(cnt = 0; cnt < NUM_REREADS; cnt++){
768 (void)flash_safe_read(fmc->mtd, pos,
769 &read_buf2[0], READ_AHEAD_BYTES);
770
771 for (i=0 ; i < retlen ; i+=4) {
772 /* buffers MUST match, double word for word! */
773 if(*((__u32 *) &read_buf1[i]) !=
774 *((__u32 *) &read_buf2[i])
775 ){
776 /* flipping bits detected, time to erase sector */
777 /* This will help us log some statistics etc. */
778 D1(printk("Flipping bits detected in re-read round:%i of %i\n",
779 cnt, NUM_REREADS));
780 D1(printk("check_partly_erased_sectors:flipping bits detected"
781 " @offset:0x%x(0x%x!=0x%x)\n",
782 (__u32)pos+i, *((__u32 *) &read_buf1[i]),
783 *((__u32 *) &read_buf2[i])));
784
785 /* calculate start of present sector */
786 offset = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
787
788 D1(printk("check_partly_erased_sector():erasing sector starting 0x%x.\n",
789 offset));
790
791 if (flash_erase_region(fmc->mtd,
792 offset, fmc->sector_size) < 0) {
793 printk(KERN_ERR "JFFS: Erase of flash failed. "
794 "offset = %u, erase_size = %d\n",
795 offset , fmc->sector_size);
796
797 err = -EIO;
798 goto returnBack;
799
800 }else{
801 D1(printk("JFFS: Erase of flash sector @0x%x successful.\n",
802 offset));
803 /* skip ahead to the next sector */
804 pos = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
805 pos += fmc->sector_size;
806 goto CHECK_NEXT;
807 }
808 }
809 }
810 }
811 pos += READ_AHEAD_BYTES;
812 }
813
814 returnBack:
815 kfree(read_buf1);
816 kfree(read_buf2);
817
818 D2(printk("check_partly_erased_sector():Done checking all sectors till offset 0x%x for flipping bits.\n",
819 (__u32)pos));
820
821 return err;
822
823}/* end check_partly_erased_sectors() */
824
825
826
827/* Scan the whole flash memory in order to find all nodes in the
828 file systems. */
829static int
830jffs_scan_flash(struct jffs_control *c)
831{
832 char name[JFFS_MAX_NAME_LEN + 2];
833 struct jffs_raw_inode raw_inode;
834 struct jffs_node *node = NULL;
835 struct jffs_fmcontrol *fmc = c->fmc;
836 __u32 checksum;
837 __u8 tmp_accurate;
838 __u16 tmp_chksum;
839 __u32 deleted_file;
840 loff_t pos = 0;
841 loff_t start;
842 loff_t test_start;
843 loff_t end = fmc->flash_size;
844 __u8 *read_buf;
845 int i, len, retlen;
846 __u32 offset;
847
848 __u32 free_chunk_size1;
849 __u32 free_chunk_size2;
850
851
852#define NUMFREEALLOWED 2 /* 2 chunks of at least erase size space allowed */
853 int num_free_space = 0; /* Flag err if more than TWO
854 free blocks found. This is NOT allowed
855 by the current jffs design.
856 */
857 int num_free_spc_not_accp = 0; /* For debugging purposed keep count
858 of how much free space was rejected and
859 marked dirty
860 */
861
862 D1(printk("jffs_scan_flash(): start pos = 0x%lx, end = 0x%lx\n",
863 (long)pos, (long)end));
864
865 flash_safe_acquire(fmc->mtd);
866
867 /*
868 check and make sure that any sector does not suffer
869 from the "partly erased, bit flipping syndrome" (TM Vipin :)
870 If so, offending sectors will be erased.
871 */
872 if(check_partly_erased_sectors(fmc) < 0){
873
874 flash_safe_release(fmc->mtd);
875 return -EIO; /* bad, bad, bad error. Cannot continue.*/
876 }
877
878 /* Allocate read buffer */
879 read_buf = kmalloc(sizeof(__u8) * 4096, GFP_KERNEL);
880 if (!read_buf) {
881 flash_safe_release(fmc->mtd);
882 return -ENOMEM;
883 }
884
885 /* Start the scan. */
886 while (pos < end) {
887 deleted_file = 0;
888
889 /* Remember the position from where we started this scan. */
890 start = pos;
891
892 switch (flash_read_u32(fmc->mtd, pos)) {
893 case JFFS_EMPTY_BITMASK:
894 /* We have found 0xffffffff at this position. We have to
895 scan the rest of the flash till the end or till
896 something else than 0xffffffff is found.
897 Keep going till we do not find JFFS_EMPTY_BITMASK
898 anymore */
899
900 D1(printk("jffs_scan_flash(): 0xffffffff at pos 0x%lx.\n",
901 (long)pos));
902
903 while(pos < end){
904
905 len = end - pos < 4096 ? end - pos : 4096;
906
907 retlen = flash_safe_read(fmc->mtd, pos,
908 &read_buf[0], len);
909
910 retlen &= ~3;
911
912 for (i=0 ; i < retlen ; i+=4, pos += 4) {
913 if(*((__u32 *) &read_buf[i]) !=
914 JFFS_EMPTY_BITMASK)
915 break;
916 }
917 if (i == retlen)
918 continue;
919 else
920 break;
921 }
922
923 D1(printk("jffs_scan_flash():0xffffffff ended at pos 0x%lx.\n",
924 (long)pos));
925
926 /* If some free space ends in the middle of a sector,
927 treat it as dirty rather than clean.
928 This is to handle the case where one thread
929 allocated space for a node, but didn't get to
930 actually _write_ it before power was lost, leaving
931 a gap in the log. Shifting all node writes into
932 a single kernel thread will fix the original problem.
933 */
934 if ((__u32) pos % fmc->sector_size) {
935 /* If there was free space in previous
936 sectors, don't mark that dirty too -
937 only from the beginning of this sector
938 (or from start)
939 */
940
941 test_start = pos & ~(fmc->sector_size-1); /* end of last sector */
942
943 if (start < test_start) {
944
945 /* free space started in the previous sector! */
946
947 if((num_free_space < NUMFREEALLOWED) &&
948 ((unsigned int)(test_start - start) >= fmc->sector_size)){
949
950 /*
951 Count it in if we are still under NUMFREEALLOWED *and* it is
952 at least 1 erase sector in length. This will keep us from
953 picking any little ole' space as "free".
954 */
955
956 D1(printk("Reducing end of free space to 0x%x from 0x%x\n",
957 (unsigned int)test_start, (unsigned int)pos));
958
959 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
960 (unsigned int) start,
961 (unsigned int)(test_start - start)));
962
963 /* below, space from "start" to "pos" will be marked dirty. */
964 start = test_start;
965
966 /* Being in here means that we have found at least an entire
967 erase sector size of free space ending on a sector boundary.
968 Keep track of free spaces accepted.
969 */
970 num_free_space++;
971 }else{
972 num_free_spc_not_accp++;
973 D1(printk("Free space (#%i) found but *Not* accepted: Starting"
974 " 0x%x for 0x%x bytes\n",
975 num_free_spc_not_accp, (unsigned int)start,
976 (unsigned int)((unsigned int)(pos & ~(fmc->sector_size-1)) - (unsigned int)start)));
977
978 }
979
980 }
981 if((((__u32)(pos - start)) != 0)){
982
983 D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
984 (unsigned int) start, (unsigned int) (pos - start)));
985 jffs_fmalloced(fmc, (__u32) start,
986 (__u32) (pos - start), NULL);
987 }else{
988 /* "Flipping bits" detected. This means that our scan for them
989 did not catch this offset. See check_partly_erased_sectors() for
990 more info.
991 */
992
993 D1(printk("jffs_scan_flash():wants to allocate dirty flash "
994 "space for 0 bytes.\n"));
995 D1(printk("jffs_scan_flash(): Flipping bits! We will free "
996 "all allocated memory, erase this sector and remount\n"));
997
998 /* calculate start of present sector */
999 offset = (((__u32)pos)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
1000
1001 D1(printk("jffs_scan_flash():erasing sector starting 0x%x.\n",
1002 offset));
1003
1004 if (flash_erase_region(fmc->mtd,
1005 offset, fmc->sector_size) < 0) {
1006 printk(KERN_ERR "JFFS: Erase of flash failed. "
1007 "offset = %u, erase_size = %d\n",
1008 offset , fmc->sector_size);
1009
1010 flash_safe_release(fmc->mtd);
1011 kfree(read_buf);
1012 return -1; /* bad, bad, bad! */
1013
1014 }
1015 flash_safe_release(fmc->mtd);
1016 kfree(read_buf);
1017
1018 return -EAGAIN; /* erased offending sector. Try mount one more time please. */
1019 }
1020 }else{
1021 /* Being in here means that we have found free space that ends on an erase sector
1022 boundary.
1023 Count it in if we are still under NUMFREEALLOWED *and* it is at least 1 erase
1024 sector in length. This will keep us from picking any little ole' space as "free".
1025 */
1026 if((num_free_space < NUMFREEALLOWED) &&
1027 ((unsigned int)(pos - start) >= fmc->sector_size)){
1028 /* We really don't do anything to mark space as free, except *not*
1029 mark it dirty and just advance the "pos" location pointer.
1030 It will automatically be picked up as free space.
1031 */
1032 num_free_space++;
1033 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
1034 (unsigned int) start, (unsigned int) (pos - start)));
1035 }else{
1036 num_free_spc_not_accp++;
1037 D1(printk("Free space (#%i) found but *Not* accepted: Starting "
1038 "0x%x for 0x%x bytes\n", num_free_spc_not_accp,
1039 (unsigned int) start,
1040 (unsigned int) (pos - start)));
1041
1042 /* Mark this space as dirty. We already have our free space. */
1043 D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
1044 (unsigned int) start, (unsigned int) (pos - start)));
1045 jffs_fmalloced(fmc, (__u32) start,
1046 (__u32) (pos - start), NULL);
1047 }
1048
1049 }
1050 if(num_free_space > NUMFREEALLOWED){
1051 printk(KERN_WARNING "jffs_scan_flash(): Found free space "
1052 "number %i. Only %i free space is allowed.\n",
1053 num_free_space, NUMFREEALLOWED);
1054 }
1055 continue;
1056
1057 case JFFS_DIRTY_BITMASK:
1058 /* We have found 0x00000000 at this position. Scan as far
1059 as possible to find out how much is dirty. */
1060 D1(printk("jffs_scan_flash(): 0x00000000 at pos 0x%lx.\n",
1061 (long)pos));
1062 for (; pos < end
1063 && JFFS_DIRTY_BITMASK == flash_read_u32(fmc->mtd, pos);
1064 pos += 4);
1065 D1(printk("jffs_scan_flash(): 0x00 ended at "
1066 "pos 0x%lx.\n", (long)pos));
1067 jffs_fmalloced(fmc, (__u32) start,
1068 (__u32) (pos - start), NULL);
1069 continue;
1070
1071 case JFFS_MAGIC_BITMASK:
1072 /* We have probably found a new raw inode. */
1073 break;
1074
1075 default:
1076 bad_inode:
1077 /* We're f*cked. This is not solved yet. We have
1078 to scan for the magic pattern. */
1079 D1(printk("*************** Dirty flash memory or "
1080 "bad inode: "
1081 "hexdump(pos = 0x%lx, len = 128):\n",
1082 (long)pos));
1083 D1(jffs_hexdump(fmc->mtd, pos, 128));
1084
1085 for (pos += 4; pos < end; pos += 4) {
1086 switch (flash_read_u32(fmc->mtd, pos)) {
1087 case JFFS_MAGIC_BITMASK:
1088 case JFFS_EMPTY_BITMASK:
1089 /* handle these in the main switch() loop */
1090 goto cont_scan;
1091
1092 default:
1093 break;
1094 }
1095 }
1096
1097 cont_scan:
1098 /* First, mark as dirty the region
1099 which really does contain crap. */
1100 jffs_fmalloced(fmc, (__u32) start,
1101 (__u32) (pos - start),
1102 NULL);
1103
1104 continue;
1105 }/* switch */
1106
1107 /* We have found the beginning of an inode. Create a
1108 node for it unless there already is one available. */
1109 if (!node) {
1110 if (!(node = jffs_alloc_node())) {
1111 /* Free read buffer */
1112 kfree(read_buf);
1113
1114 /* Release the flash device */
1115 flash_safe_release(fmc->mtd);
1116
1117 return -ENOMEM;
1118 }
1119 DJM(no_jffs_node++);
1120 }
1121
1122 /* Read the next raw inode. */
1123
1124 flash_safe_read(fmc->mtd, pos, (u_char *) &raw_inode,
1125 sizeof(struct jffs_raw_inode));
1126
1127 /* When we compute the checksum for the inode, we never
1128 count the 'accurate' or the 'checksum' fields. */
1129 tmp_accurate = raw_inode.accurate;
1130 tmp_chksum = raw_inode.chksum;
1131 raw_inode.accurate = 0;
1132 raw_inode.chksum = 0;
1133 checksum = jffs_checksum(&raw_inode,
1134 sizeof(struct jffs_raw_inode));
1135 raw_inode.accurate = tmp_accurate;
1136 raw_inode.chksum = tmp_chksum;
1137
1138 D3(printk("*** We have found this raw inode at pos 0x%lx "
1139 "on the flash:\n", (long)pos));
1140 D3(jffs_print_raw_inode(&raw_inode));
1141
1142 if (checksum != raw_inode.chksum) {
1143 D1(printk("jffs_scan_flash(): Bad checksum: "
1144 "checksum = %u, "
1145 "raw_inode.chksum = %u\n",
1146 checksum, raw_inode.chksum));
1147 pos += sizeof(struct jffs_raw_inode);
1148 jffs_fmalloced(fmc, (__u32) start,
1149 (__u32) (pos - start), NULL);
1150 /* Reuse this unused struct jffs_node. */
1151 continue;
1152 }
1153
1154 /* Check the raw inode read so far. Start with the
1155 maximum length of the filename. */
1156 if (raw_inode.nsize > JFFS_MAX_NAME_LEN) {
1157 printk(KERN_WARNING "jffs_scan_flash: Found a "
1158 "JFFS node with name too large\n");
1159 goto bad_inode;
1160 }
1161
1162 if (raw_inode.rename && raw_inode.dsize != sizeof(__u32)) {
1163 printk(KERN_WARNING "jffs_scan_flash: Found a "
1164 "rename node with dsize %u.\n",
1165 raw_inode.dsize);
1166 jffs_print_raw_inode(&raw_inode);
1167 goto bad_inode;
1168 }
1169
1170 /* The node's data segment should not exceed a
1171 certain length. */
1172 if (raw_inode.dsize > fmc->max_chunk_size) {
1173 printk(KERN_WARNING "jffs_scan_flash: Found a "
1174 "JFFS node with dsize (0x%x) > max_chunk_size (0x%x)\n",
1175 raw_inode.dsize, fmc->max_chunk_size);
1176 goto bad_inode;
1177 }
1178
1179 pos += sizeof(struct jffs_raw_inode);
1180
1181 /* This shouldn't be necessary because a node that
1182 violates the flash boundaries shouldn't be written
1183 in the first place. */
1184 if (pos >= end) {
1185 goto check_node;
1186 }
1187
1188 /* Read the name. */
1189 *name = 0;
1190 if (raw_inode.nsize) {
1191 flash_safe_read(fmc->mtd, pos, name, raw_inode.nsize);
1192 name[raw_inode.nsize] = '\0';
1193 pos += raw_inode.nsize
1194 + JFFS_GET_PAD_BYTES(raw_inode.nsize);
1195 D3(printk("name == \"%s\"\n", name));
1196 checksum = jffs_checksum(name, raw_inode.nsize);
1197 if (checksum != raw_inode.nchksum) {
1198 D1(printk("jffs_scan_flash(): Bad checksum: "
1199 "checksum = %u, "
1200 "raw_inode.nchksum = %u\n",
1201 checksum, raw_inode.nchksum));
1202 jffs_fmalloced(fmc, (__u32) start,
1203 (__u32) (pos - start), NULL);
1204 /* Reuse this unused struct jffs_node. */
1205 continue;
1206 }
1207 if (pos >= end) {
1208 goto check_node;
1209 }
1210 }
1211
1212 /* Read the data, if it exists, in order to be sure it
1213 matches the checksum. */
1214 if (raw_inode.dsize) {
1215 if (raw_inode.rename) {
1216 deleted_file = flash_read_u32(fmc->mtd, pos);
1217 }
1218 if (jffs_checksum_flash(fmc->mtd, pos, raw_inode.dsize, &checksum)) {
1219 printk("jffs_checksum_flash() failed to calculate a checksum\n");
1220 jffs_fmalloced(fmc, (__u32) start,
1221 (__u32) (pos - start), NULL);
1222 /* Reuse this unused struct jffs_node. */
1223 continue;
1224 }
1225 pos += raw_inode.dsize
1226 + JFFS_GET_PAD_BYTES(raw_inode.dsize);
1227
1228 if (checksum != raw_inode.dchksum) {
1229 D1(printk("jffs_scan_flash(): Bad checksum: "
1230 "checksum = %u, "
1231 "raw_inode.dchksum = %u\n",
1232 checksum, raw_inode.dchksum));
1233 jffs_fmalloced(fmc, (__u32) start,
1234 (__u32) (pos - start), NULL);
1235 /* Reuse this unused struct jffs_node. */
1236 continue;
1237 }
1238 }
1239
1240 check_node:
1241
1242 /* Remember the highest inode number in the whole file
1243 system. This information will be used when assigning
1244 new files new inode numbers. */
1245 if (c->next_ino <= raw_inode.ino) {
1246 c->next_ino = raw_inode.ino + 1;
1247 }
1248
1249 if (raw_inode.accurate) {
1250 int err;
1251 node->data_offset = raw_inode.offset;
1252 node->data_size = raw_inode.dsize;
1253 node->removed_size = raw_inode.rsize;
1254 /* Compute the offset to the actual data in the
1255 on-flash node. */
1256 node->fm_offset
1257 = sizeof(struct jffs_raw_inode)
1258 + raw_inode.nsize
1259 + JFFS_GET_PAD_BYTES(raw_inode.nsize);
1260 node->fm = jffs_fmalloced(fmc, (__u32) start,
1261 (__u32) (pos - start),
1262 node);
1263 if (!node->fm) {
1264 D(printk("jffs_scan_flash(): !node->fm\n"));
1265 jffs_free_node(node);
1266 DJM(no_jffs_node--);
1267
1268 /* Free read buffer */
1269 kfree(read_buf);
1270
1271 /* Release the flash device */
1272 flash_safe_release(fmc->mtd);
1273
1274 return -ENOMEM;
1275 }
1276 if ((err = jffs_insert_node(c, NULL, &raw_inode,
1277 name, node)) < 0) {
1278 printk("JFFS: Failed to handle raw inode. "
1279 "(err = %d)\n", err);
1280 break;
1281 }
1282 if (raw_inode.rename) {
1283 struct jffs_delete_list *dl
1284 = (struct jffs_delete_list *)
1285 kmalloc(sizeof(struct jffs_delete_list),
1286 GFP_KERNEL);
1287 if (!dl) {
1288 D(printk("jffs_scan_flash: !dl\n"));
1289 jffs_free_node(node);
1290 DJM(no_jffs_node--);
1291
1292 /* Release the flash device */
1293 flash_safe_release(fmc->flash_part);
1294
1295 /* Free read buffer */
1296 kfree(read_buf);
1297
1298 return -ENOMEM;
1299 }
1300 dl->ino = deleted_file;
1301 dl->next = c->delete_list;
1302 c->delete_list = dl;
1303 node->data_size = 0;
1304 }
1305 D3(jffs_print_node(node));
1306 node = NULL; /* Don't free the node! */
1307 }
1308 else {
1309 jffs_fmalloced(fmc, (__u32) start,
1310 (__u32) (pos - start), NULL);
1311 D3(printk("jffs_scan_flash(): Just found an obsolete "
1312 "raw_inode. Continuing the scan...\n"));
1313 /* Reuse this unused struct jffs_node. */
1314 }
1315 }
1316
1317 if (node) {
1318 jffs_free_node(node);
1319 DJM(no_jffs_node--);
1320 }
1321 jffs_build_end(fmc);
1322
1323 /* Free read buffer */
1324 kfree(read_buf);
1325
1326 if(!num_free_space){
1327 printk(KERN_WARNING "jffs_scan_flash(): Did not find even a single "
1328 "chunk of free space. This is BAD!\n");
1329 }
1330
1331 /* Return happy */
1332 D3(printk("jffs_scan_flash(): Leaving...\n"));
1333 flash_safe_release(fmc->mtd);
1334
1335 /* This is to trap the "free size accounting screwed error. */
1336 free_chunk_size1 = jffs_free_size1(fmc);
1337 free_chunk_size2 = jffs_free_size2(fmc);
1338
1339 if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
1340
1341 printk(KERN_WARNING "jffs_scan_falsh():Free size accounting screwed\n");
1342 printk(KERN_WARNING "jfffs_scan_flash():free_chunk_size1 == 0x%x, "
1343 "free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n",
1344 free_chunk_size1, free_chunk_size2, fmc->free_size);
1345
1346 return -1; /* Do NOT mount f/s so that we can inspect what happened.
1347 Mounting this screwed up f/s will screw us up anyway.
1348 */
1349 }
1350
1351 return 0; /* as far as we are concerned, we are happy! */
1352} /* jffs_scan_flash() */
1353
1354
1355/* Insert any kind of node into the file system. Take care of data
1356 insertions and deletions. Also remove redundant information. The
1357 memory allocated for the `name' is regarded as "given away" in the
1358 caller's perspective. */
1359int
1360jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
1361 const struct jffs_raw_inode *raw_inode,
1362 const char *name, struct jffs_node *node)
1363{
1364 int update_name = 0;
1365 int insert_into_tree = 0;
1366
1367 D2(printk("jffs_insert_node(): ino = %u, version = %u, "
1368 "name = \"%s\", deleted = %d\n",
1369 raw_inode->ino, raw_inode->version,
1370 ((name && *name) ? name : ""), raw_inode->deleted));
1371
1372 /* If there doesn't exist an associated jffs_file, then
1373 create, initialize and insert one into the file system. */
1374 if (!f && !(f = jffs_find_file(c, raw_inode->ino))) {
1375 if (!(f = jffs_create_file(c, raw_inode))) {
1376 return -ENOMEM;
1377 }
1378 jffs_insert_file_into_hash(f);
1379 insert_into_tree = 1;
1380 }
1381 node->ino = raw_inode->ino;
1382 node->version = raw_inode->version;
1383 node->data_size = raw_inode->dsize;
1384 node->fm_offset = sizeof(struct jffs_raw_inode) + raw_inode->nsize
1385 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1386 node->name_size = raw_inode->nsize;
1387
1388 /* Now insert the node at the correct position into the file's
1389 version list. */
1390 if (!f->version_head) {
1391 /* This is the first node. */
1392 f->version_head = node;
1393 f->version_tail = node;
1394 node->version_prev = NULL;
1395 node->version_next = NULL;
1396 f->highest_version = node->version;
1397 update_name = 1;
1398 f->mode = raw_inode->mode;
1399 f->uid = raw_inode->uid;
1400 f->gid = raw_inode->gid;
1401 f->atime = raw_inode->atime;
1402 f->mtime = raw_inode->mtime;
1403 f->ctime = raw_inode->ctime;
1404 }
1405 else if ((f->highest_version < node->version)
1406 || (node->version == 0)) {
1407 /* Insert at the end of the list. I.e. this node is the
1408 newest one so far. */
1409 node->version_prev = f->version_tail;
1410 node->version_next = NULL;
1411 f->version_tail->version_next = node;
1412 f->version_tail = node;
1413 f->highest_version = node->version;
1414 update_name = 1;
1415 f->pino = raw_inode->pino;
1416 f->mode = raw_inode->mode;
1417 f->uid = raw_inode->uid;
1418 f->gid = raw_inode->gid;
1419 f->atime = raw_inode->atime;
1420 f->mtime = raw_inode->mtime;
1421 f->ctime = raw_inode->ctime;
1422 }
1423 else if (f->version_head->version > node->version) {
1424 /* Insert at the bottom of the list. */
1425 node->version_prev = NULL;
1426 node->version_next = f->version_head;
1427 f->version_head->version_prev = node;
1428 f->version_head = node;
1429 if (!f->name) {
1430 update_name = 1;
1431 }
1432 }
1433 else {
1434 struct jffs_node *n;
1435 int newer_name = 0;
1436 /* Search for the insertion position starting from
1437 the tail (newest node). */
1438 for (n = f->version_tail; n; n = n->version_prev) {
1439 if (n->version < node->version) {
1440 node->version_prev = n;
1441 node->version_next = n->version_next;
1442 node->version_next->version_prev = node;
1443 n->version_next = node;
1444 if (!newer_name) {
1445 update_name = 1;
1446 }
1447 break;
1448 }
1449 if (n->name_size) {
1450 newer_name = 1;
1451 }
1452 }
1453 }
1454
1455 /* Deletion is irreversible. If any 'deleted' node is ever
1456 written, the file is deleted */
1457 if (raw_inode->deleted)
1458 f->deleted = raw_inode->deleted;
1459
1460 /* Perhaps update the name. */
1461 if (raw_inode->nsize && update_name && name && *name && (name != f->name)) {
1462 if (f->name) {
1463 kfree(f->name);
1464 DJM(no_name--);
1465 }
1466 if (!(f->name = kmalloc(raw_inode->nsize + 1,
1467 GFP_KERNEL))) {
1468 return -ENOMEM;
1469 }
1470 DJM(no_name++);
1471 memcpy(f->name, name, raw_inode->nsize);
1472 f->name[raw_inode->nsize] = '\0';
1473 f->nsize = raw_inode->nsize;
1474 D3(printk("jffs_insert_node(): Updated the name of "
1475 "the file to \"%s\".\n", name));
1476 }
1477
1478 if (!c->building_fs) {
1479 D3(printk("jffs_insert_node(): ---------------------------"
1480 "------------------------------------------- 1\n"));
1481 if (insert_into_tree) {
1482 jffs_insert_file_into_tree(f);
1483 }
1484 /* Once upon a time, we would call jffs_possibly_delete_file()
1485 here. That causes an oops if someone's still got the file
1486 open, so now we only do it in jffs_delete_inode()
1487 -- dwmw2
1488 */
1489 if (node->data_size || node->removed_size) {
1490 jffs_update_file(f, node);
1491 }
1492 jffs_remove_redundant_nodes(f);
1493
1494 jffs_garbage_collect_trigger(c);
1495
1496 D3(printk("jffs_insert_node(): ---------------------------"
1497 "------------------------------------------- 2\n"));
1498 }
1499
1500 return 0;
1501} /* jffs_insert_node() */
1502
1503
1504/* Unlink a jffs_node from the version list it is in. */
1505static inline void
1506jffs_unlink_node_from_version_list(struct jffs_file *f,
1507 struct jffs_node *node)
1508{
1509 if (node->version_prev) {
1510 node->version_prev->version_next = node->version_next;
1511 } else {
1512 f->version_head = node->version_next;
1513 }
1514 if (node->version_next) {
1515 node->version_next->version_prev = node->version_prev;
1516 } else {
1517 f->version_tail = node->version_prev;
1518 }
1519}
1520
1521
1522/* Unlink a jffs_node from the range list it is in. */
1523static inline void
1524jffs_unlink_node_from_range_list(struct jffs_file *f, struct jffs_node *node)
1525{
1526 if (node->range_prev) {
1527 node->range_prev->range_next = node->range_next;
1528 }
1529 else {
1530 f->range_head = node->range_next;
1531 }
1532 if (node->range_next) {
1533 node->range_next->range_prev = node->range_prev;
1534 }
1535 else {
1536 f->range_tail = node->range_prev;
1537 }
1538}
1539
1540
1541/* Function used by jffs_remove_redundant_nodes() below. This function
1542 classifies what kind of information a node adds to a file. */
1543static inline __u8
1544jffs_classify_node(struct jffs_node *node)
1545{
1546 __u8 mod_type = JFFS_MODIFY_INODE;
1547
1548 if (node->name_size) {
1549 mod_type |= JFFS_MODIFY_NAME;
1550 }
1551 if (node->data_size || node->removed_size) {
1552 mod_type |= JFFS_MODIFY_DATA;
1553 }
1554 return mod_type;
1555}
1556
1557
1558/* Remove redundant nodes from a file. Mark the on-flash memory
1559 as dirty. */
1560static int
1561jffs_remove_redundant_nodes(struct jffs_file *f)
1562{
1563 struct jffs_node *newest_node;
1564 struct jffs_node *cur;
1565 struct jffs_node *prev;
1566 __u8 newest_type;
1567 __u8 mod_type;
1568 __u8 node_with_name_later = 0;
1569
1570 if (!(newest_node = f->version_tail)) {
1571 return 0;
1572 }
1573
1574 /* What does the `newest_node' modify? */
1575 newest_type = jffs_classify_node(newest_node);
1576 node_with_name_later = newest_type & JFFS_MODIFY_NAME;
1577
1578 D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
1579 "newest_type: %u\n", f->ino, (f->name ? f->name : ""),
1580 newest_type));
1581
1582 /* Traverse the file's nodes and determine which of them that are
1583 superfluous. Yeah, this might look very complex at first
1584 glance but it is actually very simple. */
1585 for (cur = newest_node->version_prev; cur; cur = prev) {
1586 prev = cur->version_prev;
1587 mod_type = jffs_classify_node(cur);
1588 if ((mod_type <= JFFS_MODIFY_INODE)
1589 || ((newest_type & JFFS_MODIFY_NAME)
1590 && (mod_type
1591 <= (JFFS_MODIFY_INODE + JFFS_MODIFY_NAME)))
1592 || (cur->data_size == 0 && cur->removed_size
1593 && !cur->version_prev && node_with_name_later)) {
1594 /* Yes, this node is redundant. Remove it. */
1595 D2(printk("jffs_remove_redundant_nodes(): "
1596 "Removing node: ino: %u, version: %u, "
1597 "mod_type: %u\n", cur->ino, cur->version,
1598 mod_type));
1599 jffs_unlink_node_from_version_list(f, cur);
1600 jffs_fmfree(f->c->fmc, cur->fm, cur);
1601 jffs_free_node(cur);
1602 DJM(no_jffs_node--);
1603 }
1604 else {
1605 node_with_name_later |= (mod_type & JFFS_MODIFY_NAME);
1606 }
1607 }
1608
1609 return 0;
1610}
1611
1612
1613/* Insert a file into the hash table. */
1614static int
1615jffs_insert_file_into_hash(struct jffs_file *f)
1616{
1617 int i = f->ino % f->c->hash_len;
1618
1619 D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f->ino));
1620
1621 list_add(&f->hash, &f->c->hash[i]);
1622 return 0;
1623}
1624
1625
1626/* Insert a file into the file system tree. */
1627int
1628jffs_insert_file_into_tree(struct jffs_file *f)
1629{
1630 struct jffs_file *parent;
1631
1632 D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
1633 (f->name ? f->name : "")));
1634
1635 if (!(parent = jffs_find_file(f->c, f->pino))) {
1636 if (f->pino == 0) {
1637 f->c->root = f;
1638 f->parent = NULL;
1639 f->sibling_prev = NULL;
1640 f->sibling_next = NULL;
1641 return 0;
1642 }
1643 else {
1644 D1(printk("jffs_insert_file_into_tree(): Found "
1645 "inode with no parent and pino == %u\n",
1646 f->pino));
1647 return -1;
1648 }
1649 }
1650 f->parent = parent;
1651 f->sibling_next = parent->children;
1652 if (f->sibling_next) {
1653 f->sibling_next->sibling_prev = f;
1654 }
1655 f->sibling_prev = NULL;
1656 parent->children = f;
1657 return 0;
1658}
1659
1660
1661/* Remove a file from the hash table. */
1662static int
1663jffs_unlink_file_from_hash(struct jffs_file *f)
1664{
1665 D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
1666 "ino %u\n", f, f->ino));
1667
1668 list_del(&f->hash);
1669 return 0;
1670}
1671
1672
1673/* Just remove the file from the parent's children. Don't free
1674 any memory. */
1675int
1676jffs_unlink_file_from_tree(struct jffs_file *f)
1677{
1678 D3(printk("jffs_unlink_file_from_tree(): ino: %d, pino: %d, name: "
1679 "\"%s\"\n", f->ino, f->pino, (f->name ? f->name : "")));
1680
1681 if (f->sibling_prev) {
1682 f->sibling_prev->sibling_next = f->sibling_next;
1683 }
1684 else if (f->parent) {
1685 D3(printk("f->parent=%p\n", f->parent));
1686 f->parent->children = f->sibling_next;
1687 }
1688 if (f->sibling_next) {
1689 f->sibling_next->sibling_prev = f->sibling_prev;
1690 }
1691 return 0;
1692}
1693
1694
1695/* Find a file with its inode number. */
1696struct jffs_file *
1697jffs_find_file(struct jffs_control *c, __u32 ino)
1698{
1699 struct jffs_file *f;
1700 int i = ino % c->hash_len;
1701
1702 D3(printk("jffs_find_file(): ino: %u\n", ino));
1703
1704 list_for_each_entry(f, &c->hash[i], hash) {
1705 if (ino != f->ino)
1706 continue;
1707 D3(printk("jffs_find_file(): Found file with ino "
1708 "%u. (name: \"%s\")\n",
1709 ino, (f->name ? f->name : ""));
1710 );
1711 return f;
1712 }
1713 D3(printk("jffs_find_file(): Didn't find file "
1714 "with ino %u.\n", ino);
1715 );
1716 return NULL;
1717}
1718
1719
1720/* Find a file in a directory. We are comparing the names. */
1721struct jffs_file *
1722jffs_find_child(struct jffs_file *dir, const char *name, int len)
1723{
1724 struct jffs_file *f;
1725
1726 D3(printk("jffs_find_child()\n"));
1727
1728 for (f = dir->children; f; f = f->sibling_next) {
1729 if (!f->deleted && f->name
1730 && !strncmp(f->name, name, len)
1731 && f->name[len] == '\0') {
1732 break;
1733 }
1734 }
1735
1736 D3(if (f) {
1737 printk("jffs_find_child(): Found \"%s\".\n", f->name);
1738 }
1739 else {
1740 char *copy = kmalloc(len + 1, GFP_KERNEL);
1741 if (copy) {
1742 memcpy(copy, name, len);
1743 copy[len] = '\0';
1744 }
1745 printk("jffs_find_child(): Didn't find the file \"%s\".\n",
1746 (copy ? copy : ""));
1747 kfree(copy);
1748 });
1749
1750 return f;
1751}
1752
1753
1754/* Write a raw inode that takes up a certain amount of space in the flash
1755 memory. At the end of the flash device, there is often space that is
1756 impossible to use. At these times we want to mark this space as not
1757 used. In the cases when the amount of space is greater or equal than
1758 a struct jffs_raw_inode, we write a "dummy node" that takes up this
1759 space. The space after the raw inode, if it exists, is left as it is.
1760 Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
1761 we can compute the checksum of it; we don't have to manipulate it any
1762 further.
1763
1764 If the space left on the device is less than the size of a struct
1765 jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
1766 No raw inode is written this time. */
1767static int
1768jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
1769{
1770 struct jffs_fmcontrol *fmc = c->fmc;
1771 int err;
1772
1773 D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
1774 "dirty_fm->size = %u\n",
1775 dirty_fm->offset, dirty_fm->size));
1776
1777 if (dirty_fm->size >= sizeof(struct jffs_raw_inode)) {
1778 struct jffs_raw_inode raw_inode;
1779 memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
1780 raw_inode.magic = JFFS_MAGIC_BITMASK;
1781 raw_inode.dsize = dirty_fm->size
1782 - sizeof(struct jffs_raw_inode);
1783 raw_inode.dchksum = raw_inode.dsize * 0xff;
1784 raw_inode.chksum
1785 = jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
1786
1787 if ((err = flash_safe_write(fmc->mtd,
1788 dirty_fm->offset,
1789 (u_char *)&raw_inode,
1790 sizeof(struct jffs_raw_inode)))
1791 < 0) {
1792 printk(KERN_ERR "JFFS: jffs_write_dummy_node: "
1793 "flash_safe_write failed!\n");
1794 return err;
1795 }
1796 }
1797 else {
1798 flash_safe_acquire(fmc->mtd);
1799 flash_memset(fmc->mtd, dirty_fm->offset, 0, dirty_fm->size);
1800 flash_safe_release(fmc->mtd);
1801 }
1802
1803 D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1804 return 0;
1805}
1806
1807
1808/* Write a raw inode, possibly its name and possibly some data. */
1809int
1810jffs_write_node(struct jffs_control *c, struct jffs_node *node,
1811 struct jffs_raw_inode *raw_inode,
1812 const char *name, const unsigned char *data,
1813 int recoverable,
1814 struct jffs_file *f)
1815{
1816 struct jffs_fmcontrol *fmc = c->fmc;
1817 struct jffs_fm *fm;
1818 struct kvec node_iovec[4];
1819 unsigned long iovec_cnt;
1820
1821 __u32 pos;
1822 int err;
1823 __u32 slack = 0;
1824
1825 __u32 total_name_size = raw_inode->nsize
1826 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1827 __u32 total_data_size = raw_inode->dsize
1828 + JFFS_GET_PAD_BYTES(raw_inode->dsize);
1829 __u32 total_size = sizeof(struct jffs_raw_inode)
1830 + total_name_size + total_data_size;
1831
1832 /* If this node isn't something that will eventually let
1833 GC free even more space, then don't allow it unless
1834 there's at least max_chunk_size space still available
1835 */
1836 if (!recoverable)
1837 slack = fmc->max_chunk_size;
1838
1839
1840 /* Fire the retrorockets and shoot the fruiton torpedoes, sir! */
1841
1842 ASSERT(if (!node) {
1843 printk("jffs_write_node(): node == NULL\n");
1844 return -EINVAL;
1845 });
1846 ASSERT(if (raw_inode && raw_inode->nsize && !name) {
1847 printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
1848 raw_inode->nsize);
1849 return -EINVAL;
1850 });
1851
1852 D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
1853 "total_size = %u\n",
1854 (name ? name : ""), raw_inode->ino,
1855 total_size));
1856
1857 jffs_fm_write_lock(fmc);
1858
1859retry:
1860 fm = NULL;
1861 err = 0;
1862 while (!fm) {
1863
1864 /* Deadlocks suck. */
1865 while(fmc->free_size < fmc->min_free_size + total_size + slack) {
1866 jffs_fm_write_unlock(fmc);
1867 if (!JFFS_ENOUGH_SPACE(c, total_size + slack))
1868 return -ENOSPC;
1869 jffs_fm_write_lock(fmc);
1870 }
1871
1872 /* First try to allocate some flash memory. */
1873 err = jffs_fmalloc(fmc, total_size, node, &fm);
1874
1875 if (err == -ENOSPC) {
1876 /* Just out of space. GC and try again */
1877 if (fmc->dirty_size < fmc->sector_size) {
1878 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1879 "failed, no dirty space to GC\n", fmc,
1880 total_size));
1881 return err;
1882 }
1883
1884 D1(printk(KERN_INFO "jffs_write_node(): Calling jffs_garbage_collect_now()\n"));
1885 jffs_fm_write_unlock(fmc);
1886 if ((err = jffs_garbage_collect_now(c))) {
1887 D(printk("jffs_write_node(): jffs_garbage_collect_now() failed\n"));
1888 return err;
1889 }
1890 jffs_fm_write_lock(fmc);
1891 continue;
1892 }
1893
1894 if (err < 0) {
1895 jffs_fm_write_unlock(fmc);
1896
1897 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1898 "failed!\n", fmc, total_size));
1899 return err;
1900 }
1901
1902 if (!fm->nodes) {
1903 /* The jffs_fm struct that we got is not good enough.
1904 Make that space dirty and try again */
1905 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
1906 kfree(fm);
1907 DJM(no_jffs_fm--);
1908 jffs_fm_write_unlock(fmc);
1909 D(printk("jffs_write_node(): "
1910 "jffs_write_dummy_node(): Failed!\n"));
1911 return err;
1912 }
1913 fm = NULL;
1914 }
1915 } /* while(!fm) */
1916 node->fm = fm;
1917
1918 ASSERT(if (fm->nodes == 0) {
1919 printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
1920 });
1921
1922 pos = node->fm->offset;
1923
1924 /* Increment the version number here. We can't let the caller
1925 set it beforehand, because we might have had to do GC on a node
1926 of this file - and we'd end up reusing version numbers.
1927 */
1928 if (f) {
1929 raw_inode->version = f->highest_version + 1;
1930 D1(printk (KERN_NOTICE "jffs_write_node(): setting version of %s to %d\n", f->name, raw_inode->version));
1931
1932 /* if the file was deleted, set the deleted bit in the raw inode */
1933 if (f->deleted)
1934 raw_inode->deleted = 1;
1935 }
1936
1937 /* Compute the checksum for the data and name chunks. */
1938 raw_inode->dchksum = jffs_checksum(data, raw_inode->dsize);
1939 raw_inode->nchksum = jffs_checksum(name, raw_inode->nsize);
1940
1941 /* The checksum is calculated without the chksum and accurate
1942 fields so set them to zero first. */
1943 raw_inode->accurate = 0;
1944 raw_inode->chksum = 0;
1945 raw_inode->chksum = jffs_checksum(raw_inode,
1946 sizeof(struct jffs_raw_inode));
1947 raw_inode->accurate = 0xff;
1948
1949 D3(printk("jffs_write_node(): About to write this raw inode to the "
1950 "flash at pos 0x%lx:\n", (long)pos));
1951 D3(jffs_print_raw_inode(raw_inode));
1952
1953 /* The actual raw JFFS node */
1954 node_iovec[0].iov_base = (void *) raw_inode;
1955 node_iovec[0].iov_len = (size_t) sizeof(struct jffs_raw_inode);
1956 iovec_cnt = 1;
1957
1958 /* Get name and size if there is one */
1959 if (raw_inode->nsize) {
1960 node_iovec[iovec_cnt].iov_base = (void *) name;
1961 node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->nsize;
1962 iovec_cnt++;
1963
1964 if (JFFS_GET_PAD_BYTES(raw_inode->nsize)) {
1965 static unsigned char allff[3]={255,255,255};
1966 /* Add some extra padding if necessary */
1967 node_iovec[iovec_cnt].iov_base = allff;
1968 node_iovec[iovec_cnt].iov_len =
1969 JFFS_GET_PAD_BYTES(raw_inode->nsize);
1970 iovec_cnt++;
1971 }
1972 }
1973
1974 /* Get data and size if there is any */
1975 if (raw_inode->dsize) {
1976 node_iovec[iovec_cnt].iov_base = (void *) data;
1977 node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->dsize;
1978 iovec_cnt++;
1979 /* No need to pad this because we're not actually putting
1980 anything after it.
1981 */
1982 }
1983
1984 if ((err = flash_safe_writev(fmc->mtd, node_iovec, iovec_cnt,
1985 pos)) < 0) {
1986 jffs_fmfree_partly(fmc, fm, 0);
1987 jffs_fm_write_unlock(fmc);
1988 printk(KERN_ERR "JFFS: jffs_write_node: Failed to write, "
1989 "requested %i, wrote %i\n", total_size, err);
1990 goto retry;
1991 }
1992 if (raw_inode->deleted)
1993 f->deleted = 1;
1994
1995 jffs_fm_write_unlock(fmc);
1996 D3(printk("jffs_write_node(): Leaving...\n"));
1997 return raw_inode->dsize;
1998} /* jffs_write_node() */
1999
2000
2001/* Read data from the node and write it to the buffer. 'node_offset'
2002 is how much we have read from this particular node before and which
2003 shouldn't be read again. 'max_size' is how much space there is in
2004 the buffer. */
2005static int
2006jffs_get_node_data(struct jffs_file *f, struct jffs_node *node,
2007 unsigned char *buf,__u32 node_offset, __u32 max_size)
2008{
2009 struct jffs_fmcontrol *fmc = f->c->fmc;
2010 __u32 pos = node->fm->offset + node->fm_offset + node_offset;
2011 __u32 avail = node->data_size - node_offset;
2012 __u32 r;
2013
2014 D2(printk(" jffs_get_node_data(): file: \"%s\", ino: %u, "
2015 "version: %u, node_offset: %u\n",
2016 f->name, node->ino, node->version, node_offset));
2017
2018 r = min(avail, max_size);
2019 D3(printk(KERN_NOTICE "jffs_get_node_data\n"));
2020 flash_safe_read(fmc->mtd, pos, buf, r);
2021
2022 D3(printk(" jffs_get_node_data(): Read %u byte%s.\n",
2023 r, (r == 1 ? "" : "s")));
2024
2025 return r;
2026}
2027
2028
2029/* Read data from the file's nodes. Write the data to the buffer
2030 'buf'. 'read_offset' tells how much data we should skip. */
2031int
2032jffs_read_data(struct jffs_file *f, unsigned char *buf, __u32 read_offset,
2033 __u32 size)
2034{
2035 struct jffs_node *node;
2036 __u32 read_data = 0; /* Total amount of read data. */
2037 __u32 node_offset = 0;
2038 __u32 pos = 0; /* Number of bytes traversed. */
2039
2040 D2(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
2041 "size = %u\n",
2042 (f->name ? f->name : ""), read_offset, size));
2043
2044 if (read_offset >= f->size) {
2045 D(printk(" f->size: %d\n", f->size));
2046 return 0;
2047 }
2048
2049 /* First find the node to read data from. */
2050 node = f->range_head;
2051 while (pos <= read_offset) {
2052 node_offset = read_offset - pos;
2053 if (node_offset >= node->data_size) {
2054 pos += node->data_size;
2055 node = node->range_next;
2056 }
2057 else {
2058 break;
2059 }
2060 }
2061
2062 /* "Cats are living proof that not everything in nature
2063 has to be useful."
2064 - Garrison Keilor ('97) */
2065
2066 /* Fill the buffer. */
2067 while (node && (read_data < size)) {
2068 int r;
2069 if (!node->fm) {
2070 /* This node does not refer to real data. */
2071 r = min(size - read_data,
2072 node->data_size - node_offset);
2073 memset(&buf[read_data], 0, r);
2074 }
2075 else if ((r = jffs_get_node_data(f, node, &buf[read_data],
2076 node_offset,
2077 size - read_data)) < 0) {
2078 return r;
2079 }
2080 read_data += r;
2081 node_offset = 0;
2082 node = node->range_next;
2083 }
2084 D3(printk(" jffs_read_data(): Read %u bytes.\n", read_data));
2085 return read_data;
2086}
2087
2088
2089/* Used for traversing all nodes in the hash table. */
2090int
2091jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *))
2092{
2093 int pos;
2094 int r;
2095 int result = 0;
2096
2097 for (pos = 0; pos < c->hash_len; pos++) {
2098 struct jffs_file *f, *next;
2099
2100 /* We must do _safe, because 'func' might remove the
2101 current file 'f' from the list. */
2102 list_for_each_entry_safe(f, next, &c->hash[pos], hash) {
2103 r = func(f);
2104 if (r < 0)
2105 return r;
2106 result += r;
2107 }
2108 }
2109
2110 return result;
2111}
2112
2113
2114/* Free all nodes associated with a file. */
2115static int
2116jffs_free_node_list(struct jffs_file *f)
2117{
2118 struct jffs_node *node;
2119 struct jffs_node *p;
2120
2121 D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
2122 f->ino, (f->name ? f->name : "")));
2123 node = f->version_head;
2124 while (node) {
2125 p = node;
2126 node = node->version_next;
2127 jffs_free_node(p);
2128 DJM(no_jffs_node--);
2129 }
2130 return 0;
2131}
2132
2133
2134/* Free a file and its name. */
2135static int
2136jffs_free_file(struct jffs_file *f)
2137{
2138 D3(printk("jffs_free_file: f #%u, \"%s\"\n",
2139 f->ino, (f->name ? f->name : "")));
2140
2141 if (f->name) {
2142 kfree(f->name);
2143 DJM(no_name--);
2144 }
2145 kfree(f);
2146 no_jffs_file--;
2147 return 0;
2148}
2149
2150static long
2151jffs_get_file_count(void)
2152{
2153 return no_jffs_file;
2154}
2155
2156/* See if a file is deleted. If so, mark that file's nodes as obsolete. */
2157int
2158jffs_possibly_delete_file(struct jffs_file *f)
2159{
2160 struct jffs_node *n;
2161
2162 D3(printk("jffs_possibly_delete_file(): ino: %u\n",
2163 f->ino));
2164
2165 ASSERT(if (!f) {
2166 printk(KERN_ERR "jffs_possibly_delete_file(): f == NULL\n");
2167 return -1;
2168 });
2169
2170 if (f->deleted) {
2171 /* First try to remove all older versions. Commence with
2172 the oldest node. */
2173 for (n = f->version_head; n; n = n->version_next) {
2174 if (!n->fm) {
2175 continue;
2176 }
2177 if (jffs_fmfree(f->c->fmc, n->fm, n) < 0) {
2178 break;
2179 }
2180 }
2181 /* Unlink the file from the filesystem. */
2182 if (!f->c->building_fs) {
2183 jffs_unlink_file_from_tree(f);
2184 }
2185 jffs_unlink_file_from_hash(f);
2186 jffs_free_node_list(f);
2187 jffs_free_file(f);
2188 }
2189 return 0;
2190}
2191
2192
2193/* Used in conjunction with jffs_foreach_file() to count the number
2194 of files in the file system. */
2195int
2196jffs_file_count(struct jffs_file *f)
2197{
2198 return 1;
2199}
2200
2201
2202/* Build up a file's range list from scratch by going through the
2203 version list. */
2204static int
2205jffs_build_file(struct jffs_file *f)
2206{
2207 struct jffs_node *n;
2208
2209 D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
2210 f->ino, (f->name ? f->name : "")));
2211
2212 for (n = f->version_head; n; n = n->version_next) {
2213 jffs_update_file(f, n);
2214 }
2215 return 0;
2216}
2217
2218
2219/* Remove an amount of data from a file. If this amount of data is
2220 zero, that could mean that a node should be split in two parts.
2221 We remove or change the appropriate nodes in the lists.
2222
2223 Starting offset of area to be removed is node->data_offset,
2224 and the length of the area is in node->removed_size. */
2225static int
2226jffs_delete_data(struct jffs_file *f, struct jffs_node *node)
2227{
2228 struct jffs_node *n;
2229 __u32 offset = node->data_offset;
2230 __u32 remove_size = node->removed_size;
2231
2232 D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
2233 offset, remove_size));
2234
2235 if (remove_size == 0
2236 && f->range_tail
2237 && f->range_tail->data_offset + f->range_tail->data_size
2238 == offset) {
2239 /* A simple append; nothing to remove or no node to split. */
2240 return 0;
2241 }
2242
2243 /* Find the node where we should begin the removal. */
2244 for (n = f->range_head; n; n = n->range_next) {
2245 if (n->data_offset + n->data_size > offset) {
2246 break;
2247 }
2248 }
2249 if (!n) {
2250 /* If there's no data in the file there's no data to
2251 remove either. */
2252 return 0;
2253 }
2254
2255 if (n->data_offset > offset) {
2256 /* XXX: Not implemented yet. */
2257 printk(KERN_WARNING "JFFS: An unexpected situation "
2258 "occurred in jffs_delete_data.\n");
2259 }
2260 else if (n->data_offset < offset) {
2261 /* See if the node has to be split into two parts. */
2262 if (n->data_offset + n->data_size > offset + remove_size) {
2263 /* Do the split. */
2264 struct jffs_node *new_node;
2265 D3(printk("jffs_delete_data(): Split node with "
2266 "version number %u.\n", n->version));
2267
2268 if (!(new_node = jffs_alloc_node())) {
2269 D(printk("jffs_delete_data(): -ENOMEM\n"));
2270 return -ENOMEM;
2271 }
2272 DJM(no_jffs_node++);
2273
2274 new_node->ino = n->ino;
2275 new_node->version = n->version;
2276 new_node->data_offset = offset;
2277 new_node->data_size = n->data_size - (remove_size + (offset - n->data_offset));
2278 new_node->fm_offset = n->fm_offset + (remove_size + (offset - n->data_offset));
2279 new_node->name_size = n->name_size;
2280 new_node->fm = n->fm;
2281 new_node->version_prev = n;
2282 new_node->version_next = n->version_next;
2283 if (new_node->version_next) {
2284 new_node->version_next->version_prev
2285 = new_node;
2286 }
2287 else {
2288 f->version_tail = new_node;
2289 }
2290 n->version_next = new_node;
2291 new_node->range_prev = n;
2292 new_node->range_next = n->range_next;
2293 if (new_node->range_next) {
2294 new_node->range_next->range_prev = new_node;
2295 }
2296 else {
2297 f->range_tail = new_node;
2298 }
2299 /* A very interesting can of worms. */
2300 n->range_next = new_node;
2301 n->data_size = offset - n->data_offset;
2302 if (new_node->fm)
2303 jffs_add_node(new_node);
2304 else {
2305 D1(printk(KERN_WARNING "jffs_delete_data(): Splitting an empty node (file hold).\n!"));
2306 D1(printk(KERN_WARNING "FIXME: Did dwmw2 do the right thing here?\n"));
2307 }
2308 n = new_node->range_next;
2309 remove_size = 0;
2310 }
2311 else {
2312 /* No. No need to split the node. Just remove
2313 the end of the node. */
2314 int r = min(n->data_offset + n->data_size
2315 - offset, remove_size);
2316 n->data_size -= r;
2317 remove_size -= r;
2318 n = n->range_next;
2319 }
2320 }
2321
2322 /* Remove as many nodes as necessary. */
2323 while (n && remove_size) {
2324 if (n->data_size <= remove_size) {
2325 struct jffs_node *p = n;
2326 remove_size -= n->data_size;
2327 n = n->range_next;
2328 D3(printk("jffs_delete_data(): Removing node: "
2329 "ino: %u, version: %u%s\n",
2330 p->ino, p->version,
2331 (p->fm ? "" : " (virtual)")));
2332 if (p->fm) {
2333 jffs_fmfree(f->c->fmc, p->fm, p);
2334 }
2335 jffs_unlink_node_from_range_list(f, p);
2336 jffs_unlink_node_from_version_list(f, p);
2337 jffs_free_node(p);
2338 DJM(no_jffs_node--);
2339 }
2340 else {
2341 n->data_size -= remove_size;
2342 n->fm_offset += remove_size;
2343 n->data_offset -= (node->removed_size - remove_size);
2344 n = n->range_next;
2345 break;
2346 }
2347 }
2348
2349 /* Adjust the following nodes' information about offsets etc. */
2350 while (n && node->removed_size) {
2351 n->data_offset -= node->removed_size;
2352 n = n->range_next;
2353 }
2354
2355 if (node->removed_size > (f->size - node->data_offset)) {
2356 /* It's possible that the removed_size is in fact
2357 * greater than the amount of data we actually thought
2358 * were present in the first place - some of the nodes
2359 * which this node originally obsoleted may already have
2360 * been deleted from the flash by subsequent garbage
2361 * collection.
2362 *
2363 * If this is the case, don't let f->size go negative.
2364 * Bad things would happen :)
2365 */
2366 f->size = node->data_offset;
2367 } else {
2368 f->size -= node->removed_size;
2369 }
2370 D3(printk("jffs_delete_data(): f->size = %d\n", f->size));
2371 return 0;
2372} /* jffs_delete_data() */
2373
2374
2375/* Insert some data into a file. Prior to the call to this function,
2376 jffs_delete_data should be called. */
2377static int
2378jffs_insert_data(struct jffs_file *f, struct jffs_node *node)
2379{
2380 D3(printk("jffs_insert_data(): node->data_offset = %u, "
2381 "node->data_size = %u, f->size = %u\n",
2382 node->data_offset, node->data_size, f->size));
2383
2384 /* Find the position where we should insert data. */
2385 retry:
2386 if (node->data_offset == f->size) {
2387 /* A simple append. This is the most common operation. */
2388 node->range_next = NULL;
2389 node->range_prev = f->range_tail;
2390 if (node->range_prev) {
2391 node->range_prev->range_next = node;
2392 }
2393 f->range_tail = node;
2394 f->size += node->data_size;
2395 if (!f->range_head) {
2396 f->range_head = node;
2397 }
2398 }
2399 else if (node->data_offset < f->size) {
2400 /* Trying to insert data into the middle of the file. This
2401 means no problem because jffs_delete_data() has already
2402 prepared the range list for us. */
2403 struct jffs_node *n;
2404
2405 /* Find the correct place for the insertion and then insert
2406 the node. */
2407 for (n = f->range_head; n; n = n->range_next) {
2408 D2(printk("Cool stuff's happening!\n"));
2409
2410 if (n->data_offset == node->data_offset) {
2411 node->range_prev = n->range_prev;
2412 if (node->range_prev) {
2413 node->range_prev->range_next = node;
2414 }
2415 else {
2416 f->range_head = node;
2417 }
2418 node->range_next = n;
2419 n->range_prev = node;
2420 break;
2421 }
2422 ASSERT(else if (n->data_offset + n->data_size >
2423 node->data_offset) {
2424 printk(KERN_ERR "jffs_insert_data(): "
2425 "Couldn't find a place to insert "
2426 "the data!\n");
2427 return -1;
2428 });
2429 }
2430
2431 /* Adjust later nodes' offsets etc. */
2432 n = node->range_next;
2433 while (n) {
2434 n->data_offset += node->data_size;
2435 n = n->range_next;
2436 }
2437 f->size += node->data_size;
2438 }
2439 else if (node->data_offset > f->size) {
2440 /* Okay. This is tricky. This means that we want to insert
2441 data at a place that is beyond the limits of the file as
2442 it is constructed right now. This is actually a common
2443 event that for instance could occur during the mounting
2444 of the file system if a large file have been truncated,
2445 rewritten and then only partially garbage collected. */
2446
2447 struct jffs_node *n;
2448
2449 /* We need a place holder for the data that is missing in
2450 front of this insertion. This "virtual node" will not
2451 be associated with any space on the flash device. */
2452 struct jffs_node *virtual_node;
2453 if (!(virtual_node = jffs_alloc_node())) {
2454 return -ENOMEM;
2455 }
2456
2457 D(printk("jffs_insert_data: Inserting a virtual node.\n"));
2458 D(printk(" node->data_offset = %u\n", node->data_offset));
2459 D(printk(" f->size = %u\n", f->size));
2460
2461 virtual_node->ino = node->ino;
2462 virtual_node->version = node->version;
2463 virtual_node->removed_size = 0;
2464 virtual_node->fm_offset = 0;
2465 virtual_node->name_size = 0;
2466 virtual_node->fm = NULL; /* This is a virtual data holder. */
2467 virtual_node->version_prev = NULL;
2468 virtual_node->version_next = NULL;
2469 virtual_node->range_next = NULL;
2470
2471 /* Are there any data at all in the file yet? */
2472 if (f->range_head) {
2473 virtual_node->data_offset
2474 = f->range_tail->data_offset
2475 + f->range_tail->data_size;
2476 virtual_node->data_size
2477 = node->data_offset - virtual_node->data_offset;
2478 virtual_node->range_prev = f->range_tail;
2479 f->range_tail->range_next = virtual_node;
2480 }
2481 else {
2482 virtual_node->data_offset = 0;
2483 virtual_node->data_size = node->data_offset;
2484 virtual_node->range_prev = NULL;
2485 f->range_head = virtual_node;
2486 }
2487
2488 f->range_tail = virtual_node;
2489 f->size += virtual_node->data_size;
2490
2491 /* Insert this virtual node in the version list as well. */
2492 for (n = f->version_head; n ; n = n->version_next) {
2493 if (n->version == virtual_node->version) {
2494 virtual_node->version_prev = n->version_prev;
2495 n->version_prev = virtual_node;
2496 if (virtual_node->version_prev) {
2497 virtual_node->version_prev
2498 ->version_next = virtual_node;
2499 }
2500 else {
2501 f->version_head = virtual_node;
2502 }
2503 virtual_node->version_next = n;
2504 break;
2505 }
2506 }
2507
2508 D(jffs_print_node(virtual_node));
2509
2510 /* Make a new try to insert the node. */
2511 goto retry;
2512 }
2513
2514 D3(printk("jffs_insert_data(): f->size = %d\n", f->size));
2515 return 0;
2516}
2517
2518
2519/* A new node (with data) has been added to the file and now the range
2520 list has to be modified. */
2521static int
2522jffs_update_file(struct jffs_file *f, struct jffs_node *node)
2523{
2524 int err;
2525
2526 D3(printk("jffs_update_file(): ino: %u, version: %u\n",
2527 f->ino, node->version));
2528
2529 if (node->data_size == 0) {
2530 if (node->removed_size == 0) {
2531 /* data_offset == X */
2532 /* data_size == 0 */
2533 /* remove_size == 0 */
2534 }
2535 else {
2536 /* data_offset == X */
2537 /* data_size == 0 */
2538 /* remove_size != 0 */
2539 if ((err = jffs_delete_data(f, node)) < 0) {
2540 return err;
2541 }
2542 }
2543 }
2544 else {
2545 /* data_offset == X */
2546 /* data_size != 0 */
2547 /* remove_size == Y */
2548 if ((err = jffs_delete_data(f, node)) < 0) {
2549 return err;
2550 }
2551 if ((err = jffs_insert_data(f, node)) < 0) {
2552 return err;
2553 }
2554 }
2555 return 0;
2556}
2557
2558/* Print the contents of a file. */
2559#if 0
2560int
2561jffs_print_file(struct jffs_file *f)
2562{
2563 D(int i);
2564 D(printk("jffs_file: 0x%p\n", f));
2565 D(printk("{\n"));
2566 D(printk(" 0x%08x, /* ino */\n", f->ino));
2567 D(printk(" 0x%08x, /* pino */\n", f->pino));
2568 D(printk(" 0x%08x, /* mode */\n", f->mode));
2569 D(printk(" 0x%04x, /* uid */\n", f->uid));
2570 D(printk(" 0x%04x, /* gid */\n", f->gid));
2571 D(printk(" 0x%08x, /* atime */\n", f->atime));
2572 D(printk(" 0x%08x, /* mtime */\n", f->mtime));
2573 D(printk(" 0x%08x, /* ctime */\n", f->ctime));
2574 D(printk(" 0x%02x, /* nsize */\n", f->nsize));
2575 D(printk(" 0x%02x, /* nlink */\n", f->nlink));
2576 D(printk(" 0x%02x, /* deleted */\n", f->deleted));
2577 D(printk(" \"%s\", ", (f->name ? f->name : "")));
2578 D(for (i = strlen(f->name ? f->name : ""); i < 8; ++i) {
2579 printk(" ");
2580 });
2581 D(printk("/* name */\n"));
2582 D(printk(" 0x%08x, /* size */\n", f->size));
2583 D(printk(" 0x%08x, /* highest_version */\n",
2584 f->highest_version));
2585 D(printk(" 0x%p, /* c */\n", f->c));
2586 D(printk(" 0x%p, /* parent */\n", f->parent));
2587 D(printk(" 0x%p, /* children */\n", f->children));
2588 D(printk(" 0x%p, /* sibling_prev */\n", f->sibling_prev));
2589 D(printk(" 0x%p, /* sibling_next */\n", f->sibling_next));
2590 D(printk(" 0x%p, /* hash_prev */\n", f->hash.prev));
2591 D(printk(" 0x%p, /* hash_next */\n", f->hash.next));
2592 D(printk(" 0x%p, /* range_head */\n", f->range_head));
2593 D(printk(" 0x%p, /* range_tail */\n", f->range_tail));
2594 D(printk(" 0x%p, /* version_head */\n", f->version_head));
2595 D(printk(" 0x%p, /* version_tail */\n", f->version_tail));
2596 D(printk("}\n"));
2597 return 0;
2598}
2599#endif /* 0 */
2600
2601void
2602jffs_print_hash_table(struct jffs_control *c)
2603{
2604 int i;
2605
2606 printk("JFFS: Dumping the file system's hash table...\n");
2607 for (i = 0; i < c->hash_len; i++) {
2608 struct jffs_file *f;
2609 list_for_each_entry(f, &c->hash[i], hash) {
2610 printk("*** c->hash[%u]: \"%s\" "
2611 "(ino: %u, pino: %u)\n",
2612 i, (f->name ? f->name : ""),
2613 f->ino, f->pino);
2614 }
2615 }
2616}
2617
2618
2619void
2620jffs_print_tree(struct jffs_file *first_file, int indent)
2621{
2622 struct jffs_file *f;
2623 char *space;
2624 int dir;
2625
2626 if (!first_file) {
2627 return;
2628 }
2629
2630 if (!(space = kmalloc(indent + 1, GFP_KERNEL))) {
2631 printk("jffs_print_tree(): Out of memory!\n");
2632 return;
2633 }
2634
2635 memset(space, ' ', indent);
2636 space[indent] = '\0';
2637
2638 for (f = first_file; f; f = f->sibling_next) {
2639 dir = S_ISDIR(f->mode);
2640 printk("%s%s%s (ino: %u, highest_version: %u, size: %u)\n",
2641 space, (f->name ? f->name : ""), (dir ? "/" : ""),
2642 f->ino, f->highest_version, f->size);
2643 if (dir) {
2644 jffs_print_tree(f->children, indent + 2);
2645 }
2646 }
2647
2648 kfree(space);
2649}
2650
2651
2652#if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
2653void
2654jffs_print_memory_allocation_statistics(void)
2655{
2656 static long printout;
2657 printk("________ Memory printout #%ld ________\n", ++printout);
2658 printk("no_jffs_file = %ld\n", no_jffs_file);
2659 printk("no_jffs_node = %ld\n", no_jffs_node);
2660 printk("no_jffs_control = %ld\n", no_jffs_control);
2661 printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode);
2662 printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref);
2663 printk("no_jffs_fm = %ld\n", no_jffs_fm);
2664 printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol);
2665 printk("no_hash = %ld\n", no_hash);
2666 printk("no_name = %ld\n", no_name);
2667 printk("\n");
2668}
2669#endif
2670
2671
2672/* Rewrite `size' bytes, and begin at `node'. */
2673static int
2674jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, __u32 size)
2675{
2676 struct jffs_control *c = f->c;
2677 struct jffs_fmcontrol *fmc = c->fmc;
2678 struct jffs_raw_inode raw_inode;
2679 struct jffs_node *new_node;
2680 struct jffs_fm *fm;
2681 __u32 pos;
2682 __u32 pos_dchksum;
2683 __u32 total_name_size;
2684 __u32 total_data_size;
2685 __u32 total_size;
2686 int err;
2687
2688 D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2689 f->ino, (f->name ? f->name : "(null)"), size));
2690
2691 /* Create and initialize the new node. */
2692 if (!(new_node = jffs_alloc_node())) {
2693 D(printk("jffs_rewrite_data(): "
2694 "Failed to allocate node.\n"));
2695 return -ENOMEM;
2696 }
2697 DJM(no_jffs_node++);
2698 new_node->data_offset = node->data_offset;
2699 new_node->removed_size = size;
2700 total_name_size = JFFS_PAD(f->nsize);
2701 total_data_size = JFFS_PAD(size);
2702 total_size = sizeof(struct jffs_raw_inode)
2703 + total_name_size + total_data_size;
2704 new_node->fm_offset = sizeof(struct jffs_raw_inode)
2705 + total_name_size;
2706
2707retry:
2708 jffs_fm_write_lock(fmc);
2709 err = 0;
2710
2711 if ((err = jffs_fmalloc(fmc, total_size, new_node, &fm)) < 0) {
2712 DJM(no_jffs_node--);
2713 jffs_fm_write_unlock(fmc);
2714 D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2715 jffs_free_node(new_node);
2716 return err;
2717 }
2718 else if (!fm->nodes) {
2719 /* The jffs_fm struct that we got is not big enough. */
2720 /* This should never happen, because we deal with this case
2721 in jffs_garbage_collect_next().*/
2722 printk(KERN_WARNING "jffs_rewrite_data(): Allocated node is too small (%d bytes of %d)\n", fm->size, total_size);
2723 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
2724 D(printk("jffs_rewrite_data(): "
2725 "jffs_write_dummy_node() Failed!\n"));
2726 } else {
2727 err = -ENOSPC;
2728 }
2729 DJM(no_jffs_fm--);
2730 jffs_fm_write_unlock(fmc);
2731 kfree(fm);
2732
2733 return err;
2734 }
2735 new_node->fm = fm;
2736
2737 /* Initialize the raw inode. */
2738 raw_inode.magic = JFFS_MAGIC_BITMASK;
2739 raw_inode.ino = f->ino;
2740 raw_inode.pino = f->pino;
2741 raw_inode.version = f->highest_version + 1;
2742 raw_inode.mode = f->mode;
2743 raw_inode.uid = f->uid;
2744 raw_inode.gid = f->gid;
2745 raw_inode.atime = f->atime;
2746 raw_inode.mtime = f->mtime;
2747 raw_inode.ctime = f->ctime;
2748 raw_inode.offset = node->data_offset;
2749 raw_inode.dsize = size;
2750 raw_inode.rsize = size;
2751 raw_inode.nsize = f->nsize;
2752 raw_inode.nlink = f->nlink;
2753 raw_inode.spare = 0;
2754 raw_inode.rename = 0;
2755 raw_inode.deleted = f->deleted;
2756 raw_inode.accurate = 0xff;
2757 raw_inode.dchksum = 0;
2758 raw_inode.nchksum = 0;
2759
2760 pos = new_node->fm->offset;
2761 pos_dchksum = pos +JFFS_RAW_INODE_DCHKSUM_OFFSET;
2762
2763 D3(printk("jffs_rewrite_data(): Writing this raw inode "
2764 "to pos 0x%ul.\n", pos));
2765 D3(jffs_print_raw_inode(&raw_inode));
2766
2767 if ((err = flash_safe_write(fmc->mtd, pos,
2768 (u_char *) &raw_inode,
2769 sizeof(struct jffs_raw_inode)
2770 - sizeof(__u32)
2771 - sizeof(__u16) - sizeof(__u16))) < 0) {
2772 jffs_fmfree_partly(fmc, fm,
2773 total_name_size + total_data_size);
2774 jffs_fm_write_unlock(fmc);
2775 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2776 "rewrite. (raw inode)\n");
2777 printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
2778 "rewrite. (raw inode)\n");
2779 goto retry;
2780 }
2781 pos += sizeof(struct jffs_raw_inode);
2782
2783 /* Write the name to the flash memory. */
2784 if (f->nsize) {
2785 D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
2786 "pos 0x%ul.\n", f->name, (unsigned int) pos));
2787 if ((err = flash_safe_write(fmc->mtd, pos,
2788 (u_char *)f->name,
2789 f->nsize)) < 0) {
2790 jffs_fmfree_partly(fmc, fm, total_data_size);
2791 jffs_fm_write_unlock(fmc);
2792 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write "
2793 "error during rewrite. (name)\n");
2794 printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
2795 "rewrite. (name)\n");
2796 goto retry;
2797 }
2798 pos += total_name_size;
2799 raw_inode.nchksum = jffs_checksum(f->name, f->nsize);
2800 }
2801
2802 /* Write the data. */
2803 if (size) {
2804 int r;
2805 unsigned char *page;
2806 __u32 offset = node->data_offset;
2807
2808 if (!(page = (unsigned char *)__get_free_page(GFP_KERNEL))) {
2809 jffs_fmfree_partly(fmc, fm, 0);
2810 return -1;
2811 }
2812
2813 while (size) {
2814 __u32 s = min(size, (__u32)PAGE_SIZE);
2815 if ((r = jffs_read_data(f, (char *)page,
2816 offset, s)) < s) {
2817 free_page((unsigned long)page);
2818 jffs_fmfree_partly(fmc, fm, 0);
2819 jffs_fm_write_unlock(fmc);
2820 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2821 "jffs_read_data() "
2822 "failed! (r = %d)\n", r);
2823 return -1;
2824 }
2825 if ((err = flash_safe_write(fmc->mtd,
2826 pos, page, r)) < 0) {
2827 free_page((unsigned long)page);
2828 jffs_fmfree_partly(fmc, fm, 0);
2829 jffs_fm_write_unlock(fmc);
2830 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2831 "Write error during rewrite. "
2832 "(data)\n");
2833 goto retry;
2834 }
2835 pos += r;
2836 size -= r;
2837 offset += r;
2838 raw_inode.dchksum += jffs_checksum(page, r);
2839 }
2840
2841 free_page((unsigned long)page);
2842 }
2843
2844 raw_inode.accurate = 0;
2845 raw_inode.chksum = jffs_checksum(&raw_inode,
2846 sizeof(struct jffs_raw_inode)
2847 - sizeof(__u16));
2848
2849 /* Add the checksum. */
2850 if ((err
2851 = flash_safe_write(fmc->mtd, pos_dchksum,
2852 &((u_char *)
2853 &raw_inode)[JFFS_RAW_INODE_DCHKSUM_OFFSET],
2854 sizeof(__u32) + sizeof(__u16)
2855 + sizeof(__u16))) < 0) {
2856 jffs_fmfree_partly(fmc, fm, 0);
2857 jffs_fm_write_unlock(fmc);
2858 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2859 "rewrite. (checksum)\n");
2860 goto retry;
2861 }
2862
2863 /* Now make the file system aware of the newly written node. */
2864 jffs_insert_node(c, f, &raw_inode, f->name, new_node);
2865 jffs_fm_write_unlock(fmc);
2866
2867 D3(printk("jffs_rewrite_data(): Leaving...\n"));
2868 return 0;
2869} /* jffs_rewrite_data() */
2870
2871
2872/* jffs_garbage_collect_next implements one step in the garbage collect
2873 process and is often called multiple times at each occasion of a
2874 garbage collect. */
2875
2876static int
2877jffs_garbage_collect_next(struct jffs_control *c)
2878{
2879 struct jffs_fmcontrol *fmc = c->fmc;
2880 struct jffs_node *node;
2881 struct jffs_file *f;
2882 int err = 0;
2883 __u32 size;
2884 __u32 data_size;
2885 __u32 total_name_size;
2886 __u32 extra_available;
2887 __u32 space_needed;
2888 __u32 free_chunk_size1 = jffs_free_size1(fmc);
2889 D2(__u32 free_chunk_size2 = jffs_free_size2(fmc));
2890
2891 /* Get the oldest node in the flash. */
2892 node = jffs_get_oldest_node(fmc);
2893 ASSERT(if (!node) {
2894 printk(KERN_ERR "JFFS: jffs_garbage_collect_next: "
2895 "No oldest node found!\n");
2896 err = -1;
2897 goto jffs_garbage_collect_next_end;
2898
2899
2900 });
2901
2902 /* Find its corresponding file too. */
2903 f = jffs_find_file(c, node->ino);
2904
2905 if (!f) {
2906 printk (KERN_ERR "JFFS: jffs_garbage_collect_next: "
2907 "No file to garbage collect! "
2908 "(ino = 0x%08x)\n", node->ino);
2909 /* FIXME: Free the offending node and recover. */
2910 err = -1;
2911 goto jffs_garbage_collect_next_end;
2912 }
2913
2914 /* We always write out the name. Theoretically, we don't need
2915 to, but for now it's easier - because otherwise we'd have
2916 to keep track of how many times the current name exists on
2917 the flash and make sure it never reaches zero.
2918
2919 The current approach means that would be possible to cause
2920 the GC to end up eating its tail by writing lots of nodes
2921 with no name for it to garbage-collect. Hence the change in
2922 inode.c to write names with _every_ node.
2923
2924 It sucks, but it _should_ work.
2925 */
2926 total_name_size = JFFS_PAD(f->nsize);
2927
2928 D1(printk("jffs_garbage_collect_next(): \"%s\", "
2929 "ino: %u, version: %u, location 0x%x, dsize %u\n",
2930 (f->name ? f->name : ""), node->ino, node->version,
2931 node->fm->offset, node->data_size));
2932
2933 /* Compute how many data it's possible to rewrite at the moment. */
2934 data_size = f->size - node->data_offset;
2935
2936 /* And from that, the total size of the chunk we want to write */
2937 size = sizeof(struct jffs_raw_inode) + total_name_size
2938 + data_size + JFFS_GET_PAD_BYTES(data_size);
2939
2940 /* If that's more than max_chunk_size, reduce it accordingly */
2941 if (size > fmc->max_chunk_size) {
2942 size = fmc->max_chunk_size;
2943 data_size = size - sizeof(struct jffs_raw_inode)
2944 - total_name_size;
2945 }
2946
2947 /* If we're asking to take up more space than free_chunk_size1
2948 but we _could_ fit in it, shrink accordingly.
2949 */
2950 if (size > free_chunk_size1) {
2951
2952 if (free_chunk_size1 <
2953 (sizeof(struct jffs_raw_inode) + total_name_size + BLOCK_SIZE)){
2954 /* The space left is too small to be of any
2955 use really. */
2956 struct jffs_fm *dirty_fm
2957 = jffs_fmalloced(fmc,
2958 fmc->tail->offset + fmc->tail->size,
2959 free_chunk_size1, NULL);
2960 if (!dirty_fm) {
2961 printk(KERN_ERR "JFFS: "
2962 "jffs_garbage_collect_next: "
2963 "Failed to allocate `dirty' "
2964 "flash memory!\n");
2965 err = -1;
2966 goto jffs_garbage_collect_next_end;
2967 }
2968 D1(printk("Dirtying end of flash - too small\n"));
2969 jffs_write_dummy_node(c, dirty_fm);
2970 err = 0;
2971 goto jffs_garbage_collect_next_end;
2972 }
2973 D1(printk("Reducing size of new node from %d to %d to avoid "
2974 " exceeding free_chunk_size1\n",
2975 size, free_chunk_size1));
2976
2977 size = free_chunk_size1;
2978 data_size = size - sizeof(struct jffs_raw_inode)
2979 - total_name_size;
2980 }
2981
2982
2983 /* Calculate the amount of space needed to hold the nodes
2984 which are remaining in the tail */
2985 space_needed = fmc->min_free_size - (node->fm->offset % fmc->sector_size);
2986
2987 /* From that, calculate how much 'extra' space we can use to
2988 increase the size of the node we're writing from the size
2989 of the node we're obsoleting
2990 */
2991 if (space_needed > fmc->free_size) {
2992 /* If we've gone below min_free_size for some reason,
2993 don't fuck up. This is why we have
2994 min_free_size > sector_size. Whinge about it though,
2995 just so I can convince myself my maths is right.
2996 */
2997 D1(printk(KERN_WARNING "jffs_garbage_collect_next(): "
2998 "space_needed %d exceeded free_size %d\n",
2999 space_needed, fmc->free_size));
3000 extra_available = 0;
3001 } else {
3002 extra_available = fmc->free_size - space_needed;
3003 }
3004
3005 /* Check that we don't use up any more 'extra' space than
3006 what's available */
3007 if (size > JFFS_PAD(node->data_size) + total_name_size +
3008 sizeof(struct jffs_raw_inode) + extra_available) {
3009 D1(printk("Reducing size of new node from %d to %ld to avoid "
3010 "catching our tail\n", size,
3011 (long) (JFFS_PAD(node->data_size) + JFFS_PAD(node->name_size) +
3012 sizeof(struct jffs_raw_inode) + extra_available)));
3013 D1(printk("space_needed = %d, extra_available = %d\n",
3014 space_needed, extra_available));
3015
3016 size = JFFS_PAD(node->data_size) + total_name_size +
3017 sizeof(struct jffs_raw_inode) + extra_available;
3018 data_size = size - sizeof(struct jffs_raw_inode)
3019 - total_name_size;
3020 };
3021
3022 D2(printk(" total_name_size: %u\n", total_name_size));
3023 D2(printk(" data_size: %u\n", data_size));
3024 D2(printk(" size: %u\n", size));
3025 D2(printk(" f->nsize: %u\n", f->nsize));
3026 D2(printk(" f->size: %u\n", f->size));
3027 D2(printk(" node->data_offset: %u\n", node->data_offset));
3028 D2(printk(" free_chunk_size1: %u\n", free_chunk_size1));
3029 D2(printk(" free_chunk_size2: %u\n", free_chunk_size2));
3030 D2(printk(" node->fm->offset: 0x%08x\n", node->fm->offset));
3031
3032 if ((err = jffs_rewrite_data(f, node, data_size))) {
3033 printk(KERN_WARNING "jffs_rewrite_data() failed: %d\n", err);
3034 return err;
3035 }
3036
3037jffs_garbage_collect_next_end:
3038 D3(printk("jffs_garbage_collect_next: Leaving...\n"));
3039 return err;
3040} /* jffs_garbage_collect_next */
3041
3042
3043/* If an obsolete node is partly going to be erased due to garbage
3044 collection, the part that isn't going to be erased must be filled
3045 with zeroes so that the scan of the flash will work smoothly next
3046 time. (The data in the file could for instance be a JFFS image
3047 which could cause enormous confusion during a scan of the flash
3048 device if we didn't do this.)
3049 There are two phases in this procedure: First, the clearing of
3050 the name and data parts of the node. Second, possibly also clearing
3051 a part of the raw inode as well. If the box is power cycled during
3052 the first phase, only the checksum of this node-to-be-cleared-at-
3053 the-end will be wrong. If the box is power cycled during, or after,
3054 the clearing of the raw inode, the information like the length of
3055 the name and data parts are zeroed. The next time the box is
3056 powered up, the scanning algorithm manages this faulty data too
3057 because:
3058
3059 - The checksum is invalid and thus the raw inode must be discarded
3060 in any case.
3061 - If the lengths of the data part or the name part are zeroed, the
3062 scanning just continues after the raw inode. But after the inode
3063 the scanning procedure just finds zeroes which is the same as
3064 dirt.
3065
3066 So, in the end, this could never fail. :-) Even if it does fail,
3067 the scanning algorithm should manage that too. */
3068
3069static int
3070jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
3071{
3072 struct jffs_fm *fm;
3073 struct jffs_fmcontrol *fmc = c->fmc;
3074 __u32 zero_offset;
3075 __u32 zero_size;
3076 __u32 zero_offset_data;
3077 __u32 zero_size_data;
3078 __u32 cutting_raw_inode = 0;
3079
3080 if (!(fm = jffs_cut_node(fmc, erase_size))) {
3081 D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
3082 return 0;
3083 }
3084
3085 /* Where and how much shall we clear? */
3086 zero_offset = fmc->head->offset + erase_size;
3087 zero_size = fm->offset + fm->size - zero_offset;
3088
3089 /* Do we have to clear the raw_inode explicitly? */
3090 if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
3091 cutting_raw_inode = sizeof(struct jffs_raw_inode)
3092 - (fm->size - zero_size);
3093 }
3094
3095 /* First, clear the name and data fields. */
3096 zero_offset_data = zero_offset + cutting_raw_inode;
3097 zero_size_data = zero_size - cutting_raw_inode;
3098 flash_safe_acquire(fmc->mtd);
3099 flash_memset(fmc->mtd, zero_offset_data, 0, zero_size_data);
3100 flash_safe_release(fmc->mtd);
3101
3102 /* Should we clear a part of the raw inode? */
3103 if (cutting_raw_inode) {
3104 /* I guess it is ok to clear the raw inode in this order. */
3105 flash_safe_acquire(fmc->mtd);
3106 flash_memset(fmc->mtd, zero_offset, 0,
3107 cutting_raw_inode);
3108 flash_safe_release(fmc->mtd);
3109 }
3110
3111 return 0;
3112} /* jffs_clear_end_of_node() */
3113
3114/* Try to erase as much as possible of the dirt in the flash memory. */
3115static long
3116jffs_try_to_erase(struct jffs_control *c)
3117{
3118 struct jffs_fmcontrol *fmc = c->fmc;
3119 long erase_size;
3120 int err;
3121 __u32 offset;
3122
3123 D3(printk("jffs_try_to_erase()\n"));
3124
3125 erase_size = jffs_erasable_size(fmc);
3126
3127 D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size));
3128
3129 if (erase_size == 0) {
3130 return 0;
3131 }
3132 else if (erase_size < 0) {
3133 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
3134 "jffs_erasable_size returned %ld.\n", erase_size);
3135 return erase_size;
3136 }
3137
3138 if ((err = jffs_clear_end_of_node(c, erase_size)) < 0) {
3139 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
3140 "Clearing of node failed.\n");
3141 return err;
3142 }
3143
3144 offset = fmc->head->offset;
3145
3146 /* Now, let's try to do the erase. */
3147 if ((err = flash_erase_region(fmc->mtd,
3148 offset, erase_size)) < 0) {
3149 printk(KERN_ERR "JFFS: Erase of flash failed. "
3150 "offset = %u, erase_size = %ld\n",
3151 offset, erase_size);
3152 /* XXX: Here we should allocate this area as dirty
3153 with jffs_fmalloced or something similar. Now
3154 we just report the error. */
3155 return err;
3156 }
3157
3158#if 0
3159 /* Check if the erased sectors really got erased. */
3160 {
3161 __u32 pos;
3162 __u32 end;
3163
3164 pos = (__u32)flash_get_direct_pointer(to_kdev_t(c->sb->s_dev), offset);
3165 end = pos + erase_size;
3166
3167 D2(printk("JFFS: Checking erased sector(s)...\n"));
3168
3169 flash_safe_acquire(fmc->mtd);
3170
3171 for (; pos < end; pos += 4) {
3172 if (*(__u32 *)pos != JFFS_EMPTY_BITMASK) {
3173 printk("JFFS: Erase failed! pos = 0x%lx\n",
3174 (long)pos);
3175 jffs_hexdump(fmc->mtd, pos,
3176 jffs_min(256, end - pos));
3177 err = -1;
3178 break;
3179 }
3180 }
3181
3182 flash_safe_release(fmc->mtd);
3183
3184 if (!err) {
3185 D2(printk("JFFS: Erase succeeded.\n"));
3186 }
3187 else {
3188 /* XXX: Here we should allocate the memory
3189 with jffs_fmalloced() in order to prevent
3190 JFFS from using this area accidentally. */
3191 return err;
3192 }
3193 }
3194#endif
3195
3196 /* Update the flash memory data structures. */
3197 jffs_sync_erase(fmc, erase_size);
3198
3199 return erase_size;
3200}
3201
3202
3203/* There are different criteria that should trigger a garbage collect:
3204
3205 1. There is too much dirt in the memory.
3206 2. The free space is becoming small.
3207 3. There are many versions of a node.
3208
3209 The garbage collect should always be done in a manner that guarantees
3210 that future garbage collects cannot be locked. E.g. Rewritten chunks
3211 should not be too large (span more than one sector in the flash memory
3212 for exemple). Of course there is a limit on how intelligent this garbage
3213 collection can be. */
3214
3215
3216static int
3217jffs_garbage_collect_now(struct jffs_control *c)
3218{
3219 struct jffs_fmcontrol *fmc = c->fmc;
3220 long erased = 0;
3221 int result = 0;
3222 D1(int i = 1);
3223 D2(printk("***jffs_garbage_collect_now(): fmc->dirty_size = %u, fmc->free_size = 0x%x\n, fcs1=0x%x, fcs2=0x%x",
3224 fmc->dirty_size, fmc->free_size, jffs_free_size1(fmc), jffs_free_size2(fmc)));
3225 D2(jffs_print_fmcontrol(fmc));
3226
3227 // down(&fmc->gclock);
3228
3229 /* If it is possible to garbage collect, do so. */
3230
3231 while (erased == 0) {
3232 D1(printk("***jffs_garbage_collect_now(): round #%u, "
3233 "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3234 D2(jffs_print_fmcontrol(fmc));
3235
3236 if ((erased = jffs_try_to_erase(c)) < 0) {
3237 printk(KERN_WARNING "JFFS: Error in "
3238 "garbage collector.\n");
3239 result = erased;
3240 goto gc_end;
3241 }
3242 if (erased)
3243 break;
3244
3245 if (fmc->free_size == 0) {
3246 /* Argh */
3247 printk(KERN_ERR "jffs_garbage_collect_now(): free_size == 0. This is BAD.\n");
3248 result = -ENOSPC;
3249 break;
3250 }
3251
3252 if (fmc->dirty_size < fmc->sector_size) {
3253 /* Actually, we _may_ have been able to free some,
3254 * if there are many overlapping nodes which aren't
3255 * actually marked dirty because they still have
3256 * some valid data in each.
3257 */
3258 result = -ENOSPC;
3259 break;
3260 }
3261
3262 /* Let's dare to make a garbage collect. */
3263 if ((result = jffs_garbage_collect_next(c)) < 0) {
3264 printk(KERN_ERR "JFFS: Something "
3265 "has gone seriously wrong "
3266 "with a garbage collect.\n");
3267 goto gc_end;
3268 }
3269
3270 D1(printk(" jffs_garbage_collect_now(): erased: %ld\n", erased));
3271 DJM(jffs_print_memory_allocation_statistics());
3272 }
3273
3274gc_end:
3275 // up(&fmc->gclock);
3276
3277 D3(printk(" jffs_garbage_collect_now(): Leaving...\n"));
3278 D1(if (erased) {
3279 printk("jffs_g_c_now(): erased = %ld\n", erased);
3280 jffs_print_fmcontrol(fmc);
3281 });
3282
3283 if (!erased && !result)
3284 return -ENOSPC;
3285
3286 return result;
3287} /* jffs_garbage_collect_now() */
3288
3289
3290/* Determine if it is reasonable to start garbage collection.
3291 We start a gc pass if either:
3292 - The number of free bytes < MIN_FREE_BYTES && at least one
3293 block is dirty, OR
3294 - The number of dirty bytes > MAX_DIRTY_BYTES
3295*/
3296static inline int thread_should_wake (struct jffs_control *c)
3297{
3298 D1(printk (KERN_NOTICE "thread_should_wake(): free=%d, dirty=%d, blocksize=%d.\n",
3299 c->fmc->free_size, c->fmc->dirty_size, c->fmc->sector_size));
3300
3301 /* If there's not enough dirty space to free a block, there's no point. */
3302 if (c->fmc->dirty_size < c->fmc->sector_size) {
3303 D2(printk(KERN_NOTICE "thread_should_wake(): Not waking. Insufficient dirty space\n"));
3304 return 0;
3305 }
3306#if 1
3307 /* If there is too much RAM used by the various structures, GC */
3308 if (jffs_get_node_inuse() > (c->fmc->used_size/c->fmc->max_chunk_size * 5 + jffs_get_file_count() * 2 + 50)) {
3309 /* FIXME: Provide proof that this test can be satisfied. We
3310 don't want a filesystem doing endless GC just because this
3311 condition cannot ever be false.
3312 */
3313 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to number of nodes\n"));
3314 return 1;
3315 }
3316#endif
3317 /* If there are fewer free bytes than the threshold, GC */
3318 if (c->fmc->free_size < c->gc_minfree_threshold) {
3319 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to insufficent free space\n"));
3320 return 1;
3321 }
3322 /* If there are more dirty bytes than the threshold, GC */
3323 if (c->fmc->dirty_size > c->gc_maxdirty_threshold) {
3324 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to excessive dirty space\n"));
3325 return 1;
3326 }
3327 /* FIXME: What about the "There are many versions of a node" condition? */
3328
3329 return 0;
3330}
3331
3332
3333void jffs_garbage_collect_trigger(struct jffs_control *c)
3334{
3335 /* NOTE: We rely on the fact that we have the BKL here.
3336 * Otherwise, the gc_task could go away between the check
3337 * and the wake_up_process()
3338 */
3339 if (c->gc_task && thread_should_wake(c))
3340 send_sig(SIGHUP, c->gc_task, 1);
3341}
3342
3343
3344/* Kernel threads take (void *) as arguments. Thus we pass
3345 the jffs_control data as a (void *) and then cast it. */
3346int
3347jffs_garbage_collect_thread(void *ptr)
3348{
3349 struct jffs_control *c = (struct jffs_control *) ptr;
3350 struct jffs_fmcontrol *fmc = c->fmc;
3351 long erased;
3352 int result = 0;
3353 D1(int i = 1);
3354
3355 daemonize("jffs_gcd");
3356
3357 c->gc_task = current;
3358
3359 lock_kernel();
3360 init_completion(&c->gc_thread_comp); /* barrier */
3361 spin_lock_irq(&current->sighand->siglock);
3362 siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
3363 recalc_sigpending();
3364 spin_unlock_irq(&current->sighand->siglock);
3365
3366 D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): Starting infinite loop.\n"));
3367
3368 for (;;) {
3369
3370 /* See if we need to start gc. If we don't, go to sleep.
3371
3372 Current implementation is a BAD THING(tm). If we try
3373 to unmount the FS, the unmount operation will sleep waiting
3374 for this thread to exit. We need to arrange to send it a
3375 sig before the umount process sleeps.
3376 */
3377
3378 if (!thread_should_wake(c))
3379 set_current_state (TASK_INTERRUPTIBLE);
3380
3381 schedule(); /* Yes, we do this even if we want to go
3382 on immediately - we're a low priority
3383 background task. */
3384
3385 /* Put_super will send a SIGKILL and then wait on the sem.
3386 */
3387 while (signal_pending(current)) {
3388 siginfo_t info;
3389 unsigned long signr = 0;
3390
3391 if (try_to_freeze())
3392 continue;
3393
3394 spin_lock_irq(&current->sighand->siglock);
3395 signr = dequeue_signal(current, &current->blocked, &info);
3396 spin_unlock_irq(&current->sighand->siglock);
3397
3398 switch(signr) {
3399 case SIGSTOP:
3400 D1(printk("jffs_garbage_collect_thread(): SIGSTOP received.\n"));
3401 set_current_state(TASK_STOPPED);
3402 schedule();
3403 break;
3404
3405 case SIGKILL:
3406 D1(printk("jffs_garbage_collect_thread(): SIGKILL received.\n"));
3407 c->gc_task = NULL;
3408 complete_and_exit(&c->gc_thread_comp, 0);
3409 }
3410 }
3411
3412
3413 D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): collecting.\n"));
3414
3415 D3(printk (KERN_NOTICE "g_c_thread(): down biglock\n"));
3416 mutex_lock(&fmc->biglock);
3417
3418 D1(printk("***jffs_garbage_collect_thread(): round #%u, "
3419 "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3420 D2(jffs_print_fmcontrol(fmc));
3421
3422 if ((erased = jffs_try_to_erase(c)) < 0) {
3423 printk(KERN_WARNING "JFFS: Error in "
3424 "garbage collector: %ld.\n", erased);
3425 }
3426
3427 if (erased)
3428 goto gc_end;
3429
3430 if (fmc->free_size == 0) {
3431 /* Argh. Might as well commit suicide. */
3432 printk(KERN_ERR "jffs_garbage_collect_thread(): free_size == 0. This is BAD.\n");
3433 send_sig(SIGQUIT, c->gc_task, 1);
3434 // panic()
3435 goto gc_end;
3436 }
3437
3438 /* Let's dare to make a garbage collect. */
3439 if ((result = jffs_garbage_collect_next(c)) < 0) {
3440 printk(KERN_ERR "JFFS: Something "
3441 "has gone seriously wrong "
3442 "with a garbage collect: %d\n", result);
3443 }
3444
3445 gc_end:
3446 D3(printk (KERN_NOTICE "g_c_thread(): up biglock\n"));
3447 mutex_unlock(&fmc->biglock);
3448 } /* for (;;) */
3449} /* jffs_garbage_collect_thread() */
diff --git a/fs/jffs/intrep.h b/fs/jffs/intrep.h
deleted file mode 100644
index 5c7abe0e2695..000000000000
--- a/fs/jffs/intrep.h
+++ /dev/null
@@ -1,58 +0,0 @@
1/*
2 * JFFS -- Journaling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 1999, 2000 Axis Communications AB.
5 *
6 * Created by Finn Hakansson <finn@axis.com>.
7 *
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.
12 *
13 * $Id: intrep.h,v 1.14 2001/09/23 23:28:37 dwmw2 Exp $
14 *
15 */
16
17#ifndef __LINUX_JFFS_INTREP_H__
18#define __LINUX_JFFS_INTREP_H__
19#include "jffs_fm.h"
20struct jffs_node *jffs_alloc_node(void);
21void jffs_free_node(struct jffs_node *n);
22int jffs_get_node_inuse(void);
23
24void jffs_cleanup_control(struct jffs_control *c);
25int jffs_build_fs(struct super_block *sb);
26
27int jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
28 const struct jffs_raw_inode *raw_inode,
29 const char *name, struct jffs_node *node);
30struct jffs_file *jffs_find_file(struct jffs_control *c, __u32 ino);
31struct jffs_file *jffs_find_child(struct jffs_file *dir, const char *name, int len);
32
33void jffs_free_node(struct jffs_node *node);
34
35int jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *));
36int jffs_possibly_delete_file(struct jffs_file *f);
37int jffs_insert_file_into_tree(struct jffs_file *f);
38int jffs_unlink_file_from_tree(struct jffs_file *f);
39int jffs_file_count(struct jffs_file *f);
40
41int jffs_write_node(struct jffs_control *c, struct jffs_node *node,
42 struct jffs_raw_inode *raw_inode,
43 const char *name, const unsigned char *buf,
44 int recoverable, struct jffs_file *f);
45int jffs_read_data(struct jffs_file *f, unsigned char *buf, __u32 read_offset, __u32 size);
46
47/* Garbage collection stuff. */
48int jffs_garbage_collect_thread(void *c);
49void jffs_garbage_collect_trigger(struct jffs_control *c);
50
51/* For debugging purposes. */
52#if 0
53int jffs_print_file(struct jffs_file *f);
54#endif /* 0 */
55void jffs_print_hash_table(struct jffs_control *c);
56void jffs_print_tree(struct jffs_file *first_file, int indent);
57
58#endif /* __LINUX_JFFS_INTREP_H__ */
diff --git a/fs/jffs/jffs_fm.c b/fs/jffs/jffs_fm.c
deleted file mode 100644
index 5a95fbdd6fdb..000000000000
--- a/fs/jffs/jffs_fm.c
+++ /dev/null
@@ -1,798 +0,0 @@
1/*
2 * JFFS -- Journaling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 1999, 2000 Axis Communications AB.
5 *
6 * Created by Finn Hakansson <finn@axis.com>.
7 *
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.
12 *
13 * $Id: jffs_fm.c,v 1.27 2001/09/20 12:29:47 dwmw2 Exp $
14 *
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
17 *
18 */
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/blkdev.h>
22#include <linux/jffs.h>
23#include "jffs_fm.h"
24#include "intrep.h"
25
26#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
27static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
28#endif
29
30static struct jffs_fm *jffs_alloc_fm(void);
31static void jffs_free_fm(struct jffs_fm *n);
32
33extern struct kmem_cache *fm_cache;
34extern struct kmem_cache *node_cache;
35
36#if CONFIG_JFFS_FS_VERBOSE > 0
37void
38jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
39{
40 D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
41 D(printk("{\n"));
42 D(printk(" %u, /* flash_size */\n", fmc->flash_size));
43 D(printk(" %u, /* used_size */\n", fmc->used_size));
44 D(printk(" %u, /* dirty_size */\n", fmc->dirty_size));
45 D(printk(" %u, /* free_size */\n", fmc->free_size));
46 D(printk(" %u, /* sector_size */\n", fmc->sector_size));
47 D(printk(" %u, /* min_free_size */\n", fmc->min_free_size));
48 D(printk(" %u, /* max_chunk_size */\n", fmc->max_chunk_size));
49 D(printk(" 0x%p, /* mtd */\n", fmc->mtd));
50 D(printk(" 0x%p, /* head */ "
51 "(head->offset = 0x%08x)\n",
52 fmc->head, (fmc->head ? fmc->head->offset : 0)));
53 D(printk(" 0x%p, /* tail */ "
54 "(tail->offset + tail->size = 0x%08x)\n",
55 fmc->tail,
56 (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
57 D(printk(" 0x%p, /* head_extra */\n", fmc->head_extra));
58 D(printk(" 0x%p, /* tail_extra */\n", fmc->tail_extra));
59 D(printk("}\n"));
60}
61#endif /* CONFIG_JFFS_FS_VERBOSE > 0 */
62
63#if CONFIG_JFFS_FS_VERBOSE > 2
64static void
65jffs_print_fm(struct jffs_fm *fm)
66{
67 D(printk("struct jffs_fm: 0x%p\n", fm));
68 D(printk("{\n"));
69 D(printk(" 0x%08x, /* offset */\n", fm->offset));
70 D(printk(" %u, /* size */\n", fm->size));
71 D(printk(" 0x%p, /* prev */\n", fm->prev));
72 D(printk(" 0x%p, /* next */\n", fm->next));
73 D(printk(" 0x%p, /* nodes */\n", fm->nodes));
74 D(printk("}\n"));
75}
76#endif /* CONFIG_JFFS_FS_VERBOSE > 2 */
77
78#if 0
79void
80jffs_print_node_ref(struct jffs_node_ref *ref)
81{
82 D(printk("struct jffs_node_ref: 0x%p\n", ref));
83 D(printk("{\n"));
84 D(printk(" 0x%p, /* node */\n", ref->node));
85 D(printk(" 0x%p, /* next */\n", ref->next));
86 D(printk("}\n"));
87}
88#endif /* 0 */
89
90/* This function creates a new shiny flash memory control structure. */
91struct jffs_fmcontrol *
92jffs_build_begin(struct jffs_control *c, int unit)
93{
94 struct jffs_fmcontrol *fmc;
95 struct mtd_info *mtd;
96
97 D3(printk("jffs_build_begin()\n"));
98 fmc = kmalloc(sizeof(*fmc), GFP_KERNEL);
99 if (!fmc) {
100 D(printk("jffs_build_begin(): Allocation of "
101 "struct jffs_fmcontrol failed!\n"));
102 return (struct jffs_fmcontrol *)0;
103 }
104 DJM(no_jffs_fmcontrol++);
105
106 mtd = get_mtd_device(NULL, unit);
107
108 if (IS_ERR(mtd)) {
109 kfree(fmc);
110 DJM(no_jffs_fmcontrol--);
111 return NULL;
112 }
113
114 /* Retrieve the size of the flash memory. */
115 fmc->flash_size = mtd->size;
116 D3(printk(" fmc->flash_size = %d bytes\n", fmc->flash_size));
117
118 fmc->used_size = 0;
119 fmc->dirty_size = 0;
120 fmc->free_size = mtd->size;
121 fmc->sector_size = mtd->erasesize;
122 fmc->max_chunk_size = fmc->sector_size >> 1;
123 /* min_free_size:
124 1 sector, obviously.
125 + 1 x max_chunk_size, for when a nodes overlaps the end of a sector
126 + 1 x max_chunk_size again, which ought to be enough to handle
127 the case where a rename causes a name to grow, and GC has
128 to write out larger nodes than the ones it's obsoleting.
129 We should fix it so it doesn't have to write the name
130 _every_ time. Later.
131 + another 2 sectors because people keep getting GC stuck and
132 we don't know why. This scares me - I want formal proof
133 of correctness of whatever number we put here. dwmw2.
134 */
135 fmc->min_free_size = fmc->sector_size << 2;
136 fmc->mtd = mtd;
137 fmc->c = c;
138 fmc->head = NULL;
139 fmc->tail = NULL;
140 fmc->head_extra = NULL;
141 fmc->tail_extra = NULL;
142 mutex_init(&fmc->biglock);
143 return fmc;
144}
145
146
147/* When the flash memory scan has completed, this function should be called
148 before use of the control structure. */
149void
150jffs_build_end(struct jffs_fmcontrol *fmc)
151{
152 D3(printk("jffs_build_end()\n"));
153
154 if (!fmc->head) {
155 fmc->head = fmc->head_extra;
156 fmc->tail = fmc->tail_extra;
157 }
158 else if (fmc->head_extra) {
159 fmc->tail_extra->next = fmc->head;
160 fmc->head->prev = fmc->tail_extra;
161 fmc->head = fmc->head_extra;
162 }
163 fmc->head_extra = NULL; /* These two instructions should be omitted. */
164 fmc->tail_extra = NULL;
165 D3(jffs_print_fmcontrol(fmc));
166}
167
168
169/* Call this function when the file system is unmounted. This function
170 frees all memory used by this module. */
171void
172jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
173{
174 if (fmc) {
175 struct jffs_fm *next = fmc->head;
176 while (next) {
177 struct jffs_fm *cur = next;
178 next = next->next;
179 jffs_free_fm(cur);
180 }
181 put_mtd_device(fmc->mtd);
182 kfree(fmc);
183 DJM(no_jffs_fmcontrol--);
184 }
185}
186
187
188/* This function returns the size of the first chunk of free space on the
189 flash memory. This function will return something nonzero if the flash
190 memory contains any free space. */
191__u32
192jffs_free_size1(struct jffs_fmcontrol *fmc)
193{
194 __u32 head;
195 __u32 tail;
196 __u32 end = fmc->flash_size;
197
198 if (!fmc->head) {
199 /* There is nothing on the flash. */
200 return fmc->flash_size;
201 }
202
203 /* Compute the beginning and ending of the contents of the flash. */
204 head = fmc->head->offset;
205 tail = fmc->tail->offset + fmc->tail->size;
206 if (tail == end) {
207 tail = 0;
208 }
209 ASSERT(else if (tail > end) {
210 printk(KERN_WARNING "jffs_free_size1(): tail > end\n");
211 tail = 0;
212 });
213
214 if (head <= tail) {
215 return end - tail;
216 }
217 else {
218 return head - tail;
219 }
220}
221
222/* This function will return something nonzero in case there are two free
223 areas on the flash. Like this:
224
225 +----------------+------------------+----------------+
226 | FREE 1 | USED / DIRTY | FREE 2 |
227 +----------------+------------------+----------------+
228 fmc->head -----^
229 fmc->tail ------------------------^
230
231 The value returned, will be the size of the first empty area on the
232 flash, in this case marked "FREE 1". */
233__u32
234jffs_free_size2(struct jffs_fmcontrol *fmc)
235{
236 if (fmc->head) {
237 __u32 head = fmc->head->offset;
238 __u32 tail = fmc->tail->offset + fmc->tail->size;
239 if (tail == fmc->flash_size) {
240 tail = 0;
241 }
242
243 if (tail >= head) {
244 return head;
245 }
246 }
247 return 0;
248}
249
250
251/* Allocate a chunk of flash memory. If there is enough space on the
252 device, a reference to the associated node is stored in the jffs_fm
253 struct. */
254int
255jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
256 struct jffs_fm **result)
257{
258 struct jffs_fm *fm;
259 __u32 free_chunk_size1;
260 __u32 free_chunk_size2;
261
262 D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
263 "node = 0x%p\n", fmc, size, node));
264
265 *result = NULL;
266
267 if (!(fm = jffs_alloc_fm())) {
268 D(printk("jffs_fmalloc(): kmalloc() failed! (fm)\n"));
269 return -ENOMEM;
270 }
271
272 free_chunk_size1 = jffs_free_size1(fmc);
273 free_chunk_size2 = jffs_free_size2(fmc);
274 if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
275 printk(KERN_WARNING "Free size accounting screwed\n");
276 printk(KERN_WARNING "free_chunk_size1 == 0x%x, free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n", free_chunk_size1, free_chunk_size2, fmc->free_size);
277 }
278
279 D3(printk("jffs_fmalloc(): free_chunk_size1 = %u, "
280 "free_chunk_size2 = %u\n",
281 free_chunk_size1, free_chunk_size2));
282
283 if (size <= free_chunk_size1) {
284 if (!(fm->nodes = (struct jffs_node_ref *)
285 kmalloc(sizeof(struct jffs_node_ref),
286 GFP_KERNEL))) {
287 D(printk("jffs_fmalloc(): kmalloc() failed! "
288 "(node_ref)\n"));
289 jffs_free_fm(fm);
290 return -ENOMEM;
291 }
292 DJM(no_jffs_node_ref++);
293 fm->nodes->node = node;
294 fm->nodes->next = NULL;
295 if (fmc->tail) {
296 fm->offset = fmc->tail->offset + fmc->tail->size;
297 if (fm->offset == fmc->flash_size) {
298 fm->offset = 0;
299 }
300 ASSERT(else if (fm->offset > fmc->flash_size) {
301 printk(KERN_WARNING "jffs_fmalloc(): "
302 "offset > flash_end\n");
303 fm->offset = 0;
304 });
305 }
306 else {
307 /* There don't have to be files in the file
308 system yet. */
309 fm->offset = 0;
310 }
311 fm->size = size;
312 fmc->free_size -= size;
313 fmc->used_size += size;
314 }
315 else if (size > free_chunk_size2) {
316 printk(KERN_WARNING "JFFS: Tried to allocate a too "
317 "large flash memory chunk. (size = %u)\n", size);
318 jffs_free_fm(fm);
319 return -ENOSPC;
320 }
321 else {
322 fm->offset = fmc->tail->offset + fmc->tail->size;
323 fm->size = free_chunk_size1;
324 fm->nodes = NULL;
325 fmc->free_size -= fm->size;
326 fmc->dirty_size += fm->size; /* Changed by simonk. This seemingly fixes a
327 bug that caused infinite garbage collection.
328 It previously set fmc->dirty_size to size (which is the
329 size of the requested chunk).
330 */
331 }
332
333 fm->next = NULL;
334 if (!fmc->head) {
335 fm->prev = NULL;
336 fmc->head = fm;
337 fmc->tail = fm;
338 }
339 else {
340 fm->prev = fmc->tail;
341 fmc->tail->next = fm;
342 fmc->tail = fm;
343 }
344
345 D3(jffs_print_fmcontrol(fmc));
346 D3(jffs_print_fm(fm));
347 *result = fm;
348 return 0;
349}
350
351
352/* The on-flash space is not needed anymore by the passed node. Remove
353 the reference to the node from the node list. If the data chunk in
354 the flash memory isn't used by any more nodes anymore (fm->nodes == 0),
355 then mark that chunk as dirty. */
356int
357jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, struct jffs_node *node)
358{
359 struct jffs_node_ref *ref;
360 struct jffs_node_ref *prev;
361 ASSERT(int del = 0);
362
363 D2(printk("jffs_fmfree(): node->ino = %u, node->version = %u\n",
364 node->ino, node->version));
365
366 ASSERT(if (!fmc || !fm || !fm->nodes) {
367 printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
368 "fm->nodes: 0x%p\n",
369 fmc, fm, (fm ? fm->nodes : NULL));
370 return -1;
371 });
372
373 /* Find the reference to the node that is going to be removed
374 and remove it. */
375 for (ref = fm->nodes, prev = NULL; ref; ref = ref->next) {
376 if (ref->node == node) {
377 if (prev) {
378 prev->next = ref->next;
379 }
380 else {
381 fm->nodes = ref->next;
382 }
383 kfree(ref);
384 DJM(no_jffs_node_ref--);
385 ASSERT(del = 1);
386 break;
387 }
388 prev = ref;
389 }
390
391 /* If the data chunk in the flash memory isn't used anymore
392 just mark it as obsolete. */
393 if (!fm->nodes) {
394 /* No node uses this chunk so let's remove it. */
395 fmc->used_size -= fm->size;
396 fmc->dirty_size += fm->size;
397#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
398 if (jffs_mark_obsolete(fmc, fm->offset) < 0) {
399 D1(printk("jffs_fmfree(): Failed to mark an on-flash "
400 "node obsolete!\n"));
401 return -1;
402 }
403#endif
404 }
405
406 ASSERT(if (!del) {
407 printk(KERN_WARNING "***jffs_fmfree(): "
408 "Didn't delete any node reference!\n");
409 });
410
411 return 0;
412}
413
414
415/* This allocation function is used during the initialization of
416 the file system. */
417struct jffs_fm *
418jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
419 struct jffs_node *node)
420{
421 struct jffs_fm *fm;
422
423 D3(printk("jffs_fmalloced()\n"));
424
425 if (!(fm = jffs_alloc_fm())) {
426 D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
427 fmc, offset, size, node));
428 return NULL;
429 }
430 fm->offset = offset;
431 fm->size = size;
432 fm->prev = NULL;
433 fm->next = NULL;
434 fm->nodes = NULL;
435 if (node) {
436 /* `node' exists and it should be associated with the
437 jffs_fm structure `fm'. */
438 if (!(fm->nodes = (struct jffs_node_ref *)
439 kmalloc(sizeof(struct jffs_node_ref),
440 GFP_KERNEL))) {
441 D(printk("jffs_fmalloced(): !fm->nodes\n"));
442 jffs_free_fm(fm);
443 return NULL;
444 }
445 DJM(no_jffs_node_ref++);
446 fm->nodes->node = node;
447 fm->nodes->next = NULL;
448 fmc->used_size += size;
449 fmc->free_size -= size;
450 }
451 else {
452 /* If there is no node, then this is just a chunk of dirt. */
453 fmc->dirty_size += size;
454 fmc->free_size -= size;
455 }
456
457 if (fmc->head_extra) {
458 fm->prev = fmc->tail_extra;
459 fmc->tail_extra->next = fm;
460 fmc->tail_extra = fm;
461 }
462 else if (!fmc->head) {
463 fmc->head = fm;
464 fmc->tail = fm;
465 }
466 else if (fmc->tail->offset + fmc->tail->size < offset) {
467 fmc->head_extra = fm;
468 fmc->tail_extra = fm;
469 }
470 else {
471 fm->prev = fmc->tail;
472 fmc->tail->next = fm;
473 fmc->tail = fm;
474 }
475 D3(jffs_print_fmcontrol(fmc));
476 D3(jffs_print_fm(fm));
477 return fm;
478}
479
480
481/* Add a new node to an already existing jffs_fm struct. */
482int
483jffs_add_node(struct jffs_node *node)
484{
485 struct jffs_node_ref *ref;
486
487 D3(printk("jffs_add_node(): ino = %u\n", node->ino));
488
489 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
490 if (!ref)
491 return -ENOMEM;
492
493 DJM(no_jffs_node_ref++);
494 ref->node = node;
495 ref->next = node->fm->nodes;
496 node->fm->nodes = ref;
497 return 0;
498}
499
500
501/* Free a part of some allocated space. */
502void
503jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
504{
505 D1(printk("***jffs_fmfree_partly(): fm = 0x%p, fm->nodes = 0x%p, "
506 "fm->nodes->node->ino = %u, size = %u\n",
507 fm, (fm ? fm->nodes : 0),
508 (!fm ? 0 : (!fm->nodes ? 0 : fm->nodes->node->ino)), size));
509
510 if (fm->nodes) {
511 kfree(fm->nodes);
512 DJM(no_jffs_node_ref--);
513 fm->nodes = NULL;
514 }
515 fmc->used_size -= fm->size;
516 if (fm == fmc->tail) {
517 fm->size -= size;
518 fmc->free_size += size;
519 }
520 fmc->dirty_size += fm->size;
521}
522
523
524/* Find the jffs_fm struct that contains the end of the data chunk that
525 begins at the logical beginning of the flash memory and spans `size'
526 bytes. If we want to erase a sector of the flash memory, we use this
527 function to find where the sector limit cuts a chunk of data. */
528struct jffs_fm *
529jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
530{
531 struct jffs_fm *fm;
532 __u32 pos = 0;
533
534 if (size == 0) {
535 return NULL;
536 }
537
538 ASSERT(if (!fmc) {
539 printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
540 return NULL;
541 });
542
543 fm = fmc->head;
544
545 while (fm) {
546 pos += fm->size;
547 if (pos < size) {
548 fm = fm->next;
549 }
550 else if (pos > size) {
551 break;
552 }
553 else {
554 fm = NULL;
555 break;
556 }
557 }
558
559 return fm;
560}
561
562
563/* Move the head of the fmc structures and delete the obsolete parts. */
564void
565jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
566{
567 struct jffs_fm *fm;
568 struct jffs_fm *del;
569
570 ASSERT(if (!fmc) {
571 printk(KERN_ERR "jffs_sync_erase(): fmc == NULL\n");
572 return;
573 });
574
575 fmc->dirty_size -= erased_size;
576 fmc->free_size += erased_size;
577
578 for (fm = fmc->head; fm && (erased_size > 0);) {
579 if (erased_size >= fm->size) {
580 erased_size -= fm->size;
581 del = fm;
582 fm = fm->next;
583 fm->prev = NULL;
584 fmc->head = fm;
585 jffs_free_fm(del);
586 }
587 else {
588 fm->size -= erased_size;
589 fm->offset += erased_size;
590 break;
591 }
592 }
593}
594
595
596/* Return the oldest used node in the flash memory. */
597struct jffs_node *
598jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
599{
600 struct jffs_fm *fm;
601 struct jffs_node_ref *nref;
602 struct jffs_node *node = NULL;
603
604 ASSERT(if (!fmc) {
605 printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
606 return NULL;
607 });
608
609 for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);
610
611 if (!fm) {
612 return NULL;
613 }
614
615 /* The oldest node is the last one in the reference list. This list
616 shouldn't be too long; just one or perhaps two elements. */
617 for (nref = fm->nodes; nref; nref = nref->next) {
618 node = nref->node;
619 }
620
621 D2(printk("jffs_get_oldest_node(): ino = %u, version = %u\n",
622 (node ? node->ino : 0), (node ? node->version : 0)));
623
624 return node;
625}
626
627
628#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
629
630/* Mark an on-flash node as obsolete.
631
632 Note that this is just an optimization that isn't necessary for the
633 filesystem to work. */
634
635static int
636jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
637{
638 /* The `accurate_pos' holds the position of the accurate byte
639 in the jffs_raw_inode structure that we are going to mark
640 as obsolete. */
641 __u32 accurate_pos = fm_offset + JFFS_RAW_INODE_ACCURATE_OFFSET;
642 unsigned char zero = 0x00;
643 size_t len;
644
645 D3(printk("jffs_mark_obsolete(): accurate_pos = %u\n", accurate_pos));
646 ASSERT(if (!fmc) {
647 printk(KERN_ERR "jffs_mark_obsolete(): fmc == NULL\n");
648 return -1;
649 });
650
651 /* Write 0x00 to the raw inode's accurate member. Don't care
652 about the return value. */
653 MTD_WRITE(fmc->mtd, accurate_pos, 1, &len, &zero);
654 return 0;
655}
656
657#endif /* JFFS_MARK_OBSOLETE */
658
659/* check if it's possible to erase the wanted range, and if not, return
660 * the range that IS erasable, or a negative error code.
661 */
662static long
663jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
664{
665 u_long ssize;
666
667 /* assume that sector size for a partition is constant even
668 * if it spans more than one chip (you usually put the same
669 * type of chips in a system)
670 */
671
672 ssize = mtd->erasesize;
673
674 if (offset % ssize) {
675 printk(KERN_WARNING "jffs_flash_erasable_size() given non-aligned offset %x (erasesize %lx)\n", offset, ssize);
676 /* The offset is not sector size aligned. */
677 return -1;
678 }
679 else if (offset > mtd->size) {
680 printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size);
681 return -2;
682 }
683 else if (offset + size > mtd->size) {
684 printk(KERN_WARNING "jffs_flash_erasable_size() given length which runs off the end of device (ofs %x + len %x = %x, > %x)\n", offset,size, offset+size, mtd->size);
685 return -3;
686 }
687
688 return (size / ssize) * ssize;
689}
690
691
692/* How much dirty flash memory is possible to erase at the moment? */
693long
694jffs_erasable_size(struct jffs_fmcontrol *fmc)
695{
696 struct jffs_fm *fm;
697 __u32 size = 0;
698 long ret;
699
700 ASSERT(if (!fmc) {
701 printk(KERN_ERR "jffs_erasable_size(): fmc = NULL\n");
702 return -1;
703 });
704
705 if (!fmc->head) {
706 /* The flash memory is totally empty. No nodes. No dirt.
707 Just return. */
708 return 0;
709 }
710
711 /* Calculate how much space that is dirty. */
712 for (fm = fmc->head; fm && !fm->nodes; fm = fm->next) {
713 if (size && fm->offset == 0) {
714 /* We have reached the beginning of the flash. */
715 break;
716 }
717 size += fm->size;
718 }
719
720 /* Someone's signature contained this:
721 There's a fine line between fishing and just standing on
722 the shore like an idiot... */
723 ret = jffs_flash_erasable_size(fmc->mtd, fmc->head->offset, size);
724
725 ASSERT(if (ret < 0) {
726 printk("jffs_erasable_size: flash_erasable_size() "
727 "returned something less than zero (%ld).\n", ret);
728 printk("jffs_erasable_size: offset = 0x%08x\n",
729 fmc->head->offset);
730 });
731
732 /* If there is dirt on the flash (which is the reason to why
733 this function was called in the first place) but no space is
734 possible to erase right now, the initial part of the list of
735 jffs_fm structs, that hold place for dirty space, could perhaps
736 be shortened. The list's initial "dirty" elements are merged
737 into just one large dirty jffs_fm struct. This operation must
738 only be performed if nothing is possible to erase. Otherwise,
739 jffs_clear_end_of_node() won't work as expected. */
740 if (ret == 0) {
741 struct jffs_fm *head = fmc->head;
742 struct jffs_fm *del;
743 /* While there are two dirty nodes beside each other.*/
744 while (head->nodes == 0
745 && head->next
746 && head->next->nodes == 0) {
747 del = head->next;
748 head->size += del->size;
749 head->next = del->next;
750 if (del->next) {
751 del->next->prev = head;
752 }
753 jffs_free_fm(del);
754 }
755 }
756
757 return (ret >= 0 ? ret : 0);
758}
759
760static struct jffs_fm *jffs_alloc_fm(void)
761{
762 struct jffs_fm *fm;
763
764 fm = kmem_cache_alloc(fm_cache,GFP_KERNEL);
765 DJM(if (fm) no_jffs_fm++;);
766
767 return fm;
768}
769
770static void jffs_free_fm(struct jffs_fm *n)
771{
772 kmem_cache_free(fm_cache,n);
773 DJM(no_jffs_fm--);
774}
775
776
777
778struct jffs_node *jffs_alloc_node(void)
779{
780 struct jffs_node *n;
781
782 n = (struct jffs_node *)kmem_cache_alloc(node_cache,GFP_KERNEL);
783 if(n != NULL)
784 no_jffs_node++;
785 return n;
786}
787
788void jffs_free_node(struct jffs_node *n)
789{
790 kmem_cache_free(node_cache,n);
791 no_jffs_node--;
792}
793
794
795int jffs_get_node_inuse(void)
796{
797 return no_jffs_node;
798}
diff --git a/fs/jffs/jffs_fm.h b/fs/jffs/jffs_fm.h
deleted file mode 100644
index 9ee6ad29eff5..000000000000
--- a/fs/jffs/jffs_fm.h
+++ /dev/null
@@ -1,149 +0,0 @@
1/*
2 * JFFS -- Journaling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 1999, 2000 Axis Communications AB.
5 *
6 * Created by Finn Hakansson <finn@axis.com>.
7 *
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.
12 *
13 * $Id: jffs_fm.h,v 1.13 2001/01/11 12:03:25 dwmw2 Exp $
14 *
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
17 *
18 */
19
20#ifndef __LINUX_JFFS_FM_H__
21#define __LINUX_JFFS_FM_H__
22
23#include <linux/types.h>
24#include <linux/jffs.h>
25#include <linux/mtd/mtd.h>
26#include <linux/mutex.h>
27
28/* The alignment between two nodes in the flash memory. */
29#define JFFS_ALIGN_SIZE 4
30
31/* Mark the on-flash space as obsolete when appropriate. */
32#define JFFS_MARK_OBSOLETE 0
33
34#ifndef CONFIG_JFFS_FS_VERBOSE
35#define CONFIG_JFFS_FS_VERBOSE 1
36#endif
37
38#if CONFIG_JFFS_FS_VERBOSE > 0
39#define D(x) x
40#define D1(x) D(x)
41#else
42#define D(x)
43#define D1(x)
44#endif
45
46#if CONFIG_JFFS_FS_VERBOSE > 1
47#define D2(x) D(x)
48#else
49#define D2(x)
50#endif
51
52#if CONFIG_JFFS_FS_VERBOSE > 2
53#define D3(x) D(x)
54#else
55#define D3(x)
56#endif
57
58#define ASSERT(x) x
59
60/* How many padding bytes should be inserted between two chunks of data
61 on the flash? */
62#define JFFS_GET_PAD_BYTES(size) ( (JFFS_ALIGN_SIZE-1) & -(__u32)(size) )
63#define JFFS_PAD(size) ( (size + (JFFS_ALIGN_SIZE-1)) & ~(JFFS_ALIGN_SIZE-1) )
64
65
66
67struct jffs_node_ref
68{
69 struct jffs_node *node;
70 struct jffs_node_ref *next;
71};
72
73
74/* The struct jffs_fm represents a chunk of data in the flash memory. */
75struct jffs_fm
76{
77 __u32 offset;
78 __u32 size;
79 struct jffs_fm *prev;
80 struct jffs_fm *next;
81 struct jffs_node_ref *nodes; /* USED if != 0. */
82};
83
84struct jffs_fmcontrol
85{
86 __u32 flash_size;
87 __u32 used_size;
88 __u32 dirty_size;
89 __u32 free_size;
90 __u32 sector_size;
91 __u32 min_free_size; /* The minimum free space needed to be able
92 to perform garbage collections. */
93 __u32 max_chunk_size; /* The maximum size of a chunk of data. */
94 struct mtd_info *mtd;
95 struct jffs_control *c;
96 struct jffs_fm *head;
97 struct jffs_fm *tail;
98 struct jffs_fm *head_extra;
99 struct jffs_fm *tail_extra;
100 struct mutex biglock;
101};
102
103/* Notice the two members head_extra and tail_extra in the jffs_control
104 structure above. Those are only used during the scanning of the flash
105 memory; while the file system is being built. If the data in the flash
106 memory is organized like
107
108 +----------------+------------------+----------------+
109 | USED / DIRTY | FREE | USED / DIRTY |
110 +----------------+------------------+----------------+
111
112 then the scan is split in two parts. The first scanned part of the
113 flash memory is organized through the members head and tail. The
114 second scanned part is organized with head_extra and tail_extra. When
115 the scan is completed, the two lists are merged together. The jffs_fm
116 struct that head_extra references is the logical beginning of the
117 flash memory so it will be referenced by the head member. */
118
119
120
121struct jffs_fmcontrol *jffs_build_begin(struct jffs_control *c, int unit);
122void jffs_build_end(struct jffs_fmcontrol *fmc);
123void jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc);
124
125int jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size,
126 struct jffs_node *node, struct jffs_fm **result);
127int jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm,
128 struct jffs_node *node);
129
130__u32 jffs_free_size1(struct jffs_fmcontrol *fmc);
131__u32 jffs_free_size2(struct jffs_fmcontrol *fmc);
132void jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size);
133struct jffs_fm *jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size);
134struct jffs_node *jffs_get_oldest_node(struct jffs_fmcontrol *fmc);
135long jffs_erasable_size(struct jffs_fmcontrol *fmc);
136struct jffs_fm *jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset,
137 __u32 size, struct jffs_node *node);
138int jffs_add_node(struct jffs_node *node);
139void jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm,
140 __u32 size);
141
142#if CONFIG_JFFS_FS_VERBOSE > 0
143void jffs_print_fmcontrol(struct jffs_fmcontrol *fmc);
144#endif
145#if 0
146void jffs_print_node_ref(struct jffs_node_ref *ref);
147#endif /* 0 */
148
149#endif /* __LINUX_JFFS_FM_H__ */
diff --git a/fs/jffs/jffs_proc.c b/fs/jffs/jffs_proc.c
deleted file mode 100644
index 9bdd99a557c2..000000000000
--- a/fs/jffs/jffs_proc.c
+++ /dev/null
@@ -1,261 +0,0 @@
1/*
2 * JFFS -- Journaling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 2000 Axis Communications AB.
5 *
6 * Created by Simon Kagstrom <simonk@axis.com>.
7 *
8 * $Id: jffs_proc.c,v 1.5 2001/06/02 14:34:55 dwmw2 Exp $
9 *
10 * This is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * Overview:
16 * This file defines JFFS partition entries in the proc file system.
17 *
18 * TODO:
19 * Create some more proc files for different kinds of info, i.e. statistics
20 * about written and read bytes, number of calls to different routines,
21 * reports about failures.
22 */
23
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/jffs.h>
27#include <linux/slab.h>
28#include <linux/proc_fs.h>
29#include <linux/time.h>
30#include <linux/types.h>
31#include "jffs_fm.h"
32#include "jffs_proc.h"
33
34/*
35 * Structure for a JFFS partition in the system
36 */
37struct jffs_partition_dir {
38 struct jffs_control *c;
39 struct proc_dir_entry *part_root;
40 struct proc_dir_entry *part_info;
41 struct proc_dir_entry *part_layout;
42 struct jffs_partition_dir *next;
43};
44
45/*
46 * Structure for top-level entry in '/proc/fs' directory
47 */
48struct proc_dir_entry *jffs_proc_root;
49
50/*
51 * Linked list of 'jffs_partition_dirs' to help us track
52 * the mounted JFFS partitions in the system
53 */
54static struct jffs_partition_dir *jffs_part_dirs;
55
56/*
57 * Read functions for entries
58 */
59static int jffs_proc_info_read(char *page, char **start, off_t off,
60 int count, int *eof, void *data);
61static int jffs_proc_layout_read (char *page, char **start, off_t off,
62 int count, int *eof, void *data);
63
64
65/*
66 * Register a JFFS partition directory (called upon mount)
67 */
68int jffs_register_jffs_proc_dir(int mtd, struct jffs_control *c)
69{
70 struct jffs_partition_dir *part_dir;
71 struct proc_dir_entry *part_info = NULL;
72 struct proc_dir_entry *part_layout = NULL;
73 struct proc_dir_entry *part_root = NULL;
74 char name[10];
75
76 sprintf(name, "%d", mtd);
77 /* Allocate structure for local JFFS partition table */
78 part_dir = (struct jffs_partition_dir *)
79 kmalloc(sizeof (struct jffs_partition_dir), GFP_KERNEL);
80 if (!part_dir)
81 goto out;
82
83 /* Create entry for this partition */
84 part_root = proc_mkdir(name, jffs_proc_root);
85 if (!part_root)
86 goto out1;
87
88 /* Create entry for 'info' file */
89 part_info = create_proc_entry ("info", 0, part_root);
90 if (!part_info)
91 goto out2;
92 part_info->read_proc = jffs_proc_info_read;
93 part_info->data = (void *) c;
94
95 /* Create entry for 'layout' file */
96 part_layout = create_proc_entry ("layout", 0, part_root);
97 if (!part_layout)
98 goto out3;
99 part_layout->read_proc = jffs_proc_layout_read;
100 part_layout->data = (void *) c;
101
102 /* Fill in structure for table and insert in the list */
103 part_dir->c = c;
104 part_dir->part_root = part_root;
105 part_dir->part_info = part_info;
106 part_dir->part_layout = part_layout;
107 part_dir->next = jffs_part_dirs;
108 jffs_part_dirs = part_dir;
109
110 /* Return happy */
111 return 0;
112
113out3:
114 remove_proc_entry("info", part_root);
115out2:
116 remove_proc_entry(name, jffs_proc_root);
117out1:
118 kfree(part_dir);
119out:
120 return -ENOMEM;
121}
122
123
124/*
125 * Unregister a JFFS partition directory (called at umount)
126 */
127int jffs_unregister_jffs_proc_dir(struct jffs_control *c)
128{
129 struct jffs_partition_dir *part_dir = jffs_part_dirs;
130 struct jffs_partition_dir *prev_part_dir = NULL;
131
132 while (part_dir) {
133 if (part_dir->c == c) {
134 /* Remove entries for partition */
135 remove_proc_entry (part_dir->part_info->name,
136 part_dir->part_root);
137 remove_proc_entry (part_dir->part_layout->name,
138 part_dir->part_root);
139 remove_proc_entry (part_dir->part_root->name,
140 jffs_proc_root);
141
142 /* Remove entry from list */
143 if (prev_part_dir)
144 prev_part_dir->next = part_dir->next;
145 else
146 jffs_part_dirs = part_dir->next;
147
148 /*
149 * Check to see if this is the last one
150 * and remove the entry from '/proc/fs'
151 * if it is.
152 */
153 if (jffs_part_dirs == part_dir->next)
154 remove_proc_entry ("jffs", proc_root_fs);
155
156 /* Free memory for entry */
157 kfree(part_dir);
158
159 /* Return happy */
160 return 0;
161 }
162
163 /* Move to next entry */
164 prev_part_dir = part_dir;
165 part_dir = part_dir->next;
166 }
167
168 /* Return unhappy */
169 return -1;
170}
171
172
173/*
174 * Read a JFFS partition's `info' file
175 */
176static int jffs_proc_info_read (char *page, char **start, off_t off,
177 int count, int *eof, void *data)
178{
179 struct jffs_control *c = (struct jffs_control *) data;
180 int len = 0;
181
182 /* Get information on the parition */
183 len += sprintf (page,
184 "partition size: %08lX (%u)\n"
185 "sector size: %08lX (%u)\n"
186 "used size: %08lX (%u)\n"
187 "dirty size: %08lX (%u)\n"
188 "free size: %08lX (%u)\n\n",
189 (unsigned long) c->fmc->flash_size, c->fmc->flash_size,
190 (unsigned long) c->fmc->sector_size, c->fmc->sector_size,
191 (unsigned long) c->fmc->used_size, c->fmc->used_size,
192 (unsigned long) c->fmc->dirty_size, c->fmc->dirty_size,
193 (unsigned long) (c->fmc->flash_size -
194 (c->fmc->used_size + c->fmc->dirty_size)),
195 c->fmc->flash_size - (c->fmc->used_size + c->fmc->dirty_size));
196
197 /* We're done */
198 *eof = 1;
199
200 /* Return length */
201 return len;
202}
203
204
205/*
206 * Read a JFFS partition's `layout' file
207 */
208static int jffs_proc_layout_read (char *page, char **start, off_t off,
209 int count, int *eof, void *data)
210{
211 struct jffs_control *c = (struct jffs_control *) data;
212 struct jffs_fm *fm = NULL;
213 struct jffs_fm *last_fm = NULL;
214 int len = 0;
215
216 /* Get the first item in the list */
217 fm = c->fmc->head;
218
219 /* Print free space */
220 if (fm && fm->offset) {
221 len += sprintf (page, "00000000 %08lX free\n",
222 (unsigned long) fm->offset);
223 }
224
225 /* Loop through all of the flash control structures */
226 while (fm && (len < (off + count))) {
227 if (fm->nodes) {
228 len += sprintf (page + len,
229 "%08lX %08lX ino=%08lX, ver=%08lX\n",
230 (unsigned long) fm->offset,
231 (unsigned long) fm->size,
232 (unsigned long) fm->nodes->node->ino,
233 (unsigned long) fm->nodes->node->version);
234 }
235 else {
236 len += sprintf (page + len,
237 "%08lX %08lX dirty\n",
238 (unsigned long) fm->offset,
239 (unsigned long) fm->size);
240 }
241 last_fm = fm;
242 fm = fm->next;
243 }
244
245 /* Print free space */
246 if ((len < (off + count)) && last_fm
247 && (last_fm->offset < c->fmc->flash_size)) {
248 len += sprintf (page + len,
249 "%08lX %08lX free\n",
250 (unsigned long) last_fm->offset +
251 last_fm->size,
252 (unsigned long) (c->fmc->flash_size -
253 (last_fm->offset + last_fm->size)));
254 }
255
256 /* We're done */
257 *eof = 1;
258
259 /* Return length */
260 return len;
261}
diff --git a/fs/jffs/jffs_proc.h b/fs/jffs/jffs_proc.h
deleted file mode 100644
index 39a1c5d162b0..000000000000
--- a/fs/jffs/jffs_proc.h
+++ /dev/null
@@ -1,28 +0,0 @@
1/*
2 * JFFS -- Journaling Flash File System, Linux implementation.
3 *
4 * Copyright (C) 2000 Axis Communications AB.
5 *
6 * Created by Simon Kagstrom <simonk@axis.com>.
7 *
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.
12 *
13 * $Id: jffs_proc.h,v 1.2 2000/11/15 22:04:12 sjhill Exp $
14 */
15
16/* jffs_proc.h defines a structure for inclusion in the proc-file system. */
17#ifndef __LINUX_JFFS_PROC_H__
18#define __LINUX_JFFS_PROC_H__
19
20#include <linux/proc_fs.h>
21
22/* The proc_dir_entry for jffs (defined in jffs_proc.c). */
23extern struct proc_dir_entry *jffs_proc_root;
24
25int jffs_register_jffs_proc_dir(int mtd, struct jffs_control *c);
26int jffs_unregister_jffs_proc_dir(struct jffs_control *c);
27
28#endif /* __LINUX_JFFS_PROC_H__ */
diff --git a/fs/jffs2/build.c b/fs/jffs2/build.c
index 02826967ab58..07119c42a861 100644
--- a/fs/jffs2/build.c
+++ b/fs/jffs2/build.c
@@ -348,23 +348,27 @@ int jffs2_do_mount_fs(struct jffs2_sb_info *c)
348 348
349 ret = jffs2_sum_init(c); 349 ret = jffs2_sum_init(c);
350 if (ret) 350 if (ret)
351 return ret; 351 goto out_free;
352 352
353 if (jffs2_build_filesystem(c)) { 353 if (jffs2_build_filesystem(c)) {
354 dbg_fsbuild("build_fs failed\n"); 354 dbg_fsbuild("build_fs failed\n");
355 jffs2_free_ino_caches(c); 355 jffs2_free_ino_caches(c);
356 jffs2_free_raw_node_refs(c); 356 jffs2_free_raw_node_refs(c);
357#ifndef __ECOS 357 ret = -EIO;
358 if (jffs2_blocks_use_vmalloc(c)) 358 goto out_free;
359 vfree(c->blocks);
360 else
361#endif
362 kfree(c->blocks);
363
364 return -EIO;
365 } 359 }
366 360
367 jffs2_calc_trigger_levels(c); 361 jffs2_calc_trigger_levels(c);
368 362
369 return 0; 363 return 0;
364
365 out_free:
366#ifndef __ECOS
367 if (jffs2_blocks_use_vmalloc(c))
368 vfree(c->blocks);
369 else
370#endif
371 kfree(c->blocks);
372
373 return ret;
370} 374}
diff --git a/fs/jffs2/jffs2_fs_sb.h b/fs/jffs2/jffs2_fs_sb.h
index b98594992eed..ea88f69af130 100644
--- a/fs/jffs2/jffs2_fs_sb.h
+++ b/fs/jffs2/jffs2_fs_sb.h
@@ -98,20 +98,14 @@ struct jffs2_sb_info {
98 uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */ 98 uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */
99 99
100#ifdef CONFIG_JFFS2_FS_WRITEBUFFER 100#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
101 /* Write-behind buffer for NAND flash */ 101 unsigned char *wbuf; /* Write-behind buffer for NAND flash */
102 unsigned char *wbuf;
103 unsigned char *oobbuf;
104 uint32_t wbuf_ofs; 102 uint32_t wbuf_ofs;
105 uint32_t wbuf_len; 103 uint32_t wbuf_len;
106 struct jffs2_inodirty *wbuf_inodes; 104 struct jffs2_inodirty *wbuf_inodes;
107
108 struct rw_semaphore wbuf_sem; /* Protects the write buffer */ 105 struct rw_semaphore wbuf_sem; /* Protects the write buffer */
109 106
110 /* Information about out-of-band area usage... */ 107 unsigned char *oobbuf;
111 struct nand_ecclayout *ecclayout; 108 int oobavail; /* How many bytes are available for JFFS2 in OOB */
112 uint32_t badblock_pos;
113 uint32_t fsdata_pos;
114 uint32_t fsdata_len;
115#endif 109#endif
116 110
117 struct jffs2_summary *summary; /* Summary information */ 111 struct jffs2_summary *summary; /* Summary information */
diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
index 3af746eaff0e..31c1475d922a 100644
--- a/fs/jffs2/scan.c
+++ b/fs/jffs2/scan.c
@@ -450,16 +450,20 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
450 450
451#ifdef CONFIG_JFFS2_FS_WRITEBUFFER 451#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
452 if (jffs2_cleanmarker_oob(c)) { 452 if (jffs2_cleanmarker_oob(c)) {
453 int ret = jffs2_check_nand_cleanmarker(c, jeb); 453 int ret;
454
455 if (c->mtd->block_isbad(c->mtd, jeb->offset))
456 return BLK_STATE_BADBLOCK;
457
458 ret = jffs2_check_nand_cleanmarker(c, jeb);
454 D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret)); 459 D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
460
455 /* Even if it's not found, we still scan to see 461 /* Even if it's not found, we still scan to see
456 if the block is empty. We use this information 462 if the block is empty. We use this information
457 to decide whether to erase it or not. */ 463 to decide whether to erase it or not. */
458 switch (ret) { 464 switch (ret) {
459 case 0: cleanmarkerfound = 1; break; 465 case 0: cleanmarkerfound = 1; break;
460 case 1: break; 466 case 1: break;
461 case 2: return BLK_STATE_BADBLOCK;
462 case 3: return BLK_STATE_ALLDIRTY; /* Block has failed to erase min. once */
463 default: return ret; 467 default: return ret;
464 } 468 }
465 } 469 }
diff --git a/fs/jffs2/wbuf.c b/fs/jffs2/wbuf.c
index 9c99859f5edd..de718e3a1692 100644
--- a/fs/jffs2/wbuf.c
+++ b/fs/jffs2/wbuf.c
@@ -957,43 +957,48 @@ exit:
957 return ret; 957 return ret;
958} 958}
959 959
960#define NR_OOB_SCAN_PAGES 4 960#define NR_OOB_SCAN_PAGES 4
961
962/* For historical reasons we use only 12 bytes for OOB clean marker */
963#define OOB_CM_SIZE 12
964
965static const struct jffs2_unknown_node oob_cleanmarker =
966{
967 .magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
968 .nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
969 .totlen = cpu_to_je32(8)
970};
961 971
962/* 972/*
963 * Check, if the out of band area is empty 973 * Check, if the out of band area is empty. This function knows about the clean
974 * marker and if it is present in OOB, treats the OOB as empty anyway.
964 */ 975 */
965int jffs2_check_oob_empty(struct jffs2_sb_info *c, 976int jffs2_check_oob_empty(struct jffs2_sb_info *c,
966 struct jffs2_eraseblock *jeb, int mode) 977 struct jffs2_eraseblock *jeb, int mode)
967{ 978{
968 int i, page, ret; 979 int i, ret;
969 int oobsize = c->mtd->oobsize; 980 int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
970 struct mtd_oob_ops ops; 981 struct mtd_oob_ops ops;
971 982
972 ops.ooblen = NR_OOB_SCAN_PAGES * oobsize; 983 ops.mode = MTD_OOB_AUTO;
984 ops.ooblen = NR_OOB_SCAN_PAGES * c->oobavail;
973 ops.oobbuf = c->oobbuf; 985 ops.oobbuf = c->oobbuf;
974 ops.ooboffs = 0; 986 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
975 ops.datbuf = NULL; 987 ops.datbuf = NULL;
976 ops.mode = MTD_OOB_PLACE;
977 988
978 ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops); 989 ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
979 if (ret) { 990 if (ret || ops.oobretlen != ops.ooblen) {
980 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB " 991 printk(KERN_ERR "cannot read OOB for EB at %08x, requested %zd"
981 "failed %d for block at %08x\n", ret, jeb->offset)); 992 " bytes, read %zd bytes, error %d\n",
993 jeb->offset, ops.ooblen, ops.oobretlen, ret);
994 if (!ret)
995 ret = -EIO;
982 return ret; 996 return ret;
983 } 997 }
984 998
985 if (ops.oobretlen < ops.ooblen) { 999 for(i = 0; i < ops.ooblen; i++) {
986 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB " 1000 if (mode && i < cmlen)
987 "returned short read (%zd bytes not %d) for block " 1001 /* Yeah, we know about the cleanmarker */
988 "at %08x\n", ops.oobretlen, ops.ooblen, jeb->offset));
989 return -EIO;
990 }
991
992 /* Special check for first page */
993 for(i = 0; i < oobsize ; i++) {
994 /* Yeah, we know about the cleanmarker. */
995 if (mode && i >= c->fsdata_pos &&
996 i < c->fsdata_pos + c->fsdata_len)
997 continue; 1002 continue;
998 1003
999 if (ops.oobbuf[i] != 0xFF) { 1004 if (ops.oobbuf[i] != 0xFF) {
@@ -1003,111 +1008,63 @@ int jffs2_check_oob_empty(struct jffs2_sb_info *c,
1003 } 1008 }
1004 } 1009 }
1005 1010
1006 /* we know, we are aligned :) */
1007 for (page = oobsize; page < ops.ooblen; page += sizeof(long)) {
1008 long dat = *(long *)(&ops.oobbuf[page]);
1009 if(dat != -1)
1010 return 1;
1011 }
1012 return 0; 1011 return 0;
1013} 1012}
1014 1013
1015/* 1014/*
1016 * Scan for a valid cleanmarker and for bad blocks 1015 * Check for a valid cleanmarker.
1016 * Returns: 0 if a valid cleanmarker was found
1017 * 1 if no cleanmarker was found
1018 * negative error code if an error occurred
1017 */ 1019 */
1018int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, 1020int jffs2_check_nand_cleanmarker(struct jffs2_sb_info *c,
1019 struct jffs2_eraseblock *jeb) 1021 struct jffs2_eraseblock *jeb)
1020{ 1022{
1021 struct jffs2_unknown_node n;
1022 struct mtd_oob_ops ops; 1023 struct mtd_oob_ops ops;
1023 int oobsize = c->mtd->oobsize; 1024 int ret, cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
1024 unsigned char *p,*b;
1025 int i, ret;
1026 size_t offset = jeb->offset;
1027
1028 /* Check first if the block is bad. */
1029 if (c->mtd->block_isbad(c->mtd, offset)) {
1030 D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()"
1031 ": Bad block at %08x\n", jeb->offset));
1032 return 2;
1033 }
1034 1025
1035 ops.ooblen = oobsize; 1026 ops.mode = MTD_OOB_AUTO;
1027 ops.ooblen = cmlen;
1036 ops.oobbuf = c->oobbuf; 1028 ops.oobbuf = c->oobbuf;
1037 ops.ooboffs = 0; 1029 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
1038 ops.datbuf = NULL; 1030 ops.datbuf = NULL;
1039 ops.mode = MTD_OOB_PLACE;
1040 1031
1041 ret = c->mtd->read_oob(c->mtd, offset, &ops); 1032 ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
1042 if (ret) { 1033 if (ret || ops.oobretlen != ops.ooblen) {
1043 D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): " 1034 printk(KERN_ERR "cannot read OOB for EB at %08x, requested %zd"
1044 "Read OOB failed %d for block at %08x\n", 1035 " bytes, read %zd bytes, error %d\n",
1045 ret, jeb->offset)); 1036 jeb->offset, ops.ooblen, ops.oobretlen, ret);
1037 if (!ret)
1038 ret = -EIO;
1046 return ret; 1039 return ret;
1047 } 1040 }
1048 1041
1049 if (ops.oobretlen < ops.ooblen) { 1042 return !!memcmp(&oob_cleanmarker, c->oobbuf, cmlen);
1050 D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): "
1051 "Read OOB return short read (%zd bytes not %d) "
1052 "for block at %08x\n", ops.oobretlen, ops.ooblen,
1053 jeb->offset));
1054 return -EIO;
1055 }
1056
1057 n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
1058 n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
1059 n.totlen = cpu_to_je32 (8);
1060 p = (unsigned char *) &n;
1061 b = c->oobbuf + c->fsdata_pos;
1062
1063 for (i = c->fsdata_len; i; i--) {
1064 if (*b++ != *p++)
1065 ret = 1;
1066 }
1067
1068 D1(if (ret == 1) {
1069 printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
1070 "Cleanmarker node not detected in block at %08x\n",
1071 offset);
1072 printk(KERN_WARNING "OOB at %08zx was ", offset);
1073 for (i=0; i < oobsize; i++)
1074 printk("%02x ", c->oobbuf[i]);
1075 printk("\n");
1076 });
1077 return ret;
1078} 1043}
1079 1044
1080int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, 1045int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
1081 struct jffs2_eraseblock *jeb) 1046 struct jffs2_eraseblock *jeb)
1082{ 1047{
1083 struct jffs2_unknown_node n; 1048 int ret;
1084 int ret;
1085 struct mtd_oob_ops ops; 1049 struct mtd_oob_ops ops;
1050 int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
1086 1051
1087 n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 1052 ops.mode = MTD_OOB_AUTO;
1088 n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); 1053 ops.ooblen = cmlen;
1089 n.totlen = cpu_to_je32(8); 1054 ops.oobbuf = (uint8_t *)&oob_cleanmarker;
1090 1055 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
1091 ops.ooblen = c->fsdata_len;
1092 ops.oobbuf = (uint8_t *)&n;
1093 ops.ooboffs = c->fsdata_pos;
1094 ops.datbuf = NULL; 1056 ops.datbuf = NULL;
1095 ops.mode = MTD_OOB_PLACE;
1096 1057
1097 ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops); 1058 ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops);
1098 1059 if (ret || ops.oobretlen != ops.ooblen) {
1099 if (ret) { 1060 printk(KERN_ERR "cannot write OOB for EB at %08x, requested %zd"
1100 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): " 1061 " bytes, read %zd bytes, error %d\n",
1101 "Write failed for block at %08x: error %d\n", 1062 jeb->offset, ops.ooblen, ops.oobretlen, ret);
1102 jeb->offset, ret)); 1063 if (!ret)
1064 ret = -EIO;
1103 return ret; 1065 return ret;
1104 } 1066 }
1105 if (ops.oobretlen != ops.ooblen) { 1067
1106 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
1107 "Short write for block at %08x: %zd not %d\n",
1108 jeb->offset, ops.oobretlen, ops.ooblen));
1109 return -EIO;
1110 }
1111 return 0; 1068 return 0;
1112} 1069}
1113 1070
@@ -1140,41 +1097,24 @@ int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *
1140 return 1; 1097 return 1;
1141} 1098}
1142 1099
1143static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c) 1100int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1144{ 1101{
1145 struct nand_ecclayout *oinfo = c->mtd->ecclayout; 1102 struct nand_ecclayout *oinfo = c->mtd->ecclayout;
1146 1103
1147 /* Do this only, if we have an oob buffer */
1148 if (!c->mtd->oobsize) 1104 if (!c->mtd->oobsize)
1149 return 0; 1105 return 0;
1150 1106
1151 /* Cleanmarker is out-of-band, so inline size zero */ 1107 /* Cleanmarker is out-of-band, so inline size zero */
1152 c->cleanmarker_size = 0; 1108 c->cleanmarker_size = 0;
1153 1109
1154 /* Should we use autoplacement ? */ 1110 if (!oinfo || oinfo->oobavail == 0) {
1155 if (!oinfo) { 1111 printk(KERN_ERR "inconsistent device description\n");
1156 D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
1157 return -EINVAL; 1112 return -EINVAL;
1158 } 1113 }
1159 1114
1160 D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n")); 1115 D1(printk(KERN_DEBUG "JFFS2 using OOB on NAND\n"));
1161 /* Get the position of the free bytes */
1162 if (!oinfo->oobfree[0].length) {
1163 printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep."
1164 " Autoplacement selected and no empty space in oob\n");
1165 return -ENOSPC;
1166 }
1167 c->fsdata_pos = oinfo->oobfree[0].offset;
1168 c->fsdata_len = oinfo->oobfree[0].length;
1169 if (c->fsdata_len > 8)
1170 c->fsdata_len = 8;
1171 1116
1172 return 0; 1117 c->oobavail = oinfo->oobavail;
1173}
1174
1175int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1176{
1177 int res;
1178 1118
1179 /* Initialise write buffer */ 1119 /* Initialise write buffer */
1180 init_rwsem(&c->wbuf_sem); 1120 init_rwsem(&c->wbuf_sem);
@@ -1185,22 +1125,13 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1185 if (!c->wbuf) 1125 if (!c->wbuf)
1186 return -ENOMEM; 1126 return -ENOMEM;
1187 1127
1188 c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL); 1128 c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->oobavail, GFP_KERNEL);
1189 if (!c->oobbuf) 1129 if (!c->oobbuf) {
1190 return -ENOMEM;
1191
1192 res = jffs2_nand_set_oobinfo(c);
1193
1194#ifdef BREAKME
1195 if (!brokenbuf)
1196 brokenbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1197 if (!brokenbuf) {
1198 kfree(c->wbuf); 1130 kfree(c->wbuf);
1199 return -ENOMEM; 1131 return -ENOMEM;
1200 } 1132 }
1201 memset(brokenbuf, 0xdb, c->wbuf_pagesize); 1133
1202#endif 1134 return 0;
1203 return res;
1204} 1135}
1205 1136
1206void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c) 1137void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
diff --git a/fs/libfs.c b/fs/libfs.c
index 7d487047dbb8..cf79196535ec 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -335,17 +335,18 @@ int simple_prepare_write(struct file *file, struct page *page,
335 flush_dcache_page(page); 335 flush_dcache_page(page);
336 kunmap_atomic(kaddr, KM_USER0); 336 kunmap_atomic(kaddr, KM_USER0);
337 } 337 }
338 SetPageUptodate(page);
339 } 338 }
340 return 0; 339 return 0;
341} 340}
342 341
343int simple_commit_write(struct file *file, struct page *page, 342int simple_commit_write(struct file *file, struct page *page,
344 unsigned offset, unsigned to) 343 unsigned from, unsigned to)
345{ 344{
346 struct inode *inode = page->mapping->host; 345 struct inode *inode = page->mapping->host;
347 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; 346 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
348 347
348 if (!PageUptodate(page))
349 SetPageUptodate(page);
349 /* 350 /*
350 * No need to use i_size_read() here, the i_size 351 * No need to use i_size_read() here, the i_size
351 * cannot change under us because we hold the i_mutex. 352 * cannot change under us because we hold the i_mutex.
diff --git a/fs/lockd/svc.c b/fs/lockd/svc.c
index 50cb8daba4e5..126b1bf02c0e 100644
--- a/fs/lockd/svc.c
+++ b/fs/lockd/svc.c
@@ -440,7 +440,7 @@ static ctl_table nlm_sysctl_root[] = {
440}; 440};
441 441
442/* 442/*
443 * Module (and driverfs) parameters. 443 * Module (and sysfs) parameters.
444 */ 444 */
445 445
446#define param_set_min_max(name, type, which_strtol, min, max) \ 446#define param_set_min_max(name, type, which_strtol, min, max) \
diff --git a/fs/namei.c b/fs/namei.c
index 161e2225c757..ee60cc4d3453 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2688,10 +2688,11 @@ int __page_symlink(struct inode *inode, const char *symname, int len,
2688{ 2688{
2689 struct address_space *mapping = inode->i_mapping; 2689 struct address_space *mapping = inode->i_mapping;
2690 struct page *page; 2690 struct page *page;
2691 int err = -ENOMEM; 2691 int err;
2692 char *kaddr; 2692 char *kaddr;
2693 2693
2694retry: 2694retry:
2695 err = -ENOMEM;
2695 page = find_or_create_page(mapping, 0, gfp_mask); 2696 page = find_or_create_page(mapping, 0, gfp_mask);
2696 if (!page) 2697 if (!page)
2697 goto fail; 2698 goto fail;
diff --git a/fs/nfsd/nfs2acl.c b/fs/nfsd/nfs2acl.c
index edde5dc5f796..b61742885011 100644
--- a/fs/nfsd/nfs2acl.c
+++ b/fs/nfsd/nfs2acl.c
@@ -287,13 +287,20 @@ static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
287 return 1; 287 return 1;
288} 288}
289 289
290static int nfsaclsvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p, 290static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
291 struct nfsd_fhandle *resp) 291 struct nfsd_attrstat *resp)
292{ 292{
293 fh_put(&resp->fh); 293 fh_put(&resp->fh);
294 return 1; 294 return 1;
295} 295}
296 296
297static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
298 struct nfsd3_accessres *resp)
299{
300 fh_put(&resp->fh);
301 return 1;
302}
303
297#define nfsaclsvc_decode_voidargs NULL 304#define nfsaclsvc_decode_voidargs NULL
298#define nfsaclsvc_encode_voidres NULL 305#define nfsaclsvc_encode_voidres NULL
299#define nfsaclsvc_release_void NULL 306#define nfsaclsvc_release_void NULL
@@ -322,9 +329,9 @@ struct nfsd3_voidargs { int dummy; };
322static struct svc_procedure nfsd_acl_procedures2[] = { 329static struct svc_procedure nfsd_acl_procedures2[] = {
323 PROC(null, void, void, void, RC_NOCACHE, ST), 330 PROC(null, void, void, void, RC_NOCACHE, ST),
324 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)), 331 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
325 PROC(setacl, setacl, attrstat, fhandle, RC_NOCACHE, ST+AT), 332 PROC(setacl, setacl, attrstat, attrstat, RC_NOCACHE, ST+AT),
326 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT), 333 PROC(getattr, fhandle, attrstat, attrstat, RC_NOCACHE, ST+AT),
327 PROC(access, access, access, fhandle, RC_NOCACHE, ST+AT+1), 334 PROC(access, access, access, access, RC_NOCACHE, ST+AT+1),
328}; 335};
329 336
330struct svc_version nfsd_acl_version2 = { 337struct svc_version nfsd_acl_version2 = {
diff --git a/fs/nfsd/nfs4acl.c b/fs/nfsd/nfs4acl.c
index 5d94555cdc83..832673b14587 100644
--- a/fs/nfsd/nfs4acl.c
+++ b/fs/nfsd/nfs4acl.c
@@ -61,9 +61,11 @@
61 61
62/* flags used to simulate posix default ACLs */ 62/* flags used to simulate posix default ACLs */
63#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \ 63#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
64 | NFS4_ACE_DIRECTORY_INHERIT_ACE | NFS4_ACE_INHERIT_ONLY_ACE) 64 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
65 65
66#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS | NFS4_ACE_IDENTIFIER_GROUP) 66#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
67 | NFS4_ACE_INHERIT_ONLY_ACE \
68 | NFS4_ACE_IDENTIFIER_GROUP)
67 69
68#define MASK_EQUAL(mask1, mask2) \ 70#define MASK_EQUAL(mask1, mask2) \
69 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) ) 71 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
@@ -87,12 +89,19 @@ mask_from_posix(unsigned short perm, unsigned int flags)
87} 89}
88 90
89static u32 91static u32
90deny_mask(u32 allow_mask, unsigned int flags) 92deny_mask_from_posix(unsigned short perm, u32 flags)
91{ 93{
92 u32 ret = ~allow_mask & ~NFS4_MASK_UNSUPP; 94 u32 mask = 0;
93 if (!(flags & NFS4_ACL_DIR)) 95
94 ret &= ~NFS4_ACE_DELETE_CHILD; 96 if (perm & ACL_READ)
95 return ret; 97 mask |= NFS4_READ_MODE;
98 if (perm & ACL_WRITE)
99 mask |= NFS4_WRITE_MODE;
100 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
101 mask |= NFS4_ACE_DELETE_CHILD;
102 if (perm & ACL_EXECUTE)
103 mask |= NFS4_EXECUTE_MODE;
104 return mask;
96} 105}
97 106
98/* XXX: modify functions to return NFS errors; they're only ever 107/* XXX: modify functions to return NFS errors; they're only ever
@@ -126,108 +135,151 @@ struct ace_container {
126}; 135};
127 136
128static short ace2type(struct nfs4_ace *); 137static short ace2type(struct nfs4_ace *);
129static int _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, unsigned int); 138static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *,
130static struct posix_acl *_nfsv4_to_posix_one(struct nfs4_acl *, unsigned int); 139 unsigned int);
131int nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t); 140void nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);
132static int nfs4_acl_split(struct nfs4_acl *, struct nfs4_acl *);
133 141
134struct nfs4_acl * 142struct nfs4_acl *
135nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl, 143nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
136 unsigned int flags) 144 unsigned int flags)
137{ 145{
138 struct nfs4_acl *acl; 146 struct nfs4_acl *acl;
139 int error = -EINVAL; 147 int size = 0;
140 148
141 if ((pacl != NULL && 149 if (pacl) {
142 (posix_acl_valid(pacl) < 0 || pacl->a_count == 0)) || 150 if (posix_acl_valid(pacl) < 0)
143 (dpacl != NULL && 151 return ERR_PTR(-EINVAL);
144 (posix_acl_valid(dpacl) < 0 || dpacl->a_count == 0))) 152 size += 2*pacl->a_count;
145 goto out_err;
146
147 acl = nfs4_acl_new();
148 if (acl == NULL) {
149 error = -ENOMEM;
150 goto out_err;
151 } 153 }
152 154 if (dpacl) {
153 if (pacl != NULL) { 155 if (posix_acl_valid(dpacl) < 0)
154 error = _posix_to_nfsv4_one(pacl, acl, 156 return ERR_PTR(-EINVAL);
155 flags & ~NFS4_ACL_TYPE_DEFAULT); 157 size += 2*dpacl->a_count;
156 if (error < 0)
157 goto out_acl;
158 } 158 }
159 159
160 if (dpacl != NULL) { 160 /* Allocate for worst case: one (deny, allow) pair each: */
161 error = _posix_to_nfsv4_one(dpacl, acl, 161 acl = nfs4_acl_new(size);
162 flags | NFS4_ACL_TYPE_DEFAULT); 162 if (acl == NULL)
163 if (error < 0) 163 return ERR_PTR(-ENOMEM);
164 goto out_acl;
165 }
166 164
167 return acl; 165 if (pacl)
166 _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT);
168 167
169out_acl: 168 if (dpacl)
170 nfs4_acl_free(acl); 169 _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT);
171out_err:
172 acl = ERR_PTR(error);
173 170
174 return acl; 171 return acl;
175} 172}
176 173
177static int 174struct posix_acl_summary {
178nfs4_acl_add_pair(struct nfs4_acl *acl, int eflag, u32 mask, int whotype, 175 unsigned short owner;
179 uid_t owner, unsigned int flags) 176 unsigned short users;
177 unsigned short group;
178 unsigned short groups;
179 unsigned short other;
180 unsigned short mask;
181};
182
183static void
184summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas)
180{ 185{
181 int error; 186 struct posix_acl_entry *pa, *pe;
182 187 pas->users = 0;
183 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE, 188 pas->groups = 0;
184 eflag, mask, whotype, owner); 189 pas->mask = 07;
185 if (error < 0) 190
186 return error; 191 pe = acl->a_entries + acl->a_count;
187 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE, 192
188 eflag, deny_mask(mask, flags), whotype, owner); 193 FOREACH_ACL_ENTRY(pa, acl, pe) {
189 return error; 194 switch (pa->e_tag) {
195 case ACL_USER_OBJ:
196 pas->owner = pa->e_perm;
197 break;
198 case ACL_GROUP_OBJ:
199 pas->group = pa->e_perm;
200 break;
201 case ACL_USER:
202 pas->users |= pa->e_perm;
203 break;
204 case ACL_GROUP:
205 pas->groups |= pa->e_perm;
206 break;
207 case ACL_OTHER:
208 pas->other = pa->e_perm;
209 break;
210 case ACL_MASK:
211 pas->mask = pa->e_perm;
212 break;
213 }
214 }
215 /* We'll only care about effective permissions: */
216 pas->users &= pas->mask;
217 pas->group &= pas->mask;
218 pas->groups &= pas->mask;
190} 219}
191 220
192/* We assume the acl has been verified with posix_acl_valid. */ 221/* We assume the acl has been verified with posix_acl_valid. */
193static int 222static void
194_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl, 223_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
195 unsigned int flags) 224 unsigned int flags)
196{ 225{
197 struct posix_acl_entry *pa, *pe, *group_owner_entry; 226 struct posix_acl_entry *pa, *group_owner_entry;
198 int error = -EINVAL; 227 struct nfs4_ace *ace;
199 u32 mask, mask_mask; 228 struct posix_acl_summary pas;
229 unsigned short deny;
200 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ? 230 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
201 NFS4_INHERITANCE_FLAGS : 0); 231 NFS4_INHERITANCE_FLAGS : 0);
202 232
203 BUG_ON(pacl->a_count < 3); 233 BUG_ON(pacl->a_count < 3);
204 pe = pacl->a_entries + pacl->a_count; 234 summarize_posix_acl(pacl, &pas);
205 pa = pe - 2; /* if mask entry exists, it's second from the last. */
206 if (pa->e_tag == ACL_MASK)
207 mask_mask = deny_mask(mask_from_posix(pa->e_perm, flags), flags);
208 else
209 mask_mask = 0;
210 235
211 pa = pacl->a_entries; 236 pa = pacl->a_entries;
212 BUG_ON(pa->e_tag != ACL_USER_OBJ); 237 ace = acl->aces + acl->naces;
213 mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
214 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_OWNER, 0, flags);
215 if (error < 0)
216 goto out;
217 pa++;
218 238
219 while (pa->e_tag == ACL_USER) { 239 /* We could deny everything not granted by the owner: */
220 mask = mask_from_posix(pa->e_perm, flags); 240 deny = ~pas.owner;
221 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE, 241 /*
222 eflag, mask_mask, NFS4_ACL_WHO_NAMED, pa->e_id); 242 * but it is equivalent (and simpler) to deny only what is not
223 if (error < 0) 243 * granted by later entries:
224 goto out; 244 */
245 deny &= pas.users | pas.group | pas.groups | pas.other;
246 if (deny) {
247 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
248 ace->flag = eflag;
249 ace->access_mask = deny_mask_from_posix(deny, flags);
250 ace->whotype = NFS4_ACL_WHO_OWNER;
251 ace++;
252 acl->naces++;
253 }
225 254
255 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
256 ace->flag = eflag;
257 ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
258 ace->whotype = NFS4_ACL_WHO_OWNER;
259 ace++;
260 acl->naces++;
261 pa++;
226 262
227 error = nfs4_acl_add_pair(acl, eflag, mask, 263 while (pa->e_tag == ACL_USER) {
228 NFS4_ACL_WHO_NAMED, pa->e_id, flags); 264 deny = ~(pa->e_perm & pas.mask);
229 if (error < 0) 265 deny &= pas.groups | pas.group | pas.other;
230 goto out; 266 if (deny) {
267 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
268 ace->flag = eflag;
269 ace->access_mask = deny_mask_from_posix(deny, flags);
270 ace->whotype = NFS4_ACL_WHO_NAMED;
271 ace->who = pa->e_id;
272 ace++;
273 acl->naces++;
274 }
275 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
276 ace->flag = eflag;
277 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
278 flags);
279 ace->whotype = NFS4_ACL_WHO_NAMED;
280 ace->who = pa->e_id;
281 ace++;
282 acl->naces++;
231 pa++; 283 pa++;
232 } 284 }
233 285
@@ -236,67 +288,65 @@ _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
236 288
237 /* allow ACEs */ 289 /* allow ACEs */
238 290
239 if (pacl->a_count > 3) {
240 BUG_ON(pa->e_tag != ACL_GROUP_OBJ);
241 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
242 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
243 NFS4_ACL_WHO_GROUP, 0);
244 if (error < 0)
245 goto out;
246 }
247 group_owner_entry = pa; 291 group_owner_entry = pa;
248 mask = mask_from_posix(pa->e_perm, flags); 292
249 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE, 293 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
250 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask, 294 ace->flag = eflag;
251 NFS4_ACL_WHO_GROUP, 0); 295 ace->access_mask = mask_from_posix(pas.group, flags);
252 if (error < 0) 296 ace->whotype = NFS4_ACL_WHO_GROUP;
253 goto out; 297 ace++;
298 acl->naces++;
254 pa++; 299 pa++;
255 300
256 while (pa->e_tag == ACL_GROUP) { 301 while (pa->e_tag == ACL_GROUP) {
257 mask = mask_from_posix(pa->e_perm, flags); 302 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
258 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE, 303 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
259 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask, 304 ace->access_mask = mask_from_posix(pa->e_perm & pas.mask,
260 NFS4_ACL_WHO_NAMED, pa->e_id); 305 flags);
261 if (error < 0) 306 ace->whotype = NFS4_ACL_WHO_NAMED;
262 goto out; 307 ace->who = pa->e_id;
263 308 ace++;
264 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE, 309 acl->naces++;
265 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
266 NFS4_ACL_WHO_NAMED, pa->e_id);
267 if (error < 0)
268 goto out;
269 pa++; 310 pa++;
270 } 311 }
271 312
272 /* deny ACEs */ 313 /* deny ACEs */
273 314
274 pa = group_owner_entry; 315 pa = group_owner_entry;
275 mask = mask_from_posix(pa->e_perm, flags); 316
276 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE, 317 deny = ~pas.group & pas.other;
277 NFS4_ACE_IDENTIFIER_GROUP | eflag, 318 if (deny) {
278 deny_mask(mask, flags), NFS4_ACL_WHO_GROUP, 0); 319 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
279 if (error < 0) 320 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
280 goto out; 321 ace->access_mask = deny_mask_from_posix(deny, flags);
322 ace->whotype = NFS4_ACL_WHO_GROUP;
323 ace++;
324 acl->naces++;
325 }
281 pa++; 326 pa++;
327
282 while (pa->e_tag == ACL_GROUP) { 328 while (pa->e_tag == ACL_GROUP) {
283 mask = mask_from_posix(pa->e_perm, flags); 329 deny = ~(pa->e_perm & pas.mask);
284 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE, 330 deny &= pas.other;
285 NFS4_ACE_IDENTIFIER_GROUP | eflag, 331 if (deny) {
286 deny_mask(mask, flags), NFS4_ACL_WHO_NAMED, pa->e_id); 332 ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
287 if (error < 0) 333 ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP;
288 goto out; 334 ace->access_mask = mask_from_posix(deny, flags);
335 ace->whotype = NFS4_ACL_WHO_NAMED;
336 ace->who = pa->e_id;
337 ace++;
338 acl->naces++;
339 }
289 pa++; 340 pa++;
290 } 341 }
291 342
292 if (pa->e_tag == ACL_MASK) 343 if (pa->e_tag == ACL_MASK)
293 pa++; 344 pa++;
294 BUG_ON(pa->e_tag != ACL_OTHER); 345 ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE;
295 mask = mask_from_posix(pa->e_perm, flags); 346 ace->flag = eflag;
296 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_EVERYONE, 0, flags); 347 ace->access_mask = mask_from_posix(pa->e_perm, flags);
297 348 ace->whotype = NFS4_ACL_WHO_EVERYONE;
298out: 349 acl->naces++;
299 return error;
300} 350}
301 351
302static void 352static void
@@ -342,46 +392,6 @@ sort_pacl(struct posix_acl *pacl)
342 return; 392 return;
343} 393}
344 394
345int
346nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
347 struct posix_acl **dpacl, unsigned int flags)
348{
349 struct nfs4_acl *dacl;
350 int error = -ENOMEM;
351
352 *pacl = NULL;
353 *dpacl = NULL;
354
355 dacl = nfs4_acl_new();
356 if (dacl == NULL)
357 goto out;
358
359 error = nfs4_acl_split(acl, dacl);
360 if (error)
361 goto out_acl;
362
363 *pacl = _nfsv4_to_posix_one(acl, flags);
364 if (IS_ERR(*pacl)) {
365 error = PTR_ERR(*pacl);
366 *pacl = NULL;
367 goto out_acl;
368 }
369
370 *dpacl = _nfsv4_to_posix_one(dacl, flags);
371 if (IS_ERR(*dpacl)) {
372 error = PTR_ERR(*dpacl);
373 *dpacl = NULL;
374 }
375out_acl:
376 if (error) {
377 posix_acl_release(*pacl);
378 *pacl = NULL;
379 }
380 nfs4_acl_free(dacl);
381out:
382 return error;
383}
384
385/* 395/*
386 * While processing the NFSv4 ACE, this maintains bitmasks representing 396 * While processing the NFSv4 ACE, this maintains bitmasks representing
387 * which permission bits have been allowed and which denied to a given 397 * which permission bits have been allowed and which denied to a given
@@ -406,6 +416,7 @@ struct posix_ace_state_array {
406 * calculated so far: */ 416 * calculated so far: */
407 417
408struct posix_acl_state { 418struct posix_acl_state {
419 int empty;
409 struct posix_ace_state owner; 420 struct posix_ace_state owner;
410 struct posix_ace_state group; 421 struct posix_ace_state group;
411 struct posix_ace_state other; 422 struct posix_ace_state other;
@@ -421,6 +432,7 @@ init_state(struct posix_acl_state *state, int cnt)
421 int alloc; 432 int alloc;
422 433
423 memset(state, 0, sizeof(struct posix_acl_state)); 434 memset(state, 0, sizeof(struct posix_acl_state));
435 state->empty = 1;
424 /* 436 /*
425 * In the worst case, each individual acl could be for a distinct 437 * In the worst case, each individual acl could be for a distinct
426 * named user or group, but we don't no which, so we allocate 438 * named user or group, but we don't no which, so we allocate
@@ -488,6 +500,20 @@ posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
488 int nace; 500 int nace;
489 int i, error = 0; 501 int i, error = 0;
490 502
503 /*
504 * ACLs with no ACEs are treated differently in the inheritable
505 * and effective cases: when there are no inheritable ACEs, we
506 * set a zero-length default posix acl:
507 */
508 if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) {
509 pacl = posix_acl_alloc(0, GFP_KERNEL);
510 return pacl ? pacl : ERR_PTR(-ENOMEM);
511 }
512 /*
513 * When there are no effective ACEs, the following will end
514 * up setting a 3-element effective posix ACL with all
515 * permissions zero.
516 */
491 nace = 4 + state->users->n + state->groups->n; 517 nace = 4 + state->users->n + state->groups->n;
492 pacl = posix_acl_alloc(nace, GFP_KERNEL); 518 pacl = posix_acl_alloc(nace, GFP_KERNEL);
493 if (!pacl) 519 if (!pacl)
@@ -603,6 +629,8 @@ static void process_one_v4_ace(struct posix_acl_state *state,
603 u32 mask = ace->access_mask; 629 u32 mask = ace->access_mask;
604 int i; 630 int i;
605 631
632 state->empty = 0;
633
606 switch (ace2type(ace)) { 634 switch (ace2type(ace)) {
607 case ACL_USER_OBJ: 635 case ACL_USER_OBJ:
608 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) { 636 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
@@ -666,75 +694,62 @@ static void process_one_v4_ace(struct posix_acl_state *state,
666 } 694 }
667} 695}
668 696
669static struct posix_acl * 697int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
670_nfsv4_to_posix_one(struct nfs4_acl *n4acl, unsigned int flags) 698 struct posix_acl **dpacl, unsigned int flags)
671{ 699{
672 struct posix_acl_state state; 700 struct posix_acl_state effective_acl_state, default_acl_state;
673 struct posix_acl *pacl;
674 struct nfs4_ace *ace; 701 struct nfs4_ace *ace;
675 int ret; 702 int ret;
676 703
677 ret = init_state(&state, n4acl->naces); 704 ret = init_state(&effective_acl_state, acl->naces);
678 if (ret) 705 if (ret)
679 return ERR_PTR(ret); 706 return ret;
680 707 ret = init_state(&default_acl_state, acl->naces);
681 list_for_each_entry(ace, &n4acl->ace_head, l_ace) 708 if (ret)
682 process_one_v4_ace(&state, ace); 709 goto out_estate;
683 710 ret = -EINVAL;
684 pacl = posix_state_to_acl(&state, flags); 711 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
685
686 free_state(&state);
687
688 if (!IS_ERR(pacl))
689 sort_pacl(pacl);
690 return pacl;
691}
692
693static int
694nfs4_acl_split(struct nfs4_acl *acl, struct nfs4_acl *dacl)
695{
696 struct list_head *h, *n;
697 struct nfs4_ace *ace;
698 int error = 0;
699
700 list_for_each_safe(h, n, &acl->ace_head) {
701 ace = list_entry(h, struct nfs4_ace, l_ace);
702
703 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE && 712 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
704 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE) 713 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
705 return -EINVAL; 714 goto out_dstate;
706
707 if (ace->flag & ~NFS4_SUPPORTED_FLAGS) 715 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
708 return -EINVAL; 716 goto out_dstate;
709 717 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
710 switch (ace->flag & NFS4_INHERITANCE_FLAGS) { 718 process_one_v4_ace(&effective_acl_state, ace);
711 case 0:
712 /* Leave this ace in the effective acl: */
713 continue; 719 continue;
714 case NFS4_INHERITANCE_FLAGS:
715 /* Add this ace to the default acl and remove it
716 * from the effective acl: */
717 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
718 ace->access_mask, ace->whotype, ace->who);
719 if (error)
720 return error;
721 list_del(h);
722 kfree(ace);
723 acl->naces--;
724 break;
725 case NFS4_INHERITANCE_FLAGS & ~NFS4_ACE_INHERIT_ONLY_ACE:
726 /* Add this ace to the default, but leave it in
727 * the effective acl as well: */
728 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
729 ace->access_mask, ace->whotype, ace->who);
730 if (error)
731 return error;
732 break;
733 default:
734 return -EINVAL;
735 } 720 }
721 if (!(flags & NFS4_ACL_DIR))
722 goto out_dstate;
723 /*
724 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
725 * is set, we're effectively turning on the other. That's OK,
726 * according to rfc 3530.
727 */
728 process_one_v4_ace(&default_acl_state, ace);
729
730 if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
731 process_one_v4_ace(&effective_acl_state, ace);
736 } 732 }
737 return 0; 733 *pacl = posix_state_to_acl(&effective_acl_state, flags);
734 if (IS_ERR(*pacl)) {
735 ret = PTR_ERR(*pacl);
736 goto out_dstate;
737 }
738 *dpacl = posix_state_to_acl(&default_acl_state,
739 flags | NFS4_ACL_TYPE_DEFAULT);
740 if (IS_ERR(*dpacl)) {
741 ret = PTR_ERR(*dpacl);
742 posix_acl_release(*pacl);
743 goto out_dstate;
744 }
745 sort_pacl(*pacl);
746 sort_pacl(*dpacl);
747 ret = 0;
748out_dstate:
749 free_state(&default_acl_state);
750out_estate:
751 free_state(&effective_acl_state);
752 return ret;
738} 753}
739 754
740static short 755static short
@@ -759,48 +774,22 @@ EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
759EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix); 774EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
760 775
761struct nfs4_acl * 776struct nfs4_acl *
762nfs4_acl_new(void) 777nfs4_acl_new(int n)
763{ 778{
764 struct nfs4_acl *acl; 779 struct nfs4_acl *acl;
765 780
766 if ((acl = kmalloc(sizeof(*acl), GFP_KERNEL)) == NULL) 781 acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL);
782 if (acl == NULL)
767 return NULL; 783 return NULL;
768
769 acl->naces = 0; 784 acl->naces = 0;
770 INIT_LIST_HEAD(&acl->ace_head);
771
772 return acl; 785 return acl;
773} 786}
774 787
775void 788void
776nfs4_acl_free(struct nfs4_acl *acl)
777{
778 struct list_head *h;
779 struct nfs4_ace *ace;
780
781 if (!acl)
782 return;
783
784 while (!list_empty(&acl->ace_head)) {
785 h = acl->ace_head.next;
786 list_del(h);
787 ace = list_entry(h, struct nfs4_ace, l_ace);
788 kfree(ace);
789 }
790
791 kfree(acl);
792
793 return;
794}
795
796int
797nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask, 789nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
798 int whotype, uid_t who) 790 int whotype, uid_t who)
799{ 791{
800 struct nfs4_ace *ace; 792 struct nfs4_ace *ace = acl->aces + acl->naces;
801
802 if ((ace = kmalloc(sizeof(*ace), GFP_KERNEL)) == NULL)
803 return -ENOMEM;
804 793
805 ace->type = type; 794 ace->type = type;
806 ace->flag = flag; 795 ace->flag = flag;
@@ -808,10 +797,7 @@ nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
808 ace->whotype = whotype; 797 ace->whotype = whotype;
809 ace->who = who; 798 ace->who = who;
810 799
811 list_add_tail(&ace->l_ace, &acl->ace_head);
812 acl->naces++; 800 acl->naces++;
813
814 return 0;
815} 801}
816 802
817static struct { 803static struct {
@@ -865,7 +851,6 @@ nfs4_acl_write_who(int who, char *p)
865} 851}
866 852
867EXPORT_SYMBOL(nfs4_acl_new); 853EXPORT_SYMBOL(nfs4_acl_new);
868EXPORT_SYMBOL(nfs4_acl_free);
869EXPORT_SYMBOL(nfs4_acl_add_ace); 854EXPORT_SYMBOL(nfs4_acl_add_ace);
870EXPORT_SYMBOL(nfs4_acl_get_whotype); 855EXPORT_SYMBOL(nfs4_acl_get_whotype);
871EXPORT_SYMBOL(nfs4_acl_write_who); 856EXPORT_SYMBOL(nfs4_acl_write_who);
diff --git a/fs/nfsd/nfs4callback.c b/fs/nfsd/nfs4callback.c
index f57655a7a2b6..fb14d68eacab 100644
--- a/fs/nfsd/nfs4callback.c
+++ b/fs/nfsd/nfs4callback.c
@@ -387,7 +387,6 @@ nfsd4_probe_callback(struct nfs4_client *clp)
387 .address = (struct sockaddr *)&addr, 387 .address = (struct sockaddr *)&addr,
388 .addrsize = sizeof(addr), 388 .addrsize = sizeof(addr),
389 .timeout = &timeparms, 389 .timeout = &timeparms,
390 .servername = clp->cl_name.data,
391 .program = program, 390 .program = program,
392 .version = nfs_cb_version[1]->number, 391 .version = nfs_cb_version[1]->number,
393 .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */ 392 .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */
@@ -397,6 +396,7 @@ nfsd4_probe_callback(struct nfs4_client *clp)
397 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL], 396 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
398 .rpc_argp = clp, 397 .rpc_argp = clp,
399 }; 398 };
399 char clientname[16];
400 int status; 400 int status;
401 401
402 if (atomic_read(&cb->cb_set)) 402 if (atomic_read(&cb->cb_set))
@@ -419,6 +419,11 @@ nfsd4_probe_callback(struct nfs4_client *clp)
419 memset(program->stats, 0, sizeof(cb->cb_stat)); 419 memset(program->stats, 0, sizeof(cb->cb_stat));
420 program->stats->program = program; 420 program->stats->program = program;
421 421
422 /* Just here to make some printk's more useful: */
423 snprintf(clientname, sizeof(clientname),
424 "%u.%u.%u.%u", NIPQUAD(addr.sin_addr));
425 args.servername = clientname;
426
422 /* Create RPC client */ 427 /* Create RPC client */
423 cb->cb_client = rpc_create(&args); 428 cb->cb_client = rpc_create(&args);
424 if (IS_ERR(cb->cb_client)) { 429 if (IS_ERR(cb->cb_client)) {
diff --git a/fs/nfsd/nfs4xdr.c b/fs/nfsd/nfs4xdr.c
index 0efba557fb55..5d090f11f2be 100644
--- a/fs/nfsd/nfs4xdr.c
+++ b/fs/nfsd/nfs4xdr.c
@@ -199,24 +199,22 @@ defer_free(struct nfsd4_compoundargs *argp,
199 199
200static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes) 200static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
201{ 201{
202 void *new = NULL;
203 if (p == argp->tmp) { 202 if (p == argp->tmp) {
204 new = kmalloc(nbytes, GFP_KERNEL); 203 p = kmalloc(nbytes, GFP_KERNEL);
205 if (!new) return NULL; 204 if (!p)
206 p = new; 205 return NULL;
207 memcpy(p, argp->tmp, nbytes); 206 memcpy(p, argp->tmp, nbytes);
208 } else { 207 } else {
209 BUG_ON(p != argp->tmpp); 208 BUG_ON(p != argp->tmpp);
210 argp->tmpp = NULL; 209 argp->tmpp = NULL;
211 } 210 }
212 if (defer_free(argp, kfree, p)) { 211 if (defer_free(argp, kfree, p)) {
213 kfree(new); 212 kfree(p);
214 return NULL; 213 return NULL;
215 } else 214 } else
216 return (char *)p; 215 return (char *)p;
217} 216}
218 217
219
220static __be32 218static __be32
221nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval) 219nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
222{ 220{
@@ -255,7 +253,7 @@ nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *ia
255 return status; 253 return status;
256 254
257 /* 255 /*
258 * According to spec, unsupported attributes return ERR_NOTSUPP; 256 * According to spec, unsupported attributes return ERR_ATTRNOTSUPP;
259 * read-only attributes return ERR_INVAL. 257 * read-only attributes return ERR_INVAL.
260 */ 258 */
261 if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1)) 259 if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
@@ -273,42 +271,42 @@ nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *ia
273 iattr->ia_valid |= ATTR_SIZE; 271 iattr->ia_valid |= ATTR_SIZE;
274 } 272 }
275 if (bmval[0] & FATTR4_WORD0_ACL) { 273 if (bmval[0] & FATTR4_WORD0_ACL) {
276 int nace, i; 274 int nace;
277 struct nfs4_ace ace; 275 struct nfs4_ace *ace;
278 276
279 READ_BUF(4); len += 4; 277 READ_BUF(4); len += 4;
280 READ32(nace); 278 READ32(nace);
281 279
282 *acl = nfs4_acl_new(); 280 if (nace > NFS4_ACL_MAX)
281 return nfserr_resource;
282
283 *acl = nfs4_acl_new(nace);
283 if (*acl == NULL) { 284 if (*acl == NULL) {
284 host_err = -ENOMEM; 285 host_err = -ENOMEM;
285 goto out_nfserr; 286 goto out_nfserr;
286 } 287 }
287 defer_free(argp, (void (*)(const void *))nfs4_acl_free, *acl); 288 defer_free(argp, kfree, *acl);
288 289
289 for (i = 0; i < nace; i++) { 290 (*acl)->naces = nace;
291 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
290 READ_BUF(16); len += 16; 292 READ_BUF(16); len += 16;
291 READ32(ace.type); 293 READ32(ace->type);
292 READ32(ace.flag); 294 READ32(ace->flag);
293 READ32(ace.access_mask); 295 READ32(ace->access_mask);
294 READ32(dummy32); 296 READ32(dummy32);
295 READ_BUF(dummy32); 297 READ_BUF(dummy32);
296 len += XDR_QUADLEN(dummy32) << 2; 298 len += XDR_QUADLEN(dummy32) << 2;
297 READMEM(buf, dummy32); 299 READMEM(buf, dummy32);
298 ace.whotype = nfs4_acl_get_whotype(buf, dummy32); 300 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
299 host_err = 0; 301 host_err = 0;
300 if (ace.whotype != NFS4_ACL_WHO_NAMED) 302 if (ace->whotype != NFS4_ACL_WHO_NAMED)
301 ace.who = 0; 303 ace->who = 0;
302 else if (ace.flag & NFS4_ACE_IDENTIFIER_GROUP) 304 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
303 host_err = nfsd_map_name_to_gid(argp->rqstp, 305 host_err = nfsd_map_name_to_gid(argp->rqstp,
304 buf, dummy32, &ace.who); 306 buf, dummy32, &ace->who);
305 else 307 else
306 host_err = nfsd_map_name_to_uid(argp->rqstp, 308 host_err = nfsd_map_name_to_uid(argp->rqstp,
307 buf, dummy32, &ace.who); 309 buf, dummy32, &ace->who);
308 if (host_err)
309 goto out_nfserr;
310 host_err = nfs4_acl_add_ace(*acl, ace.type, ace.flag,
311 ace.access_mask, ace.whotype, ace.who);
312 if (host_err) 310 if (host_err)
313 goto out_nfserr; 311 goto out_nfserr;
314 } 312 }
@@ -1596,7 +1594,6 @@ nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
1596 } 1594 }
1597 if (bmval0 & FATTR4_WORD0_ACL) { 1595 if (bmval0 & FATTR4_WORD0_ACL) {
1598 struct nfs4_ace *ace; 1596 struct nfs4_ace *ace;
1599 struct list_head *h;
1600 1597
1601 if (acl == NULL) { 1598 if (acl == NULL) {
1602 if ((buflen -= 4) < 0) 1599 if ((buflen -= 4) < 0)
@@ -1609,9 +1606,7 @@ nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
1609 goto out_resource; 1606 goto out_resource;
1610 WRITE32(acl->naces); 1607 WRITE32(acl->naces);
1611 1608
1612 list_for_each(h, &acl->ace_head) { 1609 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
1613 ace = list_entry(h, struct nfs4_ace, l_ace);
1614
1615 if ((buflen -= 4*3) < 0) 1610 if ((buflen -= 4*3) < 0)
1616 goto out_resource; 1611 goto out_resource;
1617 WRITE32(ace->type); 1612 WRITE32(ace->type);
@@ -1821,7 +1816,7 @@ out_acl:
1821 status = nfs_ok; 1816 status = nfs_ok;
1822 1817
1823out: 1818out:
1824 nfs4_acl_free(acl); 1819 kfree(acl);
1825 if (fhp == &tempfh) 1820 if (fhp == &tempfh)
1826 fh_put(&tempfh); 1821 fh_put(&tempfh);
1827 return status; 1822 return status;
diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c
index 8283236c6a0f..7e6aa245b5d5 100644
--- a/fs/nfsd/vfs.c
+++ b/fs/nfsd/vfs.c
@@ -466,7 +466,10 @@ out:
466 posix_acl_release(dpacl); 466 posix_acl_release(dpacl);
467 return (error); 467 return (error);
468out_nfserr: 468out_nfserr:
469 error = nfserrno(host_error); 469 if (host_error == -EOPNOTSUPP)
470 error = nfserr_attrnotsupp;
471 else
472 error = nfserrno(host_error);
470 goto out; 473 goto out;
471} 474}
472 475
diff --git a/fs/ocfs2/namei.c b/fs/ocfs2/namei.c
index f7fa52bb3f6b..28dd757ff67d 100644
--- a/fs/ocfs2/namei.c
+++ b/fs/ocfs2/namei.c
@@ -1098,7 +1098,7 @@ static int ocfs2_rename(struct inode *old_dir,
1098 BUG(); 1098 BUG();
1099 } 1099 }
1100 1100
1101 /* Assume a directory heirarchy thusly: 1101 /* Assume a directory hierarchy thusly:
1102 * a/b/c 1102 * a/b/c
1103 * a/d 1103 * a/d
1104 * a,b,c, and d are all directories. 1104 * a,b,c, and d are all directories.
diff --git a/fs/partitions/check.c b/fs/partitions/check.c
index ac32a2e8540c..22d38ffc9ef0 100644
--- a/fs/partitions/check.c
+++ b/fs/partitions/check.c
@@ -358,8 +358,7 @@ void delete_partition(struct gendisk *disk, int part)
358 p->ios[0] = p->ios[1] = 0; 358 p->ios[0] = p->ios[1] = 0;
359 p->sectors[0] = p->sectors[1] = 0; 359 p->sectors[0] = p->sectors[1] = 0;
360 sysfs_remove_link(&p->kobj, "subsystem"); 360 sysfs_remove_link(&p->kobj, "subsystem");
361 if (p->holder_dir) 361 kobject_unregister(p->holder_dir);
362 kobject_unregister(p->holder_dir);
363 kobject_uevent(&p->kobj, KOBJ_REMOVE); 362 kobject_uevent(&p->kobj, KOBJ_REMOVE);
364 kobject_del(&p->kobj); 363 kobject_del(&p->kobj);
365 kobject_put(&p->kobj); 364 kobject_put(&p->kobj);
@@ -603,10 +602,8 @@ void del_gendisk(struct gendisk *disk)
603 disk->stamp = 0; 602 disk->stamp = 0;
604 603
605 kobject_uevent(&disk->kobj, KOBJ_REMOVE); 604 kobject_uevent(&disk->kobj, KOBJ_REMOVE);
606 if (disk->holder_dir) 605 kobject_unregister(disk->holder_dir);
607 kobject_unregister(disk->holder_dir); 606 kobject_unregister(disk->slave_dir);
608 if (disk->slave_dir)
609 kobject_unregister(disk->slave_dir);
610 if (disk->driverfs_dev) { 607 if (disk->driverfs_dev) {
611 char *disk_name = make_block_name(disk); 608 char *disk_name = make_block_name(disk);
612 sysfs_remove_link(&disk->kobj, "device"); 609 sysfs_remove_link(&disk->kobj, "device");
diff --git a/fs/pipe.c b/fs/pipe.c
index 68090e84f589..ebafde7d6aba 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -16,6 +16,7 @@
16#include <linux/uio.h> 16#include <linux/uio.h>
17#include <linux/highmem.h> 17#include <linux/highmem.h>
18#include <linux/pagemap.h> 18#include <linux/pagemap.h>
19#include <linux/audit.h>
19 20
20#include <asm/uaccess.h> 21#include <asm/uaccess.h>
21#include <asm/ioctls.h> 22#include <asm/ioctls.h>
@@ -985,6 +986,10 @@ int do_pipe(int *fd)
985 goto err_fdr; 986 goto err_fdr;
986 fdw = error; 987 fdw = error;
987 988
989 error = audit_fd_pair(fdr, fdw);
990 if (error < 0)
991 goto err_fdw;
992
988 fd_install(fdr, fr); 993 fd_install(fdr, fr);
989 fd_install(fdw, fw); 994 fd_install(fdw, fw);
990 fd[0] = fdr; 995 fd[0] = fdr;
@@ -992,6 +997,8 @@ int do_pipe(int *fd)
992 997
993 return 0; 998 return 0;
994 999
1000 err_fdw:
1001 put_unused_fd(fdw);
995 err_fdr: 1002 err_fdr:
996 put_unused_fd(fdr); 1003 put_unused_fd(fdr);
997 err_read_pipe: 1004 err_read_pipe:
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 4f5745af8c19..01f7769da8e6 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -581,7 +581,7 @@ out_no_task:
581 581
582#ifndef mem_write 582#ifndef mem_write
583/* This is a security hazard */ 583/* This is a security hazard */
584static ssize_t mem_write(struct file * file, const char * buf, 584static ssize_t mem_write(struct file * file, const char __user *buf,
585 size_t count, loff_t *ppos) 585 size_t count, loff_t *ppos)
586{ 586{
587 int copied; 587 int copied;
diff --git a/fs/stack.c b/fs/stack.c
index 8ffb880d2f46..67716f6a1a4a 100644
--- a/fs/stack.c
+++ b/fs/stack.c
@@ -20,11 +20,6 @@ EXPORT_SYMBOL_GPL(fsstack_copy_inode_size);
20void fsstack_copy_attr_all(struct inode *dest, const struct inode *src, 20void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
21 int (*get_nlinks)(struct inode *)) 21 int (*get_nlinks)(struct inode *))
22{ 22{
23 if (!get_nlinks)
24 dest->i_nlink = src->i_nlink;
25 else
26 dest->i_nlink = (*get_nlinks)(dest);
27
28 dest->i_mode = src->i_mode; 23 dest->i_mode = src->i_mode;
29 dest->i_uid = src->i_uid; 24 dest->i_uid = src->i_uid;
30 dest->i_gid = src->i_gid; 25 dest->i_gid = src->i_gid;
@@ -34,5 +29,14 @@ void fsstack_copy_attr_all(struct inode *dest, const struct inode *src,
34 dest->i_ctime = src->i_ctime; 29 dest->i_ctime = src->i_ctime;
35 dest->i_blkbits = src->i_blkbits; 30 dest->i_blkbits = src->i_blkbits;
36 dest->i_flags = src->i_flags; 31 dest->i_flags = src->i_flags;
32
33 /*
34 * Update the nlinks AFTER updating the above fields, because the
35 * get_links callback may depend on them.
36 */
37 if (!get_nlinks)
38 dest->i_nlink = src->i_nlink;
39 else
40 dest->i_nlink = (*get_nlinks)(dest);
37} 41}
38EXPORT_SYMBOL_GPL(fsstack_copy_attr_all); 42EXPORT_SYMBOL_GPL(fsstack_copy_attr_all);
diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c
index c0e117649a4d..98b0910ad80c 100644
--- a/fs/sysfs/file.c
+++ b/fs/sysfs/file.c
@@ -54,7 +54,7 @@ static struct sysfs_ops subsys_sysfs_ops = {
54/** 54/**
55 * add_to_collection - add buffer to a collection 55 * add_to_collection - add buffer to a collection
56 * @buffer: buffer to be added 56 * @buffer: buffer to be added
57 * @node inode of set to add to 57 * @node: inode of set to add to
58 */ 58 */
59 59
60static inline void 60static inline void
diff --git a/fs/xfs/linux-2.6/xfs_super.c b/fs/xfs/linux-2.6/xfs_super.c
index 1a4103ca593c..2f2c40db562e 100644
--- a/fs/xfs/linux-2.6/xfs_super.c
+++ b/fs/xfs/linux-2.6/xfs_super.c
@@ -900,7 +900,7 @@ static struct quotactl_ops xfs_quotactl_operations = {
900 .set_xquota = xfs_fs_setxquota, 900 .set_xquota = xfs_fs_setxquota,
901}; 901};
902 902
903STATIC struct file_system_type xfs_fs_type = { 903static struct file_system_type xfs_fs_type = {
904 .owner = THIS_MODULE, 904 .owner = THIS_MODULE,
905 .name = "xfs", 905 .name = "xfs",
906 .get_sb = xfs_fs_get_sb, 906 .get_sb = xfs_fs_get_sb,