aboutsummaryrefslogtreecommitdiffstats
path: root/fs/jffs2
diff options
context:
space:
mode:
Diffstat (limited to 'fs/jffs2')
-rw-r--r--fs/jffs2/Makefile3
-rw-r--r--fs/jffs2/README.Locking21
-rw-r--r--fs/jffs2/acl.c487
-rw-r--r--fs/jffs2/acl.h45
-rw-r--r--fs/jffs2/build.c2
-rw-r--r--fs/jffs2/compr.c2
-rw-r--r--fs/jffs2/compr.h4
-rw-r--r--fs/jffs2/compr_zlib.c1
-rw-r--r--fs/jffs2/debug.c14
-rw-r--r--fs/jffs2/debug.h7
-rw-r--r--fs/jffs2/dir.c121
-rw-r--r--fs/jffs2/erase.c88
-rw-r--r--fs/jffs2/file.c37
-rw-r--r--fs/jffs2/fs.c68
-rw-r--r--fs/jffs2/gc.c131
-rw-r--r--fs/jffs2/histo.h3
-rw-r--r--fs/jffs2/jffs2_fs_i.h55
-rw-r--r--fs/jffs2/jffs2_fs_sb.h136
-rw-r--r--fs/jffs2/malloc.c129
-rw-r--r--fs/jffs2/nodelist.c186
-rw-r--r--fs/jffs2/nodelist.h191
-rw-r--r--fs/jffs2/nodemgmt.c210
-rw-r--r--fs/jffs2/os-linux.h27
-rw-r--r--fs/jffs2/readinode.c138
-rw-r--r--fs/jffs2/scan.c449
-rw-r--r--fs/jffs2/security.c82
-rw-r--r--fs/jffs2/summary.c475
-rw-r--r--fs/jffs2/summary.h64
-rw-r--r--fs/jffs2/super.c70
-rw-r--r--fs/jffs2/symlink.c7
-rw-r--r--fs/jffs2/wbuf.c971
-rw-r--r--fs/jffs2/write.c147
-rw-r--r--fs/jffs2/xattr.c1326
-rw-r--r--fs/jffs2/xattr.h129
-rw-r--r--fs/jffs2/xattr_trusted.c52
-rw-r--r--fs/jffs2/xattr_user.c52
36 files changed, 4505 insertions, 1425 deletions
diff --git a/fs/jffs2/Makefile b/fs/jffs2/Makefile
index 77dc5561a04e..7f28ee0bd132 100644
--- a/fs/jffs2/Makefile
+++ b/fs/jffs2/Makefile
@@ -12,6 +12,9 @@ jffs2-y += symlink.o build.o erase.o background.o fs.o writev.o
12jffs2-y += super.o debug.o 12jffs2-y += super.o debug.o
13 13
14jffs2-$(CONFIG_JFFS2_FS_WRITEBUFFER) += wbuf.o 14jffs2-$(CONFIG_JFFS2_FS_WRITEBUFFER) += wbuf.o
15jffs2-$(CONFIG_JFFS2_FS_XATTR) += xattr.o xattr_trusted.o xattr_user.o
16jffs2-$(CONFIG_JFFS2_FS_SECURITY) += security.o
17jffs2-$(CONFIG_JFFS2_FS_POSIX_ACL) += acl.o
15jffs2-$(CONFIG_JFFS2_RUBIN) += compr_rubin.o 18jffs2-$(CONFIG_JFFS2_RUBIN) += compr_rubin.o
16jffs2-$(CONFIG_JFFS2_RTIME) += compr_rtime.o 19jffs2-$(CONFIG_JFFS2_RTIME) += compr_rtime.o
17jffs2-$(CONFIG_JFFS2_ZLIB) += compr_zlib.o 20jffs2-$(CONFIG_JFFS2_ZLIB) += compr_zlib.o
diff --git a/fs/jffs2/README.Locking b/fs/jffs2/README.Locking
index b7943439b6ec..c8f0bd64e53e 100644
--- a/fs/jffs2/README.Locking
+++ b/fs/jffs2/README.Locking
@@ -150,3 +150,24 @@ the buffer.
150 150
151Ordering constraints: 151Ordering constraints:
152 Lock wbuf_sem last, after the alloc_sem or and f->sem. 152 Lock wbuf_sem last, after the alloc_sem or and f->sem.
153
154
155 c->xattr_sem
156 ------------
157
158This read/write semaphore protects against concurrent access to the
159xattr related objects which include stuff in superblock and ic->xref.
160In read-only path, write-semaphore is too much exclusion. It's enough
161by read-semaphore. But you must hold write-semaphore when updating,
162creating or deleting any xattr related object.
163
164Once xattr_sem released, there would be no assurance for the existence
165of those objects. Thus, a series of processes is often required to retry,
166when updating such a object is necessary under holding read semaphore.
167For example, do_jffs2_getxattr() holds read-semaphore to scan xref and
168xdatum at first. But it retries this process with holding write-semaphore
169after release read-semaphore, if it's necessary to load name/value pair
170from medium.
171
172Ordering constraints:
173 Lock xattr_sem last, after the alloc_sem.
diff --git a/fs/jffs2/acl.c b/fs/jffs2/acl.c
new file mode 100644
index 000000000000..9c2077e7e081
--- /dev/null
+++ b/fs/jffs2/acl.c
@@ -0,0 +1,487 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/time.h>
15#include <linux/crc32.h>
16#include <linux/jffs2.h>
17#include <linux/xattr.h>
18#include <linux/posix_acl_xattr.h>
19#include <linux/mtd/mtd.h>
20#include "nodelist.h"
21
22static size_t jffs2_acl_size(int count)
23{
24 if (count <= 4) {
25 return sizeof(struct jffs2_acl_header)
26 + count * sizeof(struct jffs2_acl_entry_short);
27 } else {
28 return sizeof(struct jffs2_acl_header)
29 + 4 * sizeof(struct jffs2_acl_entry_short)
30 + (count - 4) * sizeof(struct jffs2_acl_entry);
31 }
32}
33
34static int jffs2_acl_count(size_t size)
35{
36 size_t s;
37
38 size -= sizeof(struct jffs2_acl_header);
39 s = size - 4 * sizeof(struct jffs2_acl_entry_short);
40 if (s < 0) {
41 if (size % sizeof(struct jffs2_acl_entry_short))
42 return -1;
43 return size / sizeof(struct jffs2_acl_entry_short);
44 } else {
45 if (s % sizeof(struct jffs2_acl_entry))
46 return -1;
47 return s / sizeof(struct jffs2_acl_entry) + 4;
48 }
49}
50
51static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
52{
53 void *end = value + size;
54 struct jffs2_acl_header *header = value;
55 struct jffs2_acl_entry *entry;
56 struct posix_acl *acl;
57 uint32_t ver;
58 int i, count;
59
60 if (!value)
61 return NULL;
62 if (size < sizeof(struct jffs2_acl_header))
63 return ERR_PTR(-EINVAL);
64 ver = je32_to_cpu(header->a_version);
65 if (ver != JFFS2_ACL_VERSION) {
66 JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
67 return ERR_PTR(-EINVAL);
68 }
69
70 value += sizeof(struct jffs2_acl_header);
71 count = jffs2_acl_count(size);
72 if (count < 0)
73 return ERR_PTR(-EINVAL);
74 if (count == 0)
75 return NULL;
76
77 acl = posix_acl_alloc(count, GFP_KERNEL);
78 if (!acl)
79 return ERR_PTR(-ENOMEM);
80
81 for (i=0; i < count; i++) {
82 entry = value;
83 if (value + sizeof(struct jffs2_acl_entry_short) > end)
84 goto fail;
85 acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
86 acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
87 switch (acl->a_entries[i].e_tag) {
88 case ACL_USER_OBJ:
89 case ACL_GROUP_OBJ:
90 case ACL_MASK:
91 case ACL_OTHER:
92 value += sizeof(struct jffs2_acl_entry_short);
93 acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
94 break;
95
96 case ACL_USER:
97 case ACL_GROUP:
98 value += sizeof(struct jffs2_acl_entry);
99 if (value > end)
100 goto fail;
101 acl->a_entries[i].e_id = je32_to_cpu(entry->e_id);
102 break;
103
104 default:
105 goto fail;
106 }
107 }
108 if (value != end)
109 goto fail;
110 return acl;
111 fail:
112 posix_acl_release(acl);
113 return ERR_PTR(-EINVAL);
114}
115
116static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
117{
118 struct jffs2_acl_header *header;
119 struct jffs2_acl_entry *entry;
120 void *e;
121 size_t i;
122
123 *size = jffs2_acl_size(acl->a_count);
124 header = kmalloc(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
125 if (!header)
126 return ERR_PTR(-ENOMEM);
127 header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
128 e = header + 1;
129 for (i=0; i < acl->a_count; i++) {
130 entry = e;
131 entry->e_tag = cpu_to_je16(acl->a_entries[i].e_tag);
132 entry->e_perm = cpu_to_je16(acl->a_entries[i].e_perm);
133 switch(acl->a_entries[i].e_tag) {
134 case ACL_USER:
135 case ACL_GROUP:
136 entry->e_id = cpu_to_je32(acl->a_entries[i].e_id);
137 e += sizeof(struct jffs2_acl_entry);
138 break;
139
140 case ACL_USER_OBJ:
141 case ACL_GROUP_OBJ:
142 case ACL_MASK:
143 case ACL_OTHER:
144 e += sizeof(struct jffs2_acl_entry_short);
145 break;
146
147 default:
148 goto fail;
149 }
150 }
151 return header;
152 fail:
153 kfree(header);
154 return ERR_PTR(-EINVAL);
155}
156
157static struct posix_acl *jffs2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
158{
159 struct posix_acl *acl = JFFS2_ACL_NOT_CACHED;
160
161 spin_lock(&inode->i_lock);
162 if (*i_acl != JFFS2_ACL_NOT_CACHED)
163 acl = posix_acl_dup(*i_acl);
164 spin_unlock(&inode->i_lock);
165 return acl;
166}
167
168static void jffs2_iset_acl(struct inode *inode, struct posix_acl **i_acl, struct posix_acl *acl)
169{
170 spin_lock(&inode->i_lock);
171 if (*i_acl != JFFS2_ACL_NOT_CACHED)
172 posix_acl_release(*i_acl);
173 *i_acl = posix_acl_dup(acl);
174 spin_unlock(&inode->i_lock);
175}
176
177static struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
178{
179 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
180 struct posix_acl *acl;
181 char *value = NULL;
182 int rc, xprefix;
183
184 switch (type) {
185 case ACL_TYPE_ACCESS:
186 acl = jffs2_iget_acl(inode, &f->i_acl_access);
187 if (acl != JFFS2_ACL_NOT_CACHED)
188 return acl;
189 xprefix = JFFS2_XPREFIX_ACL_ACCESS;
190 break;
191 case ACL_TYPE_DEFAULT:
192 acl = jffs2_iget_acl(inode, &f->i_acl_default);
193 if (acl != JFFS2_ACL_NOT_CACHED)
194 return acl;
195 xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
196 break;
197 default:
198 return ERR_PTR(-EINVAL);
199 }
200 rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
201 if (rc > 0) {
202 value = kmalloc(rc, GFP_KERNEL);
203 if (!value)
204 return ERR_PTR(-ENOMEM);
205 rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
206 }
207 if (rc > 0) {
208 acl = jffs2_acl_from_medium(value, rc);
209 } else if (rc == -ENODATA || rc == -ENOSYS) {
210 acl = NULL;
211 } else {
212 acl = ERR_PTR(rc);
213 }
214 if (value)
215 kfree(value);
216 if (!IS_ERR(acl)) {
217 switch (type) {
218 case ACL_TYPE_ACCESS:
219 jffs2_iset_acl(inode, &f->i_acl_access, acl);
220 break;
221 case ACL_TYPE_DEFAULT:
222 jffs2_iset_acl(inode, &f->i_acl_default, acl);
223 break;
224 }
225 }
226 return acl;
227}
228
229static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
230{
231 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
232 size_t size = 0;
233 char *value = NULL;
234 int rc, xprefix;
235
236 if (S_ISLNK(inode->i_mode))
237 return -EOPNOTSUPP;
238
239 switch (type) {
240 case ACL_TYPE_ACCESS:
241 xprefix = JFFS2_XPREFIX_ACL_ACCESS;
242 if (acl) {
243 mode_t mode = inode->i_mode;
244 rc = posix_acl_equiv_mode(acl, &mode);
245 if (rc < 0)
246 return rc;
247 if (inode->i_mode != mode) {
248 inode->i_mode = mode;
249 jffs2_dirty_inode(inode);
250 }
251 if (rc == 0)
252 acl = NULL;
253 }
254 break;
255 case ACL_TYPE_DEFAULT:
256 xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
257 if (!S_ISDIR(inode->i_mode))
258 return acl ? -EACCES : 0;
259 break;
260 default:
261 return -EINVAL;
262 }
263 if (acl) {
264 value = jffs2_acl_to_medium(acl, &size);
265 if (IS_ERR(value))
266 return PTR_ERR(value);
267 }
268
269 rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
270 if (!value && rc == -ENODATA)
271 rc = 0;
272 if (value)
273 kfree(value);
274 if (!rc) {
275 switch(type) {
276 case ACL_TYPE_ACCESS:
277 jffs2_iset_acl(inode, &f->i_acl_access, acl);
278 break;
279 case ACL_TYPE_DEFAULT:
280 jffs2_iset_acl(inode, &f->i_acl_default, acl);
281 break;
282 }
283 }
284 return rc;
285}
286
287static int jffs2_check_acl(struct inode *inode, int mask)
288{
289 struct posix_acl *acl;
290 int rc;
291
292 acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
293 if (IS_ERR(acl))
294 return PTR_ERR(acl);
295 if (acl) {
296 rc = posix_acl_permission(inode, acl, mask);
297 posix_acl_release(acl);
298 return rc;
299 }
300 return -EAGAIN;
301}
302
303int jffs2_permission(struct inode *inode, int mask, struct nameidata *nd)
304{
305 return generic_permission(inode, mask, jffs2_check_acl);
306}
307
308int jffs2_init_acl(struct inode *inode, struct inode *dir)
309{
310 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
311 struct posix_acl *acl = NULL, *clone;
312 mode_t mode;
313 int rc = 0;
314
315 f->i_acl_access = JFFS2_ACL_NOT_CACHED;
316 f->i_acl_default = JFFS2_ACL_NOT_CACHED;
317 if (!S_ISLNK(inode->i_mode)) {
318 acl = jffs2_get_acl(dir, ACL_TYPE_DEFAULT);
319 if (IS_ERR(acl))
320 return PTR_ERR(acl);
321 if (!acl)
322 inode->i_mode &= ~current->fs->umask;
323 }
324 if (acl) {
325 if (S_ISDIR(inode->i_mode)) {
326 rc = jffs2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
327 if (rc)
328 goto cleanup;
329 }
330 clone = posix_acl_clone(acl, GFP_KERNEL);
331 rc = -ENOMEM;
332 if (!clone)
333 goto cleanup;
334 mode = inode->i_mode;
335 rc = posix_acl_create_masq(clone, &mode);
336 if (rc >= 0) {
337 inode->i_mode = mode;
338 if (rc > 0)
339 rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
340 }
341 posix_acl_release(clone);
342 }
343 cleanup:
344 posix_acl_release(acl);
345 return rc;
346}
347
348void jffs2_clear_acl(struct inode *inode)
349{
350 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
351
352 if (f->i_acl_access && f->i_acl_access != JFFS2_ACL_NOT_CACHED) {
353 posix_acl_release(f->i_acl_access);
354 f->i_acl_access = JFFS2_ACL_NOT_CACHED;
355 }
356 if (f->i_acl_default && f->i_acl_default != JFFS2_ACL_NOT_CACHED) {
357 posix_acl_release(f->i_acl_default);
358 f->i_acl_default = JFFS2_ACL_NOT_CACHED;
359 }
360}
361
362int jffs2_acl_chmod(struct inode *inode)
363{
364 struct posix_acl *acl, *clone;
365 int rc;
366
367 if (S_ISLNK(inode->i_mode))
368 return -EOPNOTSUPP;
369 acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
370 if (IS_ERR(acl) || !acl)
371 return PTR_ERR(acl);
372 clone = posix_acl_clone(acl, GFP_KERNEL);
373 posix_acl_release(acl);
374 if (!clone)
375 return -ENOMEM;
376 rc = posix_acl_chmod_masq(clone, inode->i_mode);
377 if (!rc)
378 rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
379 posix_acl_release(clone);
380 return rc;
381}
382
383static size_t jffs2_acl_access_listxattr(struct inode *inode, char *list, size_t list_size,
384 const char *name, size_t name_len)
385{
386 const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);
387
388 if (list && retlen <= list_size)
389 strcpy(list, POSIX_ACL_XATTR_ACCESS);
390 return retlen;
391}
392
393static size_t jffs2_acl_default_listxattr(struct inode *inode, char *list, size_t list_size,
394 const char *name, size_t name_len)
395{
396 const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);
397
398 if (list && retlen <= list_size)
399 strcpy(list, POSIX_ACL_XATTR_DEFAULT);
400 return retlen;
401}
402
403static int jffs2_acl_getxattr(struct inode *inode, int type, void *buffer, size_t size)
404{
405 struct posix_acl *acl;
406 int rc;
407
408 acl = jffs2_get_acl(inode, type);
409 if (IS_ERR(acl))
410 return PTR_ERR(acl);
411 if (!acl)
412 return -ENODATA;
413 rc = posix_acl_to_xattr(acl, buffer, size);
414 posix_acl_release(acl);
415
416 return rc;
417}
418
419static int jffs2_acl_access_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
420{
421 if (name[0] != '\0')
422 return -EINVAL;
423 return jffs2_acl_getxattr(inode, ACL_TYPE_ACCESS, buffer, size);
424}
425
426static int jffs2_acl_default_getxattr(struct inode *inode, const char *name, void *buffer, size_t size)
427{
428 if (name[0] != '\0')
429 return -EINVAL;
430 return jffs2_acl_getxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
431}
432
433static int jffs2_acl_setxattr(struct inode *inode, int type, const void *value, size_t size)
434{
435 struct posix_acl *acl;
436 int rc;
437
438 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
439 return -EPERM;
440
441 if (value) {
442 acl = posix_acl_from_xattr(value, size);
443 if (IS_ERR(acl))
444 return PTR_ERR(acl);
445 if (acl) {
446 rc = posix_acl_valid(acl);
447 if (rc)
448 goto out;
449 }
450 } else {
451 acl = NULL;
452 }
453 rc = jffs2_set_acl(inode, type, acl);
454 out:
455 posix_acl_release(acl);
456 return rc;
457}
458
459static int jffs2_acl_access_setxattr(struct inode *inode, const char *name,
460 const void *buffer, size_t size, int flags)
461{
462 if (name[0] != '\0')
463 return -EINVAL;
464 return jffs2_acl_setxattr(inode, ACL_TYPE_ACCESS, buffer, size);
465}
466
467static int jffs2_acl_default_setxattr(struct inode *inode, const char *name,
468 const void *buffer, size_t size, int flags)
469{
470 if (name[0] != '\0')
471 return -EINVAL;
472 return jffs2_acl_setxattr(inode, ACL_TYPE_DEFAULT, buffer, size);
473}
474
475struct xattr_handler jffs2_acl_access_xattr_handler = {
476 .prefix = POSIX_ACL_XATTR_ACCESS,
477 .list = jffs2_acl_access_listxattr,
478 .get = jffs2_acl_access_getxattr,
479 .set = jffs2_acl_access_setxattr,
480};
481
482struct xattr_handler jffs2_acl_default_xattr_handler = {
483 .prefix = POSIX_ACL_XATTR_DEFAULT,
484 .list = jffs2_acl_default_listxattr,
485 .get = jffs2_acl_default_getxattr,
486 .set = jffs2_acl_default_setxattr,
487};
diff --git a/fs/jffs2/acl.h b/fs/jffs2/acl.h
new file mode 100644
index 000000000000..8893bd1a6ba7
--- /dev/null
+++ b/fs/jffs2/acl.h
@@ -0,0 +1,45 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11struct jffs2_acl_entry {
12 jint16_t e_tag;
13 jint16_t e_perm;
14 jint32_t e_id;
15};
16
17struct jffs2_acl_entry_short {
18 jint16_t e_tag;
19 jint16_t e_perm;
20};
21
22struct jffs2_acl_header {
23 jint32_t a_version;
24};
25
26#ifdef CONFIG_JFFS2_FS_POSIX_ACL
27
28#define JFFS2_ACL_NOT_CACHED ((void *)-1)
29
30extern int jffs2_permission(struct inode *, int, struct nameidata *);
31extern int jffs2_acl_chmod(struct inode *);
32extern int jffs2_init_acl(struct inode *, struct inode *);
33extern void jffs2_clear_acl(struct inode *);
34
35extern struct xattr_handler jffs2_acl_access_xattr_handler;
36extern struct xattr_handler jffs2_acl_default_xattr_handler;
37
38#else
39
40#define jffs2_permission NULL
41#define jffs2_acl_chmod(inode) (0)
42#define jffs2_init_acl(inode,dir) (0)
43#define jffs2_clear_acl(inode)
44
45#endif /* CONFIG_JFFS2_FS_POSIX_ACL */
diff --git a/fs/jffs2/build.c b/fs/jffs2/build.c
index 70f7a896c04a..02826967ab58 100644
--- a/fs/jffs2/build.c
+++ b/fs/jffs2/build.c
@@ -160,6 +160,7 @@ static int jffs2_build_filesystem(struct jffs2_sb_info *c)
160 ic->scan_dents = NULL; 160 ic->scan_dents = NULL;
161 cond_resched(); 161 cond_resched();
162 } 162 }
163 jffs2_build_xattr_subsystem(c);
163 c->flags &= ~JFFS2_SB_FLAG_BUILDING; 164 c->flags &= ~JFFS2_SB_FLAG_BUILDING;
164 165
165 dbg_fsbuild("FS build complete\n"); 166 dbg_fsbuild("FS build complete\n");
@@ -178,6 +179,7 @@ exit:
178 jffs2_free_full_dirent(fd); 179 jffs2_free_full_dirent(fd);
179 } 180 }
180 } 181 }
182 jffs2_clear_xattr_subsystem(c);
181 } 183 }
182 184
183 return ret; 185 return ret;
diff --git a/fs/jffs2/compr.c b/fs/jffs2/compr.c
index e7944e665b9f..7001ba26c067 100644
--- a/fs/jffs2/compr.c
+++ b/fs/jffs2/compr.c
@@ -412,7 +412,7 @@ void jffs2_free_comprbuf(unsigned char *comprbuf, unsigned char *orig)
412 kfree(comprbuf); 412 kfree(comprbuf);
413} 413}
414 414
415int jffs2_compressors_init(void) 415int __init jffs2_compressors_init(void)
416{ 416{
417/* Registering compressors */ 417/* Registering compressors */
418#ifdef CONFIG_JFFS2_ZLIB 418#ifdef CONFIG_JFFS2_ZLIB
diff --git a/fs/jffs2/compr.h b/fs/jffs2/compr.h
index a77e830d85c5..509b8b1c0811 100644
--- a/fs/jffs2/compr.h
+++ b/fs/jffs2/compr.h
@@ -23,8 +23,8 @@
23#include <linux/errno.h> 23#include <linux/errno.h>
24#include <linux/fs.h> 24#include <linux/fs.h>
25#include <linux/jffs2.h> 25#include <linux/jffs2.h>
26#include <linux/jffs2_fs_i.h> 26#include "jffs2_fs_i.h"
27#include <linux/jffs2_fs_sb.h> 27#include "jffs2_fs_sb.h"
28#include "nodelist.h" 28#include "nodelist.h"
29 29
30#define JFFS2_RUBINMIPS_PRIORITY 10 30#define JFFS2_RUBINMIPS_PRIORITY 10
diff --git a/fs/jffs2/compr_zlib.c b/fs/jffs2/compr_zlib.c
index 5c63e0cdcf4c..3681d0728ac7 100644
--- a/fs/jffs2/compr_zlib.c
+++ b/fs/jffs2/compr_zlib.c
@@ -15,7 +15,6 @@
15#error "The userspace support got too messy and was removed. Update your mkfs.jffs2" 15#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
16#endif 16#endif
17 17
18#include <linux/config.h>
19#include <linux/kernel.h> 18#include <linux/kernel.h>
20#include <linux/sched.h> 19#include <linux/sched.h>
21#include <linux/slab.h> 20#include <linux/slab.h>
diff --git a/fs/jffs2/debug.c b/fs/jffs2/debug.c
index 1fe17de713e8..72b4fc13a106 100644
--- a/fs/jffs2/debug.c
+++ b/fs/jffs2/debug.c
@@ -192,13 +192,13 @@ __jffs2_dbg_acct_paranoia_check_nolock(struct jffs2_sb_info *c,
192 else 192 else
193 my_dirty_size += totlen; 193 my_dirty_size += totlen;
194 194
195 if ((!ref2->next_phys) != (ref2 == jeb->last_node)) { 195 if ((!ref_next(ref2)) != (ref2 == jeb->last_node)) {
196 JFFS2_ERROR("node_ref for node at %#08x (mem %p) has next_phys at %#08x (mem %p), last_node is at %#08x (mem %p).\n", 196 JFFS2_ERROR("node_ref for node at %#08x (mem %p) has next at %#08x (mem %p), last_node is at %#08x (mem %p).\n",
197 ref_offset(ref2), ref2, ref_offset(ref2->next_phys), ref2->next_phys, 197 ref_offset(ref2), ref2, ref_offset(ref_next(ref2)), ref_next(ref2),
198 ref_offset(jeb->last_node), jeb->last_node); 198 ref_offset(jeb->last_node), jeb->last_node);
199 goto error; 199 goto error;
200 } 200 }
201 ref2 = ref2->next_phys; 201 ref2 = ref_next(ref2);
202 } 202 }
203 203
204 if (my_used_size != jeb->used_size) { 204 if (my_used_size != jeb->used_size) {
@@ -268,9 +268,9 @@ __jffs2_dbg_dump_node_refs_nolock(struct jffs2_sb_info *c,
268 } 268 }
269 269
270 printk(JFFS2_DBG); 270 printk(JFFS2_DBG);
271 for (ref = jeb->first_node; ; ref = ref->next_phys) { 271 for (ref = jeb->first_node; ; ref = ref_next(ref)) {
272 printk("%#08x(%#x)", ref_offset(ref), ref->__totlen); 272 printk("%#08x(%#x)", ref_offset(ref), ref->__totlen);
273 if (ref->next_phys) 273 if (ref_next(ref))
274 printk("->"); 274 printk("->");
275 else 275 else
276 break; 276 break;
diff --git a/fs/jffs2/debug.h b/fs/jffs2/debug.h
index 162af6dfe292..3daf3bca0376 100644
--- a/fs/jffs2/debug.h
+++ b/fs/jffs2/debug.h
@@ -13,7 +13,6 @@
13#ifndef _JFFS2_DEBUG_H_ 13#ifndef _JFFS2_DEBUG_H_
14#define _JFFS2_DEBUG_H_ 14#define _JFFS2_DEBUG_H_
15 15
16#include <linux/config.h>
17 16
18#ifndef CONFIG_JFFS2_FS_DEBUG 17#ifndef CONFIG_JFFS2_FS_DEBUG
19#define CONFIG_JFFS2_FS_DEBUG 0 18#define CONFIG_JFFS2_FS_DEBUG 0
@@ -171,6 +170,12 @@
171#define dbg_memalloc(fmt, ...) 170#define dbg_memalloc(fmt, ...)
172#endif 171#endif
173 172
173/* Watch the XATTR subsystem */
174#ifdef JFFS2_DBG_XATTR_MESSAGES
175#define dbg_xattr(fmt, ...) JFFS2_DEBUG(fmt, ##__VA_ARGS__)
176#else
177#define dbg_xattr(fmt, ...)
178#endif
174 179
175/* "Sanity" checks */ 180/* "Sanity" checks */
176void 181void
diff --git a/fs/jffs2/dir.c b/fs/jffs2/dir.c
index 8bc7a5018e40..edd8371fc6a5 100644
--- a/fs/jffs2/dir.c
+++ b/fs/jffs2/dir.c
@@ -17,8 +17,8 @@
17#include <linux/fs.h> 17#include <linux/fs.h>
18#include <linux/crc32.h> 18#include <linux/crc32.h>
19#include <linux/jffs2.h> 19#include <linux/jffs2.h>
20#include <linux/jffs2_fs_i.h> 20#include "jffs2_fs_i.h"
21#include <linux/jffs2_fs_sb.h> 21#include "jffs2_fs_sb.h"
22#include <linux/time.h> 22#include <linux/time.h>
23#include "nodelist.h" 23#include "nodelist.h"
24 24
@@ -57,7 +57,12 @@ struct inode_operations jffs2_dir_inode_operations =
57 .rmdir = jffs2_rmdir, 57 .rmdir = jffs2_rmdir,
58 .mknod = jffs2_mknod, 58 .mknod = jffs2_mknod,
59 .rename = jffs2_rename, 59 .rename = jffs2_rename,
60 .permission = jffs2_permission,
60 .setattr = jffs2_setattr, 61 .setattr = jffs2_setattr,
62 .setxattr = jffs2_setxattr,
63 .getxattr = jffs2_getxattr,
64 .listxattr = jffs2_listxattr,
65 .removexattr = jffs2_removexattr
61}; 66};
62 67
63/***********************************************************************/ 68/***********************************************************************/
@@ -78,6 +83,9 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
78 83
79 D1(printk(KERN_DEBUG "jffs2_lookup()\n")); 84 D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
80 85
86 if (target->d_name.len > JFFS2_MAX_NAME_LEN)
87 return ERR_PTR(-ENAMETOOLONG);
88
81 dir_f = JFFS2_INODE_INFO(dir_i); 89 dir_f = JFFS2_INODE_INFO(dir_i);
82 c = JFFS2_SB_INFO(dir_i->i_sb); 90 c = JFFS2_SB_INFO(dir_i->i_sb);
83 91
@@ -206,12 +214,15 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
206 ret = jffs2_do_create(c, dir_f, f, ri, 214 ret = jffs2_do_create(c, dir_f, f, ri,
207 dentry->d_name.name, dentry->d_name.len); 215 dentry->d_name.name, dentry->d_name.len);
208 216
209 if (ret) { 217 if (ret)
210 make_bad_inode(inode); 218 goto fail;
211 iput(inode); 219
212 jffs2_free_raw_inode(ri); 220 ret = jffs2_init_security(inode, dir_i);
213 return ret; 221 if (ret)
214 } 222 goto fail;
223 ret = jffs2_init_acl(inode, dir_i);
224 if (ret)
225 goto fail;
215 226
216 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime)); 227 dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
217 228
@@ -221,6 +232,12 @@ static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
221 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n", 232 D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
222 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages)); 233 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
223 return 0; 234 return 0;
235
236 fail:
237 make_bad_inode(inode);
238 iput(inode);
239 jffs2_free_raw_inode(ri);
240 return ret;
224} 241}
225 242
226/***********************************************************************/ 243/***********************************************************************/
@@ -291,7 +308,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
291 struct jffs2_full_dnode *fn; 308 struct jffs2_full_dnode *fn;
292 struct jffs2_full_dirent *fd; 309 struct jffs2_full_dirent *fd;
293 int namelen; 310 int namelen;
294 uint32_t alloclen, phys_ofs; 311 uint32_t alloclen;
295 int ret, targetlen = strlen(target); 312 int ret, targetlen = strlen(target);
296 313
297 /* FIXME: If you care. We'd need to use frags for the target 314 /* FIXME: If you care. We'd need to use frags for the target
@@ -310,8 +327,8 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
310 * Just the node will do for now, though 327 * Just the node will do for now, though
311 */ 328 */
312 namelen = dentry->d_name.len; 329 namelen = dentry->d_name.len;
313 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen, 330 ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
314 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 331 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
315 332
316 if (ret) { 333 if (ret) {
317 jffs2_free_raw_inode(ri); 334 jffs2_free_raw_inode(ri);
@@ -339,7 +356,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
339 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen)); 356 ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
340 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 357 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
341 358
342 fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL); 359 fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
343 360
344 jffs2_free_raw_inode(ri); 361 jffs2_free_raw_inode(ri);
345 362
@@ -371,8 +388,20 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
371 up(&f->sem); 388 up(&f->sem);
372 389
373 jffs2_complete_reservation(c); 390 jffs2_complete_reservation(c);
374 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, 391
375 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 392 ret = jffs2_init_security(inode, dir_i);
393 if (ret) {
394 jffs2_clear_inode(inode);
395 return ret;
396 }
397 ret = jffs2_init_acl(inode, dir_i);
398 if (ret) {
399 jffs2_clear_inode(inode);
400 return ret;
401 }
402
403 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
404 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
376 if (ret) { 405 if (ret) {
377 /* Eep. */ 406 /* Eep. */
378 jffs2_clear_inode(inode); 407 jffs2_clear_inode(inode);
@@ -404,7 +433,7 @@ static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char
404 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 433 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
405 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 434 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
406 435
407 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL); 436 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
408 437
409 if (IS_ERR(fd)) { 438 if (IS_ERR(fd)) {
410 /* dirent failed to write. Delete the inode normally 439 /* dirent failed to write. Delete the inode normally
@@ -442,7 +471,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
442 struct jffs2_full_dnode *fn; 471 struct jffs2_full_dnode *fn;
443 struct jffs2_full_dirent *fd; 472 struct jffs2_full_dirent *fd;
444 int namelen; 473 int namelen;
445 uint32_t alloclen, phys_ofs; 474 uint32_t alloclen;
446 int ret; 475 int ret;
447 476
448 mode |= S_IFDIR; 477 mode |= S_IFDIR;
@@ -457,8 +486,8 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
457 * Just the node will do for now, though 486 * Just the node will do for now, though
458 */ 487 */
459 namelen = dentry->d_name.len; 488 namelen = dentry->d_name.len;
460 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL, 489 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
461 JFFS2_SUMMARY_INODE_SIZE); 490 JFFS2_SUMMARY_INODE_SIZE);
462 491
463 if (ret) { 492 if (ret) {
464 jffs2_free_raw_inode(ri); 493 jffs2_free_raw_inode(ri);
@@ -483,7 +512,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
483 ri->data_crc = cpu_to_je32(0); 512 ri->data_crc = cpu_to_je32(0);
484 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 513 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
485 514
486 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL); 515 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
487 516
488 jffs2_free_raw_inode(ri); 517 jffs2_free_raw_inode(ri);
489 518
@@ -501,8 +530,20 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
501 up(&f->sem); 530 up(&f->sem);
502 531
503 jffs2_complete_reservation(c); 532 jffs2_complete_reservation(c);
504 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, 533
505 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 534 ret = jffs2_init_security(inode, dir_i);
535 if (ret) {
536 jffs2_clear_inode(inode);
537 return ret;
538 }
539 ret = jffs2_init_acl(inode, dir_i);
540 if (ret) {
541 jffs2_clear_inode(inode);
542 return ret;
543 }
544
545 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
546 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
506 if (ret) { 547 if (ret) {
507 /* Eep. */ 548 /* Eep. */
508 jffs2_clear_inode(inode); 549 jffs2_clear_inode(inode);
@@ -534,7 +575,7 @@ static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
534 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 575 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
535 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 576 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
536 577
537 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL); 578 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
538 579
539 if (IS_ERR(fd)) { 580 if (IS_ERR(fd)) {
540 /* dirent failed to write. Delete the inode normally 581 /* dirent failed to write. Delete the inode normally
@@ -588,12 +629,12 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
588 struct jffs2_full_dnode *fn; 629 struct jffs2_full_dnode *fn;
589 struct jffs2_full_dirent *fd; 630 struct jffs2_full_dirent *fd;
590 int namelen; 631 int namelen;
591 jint16_t dev; 632 union jffs2_device_node dev;
592 int devlen = 0; 633 int devlen = 0;
593 uint32_t alloclen, phys_ofs; 634 uint32_t alloclen;
594 int ret; 635 int ret;
595 636
596 if (!old_valid_dev(rdev)) 637 if (!new_valid_dev(rdev))
597 return -EINVAL; 638 return -EINVAL;
598 639
599 ri = jffs2_alloc_raw_inode(); 640 ri = jffs2_alloc_raw_inode();
@@ -602,17 +643,15 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
602 643
603 c = JFFS2_SB_INFO(dir_i->i_sb); 644 c = JFFS2_SB_INFO(dir_i->i_sb);
604 645
605 if (S_ISBLK(mode) || S_ISCHR(mode)) { 646 if (S_ISBLK(mode) || S_ISCHR(mode))
606 dev = cpu_to_je16(old_encode_dev(rdev)); 647 devlen = jffs2_encode_dev(&dev, rdev);
607 devlen = sizeof(dev);
608 }
609 648
610 /* Try to reserve enough space for both node and dirent. 649 /* Try to reserve enough space for both node and dirent.
611 * Just the node will do for now, though 650 * Just the node will do for now, though
612 */ 651 */
613 namelen = dentry->d_name.len; 652 namelen = dentry->d_name.len;
614 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, 653 ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
615 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 654 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
616 655
617 if (ret) { 656 if (ret) {
618 jffs2_free_raw_inode(ri); 657 jffs2_free_raw_inode(ri);
@@ -639,7 +678,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
639 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen)); 678 ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
640 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 679 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
641 680
642 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL); 681 fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
643 682
644 jffs2_free_raw_inode(ri); 683 jffs2_free_raw_inode(ri);
645 684
@@ -657,8 +696,20 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
657 up(&f->sem); 696 up(&f->sem);
658 697
659 jffs2_complete_reservation(c); 698 jffs2_complete_reservation(c);
660 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, 699
661 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 700 ret = jffs2_init_security(inode, dir_i);
701 if (ret) {
702 jffs2_clear_inode(inode);
703 return ret;
704 }
705 ret = jffs2_init_acl(inode, dir_i);
706 if (ret) {
707 jffs2_clear_inode(inode);
708 return ret;
709 }
710
711 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
712 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
662 if (ret) { 713 if (ret) {
663 /* Eep. */ 714 /* Eep. */
664 jffs2_clear_inode(inode); 715 jffs2_clear_inode(inode);
@@ -693,7 +744,7 @@ static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, de
693 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 744 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
694 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen)); 745 rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
695 746
696 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL); 747 fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
697 748
698 if (IS_ERR(fd)) { 749 if (IS_ERR(fd)) {
699 /* dirent failed to write. Delete the inode normally 750 /* dirent failed to write. Delete the inode normally
diff --git a/fs/jffs2/erase.c b/fs/jffs2/erase.c
index dad68fdffe9e..ad0121088dde 100644
--- a/fs/jffs2/erase.c
+++ b/fs/jffs2/erase.c
@@ -30,7 +30,6 @@ static void jffs2_erase_callback(struct erase_info *);
30#endif 30#endif
31static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset); 31static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
32static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 32static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
33static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
34static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 33static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
35 34
36static void jffs2_erase_block(struct jffs2_sb_info *c, 35static void jffs2_erase_block(struct jffs2_sb_info *c,
@@ -54,8 +53,7 @@ static void jffs2_erase_block(struct jffs2_sb_info *c,
54 if (!instr) { 53 if (!instr) {
55 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n"); 54 printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
56 spin_lock(&c->erase_completion_lock); 55 spin_lock(&c->erase_completion_lock);
57 list_del(&jeb->list); 56 list_move(&jeb->list, &c->erase_pending_list);
58 list_add(&jeb->list, &c->erase_pending_list);
59 c->erasing_size -= c->sector_size; 57 c->erasing_size -= c->sector_size;
60 c->dirty_size += c->sector_size; 58 c->dirty_size += c->sector_size;
61 jeb->dirty_size = c->sector_size; 59 jeb->dirty_size = c->sector_size;
@@ -87,8 +85,7 @@ static void jffs2_erase_block(struct jffs2_sb_info *c,
87 /* Erase failed immediately. Refile it on the list */ 85 /* Erase failed immediately. Refile it on the list */
88 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret)); 86 D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
89 spin_lock(&c->erase_completion_lock); 87 spin_lock(&c->erase_completion_lock);
90 list_del(&jeb->list); 88 list_move(&jeb->list, &c->erase_pending_list);
91 list_add(&jeb->list, &c->erase_pending_list);
92 c->erasing_size -= c->sector_size; 89 c->erasing_size -= c->sector_size;
93 c->dirty_size += c->sector_size; 90 c->dirty_size += c->sector_size;
94 jeb->dirty_size = c->sector_size; 91 jeb->dirty_size = c->sector_size;
@@ -136,7 +133,7 @@ void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count)
136 c->used_size -= jeb->used_size; 133 c->used_size -= jeb->used_size;
137 c->dirty_size -= jeb->dirty_size; 134 c->dirty_size -= jeb->dirty_size;
138 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0; 135 jeb->wasted_size = jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
139 jffs2_free_all_node_refs(c, jeb); 136 jffs2_free_jeb_node_refs(c, jeb);
140 list_add(&jeb->list, &c->erasing_list); 137 list_add(&jeb->list, &c->erasing_list);
141 spin_unlock(&c->erase_completion_lock); 138 spin_unlock(&c->erase_completion_lock);
142 139
@@ -162,8 +159,7 @@ static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblo
162{ 159{
163 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset)); 160 D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08x\n", jeb->offset));
164 spin_lock(&c->erase_completion_lock); 161 spin_lock(&c->erase_completion_lock);
165 list_del(&jeb->list); 162 list_move_tail(&jeb->list, &c->erase_complete_list);
166 list_add_tail(&jeb->list, &c->erase_complete_list);
167 spin_unlock(&c->erase_completion_lock); 163 spin_unlock(&c->erase_completion_lock);
168 /* Ensure that kupdated calls us again to mark them clean */ 164 /* Ensure that kupdated calls us again to mark them clean */
169 jffs2_erase_pending_trigger(c); 165 jffs2_erase_pending_trigger(c);
@@ -179,8 +175,7 @@ static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock
179 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) { 175 if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
180 /* We'd like to give this block another try. */ 176 /* We'd like to give this block another try. */
181 spin_lock(&c->erase_completion_lock); 177 spin_lock(&c->erase_completion_lock);
182 list_del(&jeb->list); 178 list_move(&jeb->list, &c->erase_pending_list);
183 list_add(&jeb->list, &c->erase_pending_list);
184 c->erasing_size -= c->sector_size; 179 c->erasing_size -= c->sector_size;
185 c->dirty_size += c->sector_size; 180 c->dirty_size += c->sector_size;
186 jeb->dirty_size = c->sector_size; 181 jeb->dirty_size = c->sector_size;
@@ -192,8 +187,7 @@ static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock
192 spin_lock(&c->erase_completion_lock); 187 spin_lock(&c->erase_completion_lock);
193 c->erasing_size -= c->sector_size; 188 c->erasing_size -= c->sector_size;
194 c->bad_size += c->sector_size; 189 c->bad_size += c->sector_size;
195 list_del(&jeb->list); 190 list_move(&jeb->list, &c->bad_list);
196 list_add(&jeb->list, &c->bad_list);
197 c->nr_erasing_blocks--; 191 c->nr_erasing_blocks--;
198 spin_unlock(&c->erase_completion_lock); 192 spin_unlock(&c->erase_completion_lock);
199 wake_up(&c->erase_wait); 193 wake_up(&c->erase_wait);
@@ -254,7 +248,8 @@ static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
254 248
255 /* PARANOIA */ 249 /* PARANOIA */
256 if (!ic) { 250 if (!ic) {
257 printk(KERN_WARNING "inode_cache not found in remove_node_refs()!!\n"); 251 JFFS2_WARNING("inode_cache/xattr_datum/xattr_ref"
252 " not found in remove_node_refs()!!\n");
258 return; 253 return;
259 } 254 }
260 255
@@ -279,26 +274,42 @@ static inline void jffs2_remove_node_refs_from_ino_list(struct jffs2_sb_info *c,
279 printk("\n"); 274 printk("\n");
280 }); 275 });
281 276
282 if (ic->nodes == (void *)ic && ic->nlink == 0) 277 switch (ic->class) {
283 jffs2_del_ino_cache(c, ic); 278#ifdef CONFIG_JFFS2_FS_XATTR
279 case RAWNODE_CLASS_XATTR_DATUM:
280 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
281 break;
282 case RAWNODE_CLASS_XATTR_REF:
283 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
284 break;
285#endif
286 default:
287 if (ic->nodes == (void *)ic && ic->nlink == 0)
288 jffs2_del_ino_cache(c, ic);
289 }
284} 290}
285 291
286static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 292void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
287{ 293{
288 struct jffs2_raw_node_ref *ref; 294 struct jffs2_raw_node_ref *block, *ref;
289 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset)); 295 D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
290 while(jeb->first_node) {
291 ref = jeb->first_node;
292 jeb->first_node = ref->next_phys;
293 296
294 /* Remove from the inode-list */ 297 block = ref = jeb->first_node;
295 if (ref->next_in_ino) 298
299 while (ref) {
300 if (ref->flash_offset == REF_LINK_NODE) {
301 ref = ref->next_in_ino;
302 jffs2_free_refblock(block);
303 block = ref;
304 continue;
305 }
306 if (ref->flash_offset != REF_EMPTY_NODE && ref->next_in_ino)
296 jffs2_remove_node_refs_from_ino_list(c, ref, jeb); 307 jffs2_remove_node_refs_from_ino_list(c, ref, jeb);
297 /* else it was a non-inode node or already removed, so don't bother */ 308 /* else it was a non-inode node or already removed, so don't bother */
298 309
299 jffs2_free_raw_node_ref(ref); 310 ref++;
300 } 311 }
301 jeb->last_node = NULL; 312 jeb->first_node = jeb->last_node = NULL;
302} 313}
303 314
304static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset) 315static int jffs2_block_check_erase(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t *bad_offset)
@@ -351,7 +362,6 @@ fail:
351 362
352static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 363static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
353{ 364{
354 struct jffs2_raw_node_ref *marker_ref = NULL;
355 size_t retlen; 365 size_t retlen;
356 int ret; 366 int ret;
357 uint32_t bad_offset; 367 uint32_t bad_offset;
@@ -373,12 +383,8 @@ static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseb
373 goto filebad; 383 goto filebad;
374 } 384 }
375 385
376 jeb->first_node = jeb->last_node = NULL; 386 /* Everything else got zeroed before the erase */
377 jeb->free_size = c->sector_size; 387 jeb->free_size = c->sector_size;
378 jeb->used_size = 0;
379 jeb->dirty_size = 0;
380 jeb->wasted_size = 0;
381
382 } else { 388 } else {
383 389
384 struct kvec vecs[1]; 390 struct kvec vecs[1];
@@ -388,11 +394,7 @@ static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseb
388 .totlen = cpu_to_je32(c->cleanmarker_size) 394 .totlen = cpu_to_je32(c->cleanmarker_size)
389 }; 395 };
390 396
391 marker_ref = jffs2_alloc_raw_node_ref(); 397 jffs2_prealloc_raw_node_refs(c, jeb, 1);
392 if (!marker_ref) {
393 printk(KERN_WARNING "Failed to allocate raw node ref for clean marker. Refiling\n");
394 goto refile;
395 }
396 398
397 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4)); 399 marker.hdr_crc = cpu_to_je32(crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4));
398 400
@@ -408,21 +410,13 @@ static void jffs2_mark_erased_block(struct jffs2_sb_info *c, struct jffs2_eraseb
408 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n", 410 printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %zd, got %zd\n",
409 jeb->offset, sizeof(marker), retlen); 411 jeb->offset, sizeof(marker), retlen);
410 412
411 jffs2_free_raw_node_ref(marker_ref);
412 goto filebad; 413 goto filebad;
413 } 414 }
414 415
415 marker_ref->next_in_ino = NULL; 416 /* Everything else got zeroed before the erase */
416 marker_ref->next_phys = NULL; 417 jeb->free_size = c->sector_size;
417 marker_ref->flash_offset = jeb->offset | REF_NORMAL; 418 /* FIXME Special case for cleanmarker in empty block */
418 marker_ref->__totlen = c->cleanmarker_size; 419 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL, c->cleanmarker_size, NULL);
419
420 jeb->first_node = jeb->last_node = marker_ref;
421
422 jeb->free_size = c->sector_size - c->cleanmarker_size;
423 jeb->used_size = c->cleanmarker_size;
424 jeb->dirty_size = 0;
425 jeb->wasted_size = 0;
426 } 420 }
427 421
428 spin_lock(&c->erase_completion_lock); 422 spin_lock(&c->erase_completion_lock);
diff --git a/fs/jffs2/file.c b/fs/jffs2/file.c
index 9f4171213e58..3ed6e3e120b6 100644
--- a/fs/jffs2/file.c
+++ b/fs/jffs2/file.c
@@ -54,10 +54,15 @@ const struct file_operations jffs2_file_operations =
54 54
55struct inode_operations jffs2_file_inode_operations = 55struct inode_operations jffs2_file_inode_operations =
56{ 56{
57 .setattr = jffs2_setattr 57 .permission = jffs2_permission,
58 .setattr = jffs2_setattr,
59 .setxattr = jffs2_setxattr,
60 .getxattr = jffs2_getxattr,
61 .listxattr = jffs2_listxattr,
62 .removexattr = jffs2_removexattr
58}; 63};
59 64
60struct address_space_operations jffs2_file_address_operations = 65const struct address_space_operations jffs2_file_address_operations =
61{ 66{
62 .readpage = jffs2_readpage, 67 .readpage = jffs2_readpage,
63 .prepare_write =jffs2_prepare_write, 68 .prepare_write =jffs2_prepare_write,
@@ -129,13 +134,13 @@ static int jffs2_prepare_write (struct file *filp, struct page *pg,
129 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); 134 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
130 struct jffs2_raw_inode ri; 135 struct jffs2_raw_inode ri;
131 struct jffs2_full_dnode *fn; 136 struct jffs2_full_dnode *fn;
132 uint32_t phys_ofs, alloc_len; 137 uint32_t alloc_len;
133 138
134 D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n", 139 D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
135 (unsigned int)inode->i_size, pageofs)); 140 (unsigned int)inode->i_size, pageofs));
136 141
137 ret = jffs2_reserve_space(c, sizeof(ri), &phys_ofs, &alloc_len, 142 ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
138 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 143 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
139 if (ret) 144 if (ret)
140 return ret; 145 return ret;
141 146
@@ -161,7 +166,7 @@ static int jffs2_prepare_write (struct file *filp, struct page *pg,
161 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 166 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
162 ri.data_crc = cpu_to_je32(0); 167 ri.data_crc = cpu_to_je32(0);
163 168
164 fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_NORMAL); 169 fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
165 170
166 if (IS_ERR(fn)) { 171 if (IS_ERR(fn)) {
167 ret = PTR_ERR(fn); 172 ret = PTR_ERR(fn);
@@ -215,12 +220,20 @@ static int jffs2_commit_write (struct file *filp, struct page *pg,
215 D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n", 220 D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
216 inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags)); 221 inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, pg->flags));
217 222
218 if (!start && end == PAGE_CACHE_SIZE) { 223 if (end == PAGE_CACHE_SIZE) {
219 /* We need to avoid deadlock with page_cache_read() in 224 if (!start) {
220 jffs2_garbage_collect_pass(). So we have to mark the 225 /* We need to avoid deadlock with page_cache_read() in
221 page up to date, to prevent page_cache_read() from 226 jffs2_garbage_collect_pass(). So we have to mark the
222 trying to re-lock it. */ 227 page up to date, to prevent page_cache_read() from
223 SetPageUptodate(pg); 228 trying to re-lock it. */
229 SetPageUptodate(pg);
230 } else {
231 /* When writing out the end of a page, write out the
232 _whole_ page. This helps to reduce the number of
233 nodes in files which have many short writes, like
234 syslog files. */
235 start = aligned_start = 0;
236 }
224 } 237 }
225 238
226 ri = jffs2_alloc_raw_inode(); 239 ri = jffs2_alloc_raw_inode();
diff --git a/fs/jffs2/fs.c b/fs/jffs2/fs.c
index 09e5d10b8840..4780f82825d6 100644
--- a/fs/jffs2/fs.c
+++ b/fs/jffs2/fs.c
@@ -12,7 +12,6 @@
12 */ 12 */
13 13
14#include <linux/capability.h> 14#include <linux/capability.h>
15#include <linux/config.h>
16#include <linux/kernel.h> 15#include <linux/kernel.h>
17#include <linux/sched.h> 16#include <linux/sched.h>
18#include <linux/fs.h> 17#include <linux/fs.h>
@@ -33,11 +32,11 @@ static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
33 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); 32 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
34 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb); 33 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
35 struct jffs2_raw_inode *ri; 34 struct jffs2_raw_inode *ri;
36 unsigned short dev; 35 union jffs2_device_node dev;
37 unsigned char *mdata = NULL; 36 unsigned char *mdata = NULL;
38 int mdatalen = 0; 37 int mdatalen = 0;
39 unsigned int ivalid; 38 unsigned int ivalid;
40 uint32_t phys_ofs, alloclen; 39 uint32_t alloclen;
41 int ret; 40 int ret;
42 D1(printk(KERN_DEBUG "jffs2_setattr(): ino #%lu\n", inode->i_ino)); 41 D1(printk(KERN_DEBUG "jffs2_setattr(): ino #%lu\n", inode->i_ino));
43 ret = inode_change_ok(inode, iattr); 42 ret = inode_change_ok(inode, iattr);
@@ -51,20 +50,24 @@ static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
51 it out again with the appropriate data attached */ 50 it out again with the appropriate data attached */
52 if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { 51 if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
53 /* For these, we don't actually need to read the old node */ 52 /* For these, we don't actually need to read the old node */
54 dev = old_encode_dev(inode->i_rdev); 53 mdatalen = jffs2_encode_dev(&dev, inode->i_rdev);
55 mdata = (char *)&dev; 54 mdata = (char *)&dev;
56 mdatalen = sizeof(dev);
57 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of kdev_t\n", mdatalen)); 55 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of kdev_t\n", mdatalen));
58 } else if (S_ISLNK(inode->i_mode)) { 56 } else if (S_ISLNK(inode->i_mode)) {
57 down(&f->sem);
59 mdatalen = f->metadata->size; 58 mdatalen = f->metadata->size;
60 mdata = kmalloc(f->metadata->size, GFP_USER); 59 mdata = kmalloc(f->metadata->size, GFP_USER);
61 if (!mdata) 60 if (!mdata) {
61 up(&f->sem);
62 return -ENOMEM; 62 return -ENOMEM;
63 }
63 ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen); 64 ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
64 if (ret) { 65 if (ret) {
66 up(&f->sem);
65 kfree(mdata); 67 kfree(mdata);
66 return ret; 68 return ret;
67 } 69 }
70 up(&f->sem);
68 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of symlink target\n", mdatalen)); 71 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of symlink target\n", mdatalen));
69 } 72 }
70 73
@@ -75,8 +78,8 @@ static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
75 return -ENOMEM; 78 return -ENOMEM;
76 } 79 }
77 80
78 ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &phys_ofs, &alloclen, 81 ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &alloclen,
79 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 82 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
80 if (ret) { 83 if (ret) {
81 jffs2_free_raw_inode(ri); 84 jffs2_free_raw_inode(ri);
82 if (S_ISLNK(inode->i_mode & S_IFMT)) 85 if (S_ISLNK(inode->i_mode & S_IFMT))
@@ -127,7 +130,7 @@ static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
127 else 130 else
128 ri->data_crc = cpu_to_je32(0); 131 ri->data_crc = cpu_to_je32(0);
129 132
130 new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, phys_ofs, ALLOC_NORMAL); 133 new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, ALLOC_NORMAL);
131 if (S_ISLNK(inode->i_mode)) 134 if (S_ISLNK(inode->i_mode))
132 kfree(mdata); 135 kfree(mdata);
133 136
@@ -180,12 +183,17 @@ static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
180 183
181int jffs2_setattr(struct dentry *dentry, struct iattr *iattr) 184int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
182{ 185{
183 return jffs2_do_setattr(dentry->d_inode, iattr); 186 int rc;
187
188 rc = jffs2_do_setattr(dentry->d_inode, iattr);
189 if (!rc && (iattr->ia_valid & ATTR_MODE))
190 rc = jffs2_acl_chmod(dentry->d_inode);
191 return rc;
184} 192}
185 193
186int jffs2_statfs(struct super_block *sb, struct kstatfs *buf) 194int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
187{ 195{
188 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); 196 struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
189 unsigned long avail; 197 unsigned long avail;
190 198
191 buf->f_type = JFFS2_SUPER_MAGIC; 199 buf->f_type = JFFS2_SUPER_MAGIC;
@@ -218,7 +226,6 @@ void jffs2_clear_inode (struct inode *inode)
218 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode); 226 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
219 227
220 D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode)); 228 D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode));
221
222 jffs2_do_clear_inode(c, f); 229 jffs2_do_clear_inode(c, f);
223} 230}
224 231
@@ -227,6 +234,8 @@ void jffs2_read_inode (struct inode *inode)
227 struct jffs2_inode_info *f; 234 struct jffs2_inode_info *f;
228 struct jffs2_sb_info *c; 235 struct jffs2_sb_info *c;
229 struct jffs2_raw_inode latest_node; 236 struct jffs2_raw_inode latest_node;
237 union jffs2_device_node jdev;
238 dev_t rdev = 0;
230 int ret; 239 int ret;
231 240
232 D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino)); 241 D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
@@ -258,7 +267,6 @@ void jffs2_read_inode (struct inode *inode)
258 inode->i_blocks = (inode->i_size + 511) >> 9; 267 inode->i_blocks = (inode->i_size + 511) >> 9;
259 268
260 switch (inode->i_mode & S_IFMT) { 269 switch (inode->i_mode & S_IFMT) {
261 jint16_t rdev;
262 270
263 case S_IFLNK: 271 case S_IFLNK:
264 inode->i_op = &jffs2_symlink_inode_operations; 272 inode->i_op = &jffs2_symlink_inode_operations;
@@ -292,8 +300,16 @@ void jffs2_read_inode (struct inode *inode)
292 case S_IFBLK: 300 case S_IFBLK:
293 case S_IFCHR: 301 case S_IFCHR:
294 /* Read the device numbers from the media */ 302 /* Read the device numbers from the media */
303 if (f->metadata->size != sizeof(jdev.old) &&
304 f->metadata->size != sizeof(jdev.new)) {
305 printk(KERN_NOTICE "Device node has strange size %d\n", f->metadata->size);
306 up(&f->sem);
307 jffs2_do_clear_inode(c, f);
308 make_bad_inode(inode);
309 return;
310 }
295 D1(printk(KERN_DEBUG "Reading device numbers from flash\n")); 311 D1(printk(KERN_DEBUG "Reading device numbers from flash\n"));
296 if (jffs2_read_dnode(c, f, f->metadata, (char *)&rdev, 0, sizeof(rdev)) < 0) { 312 if (jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size) < 0) {
297 /* Eep */ 313 /* Eep */
298 printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino); 314 printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino);
299 up(&f->sem); 315 up(&f->sem);
@@ -301,12 +317,15 @@ void jffs2_read_inode (struct inode *inode)
301 make_bad_inode(inode); 317 make_bad_inode(inode);
302 return; 318 return;
303 } 319 }
320 if (f->metadata->size == sizeof(jdev.old))
321 rdev = old_decode_dev(je16_to_cpu(jdev.old));
322 else
323 rdev = new_decode_dev(je32_to_cpu(jdev.new));
304 324
305 case S_IFSOCK: 325 case S_IFSOCK:
306 case S_IFIFO: 326 case S_IFIFO:
307 inode->i_op = &jffs2_file_inode_operations; 327 inode->i_op = &jffs2_file_inode_operations;
308 init_special_inode(inode, inode->i_mode, 328 init_special_inode(inode, inode->i_mode, rdev);
309 old_decode_dev((je16_to_cpu(rdev))));
310 break; 329 break;
311 330
312 default: 331 default:
@@ -492,6 +511,8 @@ int jffs2_do_fill_super(struct super_block *sb, void *data, int silent)
492 } 511 }
493 memset(c->inocache_list, 0, INOCACHE_HASHSIZE * sizeof(struct jffs2_inode_cache *)); 512 memset(c->inocache_list, 0, INOCACHE_HASHSIZE * sizeof(struct jffs2_inode_cache *));
494 513
514 jffs2_init_xattr_subsystem(c);
515
495 if ((ret = jffs2_do_mount_fs(c))) 516 if ((ret = jffs2_do_mount_fs(c)))
496 goto out_inohash; 517 goto out_inohash;
497 518
@@ -526,6 +547,7 @@ int jffs2_do_fill_super(struct super_block *sb, void *data, int silent)
526 else 547 else
527 kfree(c->blocks); 548 kfree(c->blocks);
528 out_inohash: 549 out_inohash:
550 jffs2_clear_xattr_subsystem(c);
529 kfree(c->inocache_list); 551 kfree(c->inocache_list);
530 out_wbuf: 552 out_wbuf:
531 jffs2_flash_cleanup(c); 553 jffs2_flash_cleanup(c);
@@ -639,13 +661,6 @@ static int jffs2_flash_setup(struct jffs2_sb_info *c) {
639 return ret; 661 return ret;
640 } 662 }
641 663
642 /* add setups for other bizarre flashes here... */
643 if (jffs2_nor_ecc(c)) {
644 ret = jffs2_nor_ecc_flash_setup(c);
645 if (ret)
646 return ret;
647 }
648
649 /* and Dataflash */ 664 /* and Dataflash */
650 if (jffs2_dataflash(c)) { 665 if (jffs2_dataflash(c)) {
651 ret = jffs2_dataflash_setup(c); 666 ret = jffs2_dataflash_setup(c);
@@ -669,11 +684,6 @@ void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
669 jffs2_nand_flash_cleanup(c); 684 jffs2_nand_flash_cleanup(c);
670 } 685 }
671 686
672 /* add cleanups for other bizarre flashes here... */
673 if (jffs2_nor_ecc(c)) {
674 jffs2_nor_ecc_flash_cleanup(c);
675 }
676
677 /* and DataFlash */ 687 /* and DataFlash */
678 if (jffs2_dataflash(c)) { 688 if (jffs2_dataflash(c)) {
679 jffs2_dataflash_cleanup(c); 689 jffs2_dataflash_cleanup(c);
diff --git a/fs/jffs2/gc.c b/fs/jffs2/gc.c
index f9ffece453a3..daff3341ff92 100644
--- a/fs/jffs2/gc.c
+++ b/fs/jffs2/gc.c
@@ -125,6 +125,7 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
125 struct jffs2_eraseblock *jeb; 125 struct jffs2_eraseblock *jeb;
126 struct jffs2_raw_node_ref *raw; 126 struct jffs2_raw_node_ref *raw;
127 int ret = 0, inum, nlink; 127 int ret = 0, inum, nlink;
128 int xattr = 0;
128 129
129 if (down_interruptible(&c->alloc_sem)) 130 if (down_interruptible(&c->alloc_sem))
130 return -EINTR; 131 return -EINTR;
@@ -138,7 +139,7 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
138 the node CRCs etc. Do it now. */ 139 the node CRCs etc. Do it now. */
139 140
140 /* checked_ino is protected by the alloc_sem */ 141 /* checked_ino is protected by the alloc_sem */
141 if (c->checked_ino > c->highest_ino) { 142 if (c->checked_ino > c->highest_ino && xattr) {
142 printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n", 143 printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n",
143 c->unchecked_size); 144 c->unchecked_size);
144 jffs2_dbg_dump_block_lists_nolock(c); 145 jffs2_dbg_dump_block_lists_nolock(c);
@@ -148,6 +149,9 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
148 149
149 spin_unlock(&c->erase_completion_lock); 150 spin_unlock(&c->erase_completion_lock);
150 151
152 if (!xattr)
153 xattr = jffs2_verify_xattr(c);
154
151 spin_lock(&c->inocache_lock); 155 spin_lock(&c->inocache_lock);
152 156
153 ic = jffs2_get_ino_cache(c, c->checked_ino++); 157 ic = jffs2_get_ino_cache(c, c->checked_ino++);
@@ -161,6 +165,7 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
161 D1(printk(KERN_DEBUG "Skipping check of ino #%d with nlink zero\n", 165 D1(printk(KERN_DEBUG "Skipping check of ino #%d with nlink zero\n",
162 ic->ino)); 166 ic->ino));
163 spin_unlock(&c->inocache_lock); 167 spin_unlock(&c->inocache_lock);
168 jffs2_xattr_delete_inode(c, ic);
164 continue; 169 continue;
165 } 170 }
166 switch(ic->state) { 171 switch(ic->state) {
@@ -181,6 +186,10 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
181 and trigger the BUG() above while we haven't yet 186 and trigger the BUG() above while we haven't yet
182 finished checking all its nodes */ 187 finished checking all its nodes */
183 D1(printk(KERN_DEBUG "Waiting for ino #%u to finish reading\n", ic->ino)); 188 D1(printk(KERN_DEBUG "Waiting for ino #%u to finish reading\n", ic->ino));
189 /* We need to come back again for the _same_ inode. We've
190 made no progress in this case, but that should be OK */
191 c->checked_ino--;
192
184 up(&c->alloc_sem); 193 up(&c->alloc_sem);
185 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock); 194 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
186 return 0; 195 return 0;
@@ -231,7 +240,7 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
231 240
232 while(ref_obsolete(raw)) { 241 while(ref_obsolete(raw)) {
233 D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping\n", ref_offset(raw))); 242 D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping\n", ref_offset(raw)));
234 raw = raw->next_phys; 243 raw = ref_next(raw);
235 if (unlikely(!raw)) { 244 if (unlikely(!raw)) {
236 printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n"); 245 printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n");
237 printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", 246 printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
@@ -248,16 +257,36 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
248 257
249 if (!raw->next_in_ino) { 258 if (!raw->next_in_ino) {
250 /* Inode-less node. Clean marker, snapshot or something like that */ 259 /* Inode-less node. Clean marker, snapshot or something like that */
251 /* FIXME: If it's something that needs to be copied, including something
252 we don't grok that has JFFS2_NODETYPE_RWCOMPAT_COPY, we should do so */
253 spin_unlock(&c->erase_completion_lock); 260 spin_unlock(&c->erase_completion_lock);
254 jffs2_mark_node_obsolete(c, raw); 261 if (ref_flags(raw) == REF_PRISTINE) {
262 /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
263 jffs2_garbage_collect_pristine(c, NULL, raw);
264 } else {
265 /* Just mark it obsolete */
266 jffs2_mark_node_obsolete(c, raw);
267 }
255 up(&c->alloc_sem); 268 up(&c->alloc_sem);
256 goto eraseit_lock; 269 goto eraseit_lock;
257 } 270 }
258 271
259 ic = jffs2_raw_ref_to_ic(raw); 272 ic = jffs2_raw_ref_to_ic(raw);
260 273
274#ifdef CONFIG_JFFS2_FS_XATTR
275 /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
276 * We can decide whether this node is inode or xattr by ic->class. */
277 if (ic->class == RAWNODE_CLASS_XATTR_DATUM
278 || ic->class == RAWNODE_CLASS_XATTR_REF) {
279 spin_unlock(&c->erase_completion_lock);
280
281 if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
282 ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
283 } else {
284 ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
285 }
286 goto release_sem;
287 }
288#endif
289
261 /* We need to hold the inocache. Either the erase_completion_lock or 290 /* We need to hold the inocache. Either the erase_completion_lock or
262 the inocache_lock are sufficient; we trade down since the inocache_lock 291 the inocache_lock are sufficient; we trade down since the inocache_lock
263 causes less contention. */ 292 causes less contention. */
@@ -499,7 +528,6 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
499 struct jffs2_raw_node_ref *raw) 528 struct jffs2_raw_node_ref *raw)
500{ 529{
501 union jffs2_node_union *node; 530 union jffs2_node_union *node;
502 struct jffs2_raw_node_ref *nraw;
503 size_t retlen; 531 size_t retlen;
504 int ret; 532 int ret;
505 uint32_t phys_ofs, alloclen; 533 uint32_t phys_ofs, alloclen;
@@ -508,15 +536,16 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
508 536
509 D1(printk(KERN_DEBUG "Going to GC REF_PRISTINE node at 0x%08x\n", ref_offset(raw))); 537 D1(printk(KERN_DEBUG "Going to GC REF_PRISTINE node at 0x%08x\n", ref_offset(raw)));
510 538
511 rawlen = ref_totlen(c, c->gcblock, raw); 539 alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
512 540
513 /* Ask for a small amount of space (or the totlen if smaller) because we 541 /* Ask for a small amount of space (or the totlen if smaller) because we
514 don't want to force wastage of the end of a block if splitting would 542 don't want to force wastage of the end of a block if splitting would
515 work. */ 543 work. */
516 ret = jffs2_reserve_space_gc(c, min_t(uint32_t, sizeof(struct jffs2_raw_inode) + 544 if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
517 JFFS2_MIN_DATA_LEN, rawlen), &phys_ofs, &alloclen, rawlen); 545 alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
518 /* this is not the exact summary size of it, 546
519 it is only an upper estimation */ 547 ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
548 /* 'rawlen' is not the exact summary size; it is only an upper estimation */
520 549
521 if (ret) 550 if (ret)
522 return ret; 551 return ret;
@@ -580,22 +609,17 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
580 } 609 }
581 break; 610 break;
582 default: 611 default:
583 printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n", 612 /* If it's inode-less, we don't _know_ what it is. Just copy it intact */
584 ref_offset(raw), je16_to_cpu(node->u.nodetype)); 613 if (ic) {
585 goto bail; 614 printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
586 } 615 ref_offset(raw), je16_to_cpu(node->u.nodetype));
587 616 goto bail;
588 nraw = jffs2_alloc_raw_node_ref(); 617 }
589 if (!nraw) {
590 ret = -ENOMEM;
591 goto out_node;
592 } 618 }
593 619
594 /* OK, all the CRCs are good; this node can just be copied as-is. */ 620 /* OK, all the CRCs are good; this node can just be copied as-is. */
595 retry: 621 retry:
596 nraw->flash_offset = phys_ofs; 622 phys_ofs = write_ofs(c);
597 nraw->__totlen = rawlen;
598 nraw->next_phys = NULL;
599 623
600 ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node); 624 ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
601 625
@@ -603,17 +627,11 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
603 printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n", 627 printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
604 rawlen, phys_ofs, ret, retlen); 628 rawlen, phys_ofs, ret, retlen);
605 if (retlen) { 629 if (retlen) {
606 /* Doesn't belong to any inode */ 630 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
607 nraw->next_in_ino = NULL;
608
609 nraw->flash_offset |= REF_OBSOLETE;
610 jffs2_add_physical_node_ref(c, nraw);
611 jffs2_mark_node_obsolete(c, nraw);
612 } else { 631 } else {
613 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", nraw->flash_offset); 632 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", phys_ofs);
614 jffs2_free_raw_node_ref(nraw);
615 } 633 }
616 if (!retried && (nraw = jffs2_alloc_raw_node_ref())) { 634 if (!retried) {
617 /* Try to reallocate space and retry */ 635 /* Try to reallocate space and retry */
618 uint32_t dummy; 636 uint32_t dummy;
619 struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size]; 637 struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
@@ -625,7 +643,7 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
625 jffs2_dbg_acct_sanity_check(c,jeb); 643 jffs2_dbg_acct_sanity_check(c,jeb);
626 jffs2_dbg_acct_paranoia_check(c, jeb); 644 jffs2_dbg_acct_paranoia_check(c, jeb);
627 645
628 ret = jffs2_reserve_space_gc(c, rawlen, &phys_ofs, &dummy, rawlen); 646 ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
629 /* this is not the exact summary size of it, 647 /* this is not the exact summary size of it,
630 it is only an upper estimation */ 648 it is only an upper estimation */
631 649
@@ -638,25 +656,13 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
638 goto retry; 656 goto retry;
639 } 657 }
640 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret)); 658 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
641 jffs2_free_raw_node_ref(nraw);
642 } 659 }
643 660
644 jffs2_free_raw_node_ref(nraw);
645 if (!ret) 661 if (!ret)
646 ret = -EIO; 662 ret = -EIO;
647 goto out_node; 663 goto out_node;
648 } 664 }
649 nraw->flash_offset |= REF_PRISTINE; 665 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
650 jffs2_add_physical_node_ref(c, nraw);
651
652 /* Link into per-inode list. This is safe because of the ic
653 state being INO_STATE_GC. Note that if we're doing this
654 for an inode which is in-core, the 'nraw' pointer is then
655 going to be fetched from ic->nodes by our caller. */
656 spin_lock(&c->erase_completion_lock);
657 nraw->next_in_ino = ic->nodes;
658 ic->nodes = nraw;
659 spin_unlock(&c->erase_completion_lock);
660 666
661 jffs2_mark_node_obsolete(c, raw); 667 jffs2_mark_node_obsolete(c, raw);
662 D1(printk(KERN_DEBUG "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", ref_offset(raw))); 668 D1(printk(KERN_DEBUG "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", ref_offset(raw)));
@@ -675,19 +681,16 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
675 struct jffs2_full_dnode *new_fn; 681 struct jffs2_full_dnode *new_fn;
676 struct jffs2_raw_inode ri; 682 struct jffs2_raw_inode ri;
677 struct jffs2_node_frag *last_frag; 683 struct jffs2_node_frag *last_frag;
678 jint16_t dev; 684 union jffs2_device_node dev;
679 char *mdata = NULL, mdatalen = 0; 685 char *mdata = NULL, mdatalen = 0;
680 uint32_t alloclen, phys_ofs, ilen; 686 uint32_t alloclen, ilen;
681 int ret; 687 int ret;
682 688
683 if (S_ISBLK(JFFS2_F_I_MODE(f)) || 689 if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
684 S_ISCHR(JFFS2_F_I_MODE(f)) ) { 690 S_ISCHR(JFFS2_F_I_MODE(f)) ) {
685 /* For these, we don't actually need to read the old node */ 691 /* For these, we don't actually need to read the old node */
686 /* FIXME: for minor or major > 255. */ 692 mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
687 dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) |
688 JFFS2_F_I_RDEV_MIN(f)));
689 mdata = (char *)&dev; 693 mdata = (char *)&dev;
690 mdatalen = sizeof(dev);
691 D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen)); 694 D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen));
692 } else if (S_ISLNK(JFFS2_F_I_MODE(f))) { 695 } else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
693 mdatalen = fn->size; 696 mdatalen = fn->size;
@@ -706,7 +709,7 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
706 709
707 } 710 }
708 711
709 ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &phys_ofs, &alloclen, 712 ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
710 JFFS2_SUMMARY_INODE_SIZE); 713 JFFS2_SUMMARY_INODE_SIZE);
711 if (ret) { 714 if (ret) {
712 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n", 715 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
@@ -744,7 +747,7 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
744 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 747 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
745 ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen)); 748 ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
746 749
747 new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, phys_ofs, ALLOC_GC); 750 new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
748 751
749 if (IS_ERR(new_fn)) { 752 if (IS_ERR(new_fn)) {
750 printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn)); 753 printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
@@ -765,7 +768,7 @@ static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_er
765{ 768{
766 struct jffs2_full_dirent *new_fd; 769 struct jffs2_full_dirent *new_fd;
767 struct jffs2_raw_dirent rd; 770 struct jffs2_raw_dirent rd;
768 uint32_t alloclen, phys_ofs; 771 uint32_t alloclen;
769 int ret; 772 int ret;
770 773
771 rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 774 rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
@@ -787,14 +790,14 @@ static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_er
787 rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8)); 790 rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
788 rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize)); 791 rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
789 792
790 ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &phys_ofs, &alloclen, 793 ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
791 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize)); 794 JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
792 if (ret) { 795 if (ret) {
793 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n", 796 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
794 sizeof(rd)+rd.nsize, ret); 797 sizeof(rd)+rd.nsize, ret);
795 return ret; 798 return ret;
796 } 799 }
797 new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, phys_ofs, ALLOC_GC); 800 new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
798 801
799 if (IS_ERR(new_fd)) { 802 if (IS_ERR(new_fd)) {
800 printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd)); 803 printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd));
@@ -922,7 +925,7 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
922 struct jffs2_raw_inode ri; 925 struct jffs2_raw_inode ri;
923 struct jffs2_node_frag *frag; 926 struct jffs2_node_frag *frag;
924 struct jffs2_full_dnode *new_fn; 927 struct jffs2_full_dnode *new_fn;
925 uint32_t alloclen, phys_ofs, ilen; 928 uint32_t alloclen, ilen;
926 int ret; 929 int ret;
927 930
928 D1(printk(KERN_DEBUG "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n", 931 D1(printk(KERN_DEBUG "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
@@ -1001,14 +1004,14 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
1001 ri.data_crc = cpu_to_je32(0); 1004 ri.data_crc = cpu_to_je32(0);
1002 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 1005 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1003 1006
1004 ret = jffs2_reserve_space_gc(c, sizeof(ri), &phys_ofs, &alloclen, 1007 ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
1005 JFFS2_SUMMARY_INODE_SIZE); 1008 JFFS2_SUMMARY_INODE_SIZE);
1006 if (ret) { 1009 if (ret) {
1007 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n", 1010 printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
1008 sizeof(ri), ret); 1011 sizeof(ri), ret);
1009 return ret; 1012 return ret;
1010 } 1013 }
1011 new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_GC); 1014 new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
1012 1015
1013 if (IS_ERR(new_fn)) { 1016 if (IS_ERR(new_fn)) {
1014 printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn)); 1017 printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn));
@@ -1070,7 +1073,7 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
1070{ 1073{
1071 struct jffs2_full_dnode *new_fn; 1074 struct jffs2_full_dnode *new_fn;
1072 struct jffs2_raw_inode ri; 1075 struct jffs2_raw_inode ri;
1073 uint32_t alloclen, phys_ofs, offset, orig_end, orig_start; 1076 uint32_t alloclen, offset, orig_end, orig_start;
1074 int ret = 0; 1077 int ret = 0;
1075 unsigned char *comprbuf = NULL, *writebuf; 1078 unsigned char *comprbuf = NULL, *writebuf;
1076 unsigned long pg; 1079 unsigned long pg;
@@ -1227,7 +1230,7 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
1227 uint32_t cdatalen; 1230 uint32_t cdatalen;
1228 uint16_t comprtype = JFFS2_COMPR_NONE; 1231 uint16_t comprtype = JFFS2_COMPR_NONE;
1229 1232
1230 ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, 1233 ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
1231 &alloclen, JFFS2_SUMMARY_INODE_SIZE); 1234 &alloclen, JFFS2_SUMMARY_INODE_SIZE);
1232 1235
1233 if (ret) { 1236 if (ret) {
@@ -1264,7 +1267,7 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
1264 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8)); 1267 ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
1265 ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen)); 1268 ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
1266 1269
1267 new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, phys_ofs, ALLOC_GC); 1270 new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
1268 1271
1269 jffs2_free_comprbuf(comprbuf, writebuf); 1272 jffs2_free_comprbuf(comprbuf, writebuf);
1270 1273
diff --git a/fs/jffs2/histo.h b/fs/jffs2/histo.h
deleted file mode 100644
index 22a93a08210c..000000000000
--- a/fs/jffs2/histo.h
+++ /dev/null
@@ -1,3 +0,0 @@
1/* This file provides the bit-probabilities for the input file */
2#define BIT_DIVIDER 629
3static int bits[9] = { 179,167,183,165,159,198,178,119,}; /* ia32 .so files */
diff --git a/fs/jffs2/jffs2_fs_i.h b/fs/jffs2/jffs2_fs_i.h
new file mode 100644
index 000000000000..2e0cc8e00b85
--- /dev/null
+++ b/fs/jffs2/jffs2_fs_i.h
@@ -0,0 +1,55 @@
1/* $Id: jffs2_fs_i.h,v 1.19 2005/11/07 11:14:52 gleixner Exp $ */
2
3#ifndef _JFFS2_FS_I
4#define _JFFS2_FS_I
5
6#include <linux/version.h>
7#include <linux/rbtree.h>
8#include <linux/posix_acl.h>
9#include <asm/semaphore.h>
10
11struct jffs2_inode_info {
12 /* We need an internal mutex similar to inode->i_mutex.
13 Unfortunately, we can't used the existing one, because
14 either the GC would deadlock, or we'd have to release it
15 before letting GC proceed. Or we'd have to put ugliness
16 into the GC code so it didn't attempt to obtain the i_mutex
17 for the inode(s) which are already locked */
18 struct semaphore sem;
19
20 /* The highest (datanode) version number used for this ino */
21 uint32_t highest_version;
22
23 /* List of data fragments which make up the file */
24 struct rb_root fragtree;
25
26 /* There may be one datanode which isn't referenced by any of the
27 above fragments, if it contains a metadata update but no actual
28 data - or if this is a directory inode */
29 /* This also holds the _only_ dnode for symlinks/device nodes,
30 etc. */
31 struct jffs2_full_dnode *metadata;
32
33 /* Directory entries */
34 struct jffs2_full_dirent *dents;
35
36 /* The target path if this is the inode of a symlink */
37 unsigned char *target;
38
39 /* Some stuff we just have to keep in-core at all times, for each inode. */
40 struct jffs2_inode_cache *inocache;
41
42 uint16_t flags;
43 uint8_t usercompr;
44#if !defined (__ECOS)
45#if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,2)
46 struct inode vfs_inode;
47#endif
48#endif
49#ifdef CONFIG_JFFS2_FS_POSIX_ACL
50 struct posix_acl *i_acl_access;
51 struct posix_acl *i_acl_default;
52#endif
53};
54
55#endif /* _JFFS2_FS_I */
diff --git a/fs/jffs2/jffs2_fs_sb.h b/fs/jffs2/jffs2_fs_sb.h
new file mode 100644
index 000000000000..b98594992eed
--- /dev/null
+++ b/fs/jffs2/jffs2_fs_sb.h
@@ -0,0 +1,136 @@
1/* $Id: jffs2_fs_sb.h,v 1.54 2005/09/21 13:37:34 dedekind Exp $ */
2
3#ifndef _JFFS2_FS_SB
4#define _JFFS2_FS_SB
5
6#include <linux/types.h>
7#include <linux/spinlock.h>
8#include <linux/workqueue.h>
9#include <linux/completion.h>
10#include <asm/semaphore.h>
11#include <linux/timer.h>
12#include <linux/wait.h>
13#include <linux/list.h>
14#include <linux/rwsem.h>
15
16#define JFFS2_SB_FLAG_RO 1
17#define JFFS2_SB_FLAG_SCANNING 2 /* Flash scanning is in progress */
18#define JFFS2_SB_FLAG_BUILDING 4 /* File system building is in progress */
19
20struct jffs2_inodirty;
21
22/* A struct for the overall file system control. Pointers to
23 jffs2_sb_info structs are named `c' in the source code.
24 Nee jffs_control
25*/
26struct jffs2_sb_info {
27 struct mtd_info *mtd;
28
29 uint32_t highest_ino;
30 uint32_t checked_ino;
31
32 unsigned int flags;
33
34 struct task_struct *gc_task; /* GC task struct */
35 struct completion gc_thread_start; /* GC thread start completion */
36 struct completion gc_thread_exit; /* GC thread exit completion port */
37
38 struct semaphore alloc_sem; /* Used to protect all the following
39 fields, and also to protect against
40 out-of-order writing of nodes. And GC. */
41 uint32_t cleanmarker_size; /* Size of an _inline_ CLEANMARKER
42 (i.e. zero for OOB CLEANMARKER */
43
44 uint32_t flash_size;
45 uint32_t used_size;
46 uint32_t dirty_size;
47 uint32_t wasted_size;
48 uint32_t free_size;
49 uint32_t erasing_size;
50 uint32_t bad_size;
51 uint32_t sector_size;
52 uint32_t unchecked_size;
53
54 uint32_t nr_free_blocks;
55 uint32_t nr_erasing_blocks;
56
57 /* Number of free blocks there must be before we... */
58 uint8_t resv_blocks_write; /* ... allow a normal filesystem write */
59 uint8_t resv_blocks_deletion; /* ... allow a normal filesystem deletion */
60 uint8_t resv_blocks_gctrigger; /* ... wake up the GC thread */
61 uint8_t resv_blocks_gcbad; /* ... pick a block from the bad_list to GC */
62 uint8_t resv_blocks_gcmerge; /* ... merge pages when garbage collecting */
63
64 uint32_t nospc_dirty_size;
65
66 uint32_t nr_blocks;
67 struct jffs2_eraseblock *blocks; /* The whole array of blocks. Used for getting blocks
68 * from the offset (blocks[ofs / sector_size]) */
69 struct jffs2_eraseblock *nextblock; /* The block we're currently filling */
70
71 struct jffs2_eraseblock *gcblock; /* The block we're currently garbage-collecting */
72
73 struct list_head clean_list; /* Blocks 100% full of clean data */
74 struct list_head very_dirty_list; /* Blocks with lots of dirty space */
75 struct list_head dirty_list; /* Blocks with some dirty space */
76 struct list_head erasable_list; /* Blocks which are completely dirty, and need erasing */
77 struct list_head erasable_pending_wbuf_list; /* Blocks which need erasing but only after the current wbuf is flushed */
78 struct list_head erasing_list; /* Blocks which are currently erasing */
79 struct list_head erase_pending_list; /* Blocks which need erasing now */
80 struct list_head erase_complete_list; /* Blocks which are erased and need the clean marker written to them */
81 struct list_head free_list; /* Blocks which are free and ready to be used */
82 struct list_head bad_list; /* Bad blocks. */
83 struct list_head bad_used_list; /* Bad blocks with valid data in. */
84
85 spinlock_t erase_completion_lock; /* Protect free_list and erasing_list
86 against erase completion handler */
87 wait_queue_head_t erase_wait; /* For waiting for erases to complete */
88
89 wait_queue_head_t inocache_wq;
90 struct jffs2_inode_cache **inocache_list;
91 spinlock_t inocache_lock;
92
93 /* Sem to allow jffs2_garbage_collect_deletion_dirent to
94 drop the erase_completion_lock while it's holding a pointer
95 to an obsoleted node. I don't like this. Alternatives welcomed. */
96 struct semaphore erase_free_sem;
97
98 uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */
99
100#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
101 /* Write-behind buffer for NAND flash */
102 unsigned char *wbuf;
103 unsigned char *oobbuf;
104 uint32_t wbuf_ofs;
105 uint32_t wbuf_len;
106 struct jffs2_inodirty *wbuf_inodes;
107
108 struct rw_semaphore wbuf_sem; /* Protects the write buffer */
109
110 /* Information about out-of-band area usage... */
111 struct nand_ecclayout *ecclayout;
112 uint32_t badblock_pos;
113 uint32_t fsdata_pos;
114 uint32_t fsdata_len;
115#endif
116
117 struct jffs2_summary *summary; /* Summary information */
118
119#ifdef CONFIG_JFFS2_FS_XATTR
120#define XATTRINDEX_HASHSIZE (57)
121 uint32_t highest_xid;
122 uint32_t highest_xseqno;
123 struct list_head xattrindex[XATTRINDEX_HASHSIZE];
124 struct list_head xattr_unchecked;
125 struct list_head xattr_dead_list;
126 struct jffs2_xattr_ref *xref_dead_list;
127 struct jffs2_xattr_ref *xref_temp;
128 struct rw_semaphore xattr_sem;
129 uint32_t xdatum_mem_usage;
130 uint32_t xdatum_mem_threshold;
131#endif
132 /* OS-private pointer for getting back to master superblock info */
133 void *os_priv;
134};
135
136#endif /* _JFFS2_FB_SB */
diff --git a/fs/jffs2/malloc.c b/fs/jffs2/malloc.c
index 036cbd11c004..8310c95478e9 100644
--- a/fs/jffs2/malloc.c
+++ b/fs/jffs2/malloc.c
@@ -26,6 +26,10 @@ static kmem_cache_t *tmp_dnode_info_slab;
26static kmem_cache_t *raw_node_ref_slab; 26static kmem_cache_t *raw_node_ref_slab;
27static kmem_cache_t *node_frag_slab; 27static kmem_cache_t *node_frag_slab;
28static kmem_cache_t *inode_cache_slab; 28static kmem_cache_t *inode_cache_slab;
29#ifdef CONFIG_JFFS2_FS_XATTR
30static kmem_cache_t *xattr_datum_cache;
31static kmem_cache_t *xattr_ref_cache;
32#endif
29 33
30int __init jffs2_create_slab_caches(void) 34int __init jffs2_create_slab_caches(void)
31{ 35{
@@ -53,8 +57,8 @@ int __init jffs2_create_slab_caches(void)
53 if (!tmp_dnode_info_slab) 57 if (!tmp_dnode_info_slab)
54 goto err; 58 goto err;
55 59
56 raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref", 60 raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
57 sizeof(struct jffs2_raw_node_ref), 61 sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
58 0, 0, NULL, NULL); 62 0, 0, NULL, NULL);
59 if (!raw_node_ref_slab) 63 if (!raw_node_ref_slab)
60 goto err; 64 goto err;
@@ -68,8 +72,24 @@ int __init jffs2_create_slab_caches(void)
68 inode_cache_slab = kmem_cache_create("jffs2_inode_cache", 72 inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
69 sizeof(struct jffs2_inode_cache), 73 sizeof(struct jffs2_inode_cache),
70 0, 0, NULL, NULL); 74 0, 0, NULL, NULL);
71 if (inode_cache_slab) 75 if (!inode_cache_slab)
72 return 0; 76 goto err;
77
78#ifdef CONFIG_JFFS2_FS_XATTR
79 xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum",
80 sizeof(struct jffs2_xattr_datum),
81 0, 0, NULL, NULL);
82 if (!xattr_datum_cache)
83 goto err;
84
85 xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
86 sizeof(struct jffs2_xattr_ref),
87 0, 0, NULL, NULL);
88 if (!xattr_ref_cache)
89 goto err;
90#endif
91
92 return 0;
73 err: 93 err:
74 jffs2_destroy_slab_caches(); 94 jffs2_destroy_slab_caches();
75 return -ENOMEM; 95 return -ENOMEM;
@@ -91,6 +111,12 @@ void jffs2_destroy_slab_caches(void)
91 kmem_cache_destroy(node_frag_slab); 111 kmem_cache_destroy(node_frag_slab);
92 if(inode_cache_slab) 112 if(inode_cache_slab)
93 kmem_cache_destroy(inode_cache_slab); 113 kmem_cache_destroy(inode_cache_slab);
114#ifdef CONFIG_JFFS2_FS_XATTR
115 if (xattr_datum_cache)
116 kmem_cache_destroy(xattr_datum_cache);
117 if (xattr_ref_cache)
118 kmem_cache_destroy(xattr_ref_cache);
119#endif
94} 120}
95 121
96struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize) 122struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
@@ -164,15 +190,65 @@ void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
164 kmem_cache_free(tmp_dnode_info_slab, x); 190 kmem_cache_free(tmp_dnode_info_slab, x);
165} 191}
166 192
167struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void) 193struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
168{ 194{
169 struct jffs2_raw_node_ref *ret; 195 struct jffs2_raw_node_ref *ret;
196
170 ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL); 197 ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
171 dbg_memalloc("%p\n", ret); 198 if (ret) {
199 int i = 0;
200 for (i=0; i < REFS_PER_BLOCK; i++) {
201 ret[i].flash_offset = REF_EMPTY_NODE;
202 ret[i].next_in_ino = NULL;
203 }
204 ret[i].flash_offset = REF_LINK_NODE;
205 ret[i].next_in_ino = NULL;
206 }
172 return ret; 207 return ret;
173} 208}
174 209
175void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x) 210int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
211 struct jffs2_eraseblock *jeb, int nr)
212{
213 struct jffs2_raw_node_ref **p, *ref;
214 int i = nr;
215
216 dbg_memalloc("%d\n", nr);
217
218 p = &jeb->last_node;
219 ref = *p;
220
221 dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
222
223 /* If jeb->last_node is really a valid node then skip over it */
224 if (ref && ref->flash_offset != REF_EMPTY_NODE)
225 ref++;
226
227 while (i) {
228 if (!ref) {
229 dbg_memalloc("Allocating new refblock linked from %p\n", p);
230 ref = *p = jffs2_alloc_refblock();
231 if (!ref)
232 return -ENOMEM;
233 }
234 if (ref->flash_offset == REF_LINK_NODE) {
235 p = &ref->next_in_ino;
236 ref = *p;
237 continue;
238 }
239 i--;
240 ref++;
241 }
242 jeb->allocated_refs = nr;
243
244 dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
245 nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
246 jeb->last_node->next_in_ino);
247
248 return 0;
249}
250
251void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
176{ 252{
177 dbg_memalloc("%p\n", x); 253 dbg_memalloc("%p\n", x);
178 kmem_cache_free(raw_node_ref_slab, x); 254 kmem_cache_free(raw_node_ref_slab, x);
@@ -205,3 +281,42 @@ void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
205 dbg_memalloc("%p\n", x); 281 dbg_memalloc("%p\n", x);
206 kmem_cache_free(inode_cache_slab, x); 282 kmem_cache_free(inode_cache_slab, x);
207} 283}
284
285#ifdef CONFIG_JFFS2_FS_XATTR
286struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
287{
288 struct jffs2_xattr_datum *xd;
289 xd = kmem_cache_alloc(xattr_datum_cache, GFP_KERNEL);
290 dbg_memalloc("%p\n", xd);
291
292 memset(xd, 0, sizeof(struct jffs2_xattr_datum));
293 xd->class = RAWNODE_CLASS_XATTR_DATUM;
294 xd->node = (void *)xd;
295 INIT_LIST_HEAD(&xd->xindex);
296 return xd;
297}
298
299void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
300{
301 dbg_memalloc("%p\n", xd);
302 kmem_cache_free(xattr_datum_cache, xd);
303}
304
305struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
306{
307 struct jffs2_xattr_ref *ref;
308 ref = kmem_cache_alloc(xattr_ref_cache, GFP_KERNEL);
309 dbg_memalloc("%p\n", ref);
310
311 memset(ref, 0, sizeof(struct jffs2_xattr_ref));
312 ref->class = RAWNODE_CLASS_XATTR_REF;
313 ref->node = (void *)ref;
314 return ref;
315}
316
317void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
318{
319 dbg_memalloc("%p\n", ref);
320 kmem_cache_free(xattr_ref_cache, ref);
321}
322#endif
diff --git a/fs/jffs2/nodelist.c b/fs/jffs2/nodelist.c
index 1d46677afd17..7675b33396c7 100644
--- a/fs/jffs2/nodelist.c
+++ b/fs/jffs2/nodelist.c
@@ -438,8 +438,7 @@ static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info
438 if (c->mtd->point) { 438 if (c->mtd->point) {
439 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer); 439 err = c->mtd->point(c->mtd, ofs, len, &retlen, &buffer);
440 if (!err && retlen < tn->csize) { 440 if (!err && retlen < tn->csize) {
441 JFFS2_WARNING("MTD point returned len too short: %zu " 441 JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
442 "instead of %u.\n", retlen, tn->csize);
443 c->mtd->unpoint(c->mtd, buffer, ofs, len); 442 c->mtd->unpoint(c->mtd, buffer, ofs, len);
444 } else if (err) 443 } else if (err)
445 JFFS2_WARNING("MTD point failed: error code %d.\n", err); 444 JFFS2_WARNING("MTD point failed: error code %d.\n", err);
@@ -462,8 +461,7 @@ static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info
462 } 461 }
463 462
464 if (retlen != len) { 463 if (retlen != len) {
465 JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", 464 JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
466 ofs, retlen, len);
467 err = -EIO; 465 err = -EIO;
468 goto free_out; 466 goto free_out;
469 } 467 }
@@ -908,6 +906,9 @@ void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
908{ 906{
909 struct jffs2_inode_cache **prev; 907 struct jffs2_inode_cache **prev;
910 908
909#ifdef CONFIG_JFFS2_FS_XATTR
910 BUG_ON(old->xref);
911#endif
911 dbg_inocache("del %p (ino #%u)\n", old, old->ino); 912 dbg_inocache("del %p (ino #%u)\n", old, old->ino);
912 spin_lock(&c->inocache_lock); 913 spin_lock(&c->inocache_lock);
913 914
@@ -940,6 +941,7 @@ void jffs2_free_ino_caches(struct jffs2_sb_info *c)
940 this = c->inocache_list[i]; 941 this = c->inocache_list[i];
941 while (this) { 942 while (this) {
942 next = this->next; 943 next = this->next;
944 jffs2_xattr_free_inode(c, this);
943 jffs2_free_inode_cache(this); 945 jffs2_free_inode_cache(this);
944 this = next; 946 this = next;
945 } 947 }
@@ -954,9 +956,13 @@ void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
954 956
955 for (i=0; i<c->nr_blocks; i++) { 957 for (i=0; i<c->nr_blocks; i++) {
956 this = c->blocks[i].first_node; 958 this = c->blocks[i].first_node;
957 while(this) { 959 while (this) {
958 next = this->next_phys; 960 if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
959 jffs2_free_raw_node_ref(this); 961 next = this[REFS_PER_BLOCK].next_in_ino;
962 else
963 next = NULL;
964
965 jffs2_free_refblock(this);
960 this = next; 966 this = next;
961 } 967 }
962 c->blocks[i].first_node = c->blocks[i].last_node = NULL; 968 c->blocks[i].first_node = c->blocks[i].last_node = NULL;
@@ -1047,3 +1053,169 @@ void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
1047 cond_resched(); 1053 cond_resched();
1048 } 1054 }
1049} 1055}
1056
1057struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
1058 struct jffs2_eraseblock *jeb,
1059 uint32_t ofs, uint32_t len,
1060 struct jffs2_inode_cache *ic)
1061{
1062 struct jffs2_raw_node_ref *ref;
1063
1064 BUG_ON(!jeb->allocated_refs);
1065 jeb->allocated_refs--;
1066
1067 ref = jeb->last_node;
1068
1069 dbg_noderef("Last node at %p is (%08x,%p)\n", ref, ref->flash_offset,
1070 ref->next_in_ino);
1071
1072 while (ref->flash_offset != REF_EMPTY_NODE) {
1073 if (ref->flash_offset == REF_LINK_NODE)
1074 ref = ref->next_in_ino;
1075 else
1076 ref++;
1077 }
1078
1079 dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
1080 ref->flash_offset, ofs, ref->next_in_ino, len);
1081
1082 ref->flash_offset = ofs;
1083
1084 if (!jeb->first_node) {
1085 jeb->first_node = ref;
1086 BUG_ON(ref_offset(ref) != jeb->offset);
1087 } else if (unlikely(ref_offset(ref) != jeb->offset + c->sector_size - jeb->free_size)) {
1088 uint32_t last_len = ref_totlen(c, jeb, jeb->last_node);
1089
1090 JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
1091 ref, ref_offset(ref), ref_offset(ref)+len,
1092 ref_offset(jeb->last_node),
1093 ref_offset(jeb->last_node)+last_len);
1094 BUG();
1095 }
1096 jeb->last_node = ref;
1097
1098 if (ic) {
1099 ref->next_in_ino = ic->nodes;
1100 ic->nodes = ref;
1101 } else {
1102 ref->next_in_ino = NULL;
1103 }
1104
1105 switch(ref_flags(ref)) {
1106 case REF_UNCHECKED:
1107 c->unchecked_size += len;
1108 jeb->unchecked_size += len;
1109 break;
1110
1111 case REF_NORMAL:
1112 case REF_PRISTINE:
1113 c->used_size += len;
1114 jeb->used_size += len;
1115 break;
1116
1117 case REF_OBSOLETE:
1118 c->dirty_size += len;
1119 jeb->dirty_size += len;
1120 break;
1121 }
1122 c->free_size -= len;
1123 jeb->free_size -= len;
1124
1125#ifdef TEST_TOTLEN
1126 /* Set (and test) __totlen field... for now */
1127 ref->__totlen = len;
1128 ref_totlen(c, jeb, ref);
1129#endif
1130 return ref;
1131}
1132
1133/* No locking, no reservation of 'ref'. Do not use on a live file system */
1134int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1135 uint32_t size)
1136{
1137 if (!size)
1138 return 0;
1139 if (unlikely(size > jeb->free_size)) {
1140 printk(KERN_CRIT "Dirty space 0x%x larger then free_size 0x%x (wasted 0x%x)\n",
1141 size, jeb->free_size, jeb->wasted_size);
1142 BUG();
1143 }
1144 /* REF_EMPTY_NODE is !obsolete, so that works OK */
1145 if (jeb->last_node && ref_obsolete(jeb->last_node)) {
1146#ifdef TEST_TOTLEN
1147 jeb->last_node->__totlen += size;
1148#endif
1149 c->dirty_size += size;
1150 c->free_size -= size;
1151 jeb->dirty_size += size;
1152 jeb->free_size -= size;
1153 } else {
1154 uint32_t ofs = jeb->offset + c->sector_size - jeb->free_size;
1155 ofs |= REF_OBSOLETE;
1156
1157 jffs2_link_node_ref(c, jeb, ofs, size, NULL);
1158 }
1159
1160 return 0;
1161}
1162
1163/* Calculate totlen from surrounding nodes or eraseblock */
1164static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
1165 struct jffs2_eraseblock *jeb,
1166 struct jffs2_raw_node_ref *ref)
1167{
1168 uint32_t ref_end;
1169 struct jffs2_raw_node_ref *next_ref = ref_next(ref);
1170
1171 if (next_ref)
1172 ref_end = ref_offset(next_ref);
1173 else {
1174 if (!jeb)
1175 jeb = &c->blocks[ref->flash_offset / c->sector_size];
1176
1177 /* Last node in block. Use free_space */
1178 if (unlikely(ref != jeb->last_node)) {
1179 printk(KERN_CRIT "ref %p @0x%08x is not jeb->last_node (%p @0x%08x)\n",
1180 ref, ref_offset(ref), jeb->last_node, jeb->last_node?ref_offset(jeb->last_node):0);
1181 BUG();
1182 }
1183 ref_end = jeb->offset + c->sector_size - jeb->free_size;
1184 }
1185 return ref_end - ref_offset(ref);
1186}
1187
1188uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
1189 struct jffs2_raw_node_ref *ref)
1190{
1191 uint32_t ret;
1192
1193 ret = __ref_totlen(c, jeb, ref);
1194
1195#ifdef TEST_TOTLEN
1196 if (unlikely(ret != ref->__totlen)) {
1197 if (!jeb)
1198 jeb = &c->blocks[ref->flash_offset / c->sector_size];
1199
1200 printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
1201 ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
1202 ret, ref->__totlen);
1203 if (ref_next(ref)) {
1204 printk(KERN_CRIT "next %p (0x%08x-0x%08x)\n", ref_next(ref), ref_offset(ref_next(ref)),
1205 ref_offset(ref_next(ref))+ref->__totlen);
1206 } else
1207 printk(KERN_CRIT "No next ref. jeb->last_node is %p\n", jeb->last_node);
1208
1209 printk(KERN_CRIT "jeb->wasted_size %x, dirty_size %x, used_size %x, free_size %x\n", jeb->wasted_size, jeb->dirty_size, jeb->used_size, jeb->free_size);
1210
1211#if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
1212 __jffs2_dbg_dump_node_refs_nolock(c, jeb);
1213#endif
1214
1215 WARN_ON(1);
1216
1217 ret = ref->__totlen;
1218 }
1219#endif /* TEST_TOTLEN */
1220 return ret;
1221}
diff --git a/fs/jffs2/nodelist.h b/fs/jffs2/nodelist.h
index 23a67bb3052f..f752baa8d399 100644
--- a/fs/jffs2/nodelist.h
+++ b/fs/jffs2/nodelist.h
@@ -14,12 +14,13 @@
14#ifndef __JFFS2_NODELIST_H__ 14#ifndef __JFFS2_NODELIST_H__
15#define __JFFS2_NODELIST_H__ 15#define __JFFS2_NODELIST_H__
16 16
17#include <linux/config.h>
18#include <linux/fs.h> 17#include <linux/fs.h>
19#include <linux/types.h> 18#include <linux/types.h>
20#include <linux/jffs2.h> 19#include <linux/jffs2.h>
21#include <linux/jffs2_fs_sb.h> 20#include "jffs2_fs_sb.h"
22#include <linux/jffs2_fs_i.h> 21#include "jffs2_fs_i.h"
22#include "xattr.h"
23#include "acl.h"
23#include "summary.h" 24#include "summary.h"
24 25
25#ifdef __ECOS 26#ifdef __ECOS
@@ -75,14 +76,50 @@
75struct jffs2_raw_node_ref 76struct jffs2_raw_node_ref
76{ 77{
77 struct jffs2_raw_node_ref *next_in_ino; /* Points to the next raw_node_ref 78 struct jffs2_raw_node_ref *next_in_ino; /* Points to the next raw_node_ref
78 for this inode. If this is the last, it points to the inode_cache 79 for this object. If this _is_ the last, it points to the inode_cache,
79 for this inode instead. The inode_cache will have NULL in the first 80 xattr_ref or xattr_datum instead. The common part of those structures
80 word so you know when you've got there :) */ 81 has NULL in the first word. See jffs2_raw_ref_to_ic() below */
81 struct jffs2_raw_node_ref *next_phys;
82 uint32_t flash_offset; 82 uint32_t flash_offset;
83#define TEST_TOTLEN
84#ifdef TEST_TOTLEN
83 uint32_t __totlen; /* This may die; use ref_totlen(c, jeb, ) below */ 85 uint32_t __totlen; /* This may die; use ref_totlen(c, jeb, ) below */
86#endif
84}; 87};
85 88
89#define REF_LINK_NODE ((int32_t)-1)
90#define REF_EMPTY_NODE ((int32_t)-2)
91
92/* Use blocks of about 256 bytes */
93#define REFS_PER_BLOCK ((255/sizeof(struct jffs2_raw_node_ref))-1)
94
95static inline struct jffs2_raw_node_ref *ref_next(struct jffs2_raw_node_ref *ref)
96{
97 ref++;
98
99 /* Link to another block of refs */
100 if (ref->flash_offset == REF_LINK_NODE) {
101 ref = ref->next_in_ino;
102 if (!ref)
103 return ref;
104 }
105
106 /* End of chain */
107 if (ref->flash_offset == REF_EMPTY_NODE)
108 return NULL;
109
110 return ref;
111}
112
113static inline struct jffs2_inode_cache *jffs2_raw_ref_to_ic(struct jffs2_raw_node_ref *raw)
114{
115 while(raw->next_in_ino)
116 raw = raw->next_in_ino;
117
118 /* NB. This can be a jffs2_xattr_datum or jffs2_xattr_ref and
119 not actually a jffs2_inode_cache. Check ->class */
120 return ((struct jffs2_inode_cache *)raw);
121}
122
86 /* flash_offset & 3 always has to be zero, because nodes are 123 /* flash_offset & 3 always has to be zero, because nodes are
87 always aligned at 4 bytes. So we have a couple of extra bits 124 always aligned at 4 bytes. So we have a couple of extra bits
88 to play with, which indicate the node's status; see below: */ 125 to play with, which indicate the node's status; see below: */
@@ -95,6 +132,11 @@ struct jffs2_raw_node_ref
95#define ref_obsolete(ref) (((ref)->flash_offset & 3) == REF_OBSOLETE) 132#define ref_obsolete(ref) (((ref)->flash_offset & 3) == REF_OBSOLETE)
96#define mark_ref_normal(ref) do { (ref)->flash_offset = ref_offset(ref) | REF_NORMAL; } while(0) 133#define mark_ref_normal(ref) do { (ref)->flash_offset = ref_offset(ref) | REF_NORMAL; } while(0)
97 134
135/* NB: REF_PRISTINE for an inode-less node (ref->next_in_ino == NULL) indicates
136 it is an unknown node of type JFFS2_NODETYPE_RWCOMPAT_COPY, so it'll get
137 copied. If you need to do anything different to GC inode-less nodes, then
138 you need to modify gc.c accordingly. */
139
98/* For each inode in the filesystem, we need to keep a record of 140/* For each inode in the filesystem, we need to keep a record of
99 nlink, because it would be a PITA to scan the whole directory tree 141 nlink, because it would be a PITA to scan the whole directory tree
100 at read_inode() time to calculate it, and to keep sufficient information 142 at read_inode() time to calculate it, and to keep sufficient information
@@ -103,15 +145,27 @@ struct jffs2_raw_node_ref
103 a pointer to the first physical node which is part of this inode, too. 145 a pointer to the first physical node which is part of this inode, too.
104*/ 146*/
105struct jffs2_inode_cache { 147struct jffs2_inode_cache {
148 /* First part of structure is shared with other objects which
149 can terminate the raw node refs' next_in_ino list -- which
150 currently struct jffs2_xattr_datum and struct jffs2_xattr_ref. */
151
106 struct jffs2_full_dirent *scan_dents; /* Used during scan to hold 152 struct jffs2_full_dirent *scan_dents; /* Used during scan to hold
107 temporary lists of dirents, and later must be set to 153 temporary lists of dirents, and later must be set to
108 NULL to mark the end of the raw_node_ref->next_in_ino 154 NULL to mark the end of the raw_node_ref->next_in_ino
109 chain. */ 155 chain. */
110 struct jffs2_inode_cache *next;
111 struct jffs2_raw_node_ref *nodes; 156 struct jffs2_raw_node_ref *nodes;
157 uint8_t class; /* It's used for identification */
158
159 /* end of shared structure */
160
161 uint8_t flags;
162 uint16_t state;
112 uint32_t ino; 163 uint32_t ino;
164 struct jffs2_inode_cache *next;
165#ifdef CONFIG_JFFS2_FS_XATTR
166 struct jffs2_xattr_ref *xref;
167#endif
113 int nlink; 168 int nlink;
114 int state;
115}; 169};
116 170
117/* Inode states for 'state' above. We need the 'GC' state to prevent 171/* Inode states for 'state' above. We need the 'GC' state to prevent
@@ -125,8 +179,16 @@ struct jffs2_inode_cache {
125#define INO_STATE_READING 5 /* In read_inode() */ 179#define INO_STATE_READING 5 /* In read_inode() */
126#define INO_STATE_CLEARING 6 /* In clear_inode() */ 180#define INO_STATE_CLEARING 6 /* In clear_inode() */
127 181
182#define INO_FLAGS_XATTR_CHECKED 0x01 /* has no duplicate xattr_ref */
183
184#define RAWNODE_CLASS_INODE_CACHE 0
185#define RAWNODE_CLASS_XATTR_DATUM 1
186#define RAWNODE_CLASS_XATTR_REF 2
187
128#define INOCACHE_HASHSIZE 128 188#define INOCACHE_HASHSIZE 128
129 189
190#define write_ofs(c) ((c)->nextblock->offset + (c)->sector_size - (c)->nextblock->free_size)
191
130/* 192/*
131 Larger representation of a raw node, kept in-core only when the 193 Larger representation of a raw node, kept in-core only when the
132 struct inode for this particular ino is instantiated. 194 struct inode for this particular ino is instantiated.
@@ -192,6 +254,7 @@ struct jffs2_eraseblock
192 uint32_t wasted_size; 254 uint32_t wasted_size;
193 uint32_t free_size; /* Note that sector_size - free_size 255 uint32_t free_size; /* Note that sector_size - free_size
194 is the address of the first free space */ 256 is the address of the first free space */
257 uint32_t allocated_refs;
195 struct jffs2_raw_node_ref *first_node; 258 struct jffs2_raw_node_ref *first_node;
196 struct jffs2_raw_node_ref *last_node; 259 struct jffs2_raw_node_ref *last_node;
197 260
@@ -203,57 +266,7 @@ static inline int jffs2_blocks_use_vmalloc(struct jffs2_sb_info *c)
203 return ((c->flash_size / c->sector_size) * sizeof (struct jffs2_eraseblock)) > (128 * 1024); 266 return ((c->flash_size / c->sector_size) * sizeof (struct jffs2_eraseblock)) > (128 * 1024);
204} 267}
205 268
206/* Calculate totlen from surrounding nodes or eraseblock */ 269#define ref_totlen(a, b, c) __jffs2_ref_totlen((a), (b), (c))
207static inline uint32_t __ref_totlen(struct jffs2_sb_info *c,
208 struct jffs2_eraseblock *jeb,
209 struct jffs2_raw_node_ref *ref)
210{
211 uint32_t ref_end;
212
213 if (ref->next_phys)
214 ref_end = ref_offset(ref->next_phys);
215 else {
216 if (!jeb)
217 jeb = &c->blocks[ref->flash_offset / c->sector_size];
218
219 /* Last node in block. Use free_space */
220 BUG_ON(ref != jeb->last_node);
221 ref_end = jeb->offset + c->sector_size - jeb->free_size;
222 }
223 return ref_end - ref_offset(ref);
224}
225
226static inline uint32_t ref_totlen(struct jffs2_sb_info *c,
227 struct jffs2_eraseblock *jeb,
228 struct jffs2_raw_node_ref *ref)
229{
230 uint32_t ret;
231
232#if CONFIG_JFFS2_FS_DEBUG > 0
233 if (jeb && jeb != &c->blocks[ref->flash_offset / c->sector_size]) {
234 printk(KERN_CRIT "ref_totlen called with wrong block -- at 0x%08x instead of 0x%08x; ref 0x%08x\n",
235 jeb->offset, c->blocks[ref->flash_offset / c->sector_size].offset, ref_offset(ref));
236 BUG();
237 }
238#endif
239
240#if 1
241 ret = ref->__totlen;
242#else
243 /* This doesn't actually work yet */
244 ret = __ref_totlen(c, jeb, ref);
245 if (ret != ref->__totlen) {
246 printk(KERN_CRIT "Totlen for ref at %p (0x%08x-0x%08x) miscalculated as 0x%x instead of %x\n",
247 ref, ref_offset(ref), ref_offset(ref)+ref->__totlen,
248 ret, ref->__totlen);
249 if (!jeb)
250 jeb = &c->blocks[ref->flash_offset / c->sector_size];
251 jffs2_dbg_dump_node_refs_nolock(c, jeb);
252 BUG();
253 }
254#endif
255 return ret;
256}
257 270
258#define ALLOC_NORMAL 0 /* Normal allocation */ 271#define ALLOC_NORMAL 0 /* Normal allocation */
259#define ALLOC_DELETION 1 /* Deletion node. Best to allow it */ 272#define ALLOC_DELETION 1 /* Deletion node. Best to allow it */
@@ -268,13 +281,15 @@ static inline uint32_t ref_totlen(struct jffs2_sb_info *c,
268 281
269#define PAD(x) (((x)+3)&~3) 282#define PAD(x) (((x)+3)&~3)
270 283
271static inline struct jffs2_inode_cache *jffs2_raw_ref_to_ic(struct jffs2_raw_node_ref *raw) 284static inline int jffs2_encode_dev(union jffs2_device_node *jdev, dev_t rdev)
272{ 285{
273 while(raw->next_in_ino) { 286 if (old_valid_dev(rdev)) {
274 raw = raw->next_in_ino; 287 jdev->old = cpu_to_je16(old_encode_dev(rdev));
288 return sizeof(jdev->old);
289 } else {
290 jdev->new = cpu_to_je32(new_encode_dev(rdev));
291 return sizeof(jdev->new);
275 } 292 }
276
277 return ((struct jffs2_inode_cache *)raw);
278} 293}
279 294
280static inline struct jffs2_node_frag *frag_first(struct rb_root *root) 295static inline struct jffs2_node_frag *frag_first(struct rb_root *root)
@@ -299,7 +314,6 @@ static inline struct jffs2_node_frag *frag_last(struct rb_root *root)
299 return rb_entry(node, struct jffs2_node_frag, rb); 314 return rb_entry(node, struct jffs2_node_frag, rb);
300} 315}
301 316
302#define rb_parent(rb) ((rb)->rb_parent)
303#define frag_next(frag) rb_entry(rb_next(&(frag)->rb), struct jffs2_node_frag, rb) 317#define frag_next(frag) rb_entry(rb_next(&(frag)->rb), struct jffs2_node_frag, rb)
304#define frag_prev(frag) rb_entry(rb_prev(&(frag)->rb), struct jffs2_node_frag, rb) 318#define frag_prev(frag) rb_entry(rb_prev(&(frag)->rb), struct jffs2_node_frag, rb)
305#define frag_parent(frag) rb_entry(rb_parent(&(frag)->rb), struct jffs2_node_frag, rb) 319#define frag_parent(frag) rb_entry(rb_parent(&(frag)->rb), struct jffs2_node_frag, rb)
@@ -324,28 +338,44 @@ void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *t
324int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn); 338int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn);
325void jffs2_truncate_fragtree (struct jffs2_sb_info *c, struct rb_root *list, uint32_t size); 339void jffs2_truncate_fragtree (struct jffs2_sb_info *c, struct rb_root *list, uint32_t size);
326int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn); 340int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn);
341struct jffs2_raw_node_ref *jffs2_link_node_ref(struct jffs2_sb_info *c,
342 struct jffs2_eraseblock *jeb,
343 uint32_t ofs, uint32_t len,
344 struct jffs2_inode_cache *ic);
345extern uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c,
346 struct jffs2_eraseblock *jeb,
347 struct jffs2_raw_node_ref *ref);
327 348
328/* nodemgmt.c */ 349/* nodemgmt.c */
329int jffs2_thread_should_wake(struct jffs2_sb_info *c); 350int jffs2_thread_should_wake(struct jffs2_sb_info *c);
330int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, 351int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
331 uint32_t *len, int prio, uint32_t sumsize); 352 uint32_t *len, int prio, uint32_t sumsize);
332int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, 353int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize,
333 uint32_t *len, uint32_t sumsize); 354 uint32_t *len, uint32_t sumsize);
334int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new); 355struct jffs2_raw_node_ref *jffs2_add_physical_node_ref(struct jffs2_sb_info *c,
356 uint32_t ofs, uint32_t len,
357 struct jffs2_inode_cache *ic);
335void jffs2_complete_reservation(struct jffs2_sb_info *c); 358void jffs2_complete_reservation(struct jffs2_sb_info *c);
336void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *raw); 359void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *raw);
337 360
338/* write.c */ 361/* write.c */
339int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri); 362int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri);
340 363
341struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const unsigned char *data, uint32_t datalen, uint32_t flash_ofs, int alloc_mode); 364struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
342struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_dirent *rd, const unsigned char *name, uint32_t namelen, uint32_t flash_ofs, int alloc_mode); 365 struct jffs2_raw_inode *ri, const unsigned char *data,
366 uint32_t datalen, int alloc_mode);
367struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
368 struct jffs2_raw_dirent *rd, const unsigned char *name,
369 uint32_t namelen, int alloc_mode);
343int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 370int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
344 struct jffs2_raw_inode *ri, unsigned char *buf, 371 struct jffs2_raw_inode *ri, unsigned char *buf,
345 uint32_t offset, uint32_t writelen, uint32_t *retlen); 372 uint32_t offset, uint32_t writelen, uint32_t *retlen);
346int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const char *name, int namelen); 373int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f,
347int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, const char *name, int namelen, struct jffs2_inode_info *dead_f, uint32_t time); 374 struct jffs2_raw_inode *ri, const char *name, int namelen);
348int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino, uint8_t type, const char *name, int namelen, uint32_t time); 375int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, const char *name,
376 int namelen, struct jffs2_inode_info *dead_f, uint32_t time);
377int jffs2_do_link(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino,
378 uint8_t type, const char *name, int namelen, uint32_t time);
349 379
350 380
351/* readinode.c */ 381/* readinode.c */
@@ -368,12 +398,19 @@ struct jffs2_raw_inode *jffs2_alloc_raw_inode(void);
368void jffs2_free_raw_inode(struct jffs2_raw_inode *); 398void jffs2_free_raw_inode(struct jffs2_raw_inode *);
369struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void); 399struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void);
370void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *); 400void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *);
371struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void); 401int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
372void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *); 402 struct jffs2_eraseblock *jeb, int nr);
403void jffs2_free_refblock(struct jffs2_raw_node_ref *);
373struct jffs2_node_frag *jffs2_alloc_node_frag(void); 404struct jffs2_node_frag *jffs2_alloc_node_frag(void);
374void jffs2_free_node_frag(struct jffs2_node_frag *); 405void jffs2_free_node_frag(struct jffs2_node_frag *);
375struct jffs2_inode_cache *jffs2_alloc_inode_cache(void); 406struct jffs2_inode_cache *jffs2_alloc_inode_cache(void);
376void jffs2_free_inode_cache(struct jffs2_inode_cache *); 407void jffs2_free_inode_cache(struct jffs2_inode_cache *);
408#ifdef CONFIG_JFFS2_FS_XATTR
409struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void);
410void jffs2_free_xattr_datum(struct jffs2_xattr_datum *);
411struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void);
412void jffs2_free_xattr_ref(struct jffs2_xattr_ref *);
413#endif
377 414
378/* gc.c */ 415/* gc.c */
379int jffs2_garbage_collect_pass(struct jffs2_sb_info *c); 416int jffs2_garbage_collect_pass(struct jffs2_sb_info *c);
@@ -393,12 +430,14 @@ int jffs2_fill_scan_buf(struct jffs2_sb_info *c, void *buf,
393 uint32_t ofs, uint32_t len); 430 uint32_t ofs, uint32_t len);
394struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino); 431struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uint32_t ino);
395int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb); 432int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
433int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t size);
396 434
397/* build.c */ 435/* build.c */
398int jffs2_do_mount_fs(struct jffs2_sb_info *c); 436int jffs2_do_mount_fs(struct jffs2_sb_info *c);
399 437
400/* erase.c */ 438/* erase.c */
401void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count); 439void jffs2_erase_pending_blocks(struct jffs2_sb_info *c, int count);
440void jffs2_free_jeb_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
402 441
403#ifdef CONFIG_JFFS2_FS_WRITEBUFFER 442#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
404/* wbuf.c */ 443/* wbuf.c */
diff --git a/fs/jffs2/nodemgmt.c b/fs/jffs2/nodemgmt.c
index 49127a1f0458..d88376992ed9 100644
--- a/fs/jffs2/nodemgmt.c
+++ b/fs/jffs2/nodemgmt.c
@@ -23,13 +23,12 @@
23 * jffs2_reserve_space - request physical space to write nodes to flash 23 * jffs2_reserve_space - request physical space to write nodes to flash
24 * @c: superblock info 24 * @c: superblock info
25 * @minsize: Minimum acceptable size of allocation 25 * @minsize: Minimum acceptable size of allocation
26 * @ofs: Returned value of node offset
27 * @len: Returned value of allocation length 26 * @len: Returned value of allocation length
28 * @prio: Allocation type - ALLOC_{NORMAL,DELETION} 27 * @prio: Allocation type - ALLOC_{NORMAL,DELETION}
29 * 28 *
30 * Requests a block of physical space on the flash. Returns zero for success 29 * Requests a block of physical space on the flash. Returns zero for success
31 * and puts 'ofs' and 'len' into the appriopriate place, or returns -ENOSPC 30 * and puts 'len' into the appropriate place, or returns -ENOSPC or other
32 * or other error if appropriate. 31 * error if appropriate. Doesn't return len since that's
33 * 32 *
34 * If it returns zero, jffs2_reserve_space() also downs the per-filesystem 33 * If it returns zero, jffs2_reserve_space() also downs the per-filesystem
35 * allocation semaphore, to prevent more than one allocation from being 34 * allocation semaphore, to prevent more than one allocation from being
@@ -40,9 +39,9 @@
40 */ 39 */
41 40
42static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, 41static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
43 uint32_t *ofs, uint32_t *len, uint32_t sumsize); 42 uint32_t *len, uint32_t sumsize);
44 43
45int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, 44int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
46 uint32_t *len, int prio, uint32_t sumsize) 45 uint32_t *len, int prio, uint32_t sumsize)
47{ 46{
48 int ret = -EAGAIN; 47 int ret = -EAGAIN;
@@ -132,19 +131,21 @@ int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs
132 spin_lock(&c->erase_completion_lock); 131 spin_lock(&c->erase_completion_lock);
133 } 132 }
134 133
135 ret = jffs2_do_reserve_space(c, minsize, ofs, len, sumsize); 134 ret = jffs2_do_reserve_space(c, minsize, len, sumsize);
136 if (ret) { 135 if (ret) {
137 D1(printk(KERN_DEBUG "jffs2_reserve_space: ret is %d\n", ret)); 136 D1(printk(KERN_DEBUG "jffs2_reserve_space: ret is %d\n", ret));
138 } 137 }
139 } 138 }
140 spin_unlock(&c->erase_completion_lock); 139 spin_unlock(&c->erase_completion_lock);
140 if (!ret)
141 ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
141 if (ret) 142 if (ret)
142 up(&c->alloc_sem); 143 up(&c->alloc_sem);
143 return ret; 144 return ret;
144} 145}
145 146
146int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, 147int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize,
147 uint32_t *len, uint32_t sumsize) 148 uint32_t *len, uint32_t sumsize)
148{ 149{
149 int ret = -EAGAIN; 150 int ret = -EAGAIN;
150 minsize = PAD(minsize); 151 minsize = PAD(minsize);
@@ -153,12 +154,15 @@ int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *
153 154
154 spin_lock(&c->erase_completion_lock); 155 spin_lock(&c->erase_completion_lock);
155 while(ret == -EAGAIN) { 156 while(ret == -EAGAIN) {
156 ret = jffs2_do_reserve_space(c, minsize, ofs, len, sumsize); 157 ret = jffs2_do_reserve_space(c, minsize, len, sumsize);
157 if (ret) { 158 if (ret) {
158 D1(printk(KERN_DEBUG "jffs2_reserve_space_gc: looping, ret is %d\n", ret)); 159 D1(printk(KERN_DEBUG "jffs2_reserve_space_gc: looping, ret is %d\n", ret));
159 } 160 }
160 } 161 }
161 spin_unlock(&c->erase_completion_lock); 162 spin_unlock(&c->erase_completion_lock);
163 if (!ret)
164 ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
165
162 return ret; 166 return ret;
163} 167}
164 168
@@ -207,8 +211,7 @@ static int jffs2_find_nextblock(struct jffs2_sb_info *c)
207 struct jffs2_eraseblock *ejeb; 211 struct jffs2_eraseblock *ejeb;
208 212
209 ejeb = list_entry(c->erasable_list.next, struct jffs2_eraseblock, list); 213 ejeb = list_entry(c->erasable_list.next, struct jffs2_eraseblock, list);
210 list_del(&ejeb->list); 214 list_move_tail(&ejeb->list, &c->erase_pending_list);
211 list_add_tail(&ejeb->list, &c->erase_pending_list);
212 c->nr_erasing_blocks++; 215 c->nr_erasing_blocks++;
213 jffs2_erase_pending_trigger(c); 216 jffs2_erase_pending_trigger(c);
214 D1(printk(KERN_DEBUG "jffs2_find_nextblock: Triggering erase of erasable block at 0x%08x\n", 217 D1(printk(KERN_DEBUG "jffs2_find_nextblock: Triggering erase of erasable block at 0x%08x\n",
@@ -259,10 +262,11 @@ static int jffs2_find_nextblock(struct jffs2_sb_info *c)
259} 262}
260 263
261/* Called with alloc sem _and_ erase_completion_lock */ 264/* Called with alloc sem _and_ erase_completion_lock */
262static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, uint32_t *len, uint32_t sumsize) 265static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
266 uint32_t *len, uint32_t sumsize)
263{ 267{
264 struct jffs2_eraseblock *jeb = c->nextblock; 268 struct jffs2_eraseblock *jeb = c->nextblock;
265 uint32_t reserved_size; /* for summary information at the end of the jeb */ 269 uint32_t reserved_size; /* for summary information at the end of the jeb */
266 int ret; 270 int ret;
267 271
268 restart: 272 restart:
@@ -312,6 +316,8 @@ static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uin
312 } 316 }
313 } else { 317 } else {
314 if (jeb && minsize > jeb->free_size) { 318 if (jeb && minsize > jeb->free_size) {
319 uint32_t waste;
320
315 /* Skip the end of this block and file it as having some dirty space */ 321 /* Skip the end of this block and file it as having some dirty space */
316 /* If there's a pending write to it, flush now */ 322 /* If there's a pending write to it, flush now */
317 323
@@ -324,10 +330,26 @@ static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uin
324 goto restart; 330 goto restart;
325 } 331 }
326 332
327 c->wasted_size += jeb->free_size; 333 spin_unlock(&c->erase_completion_lock);
328 c->free_size -= jeb->free_size; 334
329 jeb->wasted_size += jeb->free_size; 335 ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
330 jeb->free_size = 0; 336 if (ret)
337 return ret;
338 /* Just lock it again and continue. Nothing much can change because
339 we hold c->alloc_sem anyway. In fact, it's not entirely clear why
340 we hold c->erase_completion_lock in the majority of this function...
341 but that's a question for another (more caffeine-rich) day. */
342 spin_lock(&c->erase_completion_lock);
343
344 waste = jeb->free_size;
345 jffs2_link_node_ref(c, jeb,
346 (jeb->offset + c->sector_size - waste) | REF_OBSOLETE,
347 waste, NULL);
348 /* FIXME: that made it count as dirty. Convert to wasted */
349 jeb->dirty_size -= waste;
350 c->dirty_size -= waste;
351 jeb->wasted_size += waste;
352 c->wasted_size += waste;
331 353
332 jffs2_close_nextblock(c, jeb); 354 jffs2_close_nextblock(c, jeb);
333 jeb = NULL; 355 jeb = NULL;
@@ -349,7 +371,6 @@ static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uin
349 } 371 }
350 /* OK, jeb (==c->nextblock) is now pointing at a block which definitely has 372 /* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
351 enough space */ 373 enough space */
352 *ofs = jeb->offset + (c->sector_size - jeb->free_size);
353 *len = jeb->free_size - reserved_size; 374 *len = jeb->free_size - reserved_size;
354 375
355 if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size && 376 if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
@@ -365,7 +386,8 @@ static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uin
365 spin_lock(&c->erase_completion_lock); 386 spin_lock(&c->erase_completion_lock);
366 } 387 }
367 388
368 D1(printk(KERN_DEBUG "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x\n", *len, *ofs)); 389 D1(printk(KERN_DEBUG "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x\n",
390 *len, jeb->offset + (c->sector_size - jeb->free_size)));
369 return 0; 391 return 0;
370} 392}
371 393
@@ -374,7 +396,6 @@ static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uin
374 * @c: superblock info 396 * @c: superblock info
375 * @new: new node reference to add 397 * @new: new node reference to add
376 * @len: length of this physical node 398 * @len: length of this physical node
377 * @dirty: dirty flag for new node
378 * 399 *
379 * Should only be used to report nodes for which space has been allocated 400 * Should only be used to report nodes for which space has been allocated
380 * by jffs2_reserve_space. 401 * by jffs2_reserve_space.
@@ -382,42 +403,30 @@ static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uin
382 * Must be called with the alloc_sem held. 403 * Must be called with the alloc_sem held.
383 */ 404 */
384 405
385int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new) 406struct jffs2_raw_node_ref *jffs2_add_physical_node_ref(struct jffs2_sb_info *c,
407 uint32_t ofs, uint32_t len,
408 struct jffs2_inode_cache *ic)
386{ 409{
387 struct jffs2_eraseblock *jeb; 410 struct jffs2_eraseblock *jeb;
388 uint32_t len; 411 struct jffs2_raw_node_ref *new;
389 412
390 jeb = &c->blocks[new->flash_offset / c->sector_size]; 413 jeb = &c->blocks[ofs / c->sector_size];
391 len = ref_totlen(c, jeb, new);
392 414
393 D1(printk(KERN_DEBUG "jffs2_add_physical_node_ref(): Node at 0x%x(%d), size 0x%x\n", ref_offset(new), ref_flags(new), len)); 415 D1(printk(KERN_DEBUG "jffs2_add_physical_node_ref(): Node at 0x%x(%d), size 0x%x\n",
416 ofs & ~3, ofs & 3, len));
394#if 1 417#if 1
395 /* we could get some obsolete nodes after nextblock was refiled 418 /* Allow non-obsolete nodes only to be added at the end of c->nextblock,
396 in wbuf.c */ 419 if c->nextblock is set. Note that wbuf.c will file obsolete nodes
397 if ((c->nextblock || !ref_obsolete(new)) 420 even after refiling c->nextblock */
398 &&(jeb != c->nextblock || ref_offset(new) != jeb->offset + (c->sector_size - jeb->free_size))) { 421 if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
422 && (jeb != c->nextblock || (ofs & ~3) != jeb->offset + (c->sector_size - jeb->free_size))) {
399 printk(KERN_WARNING "argh. node added in wrong place\n"); 423 printk(KERN_WARNING "argh. node added in wrong place\n");
400 jffs2_free_raw_node_ref(new); 424 return ERR_PTR(-EINVAL);
401 return -EINVAL;
402 } 425 }
403#endif 426#endif
404 spin_lock(&c->erase_completion_lock); 427 spin_lock(&c->erase_completion_lock);
405 428
406 if (!jeb->first_node) 429 new = jffs2_link_node_ref(c, jeb, ofs, len, ic);
407 jeb->first_node = new;
408 if (jeb->last_node)
409 jeb->last_node->next_phys = new;
410 jeb->last_node = new;
411
412 jeb->free_size -= len;
413 c->free_size -= len;
414 if (ref_obsolete(new)) {
415 jeb->dirty_size += len;
416 c->dirty_size += len;
417 } else {
418 jeb->used_size += len;
419 c->used_size += len;
420 }
421 430
422 if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) { 431 if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) {
423 /* If it lives on the dirty_list, jffs2_reserve_space will put it there */ 432 /* If it lives on the dirty_list, jffs2_reserve_space will put it there */
@@ -438,7 +447,7 @@ int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_r
438 447
439 spin_unlock(&c->erase_completion_lock); 448 spin_unlock(&c->erase_completion_lock);
440 449
441 return 0; 450 return new;
442} 451}
443 452
444 453
@@ -470,8 +479,9 @@ void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
470 struct jffs2_unknown_node n; 479 struct jffs2_unknown_node n;
471 int ret, addedsize; 480 int ret, addedsize;
472 size_t retlen; 481 size_t retlen;
482 uint32_t freed_len;
473 483
474 if(!ref) { 484 if(unlikely(!ref)) {
475 printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node\n"); 485 printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
476 return; 486 return;
477 } 487 }
@@ -499,32 +509,34 @@ void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
499 509
500 spin_lock(&c->erase_completion_lock); 510 spin_lock(&c->erase_completion_lock);
501 511
512 freed_len = ref_totlen(c, jeb, ref);
513
502 if (ref_flags(ref) == REF_UNCHECKED) { 514 if (ref_flags(ref) == REF_UNCHECKED) {
503 D1(if (unlikely(jeb->unchecked_size < ref_totlen(c, jeb, ref))) { 515 D1(if (unlikely(jeb->unchecked_size < freed_len)) {
504 printk(KERN_NOTICE "raw unchecked node of size 0x%08x freed from erase block %d at 0x%08x, but unchecked_size was already 0x%08x\n", 516 printk(KERN_NOTICE "raw unchecked node of size 0x%08x freed from erase block %d at 0x%08x, but unchecked_size was already 0x%08x\n",
505 ref_totlen(c, jeb, ref), blocknr, ref->flash_offset, jeb->used_size); 517 freed_len, blocknr, ref->flash_offset, jeb->used_size);
506 BUG(); 518 BUG();
507 }) 519 })
508 D1(printk(KERN_DEBUG "Obsoleting previously unchecked node at 0x%08x of len %x: ", ref_offset(ref), ref_totlen(c, jeb, ref))); 520 D1(printk(KERN_DEBUG "Obsoleting previously unchecked node at 0x%08x of len %x: ", ref_offset(ref), freed_len));
509 jeb->unchecked_size -= ref_totlen(c, jeb, ref); 521 jeb->unchecked_size -= freed_len;
510 c->unchecked_size -= ref_totlen(c, jeb, ref); 522 c->unchecked_size -= freed_len;
511 } else { 523 } else {
512 D1(if (unlikely(jeb->used_size < ref_totlen(c, jeb, ref))) { 524 D1(if (unlikely(jeb->used_size < freed_len)) {
513 printk(KERN_NOTICE "raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n", 525 printk(KERN_NOTICE "raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n",
514 ref_totlen(c, jeb, ref), blocknr, ref->flash_offset, jeb->used_size); 526 freed_len, blocknr, ref->flash_offset, jeb->used_size);
515 BUG(); 527 BUG();
516 }) 528 })
517 D1(printk(KERN_DEBUG "Obsoleting node at 0x%08x of len %#x: ", ref_offset(ref), ref_totlen(c, jeb, ref))); 529 D1(printk(KERN_DEBUG "Obsoleting node at 0x%08x of len %#x: ", ref_offset(ref), freed_len));
518 jeb->used_size -= ref_totlen(c, jeb, ref); 530 jeb->used_size -= freed_len;
519 c->used_size -= ref_totlen(c, jeb, ref); 531 c->used_size -= freed_len;
520 } 532 }
521 533
522 // Take care, that wasted size is taken into concern 534 // Take care, that wasted size is taken into concern
523 if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + ref_totlen(c, jeb, ref))) && jeb != c->nextblock) { 535 if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + freed_len)) && jeb != c->nextblock) {
524 D1(printk(KERN_DEBUG "Dirtying\n")); 536 D1(printk("Dirtying\n"));
525 addedsize = ref_totlen(c, jeb, ref); 537 addedsize = freed_len;
526 jeb->dirty_size += ref_totlen(c, jeb, ref); 538 jeb->dirty_size += freed_len;
527 c->dirty_size += ref_totlen(c, jeb, ref); 539 c->dirty_size += freed_len;
528 540
529 /* Convert wasted space to dirty, if not a bad block */ 541 /* Convert wasted space to dirty, if not a bad block */
530 if (jeb->wasted_size) { 542 if (jeb->wasted_size) {
@@ -543,10 +555,10 @@ void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
543 } 555 }
544 } 556 }
545 } else { 557 } else {
546 D1(printk(KERN_DEBUG "Wasting\n")); 558 D1(printk("Wasting\n"));
547 addedsize = 0; 559 addedsize = 0;
548 jeb->wasted_size += ref_totlen(c, jeb, ref); 560 jeb->wasted_size += freed_len;
549 c->wasted_size += ref_totlen(c, jeb, ref); 561 c->wasted_size += freed_len;
550 } 562 }
551 ref->flash_offset = ref_offset(ref) | REF_OBSOLETE; 563 ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
552 564
@@ -622,7 +634,7 @@ void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
622 /* The erase_free_sem is locked, and has been since before we marked the node obsolete 634 /* The erase_free_sem is locked, and has been since before we marked the node obsolete
623 and potentially put its eraseblock onto the erase_pending_list. Thus, we know that 635 and potentially put its eraseblock onto the erase_pending_list. Thus, we know that
624 the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet 636 the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet
625 by jffs2_free_all_node_refs() in erase.c. Which is nice. */ 637 by jffs2_free_jeb_node_refs() in erase.c. Which is nice. */
626 638
627 D1(printk(KERN_DEBUG "obliterating obsoleted node at 0x%08x\n", ref_offset(ref))); 639 D1(printk(KERN_DEBUG "obliterating obsoleted node at 0x%08x\n", ref_offset(ref)));
628 ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n); 640 ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
@@ -634,8 +646,8 @@ void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
634 printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen); 646 printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
635 goto out_erase_sem; 647 goto out_erase_sem;
636 } 648 }
637 if (PAD(je32_to_cpu(n.totlen)) != PAD(ref_totlen(c, jeb, ref))) { 649 if (PAD(je32_to_cpu(n.totlen)) != PAD(freed_len)) {
638 printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n", je32_to_cpu(n.totlen), ref_totlen(c, jeb, ref)); 650 printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n", je32_to_cpu(n.totlen), freed_len);
639 goto out_erase_sem; 651 goto out_erase_sem;
640 } 652 }
641 if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) { 653 if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
@@ -677,57 +689,23 @@ void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
677 *p = ref->next_in_ino; 689 *p = ref->next_in_ino;
678 ref->next_in_ino = NULL; 690 ref->next_in_ino = NULL;
679 691
680 if (ic->nodes == (void *)ic && ic->nlink == 0) 692 switch (ic->class) {
681 jffs2_del_ino_cache(c, ic); 693#ifdef CONFIG_JFFS2_FS_XATTR
682 694 case RAWNODE_CLASS_XATTR_DATUM:
683 spin_unlock(&c->erase_completion_lock); 695 jffs2_release_xattr_datum(c, (struct jffs2_xattr_datum *)ic);
684 } 696 break;
685 697 case RAWNODE_CLASS_XATTR_REF:
686 698 jffs2_release_xattr_ref(c, (struct jffs2_xattr_ref *)ic);
687 /* Merge with the next node in the physical list, if there is one 699 break;
688 and if it's also obsolete and if it doesn't belong to any inode */ 700#endif
689 if (ref->next_phys && ref_obsolete(ref->next_phys) && 701 default:
690 !ref->next_phys->next_in_ino) { 702 if (ic->nodes == (void *)ic && ic->nlink == 0)
691 struct jffs2_raw_node_ref *n = ref->next_phys; 703 jffs2_del_ino_cache(c, ic);
692 704 break;
693 spin_lock(&c->erase_completion_lock);
694
695 ref->__totlen += n->__totlen;
696 ref->next_phys = n->next_phys;
697 if (jeb->last_node == n) jeb->last_node = ref;
698 if (jeb->gc_node == n) {
699 /* gc will be happy continuing gc on this node */
700 jeb->gc_node=ref;
701 } 705 }
702 spin_unlock(&c->erase_completion_lock); 706 spin_unlock(&c->erase_completion_lock);
703
704 jffs2_free_raw_node_ref(n);
705 } 707 }
706 708
707 /* Also merge with the previous node in the list, if there is one
708 and that one is obsolete */
709 if (ref != jeb->first_node ) {
710 struct jffs2_raw_node_ref *p = jeb->first_node;
711
712 spin_lock(&c->erase_completion_lock);
713
714 while (p->next_phys != ref)
715 p = p->next_phys;
716
717 if (ref_obsolete(p) && !ref->next_in_ino) {
718 p->__totlen += ref->__totlen;
719 if (jeb->last_node == ref) {
720 jeb->last_node = p;
721 }
722 if (jeb->gc_node == ref) {
723 /* gc will be happy continuing gc on this node */
724 jeb->gc_node=p;
725 }
726 p->next_phys = ref->next_phys;
727 jffs2_free_raw_node_ref(ref);
728 }
729 spin_unlock(&c->erase_completion_lock);
730 }
731 out_erase_sem: 709 out_erase_sem:
732 up(&c->erase_free_sem); 710 up(&c->erase_free_sem);
733} 711}
diff --git a/fs/jffs2/os-linux.h b/fs/jffs2/os-linux.h
index d307cf548625..9f41fc01a371 100644
--- a/fs/jffs2/os-linux.h
+++ b/fs/jffs2/os-linux.h
@@ -31,9 +31,7 @@ struct kvec;
31#define JFFS2_F_I_MODE(f) (OFNI_EDONI_2SFFJ(f)->i_mode) 31#define JFFS2_F_I_MODE(f) (OFNI_EDONI_2SFFJ(f)->i_mode)
32#define JFFS2_F_I_UID(f) (OFNI_EDONI_2SFFJ(f)->i_uid) 32#define JFFS2_F_I_UID(f) (OFNI_EDONI_2SFFJ(f)->i_uid)
33#define JFFS2_F_I_GID(f) (OFNI_EDONI_2SFFJ(f)->i_gid) 33#define JFFS2_F_I_GID(f) (OFNI_EDONI_2SFFJ(f)->i_gid)
34 34#define JFFS2_F_I_RDEV(f) (OFNI_EDONI_2SFFJ(f)->i_rdev)
35#define JFFS2_F_I_RDEV_MIN(f) (iminor(OFNI_EDONI_2SFFJ(f)))
36#define JFFS2_F_I_RDEV_MAJ(f) (imajor(OFNI_EDONI_2SFFJ(f)))
37 35
38#define ITIME(sec) ((struct timespec){sec, 0}) 36#define ITIME(sec) ((struct timespec){sec, 0})
39#define I_SEC(tv) ((tv).tv_sec) 37#define I_SEC(tv) ((tv).tv_sec)
@@ -60,6 +58,10 @@ static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
60 f->target = NULL; 58 f->target = NULL;
61 f->flags = 0; 59 f->flags = 0;
62 f->usercompr = 0; 60 f->usercompr = 0;
61#ifdef CONFIG_JFFS2_FS_POSIX_ACL
62 f->i_acl_access = JFFS2_ACL_NOT_CACHED;
63 f->i_acl_default = JFFS2_ACL_NOT_CACHED;
64#endif
63} 65}
64 66
65 67
@@ -90,13 +92,10 @@ static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
90#define jffs2_flash_writev(a,b,c,d,e,f) jffs2_flash_direct_writev(a,b,c,d,e) 92#define jffs2_flash_writev(a,b,c,d,e,f) jffs2_flash_direct_writev(a,b,c,d,e)
91#define jffs2_wbuf_timeout NULL 93#define jffs2_wbuf_timeout NULL
92#define jffs2_wbuf_process NULL 94#define jffs2_wbuf_process NULL
93#define jffs2_nor_ecc(c) (0)
94#define jffs2_dataflash(c) (0) 95#define jffs2_dataflash(c) (0)
95#define jffs2_nor_wbuf_flash(c) (0)
96#define jffs2_nor_ecc_flash_setup(c) (0)
97#define jffs2_nor_ecc_flash_cleanup(c) do {} while (0)
98#define jffs2_dataflash_setup(c) (0) 96#define jffs2_dataflash_setup(c) (0)
99#define jffs2_dataflash_cleanup(c) do {} while (0) 97#define jffs2_dataflash_cleanup(c) do {} while (0)
98#define jffs2_nor_wbuf_flash(c) (0)
100#define jffs2_nor_wbuf_flash_setup(c) (0) 99#define jffs2_nor_wbuf_flash_setup(c) (0)
101#define jffs2_nor_wbuf_flash_cleanup(c) do {} while (0) 100#define jffs2_nor_wbuf_flash_cleanup(c) do {} while (0)
102 101
@@ -107,9 +106,7 @@ static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
107#ifdef CONFIG_JFFS2_SUMMARY 106#ifdef CONFIG_JFFS2_SUMMARY
108#define jffs2_can_mark_obsolete(c) (0) 107#define jffs2_can_mark_obsolete(c) (0)
109#else 108#else
110#define jffs2_can_mark_obsolete(c) \ 109#define jffs2_can_mark_obsolete(c) (c->mtd->flags & (MTD_BIT_WRITEABLE))
111 ((c->mtd->type == MTD_NORFLASH && !(c->mtd->flags & (MTD_ECC|MTD_PROGRAM_REGIONS))) || \
112 c->mtd->type == MTD_RAM)
113#endif 110#endif
114 111
115#define jffs2_cleanmarker_oob(c) (c->mtd->type == MTD_NANDFLASH) 112#define jffs2_cleanmarker_oob(c) (c->mtd->type == MTD_NANDFLASH)
@@ -133,15 +130,11 @@ int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c);
133int jffs2_nand_flash_setup(struct jffs2_sb_info *c); 130int jffs2_nand_flash_setup(struct jffs2_sb_info *c);
134void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c); 131void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c);
135 132
136#define jffs2_nor_ecc(c) (c->mtd->type == MTD_NORFLASH && (c->mtd->flags & MTD_ECC))
137int jffs2_nor_ecc_flash_setup(struct jffs2_sb_info *c);
138void jffs2_nor_ecc_flash_cleanup(struct jffs2_sb_info *c);
139
140#define jffs2_dataflash(c) (c->mtd->type == MTD_DATAFLASH) 133#define jffs2_dataflash(c) (c->mtd->type == MTD_DATAFLASH)
141int jffs2_dataflash_setup(struct jffs2_sb_info *c); 134int jffs2_dataflash_setup(struct jffs2_sb_info *c);
142void jffs2_dataflash_cleanup(struct jffs2_sb_info *c); 135void jffs2_dataflash_cleanup(struct jffs2_sb_info *c);
143 136
144#define jffs2_nor_wbuf_flash(c) (c->mtd->type == MTD_NORFLASH && (c->mtd->flags & MTD_PROGRAM_REGIONS)) 137#define jffs2_nor_wbuf_flash(c) (c->mtd->type == MTD_NORFLASH && ! (c->mtd->flags & MTD_BIT_WRITEABLE))
145int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c); 138int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c);
146void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c); 139void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c);
147 140
@@ -165,7 +158,7 @@ extern struct inode_operations jffs2_dir_inode_operations;
165/* file.c */ 158/* file.c */
166extern const struct file_operations jffs2_file_operations; 159extern const struct file_operations jffs2_file_operations;
167extern struct inode_operations jffs2_file_inode_operations; 160extern struct inode_operations jffs2_file_inode_operations;
168extern struct address_space_operations jffs2_file_address_operations; 161extern const struct address_space_operations jffs2_file_address_operations;
169int jffs2_fsync(struct file *, struct dentry *, int); 162int jffs2_fsync(struct file *, struct dentry *, int);
170int jffs2_do_readpage_unlock (struct inode *inode, struct page *pg); 163int jffs2_do_readpage_unlock (struct inode *inode, struct page *pg);
171 164
@@ -182,7 +175,7 @@ void jffs2_clear_inode (struct inode *);
182void jffs2_dirty_inode(struct inode *inode); 175void jffs2_dirty_inode(struct inode *inode);
183struct inode *jffs2_new_inode (struct inode *dir_i, int mode, 176struct inode *jffs2_new_inode (struct inode *dir_i, int mode,
184 struct jffs2_raw_inode *ri); 177 struct jffs2_raw_inode *ri);
185int jffs2_statfs (struct super_block *, struct kstatfs *); 178int jffs2_statfs (struct dentry *, struct kstatfs *);
186void jffs2_write_super (struct super_block *); 179void jffs2_write_super (struct super_block *);
187int jffs2_remount_fs (struct super_block *, int *, char *); 180int jffs2_remount_fs (struct super_block *, int *, char *);
188int jffs2_do_fill_super(struct super_block *sb, void *data, int silent); 181int jffs2_do_fill_super(struct super_block *sb, void *data, int silent);
diff --git a/fs/jffs2/readinode.c b/fs/jffs2/readinode.c
index f1695642d0f7..cc1899268c43 100644
--- a/fs/jffs2/readinode.c
+++ b/fs/jffs2/readinode.c
@@ -66,7 +66,7 @@ static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
66 jffs2_free_full_dnode(tn->fn); 66 jffs2_free_full_dnode(tn->fn);
67 jffs2_free_tmp_dnode_info(tn); 67 jffs2_free_tmp_dnode_info(tn);
68 68
69 this = this->rb_parent; 69 this = rb_parent(this);
70 if (!this) 70 if (!this)
71 break; 71 break;
72 72
@@ -116,19 +116,42 @@ static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_r
116 uint32_t *latest_mctime, uint32_t *mctime_ver) 116 uint32_t *latest_mctime, uint32_t *mctime_ver)
117{ 117{
118 struct jffs2_full_dirent *fd; 118 struct jffs2_full_dirent *fd;
119 uint32_t crc;
119 120
120 /* The direntry nodes are checked during the flash scanning */
121 BUG_ON(ref_flags(ref) == REF_UNCHECKED);
122 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */ 121 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
123 BUG_ON(ref_obsolete(ref)); 122 BUG_ON(ref_obsolete(ref));
124 123
125 /* Sanity check */ 124 crc = crc32(0, rd, sizeof(*rd) - 8);
126 if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) { 125 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
127 JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n", 126 JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
128 ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen)); 127 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
129 return 1; 128 return 1;
130 } 129 }
131 130
131 /* If we've never checked the CRCs on this node, check them now */
132 if (ref_flags(ref) == REF_UNCHECKED) {
133 struct jffs2_eraseblock *jeb;
134 int len;
135
136 /* Sanity check */
137 if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
138 JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
139 ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
140 return 1;
141 }
142
143 jeb = &c->blocks[ref->flash_offset / c->sector_size];
144 len = ref_totlen(c, jeb, ref);
145
146 spin_lock(&c->erase_completion_lock);
147 jeb->used_size += len;
148 jeb->unchecked_size -= len;
149 c->used_size += len;
150 c->unchecked_size -= len;
151 ref->flash_offset = ref_offset(ref) | REF_PRISTINE;
152 spin_unlock(&c->erase_completion_lock);
153 }
154
132 fd = jffs2_alloc_full_dirent(rd->nsize + 1); 155 fd = jffs2_alloc_full_dirent(rd->nsize + 1);
133 if (unlikely(!fd)) 156 if (unlikely(!fd))
134 return -ENOMEM; 157 return -ENOMEM;
@@ -198,13 +221,21 @@ static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
198 struct jffs2_tmp_dnode_info *tn; 221 struct jffs2_tmp_dnode_info *tn;
199 uint32_t len, csize; 222 uint32_t len, csize;
200 int ret = 1; 223 int ret = 1;
224 uint32_t crc;
201 225
202 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */ 226 /* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
203 BUG_ON(ref_obsolete(ref)); 227 BUG_ON(ref_obsolete(ref));
204 228
229 crc = crc32(0, rd, sizeof(*rd) - 8);
230 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
231 JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
232 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
233 return 1;
234 }
235
205 tn = jffs2_alloc_tmp_dnode_info(); 236 tn = jffs2_alloc_tmp_dnode_info();
206 if (!tn) { 237 if (!tn) {
207 JFFS2_ERROR("failed to allocate tn (%d bytes).\n", sizeof(*tn)); 238 JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
208 return -ENOMEM; 239 return -ENOMEM;
209 } 240 }
210 241
@@ -213,14 +244,6 @@ static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref
213 244
214 /* If we've never checked the CRCs on this node, check them now */ 245 /* If we've never checked the CRCs on this node, check them now */
215 if (ref_flags(ref) == REF_UNCHECKED) { 246 if (ref_flags(ref) == REF_UNCHECKED) {
216 uint32_t crc;
217
218 crc = crc32(0, rd, sizeof(*rd) - 8);
219 if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
220 JFFS2_NOTICE("header CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
221 ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
222 goto free_out;
223 }
224 247
225 /* Sanity checks */ 248 /* Sanity checks */
226 if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) || 249 if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
@@ -343,7 +366,7 @@ free_out:
343 * Helper function for jffs2_get_inode_nodes(). 366 * Helper function for jffs2_get_inode_nodes().
344 * It is called every time an unknown node is found. 367 * It is called every time an unknown node is found.
345 * 368 *
346 * Returns: 0 on succes; 369 * Returns: 0 on success;
347 * 1 if the node should be marked obsolete; 370 * 1 if the node should be marked obsolete;
348 * negative error code on failure. 371 * negative error code on failure.
349 */ 372 */
@@ -354,37 +377,30 @@ static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_re
354 377
355 un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype)); 378 un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
356 379
357 if (crc32(0, un, sizeof(struct jffs2_unknown_node) - 4) != je32_to_cpu(un->hdr_crc)) { 380 switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
358 /* Hmmm. This should have been caught at scan time. */
359 JFFS2_NOTICE("node header CRC failed at %#08x. But it must have been OK earlier.\n", ref_offset(ref));
360 jffs2_dbg_dump_node(c, ref_offset(ref));
361 return 1;
362 } else {
363 switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
364 381
365 case JFFS2_FEATURE_INCOMPAT: 382 case JFFS2_FEATURE_INCOMPAT:
366 JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n", 383 JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
367 je16_to_cpu(un->nodetype), ref_offset(ref)); 384 je16_to_cpu(un->nodetype), ref_offset(ref));
368 /* EEP */ 385 /* EEP */
369 BUG(); 386 BUG();
370 break; 387 break;
371 388
372 case JFFS2_FEATURE_ROCOMPAT: 389 case JFFS2_FEATURE_ROCOMPAT:
373 JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n", 390 JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
374 je16_to_cpu(un->nodetype), ref_offset(ref)); 391 je16_to_cpu(un->nodetype), ref_offset(ref));
375 BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO)); 392 BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
376 break; 393 break;
377 394
378 case JFFS2_FEATURE_RWCOMPAT_COPY: 395 case JFFS2_FEATURE_RWCOMPAT_COPY:
379 JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n", 396 JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
380 je16_to_cpu(un->nodetype), ref_offset(ref)); 397 je16_to_cpu(un->nodetype), ref_offset(ref));
381 break; 398 break;
382 399
383 case JFFS2_FEATURE_RWCOMPAT_DELETE: 400 case JFFS2_FEATURE_RWCOMPAT_DELETE:
384 JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n", 401 JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
385 je16_to_cpu(un->nodetype), ref_offset(ref)); 402 je16_to_cpu(un->nodetype), ref_offset(ref));
386 return 1; 403 return 1;
387 }
388 } 404 }
389 405
390 return 0; 406 return 0;
@@ -434,7 +450,7 @@ static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
434 } 450 }
435 451
436 if (retlen < len) { 452 if (retlen < len) {
437 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", 453 JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
438 offs, retlen, len); 454 offs, retlen, len);
439 return -EIO; 455 return -EIO;
440 } 456 }
@@ -542,13 +558,25 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
542 } 558 }
543 559
544 if (retlen < len) { 560 if (retlen < len) {
545 JFFS2_ERROR("short read at %#08x: %d instead of %d.\n", ref_offset(ref), retlen, len); 561 JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
546 err = -EIO; 562 err = -EIO;
547 goto free_out; 563 goto free_out;
548 } 564 }
549 565
550 node = (union jffs2_node_union *)bufstart; 566 node = (union jffs2_node_union *)bufstart;
551 567
568 /* No need to mask in the valid bit; it shouldn't be invalid */
569 if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
570 JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
571 ref_offset(ref), je16_to_cpu(node->u.magic),
572 je16_to_cpu(node->u.nodetype),
573 je32_to_cpu(node->u.totlen),
574 je32_to_cpu(node->u.hdr_crc));
575 jffs2_dbg_dump_node(c, ref_offset(ref));
576 jffs2_mark_node_obsolete(c, ref);
577 goto cont;
578 }
579
552 switch (je16_to_cpu(node->u.nodetype)) { 580 switch (je16_to_cpu(node->u.nodetype)) {
553 581
554 case JFFS2_NODETYPE_DIRENT: 582 case JFFS2_NODETYPE_DIRENT:
@@ -606,6 +634,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
606 goto free_out; 634 goto free_out;
607 635
608 } 636 }
637 cont:
609 spin_lock(&c->erase_completion_lock); 638 spin_lock(&c->erase_completion_lock);
610 } 639 }
611 640
@@ -679,12 +708,12 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
679 jffs2_mark_node_obsolete(c, fn->raw); 708 jffs2_mark_node_obsolete(c, fn->raw);
680 709
681 BUG_ON(rb->rb_left); 710 BUG_ON(rb->rb_left);
682 if (rb->rb_parent && rb->rb_parent->rb_left == rb) { 711 if (rb_parent(rb) && rb_parent(rb)->rb_left == rb) {
683 /* We were then left-hand child of our parent. We need 712 /* We were then left-hand child of our parent. We need
684 * to move our own right-hand child into our place. */ 713 * to move our own right-hand child into our place. */
685 repl_rb = rb->rb_right; 714 repl_rb = rb->rb_right;
686 if (repl_rb) 715 if (repl_rb)
687 repl_rb->rb_parent = rb->rb_parent; 716 rb_set_parent(repl_rb, rb_parent(rb));
688 } else 717 } else
689 repl_rb = NULL; 718 repl_rb = NULL;
690 719
@@ -692,14 +721,14 @@ static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
692 721
693 /* Remove the spent tn from the tree; don't bother rebalancing 722 /* Remove the spent tn from the tree; don't bother rebalancing
694 * but put our right-hand child in our own place. */ 723 * but put our right-hand child in our own place. */
695 if (tn->rb.rb_parent) { 724 if (rb_parent(&tn->rb)) {
696 if (tn->rb.rb_parent->rb_left == &tn->rb) 725 if (rb_parent(&tn->rb)->rb_left == &tn->rb)
697 tn->rb.rb_parent->rb_left = repl_rb; 726 rb_parent(&tn->rb)->rb_left = repl_rb;
698 else if (tn->rb.rb_parent->rb_right == &tn->rb) 727 else if (rb_parent(&tn->rb)->rb_right == &tn->rb)
699 tn->rb.rb_parent->rb_right = repl_rb; 728 rb_parent(&tn->rb)->rb_right = repl_rb;
700 else BUG(); 729 else BUG();
701 } else if (tn->rb.rb_right) 730 } else if (tn->rb.rb_right)
702 tn->rb.rb_right->rb_parent = NULL; 731 rb_set_parent(tn->rb.rb_right, NULL);
703 732
704 jffs2_free_tmp_dnode_info(tn); 733 jffs2_free_tmp_dnode_info(tn);
705 if (ret) { 734 if (ret) {
@@ -939,6 +968,7 @@ void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
939 struct jffs2_full_dirent *fd, *fds; 968 struct jffs2_full_dirent *fd, *fds;
940 int deleted; 969 int deleted;
941 970
971 jffs2_xattr_delete_inode(c, f->inocache);
942 down(&f->sem); 972 down(&f->sem);
943 deleted = f->inocache && !f->inocache->nlink; 973 deleted = f->inocache && !f->inocache->nlink;
944 974
diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
index cf55b221fc2b..2bfdc33752d3 100644
--- a/fs/jffs2/scan.c
+++ b/fs/jffs2/scan.c
@@ -65,6 +65,28 @@ static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) {
65 return DEFAULT_EMPTY_SCAN_SIZE; 65 return DEFAULT_EMPTY_SCAN_SIZE;
66} 66}
67 67
68static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
69{
70 int ret;
71
72 if ((ret = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
73 return ret;
74 if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
75 return ret;
76 /* Turned wasted size into dirty, since we apparently
77 think it's recoverable now. */
78 jeb->dirty_size += jeb->wasted_size;
79 c->dirty_size += jeb->wasted_size;
80 c->wasted_size -= jeb->wasted_size;
81 jeb->wasted_size = 0;
82 if (VERYDIRTY(c, jeb->dirty_size)) {
83 list_add(&jeb->list, &c->very_dirty_list);
84 } else {
85 list_add(&jeb->list, &c->dirty_list);
86 }
87 return 0;
88}
89
68int jffs2_scan_medium(struct jffs2_sb_info *c) 90int jffs2_scan_medium(struct jffs2_sb_info *c)
69{ 91{
70 int i, ret; 92 int i, ret;
@@ -170,34 +192,20 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
170 (!c->nextblock || c->nextblock->free_size < jeb->free_size)) { 192 (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
171 /* Better candidate for the next writes to go to */ 193 /* Better candidate for the next writes to go to */
172 if (c->nextblock) { 194 if (c->nextblock) {
173 c->nextblock->dirty_size += c->nextblock->free_size + c->nextblock->wasted_size; 195 ret = file_dirty(c, c->nextblock);
174 c->dirty_size += c->nextblock->free_size + c->nextblock->wasted_size; 196 if (ret)
175 c->free_size -= c->nextblock->free_size; 197 return ret;
176 c->wasted_size -= c->nextblock->wasted_size;
177 c->nextblock->free_size = c->nextblock->wasted_size = 0;
178 if (VERYDIRTY(c, c->nextblock->dirty_size)) {
179 list_add(&c->nextblock->list, &c->very_dirty_list);
180 } else {
181 list_add(&c->nextblock->list, &c->dirty_list);
182 }
183 /* deleting summary information of the old nextblock */ 198 /* deleting summary information of the old nextblock */
184 jffs2_sum_reset_collected(c->summary); 199 jffs2_sum_reset_collected(c->summary);
185 } 200 }
186 /* update collected summary infromation for the current nextblock */ 201 /* update collected summary information for the current nextblock */
187 jffs2_sum_move_collected(c, s); 202 jffs2_sum_move_collected(c, s);
188 D1(printk(KERN_DEBUG "jffs2_scan_medium(): new nextblock = 0x%08x\n", jeb->offset)); 203 D1(printk(KERN_DEBUG "jffs2_scan_medium(): new nextblock = 0x%08x\n", jeb->offset));
189 c->nextblock = jeb; 204 c->nextblock = jeb;
190 } else { 205 } else {
191 jeb->dirty_size += jeb->free_size + jeb->wasted_size; 206 ret = file_dirty(c, jeb);
192 c->dirty_size += jeb->free_size + jeb->wasted_size; 207 if (ret)
193 c->free_size -= jeb->free_size; 208 return ret;
194 c->wasted_size -= jeb->wasted_size;
195 jeb->free_size = jeb->wasted_size = 0;
196 if (VERYDIRTY(c, jeb->dirty_size)) {
197 list_add(&jeb->list, &c->very_dirty_list);
198 } else {
199 list_add(&jeb->list, &c->dirty_list);
200 }
201 } 209 }
202 break; 210 break;
203 211
@@ -222,9 +230,6 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
222 } 230 }
223 } 231 }
224 232
225 if (jffs2_sum_active() && s)
226 kfree(s);
227
228 /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */ 233 /* Nextblock dirty is always seen as wasted, because we cannot recycle it now */
229 if (c->nextblock && (c->nextblock->dirty_size)) { 234 if (c->nextblock && (c->nextblock->dirty_size)) {
230 c->nextblock->wasted_size += c->nextblock->dirty_size; 235 c->nextblock->wasted_size += c->nextblock->dirty_size;
@@ -242,11 +247,8 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
242 247
243 D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n", 248 D1(printk(KERN_DEBUG "jffs2_scan_medium(): Skipping %d bytes in nextblock to ensure page alignment\n",
244 skip)); 249 skip));
245 c->nextblock->wasted_size += skip; 250 jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
246 c->wasted_size += skip; 251 jffs2_scan_dirty_space(c, c->nextblock, skip);
247
248 c->nextblock->free_size -= skip;
249 c->free_size -= skip;
250 } 252 }
251#endif 253#endif
252 if (c->nr_erasing_blocks) { 254 if (c->nr_erasing_blocks) {
@@ -266,6 +268,9 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
266 else 268 else
267 c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size); 269 c->mtd->unpoint(c->mtd, flashbuf, 0, c->mtd->size);
268#endif 270#endif
271 if (s)
272 kfree(s);
273
269 return ret; 274 return ret;
270} 275}
271 276
@@ -290,7 +295,7 @@ int jffs2_fill_scan_buf (struct jffs2_sb_info *c, void *buf,
290int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 295int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
291{ 296{
292 if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size 297 if ((jeb->used_size + jeb->unchecked_size) == PAD(c->cleanmarker_size) && !jeb->dirty_size
293 && (!jeb->first_node || !jeb->first_node->next_phys) ) 298 && (!jeb->first_node || !ref_next(jeb->first_node)) )
294 return BLK_STATE_CLEANMARKER; 299 return BLK_STATE_CLEANMARKER;
295 300
296 /* move blocks with max 4 byte dirty space to cleanlist */ 301 /* move blocks with max 4 byte dirty space to cleanlist */
@@ -306,11 +311,126 @@ int jffs2_scan_classify_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *je
306 return BLK_STATE_ALLDIRTY; 311 return BLK_STATE_ALLDIRTY;
307} 312}
308 313
314#ifdef CONFIG_JFFS2_FS_XATTR
315static int jffs2_scan_xattr_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
316 struct jffs2_raw_xattr *rx, uint32_t ofs,
317 struct jffs2_summary *s)
318{
319 struct jffs2_xattr_datum *xd;
320 uint32_t xid, version, totlen, crc;
321 int err;
322
323 crc = crc32(0, rx, sizeof(struct jffs2_raw_xattr) - 4);
324 if (crc != je32_to_cpu(rx->node_crc)) {
325 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
326 ofs, je32_to_cpu(rx->node_crc), crc);
327 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
328 return err;
329 return 0;
330 }
331
332 xid = je32_to_cpu(rx->xid);
333 version = je32_to_cpu(rx->version);
334
335 totlen = PAD(sizeof(struct jffs2_raw_xattr)
336 + rx->name_len + 1 + je16_to_cpu(rx->value_len));
337 if (totlen != je32_to_cpu(rx->totlen)) {
338 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%u\n",
339 ofs, je32_to_cpu(rx->totlen), totlen);
340 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rx->totlen))))
341 return err;
342 return 0;
343 }
344
345 xd = jffs2_setup_xattr_datum(c, xid, version);
346 if (IS_ERR(xd))
347 return PTR_ERR(xd);
348
349 if (xd->version > version) {
350 struct jffs2_raw_node_ref *raw
351 = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, NULL);
352 raw->next_in_ino = xd->node->next_in_ino;
353 xd->node->next_in_ino = raw;
354 } else {
355 xd->version = version;
356 xd->xprefix = rx->xprefix;
357 xd->name_len = rx->name_len;
358 xd->value_len = je16_to_cpu(rx->value_len);
359 xd->data_crc = je32_to_cpu(rx->data_crc);
360
361 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, totlen, (void *)xd);
362 }
363
364 if (jffs2_sum_active())
365 jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
366 dbg_xattr("scaning xdatum at %#08x (xid=%u, version=%u)\n",
367 ofs, xd->xid, xd->version);
368 return 0;
369}
370
371static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
372 struct jffs2_raw_xref *rr, uint32_t ofs,
373 struct jffs2_summary *s)
374{
375 struct jffs2_xattr_ref *ref;
376 uint32_t crc;
377 int err;
378
379 crc = crc32(0, rr, sizeof(*rr) - 4);
380 if (crc != je32_to_cpu(rr->node_crc)) {
381 JFFS2_WARNING("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
382 ofs, je32_to_cpu(rr->node_crc), crc);
383 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rr->totlen)))))
384 return err;
385 return 0;
386 }
387
388 if (PAD(sizeof(struct jffs2_raw_xref)) != je32_to_cpu(rr->totlen)) {
389 JFFS2_WARNING("node length mismatch at %#08x, read=%u, calc=%zd\n",
390 ofs, je32_to_cpu(rr->totlen),
391 PAD(sizeof(struct jffs2_raw_xref)));
392 if ((err = jffs2_scan_dirty_space(c, jeb, je32_to_cpu(rr->totlen))))
393 return err;
394 return 0;
395 }
396
397 ref = jffs2_alloc_xattr_ref();
398 if (!ref)
399 return -ENOMEM;
400
401 /* BEFORE jffs2_build_xattr_subsystem() called,
402 * and AFTER xattr_ref is marked as a dead xref,
403 * ref->xid is used to store 32bit xid, xd is not used
404 * ref->ino is used to store 32bit inode-number, ic is not used
405 * Thoes variables are declared as union, thus using those
406 * are exclusive. In a similar way, ref->next is temporarily
407 * used to chain all xattr_ref object. It's re-chained to
408 * jffs2_inode_cache in jffs2_build_xattr_subsystem() correctly.
409 */
410 ref->ino = je32_to_cpu(rr->ino);
411 ref->xid = je32_to_cpu(rr->xid);
412 ref->xseqno = je32_to_cpu(rr->xseqno);
413 if (ref->xseqno > c->highest_xseqno)
414 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
415 ref->next = c->xref_temp;
416 c->xref_temp = ref;
417
418 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rr->totlen)), (void *)ref);
419
420 if (jffs2_sum_active())
421 jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
422 dbg_xattr("scan xref at %#08x (xid=%u, ino=%u)\n",
423 ofs, ref->xid, ref->ino);
424 return 0;
425}
426#endif
427
428/* Called with 'buf_size == 0' if buf is in fact a pointer _directly_ into
429 the flash, XIP-style */
309static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 430static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
310 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) { 431 unsigned char *buf, uint32_t buf_size, struct jffs2_summary *s) {
311 struct jffs2_unknown_node *node; 432 struct jffs2_unknown_node *node;
312 struct jffs2_unknown_node crcnode; 433 struct jffs2_unknown_node crcnode;
313 struct jffs2_sum_marker *sm;
314 uint32_t ofs, prevofs; 434 uint32_t ofs, prevofs;
315 uint32_t hdr_crc, buf_ofs, buf_len; 435 uint32_t hdr_crc, buf_ofs, buf_len;
316 int err; 436 int err;
@@ -344,44 +464,75 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
344#endif 464#endif
345 465
346 if (jffs2_sum_active()) { 466 if (jffs2_sum_active()) {
347 sm = kmalloc(sizeof(struct jffs2_sum_marker), GFP_KERNEL); 467 struct jffs2_sum_marker *sm;
348 if (!sm) { 468 void *sumptr = NULL;
349 return -ENOMEM; 469 uint32_t sumlen;
350 } 470
351 471 if (!buf_size) {
352 err = jffs2_fill_scan_buf(c, (unsigned char *) sm, jeb->offset + c->sector_size - 472 /* XIP case. Just look, point at the summary if it's there */
353 sizeof(struct jffs2_sum_marker), sizeof(struct jffs2_sum_marker)); 473 sm = (void *)buf + c->sector_size - sizeof(*sm);
354 if (err) { 474 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
355 kfree(sm); 475 sumptr = buf + je32_to_cpu(sm->offset);
356 return err; 476 sumlen = c->sector_size - je32_to_cpu(sm->offset);
357 } 477 }
358 478 } else {
359 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC ) { 479 /* If NAND flash, read a whole page of it. Else just the end */
360 err = jffs2_sum_scan_sumnode(c, jeb, je32_to_cpu(sm->offset), &pseudo_random); 480 if (c->wbuf_pagesize)
361 if (err) { 481 buf_len = c->wbuf_pagesize;
362 kfree(sm); 482 else
483 buf_len = sizeof(*sm);
484
485 /* Read as much as we want into the _end_ of the preallocated buffer */
486 err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
487 jeb->offset + c->sector_size - buf_len,
488 buf_len);
489 if (err)
363 return err; 490 return err;
491
492 sm = (void *)buf + buf_size - sizeof(*sm);
493 if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
494 sumlen = c->sector_size - je32_to_cpu(sm->offset);
495 sumptr = buf + buf_size - sumlen;
496
497 /* Now, make sure the summary itself is available */
498 if (sumlen > buf_size) {
499 /* Need to kmalloc for this. */
500 sumptr = kmalloc(sumlen, GFP_KERNEL);
501 if (!sumptr)
502 return -ENOMEM;
503 memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
504 }
505 if (buf_len < sumlen) {
506 /* Need to read more so that the entire summary node is present */
507 err = jffs2_fill_scan_buf(c, sumptr,
508 jeb->offset + c->sector_size - sumlen,
509 sumlen - buf_len);
510 if (err)
511 return err;
512 }
364 } 513 }
514
365 } 515 }
366 516
367 kfree(sm); 517 if (sumptr) {
518 err = jffs2_sum_scan_sumnode(c, jeb, sumptr, sumlen, &pseudo_random);
368 519
369 ofs = jeb->offset; 520 if (buf_size && sumlen > buf_size)
370 prevofs = jeb->offset - 1; 521 kfree(sumptr);
522 /* If it returns with a real error, bail.
523 If it returns positive, that's a block classification
524 (i.e. BLK_STATE_xxx) so return that too.
525 If it returns zero, fall through to full scan. */
526 if (err)
527 return err;
528 }
371 } 529 }
372 530
373 buf_ofs = jeb->offset; 531 buf_ofs = jeb->offset;
374 532
375 if (!buf_size) { 533 if (!buf_size) {
534 /* This is the XIP case -- we're reading _directly_ from the flash chip */
376 buf_len = c->sector_size; 535 buf_len = c->sector_size;
377
378 if (jffs2_sum_active()) {
379 /* must reread because of summary test */
380 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
381 if (err)
382 return err;
383 }
384
385 } else { 536 } else {
386 buf_len = EMPTY_SCAN_SIZE(c->sector_size); 537 buf_len = EMPTY_SCAN_SIZE(c->sector_size);
387 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len); 538 err = jffs2_fill_scan_buf(c, buf, buf_ofs, buf_len);
@@ -418,7 +569,10 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
418 if (ofs) { 569 if (ofs) {
419 D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset, 570 D1(printk(KERN_DEBUG "Free space at %08x ends at %08x\n", jeb->offset,
420 jeb->offset + ofs)); 571 jeb->offset + ofs));
421 DIRTY_SPACE(ofs); 572 if ((err = jffs2_prealloc_raw_node_refs(c, jeb, 1)))
573 return err;
574 if ((err = jffs2_scan_dirty_space(c, jeb, ofs)))
575 return err;
422 } 576 }
423 577
424 /* Now ofs is a complete physical flash offset as it always was... */ 578 /* Now ofs is a complete physical flash offset as it always was... */
@@ -433,6 +587,11 @@ scan_more:
433 587
434 jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 588 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
435 589
590 /* Make sure there are node refs available for use */
591 err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
592 if (err)
593 return err;
594
436 cond_resched(); 595 cond_resched();
437 596
438 if (ofs & 3) { 597 if (ofs & 3) {
@@ -442,7 +601,8 @@ scan_more:
442 } 601 }
443 if (ofs == prevofs) { 602 if (ofs == prevofs) {
444 printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs); 603 printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
445 DIRTY_SPACE(4); 604 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
605 return err;
446 ofs += 4; 606 ofs += 4;
447 continue; 607 continue;
448 } 608 }
@@ -451,7 +611,8 @@ scan_more:
451 if (jeb->offset + c->sector_size < ofs + sizeof(*node)) { 611 if (jeb->offset + c->sector_size < ofs + sizeof(*node)) {
452 D1(printk(KERN_DEBUG "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n", sizeof(struct jffs2_unknown_node), 612 D1(printk(KERN_DEBUG "Fewer than %zd bytes left to end of block. (%x+%x<%x+%zx) Not reading\n", sizeof(struct jffs2_unknown_node),
453 jeb->offset, c->sector_size, ofs, sizeof(*node))); 613 jeb->offset, c->sector_size, ofs, sizeof(*node)));
454 DIRTY_SPACE((jeb->offset + c->sector_size)-ofs); 614 if ((err = jffs2_scan_dirty_space(c, jeb, (jeb->offset + c->sector_size)-ofs)))
615 return err;
455 break; 616 break;
456 } 617 }
457 618
@@ -481,7 +642,8 @@ scan_more:
481 if (*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff) { 642 if (*(uint32_t *)(&buf[inbuf_ofs]) != 0xffffffff) {
482 printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n", 643 printk(KERN_WARNING "Empty flash at 0x%08x ends at 0x%08x\n",
483 empty_start, ofs); 644 empty_start, ofs);
484 DIRTY_SPACE(ofs-empty_start); 645 if ((err = jffs2_scan_dirty_space(c, jeb, ofs-empty_start)))
646 return err;
485 goto scan_more; 647 goto scan_more;
486 } 648 }
487 649
@@ -494,7 +656,7 @@ scan_more:
494 /* If we're only checking the beginning of a block with a cleanmarker, 656 /* If we're only checking the beginning of a block with a cleanmarker,
495 bail now */ 657 bail now */
496 if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) && 658 if (buf_ofs == jeb->offset && jeb->used_size == PAD(c->cleanmarker_size) &&
497 c->cleanmarker_size && !jeb->dirty_size && !jeb->first_node->next_phys) { 659 c->cleanmarker_size && !jeb->dirty_size && !ref_next(jeb->first_node)) {
498 D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size))); 660 D1(printk(KERN_DEBUG "%d bytes at start of block seems clean... assuming all clean\n", EMPTY_SCAN_SIZE(c->sector_size)));
499 return BLK_STATE_CLEANMARKER; 661 return BLK_STATE_CLEANMARKER;
500 } 662 }
@@ -518,20 +680,23 @@ scan_more:
518 680
519 if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) { 681 if (ofs == jeb->offset && je16_to_cpu(node->magic) == KSAMTIB_CIGAM_2SFFJ) {
520 printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs); 682 printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
521 DIRTY_SPACE(4); 683 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
684 return err;
522 ofs += 4; 685 ofs += 4;
523 continue; 686 continue;
524 } 687 }
525 if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) { 688 if (je16_to_cpu(node->magic) == JFFS2_DIRTY_BITMASK) {
526 D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs)); 689 D1(printk(KERN_DEBUG "Dirty bitmask at 0x%08x\n", ofs));
527 DIRTY_SPACE(4); 690 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
691 return err;
528 ofs += 4; 692 ofs += 4;
529 continue; 693 continue;
530 } 694 }
531 if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) { 695 if (je16_to_cpu(node->magic) == JFFS2_OLD_MAGIC_BITMASK) {
532 printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs); 696 printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
533 printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n"); 697 printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
534 DIRTY_SPACE(4); 698 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
699 return err;
535 ofs += 4; 700 ofs += 4;
536 continue; 701 continue;
537 } 702 }
@@ -540,7 +705,8 @@ scan_more:
540 noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n", 705 noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n",
541 JFFS2_MAGIC_BITMASK, ofs, 706 JFFS2_MAGIC_BITMASK, ofs,
542 je16_to_cpu(node->magic)); 707 je16_to_cpu(node->magic));
543 DIRTY_SPACE(4); 708 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
709 return err;
544 ofs += 4; 710 ofs += 4;
545 continue; 711 continue;
546 } 712 }
@@ -557,7 +723,8 @@ scan_more:
557 je32_to_cpu(node->totlen), 723 je32_to_cpu(node->totlen),
558 je32_to_cpu(node->hdr_crc), 724 je32_to_cpu(node->hdr_crc),
559 hdr_crc); 725 hdr_crc);
560 DIRTY_SPACE(4); 726 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
727 return err;
561 ofs += 4; 728 ofs += 4;
562 continue; 729 continue;
563 } 730 }
@@ -568,7 +735,8 @@ scan_more:
568 printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n", 735 printk(KERN_WARNING "Node at 0x%08x with length 0x%08x would run over the end of the erase block\n",
569 ofs, je32_to_cpu(node->totlen)); 736 ofs, je32_to_cpu(node->totlen));
570 printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n"); 737 printk(KERN_WARNING "Perhaps the file system was created with the wrong erase size?\n");
571 DIRTY_SPACE(4); 738 if ((err = jffs2_scan_dirty_space(c, jeb, 4)))
739 return err;
572 ofs += 4; 740 ofs += 4;
573 continue; 741 continue;
574 } 742 }
@@ -576,7 +744,8 @@ scan_more:
576 if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) { 744 if (!(je16_to_cpu(node->nodetype) & JFFS2_NODE_ACCURATE)) {
577 /* Wheee. This is an obsoleted node */ 745 /* Wheee. This is an obsoleted node */
578 D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs)); 746 D2(printk(KERN_DEBUG "Node at 0x%08x is obsolete. Skipping\n", ofs));
579 DIRTY_SPACE(PAD(je32_to_cpu(node->totlen))); 747 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
748 return err;
580 ofs += PAD(je32_to_cpu(node->totlen)); 749 ofs += PAD(je32_to_cpu(node->totlen));
581 continue; 750 continue;
582 } 751 }
@@ -614,30 +783,59 @@ scan_more:
614 ofs += PAD(je32_to_cpu(node->totlen)); 783 ofs += PAD(je32_to_cpu(node->totlen));
615 break; 784 break;
616 785
786#ifdef CONFIG_JFFS2_FS_XATTR
787 case JFFS2_NODETYPE_XATTR:
788 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
789 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
790 D1(printk(KERN_DEBUG "Fewer than %d bytes (xattr node)"
791 " left to end of buf. Reading 0x%x at 0x%08x\n",
792 je32_to_cpu(node->totlen), buf_len, ofs));
793 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
794 if (err)
795 return err;
796 buf_ofs = ofs;
797 node = (void *)buf;
798 }
799 err = jffs2_scan_xattr_node(c, jeb, (void *)node, ofs, s);
800 if (err)
801 return err;
802 ofs += PAD(je32_to_cpu(node->totlen));
803 break;
804 case JFFS2_NODETYPE_XREF:
805 if (buf_ofs + buf_len < ofs + je32_to_cpu(node->totlen)) {
806 buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
807 D1(printk(KERN_DEBUG "Fewer than %d bytes (xref node)"
808 " left to end of buf. Reading 0x%x at 0x%08x\n",
809 je32_to_cpu(node->totlen), buf_len, ofs));
810 err = jffs2_fill_scan_buf(c, buf, ofs, buf_len);
811 if (err)
812 return err;
813 buf_ofs = ofs;
814 node = (void *)buf;
815 }
816 err = jffs2_scan_xref_node(c, jeb, (void *)node, ofs, s);
817 if (err)
818 return err;
819 ofs += PAD(je32_to_cpu(node->totlen));
820 break;
821#endif /* CONFIG_JFFS2_FS_XATTR */
822
617 case JFFS2_NODETYPE_CLEANMARKER: 823 case JFFS2_NODETYPE_CLEANMARKER:
618 D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs)); 824 D1(printk(KERN_DEBUG "CLEANMARKER node found at 0x%08x\n", ofs));
619 if (je32_to_cpu(node->totlen) != c->cleanmarker_size) { 825 if (je32_to_cpu(node->totlen) != c->cleanmarker_size) {
620 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n", 826 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n",
621 ofs, je32_to_cpu(node->totlen), c->cleanmarker_size); 827 ofs, je32_to_cpu(node->totlen), c->cleanmarker_size);
622 DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node))); 828 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
829 return err;
623 ofs += PAD(sizeof(struct jffs2_unknown_node)); 830 ofs += PAD(sizeof(struct jffs2_unknown_node));
624 } else if (jeb->first_node) { 831 } else if (jeb->first_node) {
625 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset); 832 printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
626 DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node))); 833 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(sizeof(struct jffs2_unknown_node)))))
834 return err;
627 ofs += PAD(sizeof(struct jffs2_unknown_node)); 835 ofs += PAD(sizeof(struct jffs2_unknown_node));
628 } else { 836 } else {
629 struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref(); 837 jffs2_link_node_ref(c, jeb, ofs | REF_NORMAL, c->cleanmarker_size, NULL);
630 if (!marker_ref) {
631 printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
632 return -ENOMEM;
633 }
634 marker_ref->next_in_ino = NULL;
635 marker_ref->next_phys = NULL;
636 marker_ref->flash_offset = ofs | REF_NORMAL;
637 marker_ref->__totlen = c->cleanmarker_size;
638 jeb->first_node = jeb->last_node = marker_ref;
639 838
640 USED_SPACE(PAD(c->cleanmarker_size));
641 ofs += PAD(c->cleanmarker_size); 839 ofs += PAD(c->cleanmarker_size);
642 } 840 }
643 break; 841 break;
@@ -645,7 +843,8 @@ scan_more:
645 case JFFS2_NODETYPE_PADDING: 843 case JFFS2_NODETYPE_PADDING:
646 if (jffs2_sum_active()) 844 if (jffs2_sum_active())
647 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen)); 845 jffs2_sum_add_padding_mem(s, je32_to_cpu(node->totlen));
648 DIRTY_SPACE(PAD(je32_to_cpu(node->totlen))); 846 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
847 return err;
649 ofs += PAD(je32_to_cpu(node->totlen)); 848 ofs += PAD(je32_to_cpu(node->totlen));
650 break; 849 break;
651 850
@@ -656,7 +855,8 @@ scan_more:
656 c->flags |= JFFS2_SB_FLAG_RO; 855 c->flags |= JFFS2_SB_FLAG_RO;
657 if (!(jffs2_is_readonly(c))) 856 if (!(jffs2_is_readonly(c)))
658 return -EROFS; 857 return -EROFS;
659 DIRTY_SPACE(PAD(je32_to_cpu(node->totlen))); 858 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
859 return err;
660 ofs += PAD(je32_to_cpu(node->totlen)); 860 ofs += PAD(je32_to_cpu(node->totlen));
661 break; 861 break;
662 862
@@ -666,15 +866,21 @@ scan_more:
666 866
667 case JFFS2_FEATURE_RWCOMPAT_DELETE: 867 case JFFS2_FEATURE_RWCOMPAT_DELETE:
668 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs)); 868 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
669 DIRTY_SPACE(PAD(je32_to_cpu(node->totlen))); 869 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(node->totlen)))))
870 return err;
670 ofs += PAD(je32_to_cpu(node->totlen)); 871 ofs += PAD(je32_to_cpu(node->totlen));
671 break; 872 break;
672 873
673 case JFFS2_FEATURE_RWCOMPAT_COPY: 874 case JFFS2_FEATURE_RWCOMPAT_COPY: {
674 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs)); 875 D1(printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", je16_to_cpu(node->nodetype), ofs));
675 USED_SPACE(PAD(je32_to_cpu(node->totlen))); 876
877 jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(node->totlen)), NULL);
878
879 /* We can't summarise nodes we don't grok */
880 jffs2_sum_disable_collecting(s);
676 ofs += PAD(je32_to_cpu(node->totlen)); 881 ofs += PAD(je32_to_cpu(node->totlen));
677 break; 882 break;
883 }
678 } 884 }
679 } 885 }
680 } 886 }
@@ -687,9 +893,9 @@ scan_more:
687 } 893 }
688 } 894 }
689 895
690 D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x\n", jeb->offset, 896 D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
691 jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size)); 897 jeb->offset,jeb->free_size, jeb->dirty_size, jeb->unchecked_size, jeb->used_size, jeb->wasted_size));
692 898
693 /* mark_node_obsolete can add to wasted !! */ 899 /* mark_node_obsolete can add to wasted !! */
694 if (jeb->wasted_size) { 900 if (jeb->wasted_size) {
695 jeb->dirty_size += jeb->wasted_size; 901 jeb->dirty_size += jeb->wasted_size;
@@ -730,9 +936,9 @@ struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uin
730static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 936static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
731 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s) 937 struct jffs2_raw_inode *ri, uint32_t ofs, struct jffs2_summary *s)
732{ 938{
733 struct jffs2_raw_node_ref *raw;
734 struct jffs2_inode_cache *ic; 939 struct jffs2_inode_cache *ic;
735 uint32_t ino = je32_to_cpu(ri->ino); 940 uint32_t ino = je32_to_cpu(ri->ino);
941 int err;
736 942
737 D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs)); 943 D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", ofs));
738 944
@@ -745,12 +951,6 @@ static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_erasebloc
745 Which means that the _full_ amount of time to get to proper write mode with GC 951 Which means that the _full_ amount of time to get to proper write mode with GC
746 operational may actually be _longer_ than before. Sucks to be me. */ 952 operational may actually be _longer_ than before. Sucks to be me. */
747 953
748 raw = jffs2_alloc_raw_node_ref();
749 if (!raw) {
750 printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of node reference failed\n");
751 return -ENOMEM;
752 }
753
754 ic = jffs2_get_ino_cache(c, ino); 954 ic = jffs2_get_ino_cache(c, ino);
755 if (!ic) { 955 if (!ic) {
756 /* Inocache get failed. Either we read a bogus ino# or it's just genuinely the 956 /* Inocache get failed. Either we read a bogus ino# or it's just genuinely the
@@ -762,30 +962,17 @@ static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_erasebloc
762 printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 962 printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
763 ofs, je32_to_cpu(ri->node_crc), crc); 963 ofs, je32_to_cpu(ri->node_crc), crc);
764 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */ 964 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
765 DIRTY_SPACE(PAD(je32_to_cpu(ri->totlen))); 965 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(ri->totlen)))))
766 jffs2_free_raw_node_ref(raw); 966 return err;
767 return 0; 967 return 0;
768 } 968 }
769 ic = jffs2_scan_make_ino_cache(c, ino); 969 ic = jffs2_scan_make_ino_cache(c, ino);
770 if (!ic) { 970 if (!ic)
771 jffs2_free_raw_node_ref(raw);
772 return -ENOMEM; 971 return -ENOMEM;
773 }
774 } 972 }
775 973
776 /* Wheee. It worked */ 974 /* Wheee. It worked */
777 975 jffs2_link_node_ref(c, jeb, ofs | REF_UNCHECKED, PAD(je32_to_cpu(ri->totlen)), ic);
778 raw->flash_offset = ofs | REF_UNCHECKED;
779 raw->__totlen = PAD(je32_to_cpu(ri->totlen));
780 raw->next_phys = NULL;
781 raw->next_in_ino = ic->nodes;
782
783 ic->nodes = raw;
784 if (!jeb->first_node)
785 jeb->first_node = raw;
786 if (jeb->last_node)
787 jeb->last_node->next_phys = raw;
788 jeb->last_node = raw;
789 976
790 D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n", 977 D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
791 je32_to_cpu(ri->ino), je32_to_cpu(ri->version), 978 je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
@@ -794,8 +981,6 @@ static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_erasebloc
794 981
795 pseudo_random += je32_to_cpu(ri->version); 982 pseudo_random += je32_to_cpu(ri->version);
796 983
797 UNCHECKED_SPACE(PAD(je32_to_cpu(ri->totlen)));
798
799 if (jffs2_sum_active()) { 984 if (jffs2_sum_active()) {
800 jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset); 985 jffs2_sum_add_inode_mem(s, ri, ofs - jeb->offset);
801 } 986 }
@@ -806,10 +991,10 @@ static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_erasebloc
806static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 991static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
807 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s) 992 struct jffs2_raw_dirent *rd, uint32_t ofs, struct jffs2_summary *s)
808{ 993{
809 struct jffs2_raw_node_ref *raw;
810 struct jffs2_full_dirent *fd; 994 struct jffs2_full_dirent *fd;
811 struct jffs2_inode_cache *ic; 995 struct jffs2_inode_cache *ic;
812 uint32_t crc; 996 uint32_t crc;
997 int err;
813 998
814 D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs)); 999 D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", ofs));
815 1000
@@ -821,7 +1006,8 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
821 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n", 1006 printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
822 ofs, je32_to_cpu(rd->node_crc), crc); 1007 ofs, je32_to_cpu(rd->node_crc), crc);
823 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */ 1008 /* We believe totlen because the CRC on the node _header_ was OK, just the node itself failed. */
824 DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen))); 1009 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1010 return err;
825 return 0; 1011 return 0;
826 } 1012 }
827 1013
@@ -842,40 +1028,23 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
842 jffs2_free_full_dirent(fd); 1028 jffs2_free_full_dirent(fd);
843 /* FIXME: Why do we believe totlen? */ 1029 /* FIXME: Why do we believe totlen? */
844 /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */ 1030 /* We believe totlen because the CRC on the node _header_ was OK, just the name failed. */
845 DIRTY_SPACE(PAD(je32_to_cpu(rd->totlen))); 1031 if ((err = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(rd->totlen)))))
1032 return err;
846 return 0; 1033 return 0;
847 } 1034 }
848 raw = jffs2_alloc_raw_node_ref();
849 if (!raw) {
850 jffs2_free_full_dirent(fd);
851 printk(KERN_NOTICE "jffs2_scan_dirent_node(): allocation of node reference failed\n");
852 return -ENOMEM;
853 }
854 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino)); 1035 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(rd->pino));
855 if (!ic) { 1036 if (!ic) {
856 jffs2_free_full_dirent(fd); 1037 jffs2_free_full_dirent(fd);
857 jffs2_free_raw_node_ref(raw);
858 return -ENOMEM; 1038 return -ENOMEM;
859 } 1039 }
860 1040
861 raw->__totlen = PAD(je32_to_cpu(rd->totlen)); 1041 fd->raw = jffs2_link_node_ref(c, jeb, ofs | REF_PRISTINE, PAD(je32_to_cpu(rd->totlen)), ic);
862 raw->flash_offset = ofs | REF_PRISTINE;
863 raw->next_phys = NULL;
864 raw->next_in_ino = ic->nodes;
865 ic->nodes = raw;
866 if (!jeb->first_node)
867 jeb->first_node = raw;
868 if (jeb->last_node)
869 jeb->last_node->next_phys = raw;
870 jeb->last_node = raw;
871 1042
872 fd->raw = raw;
873 fd->next = NULL; 1043 fd->next = NULL;
874 fd->version = je32_to_cpu(rd->version); 1044 fd->version = je32_to_cpu(rd->version);
875 fd->ino = je32_to_cpu(rd->ino); 1045 fd->ino = je32_to_cpu(rd->ino);
876 fd->nhash = full_name_hash(fd->name, rd->nsize); 1046 fd->nhash = full_name_hash(fd->name, rd->nsize);
877 fd->type = rd->type; 1047 fd->type = rd->type;
878 USED_SPACE(PAD(je32_to_cpu(rd->totlen)));
879 jffs2_add_fd_to_list(c, fd, &ic->scan_dents); 1048 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
880 1049
881 if (jffs2_sum_active()) { 1050 if (jffs2_sum_active()) {
diff --git a/fs/jffs2/security.c b/fs/jffs2/security.c
new file mode 100644
index 000000000000..52a9894a6364
--- /dev/null
+++ b/fs/jffs2/security.c
@@ -0,0 +1,82 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/time.h>
15#include <linux/pagemap.h>
16#include <linux/highmem.h>
17#include <linux/crc32.h>
18#include <linux/jffs2.h>
19#include <linux/xattr.h>
20#include <linux/mtd/mtd.h>
21#include <linux/security.h>
22#include "nodelist.h"
23
24/* ---- Initial Security Label Attachment -------------- */
25int jffs2_init_security(struct inode *inode, struct inode *dir)
26{
27 int rc;
28 size_t len;
29 void *value;
30 char *name;
31
32 rc = security_inode_init_security(inode, dir, &name, &value, &len);
33 if (rc) {
34 if (rc == -EOPNOTSUPP)
35 return 0;
36 return rc;
37 }
38 rc = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY, name, value, len, 0);
39
40 kfree(name);
41 kfree(value);
42 return rc;
43}
44
45/* ---- XATTR Handler for "security.*" ----------------- */
46static int jffs2_security_getxattr(struct inode *inode, const char *name,
47 void *buffer, size_t size)
48{
49 if (!strcmp(name, ""))
50 return -EINVAL;
51
52 return do_jffs2_getxattr(inode, JFFS2_XPREFIX_SECURITY, name, buffer, size);
53}
54
55static int jffs2_security_setxattr(struct inode *inode, const char *name, const void *buffer,
56 size_t size, int flags)
57{
58 if (!strcmp(name, ""))
59 return -EINVAL;
60
61 return do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY, name, buffer, size, flags);
62}
63
64static size_t jffs2_security_listxattr(struct inode *inode, char *list, size_t list_size,
65 const char *name, size_t name_len)
66{
67 size_t retlen = XATTR_SECURITY_PREFIX_LEN + name_len + 1;
68
69 if (list && retlen <= list_size) {
70 strcpy(list, XATTR_SECURITY_PREFIX);
71 strcpy(list + XATTR_SECURITY_PREFIX_LEN, name);
72 }
73
74 return retlen;
75}
76
77struct xattr_handler jffs2_security_xattr_handler = {
78 .prefix = XATTR_SECURITY_PREFIX,
79 .list = jffs2_security_listxattr,
80 .set = jffs2_security_setxattr,
81 .get = jffs2_security_getxattr
82};
diff --git a/fs/jffs2/summary.c b/fs/jffs2/summary.c
index fb9cec61fcf2..c19bd476e8ec 100644
--- a/fs/jffs2/summary.c
+++ b/fs/jffs2/summary.c
@@ -5,6 +5,7 @@
5 * Zoltan Sogor <weth@inf.u-szeged.hu>, 5 * Zoltan Sogor <weth@inf.u-szeged.hu>,
6 * Patrik Kluba <pajko@halom.u-szeged.hu>, 6 * Patrik Kluba <pajko@halom.u-szeged.hu>,
7 * University of Szeged, Hungary 7 * University of Szeged, Hungary
8 * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com>
8 * 9 *
9 * For licensing information, see the file 'LICENCE' in this directory. 10 * For licensing information, see the file 'LICENCE' in this directory.
10 * 11 *
@@ -42,7 +43,7 @@ int jffs2_sum_init(struct jffs2_sb_info *c)
42 return -ENOMEM; 43 return -ENOMEM;
43 } 44 }
44 45
45 dbg_summary("returned succesfully\n"); 46 dbg_summary("returned successfully\n");
46 47
47 return 0; 48 return 0;
48} 49}
@@ -81,6 +82,19 @@ static int jffs2_sum_add_mem(struct jffs2_summary *s, union jffs2_sum_mem *item)
81 dbg_summary("dirent (%u) added to summary\n", 82 dbg_summary("dirent (%u) added to summary\n",
82 je32_to_cpu(item->d.ino)); 83 je32_to_cpu(item->d.ino));
83 break; 84 break;
85#ifdef CONFIG_JFFS2_FS_XATTR
86 case JFFS2_NODETYPE_XATTR:
87 s->sum_size += JFFS2_SUMMARY_XATTR_SIZE;
88 s->sum_num++;
89 dbg_summary("xattr (xid=%u, version=%u) added to summary\n",
90 je32_to_cpu(item->x.xid), je32_to_cpu(item->x.version));
91 break;
92 case JFFS2_NODETYPE_XREF:
93 s->sum_size += JFFS2_SUMMARY_XREF_SIZE;
94 s->sum_num++;
95 dbg_summary("xref added to summary\n");
96 break;
97#endif
84 default: 98 default:
85 JFFS2_WARNING("UNKNOWN node type %u\n", 99 JFFS2_WARNING("UNKNOWN node type %u\n",
86 je16_to_cpu(item->u.nodetype)); 100 je16_to_cpu(item->u.nodetype));
@@ -141,6 +155,40 @@ int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *r
141 return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp); 155 return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
142} 156}
143 157
158#ifdef CONFIG_JFFS2_FS_XATTR
159int jffs2_sum_add_xattr_mem(struct jffs2_summary *s, struct jffs2_raw_xattr *rx, uint32_t ofs)
160{
161 struct jffs2_sum_xattr_mem *temp;
162
163 temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL);
164 if (!temp)
165 return -ENOMEM;
166
167 temp->nodetype = rx->nodetype;
168 temp->xid = rx->xid;
169 temp->version = rx->version;
170 temp->offset = cpu_to_je32(ofs);
171 temp->totlen = rx->totlen;
172 temp->next = NULL;
173
174 return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
175}
176
177int jffs2_sum_add_xref_mem(struct jffs2_summary *s, struct jffs2_raw_xref *rr, uint32_t ofs)
178{
179 struct jffs2_sum_xref_mem *temp;
180
181 temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL);
182 if (!temp)
183 return -ENOMEM;
184
185 temp->nodetype = rr->nodetype;
186 temp->offset = cpu_to_je32(ofs);
187 temp->next = NULL;
188
189 return jffs2_sum_add_mem(s, (union jffs2_sum_mem *)temp);
190}
191#endif
144/* Cleanup every collected summary information */ 192/* Cleanup every collected summary information */
145 193
146static void jffs2_sum_clean_collected(struct jffs2_summary *s) 194static void jffs2_sum_clean_collected(struct jffs2_summary *s)
@@ -259,7 +307,34 @@ int jffs2_sum_add_kvec(struct jffs2_sb_info *c, const struct kvec *invecs,
259 307
260 return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp); 308 return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
261 } 309 }
310#ifdef CONFIG_JFFS2_FS_XATTR
311 case JFFS2_NODETYPE_XATTR: {
312 struct jffs2_sum_xattr_mem *temp;
313 temp = kmalloc(sizeof(struct jffs2_sum_xattr_mem), GFP_KERNEL);
314 if (!temp)
315 goto no_mem;
316
317 temp->nodetype = node->x.nodetype;
318 temp->xid = node->x.xid;
319 temp->version = node->x.version;
320 temp->totlen = node->x.totlen;
321 temp->offset = cpu_to_je32(ofs);
322 temp->next = NULL;
262 323
324 return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
325 }
326 case JFFS2_NODETYPE_XREF: {
327 struct jffs2_sum_xref_mem *temp;
328 temp = kmalloc(sizeof(struct jffs2_sum_xref_mem), GFP_KERNEL);
329 if (!temp)
330 goto no_mem;
331 temp->nodetype = node->r.nodetype;
332 temp->offset = cpu_to_je32(ofs);
333 temp->next = NULL;
334
335 return jffs2_sum_add_mem(c->summary, (union jffs2_sum_mem *)temp);
336 }
337#endif
263 case JFFS2_NODETYPE_PADDING: 338 case JFFS2_NODETYPE_PADDING:
264 dbg_summary("node PADDING\n"); 339 dbg_summary("node PADDING\n");
265 c->summary->sum_padded += je32_to_cpu(node->u.totlen); 340 c->summary->sum_padded += je32_to_cpu(node->u.totlen);
@@ -288,23 +363,41 @@ no_mem:
288 return -ENOMEM; 363 return -ENOMEM;
289} 364}
290 365
366static struct jffs2_raw_node_ref *sum_link_node_ref(struct jffs2_sb_info *c,
367 struct jffs2_eraseblock *jeb,
368 uint32_t ofs, uint32_t len,
369 struct jffs2_inode_cache *ic)
370{
371 /* If there was a gap, mark it dirty */
372 if ((ofs & ~3) > c->sector_size - jeb->free_size) {
373 /* Ew. Summary doesn't actually tell us explicitly about dirty space */
374 jffs2_scan_dirty_space(c, jeb, (ofs & ~3) - (c->sector_size - jeb->free_size));
375 }
376
377 return jffs2_link_node_ref(c, jeb, jeb->offset + ofs, len, ic);
378}
291 379
292/* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */ 380/* Process the stored summary information - helper function for jffs2_sum_scan_sumnode() */
293 381
294static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 382static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
295 struct jffs2_raw_summary *summary, uint32_t *pseudo_random) 383 struct jffs2_raw_summary *summary, uint32_t *pseudo_random)
296{ 384{
297 struct jffs2_raw_node_ref *raw;
298 struct jffs2_inode_cache *ic; 385 struct jffs2_inode_cache *ic;
299 struct jffs2_full_dirent *fd; 386 struct jffs2_full_dirent *fd;
300 void *sp; 387 void *sp;
301 int i, ino; 388 int i, ino;
389 int err;
302 390
303 sp = summary->sum; 391 sp = summary->sum;
304 392
305 for (i=0; i<je32_to_cpu(summary->sum_num); i++) { 393 for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
306 dbg_summary("processing summary index %d\n", i); 394 dbg_summary("processing summary index %d\n", i);
307 395
396 /* Make sure there's a spare ref for dirty space */
397 err = jffs2_prealloc_raw_node_refs(c, jeb, 2);
398 if (err)
399 return err;
400
308 switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) { 401 switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
309 case JFFS2_NODETYPE_INODE: { 402 case JFFS2_NODETYPE_INODE: {
310 struct jffs2_sum_inode_flash *spi; 403 struct jffs2_sum_inode_flash *spi;
@@ -312,38 +405,20 @@ static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eras
312 405
313 ino = je32_to_cpu(spi->inode); 406 ino = je32_to_cpu(spi->inode);
314 407
315 dbg_summary("Inode at 0x%08x\n", 408 dbg_summary("Inode at 0x%08x-0x%08x\n",
316 jeb->offset + je32_to_cpu(spi->offset)); 409 jeb->offset + je32_to_cpu(spi->offset),
317 410 jeb->offset + je32_to_cpu(spi->offset) + je32_to_cpu(spi->totlen));
318 raw = jffs2_alloc_raw_node_ref();
319 if (!raw) {
320 JFFS2_NOTICE("allocation of node reference failed\n");
321 kfree(summary);
322 return -ENOMEM;
323 }
324 411
325 ic = jffs2_scan_make_ino_cache(c, ino); 412 ic = jffs2_scan_make_ino_cache(c, ino);
326 if (!ic) { 413 if (!ic) {
327 JFFS2_NOTICE("scan_make_ino_cache failed\n"); 414 JFFS2_NOTICE("scan_make_ino_cache failed\n");
328 jffs2_free_raw_node_ref(raw);
329 kfree(summary);
330 return -ENOMEM; 415 return -ENOMEM;
331 } 416 }
332 417
333 raw->flash_offset = (jeb->offset + je32_to_cpu(spi->offset)) | REF_UNCHECKED; 418 sum_link_node_ref(c, jeb, je32_to_cpu(spi->offset) | REF_UNCHECKED,
334 raw->__totlen = PAD(je32_to_cpu(spi->totlen)); 419 PAD(je32_to_cpu(spi->totlen)), ic);
335 raw->next_phys = NULL;
336 raw->next_in_ino = ic->nodes;
337
338 ic->nodes = raw;
339 if (!jeb->first_node)
340 jeb->first_node = raw;
341 if (jeb->last_node)
342 jeb->last_node->next_phys = raw;
343 jeb->last_node = raw;
344 *pseudo_random += je32_to_cpu(spi->version);
345 420
346 UNCHECKED_SPACE(PAD(je32_to_cpu(spi->totlen))); 421 *pseudo_random += je32_to_cpu(spi->version);
347 422
348 sp += JFFS2_SUMMARY_INODE_SIZE; 423 sp += JFFS2_SUMMARY_INODE_SIZE;
349 424
@@ -354,52 +429,33 @@ static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eras
354 struct jffs2_sum_dirent_flash *spd; 429 struct jffs2_sum_dirent_flash *spd;
355 spd = sp; 430 spd = sp;
356 431
357 dbg_summary("Dirent at 0x%08x\n", 432 dbg_summary("Dirent at 0x%08x-0x%08x\n",
358 jeb->offset + je32_to_cpu(spd->offset)); 433 jeb->offset + je32_to_cpu(spd->offset),
434 jeb->offset + je32_to_cpu(spd->offset) + je32_to_cpu(spd->totlen));
435
359 436
360 fd = jffs2_alloc_full_dirent(spd->nsize+1); 437 fd = jffs2_alloc_full_dirent(spd->nsize+1);
361 if (!fd) { 438 if (!fd)
362 kfree(summary);
363 return -ENOMEM; 439 return -ENOMEM;
364 }
365 440
366 memcpy(&fd->name, spd->name, spd->nsize); 441 memcpy(&fd->name, spd->name, spd->nsize);
367 fd->name[spd->nsize] = 0; 442 fd->name[spd->nsize] = 0;
368 443
369 raw = jffs2_alloc_raw_node_ref();
370 if (!raw) {
371 jffs2_free_full_dirent(fd);
372 JFFS2_NOTICE("allocation of node reference failed\n");
373 kfree(summary);
374 return -ENOMEM;
375 }
376
377 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino)); 444 ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
378 if (!ic) { 445 if (!ic) {
379 jffs2_free_full_dirent(fd); 446 jffs2_free_full_dirent(fd);
380 jffs2_free_raw_node_ref(raw);
381 kfree(summary);
382 return -ENOMEM; 447 return -ENOMEM;
383 } 448 }
384 449
385 raw->__totlen = PAD(je32_to_cpu(spd->totlen)); 450 fd->raw = sum_link_node_ref(c, jeb, je32_to_cpu(spd->offset) | REF_UNCHECKED,
386 raw->flash_offset = (jeb->offset + je32_to_cpu(spd->offset)) | REF_PRISTINE; 451 PAD(je32_to_cpu(spd->totlen)), ic);
387 raw->next_phys = NULL; 452
388 raw->next_in_ino = ic->nodes;
389 ic->nodes = raw;
390 if (!jeb->first_node)
391 jeb->first_node = raw;
392 if (jeb->last_node)
393 jeb->last_node->next_phys = raw;
394 jeb->last_node = raw;
395
396 fd->raw = raw;
397 fd->next = NULL; 453 fd->next = NULL;
398 fd->version = je32_to_cpu(spd->version); 454 fd->version = je32_to_cpu(spd->version);
399 fd->ino = je32_to_cpu(spd->ino); 455 fd->ino = je32_to_cpu(spd->ino);
400 fd->nhash = full_name_hash(fd->name, spd->nsize); 456 fd->nhash = full_name_hash(fd->name, spd->nsize);
401 fd->type = spd->type; 457 fd->type = spd->type;
402 USED_SPACE(PAD(je32_to_cpu(spd->totlen))); 458
403 jffs2_add_fd_to_list(c, fd, &ic->scan_dents); 459 jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
404 460
405 *pseudo_random += je32_to_cpu(spd->version); 461 *pseudo_random += je32_to_cpu(spd->version);
@@ -408,48 +464,100 @@ static int jffs2_sum_process_sum_data(struct jffs2_sb_info *c, struct jffs2_eras
408 464
409 break; 465 break;
410 } 466 }
467#ifdef CONFIG_JFFS2_FS_XATTR
468 case JFFS2_NODETYPE_XATTR: {
469 struct jffs2_xattr_datum *xd;
470 struct jffs2_sum_xattr_flash *spx;
471
472 spx = (struct jffs2_sum_xattr_flash *)sp;
473 dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
474 jeb->offset + je32_to_cpu(spx->offset),
475 jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen),
476 je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
477
478 xd = jffs2_setup_xattr_datum(c, je32_to_cpu(spx->xid),
479 je32_to_cpu(spx->version));
480 if (IS_ERR(xd))
481 return PTR_ERR(xd);
482 if (xd->version > je32_to_cpu(spx->version)) {
483 /* node is not the newest one */
484 struct jffs2_raw_node_ref *raw
485 = sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED,
486 PAD(je32_to_cpu(spx->totlen)), NULL);
487 raw->next_in_ino = xd->node->next_in_ino;
488 xd->node->next_in_ino = raw;
489 } else {
490 xd->version = je32_to_cpu(spx->version);
491 sum_link_node_ref(c, jeb, je32_to_cpu(spx->offset) | REF_UNCHECKED,
492 PAD(je32_to_cpu(spx->totlen)), (void *)xd);
493 }
494 *pseudo_random += je32_to_cpu(spx->xid);
495 sp += JFFS2_SUMMARY_XATTR_SIZE;
496
497 break;
498 }
499 case JFFS2_NODETYPE_XREF: {
500 struct jffs2_xattr_ref *ref;
501 struct jffs2_sum_xref_flash *spr;
502
503 spr = (struct jffs2_sum_xref_flash *)sp;
504 dbg_summary("xref at %#08x-%#08x\n",
505 jeb->offset + je32_to_cpu(spr->offset),
506 jeb->offset + je32_to_cpu(spr->offset) +
507 (uint32_t)PAD(sizeof(struct jffs2_raw_xref)));
508
509 ref = jffs2_alloc_xattr_ref();
510 if (!ref) {
511 JFFS2_NOTICE("allocation of xattr_datum failed\n");
512 return -ENOMEM;
513 }
514 ref->next = c->xref_temp;
515 c->xref_temp = ref;
516
517 sum_link_node_ref(c, jeb, je32_to_cpu(spr->offset) | REF_UNCHECKED,
518 PAD(sizeof(struct jffs2_raw_xref)), (void *)ref);
519
520 *pseudo_random += ref->node->flash_offset;
521 sp += JFFS2_SUMMARY_XREF_SIZE;
411 522
523 break;
524 }
525#endif
412 default : { 526 default : {
413 JFFS2_WARNING("Unsupported node type found in summary! Exiting..."); 527 uint16_t nodetype = je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype);
414 kfree(summary); 528 JFFS2_WARNING("Unsupported node type %x found in summary! Exiting...\n", nodetype);
415 return -EIO; 529 if ((nodetype & JFFS2_COMPAT_MASK) == JFFS2_FEATURE_INCOMPAT)
530 return -EIO;
531
532 /* For compatible node types, just fall back to the full scan */
533 c->wasted_size -= jeb->wasted_size;
534 c->free_size += c->sector_size - jeb->free_size;
535 c->used_size -= jeb->used_size;
536 c->dirty_size -= jeb->dirty_size;
537 jeb->wasted_size = jeb->used_size = jeb->dirty_size = 0;
538 jeb->free_size = c->sector_size;
539
540 jffs2_free_jeb_node_refs(c, jeb);
541 return -ENOTRECOVERABLE;
416 } 542 }
417 } 543 }
418 } 544 }
419
420 kfree(summary);
421 return 0; 545 return 0;
422} 546}
423 547
424/* Process the summary node - called from jffs2_scan_eraseblock() */ 548/* Process the summary node - called from jffs2_scan_eraseblock() */
425
426int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 549int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
427 uint32_t ofs, uint32_t *pseudo_random) 550 struct jffs2_raw_summary *summary, uint32_t sumsize,
551 uint32_t *pseudo_random)
428{ 552{
429 struct jffs2_unknown_node crcnode; 553 struct jffs2_unknown_node crcnode;
430 struct jffs2_raw_node_ref *cache_ref; 554 int ret, ofs;
431 struct jffs2_raw_summary *summary;
432 int ret, sumsize;
433 uint32_t crc; 555 uint32_t crc;
434 556
435 sumsize = c->sector_size - ofs; 557 ofs = c->sector_size - sumsize;
436 ofs += jeb->offset;
437 558
438 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n", 559 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
439 jeb->offset, ofs, sumsize); 560 jeb->offset, jeb->offset + ofs, sumsize);
440
441 summary = kmalloc(sumsize, GFP_KERNEL);
442
443 if (!summary) {
444 return -ENOMEM;
445 }
446
447 ret = jffs2_fill_scan_buf(c, (unsigned char *)summary, ofs, sumsize);
448
449 if (ret) {
450 kfree(summary);
451 return ret;
452 }
453 561
454 /* OK, now check for node validity and CRC */ 562 /* OK, now check for node validity and CRC */
455 crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 563 crcnode.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
@@ -486,66 +594,49 @@ int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
486 594
487 dbg_summary("Summary : CLEANMARKER node \n"); 595 dbg_summary("Summary : CLEANMARKER node \n");
488 596
597 ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
598 if (ret)
599 return ret;
600
489 if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) { 601 if (je32_to_cpu(summary->cln_mkr) != c->cleanmarker_size) {
490 dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n", 602 dbg_summary("CLEANMARKER node has totlen 0x%x != normal 0x%x\n",
491 je32_to_cpu(summary->cln_mkr), c->cleanmarker_size); 603 je32_to_cpu(summary->cln_mkr), c->cleanmarker_size);
492 UNCHECKED_SPACE(PAD(je32_to_cpu(summary->cln_mkr))); 604 if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr)))))
605 return ret;
493 } else if (jeb->first_node) { 606 } else if (jeb->first_node) {
494 dbg_summary("CLEANMARKER node not first node in block " 607 dbg_summary("CLEANMARKER node not first node in block "
495 "(0x%08x)\n", jeb->offset); 608 "(0x%08x)\n", jeb->offset);
496 UNCHECKED_SPACE(PAD(je32_to_cpu(summary->cln_mkr))); 609 if ((ret = jffs2_scan_dirty_space(c, jeb, PAD(je32_to_cpu(summary->cln_mkr)))))
610 return ret;
497 } else { 611 } else {
498 struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref(); 612 jffs2_link_node_ref(c, jeb, jeb->offset | REF_NORMAL,
499 613 je32_to_cpu(summary->cln_mkr), NULL);
500 if (!marker_ref) {
501 JFFS2_NOTICE("Failed to allocate node ref for clean marker\n");
502 kfree(summary);
503 return -ENOMEM;
504 }
505
506 marker_ref->next_in_ino = NULL;
507 marker_ref->next_phys = NULL;
508 marker_ref->flash_offset = jeb->offset | REF_NORMAL;
509 marker_ref->__totlen = je32_to_cpu(summary->cln_mkr);
510 jeb->first_node = jeb->last_node = marker_ref;
511
512 USED_SPACE( PAD(je32_to_cpu(summary->cln_mkr)) );
513 } 614 }
514 } 615 }
515 616
516 if (je32_to_cpu(summary->padded)) {
517 DIRTY_SPACE(je32_to_cpu(summary->padded));
518 }
519
520 ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random); 617 ret = jffs2_sum_process_sum_data(c, jeb, summary, pseudo_random);
618 /* -ENOTRECOVERABLE isn't a fatal error -- it means we should do a full
619 scan of this eraseblock. So return zero */
620 if (ret == -ENOTRECOVERABLE)
621 return 0;
521 if (ret) 622 if (ret)
522 return ret; 623 return ret; /* real error */
523 624
524 /* for PARANOIA_CHECK */ 625 /* for PARANOIA_CHECK */
525 cache_ref = jffs2_alloc_raw_node_ref(); 626 ret = jffs2_prealloc_raw_node_refs(c, jeb, 2);
526 627 if (ret)
527 if (!cache_ref) { 628 return ret;
528 JFFS2_NOTICE("Failed to allocate node ref for cache\n");
529 return -ENOMEM;
530 }
531
532 cache_ref->next_in_ino = NULL;
533 cache_ref->next_phys = NULL;
534 cache_ref->flash_offset = ofs | REF_NORMAL;
535 cache_ref->__totlen = sumsize;
536
537 if (!jeb->first_node)
538 jeb->first_node = cache_ref;
539 if (jeb->last_node)
540 jeb->last_node->next_phys = cache_ref;
541 jeb->last_node = cache_ref;
542 629
543 USED_SPACE(sumsize); 630 sum_link_node_ref(c, jeb, ofs | REF_NORMAL, sumsize, NULL);
544 631
545 jeb->wasted_size += jeb->free_size; 632 if (unlikely(jeb->free_size)) {
546 c->wasted_size += jeb->free_size; 633 JFFS2_WARNING("Free size 0x%x bytes in eraseblock @0x%08x with summary?\n",
547 c->free_size -= jeb->free_size; 634 jeb->free_size, jeb->offset);
548 jeb->free_size = 0; 635 jeb->wasted_size += jeb->free_size;
636 c->wasted_size += jeb->free_size;
637 c->free_size -= jeb->free_size;
638 jeb->free_size = 0;
639 }
549 640
550 return jffs2_scan_classify_jeb(c, jeb); 641 return jffs2_scan_classify_jeb(c, jeb);
551 642
@@ -564,6 +655,7 @@ static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock
564 union jffs2_sum_mem *temp; 655 union jffs2_sum_mem *temp;
565 struct jffs2_sum_marker *sm; 656 struct jffs2_sum_marker *sm;
566 struct kvec vecs[2]; 657 struct kvec vecs[2];
658 uint32_t sum_ofs;
567 void *wpage; 659 void *wpage;
568 int ret; 660 int ret;
569 size_t retlen; 661 size_t retlen;
@@ -581,16 +673,17 @@ static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock
581 wpage = c->summary->sum_buf; 673 wpage = c->summary->sum_buf;
582 674
583 while (c->summary->sum_num) { 675 while (c->summary->sum_num) {
676 temp = c->summary->sum_list_head;
584 677
585 switch (je16_to_cpu(c->summary->sum_list_head->u.nodetype)) { 678 switch (je16_to_cpu(temp->u.nodetype)) {
586 case JFFS2_NODETYPE_INODE: { 679 case JFFS2_NODETYPE_INODE: {
587 struct jffs2_sum_inode_flash *sino_ptr = wpage; 680 struct jffs2_sum_inode_flash *sino_ptr = wpage;
588 681
589 sino_ptr->nodetype = c->summary->sum_list_head->i.nodetype; 682 sino_ptr->nodetype = temp->i.nodetype;
590 sino_ptr->inode = c->summary->sum_list_head->i.inode; 683 sino_ptr->inode = temp->i.inode;
591 sino_ptr->version = c->summary->sum_list_head->i.version; 684 sino_ptr->version = temp->i.version;
592 sino_ptr->offset = c->summary->sum_list_head->i.offset; 685 sino_ptr->offset = temp->i.offset;
593 sino_ptr->totlen = c->summary->sum_list_head->i.totlen; 686 sino_ptr->totlen = temp->i.totlen;
594 687
595 wpage += JFFS2_SUMMARY_INODE_SIZE; 688 wpage += JFFS2_SUMMARY_INODE_SIZE;
596 689
@@ -600,30 +693,60 @@ static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock
600 case JFFS2_NODETYPE_DIRENT: { 693 case JFFS2_NODETYPE_DIRENT: {
601 struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage; 694 struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
602 695
603 sdrnt_ptr->nodetype = c->summary->sum_list_head->d.nodetype; 696 sdrnt_ptr->nodetype = temp->d.nodetype;
604 sdrnt_ptr->totlen = c->summary->sum_list_head->d.totlen; 697 sdrnt_ptr->totlen = temp->d.totlen;
605 sdrnt_ptr->offset = c->summary->sum_list_head->d.offset; 698 sdrnt_ptr->offset = temp->d.offset;
606 sdrnt_ptr->pino = c->summary->sum_list_head->d.pino; 699 sdrnt_ptr->pino = temp->d.pino;
607 sdrnt_ptr->version = c->summary->sum_list_head->d.version; 700 sdrnt_ptr->version = temp->d.version;
608 sdrnt_ptr->ino = c->summary->sum_list_head->d.ino; 701 sdrnt_ptr->ino = temp->d.ino;
609 sdrnt_ptr->nsize = c->summary->sum_list_head->d.nsize; 702 sdrnt_ptr->nsize = temp->d.nsize;
610 sdrnt_ptr->type = c->summary->sum_list_head->d.type; 703 sdrnt_ptr->type = temp->d.type;
611 704
612 memcpy(sdrnt_ptr->name, c->summary->sum_list_head->d.name, 705 memcpy(sdrnt_ptr->name, temp->d.name,
613 c->summary->sum_list_head->d.nsize); 706 temp->d.nsize);
614 707
615 wpage += JFFS2_SUMMARY_DIRENT_SIZE(c->summary->sum_list_head->d.nsize); 708 wpage += JFFS2_SUMMARY_DIRENT_SIZE(temp->d.nsize);
616 709
617 break; 710 break;
618 } 711 }
712#ifdef CONFIG_JFFS2_FS_XATTR
713 case JFFS2_NODETYPE_XATTR: {
714 struct jffs2_sum_xattr_flash *sxattr_ptr = wpage;
715
716 temp = c->summary->sum_list_head;
717 sxattr_ptr->nodetype = temp->x.nodetype;
718 sxattr_ptr->xid = temp->x.xid;
719 sxattr_ptr->version = temp->x.version;
720 sxattr_ptr->offset = temp->x.offset;
721 sxattr_ptr->totlen = temp->x.totlen;
722
723 wpage += JFFS2_SUMMARY_XATTR_SIZE;
724 break;
725 }
726 case JFFS2_NODETYPE_XREF: {
727 struct jffs2_sum_xref_flash *sxref_ptr = wpage;
728
729 temp = c->summary->sum_list_head;
730 sxref_ptr->nodetype = temp->r.nodetype;
731 sxref_ptr->offset = temp->r.offset;
619 732
733 wpage += JFFS2_SUMMARY_XREF_SIZE;
734 break;
735 }
736#endif
620 default : { 737 default : {
621 BUG(); /* unknown node in summary information */ 738 if ((je16_to_cpu(temp->u.nodetype) & JFFS2_COMPAT_MASK)
739 == JFFS2_FEATURE_RWCOMPAT_COPY) {
740 dbg_summary("Writing unknown RWCOMPAT_COPY node type %x\n",
741 je16_to_cpu(temp->u.nodetype));
742 jffs2_sum_disable_collecting(c->summary);
743 } else {
744 BUG(); /* unknown node in summary information */
745 }
622 } 746 }
623 } 747 }
624 748
625 temp = c->summary->sum_list_head; 749 c->summary->sum_list_head = temp->u.next;
626 c->summary->sum_list_head = c->summary->sum_list_head->u.next;
627 kfree(temp); 750 kfree(temp);
628 751
629 c->summary->sum_num--; 752 c->summary->sum_num--;
@@ -645,25 +768,34 @@ static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock
645 vecs[1].iov_base = c->summary->sum_buf; 768 vecs[1].iov_base = c->summary->sum_buf;
646 vecs[1].iov_len = datasize; 769 vecs[1].iov_len = datasize;
647 770
648 dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n", 771 sum_ofs = jeb->offset + c->sector_size - jeb->free_size;
649 jeb->offset + c->sector_size - jeb->free_size);
650 772
651 spin_unlock(&c->erase_completion_lock); 773 dbg_summary("JFFS2: writing out data to flash to pos : 0x%08x\n",
652 ret = jffs2_flash_writev(c, vecs, 2, jeb->offset + c->sector_size - 774 sum_ofs);
653 jeb->free_size, &retlen, 0);
654 spin_lock(&c->erase_completion_lock);
655 775
776 ret = jffs2_flash_writev(c, vecs, 2, sum_ofs, &retlen, 0);
656 777
657 if (ret || (retlen != infosize)) { 778 if (ret || (retlen != infosize)) {
658 JFFS2_WARNING("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", 779
659 infosize, jeb->offset + c->sector_size - jeb->free_size, ret, retlen); 780 JFFS2_WARNING("Write of %u bytes at 0x%08x failed. returned %d, retlen %zd\n",
781 infosize, sum_ofs, ret, retlen);
782
783 if (retlen) {
784 /* Waste remaining space */
785 spin_lock(&c->erase_completion_lock);
786 jffs2_link_node_ref(c, jeb, sum_ofs | REF_OBSOLETE, infosize, NULL);
787 spin_unlock(&c->erase_completion_lock);
788 }
660 789
661 c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE; 790 c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
662 WASTED_SPACE(infosize);
663 791
664 return 1; 792 return 0;
665 } 793 }
666 794
795 spin_lock(&c->erase_completion_lock);
796 jffs2_link_node_ref(c, jeb, sum_ofs | REF_NORMAL, infosize, NULL);
797 spin_unlock(&c->erase_completion_lock);
798
667 return 0; 799 return 0;
668} 800}
669 801
@@ -671,13 +803,16 @@ static int jffs2_sum_write_data(struct jffs2_sb_info *c, struct jffs2_eraseblock
671 803
672int jffs2_sum_write_sumnode(struct jffs2_sb_info *c) 804int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
673{ 805{
674 struct jffs2_raw_node_ref *summary_ref; 806 int datasize, infosize, padsize;
675 int datasize, infosize, padsize, ret;
676 struct jffs2_eraseblock *jeb; 807 struct jffs2_eraseblock *jeb;
808 int ret;
677 809
678 dbg_summary("called\n"); 810 dbg_summary("called\n");
679 811
812 spin_unlock(&c->erase_completion_lock);
813
680 jeb = c->nextblock; 814 jeb = c->nextblock;
815 jffs2_prealloc_raw_node_refs(c, jeb, 1);
681 816
682 if (!c->summary->sum_num || !c->summary->sum_list_head) { 817 if (!c->summary->sum_num || !c->summary->sum_list_head) {
683 JFFS2_WARNING("Empty summary info!!!\n"); 818 JFFS2_WARNING("Empty summary info!!!\n");
@@ -696,35 +831,11 @@ int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
696 jffs2_sum_disable_collecting(c->summary); 831 jffs2_sum_disable_collecting(c->summary);
697 832
698 JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize); 833 JFFS2_WARNING("Not enough space for summary, padsize = %d\n", padsize);
834 spin_lock(&c->erase_completion_lock);
699 return 0; 835 return 0;
700 } 836 }
701 837
702 ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize); 838 ret = jffs2_sum_write_data(c, jeb, infosize, datasize, padsize);
703 if (ret)
704 return 0; /* can't write out summary, block is marked as NOSUM_SIZE */
705
706 /* for ACCT_PARANOIA_CHECK */
707 spin_unlock(&c->erase_completion_lock);
708 summary_ref = jffs2_alloc_raw_node_ref();
709 spin_lock(&c->erase_completion_lock); 839 spin_lock(&c->erase_completion_lock);
710 840 return ret;
711 if (!summary_ref) {
712 JFFS2_NOTICE("Failed to allocate node ref for summary\n");
713 return -ENOMEM;
714 }
715
716 summary_ref->next_in_ino = NULL;
717 summary_ref->next_phys = NULL;
718 summary_ref->flash_offset = (jeb->offset + c->sector_size - jeb->free_size) | REF_NORMAL;
719 summary_ref->__totlen = infosize;
720
721 if (!jeb->first_node)
722 jeb->first_node = summary_ref;
723 if (jeb->last_node)
724 jeb->last_node->next_phys = summary_ref;
725 jeb->last_node = summary_ref;
726
727 USED_SPACE(infosize);
728
729 return 0;
730} 841}
diff --git a/fs/jffs2/summary.h b/fs/jffs2/summary.h
index b7a678be1709..6bf1f6aa4552 100644
--- a/fs/jffs2/summary.h
+++ b/fs/jffs2/summary.h
@@ -18,23 +18,6 @@
18#include <linux/uio.h> 18#include <linux/uio.h>
19#include <linux/jffs2.h> 19#include <linux/jffs2.h>
20 20
21#define DIRTY_SPACE(x) do { typeof(x) _x = (x); \
22 c->free_size -= _x; c->dirty_size += _x; \
23 jeb->free_size -= _x ; jeb->dirty_size += _x; \
24 }while(0)
25#define USED_SPACE(x) do { typeof(x) _x = (x); \
26 c->free_size -= _x; c->used_size += _x; \
27 jeb->free_size -= _x ; jeb->used_size += _x; \
28 }while(0)
29#define WASTED_SPACE(x) do { typeof(x) _x = (x); \
30 c->free_size -= _x; c->wasted_size += _x; \
31 jeb->free_size -= _x ; jeb->wasted_size += _x; \
32 }while(0)
33#define UNCHECKED_SPACE(x) do { typeof(x) _x = (x); \
34 c->free_size -= _x; c->unchecked_size += _x; \
35 jeb->free_size -= _x ; jeb->unchecked_size += _x; \
36 }while(0)
37
38#define BLK_STATE_ALLFF 0 21#define BLK_STATE_ALLFF 0
39#define BLK_STATE_CLEAN 1 22#define BLK_STATE_CLEAN 1
40#define BLK_STATE_PARTDIRTY 2 23#define BLK_STATE_PARTDIRTY 2
@@ -45,6 +28,8 @@
45#define JFFS2_SUMMARY_NOSUM_SIZE 0xffffffff 28#define JFFS2_SUMMARY_NOSUM_SIZE 0xffffffff
46#define JFFS2_SUMMARY_INODE_SIZE (sizeof(struct jffs2_sum_inode_flash)) 29#define JFFS2_SUMMARY_INODE_SIZE (sizeof(struct jffs2_sum_inode_flash))
47#define JFFS2_SUMMARY_DIRENT_SIZE(x) (sizeof(struct jffs2_sum_dirent_flash) + (x)) 30#define JFFS2_SUMMARY_DIRENT_SIZE(x) (sizeof(struct jffs2_sum_dirent_flash) + (x))
31#define JFFS2_SUMMARY_XATTR_SIZE (sizeof(struct jffs2_sum_xattr_flash))
32#define JFFS2_SUMMARY_XREF_SIZE (sizeof(struct jffs2_sum_xref_flash))
48 33
49/* Summary structures used on flash */ 34/* Summary structures used on flash */
50 35
@@ -75,11 +60,28 @@ struct jffs2_sum_dirent_flash
75 uint8_t name[0]; /* dirent name */ 60 uint8_t name[0]; /* dirent name */
76} __attribute__((packed)); 61} __attribute__((packed));
77 62
63struct jffs2_sum_xattr_flash
64{
65 jint16_t nodetype; /* == JFFS2_NODETYPE_XATR */
66 jint32_t xid; /* xattr identifier */
67 jint32_t version; /* version number */
68 jint32_t offset; /* offset on jeb */
69 jint32_t totlen; /* node length */
70} __attribute__((packed));
71
72struct jffs2_sum_xref_flash
73{
74 jint16_t nodetype; /* == JFFS2_NODETYPE_XREF */
75 jint32_t offset; /* offset on jeb */
76} __attribute__((packed));
77
78union jffs2_sum_flash 78union jffs2_sum_flash
79{ 79{
80 struct jffs2_sum_unknown_flash u; 80 struct jffs2_sum_unknown_flash u;
81 struct jffs2_sum_inode_flash i; 81 struct jffs2_sum_inode_flash i;
82 struct jffs2_sum_dirent_flash d; 82 struct jffs2_sum_dirent_flash d;
83 struct jffs2_sum_xattr_flash x;
84 struct jffs2_sum_xref_flash r;
83}; 85};
84 86
85/* Summary structures used in the memory */ 87/* Summary structures used in the memory */
@@ -114,11 +116,30 @@ struct jffs2_sum_dirent_mem
114 uint8_t name[0]; /* dirent name */ 116 uint8_t name[0]; /* dirent name */
115} __attribute__((packed)); 117} __attribute__((packed));
116 118
119struct jffs2_sum_xattr_mem
120{
121 union jffs2_sum_mem *next;
122 jint16_t nodetype;
123 jint32_t xid;
124 jint32_t version;
125 jint32_t offset;
126 jint32_t totlen;
127} __attribute__((packed));
128
129struct jffs2_sum_xref_mem
130{
131 union jffs2_sum_mem *next;
132 jint16_t nodetype;
133 jint32_t offset;
134} __attribute__((packed));
135
117union jffs2_sum_mem 136union jffs2_sum_mem
118{ 137{
119 struct jffs2_sum_unknown_mem u; 138 struct jffs2_sum_unknown_mem u;
120 struct jffs2_sum_inode_mem i; 139 struct jffs2_sum_inode_mem i;
121 struct jffs2_sum_dirent_mem d; 140 struct jffs2_sum_dirent_mem d;
141 struct jffs2_sum_xattr_mem x;
142 struct jffs2_sum_xref_mem r;
122}; 143};
123 144
124/* Summary related information stored in superblock */ 145/* Summary related information stored in superblock */
@@ -159,8 +180,11 @@ int jffs2_sum_write_sumnode(struct jffs2_sb_info *c);
159int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size); 180int jffs2_sum_add_padding_mem(struct jffs2_summary *s, uint32_t size);
160int jffs2_sum_add_inode_mem(struct jffs2_summary *s, struct jffs2_raw_inode *ri, uint32_t ofs); 181int jffs2_sum_add_inode_mem(struct jffs2_summary *s, struct jffs2_raw_inode *ri, uint32_t ofs);
161int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *rd, uint32_t ofs); 182int jffs2_sum_add_dirent_mem(struct jffs2_summary *s, struct jffs2_raw_dirent *rd, uint32_t ofs);
183int jffs2_sum_add_xattr_mem(struct jffs2_summary *s, struct jffs2_raw_xattr *rx, uint32_t ofs);
184int jffs2_sum_add_xref_mem(struct jffs2_summary *s, struct jffs2_raw_xref *rr, uint32_t ofs);
162int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 185int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
163 uint32_t ofs, uint32_t *pseudo_random); 186 struct jffs2_raw_summary *summary, uint32_t sumlen,
187 uint32_t *pseudo_random);
164 188
165#else /* SUMMARY DISABLED */ 189#else /* SUMMARY DISABLED */
166 190
@@ -176,7 +200,9 @@ int jffs2_sum_scan_sumnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
176#define jffs2_sum_add_padding_mem(a,b) 200#define jffs2_sum_add_padding_mem(a,b)
177#define jffs2_sum_add_inode_mem(a,b,c) 201#define jffs2_sum_add_inode_mem(a,b,c)
178#define jffs2_sum_add_dirent_mem(a,b,c) 202#define jffs2_sum_add_dirent_mem(a,b,c)
179#define jffs2_sum_scan_sumnode(a,b,c,d) (0) 203#define jffs2_sum_add_xattr_mem(a,b,c)
204#define jffs2_sum_add_xref_mem(a,b,c)
205#define jffs2_sum_scan_sumnode(a,b,c,d,e) (0)
180 206
181#endif /* CONFIG_JFFS2_SUMMARY */ 207#endif /* CONFIG_JFFS2_SUMMARY */
182 208
diff --git a/fs/jffs2/super.c b/fs/jffs2/super.c
index ffd8e84b22cc..68e3953419b4 100644
--- a/fs/jffs2/super.c
+++ b/fs/jffs2/super.c
@@ -11,7 +11,6 @@
11 * 11 *
12 */ 12 */
13 13
14#include <linux/config.h>
15#include <linux/kernel.h> 14#include <linux/kernel.h>
16#include <linux/module.h> 15#include <linux/module.h>
17#include <linux/slab.h> 16#include <linux/slab.h>
@@ -111,9 +110,10 @@ static int jffs2_sb_set(struct super_block *sb, void *data)
111 return 0; 110 return 0;
112} 111}
113 112
114static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type, 113static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
115 int flags, const char *dev_name, 114 int flags, const char *dev_name,
116 void *data, struct mtd_info *mtd) 115 void *data, struct mtd_info *mtd,
116 struct vfsmount *mnt)
117{ 117{
118 struct super_block *sb; 118 struct super_block *sb;
119 struct jffs2_sb_info *c; 119 struct jffs2_sb_info *c;
@@ -121,19 +121,20 @@ static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
121 121
122 c = kmalloc(sizeof(*c), GFP_KERNEL); 122 c = kmalloc(sizeof(*c), GFP_KERNEL);
123 if (!c) 123 if (!c)
124 return ERR_PTR(-ENOMEM); 124 return -ENOMEM;
125 memset(c, 0, sizeof(*c)); 125 memset(c, 0, sizeof(*c));
126 c->mtd = mtd; 126 c->mtd = mtd;
127 127
128 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c); 128 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
129 129
130 if (IS_ERR(sb)) 130 if (IS_ERR(sb))
131 goto out_put; 131 goto out_error;
132 132
133 if (sb->s_root) { 133 if (sb->s_root) {
134 /* New mountpoint for JFFS2 which is already mounted */ 134 /* New mountpoint for JFFS2 which is already mounted */
135 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n", 135 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
136 mtd->index, mtd->name)); 136 mtd->index, mtd->name));
137 ret = simple_set_mnt(mnt, sb);
137 goto out_put; 138 goto out_put;
138 } 139 }
139 140
@@ -151,51 +152,57 @@ static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
151 152
152 sb->s_op = &jffs2_super_operations; 153 sb->s_op = &jffs2_super_operations;
153 sb->s_flags = flags | MS_NOATIME; 154 sb->s_flags = flags | MS_NOATIME;
154 155 sb->s_xattr = jffs2_xattr_handlers;
156#ifdef CONFIG_JFFS2_FS_POSIX_ACL
157 sb->s_flags |= MS_POSIXACL;
158#endif
155 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0); 159 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
156 160
157 if (ret) { 161 if (ret) {
158 /* Failure case... */ 162 /* Failure case... */
159 up_write(&sb->s_umount); 163 up_write(&sb->s_umount);
160 deactivate_super(sb); 164 deactivate_super(sb);
161 return ERR_PTR(ret); 165 return ret;
162 } 166 }
163 167
164 sb->s_flags |= MS_ACTIVE; 168 sb->s_flags |= MS_ACTIVE;
165 return sb; 169 return simple_set_mnt(mnt, sb);
166 170
171out_error:
172 ret = PTR_ERR(sb);
167 out_put: 173 out_put:
168 kfree(c); 174 kfree(c);
169 put_mtd_device(mtd); 175 put_mtd_device(mtd);
170 176
171 return sb; 177 return ret;
172} 178}
173 179
174static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type, 180static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
175 int flags, const char *dev_name, 181 int flags, const char *dev_name,
176 void *data, int mtdnr) 182 void *data, int mtdnr,
183 struct vfsmount *mnt)
177{ 184{
178 struct mtd_info *mtd; 185 struct mtd_info *mtd;
179 186
180 mtd = get_mtd_device(NULL, mtdnr); 187 mtd = get_mtd_device(NULL, mtdnr);
181 if (!mtd) { 188 if (!mtd) {
182 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr)); 189 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
183 return ERR_PTR(-EINVAL); 190 return -EINVAL;
184 } 191 }
185 192
186 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd); 193 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
187} 194}
188 195
189static struct super_block *jffs2_get_sb(struct file_system_type *fs_type, 196static int jffs2_get_sb(struct file_system_type *fs_type,
190 int flags, const char *dev_name, 197 int flags, const char *dev_name,
191 void *data) 198 void *data, struct vfsmount *mnt)
192{ 199{
193 int err; 200 int err;
194 struct nameidata nd; 201 struct nameidata nd;
195 int mtdnr; 202 int mtdnr;
196 203
197 if (!dev_name) 204 if (!dev_name)
198 return ERR_PTR(-EINVAL); 205 return -EINVAL;
199 206
200 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name)); 207 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
201 208
@@ -217,7 +224,7 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
217 mtd = get_mtd_device(NULL, mtdnr); 224 mtd = get_mtd_device(NULL, mtdnr);
218 if (mtd) { 225 if (mtd) {
219 if (!strcmp(mtd->name, dev_name+4)) 226 if (!strcmp(mtd->name, dev_name+4))
220 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd); 227 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
221 put_mtd_device(mtd); 228 put_mtd_device(mtd);
222 } 229 }
223 } 230 }
@@ -230,7 +237,7 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
230 if (!*endptr) { 237 if (!*endptr) {
231 /* It was a valid number */ 238 /* It was a valid number */
232 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr)); 239 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
233 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr); 240 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
234 } 241 }
235 } 242 }
236 } 243 }
@@ -244,7 +251,7 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
244 err, nd.dentry->d_inode)); 251 err, nd.dentry->d_inode));
245 252
246 if (err) 253 if (err)
247 return ERR_PTR(err); 254 return err;
248 255
249 err = -EINVAL; 256 err = -EINVAL;
250 257
@@ -266,11 +273,11 @@ static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
266 mtdnr = iminor(nd.dentry->d_inode); 273 mtdnr = iminor(nd.dentry->d_inode);
267 path_release(&nd); 274 path_release(&nd);
268 275
269 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr); 276 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
270 277
271out: 278out:
272 path_release(&nd); 279 path_release(&nd);
273 return ERR_PTR(err); 280 return err;
274} 281}
275 282
276static void jffs2_put_super (struct super_block *sb) 283static void jffs2_put_super (struct super_block *sb)
@@ -293,6 +300,7 @@ static void jffs2_put_super (struct super_block *sb)
293 kfree(c->blocks); 300 kfree(c->blocks);
294 jffs2_flash_cleanup(c); 301 jffs2_flash_cleanup(c);
295 kfree(c->inocache_list); 302 kfree(c->inocache_list);
303 jffs2_clear_xattr_subsystem(c);
296 if (c->mtd->sync) 304 if (c->mtd->sync)
297 c->mtd->sync(c->mtd); 305 c->mtd->sync(c->mtd);
298 306
@@ -320,6 +328,18 @@ static int __init init_jffs2_fs(void)
320{ 328{
321 int ret; 329 int ret;
322 330
331 /* Paranoia checks for on-medium structures. If we ask GCC
332 to pack them with __attribute__((packed)) then it _also_
333 assumes that they're not aligned -- so it emits crappy
334 code on some architectures. Ideally we want an attribute
335 which means just 'no padding', without the alignment
336 thing. But GCC doesn't have that -- we have to just
337 hope the structs are the right sizes, instead. */
338 BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
339 BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
340 BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
341 BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
342
323 printk(KERN_INFO "JFFS2 version 2.2." 343 printk(KERN_INFO "JFFS2 version 2.2."
324#ifdef CONFIG_JFFS2_FS_WRITEBUFFER 344#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
325 " (NAND)" 345 " (NAND)"
@@ -327,7 +347,7 @@ static int __init init_jffs2_fs(void)
327#ifdef CONFIG_JFFS2_SUMMARY 347#ifdef CONFIG_JFFS2_SUMMARY
328 " (SUMMARY) " 348 " (SUMMARY) "
329#endif 349#endif
330 " (C) 2001-2003 Red Hat, Inc.\n"); 350 " (C) 2001-2006 Red Hat, Inc.\n");
331 351
332 jffs2_inode_cachep = kmem_cache_create("jffs2_i", 352 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
333 sizeof(struct jffs2_inode_info), 353 sizeof(struct jffs2_inode_info),
diff --git a/fs/jffs2/symlink.c b/fs/jffs2/symlink.c
index d55754fe8925..fc211b6e9b03 100644
--- a/fs/jffs2/symlink.c
+++ b/fs/jffs2/symlink.c
@@ -24,7 +24,12 @@ struct inode_operations jffs2_symlink_inode_operations =
24{ 24{
25 .readlink = generic_readlink, 25 .readlink = generic_readlink,
26 .follow_link = jffs2_follow_link, 26 .follow_link = jffs2_follow_link,
27 .setattr = jffs2_setattr 27 .permission = jffs2_permission,
28 .setattr = jffs2_setattr,
29 .setxattr = jffs2_setxattr,
30 .getxattr = jffs2_getxattr,
31 .listxattr = jffs2_listxattr,
32 .removexattr = jffs2_removexattr
28}; 33};
29 34
30static void *jffs2_follow_link(struct dentry *dentry, struct nameidata *nd) 35static void *jffs2_follow_link(struct dentry *dentry, struct nameidata *nd)
diff --git a/fs/jffs2/wbuf.c b/fs/jffs2/wbuf.c
index 4cebf0e57c46..b9b700730dfe 100644
--- a/fs/jffs2/wbuf.c
+++ b/fs/jffs2/wbuf.c
@@ -156,69 +156,130 @@ static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock
156 jffs2_erase_pending_trigger(c); 156 jffs2_erase_pending_trigger(c);
157 } 157 }
158 158
159 /* Adjust its size counts accordingly */ 159 if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) {
160 c->wasted_size += jeb->free_size; 160 uint32_t oldfree = jeb->free_size;
161 c->free_size -= jeb->free_size; 161
162 jeb->wasted_size += jeb->free_size; 162 jffs2_link_node_ref(c, jeb,
163 jeb->free_size = 0; 163 (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE,
164 oldfree, NULL);
165 /* convert to wasted */
166 c->wasted_size += oldfree;
167 jeb->wasted_size += oldfree;
168 c->dirty_size -= oldfree;
169 jeb->dirty_size -= oldfree;
170 }
164 171
165 jffs2_dbg_dump_block_lists_nolock(c); 172 jffs2_dbg_dump_block_lists_nolock(c);
166 jffs2_dbg_acct_sanity_check_nolock(c,jeb); 173 jffs2_dbg_acct_sanity_check_nolock(c,jeb);
167 jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 174 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
168} 175}
169 176
177static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c,
178 struct jffs2_inode_info *f,
179 struct jffs2_raw_node_ref *raw,
180 union jffs2_node_union *node)
181{
182 struct jffs2_node_frag *frag;
183 struct jffs2_full_dirent *fd;
184
185 dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n",
186 node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype));
187
188 BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 &&
189 je16_to_cpu(node->u.magic) != 0);
190
191 switch (je16_to_cpu(node->u.nodetype)) {
192 case JFFS2_NODETYPE_INODE:
193 if (f->metadata && f->metadata->raw == raw) {
194 dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata);
195 return &f->metadata->raw;
196 }
197 frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset));
198 BUG_ON(!frag);
199 /* Find a frag which refers to the full_dnode we want to modify */
200 while (!frag->node || frag->node->raw != raw) {
201 frag = frag_next(frag);
202 BUG_ON(!frag);
203 }
204 dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node);
205 return &frag->node->raw;
206
207 case JFFS2_NODETYPE_DIRENT:
208 for (fd = f->dents; fd; fd = fd->next) {
209 if (fd->raw == raw) {
210 dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd);
211 return &fd->raw;
212 }
213 }
214 BUG();
215
216 default:
217 dbg_noderef("Don't care about replacing raw for nodetype %x\n",
218 je16_to_cpu(node->u.nodetype));
219 break;
220 }
221 return NULL;
222}
223
170/* Recover from failure to write wbuf. Recover the nodes up to the 224/* Recover from failure to write wbuf. Recover the nodes up to the
171 * wbuf, not the one which we were starting to try to write. */ 225 * wbuf, not the one which we were starting to try to write. */
172 226
173static void jffs2_wbuf_recover(struct jffs2_sb_info *c) 227static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
174{ 228{
175 struct jffs2_eraseblock *jeb, *new_jeb; 229 struct jffs2_eraseblock *jeb, *new_jeb;
176 struct jffs2_raw_node_ref **first_raw, **raw; 230 struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL;
177 size_t retlen; 231 size_t retlen;
178 int ret; 232 int ret;
233 int nr_refile = 0;
179 unsigned char *buf; 234 unsigned char *buf;
180 uint32_t start, end, ofs, len; 235 uint32_t start, end, ofs, len;
181 236
182 spin_lock(&c->erase_completion_lock);
183
184 jeb = &c->blocks[c->wbuf_ofs / c->sector_size]; 237 jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
185 238
239 spin_lock(&c->erase_completion_lock);
186 jffs2_block_refile(c, jeb, REFILE_NOTEMPTY); 240 jffs2_block_refile(c, jeb, REFILE_NOTEMPTY);
241 spin_unlock(&c->erase_completion_lock);
242
243 BUG_ON(!ref_obsolete(jeb->last_node));
187 244
188 /* Find the first node to be recovered, by skipping over every 245 /* Find the first node to be recovered, by skipping over every
189 node which ends before the wbuf starts, or which is obsolete. */ 246 node which ends before the wbuf starts, or which is obsolete. */
190 first_raw = &jeb->first_node; 247 for (next = raw = jeb->first_node; next; raw = next) {
191 while (*first_raw && 248 next = ref_next(raw);
192 (ref_obsolete(*first_raw) || 249
193 (ref_offset(*first_raw)+ref_totlen(c, jeb, *first_raw)) < c->wbuf_ofs)) { 250 if (ref_obsolete(raw) ||
194 D1(printk(KERN_DEBUG "Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n", 251 (next && ref_offset(next) <= c->wbuf_ofs)) {
195 ref_offset(*first_raw), ref_flags(*first_raw), 252 dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
196 (ref_offset(*first_raw) + ref_totlen(c, jeb, *first_raw)), 253 ref_offset(raw), ref_flags(raw),
197 c->wbuf_ofs)); 254 (ref_offset(raw) + ref_totlen(c, jeb, raw)),
198 first_raw = &(*first_raw)->next_phys; 255 c->wbuf_ofs);
256 continue;
257 }
258 dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n",
259 ref_offset(raw), ref_flags(raw),
260 (ref_offset(raw) + ref_totlen(c, jeb, raw)));
261
262 first_raw = raw;
263 break;
199 } 264 }
200 265
201 if (!*first_raw) { 266 if (!first_raw) {
202 /* All nodes were obsolete. Nothing to recover. */ 267 /* All nodes were obsolete. Nothing to recover. */
203 D1(printk(KERN_DEBUG "No non-obsolete nodes to be recovered. Just filing block bad\n")); 268 D1(printk(KERN_DEBUG "No non-obsolete nodes to be recovered. Just filing block bad\n"));
204 spin_unlock(&c->erase_completion_lock); 269 c->wbuf_len = 0;
205 return; 270 return;
206 } 271 }
207 272
208 start = ref_offset(*first_raw); 273 start = ref_offset(first_raw);
209 end = ref_offset(*first_raw) + ref_totlen(c, jeb, *first_raw); 274 end = ref_offset(jeb->last_node);
210 275 nr_refile = 1;
211 /* Find the last node to be recovered */
212 raw = first_raw;
213 while ((*raw)) {
214 if (!ref_obsolete(*raw))
215 end = ref_offset(*raw) + ref_totlen(c, jeb, *raw);
216 276
217 raw = &(*raw)->next_phys; 277 /* Count the number of refs which need to be copied */
218 } 278 while ((raw = ref_next(raw)) != jeb->last_node)
219 spin_unlock(&c->erase_completion_lock); 279 nr_refile++;
220 280
221 D1(printk(KERN_DEBUG "wbuf recover %08x-%08x\n", start, end)); 281 dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
282 start, end, end - start, nr_refile);
222 283
223 buf = NULL; 284 buf = NULL;
224 if (start < c->wbuf_ofs) { 285 if (start < c->wbuf_ofs) {
@@ -233,28 +294,37 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
233 } 294 }
234 295
235 /* Do the read... */ 296 /* Do the read... */
236 if (jffs2_cleanmarker_oob(c)) 297 ret = c->mtd->read(c->mtd, start, c->wbuf_ofs - start, &retlen, buf);
237 ret = c->mtd->read_ecc(c->mtd, start, c->wbuf_ofs - start, &retlen, buf, NULL, c->oobinfo);
238 else
239 ret = c->mtd->read(c->mtd, start, c->wbuf_ofs - start, &retlen, buf);
240 298
241 if (ret == -EBADMSG && retlen == c->wbuf_ofs - start) { 299 /* ECC recovered ? */
242 /* ECC recovered */ 300 if ((ret == -EUCLEAN || ret == -EBADMSG) &&
301 (retlen == c->wbuf_ofs - start))
243 ret = 0; 302 ret = 0;
244 } 303
245 if (ret || retlen != c->wbuf_ofs - start) { 304 if (ret || retlen != c->wbuf_ofs - start) {
246 printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n"); 305 printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n");
247 306
248 kfree(buf); 307 kfree(buf);
249 buf = NULL; 308 buf = NULL;
250 read_failed: 309 read_failed:
251 first_raw = &(*first_raw)->next_phys; 310 first_raw = ref_next(first_raw);
311 nr_refile--;
312 while (first_raw && ref_obsolete(first_raw)) {
313 first_raw = ref_next(first_raw);
314 nr_refile--;
315 }
316
252 /* If this was the only node to be recovered, give up */ 317 /* If this was the only node to be recovered, give up */
253 if (!(*first_raw)) 318 if (!first_raw) {
319 c->wbuf_len = 0;
254 return; 320 return;
321 }
255 322
256 /* It wasn't. Go on and try to recover nodes complete in the wbuf */ 323 /* It wasn't. Go on and try to recover nodes complete in the wbuf */
257 start = ref_offset(*first_raw); 324 start = ref_offset(first_raw);
325 dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
326 start, end, end - start, nr_refile);
327
258 } else { 328 } else {
259 /* Read succeeded. Copy the remaining data from the wbuf */ 329 /* Read succeeded. Copy the remaining data from the wbuf */
260 memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs); 330 memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
@@ -263,14 +333,23 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
263 /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards. 333 /* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
264 Either 'buf' contains the data, or we find it in the wbuf */ 334 Either 'buf' contains the data, or we find it in the wbuf */
265 335
266
267 /* ... and get an allocation of space from a shiny new block instead */ 336 /* ... and get an allocation of space from a shiny new block instead */
268 ret = jffs2_reserve_space_gc(c, end-start, &ofs, &len, JFFS2_SUMMARY_NOSUM_SIZE); 337 ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE);
269 if (ret) { 338 if (ret) {
270 printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n"); 339 printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n");
271 kfree(buf); 340 kfree(buf);
272 return; 341 return;
273 } 342 }
343
344 ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile);
345 if (ret) {
346 printk(KERN_WARNING "Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
347 kfree(buf);
348 return;
349 }
350
351 ofs = write_ofs(c);
352
274 if (end-start >= c->wbuf_pagesize) { 353 if (end-start >= c->wbuf_pagesize) {
275 /* Need to do another write immediately, but it's possible 354 /* Need to do another write immediately, but it's possible
276 that this is just because the wbuf itself is completely 355 that this is just because the wbuf itself is completely
@@ -288,36 +367,22 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
288 if (breakme++ == 20) { 367 if (breakme++ == 20) {
289 printk(KERN_NOTICE "Faking write error at 0x%08x\n", ofs); 368 printk(KERN_NOTICE "Faking write error at 0x%08x\n", ofs);
290 breakme = 0; 369 breakme = 0;
291 c->mtd->write_ecc(c->mtd, ofs, towrite, &retlen, 370 c->mtd->write(c->mtd, ofs, towrite, &retlen,
292 brokenbuf, NULL, c->oobinfo); 371 brokenbuf);
293 ret = -EIO; 372 ret = -EIO;
294 } else 373 } else
295#endif 374#endif
296 if (jffs2_cleanmarker_oob(c)) 375 ret = c->mtd->write(c->mtd, ofs, towrite, &retlen,
297 ret = c->mtd->write_ecc(c->mtd, ofs, towrite, &retlen, 376 rewrite_buf);
298 rewrite_buf, NULL, c->oobinfo);
299 else
300 ret = c->mtd->write(c->mtd, ofs, towrite, &retlen, rewrite_buf);
301 377
302 if (ret || retlen != towrite) { 378 if (ret || retlen != towrite) {
303 /* Argh. We tried. Really we did. */ 379 /* Argh. We tried. Really we did. */
304 printk(KERN_CRIT "Recovery of wbuf failed due to a second write error\n"); 380 printk(KERN_CRIT "Recovery of wbuf failed due to a second write error\n");
305 kfree(buf); 381 kfree(buf);
306 382
307 if (retlen) { 383 if (retlen)
308 struct jffs2_raw_node_ref *raw2; 384 jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
309
310 raw2 = jffs2_alloc_raw_node_ref();
311 if (!raw2)
312 return;
313 385
314 raw2->flash_offset = ofs | REF_OBSOLETE;
315 raw2->__totlen = ref_totlen(c, jeb, *first_raw);
316 raw2->next_phys = NULL;
317 raw2->next_in_ino = NULL;
318
319 jffs2_add_physical_node_ref(c, raw2);
320 }
321 return; 386 return;
322 } 387 }
323 printk(KERN_NOTICE "Recovery of wbuf succeeded to %08x\n", ofs); 388 printk(KERN_NOTICE "Recovery of wbuf succeeded to %08x\n", ofs);
@@ -326,12 +391,10 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
326 c->wbuf_ofs = ofs + towrite; 391 c->wbuf_ofs = ofs + towrite;
327 memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len); 392 memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
328 /* Don't muck about with c->wbuf_inodes. False positives are harmless. */ 393 /* Don't muck about with c->wbuf_inodes. False positives are harmless. */
329 kfree(buf);
330 } else { 394 } else {
331 /* OK, now we're left with the dregs in whichever buffer we're using */ 395 /* OK, now we're left with the dregs in whichever buffer we're using */
332 if (buf) { 396 if (buf) {
333 memcpy(c->wbuf, buf, end-start); 397 memcpy(c->wbuf, buf, end-start);
334 kfree(buf);
335 } else { 398 } else {
336 memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start); 399 memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
337 } 400 }
@@ -343,62 +406,110 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
343 new_jeb = &c->blocks[ofs / c->sector_size]; 406 new_jeb = &c->blocks[ofs / c->sector_size];
344 407
345 spin_lock(&c->erase_completion_lock); 408 spin_lock(&c->erase_completion_lock);
346 if (new_jeb->first_node) { 409 for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) {
347 /* Odd, but possible with ST flash later maybe */ 410 uint32_t rawlen = ref_totlen(c, jeb, raw);
348 new_jeb->last_node->next_phys = *first_raw; 411 struct jffs2_inode_cache *ic;
349 } else { 412 struct jffs2_raw_node_ref *new_ref;
350 new_jeb->first_node = *first_raw; 413 struct jffs2_raw_node_ref **adjust_ref = NULL;
351 } 414 struct jffs2_inode_info *f = NULL;
352
353 raw = first_raw;
354 while (*raw) {
355 uint32_t rawlen = ref_totlen(c, jeb, *raw);
356 415
357 D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n", 416 D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n",
358 rawlen, ref_offset(*raw), ref_flags(*raw), ofs)); 417 rawlen, ref_offset(raw), ref_flags(raw), ofs));
418
419 ic = jffs2_raw_ref_to_ic(raw);
420
421 /* Ick. This XATTR mess should be fixed shortly... */
422 if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) {
423 struct jffs2_xattr_datum *xd = (void *)ic;
424 BUG_ON(xd->node != raw);
425 adjust_ref = &xd->node;
426 raw->next_in_ino = NULL;
427 ic = NULL;
428 } else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) {
429 struct jffs2_xattr_datum *xr = (void *)ic;
430 BUG_ON(xr->node != raw);
431 adjust_ref = &xr->node;
432 raw->next_in_ino = NULL;
433 ic = NULL;
434 } else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) {
435 struct jffs2_raw_node_ref **p = &ic->nodes;
436
437 /* Remove the old node from the per-inode list */
438 while (*p && *p != (void *)ic) {
439 if (*p == raw) {
440 (*p) = (raw->next_in_ino);
441 raw->next_in_ino = NULL;
442 break;
443 }
444 p = &((*p)->next_in_ino);
445 }
359 446
360 if (ref_obsolete(*raw)) { 447 if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) {
361 /* Shouldn't really happen much */ 448 /* If it's an in-core inode, then we have to adjust any
362 new_jeb->dirty_size += rawlen; 449 full_dirent or full_dnode structure to point to the
363 new_jeb->free_size -= rawlen; 450 new version instead of the old */
364 c->dirty_size += rawlen; 451 f = jffs2_gc_fetch_inode(c, ic->ino, ic->nlink);
365 } else { 452 if (IS_ERR(f)) {
366 new_jeb->used_size += rawlen; 453 /* Should never happen; it _must_ be present */
367 new_jeb->free_size -= rawlen; 454 JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n",
455 ic->ino, PTR_ERR(f));
456 BUG();
457 }
458 /* We don't lock f->sem. There's a number of ways we could
459 end up in here with it already being locked, and nobody's
460 going to modify it on us anyway because we hold the
461 alloc_sem. We're only changing one ->raw pointer too,
462 which we can get away with without upsetting readers. */
463 adjust_ref = jffs2_incore_replace_raw(c, f, raw,
464 (void *)(buf?:c->wbuf) + (ref_offset(raw) - start));
465 } else if (unlikely(ic->state != INO_STATE_PRESENT &&
466 ic->state != INO_STATE_CHECKEDABSENT &&
467 ic->state != INO_STATE_GC)) {
468 JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state);
469 BUG();
470 }
471 }
472
473 new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic);
474
475 if (adjust_ref) {
476 BUG_ON(*adjust_ref != raw);
477 *adjust_ref = new_ref;
478 }
479 if (f)
480 jffs2_gc_release_inode(c, f);
481
482 if (!ref_obsolete(raw)) {
368 jeb->dirty_size += rawlen; 483 jeb->dirty_size += rawlen;
369 jeb->used_size -= rawlen; 484 jeb->used_size -= rawlen;
370 c->dirty_size += rawlen; 485 c->dirty_size += rawlen;
486 c->used_size -= rawlen;
487 raw->flash_offset = ref_offset(raw) | REF_OBSOLETE;
488 BUG_ON(raw->next_in_ino);
371 } 489 }
372 c->free_size -= rawlen;
373 (*raw)->flash_offset = ofs | ref_flags(*raw);
374 ofs += rawlen; 490 ofs += rawlen;
375 new_jeb->last_node = *raw;
376
377 raw = &(*raw)->next_phys;
378 } 491 }
379 492
493 kfree(buf);
494
380 /* Fix up the original jeb now it's on the bad_list */ 495 /* Fix up the original jeb now it's on the bad_list */
381 *first_raw = NULL; 496 if (first_raw == jeb->first_node) {
382 if (first_raw == &jeb->first_node) {
383 jeb->last_node = NULL;
384 D1(printk(KERN_DEBUG "Failing block at %08x is now empty. Moving to erase_pending_list\n", jeb->offset)); 497 D1(printk(KERN_DEBUG "Failing block at %08x is now empty. Moving to erase_pending_list\n", jeb->offset));
385 list_del(&jeb->list); 498 list_move(&jeb->list, &c->erase_pending_list);
386 list_add(&jeb->list, &c->erase_pending_list);
387 c->nr_erasing_blocks++; 499 c->nr_erasing_blocks++;
388 jffs2_erase_pending_trigger(c); 500 jffs2_erase_pending_trigger(c);
389 } 501 }
390 else
391 jeb->last_node = container_of(first_raw, struct jffs2_raw_node_ref, next_phys);
392 502
393 jffs2_dbg_acct_sanity_check_nolock(c, jeb); 503 jffs2_dbg_acct_sanity_check_nolock(c, jeb);
394 jffs2_dbg_acct_paranoia_check_nolock(c, jeb); 504 jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
395 505
396 jffs2_dbg_acct_sanity_check_nolock(c, new_jeb); 506 jffs2_dbg_acct_sanity_check_nolock(c, new_jeb);
397 jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb); 507 jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb);
398 508
399 spin_unlock(&c->erase_completion_lock); 509 spin_unlock(&c->erase_completion_lock);
400 510
401 D1(printk(KERN_DEBUG "wbuf recovery completed OK\n")); 511 D1(printk(KERN_DEBUG "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n", c->wbuf_ofs, c->wbuf_len));
512
402} 513}
403 514
404/* Meaning of pad argument: 515/* Meaning of pad argument:
@@ -412,6 +523,7 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
412 523
413static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad) 524static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
414{ 525{
526 struct jffs2_eraseblock *wbuf_jeb;
415 int ret; 527 int ret;
416 size_t retlen; 528 size_t retlen;
417 529
@@ -429,6 +541,10 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
429 if (!c->wbuf_len) /* already checked c->wbuf above */ 541 if (!c->wbuf_len) /* already checked c->wbuf above */
430 return 0; 542 return 0;
431 543
544 wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
545 if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1))
546 return -ENOMEM;
547
432 /* claim remaining space on the page 548 /* claim remaining space on the page
433 this happens, if we have a change to a new block, 549 this happens, if we have a change to a new block,
434 or if fsync forces us to flush the writebuffer. 550 or if fsync forces us to flush the writebuffer.
@@ -458,15 +574,12 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
458 if (breakme++ == 20) { 574 if (breakme++ == 20) {
459 printk(KERN_NOTICE "Faking write error at 0x%08x\n", c->wbuf_ofs); 575 printk(KERN_NOTICE "Faking write error at 0x%08x\n", c->wbuf_ofs);
460 breakme = 0; 576 breakme = 0;
461 c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, 577 c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen,
462 &retlen, brokenbuf, NULL, c->oobinfo); 578 brokenbuf);
463 ret = -EIO; 579 ret = -EIO;
464 } else 580 } else
465#endif 581#endif
466 582
467 if (jffs2_cleanmarker_oob(c))
468 ret = c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf, NULL, c->oobinfo);
469 else
470 ret = c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf); 583 ret = c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf);
471 584
472 if (ret || retlen != c->wbuf_pagesize) { 585 if (ret || retlen != c->wbuf_pagesize) {
@@ -483,32 +596,34 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
483 return ret; 596 return ret;
484 } 597 }
485 598
486 spin_lock(&c->erase_completion_lock);
487
488 /* Adjust free size of the block if we padded. */ 599 /* Adjust free size of the block if we padded. */
489 if (pad) { 600 if (pad) {
490 struct jffs2_eraseblock *jeb; 601 uint32_t waste = c->wbuf_pagesize - c->wbuf_len;
491
492 jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
493 602
494 D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n", 603 D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
495 (jeb==c->nextblock)?"next":"", jeb->offset)); 604 (wbuf_jeb==c->nextblock)?"next":"", wbuf_jeb->offset));
496 605
497 /* wbuf_pagesize - wbuf_len is the amount of space that's to be 606 /* wbuf_pagesize - wbuf_len is the amount of space that's to be
498 padded. If there is less free space in the block than that, 607 padded. If there is less free space in the block than that,
499 something screwed up */ 608 something screwed up */
500 if (jeb->free_size < (c->wbuf_pagesize - c->wbuf_len)) { 609 if (wbuf_jeb->free_size < waste) {
501 printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n", 610 printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
502 c->wbuf_ofs, c->wbuf_len, c->wbuf_pagesize-c->wbuf_len); 611 c->wbuf_ofs, c->wbuf_len, waste);
503 printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n", 612 printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
504 jeb->offset, jeb->free_size); 613 wbuf_jeb->offset, wbuf_jeb->free_size);
505 BUG(); 614 BUG();
506 } 615 }
507 jeb->free_size -= (c->wbuf_pagesize - c->wbuf_len); 616
508 c->free_size -= (c->wbuf_pagesize - c->wbuf_len); 617 spin_lock(&c->erase_completion_lock);
509 jeb->wasted_size += (c->wbuf_pagesize - c->wbuf_len); 618
510 c->wasted_size += (c->wbuf_pagesize - c->wbuf_len); 619 jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL);
511 } 620 /* FIXME: that made it count as dirty. Convert to wasted */
621 wbuf_jeb->dirty_size -= waste;
622 c->dirty_size -= waste;
623 wbuf_jeb->wasted_size += waste;
624 c->wasted_size += waste;
625 } else
626 spin_lock(&c->erase_completion_lock);
512 627
513 /* Stick any now-obsoleted blocks on the erase_pending_list */ 628 /* Stick any now-obsoleted blocks on the erase_pending_list */
514 jffs2_refile_wbuf_blocks(c); 629 jffs2_refile_wbuf_blocks(c);
@@ -603,20 +718,30 @@ int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
603 718
604 return ret; 719 return ret;
605} 720}
606int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsigned long count, loff_t to, size_t *retlen, uint32_t ino) 721
722static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf,
723 size_t len)
724{
725 if (len && !c->wbuf_len && (len >= c->wbuf_pagesize))
726 return 0;
727
728 if (len > (c->wbuf_pagesize - c->wbuf_len))
729 len = c->wbuf_pagesize - c->wbuf_len;
730 memcpy(c->wbuf + c->wbuf_len, buf, len);
731 c->wbuf_len += (uint32_t) len;
732 return len;
733}
734
735int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs,
736 unsigned long count, loff_t to, size_t *retlen,
737 uint32_t ino)
607{ 738{
608 struct kvec outvecs[3]; 739 struct jffs2_eraseblock *jeb;
609 uint32_t totlen = 0; 740 size_t wbuf_retlen, donelen = 0;
610 uint32_t split_ofs = 0;
611 uint32_t old_totlen;
612 int ret, splitvec = -1;
613 int invec, outvec;
614 size_t wbuf_retlen;
615 unsigned char *wbuf_ptr;
616 size_t donelen = 0;
617 uint32_t outvec_to = to; 741 uint32_t outvec_to = to;
742 int ret, invec;
618 743
619 /* If not NAND flash, don't bother */ 744 /* If not writebuffered flash, don't bother */
620 if (!jffs2_is_writebuffered(c)) 745 if (!jffs2_is_writebuffered(c))
621 return jffs2_flash_direct_writev(c, invecs, count, to, retlen); 746 return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
622 747
@@ -629,34 +754,22 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
629 memset(c->wbuf,0xff,c->wbuf_pagesize); 754 memset(c->wbuf,0xff,c->wbuf_pagesize);
630 } 755 }
631 756
632 /* Fixup the wbuf if we are moving to a new eraseblock. The checks below 757 /*
633 fail for ECC'd NOR because cleanmarker == 16, so a block starts at 758 * Sanity checks on target address. It's permitted to write
634 xxx0010. */ 759 * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to
635 if (jffs2_nor_ecc(c)) { 760 * write at the beginning of a new erase block. Anything else,
636 if (((c->wbuf_ofs % c->sector_size) == 0) && !c->wbuf_len) { 761 * and you die. New block starts at xxx000c (0-b = block
637 c->wbuf_ofs = PAGE_DIV(to); 762 * header)
638 c->wbuf_len = PAGE_MOD(to); 763 */
639 memset(c->wbuf,0xff,c->wbuf_pagesize);
640 }
641 }
642
643 /* Sanity checks on target address.
644 It's permitted to write at PAD(c->wbuf_len+c->wbuf_ofs),
645 and it's permitted to write at the beginning of a new
646 erase block. Anything else, and you die.
647 New block starts at xxx000c (0-b = block header)
648 */
649 if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) { 764 if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) {
650 /* It's a write to a new block */ 765 /* It's a write to a new block */
651 if (c->wbuf_len) { 766 if (c->wbuf_len) {
652 D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx causes flush of wbuf at 0x%08x\n", (unsigned long)to, c->wbuf_ofs)); 767 D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx "
768 "causes flush of wbuf at 0x%08x\n",
769 (unsigned long)to, c->wbuf_ofs));
653 ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT); 770 ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
654 if (ret) { 771 if (ret)
655 /* the underlying layer has to check wbuf_len to do the cleanup */ 772 goto outerr;
656 D1(printk(KERN_WARNING "jffs2_flush_wbuf() called from jffs2_flash_writev() failed %d\n", ret));
657 *retlen = 0;
658 goto exit;
659 }
660 } 773 }
661 /* set pointer to new block */ 774 /* set pointer to new block */
662 c->wbuf_ofs = PAGE_DIV(to); 775 c->wbuf_ofs = PAGE_DIV(to);
@@ -665,165 +778,70 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
665 778
666 if (to != PAD(c->wbuf_ofs + c->wbuf_len)) { 779 if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
667 /* We're not writing immediately after the writebuffer. Bad. */ 780 /* We're not writing immediately after the writebuffer. Bad. */
668 printk(KERN_CRIT "jffs2_flash_writev(): Non-contiguous write to %08lx\n", (unsigned long)to); 781 printk(KERN_CRIT "jffs2_flash_writev(): Non-contiguous write "
782 "to %08lx\n", (unsigned long)to);
669 if (c->wbuf_len) 783 if (c->wbuf_len)
670 printk(KERN_CRIT "wbuf was previously %08x-%08x\n", 784 printk(KERN_CRIT "wbuf was previously %08x-%08x\n",
671 c->wbuf_ofs, c->wbuf_ofs+c->wbuf_len); 785 c->wbuf_ofs, c->wbuf_ofs+c->wbuf_len);
672 BUG(); 786 BUG();
673 } 787 }
674 788
675 /* Note outvecs[3] above. We know count is never greater than 2 */ 789 /* adjust alignment offset */
676 if (count > 2) { 790 if (c->wbuf_len != PAGE_MOD(to)) {
677 printk(KERN_CRIT "jffs2_flash_writev(): count is %ld\n", count); 791 c->wbuf_len = PAGE_MOD(to);
678 BUG(); 792 /* take care of alignment to next page */
679 } 793 if (!c->wbuf_len) {
680 794 c->wbuf_len = c->wbuf_pagesize;
681 invec = 0; 795 ret = __jffs2_flush_wbuf(c, NOPAD);
682 outvec = 0; 796 if (ret)
683 797 goto outerr;
684 /* Fill writebuffer first, if already in use */
685 if (c->wbuf_len) {
686 uint32_t invec_ofs = 0;
687
688 /* adjust alignment offset */
689 if (c->wbuf_len != PAGE_MOD(to)) {
690 c->wbuf_len = PAGE_MOD(to);
691 /* take care of alignment to next page */
692 if (!c->wbuf_len)
693 c->wbuf_len = c->wbuf_pagesize;
694 }
695
696 while(c->wbuf_len < c->wbuf_pagesize) {
697 uint32_t thislen;
698
699 if (invec == count)
700 goto alldone;
701
702 thislen = c->wbuf_pagesize - c->wbuf_len;
703
704 if (thislen >= invecs[invec].iov_len)
705 thislen = invecs[invec].iov_len;
706
707 invec_ofs = thislen;
708
709 memcpy(c->wbuf + c->wbuf_len, invecs[invec].iov_base, thislen);
710 c->wbuf_len += thislen;
711 donelen += thislen;
712 /* Get next invec, if actual did not fill the buffer */
713 if (c->wbuf_len < c->wbuf_pagesize)
714 invec++;
715 }
716
717 /* write buffer is full, flush buffer */
718 ret = __jffs2_flush_wbuf(c, NOPAD);
719 if (ret) {
720 /* the underlying layer has to check wbuf_len to do the cleanup */
721 D1(printk(KERN_WARNING "jffs2_flush_wbuf() called from jffs2_flash_writev() failed %d\n", ret));
722 /* Retlen zero to make sure our caller doesn't mark the space dirty.
723 We've already done everything that's necessary */
724 *retlen = 0;
725 goto exit;
726 }
727 outvec_to += donelen;
728 c->wbuf_ofs = outvec_to;
729
730 /* All invecs done ? */
731 if (invec == count)
732 goto alldone;
733
734 /* Set up the first outvec, containing the remainder of the
735 invec we partially used */
736 if (invecs[invec].iov_len > invec_ofs) {
737 outvecs[0].iov_base = invecs[invec].iov_base+invec_ofs;
738 totlen = outvecs[0].iov_len = invecs[invec].iov_len-invec_ofs;
739 if (totlen > c->wbuf_pagesize) {
740 splitvec = outvec;
741 split_ofs = outvecs[0].iov_len - PAGE_MOD(totlen);
742 }
743 outvec++;
744 }
745 invec++;
746 }
747
748 /* OK, now we've flushed the wbuf and the start of the bits
749 we have been asked to write, now to write the rest.... */
750
751 /* totlen holds the amount of data still to be written */
752 old_totlen = totlen;
753 for ( ; invec < count; invec++,outvec++ ) {
754 outvecs[outvec].iov_base = invecs[invec].iov_base;
755 totlen += outvecs[outvec].iov_len = invecs[invec].iov_len;
756 if (PAGE_DIV(totlen) != PAGE_DIV(old_totlen)) {
757 splitvec = outvec;
758 split_ofs = outvecs[outvec].iov_len - PAGE_MOD(totlen);
759 old_totlen = totlen;
760 } 798 }
761 } 799 }
762 800
763 /* Now the outvecs array holds all the remaining data to write */ 801 for (invec = 0; invec < count; invec++) {
764 /* Up to splitvec,split_ofs is to be written immediately. The rest 802 int vlen = invecs[invec].iov_len;
765 goes into the (now-empty) wbuf */ 803 uint8_t *v = invecs[invec].iov_base;
766
767 if (splitvec != -1) {
768 uint32_t remainder;
769
770 remainder = outvecs[splitvec].iov_len - split_ofs;
771 outvecs[splitvec].iov_len = split_ofs;
772
773 /* We did cross a page boundary, so we write some now */
774 if (jffs2_cleanmarker_oob(c))
775 ret = c->mtd->writev_ecc(c->mtd, outvecs, splitvec+1, outvec_to, &wbuf_retlen, NULL, c->oobinfo);
776 else
777 ret = jffs2_flash_direct_writev(c, outvecs, splitvec+1, outvec_to, &wbuf_retlen);
778 804
779 if (ret < 0 || wbuf_retlen != PAGE_DIV(totlen)) { 805 wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
780 /* At this point we have no problem,
781 c->wbuf is empty. However refile nextblock to avoid
782 writing again to same address.
783 */
784 struct jffs2_eraseblock *jeb;
785 806
786 spin_lock(&c->erase_completion_lock); 807 if (c->wbuf_len == c->wbuf_pagesize) {
787 808 ret = __jffs2_flush_wbuf(c, NOPAD);
788 jeb = &c->blocks[outvec_to / c->sector_size]; 809 if (ret)
789 jffs2_block_refile(c, jeb, REFILE_ANYWAY); 810 goto outerr;
790
791 *retlen = 0;
792 spin_unlock(&c->erase_completion_lock);
793 goto exit;
794 } 811 }
795 812 vlen -= wbuf_retlen;
813 outvec_to += wbuf_retlen;
796 donelen += wbuf_retlen; 814 donelen += wbuf_retlen;
797 c->wbuf_ofs = PAGE_DIV(outvec_to) + PAGE_DIV(totlen); 815 v += wbuf_retlen;
798 816
799 if (remainder) { 817 if (vlen >= c->wbuf_pagesize) {
800 outvecs[splitvec].iov_base += split_ofs; 818 ret = c->mtd->write(c->mtd, outvec_to, PAGE_DIV(vlen),
801 outvecs[splitvec].iov_len = remainder; 819 &wbuf_retlen, v);
802 } else { 820 if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen))
803 splitvec++; 821 goto outfile;
822
823 vlen -= wbuf_retlen;
824 outvec_to += wbuf_retlen;
825 c->wbuf_ofs = outvec_to;
826 donelen += wbuf_retlen;
827 v += wbuf_retlen;
804 } 828 }
805 829
806 } else { 830 wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
807 splitvec = 0; 831 if (c->wbuf_len == c->wbuf_pagesize) {
808 } 832 ret = __jffs2_flush_wbuf(c, NOPAD);
809 833 if (ret)
810 /* Now splitvec points to the start of the bits we have to copy 834 goto outerr;
811 into the wbuf */ 835 }
812 wbuf_ptr = c->wbuf;
813 836
814 for ( ; splitvec < outvec; splitvec++) { 837 outvec_to += wbuf_retlen;
815 /* Don't copy the wbuf into itself */ 838 donelen += wbuf_retlen;
816 if (outvecs[splitvec].iov_base == c->wbuf)
817 continue;
818 memcpy(wbuf_ptr, outvecs[splitvec].iov_base, outvecs[splitvec].iov_len);
819 wbuf_ptr += outvecs[splitvec].iov_len;
820 donelen += outvecs[splitvec].iov_len;
821 } 839 }
822 c->wbuf_len = wbuf_ptr - c->wbuf;
823 840
824 /* If there's a remainder in the wbuf and it's a non-GC write, 841 /*
825 remember that the wbuf affects this ino */ 842 * If there's a remainder in the wbuf and it's a non-GC write,
826alldone: 843 * remember that the wbuf affects this ino
844 */
827 *retlen = donelen; 845 *retlen = donelen;
828 846
829 if (jffs2_sum_active()) { 847 if (jffs2_sum_active()) {
@@ -836,8 +854,24 @@ alldone:
836 jffs2_wbuf_dirties_inode(c, ino); 854 jffs2_wbuf_dirties_inode(c, ino);
837 855
838 ret = 0; 856 ret = 0;
857 up_write(&c->wbuf_sem);
858 return ret;
839 859
840exit: 860outfile:
861 /*
862 * At this point we have no problem, c->wbuf is empty. However
863 * refile nextblock to avoid writing again to same address.
864 */
865
866 spin_lock(&c->erase_completion_lock);
867
868 jeb = &c->blocks[outvec_to / c->sector_size];
869 jffs2_block_refile(c, jeb, REFILE_ANYWAY);
870
871 spin_unlock(&c->erase_completion_lock);
872
873outerr:
874 *retlen = 0;
841 up_write(&c->wbuf_sem); 875 up_write(&c->wbuf_sem);
842 return ret; 876 return ret;
843} 877}
@@ -846,7 +880,8 @@ exit:
846 * This is the entry for flash write. 880 * This is the entry for flash write.
847 * Check, if we work on NAND FLASH, if so build an kvec and write it via vritev 881 * Check, if we work on NAND FLASH, if so build an kvec and write it via vritev
848*/ 882*/
849int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, const u_char *buf) 883int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
884 size_t *retlen, const u_char *buf)
850{ 885{
851 struct kvec vecs[1]; 886 struct kvec vecs[1];
852 887
@@ -871,25 +906,23 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re
871 906
872 /* Read flash */ 907 /* Read flash */
873 down_read(&c->wbuf_sem); 908 down_read(&c->wbuf_sem);
874 if (jffs2_cleanmarker_oob(c)) 909 ret = c->mtd->read(c->mtd, ofs, len, retlen, buf);
875 ret = c->mtd->read_ecc(c->mtd, ofs, len, retlen, buf, NULL, c->oobinfo); 910
876 else 911 if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {
877 ret = c->mtd->read(c->mtd, ofs, len, retlen, buf); 912 if (ret == -EBADMSG)
878 913 printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx)"
879 if ( (ret == -EBADMSG) && (*retlen == len) ) { 914 " returned ECC error\n", len, ofs);
880 printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
881 len, ofs);
882 /* 915 /*
883 * We have the raw data without ECC correction in the buffer, maybe 916 * We have the raw data without ECC correction in the buffer,
884 * we are lucky and all data or parts are correct. We check the node. 917 * maybe we are lucky and all data or parts are correct. We
885 * If data are corrupted node check will sort it out. 918 * check the node. If data are corrupted node check will sort
886 * We keep this block, it will fail on write or erase and the we 919 * it out. We keep this block, it will fail on write or erase
887 * mark it bad. Or should we do that now? But we should give him a chance. 920 * and the we mark it bad. Or should we do that now? But we
888 * Maybe we had a system crash or power loss before the ecc write or 921 * should give him a chance. Maybe we had a system crash or
889 * a erase was completed. 922 * power loss before the ecc write or a erase was completed.
890 * So we return success. :) 923 * So we return success. :)
891 */ 924 */
892 ret = 0; 925 ret = 0;
893 } 926 }
894 927
895 /* if no writebuffer available or write buffer empty, return */ 928 /* if no writebuffer available or write buffer empty, return */
@@ -911,7 +944,7 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re
911 orbf = (c->wbuf_ofs - ofs); /* offset in read buffer */ 944 orbf = (c->wbuf_ofs - ofs); /* offset in read buffer */
912 if (orbf > len) /* is write beyond write buffer ? */ 945 if (orbf > len) /* is write beyond write buffer ? */
913 goto exit; 946 goto exit;
914 lwbf = len - orbf; /* number of bytes to copy */ 947 lwbf = len - orbf; /* number of bytes to copy */
915 if (lwbf > c->wbuf_len) 948 if (lwbf > c->wbuf_len)
916 lwbf = c->wbuf_len; 949 lwbf = c->wbuf_len;
917 } 950 }
@@ -923,158 +956,159 @@ exit:
923 return ret; 956 return ret;
924} 957}
925 958
959#define NR_OOB_SCAN_PAGES 4
960
926/* 961/*
927 * Check, if the out of band area is empty 962 * Check, if the out of band area is empty
928 */ 963 */
929int jffs2_check_oob_empty( struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int mode) 964int jffs2_check_oob_empty(struct jffs2_sb_info *c,
965 struct jffs2_eraseblock *jeb, int mode)
930{ 966{
931 unsigned char *buf; 967 int i, page, ret;
932 int ret = 0; 968 int oobsize = c->mtd->oobsize;
933 int i,len,page; 969 struct mtd_oob_ops ops;
934 size_t retlen; 970
935 int oob_size; 971 ops.len = NR_OOB_SCAN_PAGES * oobsize;
936 972 ops.ooblen = oobsize;
937 /* allocate a buffer for all oob data in this sector */ 973 ops.oobbuf = c->oobbuf;
938 oob_size = c->mtd->oobsize; 974 ops.ooboffs = 0;
939 len = 4 * oob_size; 975 ops.datbuf = NULL;
940 buf = kmalloc(len, GFP_KERNEL); 976 ops.mode = MTD_OOB_PLACE;
941 if (!buf) { 977
942 printk(KERN_NOTICE "jffs2_check_oob_empty(): allocation of temporary data buffer for oob check failed\n"); 978 ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
943 return -ENOMEM;
944 }
945 /*
946 * if mode = 0, we scan for a total empty oob area, else we have
947 * to take care of the cleanmarker in the first page of the block
948 */
949 ret = jffs2_flash_read_oob(c, jeb->offset, len , &retlen, buf);
950 if (ret) { 979 if (ret) {
951 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB failed %d for block at %08x\n", ret, jeb->offset)); 980 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
952 goto out; 981 "failed %d for block at %08x\n", ret, jeb->offset));
982 return ret;
953 } 983 }
954 984
955 if (retlen < len) { 985 if (ops.retlen < ops.len) {
956 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB return short read " 986 D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
957 "(%zd bytes not %d) for block at %08x\n", retlen, len, jeb->offset)); 987 "returned short read (%zd bytes not %d) for block "
958 ret = -EIO; 988 "at %08x\n", ops.retlen, ops.len, jeb->offset));
959 goto out; 989 return -EIO;
960 } 990 }
961 991
962 /* Special check for first page */ 992 /* Special check for first page */
963 for(i = 0; i < oob_size ; i++) { 993 for(i = 0; i < oobsize ; i++) {
964 /* Yeah, we know about the cleanmarker. */ 994 /* Yeah, we know about the cleanmarker. */
965 if (mode && i >= c->fsdata_pos && 995 if (mode && i >= c->fsdata_pos &&
966 i < c->fsdata_pos + c->fsdata_len) 996 i < c->fsdata_pos + c->fsdata_len)
967 continue; 997 continue;
968 998
969 if (buf[i] != 0xFF) { 999 if (ops.oobbuf[i] != 0xFF) {
970 D2(printk(KERN_DEBUG "Found %02x at %x in OOB for %08x\n", 1000 D2(printk(KERN_DEBUG "Found %02x at %x in OOB for "
971 buf[i], i, jeb->offset)); 1001 "%08x\n", ops.oobbuf[i], i, jeb->offset));
972 ret = 1; 1002 return 1;
973 goto out;
974 } 1003 }
975 } 1004 }
976 1005
977 /* we know, we are aligned :) */ 1006 /* we know, we are aligned :) */
978 for (page = oob_size; page < len; page += sizeof(long)) { 1007 for (page = oobsize; page < ops.len; page += sizeof(long)) {
979 unsigned long dat = *(unsigned long *)(&buf[page]); 1008 long dat = *(long *)(&ops.oobbuf[page]);
980 if(dat != -1) { 1009 if(dat != -1)
981 ret = 1; 1010 return 1;
982 goto out;
983 }
984 } 1011 }
985 1012 return 0;
986out:
987 kfree(buf);
988
989 return ret;
990} 1013}
991 1014
992/* 1015/*
993* Scan for a valid cleanmarker and for bad blocks 1016 * Scan for a valid cleanmarker and for bad blocks
994* For virtual blocks (concatenated physical blocks) check the cleanmarker 1017 */
995* only in the first page of the first physical block, but scan for bad blocks in all 1018int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c,
996* physical blocks 1019 struct jffs2_eraseblock *jeb)
997*/
998int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
999{ 1020{
1000 struct jffs2_unknown_node n; 1021 struct jffs2_unknown_node n;
1001 unsigned char buf[2 * NAND_MAX_OOBSIZE]; 1022 struct mtd_oob_ops ops;
1002 unsigned char *p; 1023 int oobsize = c->mtd->oobsize;
1003 int ret, i, cnt, retval = 0; 1024 unsigned char *p,*b;
1004 size_t retlen, offset; 1025 int i, ret;
1005 int oob_size; 1026 size_t offset = jeb->offset;
1006 1027
1007 offset = jeb->offset; 1028 /* Check first if the block is bad. */
1008 oob_size = c->mtd->oobsize; 1029 if (c->mtd->block_isbad(c->mtd, offset)) {
1009 1030 D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()"
1010 /* Loop through the physical blocks */ 1031 ": Bad block at %08x\n", jeb->offset));
1011 for (cnt = 0; cnt < (c->sector_size / c->mtd->erasesize); cnt++) { 1032 return 2;
1012 /* Check first if the block is bad. */ 1033 }
1013 if (c->mtd->block_isbad (c->mtd, offset)) {
1014 D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Bad block at %08x\n", jeb->offset));
1015 return 2;
1016 }
1017 /*
1018 * We read oob data from page 0 and 1 of the block.
1019 * page 0 contains cleanmarker and badblock info
1020 * page 1 contains failure count of this block
1021 */
1022 ret = c->mtd->read_oob (c->mtd, offset, oob_size << 1, &retlen, buf);
1023 1034
1024 if (ret) { 1035 ops.len = oobsize;
1025 D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB failed %d for block at %08x\n", ret, jeb->offset)); 1036 ops.ooblen = oobsize;
1026 return ret; 1037 ops.oobbuf = c->oobbuf;
1027 } 1038 ops.ooboffs = 0;
1028 if (retlen < (oob_size << 1)) { 1039 ops.datbuf = NULL;
1029 D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB return short read (%zd bytes not %d) for block at %08x\n", retlen, oob_size << 1, jeb->offset)); 1040 ops.mode = MTD_OOB_PLACE;
1030 return -EIO;
1031 }
1032 1041
1033 /* Check cleanmarker only on the first physical block */ 1042 ret = c->mtd->read_oob(c->mtd, offset, &ops);
1034 if (!cnt) { 1043 if (ret) {
1035 n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK); 1044 D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
1036 n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER); 1045 "Read OOB failed %d for block at %08x\n",
1037 n.totlen = cpu_to_je32 (8); 1046 ret, jeb->offset));
1038 p = (unsigned char *) &n; 1047 return ret;
1048 }
1039 1049
1040 for (i = 0; i < c->fsdata_len; i++) { 1050 if (ops.retlen < ops.len) {
1041 if (buf[c->fsdata_pos + i] != p[i]) { 1051 D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): "
1042 retval = 1; 1052 "Read OOB return short read (%zd bytes not %d) "
1043 } 1053 "for block at %08x\n", ops.retlen, ops.len,
1044 } 1054 jeb->offset));
1045 D1(if (retval == 1) { 1055 return -EIO;
1046 printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): Cleanmarker node not detected in block at %08x\n", jeb->offset);
1047 printk(KERN_WARNING "OOB at %08x was ", offset);
1048 for (i=0; i < oob_size; i++) {
1049 printk("%02x ", buf[i]);
1050 }
1051 printk("\n");
1052 })
1053 }
1054 offset += c->mtd->erasesize;
1055 } 1056 }
1056 return retval; 1057
1058 n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
1059 n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
1060 n.totlen = cpu_to_je32 (8);
1061 p = (unsigned char *) &n;
1062 b = c->oobbuf + c->fsdata_pos;
1063
1064 for (i = c->fsdata_len; i; i--) {
1065 if (*b++ != *p++)
1066 ret = 1;
1067 }
1068
1069 D1(if (ret == 1) {
1070 printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
1071 "Cleanmarker node not detected in block at %08x\n",
1072 offset);
1073 printk(KERN_WARNING "OOB at %08zx was ", offset);
1074 for (i=0; i < oobsize; i++)
1075 printk("%02x ", c->oobbuf[i]);
1076 printk("\n");
1077 });
1078 return ret;
1057} 1079}
1058 1080
1059int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) 1081int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
1082 struct jffs2_eraseblock *jeb)
1060{ 1083{
1061 struct jffs2_unknown_node n; 1084 struct jffs2_unknown_node n;
1062 int ret; 1085 int ret;
1063 size_t retlen; 1086 struct mtd_oob_ops ops;
1064 1087
1065 n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); 1088 n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1066 n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); 1089 n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
1067 n.totlen = cpu_to_je32(8); 1090 n.totlen = cpu_to_je32(8);
1068 1091
1069 ret = jffs2_flash_write_oob(c, jeb->offset + c->fsdata_pos, c->fsdata_len, &retlen, (unsigned char *)&n); 1092 ops.len = c->fsdata_len;
1093 ops.ooblen = c->fsdata_len;;
1094 ops.oobbuf = (uint8_t *)&n;
1095 ops.ooboffs = c->fsdata_pos;
1096 ops.datbuf = NULL;
1097 ops.mode = MTD_OOB_PLACE;
1098
1099 ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops);
1070 1100
1071 if (ret) { 1101 if (ret) {
1072 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Write failed for block at %08x: error %d\n", jeb->offset, ret)); 1102 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
1103 "Write failed for block at %08x: error %d\n",
1104 jeb->offset, ret));
1073 return ret; 1105 return ret;
1074 } 1106 }
1075 if (retlen != c->fsdata_len) { 1107 if (ops.retlen != ops.len) {
1076 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Short write for block at %08x: %zd not %d\n", jeb->offset, retlen, c->fsdata_len)); 1108 D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
1077 return ret; 1109 "Short write for block at %08x: %zd not %d\n",
1110 jeb->offset, ops.retlen, ops.len));
1111 return -EIO;
1078 } 1112 }
1079 return 0; 1113 return 0;
1080} 1114}
@@ -1108,18 +1142,9 @@ int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *
1108 return 1; 1142 return 1;
1109} 1143}
1110 1144
1111#define NAND_JFFS2_OOB16_FSDALEN 8
1112
1113static struct nand_oobinfo jffs2_oobinfo_docecc = {
1114 .useecc = MTD_NANDECC_PLACE,
1115 .eccbytes = 6,
1116 .eccpos = {0,1,2,3,4,5}
1117};
1118
1119
1120static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c) 1145static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
1121{ 1146{
1122 struct nand_oobinfo *oinfo = &c->mtd->oobinfo; 1147 struct nand_ecclayout *oinfo = c->mtd->ecclayout;
1123 1148
1124 /* Do this only, if we have an oob buffer */ 1149 /* Do this only, if we have an oob buffer */
1125 if (!c->mtd->oobsize) 1150 if (!c->mtd->oobsize)
@@ -1129,33 +1154,23 @@ static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
1129 c->cleanmarker_size = 0; 1154 c->cleanmarker_size = 0;
1130 1155
1131 /* Should we use autoplacement ? */ 1156 /* Should we use autoplacement ? */
1132 if (oinfo && oinfo->useecc == MTD_NANDECC_AUTOPLACE) { 1157 if (!oinfo) {
1133 D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n")); 1158 D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
1134 /* Get the position of the free bytes */ 1159 return -EINVAL;
1135 if (!oinfo->oobfree[0][1]) { 1160 }
1136 printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep. Autoplacement selected and no empty space in oob\n");
1137 return -ENOSPC;
1138 }
1139 c->fsdata_pos = oinfo->oobfree[0][0];
1140 c->fsdata_len = oinfo->oobfree[0][1];
1141 if (c->fsdata_len > 8)
1142 c->fsdata_len = 8;
1143 } else {
1144 /* This is just a legacy fallback and should go away soon */
1145 switch(c->mtd->ecctype) {
1146 case MTD_ECC_RS_DiskOnChip:
1147 printk(KERN_WARNING "JFFS2 using DiskOnChip hardware ECC without autoplacement. Fix it!\n");
1148 c->oobinfo = &jffs2_oobinfo_docecc;
1149 c->fsdata_pos = 6;
1150 c->fsdata_len = NAND_JFFS2_OOB16_FSDALEN;
1151 c->badblock_pos = 15;
1152 break;
1153 1161
1154 default: 1162 D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n"));
1155 D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n")); 1163 /* Get the position of the free bytes */
1156 return -EINVAL; 1164 if (!oinfo->oobfree[0].length) {
1157 } 1165 printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep."
1166 " Autoplacement selected and no empty space in oob\n");
1167 return -ENOSPC;
1158 } 1168 }
1169 c->fsdata_pos = oinfo->oobfree[0].offset;
1170 c->fsdata_len = oinfo->oobfree[0].length;
1171 if (c->fsdata_len > 8)
1172 c->fsdata_len = 8;
1173
1159 return 0; 1174 return 0;
1160} 1175}
1161 1176
@@ -1165,13 +1180,17 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1165 1180
1166 /* Initialise write buffer */ 1181 /* Initialise write buffer */
1167 init_rwsem(&c->wbuf_sem); 1182 init_rwsem(&c->wbuf_sem);
1168 c->wbuf_pagesize = c->mtd->oobblock; 1183 c->wbuf_pagesize = c->mtd->writesize;
1169 c->wbuf_ofs = 0xFFFFFFFF; 1184 c->wbuf_ofs = 0xFFFFFFFF;
1170 1185
1171 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 1186 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1172 if (!c->wbuf) 1187 if (!c->wbuf)
1173 return -ENOMEM; 1188 return -ENOMEM;
1174 1189
1190 c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL);
1191 if (!c->oobbuf)
1192 return -ENOMEM;
1193
1175 res = jffs2_nand_set_oobinfo(c); 1194 res = jffs2_nand_set_oobinfo(c);
1176 1195
1177#ifdef BREAKME 1196#ifdef BREAKME
@@ -1189,6 +1208,7 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
1189void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c) 1208void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
1190{ 1209{
1191 kfree(c->wbuf); 1210 kfree(c->wbuf);
1211 kfree(c->oobbuf);
1192} 1212}
1193 1213
1194int jffs2_dataflash_setup(struct jffs2_sb_info *c) { 1214int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
@@ -1236,33 +1256,14 @@ void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) {
1236 kfree(c->wbuf); 1256 kfree(c->wbuf);
1237} 1257}
1238 1258
1239int jffs2_nor_ecc_flash_setup(struct jffs2_sb_info *c) {
1240 /* Cleanmarker is actually larger on the flashes */
1241 c->cleanmarker_size = 16;
1242
1243 /* Initialize write buffer */
1244 init_rwsem(&c->wbuf_sem);
1245 c->wbuf_pagesize = c->mtd->eccsize;
1246 c->wbuf_ofs = 0xFFFFFFFF;
1247
1248 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
1249 if (!c->wbuf)
1250 return -ENOMEM;
1251
1252 return 0;
1253}
1254
1255void jffs2_nor_ecc_flash_cleanup(struct jffs2_sb_info *c) {
1256 kfree(c->wbuf);
1257}
1258
1259int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) { 1259int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) {
1260 /* Cleanmarker currently occupies a whole programming region */ 1260 /* Cleanmarker currently occupies whole programming regions,
1261 c->cleanmarker_size = MTD_PROGREGION_SIZE(c->mtd); 1261 * either one or 2 for 8Byte STMicro flashes. */
1262 c->cleanmarker_size = max(16u, c->mtd->writesize);
1262 1263
1263 /* Initialize write buffer */ 1264 /* Initialize write buffer */
1264 init_rwsem(&c->wbuf_sem); 1265 init_rwsem(&c->wbuf_sem);
1265 c->wbuf_pagesize = MTD_PROGREGION_SIZE(c->mtd); 1266 c->wbuf_pagesize = c->mtd->writesize;
1266 c->wbuf_ofs = 0xFFFFFFFF; 1267 c->wbuf_ofs = 0xFFFFFFFF;
1267 1268
1268 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL); 1269 c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
diff --git a/fs/jffs2/write.c b/fs/jffs2/write.c
index 1342f0158e9b..67176792e138 100644
--- a/fs/jffs2/write.c
+++ b/fs/jffs2/write.c
@@ -37,7 +37,6 @@ int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint
37 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache; 37 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
38 f->inocache->state = INO_STATE_PRESENT; 38 f->inocache->state = INO_STATE_PRESENT;
39 39
40
41 jffs2_add_ino_cache(c, f->inocache); 40 jffs2_add_ino_cache(c, f->inocache);
42 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino)); 41 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
43 ri->ino = cpu_to_je32(f->inocache->ino); 42 ri->ino = cpu_to_je32(f->inocache->ino);
@@ -57,12 +56,14 @@ int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint
57/* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it, 56/* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
58 write it to the flash, link it into the existing inode/fragment list */ 57 write it to the flash, link it into the existing inode/fragment list */
59 58
60struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const unsigned char *data, uint32_t datalen, uint32_t flash_ofs, int alloc_mode) 59struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
60 struct jffs2_raw_inode *ri, const unsigned char *data,
61 uint32_t datalen, int alloc_mode)
61 62
62{ 63{
63 struct jffs2_raw_node_ref *raw;
64 struct jffs2_full_dnode *fn; 64 struct jffs2_full_dnode *fn;
65 size_t retlen; 65 size_t retlen;
66 uint32_t flash_ofs;
66 struct kvec vecs[2]; 67 struct kvec vecs[2];
67 int ret; 68 int ret;
68 int retried = 0; 69 int retried = 0;
@@ -78,34 +79,21 @@ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2
78 vecs[1].iov_base = (unsigned char *)data; 79 vecs[1].iov_base = (unsigned char *)data;
79 vecs[1].iov_len = datalen; 80 vecs[1].iov_len = datalen;
80 81
81 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
82
83 if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) { 82 if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
84 printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n", je32_to_cpu(ri->totlen), sizeof(*ri), datalen); 83 printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n", je32_to_cpu(ri->totlen), sizeof(*ri), datalen);
85 } 84 }
86 raw = jffs2_alloc_raw_node_ref();
87 if (!raw)
88 return ERR_PTR(-ENOMEM);
89 85
90 fn = jffs2_alloc_full_dnode(); 86 fn = jffs2_alloc_full_dnode();
91 if (!fn) { 87 if (!fn)
92 jffs2_free_raw_node_ref(raw);
93 return ERR_PTR(-ENOMEM); 88 return ERR_PTR(-ENOMEM);
94 }
95
96 fn->ofs = je32_to_cpu(ri->offset);
97 fn->size = je32_to_cpu(ri->dsize);
98 fn->frags = 0;
99 89
100 /* check number of valid vecs */ 90 /* check number of valid vecs */
101 if (!datalen || !data) 91 if (!datalen || !data)
102 cnt = 1; 92 cnt = 1;
103 retry: 93 retry:
104 fn->raw = raw; 94 flash_ofs = write_ofs(c);
105 95
106 raw->flash_offset = flash_ofs; 96 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
107 raw->__totlen = PAD(sizeof(*ri)+datalen);
108 raw->next_phys = NULL;
109 97
110 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) { 98 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
111 BUG_ON(!retried); 99 BUG_ON(!retried);
@@ -125,22 +113,16 @@ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2
125 113
126 /* Mark the space as dirtied */ 114 /* Mark the space as dirtied */
127 if (retlen) { 115 if (retlen) {
128 /* Doesn't belong to any inode */
129 raw->next_in_ino = NULL;
130
131 /* Don't change raw->size to match retlen. We may have 116 /* Don't change raw->size to match retlen. We may have
132 written the node header already, and only the data will 117 written the node header already, and only the data will
133 seem corrupted, in which case the scan would skip over 118 seem corrupted, in which case the scan would skip over
134 any node we write before the original intended end of 119 any node we write before the original intended end of
135 this node */ 120 this node */
136 raw->flash_offset |= REF_OBSOLETE; 121 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
137 jffs2_add_physical_node_ref(c, raw);
138 jffs2_mark_node_obsolete(c, raw);
139 } else { 122 } else {
140 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset); 123 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
141 jffs2_free_raw_node_ref(raw);
142 } 124 }
143 if (!retried && alloc_mode != ALLOC_NORETRY && (raw = jffs2_alloc_raw_node_ref())) { 125 if (!retried && alloc_mode != ALLOC_NORETRY) {
144 /* Try to reallocate space and retry */ 126 /* Try to reallocate space and retry */
145 uint32_t dummy; 127 uint32_t dummy;
146 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size]; 128 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
@@ -153,19 +135,20 @@ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2
153 jffs2_dbg_acct_paranoia_check(c, jeb); 135 jffs2_dbg_acct_paranoia_check(c, jeb);
154 136
155 if (alloc_mode == ALLOC_GC) { 137 if (alloc_mode == ALLOC_GC) {
156 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs, 138 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
157 &dummy, JFFS2_SUMMARY_INODE_SIZE); 139 JFFS2_SUMMARY_INODE_SIZE);
158 } else { 140 } else {
159 /* Locking pain */ 141 /* Locking pain */
160 up(&f->sem); 142 up(&f->sem);
161 jffs2_complete_reservation(c); 143 jffs2_complete_reservation(c);
162 144
163 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs, 145 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
164 &dummy, alloc_mode, JFFS2_SUMMARY_INODE_SIZE); 146 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
165 down(&f->sem); 147 down(&f->sem);
166 } 148 }
167 149
168 if (!ret) { 150 if (!ret) {
151 flash_ofs = write_ofs(c);
169 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs)); 152 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
170 153
171 jffs2_dbg_acct_sanity_check(c,jeb); 154 jffs2_dbg_acct_sanity_check(c,jeb);
@@ -174,7 +157,6 @@ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2
174 goto retry; 157 goto retry;
175 } 158 }
176 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret)); 159 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
177 jffs2_free_raw_node_ref(raw);
178 } 160 }
179 /* Release the full_dnode which is now useless, and return */ 161 /* Release the full_dnode which is now useless, and return */
180 jffs2_free_full_dnode(fn); 162 jffs2_free_full_dnode(fn);
@@ -188,20 +170,17 @@ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2
188 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) || 170 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
189 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) && 171 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
190 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) { 172 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
191 raw->flash_offset |= REF_PRISTINE; 173 flash_ofs |= REF_PRISTINE;
192 } else { 174 } else {
193 raw->flash_offset |= REF_NORMAL; 175 flash_ofs |= REF_NORMAL;
194 } 176 }
195 jffs2_add_physical_node_ref(c, raw); 177 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
196 178 fn->ofs = je32_to_cpu(ri->offset);
197 /* Link into per-inode list */ 179 fn->size = je32_to_cpu(ri->dsize);
198 spin_lock(&c->erase_completion_lock); 180 fn->frags = 0;
199 raw->next_in_ino = f->inocache->nodes;
200 f->inocache->nodes = raw;
201 spin_unlock(&c->erase_completion_lock);
202 181
203 D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n", 182 D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n",
204 flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize), 183 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
205 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc), 184 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
206 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen))); 185 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
207 186
@@ -212,12 +191,14 @@ struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2
212 return fn; 191 return fn;
213} 192}
214 193
215struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_dirent *rd, const unsigned char *name, uint32_t namelen, uint32_t flash_ofs, int alloc_mode) 194struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
195 struct jffs2_raw_dirent *rd, const unsigned char *name,
196 uint32_t namelen, int alloc_mode)
216{ 197{
217 struct jffs2_raw_node_ref *raw;
218 struct jffs2_full_dirent *fd; 198 struct jffs2_full_dirent *fd;
219 size_t retlen; 199 size_t retlen;
220 struct kvec vecs[2]; 200 struct kvec vecs[2];
201 uint32_t flash_ofs;
221 int retried = 0; 202 int retried = 0;
222 int ret; 203 int ret;
223 204
@@ -228,26 +209,16 @@ struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jff
228 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) { 209 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
229 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n"); 210 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
230 BUG(); 211 BUG();
231 } 212 });
232 );
233 213
234 vecs[0].iov_base = rd; 214 vecs[0].iov_base = rd;
235 vecs[0].iov_len = sizeof(*rd); 215 vecs[0].iov_len = sizeof(*rd);
236 vecs[1].iov_base = (unsigned char *)name; 216 vecs[1].iov_base = (unsigned char *)name;
237 vecs[1].iov_len = namelen; 217 vecs[1].iov_len = namelen;
238 218
239 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
240
241 raw = jffs2_alloc_raw_node_ref();
242
243 if (!raw)
244 return ERR_PTR(-ENOMEM);
245
246 fd = jffs2_alloc_full_dirent(namelen+1); 219 fd = jffs2_alloc_full_dirent(namelen+1);
247 if (!fd) { 220 if (!fd)
248 jffs2_free_raw_node_ref(raw);
249 return ERR_PTR(-ENOMEM); 221 return ERR_PTR(-ENOMEM);
250 }
251 222
252 fd->version = je32_to_cpu(rd->version); 223 fd->version = je32_to_cpu(rd->version);
253 fd->ino = je32_to_cpu(rd->ino); 224 fd->ino = je32_to_cpu(rd->ino);
@@ -257,11 +228,9 @@ struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jff
257 fd->name[namelen]=0; 228 fd->name[namelen]=0;
258 229
259 retry: 230 retry:
260 fd->raw = raw; 231 flash_ofs = write_ofs(c);
261 232
262 raw->flash_offset = flash_ofs; 233 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
263 raw->__totlen = PAD(sizeof(*rd)+namelen);
264 raw->next_phys = NULL;
265 234
266 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) { 235 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
267 BUG_ON(!retried); 236 BUG_ON(!retried);
@@ -280,15 +249,11 @@ struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jff
280 sizeof(*rd)+namelen, flash_ofs, ret, retlen); 249 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
281 /* Mark the space as dirtied */ 250 /* Mark the space as dirtied */
282 if (retlen) { 251 if (retlen) {
283 raw->next_in_ino = NULL; 252 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
284 raw->flash_offset |= REF_OBSOLETE;
285 jffs2_add_physical_node_ref(c, raw);
286 jffs2_mark_node_obsolete(c, raw);
287 } else { 253 } else {
288 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset); 254 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
289 jffs2_free_raw_node_ref(raw);
290 } 255 }
291 if (!retried && (raw = jffs2_alloc_raw_node_ref())) { 256 if (!retried) {
292 /* Try to reallocate space and retry */ 257 /* Try to reallocate space and retry */
293 uint32_t dummy; 258 uint32_t dummy;
294 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size]; 259 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
@@ -301,39 +266,33 @@ struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jff
301 jffs2_dbg_acct_paranoia_check(c, jeb); 266 jffs2_dbg_acct_paranoia_check(c, jeb);
302 267
303 if (alloc_mode == ALLOC_GC) { 268 if (alloc_mode == ALLOC_GC) {
304 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs, 269 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
305 &dummy, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 270 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
306 } else { 271 } else {
307 /* Locking pain */ 272 /* Locking pain */
308 up(&f->sem); 273 up(&f->sem);
309 jffs2_complete_reservation(c); 274 jffs2_complete_reservation(c);
310 275
311 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs, 276 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
312 &dummy, alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 277 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
313 down(&f->sem); 278 down(&f->sem);
314 } 279 }
315 280
316 if (!ret) { 281 if (!ret) {
282 flash_ofs = write_ofs(c);
317 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs)); 283 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
318 jffs2_dbg_acct_sanity_check(c,jeb); 284 jffs2_dbg_acct_sanity_check(c,jeb);
319 jffs2_dbg_acct_paranoia_check(c, jeb); 285 jffs2_dbg_acct_paranoia_check(c, jeb);
320 goto retry; 286 goto retry;
321 } 287 }
322 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret)); 288 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
323 jffs2_free_raw_node_ref(raw);
324 } 289 }
325 /* Release the full_dnode which is now useless, and return */ 290 /* Release the full_dnode which is now useless, and return */
326 jffs2_free_full_dirent(fd); 291 jffs2_free_full_dirent(fd);
327 return ERR_PTR(ret?ret:-EIO); 292 return ERR_PTR(ret?ret:-EIO);
328 } 293 }
329 /* Mark the space used */ 294 /* Mark the space used */
330 raw->flash_offset |= REF_PRISTINE; 295 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | REF_PRISTINE, PAD(sizeof(*rd)+namelen), f->inocache);
331 jffs2_add_physical_node_ref(c, raw);
332
333 spin_lock(&c->erase_completion_lock);
334 raw->next_in_ino = f->inocache->nodes;
335 f->inocache->nodes = raw;
336 spin_unlock(&c->erase_completion_lock);
337 296
338 if (retried) { 297 if (retried) {
339 jffs2_dbg_acct_sanity_check(c,NULL); 298 jffs2_dbg_acct_sanity_check(c,NULL);
@@ -359,14 +318,14 @@ int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
359 struct jffs2_full_dnode *fn; 318 struct jffs2_full_dnode *fn;
360 unsigned char *comprbuf = NULL; 319 unsigned char *comprbuf = NULL;
361 uint16_t comprtype = JFFS2_COMPR_NONE; 320 uint16_t comprtype = JFFS2_COMPR_NONE;
362 uint32_t phys_ofs, alloclen; 321 uint32_t alloclen;
363 uint32_t datalen, cdatalen; 322 uint32_t datalen, cdatalen;
364 int retried = 0; 323 int retried = 0;
365 324
366 retry: 325 retry:
367 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset)); 326 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
368 327
369 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, 328 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
370 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE); 329 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
371 if (ret) { 330 if (ret) {
372 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret)); 331 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
@@ -394,7 +353,7 @@ int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
394 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 353 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
395 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen)); 354 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
396 355
397 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY); 356 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
398 357
399 jffs2_free_comprbuf(comprbuf, buf); 358 jffs2_free_comprbuf(comprbuf, buf);
400 359
@@ -448,13 +407,13 @@ int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, str
448 struct jffs2_raw_dirent *rd; 407 struct jffs2_raw_dirent *rd;
449 struct jffs2_full_dnode *fn; 408 struct jffs2_full_dnode *fn;
450 struct jffs2_full_dirent *fd; 409 struct jffs2_full_dirent *fd;
451 uint32_t alloclen, phys_ofs; 410 uint32_t alloclen;
452 int ret; 411 int ret;
453 412
454 /* Try to reserve enough space for both node and dirent. 413 /* Try to reserve enough space for both node and dirent.
455 * Just the node will do for now, though 414 * Just the node will do for now, though
456 */ 415 */
457 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL, 416 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
458 JFFS2_SUMMARY_INODE_SIZE); 417 JFFS2_SUMMARY_INODE_SIZE);
459 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen)); 418 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
460 if (ret) { 419 if (ret) {
@@ -465,7 +424,7 @@ int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, str
465 ri->data_crc = cpu_to_je32(0); 424 ri->data_crc = cpu_to_je32(0);
466 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8)); 425 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
467 426
468 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL); 427 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
469 428
470 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n", 429 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
471 jemode_to_cpu(ri->mode))); 430 jemode_to_cpu(ri->mode)));
@@ -484,7 +443,7 @@ int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, str
484 443
485 up(&f->sem); 444 up(&f->sem);
486 jffs2_complete_reservation(c); 445 jffs2_complete_reservation(c);
487 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, 446 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
488 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 447 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
489 448
490 if (ret) { 449 if (ret) {
@@ -516,7 +475,7 @@ int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, str
516 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 475 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
517 rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); 476 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
518 477
519 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL); 478 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
520 479
521 jffs2_free_raw_dirent(rd); 480 jffs2_free_raw_dirent(rd);
522 481
@@ -545,7 +504,7 @@ int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
545{ 504{
546 struct jffs2_raw_dirent *rd; 505 struct jffs2_raw_dirent *rd;
547 struct jffs2_full_dirent *fd; 506 struct jffs2_full_dirent *fd;
548 uint32_t alloclen, phys_ofs; 507 uint32_t alloclen;
549 int ret; 508 int ret;
550 509
551 if (1 /* alternative branch needs testing */ || 510 if (1 /* alternative branch needs testing */ ||
@@ -556,7 +515,7 @@ int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
556 if (!rd) 515 if (!rd)
557 return -ENOMEM; 516 return -ENOMEM;
558 517
559 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, 518 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
560 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 519 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
561 if (ret) { 520 if (ret) {
562 jffs2_free_raw_dirent(rd); 521 jffs2_free_raw_dirent(rd);
@@ -580,7 +539,7 @@ int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
580 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 539 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
581 rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); 540 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
582 541
583 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION); 542 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
584 543
585 jffs2_free_raw_dirent(rd); 544 jffs2_free_raw_dirent(rd);
586 545
@@ -659,14 +618,14 @@ int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint
659{ 618{
660 struct jffs2_raw_dirent *rd; 619 struct jffs2_raw_dirent *rd;
661 struct jffs2_full_dirent *fd; 620 struct jffs2_full_dirent *fd;
662 uint32_t alloclen, phys_ofs; 621 uint32_t alloclen;
663 int ret; 622 int ret;
664 623
665 rd = jffs2_alloc_raw_dirent(); 624 rd = jffs2_alloc_raw_dirent();
666 if (!rd) 625 if (!rd)
667 return -ENOMEM; 626 return -ENOMEM;
668 627
669 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, 628 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
670 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen)); 629 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
671 if (ret) { 630 if (ret) {
672 jffs2_free_raw_dirent(rd); 631 jffs2_free_raw_dirent(rd);
@@ -692,7 +651,7 @@ int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint
692 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8)); 651 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
693 rd->name_crc = cpu_to_je32(crc32(0, name, namelen)); 652 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
694 653
695 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL); 654 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
696 655
697 jffs2_free_raw_dirent(rd); 656 jffs2_free_raw_dirent(rd);
698 657
diff --git a/fs/jffs2/xattr.c b/fs/jffs2/xattr.c
new file mode 100644
index 000000000000..18e66dbf23b4
--- /dev/null
+++ b/fs/jffs2/xattr.c
@@ -0,0 +1,1326 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/time.h>
15#include <linux/pagemap.h>
16#include <linux/highmem.h>
17#include <linux/crc32.h>
18#include <linux/jffs2.h>
19#include <linux/xattr.h>
20#include <linux/mtd/mtd.h>
21#include "nodelist.h"
22/* -------- xdatum related functions ----------------
23 * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
24 * is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
25 * the index of the xattr name/value pair cache (c->xattrindex).
26 * is_xattr_datum_unchecked(c, xd)
27 * returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
28 * unchecked, it returns 0.
29 * unload_xattr_datum(c, xd)
30 * is used to release xattr name/value pair and detach from c->xattrindex.
31 * reclaim_xattr_datum(c)
32 * is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
33 * memory usage by cache is over c->xdatum_mem_threshold. Currentry, this threshold
34 * is hard coded as 32KiB.
35 * do_verify_xattr_datum(c, xd)
36 * is used to load the xdatum informations without name/value pair from the medium.
37 * It's necessary once, because those informations are not collected during mounting
38 * process when EBS is enabled.
39 * 0 will be returned, if success. An negative return value means recoverable error, and
40 * positive return value means unrecoverable error. Thus, caller must remove this xdatum
41 * and xref when it returned positive value.
42 * do_load_xattr_datum(c, xd)
43 * is used to load name/value pair from the medium.
44 * The meanings of return value is same as do_verify_xattr_datum().
45 * load_xattr_datum(c, xd)
46 * is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
47 * If xd need to call do_verify_xattr_datum() at first, it's called before calling
48 * do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
49 * save_xattr_datum(c, xd)
50 * is used to write xdatum to medium. xd->version will be incremented.
51 * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
52 * is used to create new xdatum and write to medium.
53 * delete_xattr_datum(c, xd)
54 * is used to delete a xdatum. It marks xd JFFS2_XFLAGS_DEAD, and allows
55 * GC to reclaim those physical nodes.
56 * -------------------------------------------------- */
57static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
58{
59 int name_len = strlen(xname);
60
61 return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
62}
63
64static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
65{
66 struct jffs2_raw_node_ref *raw;
67 int rc = 0;
68
69 spin_lock(&c->erase_completion_lock);
70 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
71 if (ref_flags(raw) == REF_UNCHECKED) {
72 rc = 1;
73 break;
74 }
75 }
76 spin_unlock(&c->erase_completion_lock);
77 return rc;
78}
79
80static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
81{
82 /* must be called under down_write(xattr_sem) */
83 D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version));
84 if (xd->xname) {
85 c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
86 kfree(xd->xname);
87 }
88
89 list_del_init(&xd->xindex);
90 xd->hashkey = 0;
91 xd->xname = NULL;
92 xd->xvalue = NULL;
93}
94
95static void reclaim_xattr_datum(struct jffs2_sb_info *c)
96{
97 /* must be called under down_write(xattr_sem) */
98 struct jffs2_xattr_datum *xd, *_xd;
99 uint32_t target, before;
100 static int index = 0;
101 int count;
102
103 if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
104 return;
105
106 before = c->xdatum_mem_usage;
107 target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
108 for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
109 list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
110 if (xd->flags & JFFS2_XFLAGS_HOT) {
111 xd->flags &= ~JFFS2_XFLAGS_HOT;
112 } else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
113 unload_xattr_datum(c, xd);
114 }
115 if (c->xdatum_mem_usage <= target)
116 goto out;
117 }
118 index = (index+1) % XATTRINDEX_HASHSIZE;
119 }
120 out:
121 JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
122 before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
123}
124
125static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
126{
127 /* must be called under down_write(xattr_sem) */
128 struct jffs2_eraseblock *jeb;
129 struct jffs2_raw_node_ref *raw;
130 struct jffs2_raw_xattr rx;
131 size_t readlen;
132 uint32_t crc, offset, totlen;
133 int rc;
134
135 spin_lock(&c->erase_completion_lock);
136 offset = ref_offset(xd->node);
137 if (ref_flags(xd->node) == REF_PRISTINE)
138 goto complete;
139 spin_unlock(&c->erase_completion_lock);
140
141 rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
142 if (rc || readlen != sizeof(rx)) {
143 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
144 rc, sizeof(rx), readlen, offset);
145 return rc ? rc : -EIO;
146 }
147 crc = crc32(0, &rx, sizeof(rx) - 4);
148 if (crc != je32_to_cpu(rx.node_crc)) {
149 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
150 offset, je32_to_cpu(rx.hdr_crc), crc);
151 xd->flags |= JFFS2_XFLAGS_INVALID;
152 return EIO;
153 }
154 totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
155 if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
156 || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
157 || je32_to_cpu(rx.totlen) != totlen
158 || je32_to_cpu(rx.xid) != xd->xid
159 || je32_to_cpu(rx.version) != xd->version) {
160 JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
161 "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
162 offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
163 je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
164 je32_to_cpu(rx.totlen), totlen,
165 je32_to_cpu(rx.xid), xd->xid,
166 je32_to_cpu(rx.version), xd->version);
167 xd->flags |= JFFS2_XFLAGS_INVALID;
168 return EIO;
169 }
170 xd->xprefix = rx.xprefix;
171 xd->name_len = rx.name_len;
172 xd->value_len = je16_to_cpu(rx.value_len);
173 xd->data_crc = je32_to_cpu(rx.data_crc);
174
175 spin_lock(&c->erase_completion_lock);
176 complete:
177 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
178 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
179 totlen = PAD(ref_totlen(c, jeb, raw));
180 if (ref_flags(raw) == REF_UNCHECKED) {
181 c->unchecked_size -= totlen; c->used_size += totlen;
182 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
183 }
184 raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
185 }
186 spin_unlock(&c->erase_completion_lock);
187
188 /* unchecked xdatum is chained with c->xattr_unchecked */
189 list_del_init(&xd->xindex);
190
191 dbg_xattr("success on verfying xdatum (xid=%u, version=%u)\n",
192 xd->xid, xd->version);
193
194 return 0;
195}
196
197static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
198{
199 /* must be called under down_write(xattr_sem) */
200 char *data;
201 size_t readlen;
202 uint32_t crc, length;
203 int i, ret, retry = 0;
204
205 BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
206 BUG_ON(!list_empty(&xd->xindex));
207 retry:
208 length = xd->name_len + 1 + xd->value_len;
209 data = kmalloc(length, GFP_KERNEL);
210 if (!data)
211 return -ENOMEM;
212
213 ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
214 length, &readlen, data);
215
216 if (ret || length!=readlen) {
217 JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
218 ret, length, readlen, ref_offset(xd->node));
219 kfree(data);
220 return ret ? ret : -EIO;
221 }
222
223 data[xd->name_len] = '\0';
224 crc = crc32(0, data, length);
225 if (crc != xd->data_crc) {
226 JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XREF)"
227 " at %#08x, read: 0x%08x calculated: 0x%08x\n",
228 ref_offset(xd->node), xd->data_crc, crc);
229 kfree(data);
230 xd->flags |= JFFS2_XFLAGS_INVALID;
231 return EIO;
232 }
233
234 xd->flags |= JFFS2_XFLAGS_HOT;
235 xd->xname = data;
236 xd->xvalue = data + xd->name_len+1;
237
238 c->xdatum_mem_usage += length;
239
240 xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
241 i = xd->hashkey % XATTRINDEX_HASHSIZE;
242 list_add(&xd->xindex, &c->xattrindex[i]);
243 if (!retry) {
244 retry = 1;
245 reclaim_xattr_datum(c);
246 if (!xd->xname)
247 goto retry;
248 }
249
250 dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
251 xd->xid, xd->xprefix, xd->xname);
252
253 return 0;
254}
255
256static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
257{
258 /* must be called under down_write(xattr_sem);
259 * rc < 0 : recoverable error, try again
260 * rc = 0 : success
261 * rc > 0 : Unrecoverable error, this node should be deleted.
262 */
263 int rc = 0;
264
265 BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
266 if (xd->xname)
267 return 0;
268 if (xd->flags & JFFS2_XFLAGS_INVALID)
269 return EIO;
270 if (unlikely(is_xattr_datum_unchecked(c, xd)))
271 rc = do_verify_xattr_datum(c, xd);
272 if (!rc)
273 rc = do_load_xattr_datum(c, xd);
274 return rc;
275}
276
277static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
278{
279 /* must be called under down_write(xattr_sem) */
280 struct jffs2_raw_xattr rx;
281 struct kvec vecs[2];
282 size_t length;
283 int rc, totlen;
284 uint32_t phys_ofs = write_ofs(c);
285
286 BUG_ON(!xd->xname);
287 BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
288
289 vecs[0].iov_base = &rx;
290 vecs[0].iov_len = sizeof(rx);
291 vecs[1].iov_base = xd->xname;
292 vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
293 totlen = vecs[0].iov_len + vecs[1].iov_len;
294
295 /* Setup raw-xattr */
296 memset(&rx, 0, sizeof(rx));
297 rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
298 rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
299 rx.totlen = cpu_to_je32(PAD(totlen));
300 rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
301
302 rx.xid = cpu_to_je32(xd->xid);
303 rx.version = cpu_to_je32(++xd->version);
304 rx.xprefix = xd->xprefix;
305 rx.name_len = xd->name_len;
306 rx.value_len = cpu_to_je16(xd->value_len);
307 rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
308 rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
309
310 rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
311 if (rc || totlen != length) {
312 JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
313 rc, totlen, length, phys_ofs);
314 rc = rc ? rc : -EIO;
315 if (length)
316 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
317
318 return rc;
319 }
320 /* success */
321 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
322
323 dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
324 xd->xid, xd->version, xd->xprefix, xd->xname);
325
326 return 0;
327}
328
329static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
330 int xprefix, const char *xname,
331 const char *xvalue, int xsize)
332{
333 /* must be called under down_write(xattr_sem) */
334 struct jffs2_xattr_datum *xd;
335 uint32_t hashkey, name_len;
336 char *data;
337 int i, rc;
338
339 /* Search xattr_datum has same xname/xvalue by index */
340 hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
341 i = hashkey % XATTRINDEX_HASHSIZE;
342 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
343 if (xd->hashkey==hashkey
344 && xd->xprefix==xprefix
345 && xd->value_len==xsize
346 && !strcmp(xd->xname, xname)
347 && !memcmp(xd->xvalue, xvalue, xsize)) {
348 atomic_inc(&xd->refcnt);
349 return xd;
350 }
351 }
352
353 /* Not found, Create NEW XATTR-Cache */
354 name_len = strlen(xname);
355
356 xd = jffs2_alloc_xattr_datum();
357 if (!xd)
358 return ERR_PTR(-ENOMEM);
359
360 data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
361 if (!data) {
362 jffs2_free_xattr_datum(xd);
363 return ERR_PTR(-ENOMEM);
364 }
365 strcpy(data, xname);
366 memcpy(data + name_len + 1, xvalue, xsize);
367
368 atomic_set(&xd->refcnt, 1);
369 xd->xid = ++c->highest_xid;
370 xd->flags |= JFFS2_XFLAGS_HOT;
371 xd->xprefix = xprefix;
372
373 xd->hashkey = hashkey;
374 xd->xname = data;
375 xd->xvalue = data + name_len + 1;
376 xd->name_len = name_len;
377 xd->value_len = xsize;
378 xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
379
380 rc = save_xattr_datum(c, xd);
381 if (rc) {
382 kfree(xd->xname);
383 jffs2_free_xattr_datum(xd);
384 return ERR_PTR(rc);
385 }
386
387 /* Insert Hash Index */
388 i = hashkey % XATTRINDEX_HASHSIZE;
389 list_add(&xd->xindex, &c->xattrindex[i]);
390
391 c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
392 reclaim_xattr_datum(c);
393
394 return xd;
395}
396
397static void delete_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
398{
399 /* must be called under down_write(xattr_sem) */
400 BUG_ON(atomic_read(&xd->refcnt));
401
402 unload_xattr_datum(c, xd);
403 xd->flags |= JFFS2_XFLAGS_DEAD;
404 spin_lock(&c->erase_completion_lock);
405 if (xd->node == (void *)xd) {
406 BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
407 jffs2_free_xattr_datum(xd);
408 } else {
409 list_add(&xd->xindex, &c->xattr_dead_list);
410 }
411 spin_unlock(&c->erase_completion_lock);
412 dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n", xd->xid, xd->version);
413}
414
415/* -------- xref related functions ------------------
416 * verify_xattr_ref(c, ref)
417 * is used to load xref information from medium. Because summary data does not
418 * contain xid/ino, it's necessary to verify once while mounting process.
419 * save_xattr_ref(c, ref)
420 * is used to write xref to medium. If delete marker is marked, it write
421 * a delete marker of xref into medium.
422 * create_xattr_ref(c, ic, xd)
423 * is used to create a new xref and write to medium.
424 * delete_xattr_ref(c, ref)
425 * is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
426 * and allows GC to reclaim those physical nodes.
427 * jffs2_xattr_delete_inode(c, ic)
428 * is called to remove xrefs related to obsolete inode when inode is unlinked.
429 * jffs2_xattr_free_inode(c, ic)
430 * is called to release xattr related objects when unmounting.
431 * check_xattr_ref_inode(c, ic)
432 * is used to confirm inode does not have duplicate xattr name/value pair.
433 * -------------------------------------------------- */
434static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
435{
436 struct jffs2_eraseblock *jeb;
437 struct jffs2_raw_node_ref *raw;
438 struct jffs2_raw_xref rr;
439 size_t readlen;
440 uint32_t crc, offset, totlen;
441 int rc;
442
443 spin_lock(&c->erase_completion_lock);
444 if (ref_flags(ref->node) != REF_UNCHECKED)
445 goto complete;
446 offset = ref_offset(ref->node);
447 spin_unlock(&c->erase_completion_lock);
448
449 rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
450 if (rc || sizeof(rr) != readlen) {
451 JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
452 rc, sizeof(rr), readlen, offset);
453 return rc ? rc : -EIO;
454 }
455 /* obsolete node */
456 crc = crc32(0, &rr, sizeof(rr) - 4);
457 if (crc != je32_to_cpu(rr.node_crc)) {
458 JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
459 offset, je32_to_cpu(rr.node_crc), crc);
460 return EIO;
461 }
462 if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
463 || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
464 || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
465 JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
466 "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
467 offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
468 je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
469 je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
470 return EIO;
471 }
472 ref->ino = je32_to_cpu(rr.ino);
473 ref->xid = je32_to_cpu(rr.xid);
474 ref->xseqno = je32_to_cpu(rr.xseqno);
475 if (ref->xseqno > c->highest_xseqno)
476 c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
477
478 spin_lock(&c->erase_completion_lock);
479 complete:
480 for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
481 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
482 totlen = PAD(ref_totlen(c, jeb, raw));
483 if (ref_flags(raw) == REF_UNCHECKED) {
484 c->unchecked_size -= totlen; c->used_size += totlen;
485 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
486 }
487 raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
488 }
489 spin_unlock(&c->erase_completion_lock);
490
491 dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
492 ref->ino, ref->xid, ref_offset(ref->node));
493 return 0;
494}
495
496static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
497{
498 /* must be called under down_write(xattr_sem) */
499 struct jffs2_raw_xref rr;
500 size_t length;
501 uint32_t xseqno, phys_ofs = write_ofs(c);
502 int ret;
503
504 rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
505 rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
506 rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
507 rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
508
509 xseqno = (c->highest_xseqno += 2);
510 if (is_xattr_ref_dead(ref)) {
511 xseqno |= XREF_DELETE_MARKER;
512 rr.ino = cpu_to_je32(ref->ino);
513 rr.xid = cpu_to_je32(ref->xid);
514 } else {
515 rr.ino = cpu_to_je32(ref->ic->ino);
516 rr.xid = cpu_to_je32(ref->xd->xid);
517 }
518 rr.xseqno = cpu_to_je32(xseqno);
519 rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
520
521 ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
522 if (ret || sizeof(rr) != length) {
523 JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
524 ret, sizeof(rr), length, phys_ofs);
525 ret = ret ? ret : -EIO;
526 if (length)
527 jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
528
529 return ret;
530 }
531 /* success */
532 ref->xseqno = xseqno;
533 jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
534
535 dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
536
537 return 0;
538}
539
540static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
541 struct jffs2_xattr_datum *xd)
542{
543 /* must be called under down_write(xattr_sem) */
544 struct jffs2_xattr_ref *ref;
545 int ret;
546
547 ref = jffs2_alloc_xattr_ref();
548 if (!ref)
549 return ERR_PTR(-ENOMEM);
550 ref->ic = ic;
551 ref->xd = xd;
552
553 ret = save_xattr_ref(c, ref);
554 if (ret) {
555 jffs2_free_xattr_ref(ref);
556 return ERR_PTR(ret);
557 }
558
559 /* Chain to inode */
560 ref->next = ic->xref;
561 ic->xref = ref;
562
563 return ref; /* success */
564}
565
566static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
567{
568 /* must be called under down_write(xattr_sem) */
569 struct jffs2_xattr_datum *xd;
570
571 xd = ref->xd;
572 ref->xseqno |= XREF_DELETE_MARKER;
573 ref->ino = ref->ic->ino;
574 ref->xid = ref->xd->xid;
575 spin_lock(&c->erase_completion_lock);
576 ref->next = c->xref_dead_list;
577 c->xref_dead_list = ref;
578 spin_unlock(&c->erase_completion_lock);
579
580 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
581 ref->ino, ref->xid, ref->xseqno);
582
583 if (atomic_dec_and_test(&xd->refcnt))
584 delete_xattr_datum(c, xd);
585}
586
587void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
588{
589 /* It's called from jffs2_clear_inode() on inode removing.
590 When an inode with XATTR is removed, those XATTRs must be removed. */
591 struct jffs2_xattr_ref *ref, *_ref;
592
593 if (!ic || ic->nlink > 0)
594 return;
595
596 down_write(&c->xattr_sem);
597 for (ref = ic->xref; ref; ref = _ref) {
598 _ref = ref->next;
599 delete_xattr_ref(c, ref);
600 }
601 ic->xref = NULL;
602 up_write(&c->xattr_sem);
603}
604
605void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
606{
607 /* It's called from jffs2_free_ino_caches() until unmounting FS. */
608 struct jffs2_xattr_datum *xd;
609 struct jffs2_xattr_ref *ref, *_ref;
610
611 down_write(&c->xattr_sem);
612 for (ref = ic->xref; ref; ref = _ref) {
613 _ref = ref->next;
614 xd = ref->xd;
615 if (atomic_dec_and_test(&xd->refcnt)) {
616 unload_xattr_datum(c, xd);
617 jffs2_free_xattr_datum(xd);
618 }
619 jffs2_free_xattr_ref(ref);
620 }
621 ic->xref = NULL;
622 up_write(&c->xattr_sem);
623}
624
625static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
626{
627 /* success of check_xattr_ref_inode() means taht inode (ic) dose not have
628 * duplicate name/value pairs. If duplicate name/value pair would be found,
629 * one will be removed.
630 */
631 struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
632 int rc = 0;
633
634 if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
635 return 0;
636 down_write(&c->xattr_sem);
637 retry:
638 rc = 0;
639 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
640 if (!ref->xd->xname) {
641 rc = load_xattr_datum(c, ref->xd);
642 if (unlikely(rc > 0)) {
643 *pref = ref->next;
644 delete_xattr_ref(c, ref);
645 goto retry;
646 } else if (unlikely(rc < 0))
647 goto out;
648 }
649 for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
650 if (!cmp->xd->xname) {
651 ref->xd->flags |= JFFS2_XFLAGS_BIND;
652 rc = load_xattr_datum(c, cmp->xd);
653 ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
654 if (unlikely(rc > 0)) {
655 *pcmp = cmp->next;
656 delete_xattr_ref(c, cmp);
657 goto retry;
658 } else if (unlikely(rc < 0))
659 goto out;
660 }
661 if (ref->xd->xprefix == cmp->xd->xprefix
662 && !strcmp(ref->xd->xname, cmp->xd->xname)) {
663 if (ref->xseqno > cmp->xseqno) {
664 *pcmp = cmp->next;
665 delete_xattr_ref(c, cmp);
666 } else {
667 *pref = ref->next;
668 delete_xattr_ref(c, ref);
669 }
670 goto retry;
671 }
672 }
673 }
674 ic->flags |= INO_FLAGS_XATTR_CHECKED;
675 out:
676 up_write(&c->xattr_sem);
677
678 return rc;
679}
680
681/* -------- xattr subsystem functions ---------------
682 * jffs2_init_xattr_subsystem(c)
683 * is used to initialize semaphore and list_head, and some variables.
684 * jffs2_find_xattr_datum(c, xid)
685 * is used to lookup xdatum while scanning process.
686 * jffs2_clear_xattr_subsystem(c)
687 * is used to release any xattr related objects.
688 * jffs2_build_xattr_subsystem(c)
689 * is used to associate xdatum and xref while super block building process.
690 * jffs2_setup_xattr_datum(c, xid, version)
691 * is used to insert xdatum while scanning process.
692 * -------------------------------------------------- */
693void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
694{
695 int i;
696
697 for (i=0; i < XATTRINDEX_HASHSIZE; i++)
698 INIT_LIST_HEAD(&c->xattrindex[i]);
699 INIT_LIST_HEAD(&c->xattr_unchecked);
700 INIT_LIST_HEAD(&c->xattr_dead_list);
701 c->xref_dead_list = NULL;
702 c->xref_temp = NULL;
703
704 init_rwsem(&c->xattr_sem);
705 c->highest_xid = 0;
706 c->highest_xseqno = 0;
707 c->xdatum_mem_usage = 0;
708 c->xdatum_mem_threshold = 32 * 1024; /* Default 32KB */
709}
710
711static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
712{
713 struct jffs2_xattr_datum *xd;
714 int i = xid % XATTRINDEX_HASHSIZE;
715
716 /* It's only used in scanning/building process. */
717 BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
718
719 list_for_each_entry(xd, &c->xattrindex[i], xindex) {
720 if (xd->xid==xid)
721 return xd;
722 }
723 return NULL;
724}
725
726void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
727{
728 struct jffs2_xattr_datum *xd, *_xd;
729 struct jffs2_xattr_ref *ref, *_ref;
730 int i;
731
732 for (ref=c->xref_temp; ref; ref = _ref) {
733 _ref = ref->next;
734 jffs2_free_xattr_ref(ref);
735 }
736
737 for (ref=c->xref_dead_list; ref; ref = _ref) {
738 _ref = ref->next;
739 jffs2_free_xattr_ref(ref);
740 }
741
742 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
743 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
744 list_del(&xd->xindex);
745 if (xd->xname)
746 kfree(xd->xname);
747 jffs2_free_xattr_datum(xd);
748 }
749 }
750
751 list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
752 list_del(&xd->xindex);
753 jffs2_free_xattr_datum(xd);
754 }
755}
756
757#define XREF_TMPHASH_SIZE (128)
758void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
759{
760 struct jffs2_xattr_ref *ref, *_ref;
761 struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
762 struct jffs2_xattr_datum *xd, *_xd;
763 struct jffs2_inode_cache *ic;
764 struct jffs2_raw_node_ref *raw;
765 int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
766 int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
767
768 BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
769
770 /* Phase.1 : Merge same xref */
771 for (i=0; i < XREF_TMPHASH_SIZE; i++)
772 xref_tmphash[i] = NULL;
773 for (ref=c->xref_temp; ref; ref=_ref) {
774 struct jffs2_xattr_ref *tmp;
775
776 _ref = ref->next;
777 if (ref_flags(ref->node) != REF_PRISTINE) {
778 if (verify_xattr_ref(c, ref)) {
779 BUG_ON(ref->node->next_in_ino != (void *)ref);
780 ref->node->next_in_ino = NULL;
781 jffs2_mark_node_obsolete(c, ref->node);
782 jffs2_free_xattr_ref(ref);
783 continue;
784 }
785 }
786
787 i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
788 for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
789 if (tmp->ino == ref->ino && tmp->xid == ref->xid)
790 break;
791 }
792 if (tmp) {
793 raw = ref->node;
794 if (ref->xseqno > tmp->xseqno) {
795 tmp->xseqno = ref->xseqno;
796 raw->next_in_ino = tmp->node;
797 tmp->node = raw;
798 } else {
799 raw->next_in_ino = tmp->node->next_in_ino;
800 tmp->node->next_in_ino = raw;
801 }
802 jffs2_free_xattr_ref(ref);
803 continue;
804 } else {
805 ref->next = xref_tmphash[i];
806 xref_tmphash[i] = ref;
807 }
808 }
809 c->xref_temp = NULL;
810
811 /* Phase.2 : Bind xref with inode_cache and xattr_datum */
812 for (i=0; i < XREF_TMPHASH_SIZE; i++) {
813 for (ref=xref_tmphash[i]; ref; ref=_ref) {
814 xref_count++;
815 _ref = ref->next;
816 if (is_xattr_ref_dead(ref)) {
817 ref->next = c->xref_dead_list;
818 c->xref_dead_list = ref;
819 xref_dead_count++;
820 continue;
821 }
822 /* At this point, ref->xid and ref->ino contain XID and inode number.
823 ref->xd and ref->ic are not valid yet. */
824 xd = jffs2_find_xattr_datum(c, ref->xid);
825 ic = jffs2_get_ino_cache(c, ref->ino);
826 if (!xd || !ic) {
827 dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
828 ref->ino, ref->xid, ref->xseqno);
829 ref->xseqno |= XREF_DELETE_MARKER;
830 ref->next = c->xref_dead_list;
831 c->xref_dead_list = ref;
832 xref_orphan_count++;
833 continue;
834 }
835 ref->xd = xd;
836 ref->ic = ic;
837 atomic_inc(&xd->refcnt);
838 ref->next = ic->xref;
839 ic->xref = ref;
840 }
841 }
842
843 /* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
844 for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
845 list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
846 xdatum_count++;
847 list_del_init(&xd->xindex);
848 if (!atomic_read(&xd->refcnt)) {
849 dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
850 xd->xid, xd->version);
851 xd->flags |= JFFS2_XFLAGS_DEAD;
852 list_add(&xd->xindex, &c->xattr_unchecked);
853 xdatum_orphan_count++;
854 continue;
855 }
856 if (is_xattr_datum_unchecked(c, xd)) {
857 dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
858 xd->xid, xd->version);
859 list_add(&xd->xindex, &c->xattr_unchecked);
860 xdatum_unchecked_count++;
861 }
862 }
863 }
864 /* build complete */
865 JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
866 " (%u unchecked, %u orphan) and "
867 "%u of xref (%u dead, %u orphan) found.\n",
868 xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
869 xref_count, xref_dead_count, xref_orphan_count);
870}
871
872struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
873 uint32_t xid, uint32_t version)
874{
875 struct jffs2_xattr_datum *xd;
876
877 xd = jffs2_find_xattr_datum(c, xid);
878 if (!xd) {
879 xd = jffs2_alloc_xattr_datum();
880 if (!xd)
881 return ERR_PTR(-ENOMEM);
882 xd->xid = xid;
883 xd->version = version;
884 if (xd->xid > c->highest_xid)
885 c->highest_xid = xd->xid;
886 list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
887 }
888 return xd;
889}
890
891/* -------- xattr subsystem functions ---------------
892 * xprefix_to_handler(xprefix)
893 * is used to translate xprefix into xattr_handler.
894 * jffs2_listxattr(dentry, buffer, size)
895 * is an implementation of listxattr handler on jffs2.
896 * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
897 * is an implementation of getxattr handler on jffs2.
898 * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
899 * is an implementation of setxattr handler on jffs2.
900 * -------------------------------------------------- */
901struct xattr_handler *jffs2_xattr_handlers[] = {
902 &jffs2_user_xattr_handler,
903#ifdef CONFIG_JFFS2_FS_SECURITY
904 &jffs2_security_xattr_handler,
905#endif
906#ifdef CONFIG_JFFS2_FS_POSIX_ACL
907 &jffs2_acl_access_xattr_handler,
908 &jffs2_acl_default_xattr_handler,
909#endif
910 &jffs2_trusted_xattr_handler,
911 NULL
912};
913
914static struct xattr_handler *xprefix_to_handler(int xprefix) {
915 struct xattr_handler *ret;
916
917 switch (xprefix) {
918 case JFFS2_XPREFIX_USER:
919 ret = &jffs2_user_xattr_handler;
920 break;
921#ifdef CONFIG_JFFS2_FS_SECURITY
922 case JFFS2_XPREFIX_SECURITY:
923 ret = &jffs2_security_xattr_handler;
924 break;
925#endif
926#ifdef CONFIG_JFFS2_FS_POSIX_ACL
927 case JFFS2_XPREFIX_ACL_ACCESS:
928 ret = &jffs2_acl_access_xattr_handler;
929 break;
930 case JFFS2_XPREFIX_ACL_DEFAULT:
931 ret = &jffs2_acl_default_xattr_handler;
932 break;
933#endif
934 case JFFS2_XPREFIX_TRUSTED:
935 ret = &jffs2_trusted_xattr_handler;
936 break;
937 default:
938 ret = NULL;
939 break;
940 }
941 return ret;
942}
943
944ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
945{
946 struct inode *inode = dentry->d_inode;
947 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
948 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
949 struct jffs2_inode_cache *ic = f->inocache;
950 struct jffs2_xattr_ref *ref, **pref;
951 struct jffs2_xattr_datum *xd;
952 struct xattr_handler *xhandle;
953 ssize_t len, rc;
954 int retry = 0;
955
956 rc = check_xattr_ref_inode(c, ic);
957 if (unlikely(rc))
958 return rc;
959
960 down_read(&c->xattr_sem);
961 retry:
962 len = 0;
963 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
964 BUG_ON(ref->ic != ic);
965 xd = ref->xd;
966 if (!xd->xname) {
967 /* xdatum is unchached */
968 if (!retry) {
969 retry = 1;
970 up_read(&c->xattr_sem);
971 down_write(&c->xattr_sem);
972 goto retry;
973 } else {
974 rc = load_xattr_datum(c, xd);
975 if (unlikely(rc > 0)) {
976 *pref = ref->next;
977 delete_xattr_ref(c, ref);
978 goto retry;
979 } else if (unlikely(rc < 0))
980 goto out;
981 }
982 }
983 xhandle = xprefix_to_handler(xd->xprefix);
984 if (!xhandle)
985 continue;
986 if (buffer) {
987 rc = xhandle->list(inode, buffer+len, size-len, xd->xname, xd->name_len);
988 } else {
989 rc = xhandle->list(inode, NULL, 0, xd->xname, xd->name_len);
990 }
991 if (rc < 0)
992 goto out;
993 len += rc;
994 }
995 rc = len;
996 out:
997 if (!retry) {
998 up_read(&c->xattr_sem);
999 } else {
1000 up_write(&c->xattr_sem);
1001 }
1002 return rc;
1003}
1004
1005int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
1006 char *buffer, size_t size)
1007{
1008 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1009 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1010 struct jffs2_inode_cache *ic = f->inocache;
1011 struct jffs2_xattr_datum *xd;
1012 struct jffs2_xattr_ref *ref, **pref;
1013 int rc, retry = 0;
1014
1015 rc = check_xattr_ref_inode(c, ic);
1016 if (unlikely(rc))
1017 return rc;
1018
1019 down_read(&c->xattr_sem);
1020 retry:
1021 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1022 BUG_ON(ref->ic!=ic);
1023
1024 xd = ref->xd;
1025 if (xd->xprefix != xprefix)
1026 continue;
1027 if (!xd->xname) {
1028 /* xdatum is unchached */
1029 if (!retry) {
1030 retry = 1;
1031 up_read(&c->xattr_sem);
1032 down_write(&c->xattr_sem);
1033 goto retry;
1034 } else {
1035 rc = load_xattr_datum(c, xd);
1036 if (unlikely(rc > 0)) {
1037 *pref = ref->next;
1038 delete_xattr_ref(c, ref);
1039 goto retry;
1040 } else if (unlikely(rc < 0)) {
1041 goto out;
1042 }
1043 }
1044 }
1045 if (!strcmp(xname, xd->xname)) {
1046 rc = xd->value_len;
1047 if (buffer) {
1048 if (size < rc) {
1049 rc = -ERANGE;
1050 } else {
1051 memcpy(buffer, xd->xvalue, rc);
1052 }
1053 }
1054 goto out;
1055 }
1056 }
1057 rc = -ENODATA;
1058 out:
1059 if (!retry) {
1060 up_read(&c->xattr_sem);
1061 } else {
1062 up_write(&c->xattr_sem);
1063 }
1064 return rc;
1065}
1066
1067int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
1068 const char *buffer, size_t size, int flags)
1069{
1070 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1071 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
1072 struct jffs2_inode_cache *ic = f->inocache;
1073 struct jffs2_xattr_datum *xd;
1074 struct jffs2_xattr_ref *ref, *newref, **pref;
1075 uint32_t length, request;
1076 int rc;
1077
1078 rc = check_xattr_ref_inode(c, ic);
1079 if (unlikely(rc))
1080 return rc;
1081
1082 request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
1083 rc = jffs2_reserve_space(c, request, &length,
1084 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
1085 if (rc) {
1086 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1087 return rc;
1088 }
1089
1090 /* Find existing xattr */
1091 down_write(&c->xattr_sem);
1092 retry:
1093 for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
1094 xd = ref->xd;
1095 if (xd->xprefix != xprefix)
1096 continue;
1097 if (!xd->xname) {
1098 rc = load_xattr_datum(c, xd);
1099 if (unlikely(rc > 0)) {
1100 *pref = ref->next;
1101 delete_xattr_ref(c, ref);
1102 goto retry;
1103 } else if (unlikely(rc < 0))
1104 goto out;
1105 }
1106 if (!strcmp(xd->xname, xname)) {
1107 if (flags & XATTR_CREATE) {
1108 rc = -EEXIST;
1109 goto out;
1110 }
1111 if (!buffer) {
1112 ref->ino = ic->ino;
1113 ref->xid = xd->xid;
1114 ref->xseqno |= XREF_DELETE_MARKER;
1115 rc = save_xattr_ref(c, ref);
1116 if (!rc) {
1117 *pref = ref->next;
1118 spin_lock(&c->erase_completion_lock);
1119 ref->next = c->xref_dead_list;
1120 c->xref_dead_list = ref;
1121 spin_unlock(&c->erase_completion_lock);
1122 if (atomic_dec_and_test(&xd->refcnt))
1123 delete_xattr_datum(c, xd);
1124 } else {
1125 ref->ic = ic;
1126 ref->xd = xd;
1127 ref->xseqno &= ~XREF_DELETE_MARKER;
1128 }
1129 goto out;
1130 }
1131 goto found;
1132 }
1133 }
1134 /* not found */
1135 if (flags & XATTR_REPLACE) {
1136 rc = -ENODATA;
1137 goto out;
1138 }
1139 if (!buffer) {
1140 rc = -ENODATA;
1141 goto out;
1142 }
1143 found:
1144 xd = create_xattr_datum(c, xprefix, xname, buffer, size);
1145 if (IS_ERR(xd)) {
1146 rc = PTR_ERR(xd);
1147 goto out;
1148 }
1149 up_write(&c->xattr_sem);
1150 jffs2_complete_reservation(c);
1151
1152 /* create xattr_ref */
1153 request = PAD(sizeof(struct jffs2_raw_xref));
1154 rc = jffs2_reserve_space(c, request, &length,
1155 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
1156 down_write(&c->xattr_sem);
1157 if (rc) {
1158 JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
1159 if (atomic_dec_and_test(&xd->refcnt))
1160 delete_xattr_datum(c, xd);
1161 up_write(&c->xattr_sem);
1162 return rc;
1163 }
1164 if (ref)
1165 *pref = ref->next;
1166 newref = create_xattr_ref(c, ic, xd);
1167 if (IS_ERR(newref)) {
1168 if (ref) {
1169 ref->next = ic->xref;
1170 ic->xref = ref;
1171 }
1172 rc = PTR_ERR(newref);
1173 if (atomic_dec_and_test(&xd->refcnt))
1174 delete_xattr_datum(c, xd);
1175 } else if (ref) {
1176 delete_xattr_ref(c, ref);
1177 }
1178 out:
1179 up_write(&c->xattr_sem);
1180 jffs2_complete_reservation(c);
1181 return rc;
1182}
1183
1184/* -------- garbage collector functions -------------
1185 * jffs2_garbage_collect_xattr_datum(c, xd, raw)
1186 * is used to move xdatum into new node.
1187 * jffs2_garbage_collect_xattr_ref(c, ref, raw)
1188 * is used to move xref into new node.
1189 * jffs2_verify_xattr(c)
1190 * is used to call do_verify_xattr_datum() before garbage collecting.
1191 * jffs2_release_xattr_datum(c, xd)
1192 * is used to release an in-memory object of xdatum.
1193 * jffs2_release_xattr_ref(c, ref)
1194 * is used to release an in-memory object of xref.
1195 * -------------------------------------------------- */
1196int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
1197 struct jffs2_raw_node_ref *raw)
1198{
1199 uint32_t totlen, length, old_ofs;
1200 int rc = 0;
1201
1202 down_write(&c->xattr_sem);
1203 if (xd->node != raw)
1204 goto out;
1205 if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
1206 goto out;
1207
1208 rc = load_xattr_datum(c, xd);
1209 if (unlikely(rc)) {
1210 rc = (rc > 0) ? 0 : rc;
1211 goto out;
1212 }
1213 old_ofs = ref_offset(xd->node);
1214 totlen = PAD(sizeof(struct jffs2_raw_xattr)
1215 + xd->name_len + 1 + xd->value_len);
1216 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
1217 if (rc) {
1218 JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
1219 rc = rc ? rc : -EBADFD;
1220 goto out;
1221 }
1222 rc = save_xattr_datum(c, xd);
1223 if (!rc)
1224 dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
1225 xd->xid, xd->version, old_ofs, ref_offset(xd->node));
1226 out:
1227 if (!rc)
1228 jffs2_mark_node_obsolete(c, raw);
1229 up_write(&c->xattr_sem);
1230 return rc;
1231}
1232
1233int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
1234 struct jffs2_raw_node_ref *raw)
1235{
1236 uint32_t totlen, length, old_ofs;
1237 int rc = 0;
1238
1239 down_write(&c->xattr_sem);
1240 BUG_ON(!ref->node);
1241
1242 if (ref->node != raw)
1243 goto out;
1244 if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
1245 goto out;
1246
1247 old_ofs = ref_offset(ref->node);
1248 totlen = ref_totlen(c, c->gcblock, ref->node);
1249
1250 rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
1251 if (rc) {
1252 JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
1253 __FUNCTION__, rc, totlen);
1254 rc = rc ? rc : -EBADFD;
1255 goto out;
1256 }
1257 rc = save_xattr_ref(c, ref);
1258 if (!rc)
1259 dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
1260 ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
1261 out:
1262 if (!rc)
1263 jffs2_mark_node_obsolete(c, raw);
1264 up_write(&c->xattr_sem);
1265 return rc;
1266}
1267
1268int jffs2_verify_xattr(struct jffs2_sb_info *c)
1269{
1270 struct jffs2_xattr_datum *xd, *_xd;
1271 struct jffs2_eraseblock *jeb;
1272 struct jffs2_raw_node_ref *raw;
1273 uint32_t totlen;
1274 int rc;
1275
1276 down_write(&c->xattr_sem);
1277 list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
1278 rc = do_verify_xattr_datum(c, xd);
1279 if (rc < 0)
1280 continue;
1281 list_del_init(&xd->xindex);
1282 spin_lock(&c->erase_completion_lock);
1283 for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
1284 if (ref_flags(raw) != REF_UNCHECKED)
1285 continue;
1286 jeb = &c->blocks[ref_offset(raw) / c->sector_size];
1287 totlen = PAD(ref_totlen(c, jeb, raw));
1288 c->unchecked_size -= totlen; c->used_size += totlen;
1289 jeb->unchecked_size -= totlen; jeb->used_size += totlen;
1290 raw->flash_offset = ref_offset(raw)
1291 | ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
1292 }
1293 if (xd->flags & JFFS2_XFLAGS_DEAD)
1294 list_add(&xd->xindex, &c->xattr_dead_list);
1295 spin_unlock(&c->erase_completion_lock);
1296 }
1297 up_write(&c->xattr_sem);
1298 return list_empty(&c->xattr_unchecked) ? 1 : 0;
1299}
1300
1301void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
1302{
1303 /* must be called under spin_lock(&c->erase_completion_lock) */
1304 if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
1305 return;
1306
1307 list_del(&xd->xindex);
1308 jffs2_free_xattr_datum(xd);
1309}
1310
1311void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
1312{
1313 /* must be called under spin_lock(&c->erase_completion_lock) */
1314 struct jffs2_xattr_ref *tmp, **ptmp;
1315
1316 if (ref->node != (void *)ref)
1317 return;
1318
1319 for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
1320 if (ref == tmp) {
1321 *ptmp = tmp->next;
1322 break;
1323 }
1324 }
1325 jffs2_free_xattr_ref(ref);
1326}
diff --git a/fs/jffs2/xattr.h b/fs/jffs2/xattr.h
new file mode 100644
index 000000000000..06a5c69dcf8b
--- /dev/null
+++ b/fs/jffs2/xattr.h
@@ -0,0 +1,129 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11#ifndef _JFFS2_FS_XATTR_H_
12#define _JFFS2_FS_XATTR_H_
13
14#include <linux/xattr.h>
15#include <linux/list.h>
16
17#define JFFS2_XFLAGS_HOT (0x01) /* This datum is HOT */
18#define JFFS2_XFLAGS_BIND (0x02) /* This datum is not reclaimed */
19#define JFFS2_XFLAGS_DEAD (0x40) /* This datum is already dead */
20#define JFFS2_XFLAGS_INVALID (0x80) /* This datum contains crc error */
21
22struct jffs2_xattr_datum
23{
24 void *always_null;
25 struct jffs2_raw_node_ref *node;
26 uint8_t class;
27 uint8_t flags;
28 uint16_t xprefix; /* see JFFS2_XATTR_PREFIX_* */
29
30 struct list_head xindex; /* chained from c->xattrindex[n] */
31 atomic_t refcnt; /* # of xattr_ref refers this */
32 uint32_t xid;
33 uint32_t version;
34
35 uint32_t data_crc;
36 uint32_t hashkey;
37 char *xname; /* XATTR name without prefix */
38 uint32_t name_len; /* length of xname */
39 char *xvalue; /* XATTR value */
40 uint32_t value_len; /* length of xvalue */
41};
42
43struct jffs2_inode_cache;
44struct jffs2_xattr_ref
45{
46 void *always_null;
47 struct jffs2_raw_node_ref *node;
48 uint8_t class;
49 uint8_t flags; /* Currently unused */
50 u16 unused;
51
52 uint32_t xseqno;
53 union {
54 struct jffs2_inode_cache *ic; /* reference to jffs2_inode_cache */
55 uint32_t ino; /* only used in scanning/building */
56 };
57 union {
58 struct jffs2_xattr_datum *xd; /* reference to jffs2_xattr_datum */
59 uint32_t xid; /* only used in sccanning/building */
60 };
61 struct jffs2_xattr_ref *next; /* chained from ic->xref_list */
62};
63
64#define XREF_DELETE_MARKER (0x00000001)
65static inline int is_xattr_ref_dead(struct jffs2_xattr_ref *ref)
66{
67 return ((ref->xseqno & XREF_DELETE_MARKER) != 0);
68}
69
70#ifdef CONFIG_JFFS2_FS_XATTR
71
72extern void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c);
73extern void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c);
74extern void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c);
75
76extern struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
77 uint32_t xid, uint32_t version);
78
79extern void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic);
80extern void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic);
81
82extern int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
83 struct jffs2_raw_node_ref *raw);
84extern int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
85 struct jffs2_raw_node_ref *raw);
86extern int jffs2_verify_xattr(struct jffs2_sb_info *c);
87extern void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd);
88extern void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref);
89
90extern int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
91 char *buffer, size_t size);
92extern int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
93 const char *buffer, size_t size, int flags);
94
95extern struct xattr_handler *jffs2_xattr_handlers[];
96extern struct xattr_handler jffs2_user_xattr_handler;
97extern struct xattr_handler jffs2_trusted_xattr_handler;
98
99extern ssize_t jffs2_listxattr(struct dentry *, char *, size_t);
100#define jffs2_getxattr generic_getxattr
101#define jffs2_setxattr generic_setxattr
102#define jffs2_removexattr generic_removexattr
103
104#else
105
106#define jffs2_init_xattr_subsystem(c)
107#define jffs2_build_xattr_subsystem(c)
108#define jffs2_clear_xattr_subsystem(c)
109
110#define jffs2_xattr_delete_inode(c, ic)
111#define jffs2_xattr_free_inode(c, ic)
112#define jffs2_verify_xattr(c) (1)
113
114#define jffs2_xattr_handlers NULL
115#define jffs2_listxattr NULL
116#define jffs2_getxattr NULL
117#define jffs2_setxattr NULL
118#define jffs2_removexattr NULL
119
120#endif /* CONFIG_JFFS2_FS_XATTR */
121
122#ifdef CONFIG_JFFS2_FS_SECURITY
123extern int jffs2_init_security(struct inode *inode, struct inode *dir);
124extern struct xattr_handler jffs2_security_xattr_handler;
125#else
126#define jffs2_init_security(inode,dir) (0)
127#endif /* CONFIG_JFFS2_FS_SECURITY */
128
129#endif /* _JFFS2_FS_XATTR_H_ */
diff --git a/fs/jffs2/xattr_trusted.c b/fs/jffs2/xattr_trusted.c
new file mode 100644
index 000000000000..ed046e19dbfa
--- /dev/null
+++ b/fs/jffs2/xattr_trusted.c
@@ -0,0 +1,52 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/fs.h>
13#include <linux/jffs2.h>
14#include <linux/xattr.h>
15#include <linux/mtd/mtd.h>
16#include "nodelist.h"
17
18static int jffs2_trusted_getxattr(struct inode *inode, const char *name,
19 void *buffer, size_t size)
20{
21 if (!strcmp(name, ""))
22 return -EINVAL;
23 return do_jffs2_getxattr(inode, JFFS2_XPREFIX_TRUSTED, name, buffer, size);
24}
25
26static int jffs2_trusted_setxattr(struct inode *inode, const char *name, const void *buffer,
27 size_t size, int flags)
28{
29 if (!strcmp(name, ""))
30 return -EINVAL;
31 return do_jffs2_setxattr(inode, JFFS2_XPREFIX_TRUSTED, name, buffer, size, flags);
32}
33
34static size_t jffs2_trusted_listxattr(struct inode *inode, char *list, size_t list_size,
35 const char *name, size_t name_len)
36{
37 size_t retlen = XATTR_TRUSTED_PREFIX_LEN + name_len + 1;
38
39 if (list && retlen<=list_size) {
40 strcpy(list, XATTR_TRUSTED_PREFIX);
41 strcpy(list + XATTR_TRUSTED_PREFIX_LEN, name);
42 }
43
44 return retlen;
45}
46
47struct xattr_handler jffs2_trusted_xattr_handler = {
48 .prefix = XATTR_TRUSTED_PREFIX,
49 .list = jffs2_trusted_listxattr,
50 .set = jffs2_trusted_setxattr,
51 .get = jffs2_trusted_getxattr
52};
diff --git a/fs/jffs2/xattr_user.c b/fs/jffs2/xattr_user.c
new file mode 100644
index 000000000000..2f8e9aa01ea0
--- /dev/null
+++ b/fs/jffs2/xattr_user.c
@@ -0,0 +1,52 @@
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2006 NEC Corporation
5 *
6 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/fs.h>
13#include <linux/jffs2.h>
14#include <linux/xattr.h>
15#include <linux/mtd/mtd.h>
16#include "nodelist.h"
17
18static int jffs2_user_getxattr(struct inode *inode, const char *name,
19 void *buffer, size_t size)
20{
21 if (!strcmp(name, ""))
22 return -EINVAL;
23 return do_jffs2_getxattr(inode, JFFS2_XPREFIX_USER, name, buffer, size);
24}
25
26static int jffs2_user_setxattr(struct inode *inode, const char *name, const void *buffer,
27 size_t size, int flags)
28{
29 if (!strcmp(name, ""))
30 return -EINVAL;
31 return do_jffs2_setxattr(inode, JFFS2_XPREFIX_USER, name, buffer, size, flags);
32}
33
34static size_t jffs2_user_listxattr(struct inode *inode, char *list, size_t list_size,
35 const char *name, size_t name_len)
36{
37 size_t retlen = XATTR_USER_PREFIX_LEN + name_len + 1;
38
39 if (list && retlen <= list_size) {
40 strcpy(list, XATTR_USER_PREFIX);
41 strcpy(list + XATTR_USER_PREFIX_LEN, name);
42 }
43
44 return retlen;
45}
46
47struct xattr_handler jffs2_user_xattr_handler = {
48 .prefix = XATTR_USER_PREFIX,
49 .list = jffs2_user_listxattr,
50 .set = jffs2_user_setxattr,
51 .get = jffs2_user_getxattr
52};