• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff -Nupr old/fs/jffs2/acl.c new/fs/jffs2/acl.c
2--- old/fs/jffs2/acl.c	2022-05-09 17:15:24.350000000 +0800
3+++ new/fs/jffs2/acl.c	1970-01-01 08:00:00.000000000 +0800
4@@ -1,307 +0,0 @@
5-/*
6- * JFFS2 -- Journalling Flash File System, Version 2.
7- *
8- * Copyright © 2006  NEC Corporation
9- *
10- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
11- *
12- * For licensing information, see the file 'LICENCE' in this directory.
13- *
14- */
15-
16-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17-
18-#include <linux/kernel.h>
19-#include <linux/slab.h>
20-#include <linux/fs.h>
21-#include <linux/sched.h>
22-#include <linux/time.h>
23-#include <linux/crc32.h>
24-#include <linux/jffs2.h>
25-#include <linux/xattr.h>
26-#include <linux/posix_acl_xattr.h>
27-#include <linux/mtd/mtd.h>
28-#include "nodelist.h"
29-
30-static size_t jffs2_acl_size(int count)
31-{
32-	if (count <= 4) {
33-		return sizeof(struct jffs2_acl_header)
34-		       + count * sizeof(struct jffs2_acl_entry_short);
35-	} else {
36-		return sizeof(struct jffs2_acl_header)
37-		       + 4 * sizeof(struct jffs2_acl_entry_short)
38-		       + (count - 4) * sizeof(struct jffs2_acl_entry);
39-	}
40-}
41-
42-static int jffs2_acl_count(size_t size)
43-{
44-	size_t s;
45-
46-	size -= sizeof(struct jffs2_acl_header);
47-	if (size < 4 * sizeof(struct jffs2_acl_entry_short)) {
48-		if (size % sizeof(struct jffs2_acl_entry_short))
49-			return -1;
50-		return size / sizeof(struct jffs2_acl_entry_short);
51-	} else {
52-		s = size - 4 * sizeof(struct jffs2_acl_entry_short);
53-		if (s % sizeof(struct jffs2_acl_entry))
54-			return -1;
55-		return s / sizeof(struct jffs2_acl_entry) + 4;
56-	}
57-}
58-
59-static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
60-{
61-	void *end = value + size;
62-	struct jffs2_acl_header *header = value;
63-	struct jffs2_acl_entry *entry;
64-	struct posix_acl *acl;
65-	uint32_t ver;
66-	int i, count;
67-
68-	if (!value)
69-		return NULL;
70-	if (size < sizeof(struct jffs2_acl_header))
71-		return ERR_PTR(-EINVAL);
72-	ver = je32_to_cpu(header->a_version);
73-	if (ver != JFFS2_ACL_VERSION) {
74-		JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
75-		return ERR_PTR(-EINVAL);
76-	}
77-
78-	value += sizeof(struct jffs2_acl_header);
79-	count = jffs2_acl_count(size);
80-	if (count < 0)
81-		return ERR_PTR(-EINVAL);
82-	if (count == 0)
83-		return NULL;
84-
85-	acl = posix_acl_alloc(count, GFP_KERNEL);
86-	if (!acl)
87-		return ERR_PTR(-ENOMEM);
88-
89-	for (i=0; i < count; i++) {
90-		entry = value;
91-		if (value + sizeof(struct jffs2_acl_entry_short) > end)
92-			goto fail;
93-		acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
94-		acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
95-		switch (acl->a_entries[i].e_tag) {
96-			case ACL_USER_OBJ:
97-			case ACL_GROUP_OBJ:
98-			case ACL_MASK:
99-			case ACL_OTHER:
100-				value += sizeof(struct jffs2_acl_entry_short);
101-				break;
102-
103-			case ACL_USER:
104-				value += sizeof(struct jffs2_acl_entry);
105-				if (value > end)
106-					goto fail;
107-				acl->a_entries[i].e_uid =
108-					make_kuid(&init_user_ns,
109-						  je32_to_cpu(entry->e_id));
110-				break;
111-			case ACL_GROUP:
112-				value += sizeof(struct jffs2_acl_entry);
113-				if (value > end)
114-					goto fail;
115-				acl->a_entries[i].e_gid =
116-					make_kgid(&init_user_ns,
117-						  je32_to_cpu(entry->e_id));
118-				break;
119-
120-			default:
121-				goto fail;
122-		}
123-	}
124-	if (value != end)
125-		goto fail;
126-	return acl;
127- fail:
128-	posix_acl_release(acl);
129-	return ERR_PTR(-EINVAL);
130-}
131-
132-static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
133-{
134-	struct jffs2_acl_header *header;
135-	struct jffs2_acl_entry *entry;
136-	void *e;
137-	size_t i;
138-
139-	*size = jffs2_acl_size(acl->a_count);
140-	header = kmalloc(struct_size(header, a_entries, acl->a_count),
141-			GFP_KERNEL);
142-	if (!header)
143-		return ERR_PTR(-ENOMEM);
144-	header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
145-	e = header + 1;
146-	for (i=0; i < acl->a_count; i++) {
147-		const struct posix_acl_entry *acl_e = &acl->a_entries[i];
148-		entry = e;
149-		entry->e_tag = cpu_to_je16(acl_e->e_tag);
150-		entry->e_perm = cpu_to_je16(acl_e->e_perm);
151-		switch(acl_e->e_tag) {
152-			case ACL_USER:
153-				entry->e_id = cpu_to_je32(
154-					from_kuid(&init_user_ns, acl_e->e_uid));
155-				e += sizeof(struct jffs2_acl_entry);
156-				break;
157-			case ACL_GROUP:
158-				entry->e_id = cpu_to_je32(
159-					from_kgid(&init_user_ns, acl_e->e_gid));
160-				e += sizeof(struct jffs2_acl_entry);
161-				break;
162-
163-			case ACL_USER_OBJ:
164-			case ACL_GROUP_OBJ:
165-			case ACL_MASK:
166-			case ACL_OTHER:
167-				e += sizeof(struct jffs2_acl_entry_short);
168-				break;
169-
170-			default:
171-				goto fail;
172-		}
173-	}
174-	return header;
175- fail:
176-	kfree(header);
177-	return ERR_PTR(-EINVAL);
178-}
179-
180-struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
181-{
182-	struct posix_acl *acl;
183-	char *value = NULL;
184-	int rc, xprefix;
185-
186-	switch (type) {
187-	case ACL_TYPE_ACCESS:
188-		xprefix = JFFS2_XPREFIX_ACL_ACCESS;
189-		break;
190-	case ACL_TYPE_DEFAULT:
191-		xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
192-		break;
193-	default:
194-		BUG();
195-	}
196-	rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
197-	if (rc > 0) {
198-		value = kmalloc(rc, GFP_KERNEL);
199-		if (!value)
200-			return ERR_PTR(-ENOMEM);
201-		rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
202-	}
203-	if (rc > 0) {
204-		acl = jffs2_acl_from_medium(value, rc);
205-	} else if (rc == -ENODATA || rc == -ENOSYS) {
206-		acl = NULL;
207-	} else {
208-		acl = ERR_PTR(rc);
209-	}
210-	kfree(value);
211-	return acl;
212-}
213-
214-static int __jffs2_set_acl(struct inode *inode, int xprefix, struct posix_acl *acl)
215-{
216-	char *value = NULL;
217-	size_t size = 0;
218-	int rc;
219-
220-	if (acl) {
221-		value = jffs2_acl_to_medium(acl, &size);
222-		if (IS_ERR(value))
223-			return PTR_ERR(value);
224-	}
225-	rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
226-	if (!value && rc == -ENODATA)
227-		rc = 0;
228-	kfree(value);
229-
230-	return rc;
231-}
232-
233-int jffs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
234-{
235-	int rc, xprefix;
236-
237-	switch (type) {
238-	case ACL_TYPE_ACCESS:
239-		xprefix = JFFS2_XPREFIX_ACL_ACCESS;
240-		if (acl) {
241-			umode_t mode;
242-
243-			rc = posix_acl_update_mode(inode, &mode, &acl);
244-			if (rc)
245-				return rc;
246-			if (inode->i_mode != mode) {
247-				struct iattr attr;
248-
249-				attr.ia_valid = ATTR_MODE | ATTR_CTIME;
250-				attr.ia_mode = mode;
251-				attr.ia_ctime = current_time(inode);
252-				rc = jffs2_do_setattr(inode, &attr);
253-				if (rc < 0)
254-					return rc;
255-			}
256-		}
257-		break;
258-	case ACL_TYPE_DEFAULT:
259-		xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
260-		if (!S_ISDIR(inode->i_mode))
261-			return acl ? -EACCES : 0;
262-		break;
263-	default:
264-		return -EINVAL;
265-	}
266-	rc = __jffs2_set_acl(inode, xprefix, acl);
267-	if (!rc)
268-		set_cached_acl(inode, type, acl);
269-	return rc;
270-}
271-
272-int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, umode_t *i_mode)
273-{
274-	struct posix_acl *default_acl, *acl;
275-	int rc;
276-
277-	cache_no_acl(inode);
278-
279-	rc = posix_acl_create(dir_i, i_mode, &default_acl, &acl);
280-	if (rc)
281-		return rc;
282-
283-	if (default_acl) {
284-		set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
285-		posix_acl_release(default_acl);
286-	}
287-	if (acl) {
288-		set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
289-		posix_acl_release(acl);
290-	}
291-	return 0;
292-}
293-
294-int jffs2_init_acl_post(struct inode *inode)
295-{
296-	int rc;
297-
298-	if (inode->i_default_acl) {
299-		rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_DEFAULT, inode->i_default_acl);
300-		if (rc)
301-			return rc;
302-	}
303-
304-	if (inode->i_acl) {
305-		rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_ACCESS, inode->i_acl);
306-		if (rc)
307-			return rc;
308-	}
309-
310-	return 0;
311-}
312diff -Nupr old/fs/jffs2/acl.h new/fs/jffs2/acl.h
313--- old/fs/jffs2/acl.h	2022-05-09 17:22:53.000000000 +0800
314+++ new/fs/jffs2/acl.h	2022-05-10 14:52:22.930000000 +0800
315@@ -8,6 +8,8 @@
316  * For licensing information, see the file 'LICENCE' in this directory.
317  *
318  */
319+#ifndef _JFFS2_ACL_H_
320+#define _JFFS2_ACL_H_
321
322 struct jffs2_acl_entry {
323 	jint16_t	e_tag;
324@@ -27,11 +29,6 @@ struct jffs2_acl_header {
325
326 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
327
328-struct posix_acl *jffs2_get_acl(struct inode *inode, int type);
329-int jffs2_set_acl(struct inode *inode, struct posix_acl *acl, int type);
330-extern int jffs2_init_acl_pre(struct inode *, struct inode *, umode_t *);
331-extern int jffs2_init_acl_post(struct inode *);
332-
333 #else
334
335 #define jffs2_get_acl				(NULL)
336@@ -40,3 +37,4 @@ extern int jffs2_init_acl_post(struct in
337 #define jffs2_init_acl_post(inode)		(0)
338
339 #endif	/* CONFIG_JFFS2_FS_POSIX_ACL */
340+#endif  /* _JFFS2_ACL_H_ */
341diff -Nupr old/fs/jffs2/background.c new/fs/jffs2/background.c
342--- old/fs/jffs2/background.c	2022-05-09 17:22:53.000000000 +0800
343+++ new/fs/jffs2/background.c	2022-05-10 14:53:26.200000000 +0800
344@@ -10,156 +10,113 @@
345  *
346  */
347
348-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
349-
350 #include <linux/kernel.h>
351-#include <linux/jffs2.h>
352-#include <linux/mtd/mtd.h>
353-#include <linux/completion.h>
354-#include <linux/sched/signal.h>
355-#include <linux/freezer.h>
356-#include <linux/kthread.h>
357+#include <stdio.h>
358 #include "nodelist.h"
359+#include "vfs_jffs2.h"
360+#include "mtd_partition.h"
361
362+#define GC_THREAD_FLAG_TRIG 1
363+#define GC_THREAD_FLAG_STOP 2
364+#define GC_THREAD_FLAG_HAS_EXIT 4
365
366-static int jffs2_garbage_collect_thread(void *);
367+extern struct MtdNorDev jffs2_dev_list[CONFIG_MTD_PATTITION_NUM];
368+static void jffs2_garbage_collect_thread(unsigned long data);
369
370 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
371 {
372-	assert_spin_locked(&c->erase_completion_lock);
373-	if (c->gc_task && jffs2_thread_should_wake(c))
374-		send_sig(SIGHUP, c->gc_task, 1);
375+	struct super_block *sb = OFNI_BS_2SFFJ(c);
376+	/* Wake up the thread */
377+	jffs2_dbg(1, "jffs2_garbage_collect_trigger\n");
378+	LOS_EventWrite(&sb->s_gc_thread_flags, GC_THREAD_FLAG_TRIG);
379 }
380
381 /* This must only ever be called when no GC thread is currently running */
382-int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
383+void jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
384 {
385-	struct task_struct *tsk;
386-	int ret = 0;
387+	struct super_block *sb = OFNI_BS_2SFFJ(c);
388+	TSK_INIT_PARAM_S stGcTask;
389
390-	BUG_ON(c->gc_task);
391+	if (c == NULL)
392+		return;
393
394-	init_completion(&c->gc_thread_start);
395-	init_completion(&c->gc_thread_exit);
396+	if (sb->s_root == NULL)
397+		return;
398
399-	tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index);
400-	if (IS_ERR(tsk)) {
401-		pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n",
402-			-PTR_ERR(tsk));
403-		complete(&c->gc_thread_exit);
404-		ret = PTR_ERR(tsk);
405-	} else {
406-		/* Wait for it... */
407-		jffs2_dbg(1, "Garbage collect thread is pid %d\n", tsk->pid);
408-		wait_for_completion(&c->gc_thread_start);
409-		ret = tsk->pid;
410+	LOS_EventInit(&sb->s_gc_thread_flags);
411+
412+	/* Start the thread. Doesn't matter if it fails -- it's only an
413+	 * optimisation anyway */
414+	(void)memset_s(&stGcTask, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
415+
416+	stGcTask.pfnTaskEntry = (TSK_ENTRY_FUNC)jffs2_garbage_collect_thread;
417+	stGcTask.auwArgs[0] = (UINTPTR)c;
418+	stGcTask.uwStackSize  = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
419+	stGcTask.pcName = "jffs2_gc_thread";
420+#ifdef LOSCFG_KERNEL_SMP
421+	unsigned int i;
422+	for (i = 0; i < CONFIG_MTD_PATTITION_NUM; i++) {
423+		if (sb->s_dev == &jffs2_dev_list[i])
424+			break;
425 	}
426+	stGcTask.usCpuAffiMask = CPUID_TO_AFFI_MASK(i % LOSCFG_KERNEL_CORE_NUM);
427+#endif
428+	stGcTask.usTaskPrio = JFFS2_GC_THREAD_PRIORITY;
429
430-	return ret;
431+	if (LOS_TaskCreate(&sb->s_gc_thread, &stGcTask))
432+		JFFS2_ERROR("Create gc task failed!!!\n");
433 }
434
435 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
436 {
437-	int wait = 0;
438-	spin_lock(&c->erase_completion_lock);
439-	if (c->gc_task) {
440-		jffs2_dbg(1, "Killing GC task %d\n", c->gc_task->pid);
441-		send_sig(SIGKILL, c->gc_task, 1);
442-		wait = 1;
443-	}
444-	spin_unlock(&c->erase_completion_lock);
445-	if (wait)
446-		wait_for_completion(&c->gc_thread_exit);
447+	struct super_block *sb = OFNI_BS_2SFFJ(c);
448+
449+	JFFS2_DEBUG("jffs2_stop_garbage_collect_thread\n");
450+	/* Stop the thread and wait for it if necessary */
451+
452+	LOS_EventWrite(&sb->s_gc_thread_flags, GC_THREAD_FLAG_STOP);
453+
454+	JFFS2_DEBUG("jffs2_stop_garbage_collect_thread wait\n");
455+
456+	(void)LOS_EventRead(&sb->s_gc_thread_flags,
457+			GC_THREAD_FLAG_HAS_EXIT,
458+			LOS_WAITMODE_OR | LOS_WAITMODE_CLR,
459+			LOS_WAIT_FOREVER);
460+
461+	// Kill and free the resources ...  this is safe due to the flag
462+	// from the thread.
463+	(void)LOS_TaskDelete(sb->s_gc_thread);
464+	(void)LOS_EventWrite(&sb->s_gc_thread_flags, 0xFFFFFFFF);
465 }
466
467-static int jffs2_garbage_collect_thread(void *_c)
468+static void jffs2_garbage_collect_thread(unsigned long data)
469 {
470-	struct jffs2_sb_info *c = _c;
471-	sigset_t hupmask;
472+	struct jffs2_sb_info *c = (struct jffs2_sb_info *)data;
473+	struct super_block *sb = OFNI_BS_2SFFJ(c);
474+	unsigned int flag = 0;
475+
476+	jffs2_dbg(1, "jffs2_garbage_collect_thread START\n");
477+	while(1) {
478+		flag = LOS_EventRead(&sb->s_gc_thread_flags,
479+			GC_THREAD_FLAG_TRIG | GC_THREAD_FLAG_STOP,
480+			LOS_WAITMODE_OR | LOS_WAITMODE_CLR,
481+			LOS_WAIT_FOREVER
482+		);
483+		if (flag & GC_THREAD_FLAG_STOP)
484+			break;
485
486-	siginitset(&hupmask, sigmask(SIGHUP));
487-	allow_signal(SIGKILL);
488-	allow_signal(SIGSTOP);
489-	allow_signal(SIGHUP);
490-
491-	c->gc_task = current;
492-	complete(&c->gc_thread_start);
493-
494-	set_user_nice(current, 10);
495-
496-	set_freezable();
497-	for (;;) {
498-		sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
499-	again:
500-		spin_lock(&c->erase_completion_lock);
501-		if (!jffs2_thread_should_wake(c)) {
502-			set_current_state (TASK_INTERRUPTIBLE);
503-			spin_unlock(&c->erase_completion_lock);
504-			jffs2_dbg(1, "%s(): sleeping...\n", __func__);
505-			schedule();
506-		} else {
507-			spin_unlock(&c->erase_completion_lock);
508-		}
509-		/* Problem - immediately after bootup, the GCD spends a lot
510-		 * of time in places like jffs2_kill_fragtree(); so much so
511-		 * that userspace processes (like gdm and X) are starved
512-		 * despite plenty of cond_resched()s and renicing.  Yield()
513-		 * doesn't help, either (presumably because userspace and GCD
514-		 * are generally competing for a higher latency resource -
515-		 * disk).
516-		 * This forces the GCD to slow the hell down.   Pulling an
517-		 * inode in with read_inode() is much preferable to having
518-		 * the GC thread get there first. */
519-		schedule_timeout_interruptible(msecs_to_jiffies(50));
520-
521-		if (kthread_should_stop()) {
522-			jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
523-			goto die;
524-		}
525+		jffs2_dbg(1, "jffs2: GC THREAD GC BEGIN\n");
526
527-		/* Put_super will send a SIGKILL and then wait on the sem.
528-		 */
529-		while (signal_pending(current) || freezing(current)) {
530-			unsigned long signr;
531-
532-			if (try_to_freeze())
533-				goto again;
534-
535-			signr = kernel_dequeue_signal();
536-
537-			switch(signr) {
538-			case SIGSTOP:
539-				jffs2_dbg(1, "%s(): SIGSTOP received\n",
540-					  __func__);
541-				kernel_signal_stop();
542-				break;
543-
544-			case SIGKILL:
545-				jffs2_dbg(1, "%s(): SIGKILL received\n",
546-					  __func__);
547-				goto die;
548-
549-			case SIGHUP:
550-				jffs2_dbg(1, "%s(): SIGHUP received\n",
551-					  __func__);
552-				break;
553-			default:
554-				jffs2_dbg(1, "%s(): signal %ld received\n",
555-					  __func__, signr);
556-			}
557-		}
558-		/* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
559-		sigprocmask(SIG_BLOCK, &hupmask, NULL);
560+		if (sb->s_root == NULL)
561+			return;
562
563-		jffs2_dbg(1, "%s(): pass\n", __func__);
564 		if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
565-			pr_notice("No space for garbage collection. Aborting GC thread\n");
566-			goto die;
567+			PRINTK("No space for garbage collection. "
568+			"Aborting JFFS2 GC thread\n");
569+			break;
570 		}
571+		jffs2_dbg(1, "jffs2: GC THREAD GC END\n");
572 	}
573- die:
574-	spin_lock(&c->erase_completion_lock);
575-	c->gc_task = NULL;
576-	spin_unlock(&c->erase_completion_lock);
577-	complete_and_exit(&c->gc_thread_exit, 0);
578+	JFFS2_DEBUG("jffs2_garbage_collect_thread EXIT\n");
579+	LOS_EventWrite(&sb->s_gc_thread_flags, GC_THREAD_FLAG_HAS_EXIT);
580 }
581diff -Nupr old/fs/jffs2/build.c new/fs/jffs2/build.c
582--- old/fs/jffs2/build.c	2022-05-09 17:22:53.000000000 +0800
583+++ new/fs/jffs2/build.c	2022-05-10 15:01:38.800000000 +0800
584@@ -10,15 +10,13 @@
585  *
586  */
587
588-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
589-
590+#include <dirent.h>
591 #include <linux/kernel.h>
592 #include <linux/sched.h>
593 #include <linux/slab.h>
594-#include <linux/vmalloc.h>
595-#include <linux/mtd/mtd.h>
596-#include <linux/mm.h> /* kvfree() */
597+#include <mtd_dev.h>
598 #include "nodelist.h"
599+#include "los_exc.h"
600
601 static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *,
602 		struct jffs2_inode_cache *, struct jffs2_full_dirent **);
603@@ -50,8 +48,7 @@ next_inode(int *i, struct jffs2_inode_ca
604
605
606 static void jffs2_build_inode_pass1(struct jffs2_sb_info *c,
607-				    struct jffs2_inode_cache *ic,
608-				    int *dir_hardlinks)
609+				    struct jffs2_inode_cache *ic)
610 {
611 	struct jffs2_full_dirent *fd;
612
613@@ -372,20 +369,24 @@ int jffs2_do_mount_fs(struct jffs2_sb_in
614 	int ret;
615 	int i;
616 	int size;
617+	struct super_block *sb;
618+	struct MtdNorDev *device;
619
620 	c->free_size = c->flash_size;
621 	c->nr_blocks = c->flash_size / c->sector_size;
622-	size = sizeof(struct jffs2_eraseblock) * c->nr_blocks;
623+	sb = OFNI_BS_2SFFJ(c);
624+	device = (struct MtdNorDev *)(sb->s_dev);
625+	size = sizeof(struct jffs2_eraseblock) *(c->nr_blocks + device->blockStart);
626 #ifndef __ECOS
627 	if (jffs2_blocks_use_vmalloc(c))
628-		c->blocks = vzalloc(size);
629+		c->blocks = malloc(size);
630 	else
631 #endif
632 		c->blocks = kzalloc(size, GFP_KERNEL);
633 	if (!c->blocks)
634 		return -ENOMEM;
635
636-	for (i=0; i<c->nr_blocks; i++) {
637+	for (i = device->blockStart; i < c->nr_blocks + device->blockStart; i++) {
638 		INIT_LIST_HEAD(&c->blocks[i].list);
639 		c->blocks[i].offset = i * c->sector_size;
640 		c->blocks[i].free_size = c->sector_size;
641diff -Nupr old/fs/jffs2/compr.c new/fs/jffs2/compr.c
642--- old/fs/jffs2/compr.c	2022-05-09 17:22:53.000000000 +0800
643+++ new/fs/jffs2/compr.c	2022-05-10 15:02:17.440000000 +0800
644@@ -12,14 +12,13 @@
645  *
646  */
647
648-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
649-
650 #include "compr.h"
651+#include "jffs2.h"
652+#include "user_copy.h"
653
654-static DEFINE_SPINLOCK(jffs2_compressor_list_lock);
655-
656+static spinlock_t jffs2_compressor_list_lock;
657 /* Available compressors are on this list */
658-static LIST_HEAD(jffs2_compressor_list);
659+static LINUX_LIST_HEAD(jffs2_compressor_list);
660
661 /* Actual compression mode */
662 static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
663@@ -71,15 +70,15 @@ static int jffs2_is_best_compression(str
664  * could not be compressed; probably because we couldn't find the requested
665  * compression mode.
666  */
667-static int jffs2_selected_compress(u8 compr, unsigned char *data_in,
668-		unsigned char **cpage_out, u32 *datalen, u32 *cdatalen)
669+static int jffs2_selected_compress(uint8_t compr, unsigned char *data_in,
670+		unsigned char **cpage_out, uint32_t *datalen, uint32_t *cdatalen)
671 {
672 	struct jffs2_compressor *this;
673 	int err, ret = JFFS2_COMPR_NONE;
674 	uint32_t orig_slen, orig_dlen;
675-	char *output_buf;
676+	unsigned char *output_buf;
677
678-	output_buf = kmalloc(*cdatalen, GFP_KERNEL);
679+	output_buf = kmalloc(*cdatalen,GFP_KERNEL);
680 	if (!output_buf) {
681 		pr_warn("No memory for compressor allocation. Compression failed.\n");
682 		return ret;
683@@ -265,11 +264,16 @@ int jffs2_decompress(struct jffs2_sb_inf
684 	switch (comprtype & 0xff) {
685 	case JFFS2_COMPR_NONE:
686 		/* This should be special-cased elsewhere, but we might as well deal with it */
687-		memcpy(data_out, cdata_in, datalen);
688+		if (LOS_CopyFromKernel(data_out, datalen, cdata_in, datalen) != 0) {
689+			return -EFAULT;
690+		}
691 		none_stat_decompr_blocks++;
692 		break;
693 	case JFFS2_COMPR_ZERO:
694-		memset(data_out, 0, datalen);
695+		ret = LOS_UserMemClear(data_out, datalen);
696+		if (ret != 0) {
697+			return ret;
698+		}
699 		break;
700 	default:
701 		spin_lock(&jffs2_compressor_list_lock);
702diff -Nupr old/fs/jffs2/compr.h new/fs/jffs2/compr.h
703--- old/fs/jffs2/compr.h	2022-05-09 17:22:53.000000000 +0800
704+++ new/fs/jffs2/compr.h	2022-05-10 15:02:50.040000000 +0800
705@@ -13,18 +13,20 @@
706 #define __JFFS2_COMPR_H__
707
708 #include <linux/kernel.h>
709-#include <linux/vmalloc.h>
710 #include <linux/list.h>
711 #include <linux/types.h>
712 #include <linux/string.h>
713 #include <linux/slab.h>
714 #include <linux/errno.h>
715-#include <linux/fs.h>
716-#include <linux/jffs2.h>
717-#include "jffs2_fs_i.h"
718-#include "jffs2_fs_sb.h"
719+#include <linux/stat.h>
720 #include "nodelist.h"
721
722+#ifdef __cplusplus
723+#if __cplusplus
724+extern "C" {
725+#endif /* __cplusplus */
726+#endif /* __cplusplus */
727+
728 #define JFFS2_RUBINMIPS_PRIORITY 10
729 #define JFFS2_DYNRUBIN_PRIORITY  20
730 #define JFFS2_LZARI_PRIORITY     30
731@@ -102,4 +104,10 @@ int jffs2_lzo_init(void);
732 void jffs2_lzo_exit(void);
733 #endif
734
735+#ifdef __cplusplus
736+#if __cplusplus
737+}
738+#endif /* __cplusplus */
739+#endif /* __cplusplus */
740+
741 #endif /* __JFFS2_COMPR_H__ */
742diff -Nupr old/fs/jffs2/compr_lzo.c new/fs/jffs2/compr_lzo.c
743--- old/fs/jffs2/compr_lzo.c	2022-05-09 17:15:24.350000000 +0800
744+++ new/fs/jffs2/compr_lzo.c	1970-01-01 08:00:00.000000000 +0800
745@@ -1,110 +0,0 @@
746-/*
747- * JFFS2 -- Journalling Flash File System, Version 2.
748- *
749- * Copyright © 2007 Nokia Corporation. All rights reserved.
750- * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
751- *
752- * Created by Richard Purdie <rpurdie@openedhand.com>
753- *
754- * For licensing information, see the file 'LICENCE' in this directory.
755- *
756- */
757-
758-#include <linux/kernel.h>
759-#include <linux/sched.h>
760-#include <linux/vmalloc.h>
761-#include <linux/init.h>
762-#include <linux/lzo.h>
763-#include "compr.h"
764-
765-static void *lzo_mem;
766-static void *lzo_compress_buf;
767-static DEFINE_MUTEX(deflate_mutex);	/* for lzo_mem and lzo_compress_buf */
768-
769-static void free_workspace(void)
770-{
771-	vfree(lzo_mem);
772-	vfree(lzo_compress_buf);
773-}
774-
775-static int __init alloc_workspace(void)
776-{
777-	lzo_mem = vmalloc(LZO1X_MEM_COMPRESS);
778-	lzo_compress_buf = vmalloc(lzo1x_worst_compress(PAGE_SIZE));
779-
780-	if (!lzo_mem || !lzo_compress_buf) {
781-		free_workspace();
782-		return -ENOMEM;
783-	}
784-
785-	return 0;
786-}
787-
788-static int jffs2_lzo_compress(unsigned char *data_in, unsigned char *cpage_out,
789-			      uint32_t *sourcelen, uint32_t *dstlen)
790-{
791-	size_t compress_size;
792-	int ret;
793-
794-	mutex_lock(&deflate_mutex);
795-	ret = lzo1x_1_compress(data_in, *sourcelen, lzo_compress_buf, &compress_size, lzo_mem);
796-	if (ret != LZO_E_OK)
797-		goto fail;
798-
799-	if (compress_size > *dstlen)
800-		goto fail;
801-
802-	memcpy(cpage_out, lzo_compress_buf, compress_size);
803-	mutex_unlock(&deflate_mutex);
804-
805-	*dstlen = compress_size;
806-	return 0;
807-
808- fail:
809-	mutex_unlock(&deflate_mutex);
810-	return -1;
811-}
812-
813-static int jffs2_lzo_decompress(unsigned char *data_in, unsigned char *cpage_out,
814-				 uint32_t srclen, uint32_t destlen)
815-{
816-	size_t dl = destlen;
817-	int ret;
818-
819-	ret = lzo1x_decompress_safe(data_in, srclen, cpage_out, &dl);
820-
821-	if (ret != LZO_E_OK || dl != destlen)
822-		return -1;
823-
824-	return 0;
825-}
826-
827-static struct jffs2_compressor jffs2_lzo_comp = {
828-	.priority = JFFS2_LZO_PRIORITY,
829-	.name = "lzo",
830-	.compr = JFFS2_COMPR_LZO,
831-	.compress = &jffs2_lzo_compress,
832-	.decompress = &jffs2_lzo_decompress,
833-	.disabled = 0,
834-};
835-
836-int __init jffs2_lzo_init(void)
837-{
838-	int ret;
839-
840-	ret = alloc_workspace();
841-	if (ret < 0)
842-		return ret;
843-
844-	ret = jffs2_register_compressor(&jffs2_lzo_comp);
845-	if (ret)
846-		free_workspace();
847-
848-	return ret;
849-}
850-
851-void jffs2_lzo_exit(void)
852-{
853-	jffs2_unregister_compressor(&jffs2_lzo_comp);
854-	free_workspace();
855-}
856diff -Nupr old/fs/jffs2/compr_rtime.c new/fs/jffs2/compr_rtime.c
857--- old/fs/jffs2/compr_rtime.c	2022-05-09 17:22:53.000000000 +0800
858+++ new/fs/jffs2/compr_rtime.c	2022-05-10 15:05:05.970000000 +0800
859@@ -25,7 +25,7 @@
860 #include <linux/types.h>
861 #include <linux/errno.h>
862 #include <linux/string.h>
863-#include <linux/jffs2.h>
864+#include "jffs2.h"
865 #include "compr.h"
866
867 /* _compress returns the compressed size, -1 if bigger */
868diff -Nupr old/fs/jffs2/compr_rubin.c new/fs/jffs2/compr_rubin.c
869--- old/fs/jffs2/compr_rubin.c	2022-05-09 17:22:53.000000000 +0800
870+++ new/fs/jffs2/compr_rubin.c	2022-05-10 15:05:51.830000000 +0800
871@@ -10,15 +10,12 @@
872  *
873  */
874
875-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
876-
877 #include <linux/string.h>
878 #include <linux/types.h>
879-#include <linux/jffs2.h>
880 #include <linux/errno.h>
881+#include "jffs2.h"
882 #include "compr.h"
883
884-
885 #define RUBIN_REG_SIZE   16
886 #define UPPER_BIT_RUBIN    (((long) 1)<<(RUBIN_REG_SIZE-1))
887 #define LOWER_BITS_RUBIN   ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
888@@ -48,7 +45,7 @@ static inline void init_pushpull(struct
889 				 unsigned buflen, unsigned ofs,
890 				 unsigned reserve)
891 {
892-	pp->buf = buf;
893+	pp->buf = (unsigned char *)buf;
894 	pp->buflen = buflen;
895 	pp->ofs = ofs;
896 	pp->reserve = reserve;
897@@ -267,7 +264,7 @@ static int rubin_do_compress(int bit_div
898 	int pos=0;
899 	struct rubin_state rs;
900
901-	init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
902+	init_pushpull(&rs.pp, (char *)cpage_out, *dstlen * 8, 0, 32);
903
904 	init_rubin(&rs, bit_divider, bits);
905
906@@ -366,14 +363,14 @@ static int jffs2_dynrubin_compress(unsig
907 }
908
909 static void rubin_do_decompress(int bit_divider, int *bits,
910-				unsigned char *cdata_in,
911+				unsigned char *cdata_in,
912 				unsigned char *page_out, uint32_t srclen,
913 				uint32_t destlen)
914 {
915 	int outpos = 0;
916 	struct rubin_state rs;
917
918-	init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
919+	init_pushpull(&rs.pp, (char *)cdata_in, srclen, 0, 0);
920 	init_decode(&rs, bit_divider, bits);
921
922 	while (outpos < destlen)
923diff -Nupr old/fs/jffs2/compr_zlib.c new/fs/jffs2/compr_zlib.c
924--- old/fs/jffs2/compr_zlib.c	2022-05-09 17:22:53.000000000 +0800
925+++ new/fs/jffs2/compr_zlib.c	2022-05-10 15:06:46.640000000 +0800
926@@ -10,15 +10,10 @@
927  *
928  */
929
930-#if !defined(__KERNEL__) && !defined(__ECOS)
931-#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
932-#endif
933-
934-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
935-
936 #include <linux/kernel.h>
937-#include <linux/zlib.h>
938+#include <zlib.h>
939 #include <linux/zutil.h>
940+#include <linux/semaphore.h>
941 #include "nodelist.h"
942 #include "compr.h"
943
944@@ -35,39 +30,8 @@ static DEFINE_MUTEX(deflate_mutex);
945 static DEFINE_MUTEX(inflate_mutex);
946 static z_stream inf_strm, def_strm;
947
948-#ifdef __KERNEL__ /* Linux-only */
949-#include <linux/vmalloc.h>
950-#include <linux/init.h>
951-#include <linux/mutex.h>
952-
953-static int __init alloc_workspaces(void)
954-{
955-	def_strm.workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS,
956-							MAX_MEM_LEVEL));
957-	if (!def_strm.workspace)
958-		return -ENOMEM;
959-
960-	jffs2_dbg(1, "Allocated %d bytes for deflate workspace\n",
961-		  zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
962-	inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
963-	if (!inf_strm.workspace) {
964-		vfree(def_strm.workspace);
965-		return -ENOMEM;
966-	}
967-	jffs2_dbg(1, "Allocated %d bytes for inflate workspace\n",
968-		  zlib_inflate_workspacesize());
969-	return 0;
970-}
971-
972-static void free_workspaces(void)
973-{
974-	vfree(def_strm.workspace);
975-	vfree(inf_strm.workspace);
976-}
977-#else
978 #define alloc_workspaces() (0)
979 #define free_workspaces() do { } while(0)
980-#endif /* __KERNEL__ */
981
982 static int jffs2_zlib_compress(unsigned char *data_in,
983 			       unsigned char *cpage_out,
984@@ -80,7 +44,7 @@ static int jffs2_zlib_compress(unsigned
985
986 	mutex_lock(&deflate_mutex);
987
988-	if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
989+	if (Z_OK != deflateInit(&def_strm, 3)) {
990 		pr_warn("deflateInit failed\n");
991 		mutex_unlock(&deflate_mutex);
992 		return -1;
993@@ -98,21 +62,21 @@ static int jffs2_zlib_compress(unsigned
994 			(*sourcelen-def_strm.total_in), def_strm.avail_out);
995 		jffs2_dbg(1, "calling deflate with avail_in %ld, avail_out %ld\n",
996 			  def_strm.avail_in, def_strm.avail_out);
997-		ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
998+		ret = deflate(&def_strm, Z_PARTIAL_FLUSH);
999 		jffs2_dbg(1, "deflate returned with avail_in %ld, avail_out %ld, total_in %ld, total_out %ld\n",
1000 			  def_strm.avail_in, def_strm.avail_out,
1001 			  def_strm.total_in, def_strm.total_out);
1002 		if (ret != Z_OK) {
1003 			jffs2_dbg(1, "deflate in loop returned %d\n", ret);
1004-			zlib_deflateEnd(&def_strm);
1005+			deflateEnd(&def_strm);
1006 			mutex_unlock(&deflate_mutex);
1007 			return -1;
1008 		}
1009 	}
1010 	def_strm.avail_out += STREAM_END_SPACE;
1011 	def_strm.avail_in = 0;
1012-	ret = zlib_deflate(&def_strm, Z_FINISH);
1013-	zlib_deflateEnd(&def_strm);
1014+	ret = deflate(&def_strm, Z_FINISH);
1015+	deflateEnd(&def_strm);
1016
1017 	if (ret != Z_STREAM_END) {
1018 		jffs2_dbg(1, "final deflate returned %d\n", ret);
1019@@ -171,18 +135,18 @@ static int jffs2_zlib_decompress(unsigne
1020 	}
1021
1022
1023-	if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
1024+	if (Z_OK != inflateInit2(&inf_strm, wbits)) {
1025 		pr_warn("inflateInit failed\n");
1026 		mutex_unlock(&inflate_mutex);
1027 		return 1;
1028 	}
1029
1030-	while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
1031+	while((ret = inflate(&inf_strm, Z_FINISH)) == Z_OK)
1032 		;
1033 	if (ret != Z_STREAM_END) {
1034 		pr_notice("inflate returned %d\n", ret);
1035 	}
1036-	zlib_inflateEnd(&inf_strm);
1037+	inflateEnd(&inf_strm);
1038 	mutex_unlock(&inflate_mutex);
1039 	return 0;
1040 }
1041@@ -204,13 +168,30 @@ int __init jffs2_zlib_init(void)
1042 {
1043     int ret;
1044
1045+    ret = pthread_mutex_init(&inflate_mutex, NULL);
1046+    if (ret) {
1047+        return ret;
1048+    }
1049+
1050+    ret = pthread_mutex_init(&deflate_mutex, NULL);
1051+    if (ret) {
1052+        pthread_mutex_destroy(&inflate_mutex);
1053+        return ret;
1054+    }
1055+
1056     ret = alloc_workspaces();
1057-    if (ret)
1058-	    return ret;
1059+    if (ret) {
1060+        pthread_mutex_destroy(&inflate_mutex);
1061+        pthread_mutex_destroy(&deflate_mutex);
1062+        return ret;
1063+    }
1064
1065     ret = jffs2_register_compressor(&jffs2_zlib_comp);
1066-    if (ret)
1067-	    free_workspaces();
1068+    if (ret) {
1069+        pthread_mutex_destroy(&inflate_mutex);
1070+        pthread_mutex_destroy(&deflate_mutex);
1071+        free_workspaces();
1072+    }
1073
1074     return ret;
1075 }
1076@@ -219,4 +200,6 @@ void jffs2_zlib_exit(void)
1077 {
1078     jffs2_unregister_compressor(&jffs2_zlib_comp);
1079     free_workspaces();
1080+    pthread_mutex_destroy(&inflate_mutex);
1081+    pthread_mutex_destroy(&deflate_mutex);
1082 }
1083diff -Nupr old/fs/jffs2/debug.c new/fs/jffs2/debug.c
1084--- old/fs/jffs2/debug.c	2022-05-09 17:22:53.000000000 +0800
1085+++ new/fs/jffs2/debug.c	2022-05-10 15:11:46.200000000 +0800
1086@@ -10,15 +10,12 @@
1087  *
1088  */
1089
1090-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1091-
1092 #include <linux/kernel.h>
1093 #include <linux/types.h>
1094 #include <linux/pagemap.h>
1095-#include <linux/crc32.h>
1096-#include <linux/jffs2.h>
1097-#include <linux/mtd/mtd.h>
1098 #include <linux/slab.h>
1099+#include <mtd_dev.h>
1100+#include "los_crc32.h"
1101 #include "nodelist.h"
1102 #include "debug.h"
1103
1104@@ -133,7 +130,7 @@ __jffs2_dbg_prewrite_paranoia_check(stru
1105 	if (!buf)
1106 		return;
1107
1108-	ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
1109+	ret = jffs2_flash_read(c, ofs, len, &retlen, (char *)buf);
1110 	if (ret || (retlen != len)) {
1111 		JFFS2_WARNING("read %d bytes failed or short. ret %d, retlen %zd.\n",
1112 				len, ret, retlen);
1113diff -Nupr old/fs/jffs2/debug.h new/fs/jffs2/debug.h
1114--- old/fs/jffs2/debug.h	2022-05-09 17:22:53.000000000 +0800
1115+++ new/fs/jffs2/debug.h	2022-05-10 15:12:30.850000000 +0800
1116@@ -14,7 +14,12 @@
1117 #define _JFFS2_DEBUG_H_
1118
1119 #include <linux/sched.h>
1120-
1121+#include "los_process.h"
1122+#ifdef __cplusplus
1123+#if __cplusplus
1124+extern "C" {
1125+#endif /* __cplusplus */
1126+#endif /* __cplusplus */
1127 #ifndef CONFIG_JFFS2_FS_DEBUG
1128 #define CONFIG_JFFS2_FS_DEBUG 0
1129 #endif
1130@@ -71,25 +76,26 @@ do {						\
1131
1132 /* The prefixes of JFFS2 messages */
1133 #define JFFS2_DBG		KERN_DEBUG
1134+#define JFFS2_DBG_LVL	KERN_DEBUG
1135 #define JFFS2_DBG_PREFIX	"[JFFS2 DBG]"
1136 #define JFFS2_DBG_MSG_PREFIX	JFFS2_DBG JFFS2_DBG_PREFIX
1137
1138 /* JFFS2 message macros */
1139 #define JFFS2_ERROR(fmt, ...)					\
1140-	pr_err("error: (%d) %s: " fmt,				\
1141-	       task_pid_nr(current), __func__, ##__VA_ARGS__)
1142+	pr_err("error: (%u) %s: " fmt,				\
1143+	       LOS_GetCurrProcessID, __func__, ##__VA_ARGS__)
1144
1145 #define JFFS2_WARNING(fmt, ...)						\
1146-	pr_warn("warning: (%d) %s: " fmt,				\
1147-		task_pid_nr(current), __func__, ##__VA_ARGS__)
1148+	pr_warn("warning: (%u) %s: " fmt,				\
1149+		LOS_GetCurrProcessID, __func__, ##__VA_ARGS__)
1150
1151 #define JFFS2_NOTICE(fmt, ...)						\
1152-	pr_notice("notice: (%d) %s: " fmt,				\
1153-		  task_pid_nr(current), __func__, ##__VA_ARGS__)
1154+	pr_notice("notice: (%u) %s: " fmt,				\
1155+		  LOS_GetCurrProcessID, __func__, ##__VA_ARGS__)
1156
1157 #define JFFS2_DEBUG(fmt, ...)						\
1158-	printk(KERN_DEBUG "[JFFS2 DBG] (%d) %s: " fmt,			\
1159-	       task_pid_nr(current), __func__, ##__VA_ARGS__)
1160+	printk(KERN_DEBUG "[JFFS2 DBG] (%u) %s: " fmt,			\
1161+	       LOS_GetCurrProcessID, __func__, ##__VA_ARGS__)
1162
1163 /*
1164  * We split our debugging messages on several parts, depending on the JFFS2
1165@@ -272,4 +278,10 @@ __jffs2_dbg_dump_node(struct jffs2_sb_in
1166 #define jffs2_dbg_acct_sanity_check_nolock(c, jeb)
1167 #endif /* !JFFS2_DBG_SANITY_CHECKS */
1168
1169+#ifdef __cplusplus
1170+#if __cplusplus
1171+}
1172+#endif /* __cplusplus */
1173+#endif /* __cplusplus */
1174+
1175 #endif /* _JFFS2_DEBUG_H_ */
1176diff -Nupr old/fs/jffs2/dir.c new/fs/jffs2/dir.c
1177--- old/fs/jffs2/dir.c	2022-05-09 17:22:53.000000000 +0800
1178+++ new/fs/jffs2/dir.c	2022-05-10 16:08:26.380000000 +0800
1179@@ -10,95 +10,42 @@
1180  *
1181  */
1182
1183-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1184-
1185+#include <dirent.h>
1186 #include <linux/kernel.h>
1187 #include <linux/slab.h>
1188 #include <linux/fs.h>
1189-#include <linux/crc32.h>
1190-#include <linux/jffs2.h>
1191-#include "jffs2_fs_i.h"
1192-#include "jffs2_fs_sb.h"
1193-#include <linux/time.h>
1194+#include "los_crc32.h"
1195 #include "nodelist.h"
1196-
1197-static int jffs2_readdir (struct file *, struct dir_context *);
1198-
1199-static int jffs2_create (struct inode *,struct dentry *,umode_t,
1200-			 bool);
1201-static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
1202-				    unsigned int);
1203-static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
1204-static int jffs2_unlink (struct inode *,struct dentry *);
1205-static int jffs2_symlink (struct inode *,struct dentry *,const char *);
1206-static int jffs2_mkdir (struct inode *,struct dentry *,umode_t);
1207-static int jffs2_rmdir (struct inode *,struct dentry *);
1208-static int jffs2_mknod (struct inode *,struct dentry *,umode_t,dev_t);
1209-static int jffs2_rename (struct inode *, struct dentry *,
1210-			 struct inode *, struct dentry *,
1211-			 unsigned int);
1212-
1213-const struct file_operations jffs2_dir_operations =
1214-{
1215-	.read =		generic_read_dir,
1216-	.iterate_shared=jffs2_readdir,
1217-	.unlocked_ioctl=jffs2_ioctl,
1218-	.fsync =	jffs2_fsync,
1219-	.llseek =	generic_file_llseek,
1220-};
1221-
1222-
1223-const struct inode_operations jffs2_dir_inode_operations =
1224-{
1225-	.create =	jffs2_create,
1226-	.lookup =	jffs2_lookup,
1227-	.link =		jffs2_link,
1228-	.unlink =	jffs2_unlink,
1229-	.symlink =	jffs2_symlink,
1230-	.mkdir =	jffs2_mkdir,
1231-	.rmdir =	jffs2_rmdir,
1232-	.mknod =	jffs2_mknod,
1233-	.rename =	jffs2_rename,
1234-	.get_acl =	jffs2_get_acl,
1235-	.set_acl =	jffs2_set_acl,
1236-	.setattr =	jffs2_setattr,
1237-	.listxattr =	jffs2_listxattr,
1238-};
1239-
1240-/***********************************************************************/
1241-
1242+#include "vfs_jffs2.h"
1243+#include "jffs2_hash.h"
1244
1245 /* We keep the dirent list sorted in increasing order of name hash,
1246    and we use the same hash function as the dentries. Makes this
1247    nice and simple
1248 */
1249-static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
1250-				   unsigned int flags)
1251+struct jffs2_inode *jffs2_lookup(struct jffs2_inode *dir_i, const unsigned char *d_name, int namelen)
1252 {
1253 	struct jffs2_inode_info *dir_f;
1254 	struct jffs2_full_dirent *fd = NULL, *fd_list;
1255 	uint32_t ino = 0;
1256-	struct inode *inode = NULL;
1257-	unsigned int nhash;
1258+	uint32_t hash = full_name_hash(d_name, namelen);
1259+	struct jffs2_inode *inode = NULL;
1260
1261 	jffs2_dbg(1, "jffs2_lookup()\n");
1262
1263-	if (target->d_name.len > JFFS2_MAX_NAME_LEN)
1264+	if (namelen > JFFS2_MAX_NAME_LEN)
1265 		return ERR_PTR(-ENAMETOOLONG);
1266
1267 	dir_f = JFFS2_INODE_INFO(dir_i);
1268
1269-	/* The 'nhash' on the fd_list is not the same as the dentry hash */
1270-	nhash = full_name_hash(NULL, target->d_name.name, target->d_name.len);
1271-
1272 	mutex_lock(&dir_f->sem);
1273
1274 	/* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
1275-	for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= nhash; fd_list = fd_list->next) {
1276-		if (fd_list->nhash == nhash &&
1277-		    (!fd || fd_list->version > fd->version) &&
1278-		    strlen(fd_list->name) == target->d_name.len &&
1279-		    !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
1280+	for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= hash; fd_list = fd_list->next) {
1281+		if (fd_list->nhash == hash &&
1282+			(!fd || fd_list->version > fd->version) &&
1283+			strlen((char *)fd_list->name) == namelen &&
1284+			!strncmp((char *)fd_list->name, (char *)d_name, namelen)) {
1285 			fd = fd_list;
1286 		}
1287 	}
1288@@ -111,176 +58,57 @@ static struct dentry *jffs2_lookup(struc
1289 			pr_warn("iget() failed for ino #%u\n", ino);
1290 	}
1291
1292-	return d_splice_alias(inode, target);
1293-}
1294-
1295-/***********************************************************************/
1296-
1297-
1298-static int jffs2_readdir(struct file *file, struct dir_context *ctx)
1299-{
1300-	struct inode *inode = file_inode(file);
1301-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
1302-	struct jffs2_full_dirent *fd;
1303-	unsigned long curofs = 1;
1304-
1305-	jffs2_dbg(1, "jffs2_readdir() for dir_i #%lu\n", inode->i_ino);
1306-
1307-	if (!dir_emit_dots(file, ctx))
1308-		return 0;
1309-
1310-	mutex_lock(&f->sem);
1311-	for (fd = f->dents; fd; fd = fd->next) {
1312-		curofs++;
1313-		/* First loop: curofs = 2; pos = 2 */
1314-		if (curofs < ctx->pos) {
1315-			jffs2_dbg(2, "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
1316-				  fd->name, fd->ino, fd->type, curofs, (unsigned long)ctx->pos);
1317-			continue;
1318-		}
1319-		if (!fd->ino) {
1320-			jffs2_dbg(2, "Skipping deletion dirent \"%s\"\n",
1321-				  fd->name);
1322-			ctx->pos++;
1323-			continue;
1324-		}
1325-		jffs2_dbg(2, "Dirent %ld: \"%s\", ino #%u, type %d\n",
1326-			  (unsigned long)ctx->pos, fd->name, fd->ino, fd->type);
1327-		if (!dir_emit(ctx, fd->name, strlen(fd->name), fd->ino, fd->type))
1328-			break;
1329-		ctx->pos++;
1330-	}
1331-	mutex_unlock(&f->sem);
1332-	return 0;
1333-}
1334-
1335-/***********************************************************************/
1336-
1337-
1338-static int jffs2_create(struct inode *dir_i, struct dentry *dentry,
1339-			umode_t mode, bool excl)
1340-{
1341-	struct jffs2_raw_inode *ri;
1342-	struct jffs2_inode_info *f, *dir_f;
1343-	struct jffs2_sb_info *c;
1344-	struct inode *inode;
1345-	int ret;
1346-
1347-	ri = jffs2_alloc_raw_inode();
1348-	if (!ri)
1349-		return -ENOMEM;
1350-
1351-	c = JFFS2_SB_INFO(dir_i->i_sb);
1352-
1353-	jffs2_dbg(1, "%s()\n", __func__);
1354-
1355-	inode = jffs2_new_inode(dir_i, mode, ri);
1356-
1357-	if (IS_ERR(inode)) {
1358-		jffs2_dbg(1, "jffs2_new_inode() failed\n");
1359-		jffs2_free_raw_inode(ri);
1360-		return PTR_ERR(inode);
1361-	}
1362-
1363-	inode->i_op = &jffs2_file_inode_operations;
1364-	inode->i_fop = &jffs2_file_operations;
1365-	inode->i_mapping->a_ops = &jffs2_file_address_operations;
1366-	inode->i_mapping->nrpages = 0;
1367-
1368-	f = JFFS2_INODE_INFO(inode);
1369-	dir_f = JFFS2_INODE_INFO(dir_i);
1370-
1371-	/* jffs2_do_create() will want to lock it, _after_ reserving
1372-	   space and taking c-alloc_sem. If we keep it locked here,
1373-	   lockdep gets unhappy (although it's a false positive;
1374-	   nothing else will be looking at this inode yet so there's
1375-	   no chance of AB-BA deadlock involving its f->sem). */
1376-	mutex_unlock(&f->sem);
1377-
1378-	ret = jffs2_do_create(c, dir_f, f, ri, &dentry->d_name);
1379-	if (ret)
1380-		goto fail;
1381-
1382-	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
1383-
1384-	jffs2_free_raw_inode(ri);
1385-
1386-	jffs2_dbg(1, "%s(): Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
1387-		  __func__, inode->i_ino, inode->i_mode, inode->i_nlink,
1388-		  f->inocache->pino_nlink, inode->i_mapping->nrpages);
1389-
1390-	d_instantiate_new(dentry, inode);
1391-	return 0;
1392-
1393- fail:
1394-	iget_failed(inode);
1395-	jffs2_free_raw_inode(ri);
1396-	return ret;
1397+	return inode;
1398 }
1399
1400-/***********************************************************************/
1401-
1402-
1403-static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
1404+int jffs2_unlink(struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name)
1405 {
1406 	struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
1407 	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
1408-	struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode(dentry));
1409+	struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode);
1410 	int ret;
1411-	uint32_t now = JFFS2_NOW();
1412+	uint32_t now = Jffs2CurSec();
1413
1414-	ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
1415-			      dentry->d_name.len, dead_f, now);
1416+	ret = jffs2_do_unlink(c, dir_f, (const char *)d_name,
1417+		strlen((char *)d_name), dead_f, now);
1418 	if (dead_f->inocache)
1419-		set_nlink(d_inode(dentry), dead_f->inocache->pino_nlink);
1420+		d_inode->i_nlink = dead_f->inocache->pino_nlink;
1421 	if (!ret)
1422-		dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
1423+		dir_i->i_mtime = dir_i->i_ctime = now;
1424 	return ret;
1425 }
1426-/***********************************************************************/
1427
1428-
1429-static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
1430+int jffs2_link(struct jffs2_inode *old_d_inode, struct jffs2_inode *dir_i, const unsigned char *d_name)
1431 {
1432-	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_sb);
1433-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(old_dentry));
1434+	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_d_inode->i_sb);
1435+	struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_d_inode);
1436 	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
1437 	int ret;
1438 	uint8_t type;
1439 	uint32_t now;
1440
1441-	/* Don't let people make hard links to bad inodes. */
1442-	if (!f->inocache)
1443-		return -EIO;
1444-
1445-	if (d_is_dir(old_dentry))
1446-		return -EPERM;
1447-
1448 	/* XXX: This is ugly */
1449-	type = (d_inode(old_dentry)->i_mode & S_IFMT) >> 12;
1450+	type = (old_d_inode->i_mode & S_IFMT) >> 12;
1451 	if (!type) type = DT_REG;
1452
1453-	now = JFFS2_NOW();
1454-	ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
1455+	now = Jffs2CurSec();
1456+	ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, (const char *)d_name,
1457+						strlen((char *)d_name), now);
1458
1459 	if (!ret) {
1460 		mutex_lock(&f->sem);
1461-		set_nlink(d_inode(old_dentry), ++f->inocache->pino_nlink);
1462+		old_d_inode->i_nlink = ++f->inocache->pino_nlink;
1463 		mutex_unlock(&f->sem);
1464-		d_instantiate(dentry, d_inode(old_dentry));
1465-		dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
1466-		ihold(d_inode(old_dentry));
1467+		dir_i->i_mtime = dir_i->i_ctime = now;
1468 	}
1469 	return ret;
1470 }
1471
1472-/***********************************************************************/
1473-
1474-static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
1475+int jffs2_symlink(struct jffs2_inode *dir_i, struct jffs2_inode **d_inode, const unsigned char *d_name, const char *target)
1476 {
1477 	struct jffs2_inode_info *f, *dir_f;
1478 	struct jffs2_sb_info *c;
1479-	struct inode *inode;
1480+	struct jffs2_inode *inode;
1481 	struct jffs2_raw_inode *ri;
1482 	struct jffs2_raw_dirent *rd;
1483 	struct jffs2_full_dnode *fn;
1484@@ -304,7 +132,7 @@ static int jffs2_symlink (struct inode *
1485 	/* Try to reserve enough space for both node and dirent.
1486 	 * Just the node will do for now, though
1487 	 */
1488-	namelen = dentry->d_name.len;
1489+	namelen = strlen((char *)d_name);
1490 	ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
1491 				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
1492
1493@@ -321,8 +149,6 @@ static int jffs2_symlink (struct inode *
1494 		return PTR_ERR(inode);
1495 	}
1496
1497-	inode->i_op = &jffs2_symlink_inode_operations;
1498-
1499 	f = JFFS2_INODE_INFO(inode);
1500
1501 	inode->i_size = targetlen;
1502@@ -334,7 +160,7 @@ static int jffs2_symlink (struct inode *
1503 	ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
1504 	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
1505
1506-	fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
1507+	fn = jffs2_write_dnode(c, f, ri, (const unsigned char *)target, targetlen, ALLOC_NORMAL);
1508
1509 	jffs2_free_raw_inode(ri);
1510
1511@@ -347,7 +173,8 @@ static int jffs2_symlink (struct inode *
1512 	}
1513
1514 	/* We use f->target field to store the target path. */
1515-	f->target = kmemdup(target, targetlen + 1, GFP_KERNEL);
1516+
1517+	f->target = (unsigned char *)malloc(targetlen + 1);
1518 	if (!f->target) {
1519 		pr_warn("Can't allocate %d bytes of memory\n", targetlen + 1);
1520 		mutex_unlock(&f->sem);
1521@@ -355,7 +182,15 @@ static int jffs2_symlink (struct inode *
1522 		ret = -ENOMEM;
1523 		goto fail;
1524 	}
1525-	inode->i_link = f->target;
1526+
1527+	ret = LOS_CopyToKernel((char *)f->target, targetlen + 1, target, targetlen + 1);
1528+	if (ret != EOK) {
1529+		(void)free(f->target);
1530+		f->target = NULL;
1531+		mutex_unlock(&f->sem);
1532+		jffs2_complete_reservation(c);
1533+		goto fail;
1534+	}
1535
1536 	jffs2_dbg(1, "%s(): symlink's target '%s' cached\n",
1537 		  __func__, (char *)f->target);
1538@@ -368,14 +203,6 @@ static int jffs2_symlink (struct inode *
1539
1540 	jffs2_complete_reservation(c);
1541
1542-	ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
1543-	if (ret)
1544-		goto fail;
1545-
1546-	ret = jffs2_init_acl_post(inode);
1547-	if (ret)
1548-		goto fail;
1549-
1550 	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
1551 				  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
1552 	if (ret)
1553@@ -400,13 +227,13 @@ static int jffs2_symlink (struct inode *
1554 	rd->pino = cpu_to_je32(dir_i->i_ino);
1555 	rd->version = cpu_to_je32(++dir_f->highest_version);
1556 	rd->ino = cpu_to_je32(inode->i_ino);
1557-	rd->mctime = cpu_to_je32(JFFS2_NOW());
1558+	rd->mctime = cpu_to_je32(Jffs2CurSec());
1559 	rd->nsize = namelen;
1560 	rd->type = DT_LNK;
1561 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
1562-	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
1563+	rd->name_crc = cpu_to_je32(crc32(0, (const char *)d_name, namelen));
1564
1565-	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
1566+	fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)d_name, namelen, ALLOC_NORMAL);
1567
1568 	if (IS_ERR(fd)) {
1569 		/* dirent failed to write. Delete the inode normally
1570@@ -418,7 +245,7 @@ static int jffs2_symlink (struct inode *
1571 		goto fail;
1572 	}
1573
1574-	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
1575+	dir_i->i_mtime = dir_i->i_ctime = je32_to_cpu(rd->mctime);
1576
1577 	jffs2_free_raw_dirent(rd);
1578
1579@@ -429,20 +256,20 @@ static int jffs2_symlink (struct inode *
1580 	mutex_unlock(&dir_f->sem);
1581 	jffs2_complete_reservation(c);
1582
1583-	d_instantiate_new(dentry, inode);
1584+	*d_inode = inode;
1585 	return 0;
1586
1587  fail:
1588-	iget_failed(inode);
1589+	inode->i_nlink = 0;
1590+	jffs2_iput(inode);
1591 	return ret;
1592 }
1593
1594-
1595-static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, umode_t mode)
1596+int jffs2_mkdir(struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i)
1597 {
1598 	struct jffs2_inode_info *f, *dir_f;
1599 	struct jffs2_sb_info *c;
1600-	struct inode *inode;
1601+	struct jffs2_inode *inode;
1602 	struct jffs2_raw_inode *ri;
1603 	struct jffs2_raw_dirent *rd;
1604 	struct jffs2_full_dnode *fn;
1605@@ -450,7 +277,7 @@ static int jffs2_mkdir (struct inode *di
1606 	int namelen;
1607 	uint32_t alloclen;
1608 	int ret;
1609-
1610+	mode  &= ~S_IFMT;
1611 	mode |= S_IFDIR;
1612
1613 	ri = jffs2_alloc_raw_inode();
1614@@ -462,9 +289,8 @@ static int jffs2_mkdir (struct inode *di
1615 	/* Try to reserve enough space for both node and dirent.
1616 	 * Just the node will do for now, though
1617 	 */
1618-	namelen = dentry->d_name.len;
1619-	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
1620-				  JFFS2_SUMMARY_INODE_SIZE);
1621+	namelen = strlen((char *)d_name);
1622+	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
1623
1624 	if (ret) {
1625 		jffs2_free_raw_inode(ri);
1626@@ -478,14 +304,8 @@ static int jffs2_mkdir (struct inode *di
1627 		jffs2_complete_reservation(c);
1628 		return PTR_ERR(inode);
1629 	}
1630-
1631-	inode->i_op = &jffs2_dir_inode_operations;
1632-	inode->i_fop = &jffs2_dir_operations;
1633-
1634 	f = JFFS2_INODE_INFO(inode);
1635
1636-	/* Directories get nlink 2 at start */
1637-	set_nlink(inode, 2);
1638 	/* but ic->pino_nlink is the parent ino# */
1639 	f->inocache->pino_nlink = dir_i->i_ino;
1640
1641@@ -500,6 +320,7 @@ static int jffs2_mkdir (struct inode *di
1642 		/* Eeek. Wave bye bye */
1643 		mutex_unlock(&f->sem);
1644 		jffs2_complete_reservation(c);
1645+
1646 		ret = PTR_ERR(fn);
1647 		goto fail;
1648 	}
1649@@ -511,14 +332,6 @@ static int jffs2_mkdir (struct inode *di
1650
1651 	jffs2_complete_reservation(c);
1652
1653-	ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
1654-	if (ret)
1655-		goto fail;
1656-
1657-	ret = jffs2_init_acl_post(inode);
1658-	if (ret)
1659-		goto fail;
1660-
1661 	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
1662 				  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
1663 	if (ret)
1664@@ -543,13 +356,13 @@ static int jffs2_mkdir (struct inode *di
1665 	rd->pino = cpu_to_je32(dir_i->i_ino);
1666 	rd->version = cpu_to_je32(++dir_f->highest_version);
1667 	rd->ino = cpu_to_je32(inode->i_ino);
1668-	rd->mctime = cpu_to_je32(JFFS2_NOW());
1669+	rd->mctime = cpu_to_je32(Jffs2CurSec());
1670 	rd->nsize = namelen;
1671 	rd->type = DT_DIR;
1672 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
1673-	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
1674+	rd->name_crc = cpu_to_je32(crc32(0, d_name, namelen));
1675
1676-	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
1677+	fd = jffs2_write_dirent(c, dir_f, rd, d_name, namelen, ALLOC_NORMAL);
1678
1679 	if (IS_ERR(fd)) {
1680 		/* dirent failed to write. Delete the inode normally
1681@@ -557,12 +370,12 @@ static int jffs2_mkdir (struct inode *di
1682 		jffs2_complete_reservation(c);
1683 		jffs2_free_raw_dirent(rd);
1684 		mutex_unlock(&dir_f->sem);
1685+		inode->i_nlink = 0;
1686 		ret = PTR_ERR(fd);
1687 		goto fail;
1688 	}
1689
1690-	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
1691-	inc_nlink(dir_i);
1692+	dir_i->i_mtime = dir_i->i_ctime = je32_to_cpu(rd->mctime);
1693
1694 	jffs2_free_raw_dirent(rd);
1695
1696@@ -572,300 +385,198 @@ static int jffs2_mkdir (struct inode *di
1697
1698 	mutex_unlock(&dir_f->sem);
1699 	jffs2_complete_reservation(c);
1700+	*new_i = inode;
1701
1702-	d_instantiate_new(dentry, inode);
1703 	return 0;
1704
1705  fail:
1706-	iget_failed(inode);
1707+	inode->i_nlink = 0;
1708+	jffs2_iput(inode);
1709 	return ret;
1710 }
1711
1712-static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
1713+int jffs2_rmdir (struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name)
1714 {
1715 	struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
1716 	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
1717-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(dentry));
1718+	struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
1719 	struct jffs2_full_dirent *fd;
1720 	int ret;
1721-	uint32_t now = JFFS2_NOW();
1722+	uint32_t now = Jffs2CurSec();
1723
1724-	mutex_lock(&f->sem);
1725 	for (fd = f->dents ; fd; fd = fd->next) {
1726 		if (fd->ino) {
1727-			mutex_unlock(&f->sem);
1728+			PRINT_ERR("%s-%d: ret=%d\n", __FUNCTION__, __LINE__, ENOTEMPTY);
1729 			return -ENOTEMPTY;
1730 		}
1731 	}
1732-	mutex_unlock(&f->sem);
1733
1734-	ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
1735-			      dentry->d_name.len, f, now);
1736-	if (!ret) {
1737-		dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
1738-		clear_nlink(d_inode(dentry));
1739-		drop_nlink(dir_i);
1740-	}
1741+	ret = jffs2_do_unlink(c, dir_f, (const char *)d_name,
1742+						strlen((char *)d_name), f, now);
1743+	if (f->inocache)
1744+		d_inode->i_nlink = f->inocache->pino_nlink;
1745+	if (!ret)
1746+		dir_i->i_mtime = dir_i->i_ctime = now;
1747+
1748 	return ret;
1749 }
1750
1751-static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, umode_t mode, dev_t rdev)
1752+int jffs2_rename (struct jffs2_inode *old_dir_i, struct jffs2_inode *d_inode, const unsigned char *old_d_name,
1753+		struct jffs2_inode *new_dir_i, const unsigned char *new_d_name)
1754 {
1755-	struct jffs2_inode_info *f, *dir_f;
1756-	struct jffs2_sb_info *c;
1757-	struct inode *inode;
1758-	struct jffs2_raw_inode *ri;
1759-	struct jffs2_raw_dirent *rd;
1760-	struct jffs2_full_dnode *fn;
1761-	struct jffs2_full_dirent *fd;
1762-	int namelen;
1763-	union jffs2_device_node dev;
1764-	int devlen = 0;
1765-	uint32_t alloclen;
1766 	int ret;
1767+	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
1768+	uint8_t type;
1769+	uint32_t now;
1770
1771-	ri = jffs2_alloc_raw_inode();
1772-	if (!ri)
1773-		return -ENOMEM;
1774-
1775-	c = JFFS2_SB_INFO(dir_i->i_sb);
1776-
1777-	if (S_ISBLK(mode) || S_ISCHR(mode))
1778-		devlen = jffs2_encode_dev(&dev, rdev);
1779+	/* XXX: This is ugly */
1780+	type = (d_inode->i_mode & S_IFMT) >> 12;
1781+	if (!type) type = DT_REG;
1782
1783-	/* Try to reserve enough space for both node and dirent.
1784-	 * Just the node will do for now, though
1785-	 */
1786-	namelen = dentry->d_name.len;
1787-	ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
1788-				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
1789+	now = Jffs2CurSec();
1790+	ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
1791+				d_inode->i_ino, type,
1792+				(const char *)new_d_name, strlen((char *)new_d_name), now);
1793
1794-	if (ret) {
1795-		jffs2_free_raw_inode(ri);
1796+	if (ret)
1797 		return ret;
1798-	}
1799
1800-	inode = jffs2_new_inode(dir_i, mode, ri);
1801
1802-	if (IS_ERR(inode)) {
1803-		jffs2_free_raw_inode(ri);
1804-		jffs2_complete_reservation(c);
1805-		return PTR_ERR(inode);
1806+	/* If it was a directory we moved, and there was no victim,
1807+	   increase i_nlink on its new parent */
1808+	if ((d_inode->i_mode & S_IFMT) == S_IFDIR) {
1809+		new_dir_i->i_nlink++;
1810 	}
1811-	inode->i_op = &jffs2_file_inode_operations;
1812-	init_special_inode(inode, inode->i_mode, rdev);
1813
1814-	f = JFFS2_INODE_INFO(inode);
1815-
1816-	ri->dsize = ri->csize = cpu_to_je32(devlen);
1817-	ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
1818-	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
1819-
1820-	ri->compr = JFFS2_COMPR_NONE;
1821-	ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
1822-	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
1823-
1824-	fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
1825+	/* Unlink the original */
1826+	ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
1827+				(const char *)old_d_name, strlen((char *)old_d_name), NULL, now);
1828
1829-	jffs2_free_raw_inode(ri);
1830+	/* We don't touch inode->i_nlink */
1831
1832-	if (IS_ERR(fn)) {
1833-		/* Eeek. Wave bye bye */
1834+	if (ret) {
1835+		/* Oh shit. We really ought to make a single node which can do both atomically */
1836+		struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
1837+		mutex_lock(&f->sem);
1838+		if (f->inocache)
1839+			d_inode->i_nlink = f->inocache->pino_nlink++;
1840 		mutex_unlock(&f->sem);
1841-		jffs2_complete_reservation(c);
1842-		ret = PTR_ERR(fn);
1843-		goto fail;
1844-	}
1845-	/* No data here. Only a metadata node, which will be
1846-	   obsoleted by the first data write
1847-	*/
1848-	f->metadata = fn;
1849-	mutex_unlock(&f->sem);
1850-
1851-	jffs2_complete_reservation(c);
1852-
1853-	ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
1854-	if (ret)
1855-		goto fail;
1856-
1857-	ret = jffs2_init_acl_post(inode);
1858-	if (ret)
1859-		goto fail;
1860-
1861-	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
1862-				  ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
1863-	if (ret)
1864-		goto fail;
1865
1866-	rd = jffs2_alloc_raw_dirent();
1867-	if (!rd) {
1868-		/* Argh. Now we treat it like a normal delete */
1869-		jffs2_complete_reservation(c);
1870-		ret = -ENOMEM;
1871-		goto fail;
1872+		pr_notice("%s(): Link succeeded, unlink failed (err %d). You now have a hard link\n",
1873+			  __func__, ret);
1874+		/* Might as well let the VFS know */
1875+		new_dir_i->i_mtime = new_dir_i->i_ctime = now;
1876+		return ret;
1877 	}
1878
1879-	dir_f = JFFS2_INODE_INFO(dir_i);
1880-	mutex_lock(&dir_f->sem);
1881
1882-	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
1883-	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
1884-	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
1885-	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
1886+	new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = now;
1887
1888-	rd->pino = cpu_to_je32(dir_i->i_ino);
1889-	rd->version = cpu_to_je32(++dir_f->highest_version);
1890-	rd->ino = cpu_to_je32(inode->i_ino);
1891-	rd->mctime = cpu_to_je32(JFFS2_NOW());
1892-	rd->nsize = namelen;
1893+	return 0;
1894+}
1895
1896-	/* XXX: This is ugly. */
1897-	rd->type = (mode & S_IFMT) >> 12;
1898+int jffs2_create(struct jffs2_inode *dir_i, const unsigned char *d_name, int mode,
1899+		struct jffs2_inode **new_i)
1900+{
1901+	struct jffs2_raw_inode *ri;
1902+	struct jffs2_inode_info *f, *dir_f;
1903+	struct jffs2_sb_info *c;
1904+	struct jffs2_inode *inode;
1905+	int ret;
1906+	mode  &= ~S_IFMT;
1907+	mode |= S_IFREG;
1908+	ri = jffs2_alloc_raw_inode();
1909+	if (!ri)
1910+		return -ENOMEM;
1911
1912-	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
1913-	rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
1914+	c = JFFS2_SB_INFO(dir_i->i_sb);
1915
1916-	fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
1917+	D1(printk(KERN_DEBUG "jffs2_create()\n"));
1918+	inode = jffs2_new_inode(dir_i, mode, ri);
1919
1920-	if (IS_ERR(fd)) {
1921-		/* dirent failed to write. Delete the inode normally
1922-		   as if it were the final unlink() */
1923-		jffs2_complete_reservation(c);
1924-		jffs2_free_raw_dirent(rd);
1925-		mutex_unlock(&dir_f->sem);
1926-		ret = PTR_ERR(fd);
1927-		goto fail;
1928+	if (IS_ERR(inode)) {
1929+		D1(printk(KERN_DEBUG "jffs2_new_inode() failed, error:%ld\n", PTR_ERR(inode)));
1930+		jffs2_free_raw_inode(ri);
1931+		return PTR_ERR(inode);
1932 	}
1933
1934-	dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
1935+	f = JFFS2_INODE_INFO(inode);
1936+	dir_f = JFFS2_INODE_INFO(dir_i);
1937
1938-	jffs2_free_raw_dirent(rd);
1939+	/* jffs2_do_create() will want to lock it, _after_ reserving
1940+	   space and taking c-alloc_sem. If we keep it locked here,
1941+	   lockdep gets unhappy (although it's a false positive;
1942+	   nothing else will be looking at this inode yet so there's
1943+	   no chance of AB-BA deadlock involving its f->sem). */
1944+	mutex_unlock(&f->sem);
1945+	ret = jffs2_do_create(c, dir_f, f, ri,
1946+				(const char *)d_name,
1947+				strlen((char *)d_name));
1948
1949-	/* Link the fd into the inode's list, obsoleting an old
1950-	   one if necessary. */
1951-	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
1952+	if (ret) {
1953+		inode->i_nlink = 0;
1954+		jffs2_iput(inode);
1955+		jffs2_free_raw_inode(ri);
1956+		return ret;
1957+	}
1958
1959-	mutex_unlock(&dir_f->sem);
1960-	jffs2_complete_reservation(c);
1961+	jffs2_free_raw_inode(ri);
1962
1963-	d_instantiate_new(dentry, inode);
1964+	D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d)\n",
1965+		inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->pino_nlink));
1966+	*new_i = inode;
1967 	return 0;
1968+}
1969
1970- fail:
1971-	iget_failed(inode);
1972-	return ret;
1973+static __inline void fill_name(char *dst_name, int nlen, const unsigned char *name, int namlen)
1974+{
1975+	int len = nlen < namlen ? nlen : namlen;
1976+	(void)memcpy_s(dst_name, nlen, name, len);
1977+	dst_name[len] = '\0';
1978 }
1979
1980-static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
1981-			 struct inode *new_dir_i, struct dentry *new_dentry,
1982-			 unsigned int flags)
1983+int jffs2_readdir(struct jffs2_inode *inode, off_t *offset, off_t *int_off, struct dirent *ent)
1984 {
1985-	int ret;
1986-	struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
1987-	struct jffs2_inode_info *victim_f = NULL;
1988-	uint8_t type;
1989-	uint32_t now;
1990+	struct jffs2_inode_info *f;
1991+	struct jffs2_full_dirent *fd;
1992+	off_t curofs = 0;
1993
1994-	if (flags & ~RENAME_NOREPLACE)
1995-		return -EINVAL;
1996+	f = JFFS2_INODE_INFO(inode);
1997
1998-	/* The VFS will check for us and prevent trying to rename a
1999-	 * file over a directory and vice versa, but if it's a directory,
2000-	 * the VFS can't check whether the victim is empty. The filesystem
2001-	 * needs to do that for itself.
2002-	 */
2003-	if (d_really_is_positive(new_dentry)) {
2004-		victim_f = JFFS2_INODE_INFO(d_inode(new_dentry));
2005-		if (d_is_dir(new_dentry)) {
2006-			struct jffs2_full_dirent *fd;
2007-
2008-			mutex_lock(&victim_f->sem);
2009-			for (fd = victim_f->dents; fd; fd = fd->next) {
2010-				if (fd->ino) {
2011-					mutex_unlock(&victim_f->sem);
2012-					return -ENOTEMPTY;
2013-				}
2014-			}
2015-			mutex_unlock(&victim_f->sem);
2016+	mutex_lock(&f->sem);
2017+	for (fd = f->dents; fd; fd = fd->next) {
2018+		if (curofs++ < *int_off) {
2019+			D2(printk
2020+				(KERN_DEBUG
2021+				"Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
2022+				fd->name, fd->ino, fd->type, curofs, offset));
2023+			continue;
2024 		}
2025-	}
2026-
2027-	/* XXX: We probably ought to alloc enough space for
2028-	   both nodes at the same time. Writing the new link,
2029-	   then getting -ENOSPC, is quite bad :)
2030-	*/
2031-
2032-	/* Make a hard link */
2033-
2034-	/* XXX: This is ugly */
2035-	type = (d_inode(old_dentry)->i_mode & S_IFMT) >> 12;
2036-	if (!type) type = DT_REG;
2037-
2038-	now = JFFS2_NOW();
2039-	ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
2040-			    d_inode(old_dentry)->i_ino, type,
2041-			    new_dentry->d_name.name, new_dentry->d_name.len, now);
2042-
2043-	if (ret)
2044-		return ret;
2045-
2046-	if (victim_f) {
2047-		/* There was a victim. Kill it off nicely */
2048-		if (d_is_dir(new_dentry))
2049-			clear_nlink(d_inode(new_dentry));
2050-		else
2051-			drop_nlink(d_inode(new_dentry));
2052-		/* Don't oops if the victim was a dirent pointing to an
2053-		   inode which didn't exist. */
2054-		if (victim_f->inocache) {
2055-			mutex_lock(&victim_f->sem);
2056-			if (d_is_dir(new_dentry))
2057-				victim_f->inocache->pino_nlink = 0;
2058-			else
2059-				victim_f->inocache->pino_nlink--;
2060-			mutex_unlock(&victim_f->sem);
2061+		if (!fd->ino) {
2062+			D2(printk (KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
2063+			(*int_off)++;
2064+			continue;
2065 		}
2066-	}
2067
2068-	/* If it was a directory we moved, and there was no victim,
2069-	   increase i_nlink on its new parent */
2070-	if (d_is_dir(old_dentry) && !victim_f)
2071-		inc_nlink(new_dir_i);
2072-
2073-	/* Unlink the original */
2074-	ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
2075-			      old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
2076-
2077-	/* We don't touch inode->i_nlink */
2078-
2079-	if (ret) {
2080-		/* Oh shit. We really ought to make a single node which can do both atomically */
2081-		struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(old_dentry));
2082-		mutex_lock(&f->sem);
2083-		inc_nlink(d_inode(old_dentry));
2084-		if (f->inocache && !d_is_dir(old_dentry))
2085-			f->inocache->pino_nlink++;
2086-		mutex_unlock(&f->sem);
2087+		D2(printk
2088+			(KERN_DEBUG "%s-%d: Dirent %ld: \"%s\", ino #%u, type %d\n", __FUNCTION__, __LINE__, offset,
2089+			fd->name, fd->ino, fd->type));
2090+		fill_name(ent->d_name, sizeof(ent->d_name) - 1, fd->name, strlen((char *)fd->name));
2091+		ent->d_type = fd->type;
2092+		ent->d_off = ++(*offset);
2093+		ent->d_reclen = (uint16_t)sizeof(struct dirent);
2094
2095-		pr_notice("%s(): Link succeeded, unlink failed (err %d). You now have a hard link\n",
2096-			  __func__, ret);
2097-		/*
2098-		 * We can't keep the target in dcache after that.
2099-		 * For one thing, we can't afford dentry aliases for directories.
2100-		 * For another, if there was a victim, we _can't_ set new inode
2101-		 * for that sucker and we have to trigger mount eviction - the
2102-		 * caller won't do it on its own since we are returning an error.
2103-		 */
2104-		d_invalidate(new_dentry);
2105-		new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
2106-		return ret;
2107+		(*int_off)++;
2108+		break;
2109 	}
2110
2111-	if (d_is_dir(old_dentry))
2112-		drop_nlink(old_dir_i);
2113+	mutex_unlock(&f->sem);
2114
2115-	new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
2116+	if (fd == NULL) {
2117+		D2(printk(KERN_DEBUG "reached the end of the directory\n"));
2118+		return ENOENT;
2119+	}
2120
2121-	return 0;
2122+	return ENOERR;
2123 }
2124
2125diff -Nupr old/fs/jffs2/erase.c new/fs/jffs2/erase.c
2126--- old/fs/jffs2/erase.c	2022-05-09 17:22:53.000000000 +0800
2127+++ new/fs/jffs2/erase.c	2022-05-10 16:09:47.150000000 +0800
2128@@ -10,16 +10,19 @@
2129  *
2130  */
2131
2132-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2133-
2134 #include <linux/kernel.h>
2135 #include <linux/slab.h>
2136-#include <linux/mtd/mtd.h>
2137 #include <linux/compiler.h>
2138-#include <linux/crc32.h>
2139 #include <linux/sched.h>
2140 #include <linux/pagemap.h>
2141+#include "mtd_dev.h"
2142 #include "nodelist.h"
2143+#include "los_crc32.h"
2144+
2145+struct erase_priv_struct {
2146+	struct jffs2_eraseblock *jeb;
2147+	struct jffs2_sb_info *c;
2148+};
2149
2150 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset);
2151 static void jffs2_erase_succeeded(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
2152@@ -29,50 +32,14 @@ static void jffs2_erase_block(struct jff
2153 			      struct jffs2_eraseblock *jeb)
2154 {
2155 	int ret;
2156-	uint32_t bad_offset;
2157-#ifdef __ECOS
2158-       ret = jffs2_flash_erase(c, jeb);
2159-       if (!ret) {
2160-	       jffs2_erase_succeeded(c, jeb);
2161-	       return;
2162-       }
2163-       bad_offset = jeb->offset;
2164-#else /* Linux */
2165-	struct erase_info *instr;
2166-
2167-	jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
2168-		  __func__,
2169-		  jeb->offset, jeb->offset, jeb->offset + c->sector_size);
2170-	instr = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
2171-	if (!instr) {
2172-		pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
2173-		mutex_lock(&c->erase_free_sem);
2174-		spin_lock(&c->erase_completion_lock);
2175-		list_move(&jeb->list, &c->erase_pending_list);
2176-		c->erasing_size -= c->sector_size;
2177-		c->dirty_size += c->sector_size;
2178-		jeb->dirty_size = c->sector_size;
2179-		spin_unlock(&c->erase_completion_lock);
2180-		mutex_unlock(&c->erase_free_sem);
2181-		return;
2182-	}
2183+	uint64_t bad_offset = 0;
2184
2185-	memset(instr, 0, sizeof(*instr));
2186-
2187-	instr->addr = jeb->offset;
2188-	instr->len = c->sector_size;
2189-
2190-	ret = mtd_erase(c->mtd, instr);
2191+	ret = c->mtd->erase(c->mtd, jeb->offset, c->sector_size, &bad_offset);
2192 	if (!ret) {
2193 		jffs2_erase_succeeded(c, jeb);
2194-		kfree(instr);
2195 		return;
2196 	}
2197
2198-	bad_offset = instr->fail_addr;
2199-	kfree(instr);
2200-#endif /* __ECOS */
2201-
2202 	if (ret == -ENOMEM || ret == -EAGAIN) {
2203 		/* Erase failed immediately. Refile it on the list */
2204 		jffs2_dbg(1, "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n",
2205@@ -168,29 +135,10 @@ static void jffs2_erase_succeeded(struct
2206 	jffs2_garbage_collect_trigger(c);
2207 	spin_unlock(&c->erase_completion_lock);
2208 	mutex_unlock(&c->erase_free_sem);
2209-	wake_up(&c->erase_wait);
2210 }
2211
2212 static void jffs2_erase_failed(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
2213 {
2214-	/* For NAND, if the failure did not occur at the device level for a
2215-	   specific physical page, don't bother updating the bad block table. */
2216-	if (jffs2_cleanmarker_oob(c) && (bad_offset != (uint32_t)MTD_FAIL_ADDR_UNKNOWN)) {
2217-		/* We had a device-level failure to erase.  Let's see if we've
2218-		   failed too many times. */
2219-		if (!jffs2_write_nand_badblock(c, jeb, bad_offset)) {
2220-			/* We'd like to give this block another try. */
2221-			mutex_lock(&c->erase_free_sem);
2222-			spin_lock(&c->erase_completion_lock);
2223-			list_move(&jeb->list, &c->erase_pending_list);
2224-			c->erasing_size -= c->sector_size;
2225-			c->dirty_size += c->sector_size;
2226-			jeb->dirty_size = c->sector_size;
2227-			spin_unlock(&c->erase_completion_lock);
2228-			mutex_unlock(&c->erase_free_sem);
2229-			return;
2230-		}
2231-	}
2232
2233 	mutex_lock(&c->erase_free_sem);
2234 	spin_lock(&c->erase_completion_lock);
2235@@ -200,7 +148,6 @@ static void jffs2_erase_failed(struct jf
2236 	c->nr_erasing_blocks--;
2237 	spin_unlock(&c->erase_completion_lock);
2238 	mutex_unlock(&c->erase_free_sem);
2239-	wake_up(&c->erase_wait);
2240 }
2241
2242 /* Hmmm. Maybe we should accept the extra space it takes and make
2243@@ -315,40 +262,8 @@ static int jffs2_block_check_erase(struc
2244 	void *ebuf;
2245 	uint32_t ofs;
2246 	size_t retlen;
2247-	int ret;
2248-	unsigned long *wordebuf;
2249+	int ret = -EIO;
2250
2251-	ret = mtd_point(c->mtd, jeb->offset, c->sector_size, &retlen,
2252-			&ebuf, NULL);
2253-	if (ret != -EOPNOTSUPP) {
2254-		if (ret) {
2255-			jffs2_dbg(1, "MTD point failed %d\n", ret);
2256-			goto do_flash_read;
2257-		}
2258-		if (retlen < c->sector_size) {
2259-			/* Don't muck about if it won't let us point to the whole erase sector */
2260-			jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
2261-				  retlen);
2262-			mtd_unpoint(c->mtd, jeb->offset, retlen);
2263-			goto do_flash_read;
2264-		}
2265-		wordebuf = ebuf-sizeof(*wordebuf);
2266-		retlen /= sizeof(*wordebuf);
2267-		do {
2268-		   if (*++wordebuf != ~0)
2269-			   break;
2270-		} while(--retlen);
2271-		mtd_unpoint(c->mtd, jeb->offset, c->sector_size);
2272-		if (retlen) {
2273-			pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08tx\n",
2274-				*wordebuf,
2275-				jeb->offset +
2276-				c->sector_size-retlen * sizeof(*wordebuf));
2277-			return -EIO;
2278-		}
2279-		return 0;
2280-	}
2281- do_flash_read:
2282 	ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2283 	if (!ebuf) {
2284 		pr_warn("Failed to allocate page buffer for verifying erase at 0x%08x. Refiling\n",
2285@@ -364,7 +279,7 @@ static int jffs2_block_check_erase(struc
2286
2287 		*bad_offset = ofs;
2288
2289-		ret = mtd_read(c->mtd, ofs, readlen, &retlen, ebuf);
2290+		ret = jffs2_flash_read(c, ofs, readlen, &retlen, ebuf);
2291 		if (ret) {
2292 			pr_warn("Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n",
2293 				ofs, ret);
2294@@ -379,7 +294,7 @@ static int jffs2_block_check_erase(struc
2295 		}
2296 		for (i=0; i<readlen; i += sizeof(unsigned long)) {
2297 			/* It's OK. We know it's properly aligned */
2298-			unsigned long *datum = ebuf + i;
2299+			unsigned long *datum = (unsigned long *)((char *)ebuf + i);;
2300 			if (*datum + 1) {
2301 				*bad_offset += i;
2302 				pr_warn("Newly-erased block contained word 0x%lx at offset 0x%08x\n",
2303@@ -469,7 +384,6 @@ static void jffs2_mark_erased_block(stru
2304
2305 	spin_unlock(&c->erase_completion_lock);
2306 	mutex_unlock(&c->erase_free_sem);
2307-	wake_up(&c->erase_wait);
2308 	return;
2309
2310 filebad:
2311diff -Nupr old/fs/jffs2/file.c new/fs/jffs2/file.c
2312--- old/fs/jffs2/file.c	2022-05-09 17:22:53.000000000 +0800
2313+++ new/fs/jffs2/file.c	2022-05-10 09:43:14.250000000 +0800
2314@@ -9,335 +9,30 @@
2315  * For licensing information, see the file 'LICENCE' in this directory.
2316  *
2317  */
2318+#include "los_vm_common.h"
2319
2320-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2321-
2322-#include <linux/kernel.h>
2323-#include <linux/fs.h>
2324-#include <linux/time.h>
2325-#include <linux/pagemap.h>
2326-#include <linux/highmem.h>
2327-#include <linux/crc32.h>
2328-#include <linux/jffs2.h>
2329 #include "nodelist.h"
2330+#include "vfs_jffs2.h"
2331
2332-static int jffs2_write_end(struct file *filp, struct address_space *mapping,
2333-			loff_t pos, unsigned len, unsigned copied,
2334-			struct page *pg, void *fsdata);
2335-static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
2336-			loff_t pos, unsigned len, unsigned flags,
2337-			struct page **pagep, void **fsdata);
2338-static int jffs2_readpage (struct file *filp, struct page *pg);
2339+static unsigned char gc_buffer[PAGE_SIZE];	//avoids malloc when user may be under memory pressure
2340
2341-int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
2342+unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
2343+				   struct jffs2_inode_info *f,
2344+				   unsigned long offset,
2345+				   unsigned long *priv)
2346 {
2347-	struct inode *inode = filp->f_mapping->host;
2348-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2349+	/* FIXME: This works only with one file system mounted at a time */
2350 	int ret;
2351-
2352-	ret = file_write_and_wait_range(filp, start, end);
2353+	ret = jffs2_read_inode_range(c, f, gc_buffer,
2354+			 offset & ~(PAGE_SIZE-1), PAGE_SIZE);
2355 	if (ret)
2356-		return ret;
2357-
2358-	inode_lock(inode);
2359-	/* Trigger GC to flush any pending writes for this inode */
2360-	jffs2_flush_wbuf_gc(c, inode->i_ino);
2361-	inode_unlock(inode);
2362-
2363-	return 0;
2364-}
2365-
2366-const struct file_operations jffs2_file_operations =
2367-{
2368-	.llseek =	generic_file_llseek,
2369-	.open =		generic_file_open,
2370- 	.read_iter =	generic_file_read_iter,
2371- 	.write_iter =	generic_file_write_iter,
2372-	.unlocked_ioctl=jffs2_ioctl,
2373-	.mmap =		generic_file_readonly_mmap,
2374-	.fsync =	jffs2_fsync,
2375-	.splice_read =	generic_file_splice_read,
2376-	.splice_write = iter_file_splice_write,
2377-};
2378-
2379-/* jffs2_file_inode_operations */
2380-
2381-const struct inode_operations jffs2_file_inode_operations =
2382-{
2383-	.get_acl =	jffs2_get_acl,
2384-	.set_acl =	jffs2_set_acl,
2385-	.setattr =	jffs2_setattr,
2386-	.listxattr =	jffs2_listxattr,
2387-};
2388-
2389-const struct address_space_operations jffs2_file_address_operations =
2390-{
2391-	.readpage =	jffs2_readpage,
2392-	.write_begin =	jffs2_write_begin,
2393-	.write_end =	jffs2_write_end,
2394-};
2395-
2396-static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
2397-{
2398-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2399-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2400-	unsigned char *pg_buf;
2401-	int ret;
2402-
2403-	jffs2_dbg(2, "%s(): ino #%lu, page at offset 0x%lx\n",
2404-		  __func__, inode->i_ino, pg->index << PAGE_SHIFT);
2405-
2406-	BUG_ON(!PageLocked(pg));
2407-
2408-	pg_buf = kmap(pg);
2409-	/* FIXME: Can kmap fail? */
2410-
2411-	ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_SHIFT,
2412-				     PAGE_SIZE);
2413-
2414-	if (ret) {
2415-		ClearPageUptodate(pg);
2416-		SetPageError(pg);
2417-	} else {
2418-		SetPageUptodate(pg);
2419-		ClearPageError(pg);
2420-	}
2421-
2422-	flush_dcache_page(pg);
2423-	kunmap(pg);
2424-
2425-	jffs2_dbg(2, "readpage finished\n");
2426-	return ret;
2427-}
2428-
2429-int jffs2_do_readpage_unlock(void *data, struct page *pg)
2430-{
2431-	int ret = jffs2_do_readpage_nolock(data, pg);
2432-	unlock_page(pg);
2433-	return ret;
2434-}
2435-
2436-
2437-static int jffs2_readpage (struct file *filp, struct page *pg)
2438-{
2439-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
2440-	int ret;
2441-
2442-	mutex_lock(&f->sem);
2443-	ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
2444-	mutex_unlock(&f->sem);
2445-	return ret;
2446+		return ERR_PTR(ret);
2447+	return gc_buffer;
2448 }
2449
2450-static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
2451-			loff_t pos, unsigned len, unsigned flags,
2452-			struct page **pagep, void **fsdata)
2453+void jffs2_gc_release_page(struct jffs2_sb_info *c,
2454+			   unsigned char *ptr,
2455+			   unsigned long *priv)
2456 {
2457-	struct page *pg;
2458-	struct inode *inode = mapping->host;
2459-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2460-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2461-	pgoff_t index = pos >> PAGE_SHIFT;
2462-	uint32_t pageofs = index << PAGE_SHIFT;
2463-	int ret = 0;
2464-
2465-	jffs2_dbg(1, "%s()\n", __func__);
2466-
2467-	if (pageofs > inode->i_size) {
2468-		/* Make new hole frag from old EOF to new page */
2469-		struct jffs2_raw_inode ri;
2470-		struct jffs2_full_dnode *fn;
2471-		uint32_t alloc_len;
2472-
2473-		jffs2_dbg(1, "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
2474-			  (unsigned int)inode->i_size, pageofs);
2475-
2476-		ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
2477-					  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
2478-		if (ret)
2479-			goto out_err;
2480-
2481-		mutex_lock(&f->sem);
2482-		memset(&ri, 0, sizeof(ri));
2483-
2484-		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
2485-		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
2486-		ri.totlen = cpu_to_je32(sizeof(ri));
2487-		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
2488-
2489-		ri.ino = cpu_to_je32(f->inocache->ino);
2490-		ri.version = cpu_to_je32(++f->highest_version);
2491-		ri.mode = cpu_to_jemode(inode->i_mode);
2492-		ri.uid = cpu_to_je16(i_uid_read(inode));
2493-		ri.gid = cpu_to_je16(i_gid_read(inode));
2494-		ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
2495-		ri.atime = ri.ctime = ri.mtime = cpu_to_je32(JFFS2_NOW());
2496-		ri.offset = cpu_to_je32(inode->i_size);
2497-		ri.dsize = cpu_to_je32(pageofs - inode->i_size);
2498-		ri.csize = cpu_to_je32(0);
2499-		ri.compr = JFFS2_COMPR_ZERO;
2500-		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
2501-		ri.data_crc = cpu_to_je32(0);
2502-
2503-		fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
2504-
2505-		if (IS_ERR(fn)) {
2506-			ret = PTR_ERR(fn);
2507-			jffs2_complete_reservation(c);
2508-			mutex_unlock(&f->sem);
2509-			goto out_err;
2510-		}
2511-		ret = jffs2_add_full_dnode_to_inode(c, f, fn);
2512-		if (f->metadata) {
2513-			jffs2_mark_node_obsolete(c, f->metadata->raw);
2514-			jffs2_free_full_dnode(f->metadata);
2515-			f->metadata = NULL;
2516-		}
2517-		if (ret) {
2518-			jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n",
2519-				  ret);
2520-			jffs2_mark_node_obsolete(c, fn->raw);
2521-			jffs2_free_full_dnode(fn);
2522-			jffs2_complete_reservation(c);
2523-			mutex_unlock(&f->sem);
2524-			goto out_err;
2525-		}
2526-		jffs2_complete_reservation(c);
2527-		inode->i_size = pageofs;
2528-		mutex_unlock(&f->sem);
2529-	}
2530-
2531-	/*
2532-	 * While getting a page and reading data in, lock c->alloc_sem until
2533-	 * the page is Uptodate. Otherwise GC task may attempt to read the same
2534-	 * page in read_cache_page(), which causes a deadlock.
2535-	 */
2536-	mutex_lock(&c->alloc_sem);
2537-	pg = grab_cache_page_write_begin(mapping, index, flags);
2538-	if (!pg) {
2539-		ret = -ENOMEM;
2540-		goto release_sem;
2541-	}
2542-	*pagep = pg;
2543-
2544-	/*
2545-	 * Read in the page if it wasn't already present. Cannot optimize away
2546-	 * the whole page write case until jffs2_write_end can handle the
2547-	 * case of a short-copy.
2548-	 */
2549-	if (!PageUptodate(pg)) {
2550-		mutex_lock(&f->sem);
2551-		ret = jffs2_do_readpage_nolock(inode, pg);
2552-		mutex_unlock(&f->sem);
2553-		if (ret) {
2554-			unlock_page(pg);
2555-			put_page(pg);
2556-			goto release_sem;
2557-		}
2558-	}
2559-	jffs2_dbg(1, "end write_begin(). pg->flags %lx\n", pg->flags);
2560-
2561-release_sem:
2562-	mutex_unlock(&c->alloc_sem);
2563-out_err:
2564-	return ret;
2565-}
2566-
2567-static int jffs2_write_end(struct file *filp, struct address_space *mapping,
2568-			loff_t pos, unsigned len, unsigned copied,
2569-			struct page *pg, void *fsdata)
2570-{
2571-	/* Actually commit the write from the page cache page we're looking at.
2572-	 * For now, we write the full page out each time. It sucks, but it's simple
2573-	 */
2574-	struct inode *inode = mapping->host;
2575-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2576-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2577-	struct jffs2_raw_inode *ri;
2578-	unsigned start = pos & (PAGE_SIZE - 1);
2579-	unsigned end = start + copied;
2580-	unsigned aligned_start = start & ~3;
2581-	int ret = 0;
2582-	uint32_t writtenlen = 0;
2583-
2584-	jffs2_dbg(1, "%s(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
2585-		  __func__, inode->i_ino, pg->index << PAGE_SHIFT,
2586-		  start, end, pg->flags);
2587-
2588-	/* We need to avoid deadlock with page_cache_read() in
2589-	   jffs2_garbage_collect_pass(). So the page must be
2590-	   up to date to prevent page_cache_read() from trying
2591-	   to re-lock it. */
2592-	BUG_ON(!PageUptodate(pg));
2593-
2594-	if (end == PAGE_SIZE) {
2595-		/* When writing out the end of a page, write out the
2596-		   _whole_ page. This helps to reduce the number of
2597-		   nodes in files which have many short writes, like
2598-		   syslog files. */
2599-		aligned_start = 0;
2600-	}
2601-
2602-	ri = jffs2_alloc_raw_inode();
2603-
2604-	if (!ri) {
2605-		jffs2_dbg(1, "%s(): Allocation of raw inode failed\n",
2606-			  __func__);
2607-		unlock_page(pg);
2608-		put_page(pg);
2609-		return -ENOMEM;
2610-	}
2611-
2612-	/* Set the fields that the generic jffs2_write_inode_range() code can't find */
2613-	ri->ino = cpu_to_je32(inode->i_ino);
2614-	ri->mode = cpu_to_jemode(inode->i_mode);
2615-	ri->uid = cpu_to_je16(i_uid_read(inode));
2616-	ri->gid = cpu_to_je16(i_gid_read(inode));
2617-	ri->isize = cpu_to_je32((uint32_t)inode->i_size);
2618-	ri->atime = ri->ctime = ri->mtime = cpu_to_je32(JFFS2_NOW());
2619-
2620-	/* In 2.4, it was already kmapped by generic_file_write(). Doesn't
2621-	   hurt to do it again. The alternative is ifdefs, which are ugly. */
2622-	kmap(pg);
2623-
2624-	ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
2625-				      (pg->index << PAGE_SHIFT) + aligned_start,
2626-				      end - aligned_start, &writtenlen);
2627-
2628-	kunmap(pg);
2629-
2630-	if (ret) {
2631-		/* There was an error writing. */
2632-		SetPageError(pg);
2633-	}
2634-
2635-	/* Adjust writtenlen for the padding we did, so we don't confuse our caller */
2636-	writtenlen -= min(writtenlen, (start - aligned_start));
2637-
2638-	if (writtenlen) {
2639-		if (inode->i_size < pos + writtenlen) {
2640-			inode->i_size = pos + writtenlen;
2641-			inode->i_blocks = (inode->i_size + 511) >> 9;
2642-
2643-			inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
2644-		}
2645-	}
2646-
2647-	jffs2_free_raw_inode(ri);
2648-
2649-	if (start+writtenlen < end) {
2650-		/* generic_file_write has written more to the page cache than we've
2651-		   actually written to the medium. Mark the page !Uptodate so that
2652-		   it gets reread */
2653-		jffs2_dbg(1, "%s(): Not all bytes written. Marking page !uptodate\n",
2654-			__func__);
2655-		SetPageError(pg);
2656-		ClearPageUptodate(pg);
2657-	}
2658-
2659-	jffs2_dbg(1, "%s() returning %d\n",
2660-		  __func__, writtenlen > 0 ? writtenlen : ret);
2661-	unlock_page(pg);
2662-	put_page(pg);
2663-	return writtenlen > 0 ? writtenlen : ret;
2664+	/* Do nothing */
2665 }
2666diff -Nupr old/fs/jffs2/fs.c new/fs/jffs2/fs.c
2667--- old/fs/jffs2/fs.c	2022-05-09 17:22:53.000000000 +0800
2668+++ new/fs/jffs2/fs.c	2022-05-10 16:13:37.830000000 +0800
2669@@ -10,136 +10,129 @@
2670  *
2671  */
2672
2673-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2674-
2675-#include <linux/capability.h>
2676-#include <linux/kernel.h>
2677-#include <linux/sched.h>
2678-#include <linux/cred.h>
2679-#include <linux/fs.h>
2680-#include <linux/fs_context.h>
2681-#include <linux/list.h>
2682-#include <linux/mtd/mtd.h>
2683-#include <linux/pagemap.h>
2684-#include <linux/slab.h>
2685-#include <linux/vmalloc.h>
2686-#include <linux/vfs.h>
2687-#include <linux/crc32.h>
2688+#include <linux/delay.h>
2689 #include "nodelist.h"
2690+#include "os-linux.h"
2691+#include "los_crc32.h"
2692+#include "jffs2_hash.h"
2693+#include "capability_type.h"
2694+#include "capability_api.h"
2695
2696-static int jffs2_flash_setup(struct jffs2_sb_info *c);
2697-
2698-int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
2699+int jffs2_setattr (struct jffs2_inode *inode, struct IATTR *attr)
2700 {
2701 	struct jffs2_full_dnode *old_metadata, *new_metadata;
2702 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2703 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2704 	struct jffs2_raw_inode *ri;
2705-	union jffs2_device_node dev;
2706-	unsigned char *mdata = NULL;
2707-	int mdatalen = 0;
2708 	unsigned int ivalid;
2709+	mode_t tmp_mode;
2710+	uint c_uid = OsCurrUserGet()->effUserID;
2711+	uint c_gid = OsCurrUserGet()->effGid;
2712 	uint32_t alloclen;
2713 	int ret;
2714 	int alloc_type = ALLOC_NORMAL;
2715
2716 	jffs2_dbg(1, "%s(): ino #%lu\n", __func__, inode->i_ino);
2717-
2718-	/* Special cases - we don't want more than one data node
2719-	   for these types on the medium at any time. So setattr
2720-	   must read the original data associated with the node
2721-	   (i.e. the device numbers or the target name) and write
2722-	   it out again with the appropriate data attached */
2723-	if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
2724-		/* For these, we don't actually need to read the old node */
2725-		mdatalen = jffs2_encode_dev(&dev, inode->i_rdev);
2726-		mdata = (char *)&dev;
2727-		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
2728-			  __func__, mdatalen);
2729-	} else if (S_ISLNK(inode->i_mode)) {
2730-		mutex_lock(&f->sem);
2731-		mdatalen = f->metadata->size;
2732-		mdata = kmalloc(f->metadata->size, GFP_USER);
2733-		if (!mdata) {
2734-			mutex_unlock(&f->sem);
2735-			return -ENOMEM;
2736-		}
2737-		ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
2738-		if (ret) {
2739-			mutex_unlock(&f->sem);
2740-			kfree(mdata);
2741-			return ret;
2742-		}
2743-		mutex_unlock(&f->sem);
2744-		jffs2_dbg(1, "%s(): Writing %d bytes of symlink target\n",
2745-			  __func__, mdatalen);
2746-	}
2747-
2748 	ri = jffs2_alloc_raw_inode();
2749 	if (!ri) {
2750-		if (S_ISLNK(inode->i_mode))
2751-			kfree(mdata);
2752 		return -ENOMEM;
2753 	}
2754
2755-	ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &alloclen,
2756-				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
2757+	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
2758+
2759 	if (ret) {
2760 		jffs2_free_raw_inode(ri);
2761-		if (S_ISLNK(inode->i_mode))
2762-			 kfree(mdata);
2763 		return ret;
2764 	}
2765 	mutex_lock(&f->sem);
2766-	ivalid = iattr->ia_valid;
2767+	ivalid = attr->attr_chg_valid;
2768+	tmp_mode = inode->i_mode;
2769
2770 	ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
2771 	ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
2772-	ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
2773-	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
2774+	ri->totlen = cpu_to_je32(sizeof(*ri));
2775+	ri->hdr_crc = cpu_to_je32(crc32(0, ri, (sizeof(struct jffs2_unknown_node)-4)));
2776
2777 	ri->ino = cpu_to_je32(inode->i_ino);
2778 	ri->version = cpu_to_je32(++f->highest_version);
2779+	ri->uid = cpu_to_je16(inode->i_uid);
2780+	ri->gid = cpu_to_je16(inode->i_gid);
2781+
2782+	if (ivalid & CHG_UID) {
2783+		if (((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) && (!IsCapPermit(CAP_CHOWN))) {
2784+			jffs2_complete_reservation(c);
2785+			jffs2_free_raw_inode(ri);
2786+			mutex_unlock(&f->sem);
2787+			return -EPERM;
2788+		} else {
2789+			ri->uid = cpu_to_je16(attr->attr_chg_uid);
2790+		}
2791+	}
2792+
2793+	if (ivalid & CHG_GID) {
2794+		if (((c_gid != inode->i_gid) || (attr->attr_chg_gid != inode->i_gid)) && (!IsCapPermit(CAP_CHOWN))) {
2795+			jffs2_complete_reservation(c);
2796+			jffs2_free_raw_inode(ri);
2797+			mutex_unlock(&f->sem);
2798+			return -EPERM;
2799+		} else {
2800+			ri->gid = cpu_to_je16(attr->attr_chg_gid);
2801+		}
2802+	}
2803
2804-	ri->uid = cpu_to_je16((ivalid & ATTR_UID)?
2805-		from_kuid(&init_user_ns, iattr->ia_uid):i_uid_read(inode));
2806-	ri->gid = cpu_to_je16((ivalid & ATTR_GID)?
2807-		from_kgid(&init_user_ns, iattr->ia_gid):i_gid_read(inode));
2808-
2809-	if (ivalid & ATTR_MODE)
2810-		ri->mode = cpu_to_jemode(iattr->ia_mode);
2811-	else
2812-		ri->mode = cpu_to_jemode(inode->i_mode);
2813+	if (ivalid & CHG_MODE) {
2814+		if (!IsCapPermit(CAP_FOWNER) && (c_uid != inode->i_uid)) {
2815+			jffs2_complete_reservation(c);
2816+			jffs2_free_raw_inode(ri);
2817+			mutex_unlock(&f->sem);
2818+			return -EPERM;
2819+		} else {
2820+			attr->attr_chg_mode  &= ~S_IFMT; // delete file type
2821+			tmp_mode &= S_IFMT;
2822+			tmp_mode = attr->attr_chg_mode | tmp_mode; // add old file type
2823+		}
2824+	}
2825
2826+	if (ivalid & CHG_ATIME) {
2827+		if ((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) {
2828+			return -EPERM;
2829+		} else {
2830+			ri->atime = cpu_to_je32(attr->attr_chg_atime);
2831+		}
2832+	} else {
2833+		ri->atime = cpu_to_je32(inode->i_atime);
2834+	}
2835+
2836+	if (ivalid & CHG_MTIME) {
2837+		if ((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) {
2838+			return -EPERM;
2839+		} else {
2840+			ri->mtime = cpu_to_je32(attr->attr_chg_mtime);
2841+		}
2842+	} else {
2843+		ri->mtime = cpu_to_je32(Jffs2CurSec());
2844+	}
2845+	ri->mode = cpu_to_jemode(tmp_mode);
2846
2847-	ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
2848-	ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
2849-	ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
2850-	ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
2851+	ri->isize = cpu_to_je32((ivalid & CHG_SIZE) ? attr->attr_chg_size : inode->i_size);
2852+	ri->ctime = cpu_to_je32(Jffs2CurSec());
2853
2854 	ri->offset = cpu_to_je32(0);
2855-	ri->csize = ri->dsize = cpu_to_je32(mdatalen);
2856+	ri->csize = ri->dsize = cpu_to_je32(0);
2857 	ri->compr = JFFS2_COMPR_NONE;
2858-	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
2859+	if (ivalid & CHG_SIZE && inode->i_size < attr->attr_chg_size) {
2860 		/* It's an extension. Make it a hole node */
2861 		ri->compr = JFFS2_COMPR_ZERO;
2862-		ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
2863+		ri->dsize = cpu_to_je32(attr->attr_chg_size - inode->i_size);
2864 		ri->offset = cpu_to_je32(inode->i_size);
2865-	} else if (ivalid & ATTR_SIZE && !iattr->ia_size) {
2866+	} else if (ivalid & CHG_SIZE && !attr->attr_chg_size) {
2867 		/* For truncate-to-zero, treat it as deletion because
2868 		   it'll always be obsoleting all previous nodes */
2869 		alloc_type = ALLOC_DELETION;
2870 	}
2871-	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
2872-	if (mdatalen)
2873-		ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
2874-	else
2875-		ri->data_crc = cpu_to_je32(0);
2876-
2877-	new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, alloc_type);
2878-	if (S_ISLNK(inode->i_mode))
2879-		kfree(mdata);
2880-
2881+	ri->node_crc = cpu_to_je32(crc32(0, ri, (sizeof(*ri)-8)));
2882+	ri->data_crc = cpu_to_je32(0);
2883+	new_metadata = jffs2_write_dnode(c, f, ri, NULL, 0, alloc_type);
2884 	if (IS_ERR(new_metadata)) {
2885 		jffs2_complete_reservation(c);
2886 		jffs2_free_raw_inode(ri);
2887@@ -147,23 +140,20 @@ int jffs2_do_setattr (struct inode *inod
2888 		return PTR_ERR(new_metadata);
2889 	}
2890 	/* It worked. Update the inode */
2891-	inode->i_atime = ITIME(je32_to_cpu(ri->atime));
2892-	inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
2893-	inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
2894+	inode->i_atime = je32_to_cpu(ri->atime);
2895+	inode->i_ctime = je32_to_cpu(ri->ctime);
2896+	inode->i_mtime = je32_to_cpu(ri->mtime);
2897 	inode->i_mode = jemode_to_cpu(ri->mode);
2898-	i_uid_write(inode, je16_to_cpu(ri->uid));
2899-	i_gid_write(inode, je16_to_cpu(ri->gid));
2900-
2901+	inode->i_uid = je16_to_cpu(ri->uid);
2902+	inode->i_gid = je16_to_cpu(ri->gid);
2903
2904 	old_metadata = f->metadata;
2905+	if (ivalid & CHG_SIZE && inode->i_size > attr->attr_chg_size)
2906+		jffs2_truncate_fragtree (c, &f->fragtree, attr->attr_chg_size);
2907
2908-	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
2909-		jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
2910-
2911-	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
2912+	if (ivalid & CHG_SIZE && inode->i_size < attr->attr_chg_size) {
2913 		jffs2_add_full_dnode_to_inode(c, f, new_metadata);
2914-		inode->i_size = iattr->ia_size;
2915-		inode->i_blocks = (inode->i_size + 511) >> 9;
2916+		inode->i_size = attr->attr_chg_size;
2917 		f->metadata = NULL;
2918 	} else {
2919 		f->metadata = new_metadata;
2920@@ -182,315 +172,201 @@ int jffs2_do_setattr (struct inode *inod
2921 	   We are protected from a simultaneous write() extending i_size
2922 	   back past iattr->ia_size, because do_truncate() holds the
2923 	   generic inode semaphore. */
2924-	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
2925-		truncate_setsize(inode, iattr->ia_size);
2926-		inode->i_blocks = (inode->i_size + 511) >> 9;
2927+	if (ivalid & CHG_SIZE && inode->i_size > attr->attr_chg_size) {
2928+		inode->i_size = attr->attr_chg_size; // truncate_setsize
2929 	}
2930
2931 	return 0;
2932 }
2933
2934-int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
2935+static void jffs2_clear_inode (struct jffs2_inode *inode)
2936 {
2937-	struct inode *inode = d_inode(dentry);
2938-	int rc;
2939-
2940-	rc = setattr_prepare(dentry, iattr);
2941-	if (rc)
2942-		return rc;
2943-
2944-	rc = jffs2_do_setattr(inode, iattr);
2945-	if (!rc && (iattr->ia_valid & ATTR_MODE))
2946-		rc = posix_acl_chmod(inode, inode->i_mode);
2947+	/* We can forget about this inode for now - drop all
2948+	 *  the nodelists associated with it, etc.
2949+	 */
2950+	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2951+	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2952
2953-	return rc;
2954+	jffs2_do_clear_inode(c, f);
2955 }
2956
2957-int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
2958+static struct jffs2_inode *ilookup(struct super_block *sb, uint32_t ino)
2959 {
2960-	struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
2961-	unsigned long avail;
2962+	struct jffs2_inode *node = NULL;
2963
2964-	buf->f_type = JFFS2_SUPER_MAGIC;
2965-	buf->f_bsize = 1 << PAGE_SHIFT;
2966-	buf->f_blocks = c->flash_size >> PAGE_SHIFT;
2967-	buf->f_files = 0;
2968-	buf->f_ffree = 0;
2969-	buf->f_namelen = JFFS2_MAX_NAME_LEN;
2970-	buf->f_fsid.val[0] = JFFS2_SUPER_MAGIC;
2971-	buf->f_fsid.val[1] = c->mtd->index;
2972-
2973-	spin_lock(&c->erase_completion_lock);
2974-	avail = c->dirty_size + c->free_size;
2975-	if (avail > c->sector_size * c->resv_blocks_write)
2976-		avail -= c->sector_size * c->resv_blocks_write;
2977-	else
2978-		avail = 0;
2979-	spin_unlock(&c->erase_completion_lock);
2980-
2981-	buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
2982+	if (sb->s_root == NULL) {
2983+		return NULL;
2984+	}
2985
2986-	return 0;
2987+	// Check for this inode in the cache
2988+	Jffs2NodeLock();
2989+	(void)Jffs2HashGet(&sb->s_node_hash_lock, &sb->s_node_hash[0], sb, ino, &node);
2990+	Jffs2NodeUnlock();
2991+	return node;
2992 }
2993
2994-
2995-void jffs2_evict_inode (struct inode *inode)
2996+struct jffs2_inode *new_inode(struct super_block *sb)
2997 {
2998-	/* We can forget about this inode for now - drop all
2999-	 *  the nodelists associated with it, etc.
3000-	 */
3001-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
3002-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
3003+	struct jffs2_inode *inode = NULL;
3004
3005-	jffs2_dbg(1, "%s(): ino #%lu mode %o\n",
3006-		  __func__, inode->i_ino, inode->i_mode);
3007-	truncate_inode_pages_final(&inode->i_data);
3008-	clear_inode(inode);
3009-	jffs2_do_clear_inode(c, f);
3010+	inode = zalloc(sizeof (struct jffs2_inode));
3011+	if (inode == NULL)
3012+		return 0;
3013+
3014+	D2(PRINTK("malloc new_inode %x ####################################\n",
3015+		inode));
3016+
3017+	inode->i_sb = sb;
3018+	inode->i_ino = 1;
3019+	inode->i_nlink = 1;    // Let JFFS2 manage the link count
3020+	inode->i_size = 0;
3021+	LOS_ListInit((&(inode->i_hashlist)));
3022+
3023+	return inode;
3024 }
3025
3026-struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
3027+struct jffs2_inode *jffs2_iget(struct super_block *sb, uint32_t ino)
3028 {
3029 	struct jffs2_inode_info *f;
3030 	struct jffs2_sb_info *c;
3031 	struct jffs2_raw_inode latest_node;
3032-	union jffs2_device_node jdev;
3033-	struct inode *inode;
3034-	dev_t rdev = 0;
3035+	struct jffs2_inode *inode;
3036 	int ret;
3037
3038-	jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
3039-
3040-	inode = iget_locked(sb, ino);
3041-	if (!inode)
3042-		return ERR_PTR(-ENOMEM);
3043-	if (!(inode->i_state & I_NEW))
3044+	Jffs2NodeLock();
3045+	inode = ilookup(sb, ino);
3046+	if (inode) {
3047+		Jffs2NodeUnlock();
3048 		return inode;
3049+	}
3050+	inode = new_inode(sb);
3051+	if (inode == NULL) {
3052+		Jffs2NodeUnlock();
3053+		return (struct jffs2_inode *)-ENOMEM;
3054+	}
3055
3056+	inode->i_ino = ino;
3057 	f = JFFS2_INODE_INFO(inode);
3058 	c = JFFS2_SB_INFO(inode->i_sb);
3059
3060-	jffs2_init_inode_info(f);
3061-	mutex_lock(&f->sem);
3062+	(void)mutex_init(&f->sem);
3063+	(void)mutex_lock(&f->sem);
3064
3065 	ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
3066-	if (ret)
3067-		goto error;
3068+	if (ret) {
3069+		(void)mutex_unlock(&f->sem);
3070+        inode->i_nlink = 0;
3071+        free(inode);
3072+		Jffs2NodeUnlock();
3073+		return (struct jffs2_inode *)ret;
3074+	}
3075
3076 	inode->i_mode = jemode_to_cpu(latest_node.mode);
3077-	i_uid_write(inode, je16_to_cpu(latest_node.uid));
3078-	i_gid_write(inode, je16_to_cpu(latest_node.gid));
3079+	inode->i_uid = je16_to_cpu(latest_node.uid);
3080+	inode->i_gid = je16_to_cpu(latest_node.gid);
3081 	inode->i_size = je32_to_cpu(latest_node.isize);
3082-	inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
3083-	inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
3084-	inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
3085-
3086-	set_nlink(inode, f->inocache->pino_nlink);
3087+	inode->i_atime = je32_to_cpu(latest_node.atime);
3088+	inode->i_mtime = je32_to_cpu(latest_node.mtime);
3089+	inode->i_ctime = je32_to_cpu(latest_node.ctime);
3090+	inode->i_nlink = f->inocache->pino_nlink;
3091
3092-	inode->i_blocks = (inode->i_size + 511) >> 9;
3093+	(void)mutex_unlock(&f->sem);
3094
3095-	switch (inode->i_mode & S_IFMT) {
3096-
3097-	case S_IFLNK:
3098-		inode->i_op = &jffs2_symlink_inode_operations;
3099-		inode->i_link = f->target;
3100-		break;
3101-
3102-	case S_IFDIR:
3103-	{
3104-		struct jffs2_full_dirent *fd;
3105-		set_nlink(inode, 2); /* parent and '.' */
3106-
3107-		for (fd=f->dents; fd; fd = fd->next) {
3108-			if (fd->type == DT_DIR && fd->ino)
3109-				inc_nlink(inode);
3110-		}
3111-		/* Root dir gets i_nlink 3 for some reason */
3112-		if (inode->i_ino == 1)
3113-			inc_nlink(inode);
3114-
3115-		inode->i_op = &jffs2_dir_inode_operations;
3116-		inode->i_fop = &jffs2_dir_operations;
3117-		break;
3118-	}
3119-	case S_IFREG:
3120-		inode->i_op = &jffs2_file_inode_operations;
3121-		inode->i_fop = &jffs2_file_operations;
3122-		inode->i_mapping->a_ops = &jffs2_file_address_operations;
3123-		inode->i_mapping->nrpages = 0;
3124-		break;
3125-
3126-	case S_IFBLK:
3127-	case S_IFCHR:
3128-		/* Read the device numbers from the media */
3129-		if (f->metadata->size != sizeof(jdev.old_id) &&
3130-		    f->metadata->size != sizeof(jdev.new_id)) {
3131-			pr_notice("Device node has strange size %d\n",
3132-				  f->metadata->size);
3133-			goto error_io;
3134-		}
3135-		jffs2_dbg(1, "Reading device numbers from flash\n");
3136-		ret = jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size);
3137-		if (ret < 0) {
3138-			/* Eep */
3139-			pr_notice("Read device numbers for inode %lu failed\n",
3140-				  (unsigned long)inode->i_ino);
3141-			goto error;
3142-		}
3143-		if (f->metadata->size == sizeof(jdev.old_id))
3144-			rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
3145-		else
3146-			rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
3147-		fallthrough;
3148-
3149-	case S_IFSOCK:
3150-	case S_IFIFO:
3151-		inode->i_op = &jffs2_file_inode_operations;
3152-		init_special_inode(inode, inode->i_mode, rdev);
3153-		break;
3154-
3155-	default:
3156-		pr_warn("%s(): Bogus i_mode %o for ino %lu\n",
3157-			__func__, inode->i_mode, (unsigned long)inode->i_ino);
3158-	}
3159-
3160-	mutex_unlock(&f->sem);
3161+	(void)Jffs2HashInsert(&sb->s_node_hash_lock, &sb->s_node_hash[0], inode, ino);
3162
3163 	jffs2_dbg(1, "jffs2_read_inode() returning\n");
3164-	unlock_new_inode(inode);
3165-	return inode;
3166-
3167-error_io:
3168-	ret = -EIO;
3169-error:
3170-	mutex_unlock(&f->sem);
3171-	iget_failed(inode);
3172-	return ERR_PTR(ret);
3173-}
3174+	Jffs2NodeUnlock();
3175
3176-void jffs2_dirty_inode(struct inode *inode, int flags)
3177-{
3178-	struct iattr iattr;
3179-
3180-	if (!(inode->i_state & I_DIRTY_DATASYNC)) {
3181-		jffs2_dbg(2, "%s(): not calling setattr() for ino #%lu\n",
3182-			  __func__, inode->i_ino);
3183-		return;
3184-	}
3185-
3186-	jffs2_dbg(1, "%s(): calling setattr() for ino #%lu\n",
3187-		  __func__, inode->i_ino);
3188-
3189-	iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
3190-	iattr.ia_mode = inode->i_mode;
3191-	iattr.ia_uid = inode->i_uid;
3192-	iattr.ia_gid = inode->i_gid;
3193-	iattr.ia_atime = inode->i_atime;
3194-	iattr.ia_mtime = inode->i_mtime;
3195-	iattr.ia_ctime = inode->i_ctime;
3196-
3197-	jffs2_do_setattr(inode, &iattr);
3198+	return inode;
3199 }
3200
3201-int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc)
3202-{
3203-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
3204-
3205-	if (c->flags & JFFS2_SB_FLAG_RO && !sb_rdonly(sb))
3206-		return -EROFS;
3207
3208-	/* We stop if it was running, then restart if it needs to.
3209-	   This also catches the case where it was stopped and this
3210-	   is just a remount to restart it.
3211-	   Flush the writebuffer, if neccecary, else we loose it */
3212-	if (!sb_rdonly(sb)) {
3213-		jffs2_stop_garbage_collect_thread(c);
3214-		mutex_lock(&c->alloc_sem);
3215-		jffs2_flush_wbuf_pad(c);
3216-		mutex_unlock(&c->alloc_sem);
3217-	}
3218+// -------------------------------------------------------------------------
3219+// Decrement the reference count on an inode. If this makes the ref count
3220+// zero, then this inode can be freed.
3221+
3222+int jffs2_iput(struct jffs2_inode *i)
3223+{
3224+	// Called in jffs2_find
3225+	// (and jffs2_open and jffs2_ops_mkdir?)
3226+	// super.c jffs2_fill_super,
3227+	// and gc.c jffs2_garbage_collect_pass
3228+	struct jffs2_inode_info *f = NULL;
3229+
3230+	Jffs2NodeLock();
3231+	if (!i || i->i_nlink) {
3232+		// and let it fault...
3233+		Jffs2NodeUnlock();
3234+		return -EBUSY;
3235+	}
3236+
3237+	jffs2_clear_inode(i);
3238+	f = JFFS2_INODE_INFO(i);
3239+	(void)mutex_destroy(&(f->sem));
3240+	(void)Jffs2HashRemove(&i->i_sb->s_node_hash_lock, i);
3241+	(void)memset_s(i, sizeof(*i), 0x5a, sizeof(*i));
3242+	free(i);
3243+	Jffs2NodeUnlock();
3244
3245-	if (!(fc->sb_flags & SB_RDONLY))
3246-		jffs2_start_garbage_collect_thread(c);
3247-
3248-	fc->sb_flags |= SB_NOATIME;
3249 	return 0;
3250 }
3251
3252+
3253 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
3254    fill in the raw_inode while you're at it. */
3255-struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode, struct jffs2_raw_inode *ri)
3256+struct jffs2_inode *jffs2_new_inode (struct jffs2_inode *dir_i, int mode, struct jffs2_raw_inode *ri)
3257 {
3258-	struct inode *inode;
3259+	struct jffs2_inode *inode;
3260 	struct super_block *sb = dir_i->i_sb;
3261 	struct jffs2_sb_info *c;
3262 	struct jffs2_inode_info *f;
3263 	int ret;
3264
3265-	jffs2_dbg(1, "%s(): dir_i %ld, mode 0x%x\n",
3266-		  __func__, dir_i->i_ino, mode);
3267-
3268 	c = JFFS2_SB_INFO(sb);
3269
3270+	Jffs2NodeLock();
3271 	inode = new_inode(sb);
3272
3273 	if (!inode)
3274-		return ERR_PTR(-ENOMEM);
3275+		return (struct jffs2_inode *)-ENOMEM;
3276
3277 	f = JFFS2_INODE_INFO(inode);
3278-	jffs2_init_inode_info(f);
3279-	mutex_lock(&f->sem);
3280+	(void)mutex_init(&f->sem);
3281+	(void)mutex_lock(&f->sem);;
3282
3283 	memset(ri, 0, sizeof(*ri));
3284 	/* Set OS-specific defaults for new inodes */
3285-	ri->uid = cpu_to_je16(from_kuid(&init_user_ns, current_fsuid()));
3286+	ri->uid = cpu_to_je16(OsCurrUserGet()->effUserID);
3287+	ri->gid = cpu_to_je16(OsCurrUserGet()->effGid);
3288
3289-	if (dir_i->i_mode & S_ISGID) {
3290-		ri->gid = cpu_to_je16(i_gid_read(dir_i));
3291-		if (S_ISDIR(mode))
3292-			mode |= S_ISGID;
3293-	} else {
3294-		ri->gid = cpu_to_je16(from_kgid(&init_user_ns, current_fsgid()));
3295-	}
3296-
3297-	/* POSIX ACLs have to be processed now, at least partly.
3298-	   The umask is only applied if there's no default ACL */
3299-	ret = jffs2_init_acl_pre(dir_i, inode, &mode);
3300-	if (ret) {
3301-		mutex_unlock(&f->sem);
3302-		make_bad_inode(inode);
3303-		iput(inode);
3304-		return ERR_PTR(ret);
3305-	}
3306 	ret = jffs2_do_new_inode (c, f, mode, ri);
3307 	if (ret) {
3308-		mutex_unlock(&f->sem);
3309-		make_bad_inode(inode);
3310-		iput(inode);
3311-		return ERR_PTR(ret);
3312+		mutex_unlock(&(f->sem));
3313+		jffs2_clear_inode(inode);
3314+		(void)mutex_destroy(&(f->sem));
3315+		(void)memset_s(inode, sizeof(*inode), 0x6a, sizeof(*inode));
3316+		free(inode);
3317+		Jffs2NodeUnlock();
3318+		return (struct jffs2_inode *)ret;
3319+
3320 	}
3321-	set_nlink(inode, 1);
3322+	inode->i_nlink = 1;
3323 	inode->i_ino = je32_to_cpu(ri->ino);
3324 	inode->i_mode = jemode_to_cpu(ri->mode);
3325-	i_gid_write(inode, je16_to_cpu(ri->gid));
3326-	i_uid_write(inode, je16_to_cpu(ri->uid));
3327-	inode->i_atime = inode->i_ctime = inode->i_mtime = current_time(inode);
3328-	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
3329+	inode->i_gid = je16_to_cpu(ri->gid);
3330+	inode->i_uid = je16_to_cpu(ri->uid);
3331+	inode->i_atime = inode->i_ctime = inode->i_mtime = Jffs2CurSec();
3332+	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(inode->i_mtime);
3333
3334-	inode->i_blocks = 0;
3335 	inode->i_size = 0;
3336
3337-	if (insert_inode_locked(inode) < 0) {
3338-		mutex_unlock(&f->sem);
3339-		make_bad_inode(inode);
3340-		iput(inode);
3341-		return ERR_PTR(-EINVAL);
3342-	}
3343+	(void)Jffs2HashInsert(&sb->s_node_hash_lock, &sb->s_node_hash[0], inode, inode->i_ino);
3344+	Jffs2NodeUnlock();
3345
3346 	return inode;
3347 }
3348
3349-static int calculate_inocache_hashsize(uint32_t flash_size)
3350+int calculate_inocache_hashsize(uint32_t flash_size)
3351 {
3352 	/*
3353 	 * Pick a inocache hash size based on the size of the medium.
3354@@ -510,117 +386,17 @@ static int calculate_inocache_hashsize(u
3355 	return hashsize;
3356 }
3357
3358-int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc)
3359-{
3360-	struct jffs2_sb_info *c;
3361-	struct inode *root_i;
3362-	int ret;
3363-	size_t blocks;
3364-
3365-	c = JFFS2_SB_INFO(sb);
3366-
3367-	/* Do not support the MLC nand */
3368-	if (c->mtd->type == MTD_MLCNANDFLASH)
3369-		return -EINVAL;
3370-
3371-#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
3372-	if (c->mtd->type == MTD_NANDFLASH) {
3373-		errorf(fc, "Cannot operate on NAND flash unless jffs2 NAND support is compiled in");
3374-		return -EINVAL;
3375-	}
3376-	if (c->mtd->type == MTD_DATAFLASH) {
3377-		errorf(fc, "Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in");
3378-		return -EINVAL;
3379-	}
3380-#endif
3381-
3382-	c->flash_size = c->mtd->size;
3383-	c->sector_size = c->mtd->erasesize;
3384-	blocks = c->flash_size / c->sector_size;
3385-
3386-	/*
3387-	 * Size alignment check
3388-	 */
3389-	if ((c->sector_size * blocks) != c->flash_size) {
3390-		c->flash_size = c->sector_size * blocks;
3391-		infof(fc, "Flash size not aligned to erasesize, reducing to %dKiB",
3392-		      c->flash_size / 1024);
3393-	}
3394-
3395-	if (c->flash_size < 5*c->sector_size) {
3396-		errorf(fc, "Too few erase blocks (%d)",
3397-		       c->flash_size / c->sector_size);
3398-		return -EINVAL;
3399-	}
3400-
3401-	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
3402-
3403-	/* NAND (or other bizarre) flash... do setup accordingly */
3404-	ret = jffs2_flash_setup(c);
3405-	if (ret)
3406-		return ret;
3407-
3408-	c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
3409-	c->inocache_list = kcalloc(c->inocache_hashsize, sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
3410-	if (!c->inocache_list) {
3411-		ret = -ENOMEM;
3412-		goto out_wbuf;
3413-	}
3414-
3415-	jffs2_init_xattr_subsystem(c);
3416-
3417-	if ((ret = jffs2_do_mount_fs(c)))
3418-		goto out_inohash;
3419-
3420-	jffs2_dbg(1, "%s(): Getting root inode\n", __func__);
3421-	root_i = jffs2_iget(sb, 1);
3422-	if (IS_ERR(root_i)) {
3423-		jffs2_dbg(1, "get root inode failed\n");
3424-		ret = PTR_ERR(root_i);
3425-		goto out_root;
3426-	}
3427-
3428-	ret = -ENOMEM;
3429-
3430-	jffs2_dbg(1, "%s(): d_make_root()\n", __func__);
3431-	sb->s_root = d_make_root(root_i);
3432-	if (!sb->s_root)
3433-		goto out_root;
3434-
3435-	sb->s_maxbytes = 0xFFFFFFFF;
3436-	sb->s_blocksize = PAGE_SIZE;
3437-	sb->s_blocksize_bits = PAGE_SHIFT;
3438-	sb->s_magic = JFFS2_SUPER_MAGIC;
3439-	sb->s_time_min = 0;
3440-	sb->s_time_max = U32_MAX;
3441-
3442-	if (!sb_rdonly(sb))
3443-		jffs2_start_garbage_collect_thread(c);
3444-	return 0;
3445-
3446-out_root:
3447-	jffs2_free_ino_caches(c);
3448-	jffs2_free_raw_node_refs(c);
3449-	kvfree(c->blocks);
3450- out_inohash:
3451-	jffs2_clear_xattr_subsystem(c);
3452-	kfree(c->inocache_list);
3453- out_wbuf:
3454-	jffs2_flash_cleanup(c);
3455-
3456-	return ret;
3457-}
3458-
3459 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
3460-				   struct jffs2_inode_info *f)
3461+		   	    struct jffs2_inode_info *f)
3462 {
3463-	iput(OFNI_EDONI_2SFFJ(f));
3464+	struct jffs2_inode *node = OFNI_EDONI_2SFFJ(f);
3465+	jffs2_iput(node);
3466 }
3467
3468 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
3469 					      int inum, int unlinked)
3470 {
3471-	struct inode *inode;
3472+	struct jffs2_inode *inode;
3473 	struct jffs2_inode_cache *ic;
3474
3475 	if (unlinked) {
3476@@ -668,72 +444,9 @@ struct jffs2_inode_info *jffs2_gc_fetch_
3477 		   Just iget() it, and if read_inode() is necessary that's OK.
3478 		*/
3479 		inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
3480-		if (IS_ERR(inode))
3481-			return ERR_CAST(inode);
3482-	}
3483-	if (is_bad_inode(inode)) {
3484-		pr_notice("Eep. read_inode() failed for ino #%u. unlinked %d\n",
3485-			  inum, unlinked);
3486-		/* NB. This will happen again. We need to do something appropriate here. */
3487-		iput(inode);
3488-		return ERR_PTR(-EIO);
3489+		if (inode <= 0)
3490+			return (struct jffs2_inode_info *)inode;
3491 	}
3492
3493 	return JFFS2_INODE_INFO(inode);
3494 }
3495-
3496-static int jffs2_flash_setup(struct jffs2_sb_info *c) {
3497-	int ret = 0;
3498-
3499-	if (jffs2_cleanmarker_oob(c)) {
3500-		/* NAND flash... do setup accordingly */
3501-		ret = jffs2_nand_flash_setup(c);
3502-		if (ret)
3503-			return ret;
3504-	}
3505-
3506-	/* and Dataflash */
3507-	if (jffs2_dataflash(c)) {
3508-		ret = jffs2_dataflash_setup(c);
3509-		if (ret)
3510-			return ret;
3511-	}
3512-
3513-	/* and Intel "Sibley" flash */
3514-	if (jffs2_nor_wbuf_flash(c)) {
3515-		ret = jffs2_nor_wbuf_flash_setup(c);
3516-		if (ret)
3517-			return ret;
3518-	}
3519-
3520-	/* and an UBI volume */
3521-	if (jffs2_ubivol(c)) {
3522-		ret = jffs2_ubivol_setup(c);
3523-		if (ret)
3524-			return ret;
3525-	}
3526-
3527-	return ret;
3528-}
3529-
3530-void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
3531-
3532-	if (jffs2_cleanmarker_oob(c)) {
3533-		jffs2_nand_flash_cleanup(c);
3534-	}
3535-
3536-	/* and DataFlash */
3537-	if (jffs2_dataflash(c)) {
3538-		jffs2_dataflash_cleanup(c);
3539-	}
3540-
3541-	/* and Intel "Sibley" flash */
3542-	if (jffs2_nor_wbuf_flash(c)) {
3543-		jffs2_nor_wbuf_flash_cleanup(c);
3544-	}
3545-
3546-	/* and an UBI volume */
3547-	if (jffs2_ubivol(c)) {
3548-		jffs2_ubivol_cleanup(c);
3549-	}
3550-}
3551diff -Nupr old/fs/jffs2/gc.c new/fs/jffs2/gc.c
3552--- old/fs/jffs2/gc.c	2022-05-09 17:22:53.000000000 +0800
3553+++ new/fs/jffs2/gc.c	2022-05-10 16:11:42.090000000 +0800
3554@@ -10,17 +10,17 @@
3555  *
3556  */
3557
3558-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3559-
3560 #include <linux/kernel.h>
3561-#include <linux/mtd/mtd.h>
3562 #include <linux/slab.h>
3563 #include <linux/pagemap.h>
3564-#include <linux/crc32.h>
3565+#include <linux/delay.h>
3566+#include <linux/semaphore.h>
3567 #include <linux/compiler.h>
3568 #include <linux/stat.h>
3569+#include "mtd_dev.h"
3570 #include "nodelist.h"
3571 #include "compr.h"
3572+#include "los_crc32.h"
3573
3574 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
3575 					  struct jffs2_inode_cache *ic,
3576@@ -43,7 +43,7 @@ static int jffs2_garbage_collect_live(st
3577 /* Called with erase_completion_lock held */
3578 static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
3579 {
3580-	struct jffs2_eraseblock *ret;
3581+	struct jffs2_eraseblock *ret = NULL;
3582 	struct list_head *nextlist = NULL;
3583 	int n = jiffies % 128;
3584
3585@@ -131,62 +131,40 @@ int jffs2_garbage_collect_pass(struct jf
3586 	int ret = 0, inum, nlink;
3587 	int xattr = 0;
3588
3589-	if (mutex_lock_interruptible(&c->alloc_sem))
3590+	if (mutex_lock(&c->alloc_sem))
3591 		return -EINTR;
3592
3593-
3594 	for (;;) {
3595-		/* We can't start doing GC until we've finished checking
3596-		   the node CRCs etc. */
3597-		int bucket, want_ino;
3598-
3599 		spin_lock(&c->erase_completion_lock);
3600 		if (!c->unchecked_size)
3601 			break;
3602+
3603+		/* We can't start doing GC yet. We haven't finished checking
3604+		   the node CRCs etc. Do it now. */
3605+
3606+		/* checked_ino is protected by the alloc_sem */
3607+		if (c->checked_ino > c->highest_ino && xattr) {
3608+			pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
3609+				c->unchecked_size);
3610+			jffs2_dbg_dump_block_lists_nolock(c);
3611+			spin_unlock(&c->erase_completion_lock);
3612+			mutex_unlock(&c->alloc_sem);
3613+			return -ENOSPC;
3614+		}
3615+
3616 		spin_unlock(&c->erase_completion_lock);
3617
3618 		if (!xattr)
3619 			xattr = jffs2_verify_xattr(c);
3620
3621 		spin_lock(&c->inocache_lock);
3622-		/* Instead of doing the inodes in numeric order, doing a lookup
3623-		 * in the hash for each possible number, just walk the hash
3624-		 * buckets of *existing* inodes. This means that we process
3625-		 * them out-of-order, but it can be a lot faster if there's
3626-		 * a sparse inode# space. Which there often is. */
3627-		want_ino = c->check_ino;
3628-		for (bucket = c->check_ino % c->inocache_hashsize ; bucket < c->inocache_hashsize; bucket++) {
3629-			for (ic = c->inocache_list[bucket]; ic; ic = ic->next) {
3630-				if (ic->ino < want_ino)
3631-					continue;
3632-
3633-				if (ic->state != INO_STATE_CHECKEDABSENT &&
3634-				    ic->state != INO_STATE_PRESENT)
3635-					goto got_next; /* with inocache_lock held */
3636
3637-				jffs2_dbg(1, "Skipping ino #%u already checked\n",
3638-					  ic->ino);
3639-			}
3640-			want_ino = 0;
3641-		}
3642-
3643-		/* Point c->check_ino past the end of the last bucket. */
3644-		c->check_ino = ((c->highest_ino + c->inocache_hashsize + 1) &
3645-				~c->inocache_hashsize) - 1;
3646-
3647-		spin_unlock(&c->inocache_lock);
3648-
3649-		pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
3650-			c->unchecked_size);
3651-		jffs2_dbg_dump_block_lists_nolock(c);
3652-		mutex_unlock(&c->alloc_sem);
3653-		return -ENOSPC;
3654+		ic = jffs2_get_ino_cache(c, c->checked_ino++);
3655
3656-	got_next:
3657-		/* For next time round the loop, we want c->checked_ino to indicate
3658-		 * the *next* one we want to check. And since we're walking the
3659-		 * buckets rather than doing it sequentially, it's: */
3660-		c->check_ino = ic->ino + c->inocache_hashsize;
3661+		if (!ic) {
3662+			spin_unlock(&c->inocache_lock);
3663+			continue;
3664+		}
3665
3666 		if (!ic->pino_nlink) {
3667 			jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
3668@@ -198,6 +176,8 @@ int jffs2_garbage_collect_pass(struct jf
3669 		switch(ic->state) {
3670 		case INO_STATE_CHECKEDABSENT:
3671 		case INO_STATE_PRESENT:
3672+			jffs2_dbg(1, "Skipping ino #%u already checked\n",
3673+				  ic->ino);
3674 			spin_unlock(&c->inocache_lock);
3675 			continue;
3676
3677@@ -207,6 +187,7 @@ int jffs2_garbage_collect_pass(struct jf
3678 				ic->ino, ic->state);
3679 			spin_unlock(&c->inocache_lock);
3680 			BUG();
3681+			break;
3682
3683 		case INO_STATE_READING:
3684 			/* We need to wait for it to finish, lest we move on
3685@@ -216,7 +197,7 @@ int jffs2_garbage_collect_pass(struct jf
3686 				  ic->ino);
3687 			/* We need to come back again for the _same_ inode. We've
3688 			 made no progress in this case, but that should be OK */
3689-			c->check_ino = ic->ino;
3690+			c->checked_ino--;
3691
3692 			mutex_unlock(&c->alloc_sem);
3693 			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
3694@@ -224,6 +205,7 @@ int jffs2_garbage_collect_pass(struct jf
3695
3696 		default:
3697 			BUG();
3698+			break;
3699
3700 		case INO_STATE_UNCHECKED:
3701 			;
3702@@ -290,7 +272,7 @@ int jffs2_garbage_collect_pass(struct jf
3703 	raw = jeb->gc_node;
3704 	gcblock_dirty = jeb->dirty_size;
3705
3706-	while(ref_obsolete(raw)) {
3707+	while(raw && ref_obsolete(raw)) {
3708 		jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
3709 			  ref_offset(raw));
3710 		raw = ref_next(raw);
3711@@ -310,7 +292,7 @@ int jffs2_garbage_collect_pass(struct jf
3712 	jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
3713 		  ref_offset(raw));
3714
3715-	if (!raw->next_in_ino) {
3716+	if (raw &&!raw->next_in_ino) {
3717 		/* Inode-less node. Clean marker, snapshot or something like that */
3718 		spin_unlock(&c->erase_completion_lock);
3719 		if (ref_flags(raw) == REF_PRISTINE) {
3720@@ -368,7 +350,7 @@ int jffs2_garbage_collect_pass(struct jf
3721 		   We can just copy any pristine nodes, but have
3722 		   to prevent anyone else from doing read_inode() while
3723 		   we're at it, so we set the state accordingly */
3724-		if (ref_flags(raw) == REF_PRISTINE)
3725+		if (raw && ref_flags(raw) == REF_PRISTINE)
3726 			ic->state = INO_STATE_GC;
3727 		else {
3728 			jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
3729@@ -393,6 +375,7 @@ int jffs2_garbage_collect_pass(struct jf
3730 		mutex_unlock(&c->alloc_sem);
3731 		spin_unlock(&c->inocache_lock);
3732 		BUG();
3733+		break;
3734
3735 	case INO_STATE_READING:
3736 		/* Someone's currently trying to read it. We must wait for
3737@@ -430,7 +413,6 @@ int jffs2_garbage_collect_pass(struct jf
3738
3739 		spin_lock(&c->inocache_lock);
3740 		ic->state = INO_STATE_CHECKEDABSENT;
3741-		wake_up(&c->inocache_wq);
3742
3743 		if (ret != -EBADFD) {
3744 			spin_unlock(&c->inocache_lock);
3745@@ -460,9 +442,7 @@ int jffs2_garbage_collect_pass(struct jf
3746 		ret = 0;
3747 		goto release_sem;
3748 	}
3749-
3750 	ret = jffs2_garbage_collect_live(c, jeb, raw, f);
3751-
3752 	jffs2_gc_release_inode(c, f);
3753
3754  test_gcnode:
3755@@ -541,7 +521,7 @@ static int jffs2_garbage_collect_live(st
3756 				break; /* We've found them all */
3757 		}
3758 	}
3759-	if (fn) {
3760+	if (fn != NULL && frag != NULL) {
3761 		if (ref_flags(raw) == REF_PRISTINE) {
3762 			ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
3763 			if (!ret) {
3764@@ -552,7 +532,7 @@ static int jffs2_garbage_collect_live(st
3765 				goto upnout;
3766 		}
3767 		/* We found a datanode. Do the GC */
3768-		if((start >> PAGE_SHIFT) < ((end-1) >> PAGE_SHIFT)) {
3769+		if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
3770 			/* It crosses a page boundary. Therefore, it must be a hole. */
3771 			ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
3772 		} else {
3773@@ -635,6 +615,7 @@ static int jffs2_garbage_collect_pristin
3774 	if (je32_to_cpu(node->u.hdr_crc) != crc) {
3775 		pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3776 			ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
3777+		jffs2_dbg_dump_node(c, ref_offset(raw));
3778 		goto bail;
3779 	}
3780
3781@@ -645,6 +626,7 @@ static int jffs2_garbage_collect_pristin
3782 			pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3783 				ref_offset(raw), je32_to_cpu(node->i.node_crc),
3784 				crc);
3785+			jffs2_dbg_dump_node(c, ref_offset(raw));
3786 			goto bail;
3787 		}
3788
3789@@ -654,6 +636,7 @@ static int jffs2_garbage_collect_pristin
3790 				pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3791 					ref_offset(raw),
3792 					je32_to_cpu(node->i.data_crc), crc);
3793+				jffs2_dbg_dump_node(c, ref_offset(raw));
3794 				goto bail;
3795 			}
3796 		}
3797@@ -665,12 +648,14 @@ static int jffs2_garbage_collect_pristin
3798 			pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3799 				ref_offset(raw),
3800 				je32_to_cpu(node->d.node_crc), crc);
3801+			jffs2_dbg_dump_node(c, ref_offset(raw));
3802 			goto bail;
3803 		}
3804
3805-		if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
3806+		if (strnlen((const char *)node->d.name, node->d.nsize) != node->d.nsize) {
3807 			pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
3808 				ref_offset(raw));
3809+			jffs2_dbg_dump_node(c, ref_offset(raw));
3810 			goto bail;
3811 		}
3812
3813@@ -680,6 +665,7 @@ static int jffs2_garbage_collect_pristin
3814 				pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3815 					ref_offset(raw),
3816 					je32_to_cpu(node->d.name_crc), crc);
3817+				jffs2_dbg_dump_node(c, ref_offset(raw));
3818 				goto bail;
3819 			}
3820 		}
3821@@ -689,6 +675,7 @@ static int jffs2_garbage_collect_pristin
3822 		if (ic) {
3823 			pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
3824 				ref_offset(raw), je16_to_cpu(node->u.nodetype));
3825+			jffs2_dbg_dump_node(c, ref_offset(raw));
3826 			goto bail;
3827 		}
3828 	}
3829@@ -697,7 +684,7 @@ static int jffs2_garbage_collect_pristin
3830  retry:
3831 	phys_ofs = write_ofs(c);
3832
3833-	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
3834+	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (const u_char *)node);
3835
3836 	if (ret || (retlen != rawlen)) {
3837 		pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
3838@@ -761,7 +748,7 @@ static int jffs2_garbage_collect_metadat
3839 	struct jffs2_full_dnode *new_fn;
3840 	struct jffs2_raw_inode ri;
3841 	struct jffs2_node_frag *last_frag;
3842-	union jffs2_device_node dev;
3843+	jint16_t dev;
3844 	char *mdata = NULL;
3845 	int mdatalen = 0;
3846 	uint32_t alloclen, ilen;
3847@@ -770,8 +757,9 @@ static int jffs2_garbage_collect_metadat
3848 	if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
3849 	    S_ISCHR(JFFS2_F_I_MODE(f)) ) {
3850 		/* For these, we don't actually need to read the old node */
3851-		mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
3852+		dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) | JFFS2_F_I_RDEV_MIN(f)));
3853 		mdata = (char *)&dev;
3854+		mdatalen = sizeof(dev);
3855 		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
3856 			  __func__, mdatalen);
3857 	} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
3858@@ -781,7 +769,7 @@ static int jffs2_garbage_collect_metadat
3859 			pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
3860 			return -ENOMEM;
3861 		}
3862-		ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
3863+		ret = jffs2_read_dnode(c, f, fn, (unsigned char *)mdata, 0, mdatalen);
3864 		if (ret) {
3865 			pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
3866 				ret);
3867@@ -831,7 +819,7 @@ static int jffs2_garbage_collect_metadat
3868 	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
3869 	ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
3870
3871-	new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
3872+	new_fn = jffs2_write_dnode(c, f, &ri, (unsigned char *)mdata, mdatalen, ALLOC_GC);
3873
3874 	if (IS_ERR(new_fn)) {
3875 		pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
3876@@ -857,7 +845,7 @@ static int jffs2_garbage_collect_dirent(
3877
3878 	rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
3879 	rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
3880-	rd.nsize = strlen(fd->name);
3881+	rd.nsize = strlen((const char *)fd->name);
3882 	rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
3883 	rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
3884
3885@@ -908,7 +896,7 @@ static int jffs2_garbage_collect_deletio
3886 		struct jffs2_raw_node_ref *raw;
3887 		int ret;
3888 		size_t retlen;
3889-		int name_len = strlen(fd->name);
3890+		int name_len = strlen((const char *)fd->name);
3891 		uint32_t name_crc = crc32(0, fd->name, name_len);
3892 		uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
3893
3894@@ -1053,6 +1041,7 @@ static int jffs2_garbage_collect_hole(st
3895 			pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
3896 				__func__, ref_offset(fn->raw),
3897 				je32_to_cpu(ri.node_crc), crc);
3898+				jffs2_dbg_dump_node(c, ref_offset(fn->raw));
3899 			/* FIXME: We could possibly deal with this by writing new holes for each frag */
3900 			pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
3901 				start, end, f->inocache->ino);
3902@@ -1165,13 +1154,12 @@ static int jffs2_garbage_collect_dnode(s
3903 				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
3904 				       uint32_t start, uint32_t end)
3905 {
3906-	struct inode *inode = OFNI_EDONI_2SFFJ(f);
3907 	struct jffs2_full_dnode *new_fn;
3908 	struct jffs2_raw_inode ri;
3909 	uint32_t alloclen, offset, orig_end, orig_start;
3910 	int ret = 0;
3911 	unsigned char *comprbuf = NULL, *writebuf;
3912-	struct page *page;
3913+	unsigned long pg;
3914 	unsigned char *pg_ptr;
3915
3916 	memset(&ri, 0, sizeof(ri));
3917@@ -1193,8 +1181,8 @@ static int jffs2_garbage_collect_dnode(s
3918 		struct jffs2_node_frag *frag;
3919 		uint32_t min, max;
3920
3921-		min = start & ~(PAGE_SIZE-1);
3922-		max = min + PAGE_SIZE;
3923+		min = start & ~(PAGE_CACHE_SIZE-1);
3924+		max = min + PAGE_CACHE_SIZE;
3925
3926 		frag = jffs2_lookup_node_frag(&f->fragtree, start);
3927
3928@@ -1203,7 +1191,7 @@ static int jffs2_garbage_collect_dnode(s
3929 		BUG_ON(frag->ofs != start);
3930
3931 		/* First grow down... */
3932-		while((frag = frag_prev(frag)) && frag->ofs >= min) {
3933+		while(frag && (frag = frag_prev(frag)) && frag->ofs >= min) {
3934
3935 			/* If the previous frag doesn't even reach the beginning, there's
3936 			   excessive fragmentation. Just merge. */
3937@@ -1259,7 +1247,7 @@ static int jffs2_garbage_collect_dnode(s
3938 		/* Find last frag which is actually part of the node we're to GC. */
3939 		frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
3940
3941-		while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
3942+		while(frag && (frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
3943
3944 			/* If the previous frag doesn't even reach the beginning, there's lots
3945 			   of fragmentation. Just merge. */
3946@@ -1317,27 +1305,21 @@ static int jffs2_garbage_collect_dnode(s
3947 		BUG_ON(start > orig_start);
3948 	}
3949
3950-	/* The rules state that we must obtain the page lock *before* f->sem, so
3951-	 * drop f->sem temporarily. Since we also hold c->alloc_sem, nothing's
3952-	 * actually going to *change* so we're safe; we only allow reading.
3953-	 *
3954-	 * It is important to note that jffs2_write_begin() will ensure that its
3955-	 * page is marked Uptodate before allocating space. That means that if we
3956-	 * end up here trying to GC the *same* page that jffs2_write_begin() is
3957-	 * trying to write out, read_cache_page() will not deadlock. */
3958-	mutex_unlock(&f->sem);
3959-	page = read_cache_page(inode->i_mapping, start >> PAGE_SHIFT,
3960-			       jffs2_do_readpage_unlock, inode);
3961-	if (IS_ERR(page)) {
3962+	/* First, use readpage() to read the appropriate page into the page cache */
3963+	/* Q: What happens if we actually try to GC the _same_ page for which commit_write()
3964+	 *    triggered garbage collection in the first place?
3965+	 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
3966+	 *    page OK. We'll actually write it out again in commit_write, which is a little
3967+	 *    suboptimal, but at least we're correct.
3968+	 */
3969+	pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
3970+
3971+	if (IS_ERR(pg_ptr)) {
3972 		pr_warn("read_cache_page() returned error: %ld\n",
3973-			PTR_ERR(page));
3974-		mutex_lock(&f->sem);
3975-		return PTR_ERR(page);
3976+			PTR_ERR(pg_ptr));
3977+		return PTR_ERR(pg_ptr);
3978 	}
3979
3980-	pg_ptr = kmap(page);
3981-	mutex_lock(&f->sem);
3982-
3983 	offset = start;
3984 	while(offset < orig_end) {
3985 		uint32_t datalen;
3986@@ -1355,7 +1337,7 @@ static int jffs2_garbage_collect_dnode(s
3987 		cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
3988 		datalen = end - offset;
3989
3990-		writebuf = pg_ptr + (offset & (PAGE_SIZE -1));
3991+		writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
3992
3993 		comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
3994
3995@@ -1400,7 +1382,6 @@ static int jffs2_garbage_collect_dnode(s
3996 		}
3997 	}
3998
3999-	kunmap(page);
4000-	put_page(page);
4001+	jffs2_gc_release_page(c, pg_ptr, &pg);
4002 	return ret;
4003 }
4004diff -Nupr old/fs/jffs2/ioctl.c new/fs/jffs2/ioctl.c
4005--- old/fs/jffs2/ioctl.c	2022-05-09 17:15:24.350000000 +0800
4006+++ new/fs/jffs2/ioctl.c	1970-01-01 08:00:00.000000000 +0800
4007@@ -1,22 +0,0 @@
4008-/*
4009- * JFFS2 -- Journalling Flash File System, Version 2.
4010- *
4011- * Copyright © 2001-2007 Red Hat, Inc.
4012- * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
4013- *
4014- * Created by David Woodhouse <dwmw2@infradead.org>
4015- *
4016- * For licensing information, see the file 'LICENCE' in this directory.
4017- *
4018- */
4019-
4020-#include <linux/fs.h>
4021-#include "nodelist.h"
4022-
4023-long jffs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4024-{
4025-	/* Later, this will provide for lsattr.jffs2 and chattr.jffs2, which
4026-	   will include compression support etc. */
4027-	return -ENOTTY;
4028-}
4029-
4030diff -Nupr old/fs/jffs2/jffs2_fs_i.h new/fs/jffs2/jffs2_fs_i.h
4031--- old/fs/jffs2/jffs2_fs_i.h	2022-05-09 17:22:53.000000000 +0800
4032+++ new/fs/jffs2/jffs2_fs_i.h	2022-05-09 20:50:05.810000000 +0800
4033@@ -14,8 +14,13 @@
4034 #define _JFFS2_FS_I
4035
4036 #include <linux/rbtree.h>
4037-#include <linux/posix_acl.h>
4038-#include <linux/mutex.h>
4039+#include <linux/kernel.h>
4040+
4041+#ifdef __cplusplus
4042+#if __cplusplus
4043+extern "C" {
4044+#endif /* __cplusplus */
4045+#endif /* __cplusplus */
4046
4047 struct jffs2_inode_info {
4048 	/* We need an internal mutex similar to inode->i_mutex.
4049@@ -24,7 +29,7 @@ struct jffs2_inode_info {
4050 	   before letting GC proceed. Or we'd have to put ugliness
4051 	   into the GC code so it didn't attempt to obtain the i_mutex
4052 	   for the inode(s) which are already locked */
4053-	struct mutex sem;
4054+	struct pthread_mutex sem;
4055
4056 	/* The highest (datanode) version number used for this ino */
4057 	uint32_t highest_version;
4058@@ -50,7 +55,29 @@ struct jffs2_inode_info {
4059
4060 	uint16_t flags;
4061 	uint8_t usercompr;
4062-	struct inode vfs_inode;
4063 };
4064
4065+struct super_block;
4066+
4067+struct jffs2_inode {
4068+	uint32_t i_ino;
4069+	mode_t i_mode;
4070+	nlink_t i_nlink;
4071+	uid_t i_uid;
4072+	gid_t i_gid;
4073+	time_t i_atime;
4074+	time_t i_mtime;
4075+	time_t i_ctime;
4076+	off_t i_size;
4077+	struct super_block *i_sb;
4078+	LOS_DL_LIST i_hashlist;
4079+	struct jffs2_inode_info jffs2_i;
4080+};
4081+
4082+#ifdef __cplusplus
4083+#if __cplusplus
4084+}
4085+#endif /* __cplusplus */
4086+#endif /* __cplusplus */
4087+
4088 #endif /* _JFFS2_FS_I */
4089diff -Nupr old/fs/jffs2/jffs2_fs_sb.h new/fs/jffs2/jffs2_fs_sb.h
4090--- old/fs/jffs2/jffs2_fs_sb.h	2022-05-09 17:22:53.000000000 +0800
4091+++ new/fs/jffs2/jffs2_fs_sb.h	2022-05-09 20:49:43.100000000 +0800
4092@@ -17,11 +17,18 @@
4093 #include <linux/spinlock.h>
4094 #include <linux/workqueue.h>
4095 #include <linux/completion.h>
4096-#include <linux/mutex.h>
4097 #include <linux/timer.h>
4098 #include <linux/wait.h>
4099 #include <linux/list.h>
4100 #include <linux/rwsem.h>
4101+#include "vfs_jffs2.h"
4102+#include "mtd_dev.h"
4103+
4104+#ifdef __cplusplus
4105+#if __cplusplus
4106+extern "C" {
4107+#endif /* __cplusplus */
4108+#endif /* __cplusplus */
4109
4110 #define JFFS2_SB_FLAG_RO 1
4111 #define JFFS2_SB_FLAG_SCANNING 2 /* Flash scanning is in progress */
4112@@ -47,10 +54,10 @@ struct jffs2_mount_opts {
4113    Nee jffs_control
4114 */
4115 struct jffs2_sb_info {
4116-	struct mtd_info *mtd;
4117+	struct MtdDev *mtd;
4118
4119 	uint32_t highest_ino;
4120-	uint32_t check_ino;		/* *NEXT* inode to be checked */
4121+	uint32_t checked_ino;
4122
4123 	unsigned int flags;
4124
4125@@ -58,7 +65,7 @@ struct jffs2_sb_info {
4126 	struct completion gc_thread_start; /* GC thread start completion */
4127 	struct completion gc_thread_exit; /* GC thread exit completion port */
4128
4129-	struct mutex alloc_sem;		/* Used to protect all the following
4130+	struct pthread_mutex alloc_sem;	/* Used to protect all the following
4131 					   fields, and also to protect against
4132 					   out-of-order writing of nodes. And GC. */
4133 	uint32_t cleanmarker_size;	/* Size of an _inline_ CLEANMARKER
4134@@ -120,7 +127,7 @@ struct jffs2_sb_info {
4135 	/* Sem to allow jffs2_garbage_collect_deletion_dirent to
4136 	   drop the erase_completion_lock while it's holding a pointer
4137 	   to an obsoleted node. I don't like this. Alternatives welcomed. */
4138-	struct mutex erase_free_sem;
4139+	struct pthread_mutex  erase_free_sem;
4140
4141 	uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */
4142
4143@@ -160,4 +167,27 @@ struct jffs2_sb_info {
4144 	void *os_priv;
4145 };
4146
4147+struct super_block {
4148+	struct jffs2_sb_info	jffs2_sb;
4149+	LIST_HEAD		s_node_hash[JFFS2_NODE_HASH_BUCKETS];
4150+	LosMux			s_node_hash_lock;
4151+	struct jffs2_inode 	*s_root;
4152+	void			*s_dev;
4153+
4154+	UINT32			s_lock;			/* Lock the inode cache */
4155+	EVENT_CB_S		s_gc_thread_flags;	/* Communication with the gcthread */
4156+	unsigned int		s_gc_thread;
4157+	unsigned long		s_mount_flags;
4158+};
4159+
4160+#define JFFS2_SB_INFO(sb) (&(sb)->jffs2_sb)
4161+#define OFNI_BS_2SFFJ(c)  \
4162+        ((struct super_block *) ( ((char *)c) - ((char *)(&((struct super_block *)NULL)->jffs2_sb)) ) )
4163+
4164+#ifdef __cplusplus
4165+#if __cplusplus
4166+}
4167+#endif /* __cplusplus */
4168+#endif /* __cplusplus */
4169+
4170 #endif /* _JFFS2_FS_SB */
4171diff -Nupr old/fs/jffs2/malloc.c new/fs/jffs2/malloc.c
4172--- old/fs/jffs2/malloc.c	2022-05-09 17:22:53.000000000 +0800
4173+++ new/fs/jffs2/malloc.c	2022-05-10 09:43:16.720000000 +0800
4174@@ -9,111 +9,31 @@
4175  *
4176  */
4177
4178-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4179-
4180 #include <linux/kernel.h>
4181 #include <linux/slab.h>
4182-#include <linux/init.h>
4183-#include <linux/jffs2.h>
4184+#include <stdlib.h>
4185 #include "nodelist.h"
4186
4187-/* These are initialised to NULL in the kernel startup code.
4188-   If you're porting to other operating systems, beware */
4189-static struct kmem_cache *full_dnode_slab;
4190-static struct kmem_cache *raw_dirent_slab;
4191-static struct kmem_cache *raw_inode_slab;
4192-static struct kmem_cache *tmp_dnode_info_slab;
4193-static struct kmem_cache *raw_node_ref_slab;
4194-static struct kmem_cache *node_frag_slab;
4195-static struct kmem_cache *inode_cache_slab;
4196-#ifdef CONFIG_JFFS2_FS_XATTR
4197-static struct kmem_cache *xattr_datum_cache;
4198-static struct kmem_cache *xattr_ref_cache;
4199+#if !defined(JFFS2NUM_FS_JFFS2_RAW_NODE_REF_CACHE_POOL_SIZE)
4200+# define JFFS2NUM_FS_JFFS2_RAW_NODE_REF_CACHE_POOL_SIZE 0
4201 #endif
4202
4203 int __init jffs2_create_slab_caches(void)
4204 {
4205-	full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
4206-					    sizeof(struct jffs2_full_dnode),
4207-					    0, 0, NULL);
4208-	if (!full_dnode_slab)
4209-		goto err;
4210-
4211-	raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
4212-					    sizeof(struct jffs2_raw_dirent),
4213-					    0, SLAB_HWCACHE_ALIGN, NULL);
4214-	if (!raw_dirent_slab)
4215-		goto err;
4216-
4217-	raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
4218-					   sizeof(struct jffs2_raw_inode),
4219-					   0, SLAB_HWCACHE_ALIGN, NULL);
4220-	if (!raw_inode_slab)
4221-		goto err;
4222-
4223-	tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
4224-						sizeof(struct jffs2_tmp_dnode_info),
4225-						0, 0, NULL);
4226-	if (!tmp_dnode_info_slab)
4227-		goto err;
4228-
4229-	raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
4230-					      sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
4231-					      0, 0, NULL);
4232-	if (!raw_node_ref_slab)
4233-		goto err;
4234-
4235-	node_frag_slab = kmem_cache_create("jffs2_node_frag",
4236-					   sizeof(struct jffs2_node_frag),
4237-					   0, 0, NULL);
4238-	if (!node_frag_slab)
4239-		goto err;
4240-
4241-	inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
4242-					     sizeof(struct jffs2_inode_cache),
4243-					     0, 0, NULL);
4244-	if (!inode_cache_slab)
4245-		goto err;
4246-
4247-#ifdef CONFIG_JFFS2_FS_XATTR
4248-	xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum",
4249-					     sizeof(struct jffs2_xattr_datum),
4250-					     0, 0, NULL);
4251-	if (!xattr_datum_cache)
4252-		goto err;
4253-
4254-	xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
4255-					   sizeof(struct jffs2_xattr_ref),
4256-					   0, 0, NULL);
4257-	if (!xattr_ref_cache)
4258-		goto err;
4259-#endif
4260-
4261 	return 0;
4262- err:
4263-	jffs2_destroy_slab_caches();
4264-	return -ENOMEM;
4265+
4266 }
4267
4268 void jffs2_destroy_slab_caches(void)
4269 {
4270-	kmem_cache_destroy(full_dnode_slab);
4271-	kmem_cache_destroy(raw_dirent_slab);
4272-	kmem_cache_destroy(raw_inode_slab);
4273-	kmem_cache_destroy(tmp_dnode_info_slab);
4274-	kmem_cache_destroy(raw_node_ref_slab);
4275-	kmem_cache_destroy(node_frag_slab);
4276-	kmem_cache_destroy(inode_cache_slab);
4277-#ifdef CONFIG_JFFS2_FS_XATTR
4278-	kmem_cache_destroy(xattr_datum_cache);
4279-	kmem_cache_destroy(xattr_ref_cache);
4280-#endif
4281+	return;
4282 }
4283
4284+
4285 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
4286 {
4287 	struct jffs2_full_dirent *ret;
4288-	ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
4289+	ret = zalloc(sizeof(struct jffs2_full_dirent) + namesize);
4290 	dbg_memalloc("%p\n", ret);
4291 	return ret;
4292 }
4293@@ -127,7 +47,7 @@ void jffs2_free_full_dirent(struct jffs2
4294 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
4295 {
4296 	struct jffs2_full_dnode *ret;
4297-	ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
4298+	ret = zalloc(sizeof(struct jffs2_full_dnode));
4299 	dbg_memalloc("%p\n", ret);
4300 	return ret;
4301 }
4302@@ -135,13 +55,13 @@ struct jffs2_full_dnode *jffs2_alloc_ful
4303 void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
4304 {
4305 	dbg_memalloc("%p\n", x);
4306-	kmem_cache_free(full_dnode_slab, x);
4307+	free(x);
4308 }
4309
4310 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
4311 {
4312 	struct jffs2_raw_dirent *ret;
4313-	ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
4314+	ret = zalloc(sizeof(struct jffs2_raw_dirent));
4315 	dbg_memalloc("%p\n", ret);
4316 	return ret;
4317 }
4318@@ -149,13 +69,13 @@ struct jffs2_raw_dirent *jffs2_alloc_raw
4319 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
4320 {
4321 	dbg_memalloc("%p\n", x);
4322-	kmem_cache_free(raw_dirent_slab, x);
4323+	free(x);
4324 }
4325
4326 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
4327 {
4328 	struct jffs2_raw_inode *ret;
4329-	ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
4330+	ret = zalloc(sizeof(struct jffs2_raw_inode));
4331 	dbg_memalloc("%p\n", ret);
4332 	return ret;
4333 }
4334@@ -163,13 +83,13 @@ struct jffs2_raw_inode *jffs2_alloc_raw_
4335 void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
4336 {
4337 	dbg_memalloc("%p\n", x);
4338-	kmem_cache_free(raw_inode_slab, x);
4339+	free(x);
4340 }
4341
4342 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
4343 {
4344 	struct jffs2_tmp_dnode_info *ret;
4345-	ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
4346+	ret = zalloc(sizeof(struct jffs2_tmp_dnode_info));
4347 	dbg_memalloc("%p\n",
4348 		ret);
4349 	return ret;
4350@@ -178,14 +98,14 @@ struct jffs2_tmp_dnode_info *jffs2_alloc
4351 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
4352 {
4353 	dbg_memalloc("%p\n", x);
4354-	kmem_cache_free(tmp_dnode_info_slab, x);
4355+	free(x);
4356 }
4357
4358 static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
4359 {
4360 	struct jffs2_raw_node_ref *ret;
4361
4362-	ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
4363+	ret = malloc(sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK+1));
4364 	if (ret) {
4365 		int i = 0;
4366 		for (i=0; i < REFS_PER_BLOCK; i++) {
4367@@ -242,13 +162,13 @@ int jffs2_prealloc_raw_node_refs(struct
4368 void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
4369 {
4370 	dbg_memalloc("%p\n", x);
4371-	kmem_cache_free(raw_node_ref_slab, x);
4372+	free(x);
4373 }
4374
4375 struct jffs2_node_frag *jffs2_alloc_node_frag(void)
4376 {
4377 	struct jffs2_node_frag *ret;
4378-	ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
4379+	ret = malloc(sizeof(struct jffs2_node_frag));
4380 	dbg_memalloc("%p\n", ret);
4381 	return ret;
4382 }
4383@@ -256,13 +176,14 @@ struct jffs2_node_frag *jffs2_alloc_node
4384 void jffs2_free_node_frag(struct jffs2_node_frag *x)
4385 {
4386 	dbg_memalloc("%p\n", x);
4387-	kmem_cache_free(node_frag_slab, x);
4388+	free(x);
4389 }
4390
4391+
4392 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
4393 {
4394 	struct jffs2_inode_cache *ret;
4395-	ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
4396+	ret = zalloc(sizeof(struct jffs2_inode_cache));;
4397 	dbg_memalloc("%p\n", ret);
4398 	return ret;
4399 }
4400@@ -270,14 +191,14 @@ struct jffs2_inode_cache *jffs2_alloc_in
4401 void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
4402 {
4403 	dbg_memalloc("%p\n", x);
4404-	kmem_cache_free(inode_cache_slab, x);
4405+	kfree(x);
4406 }
4407
4408 #ifdef CONFIG_JFFS2_FS_XATTR
4409 struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
4410 {
4411 	struct jffs2_xattr_datum *xd;
4412-	xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
4413+	xd = malloc(sizeof(struct jffs2_xattr_datum));
4414 	dbg_memalloc("%p\n", xd);
4415 	if (!xd)
4416 		return NULL;
4417@@ -291,13 +212,13 @@ struct jffs2_xattr_datum *jffs2_alloc_xa
4418 void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
4419 {
4420 	dbg_memalloc("%p\n", xd);
4421-	kmem_cache_free(xattr_datum_cache, xd);
4422+	kfree(xd);
4423 }
4424
4425 struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
4426 {
4427 	struct jffs2_xattr_ref *ref;
4428-	ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
4429+	ref = malloc(sizeof(struct jffs2_xattr_ref));
4430 	dbg_memalloc("%p\n", ref);
4431 	if (!ref)
4432 		return NULL;
4433@@ -310,6 +231,6 @@ struct jffs2_xattr_ref *jffs2_alloc_xatt
4434 void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
4435 {
4436 	dbg_memalloc("%p\n", ref);
4437-	kmem_cache_free(xattr_ref_cache, ref);
4438+	kfree(ref);
4439 }
4440 #endif
4441diff -Nupr old/fs/jffs2/nodelist.c new/fs/jffs2/nodelist.c
4442--- old/fs/jffs2/nodelist.c	2022-05-09 17:22:53.000000000 +0800
4443+++ new/fs/jffs2/nodelist.c	2022-05-09 20:37:35.680000000 +0800
4444@@ -9,16 +9,15 @@
4445  *
4446  */
4447
4448-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4449-
4450 #include <linux/kernel.h>
4451 #include <linux/sched.h>
4452 #include <linux/fs.h>
4453-#include <linux/mtd/mtd.h>
4454 #include <linux/rbtree.h>
4455-#include <linux/crc32.h>
4456 #include <linux/pagemap.h>
4457+#include <mtd_dev.h>
4458 #include "nodelist.h"
4459+#include "jffs2.h"
4460+#include "los_crc32.h"
4461
4462 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
4463 				     struct jffs2_node_frag *this);
4464@@ -30,7 +29,7 @@ void jffs2_add_fd_to_list(struct jffs2_s
4465 	dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
4466
4467 	while ((*prev) && (*prev)->nhash <= new->nhash) {
4468-		if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
4469+		if ((*prev)->nhash == new->nhash && !strcmp((const char *)((*prev)->name), (const char *)new->name)) {
4470 			/* Duplicate. Free one */
4471 			if (new->version < (*prev)->version) {
4472 				dbg_dentlist("Eep! Marking new dirent node obsolete, old is \"%s\", ino #%u\n",
4473@@ -41,7 +40,7 @@ void jffs2_add_fd_to_list(struct jffs2_s
4474 				dbg_dentlist("marking old dirent \"%s\", ino #%u obsolete\n",
4475 					(*prev)->name, (*prev)->ino);
4476 				new->next = (*prev)->next;
4477-				/* It may have been a 'placeholder' deletion dirent,
4478+				/* It may have been a 'placeholder' deletion dirent,
4479 				   if jffs2_can_mark_obsolete() (see jffs2_do_unlink()) */
4480 				if ((*prev)->raw)
4481 					jffs2_mark_node_obsolete(c, ((*prev)->raw));
4482@@ -65,13 +64,14 @@ uint32_t jffs2_truncate_fragtree(struct
4483 	/* We know frag->ofs <= size. That's what lookup does for us */
4484 	if (frag && frag->ofs != size) {
4485 		if (frag->ofs+frag->size > size) {
4486+		    dbg_fragtree("truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
4487 			frag->size = size - frag->ofs;
4488 		}
4489 		frag = frag_next(frag);
4490 	}
4491 	while (frag && frag->ofs >= size) {
4492 		struct jffs2_node_frag *next = frag_next(frag);
4493-
4494+        dbg_fragtree("removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
4495 		frag_erase(frag, list);
4496 		jffs2_obsolete_node_frag(c, frag);
4497 		frag = next;
4498@@ -90,7 +90,7 @@ uint32_t jffs2_truncate_fragtree(struct
4499
4500 	/* If the last fragment starts at the RAM page boundary, it is
4501 	 * REF_PRISTINE irrespective of its size. */
4502-	if (frag->node && (frag->ofs & (PAGE_SIZE - 1)) == 0) {
4503+	if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
4504 		dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
4505 			frag->ofs, frag->ofs + frag->size);
4506 		frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
4507@@ -237,7 +237,7 @@ static int jffs2_add_frag_to_fragtree(st
4508 		   If so, both 'this' and the new node get marked REF_NORMAL so
4509 		   the GC can take a look.
4510 		*/
4511-		if (lastend && (lastend-1) >> PAGE_SHIFT == newfrag->ofs >> PAGE_SHIFT) {
4512+		if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
4513 			if (this->node)
4514 				mark_ref_normal(this->node->raw);
4515 			mark_ref_normal(newfrag->node->raw);
4516@@ -382,7 +382,7 @@ int jffs2_add_full_dnode_to_inode(struct
4517
4518 	/* If we now share a page with other nodes, mark either previous
4519 	   or next node REF_NORMAL, as appropriate.  */
4520-	if (newfrag->ofs & (PAGE_SIZE-1)) {
4521+	if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
4522 		struct jffs2_node_frag *prev = frag_prev(newfrag);
4523
4524 		mark_ref_normal(fn->raw);
4525@@ -391,7 +391,7 @@ int jffs2_add_full_dnode_to_inode(struct
4526 			mark_ref_normal(prev->node->raw);
4527 	}
4528
4529-	if ((newfrag->ofs+newfrag->size) & (PAGE_SIZE-1)) {
4530+	if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
4531 		struct jffs2_node_frag *next = frag_next(newfrag);
4532
4533 		if (next) {
4534@@ -401,7 +401,7 @@ int jffs2_add_full_dnode_to_inode(struct
4535 		}
4536 	}
4537 	jffs2_dbg_fragtree_paranoia_check_nolock(f);
4538-
4539+	jffs2_dbg_dump_fragtree_nolock(f);
4540 	return 0;
4541 }
4542
4543@@ -409,7 +409,6 @@ void jffs2_set_inocache_state(struct jff
4544 {
4545 	spin_lock(&c->inocache_lock);
4546 	ic->state = state;
4547-	wake_up(&c->inocache_wq);
4548 	spin_unlock(&c->inocache_lock);
4549 }
4550
4551@@ -505,8 +504,12 @@ void jffs2_free_raw_node_refs(struct jff
4552 {
4553 	int i;
4554 	struct jffs2_raw_node_ref *this, *next;
4555+	struct super_block *sb = NULL;
4556+	struct MtdNorDev *device = NULL;
4557+	sb = OFNI_BS_2SFFJ(c);
4558+	device = (struct MtdNorDev*)(sb->s_dev);
4559
4560-	for (i=0; i<c->nr_blocks; i++) {
4561+	for (i=device->blockStart; i<c->nr_blocks+device->blockStart; i++) {
4562 		this = c->blocks[i].first_node;
4563 		while (this) {
4564 			if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
4565@@ -536,14 +539,22 @@ struct jffs2_node_frag *jffs2_lookup_nod
4566 	while(next) {
4567 		frag = rb_entry(next, struct jffs2_node_frag, rb);
4568
4569+		dbg_fragtree2("considering frag %#04x-%#04x (%p). left %p, right %p\n",
4570+			  frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right);
4571 		if (frag->ofs + frag->size <= offset) {
4572+			dbg_fragtree2("going right from frag %#04x-%#04x, before the region we care about\n",
4573+				  frag->ofs, frag->ofs+frag->size);
4574 			/* Remember the closest smaller match on the way down */
4575 			if (!prev || frag->ofs > prev->ofs)
4576 				prev = frag;
4577 			next = frag->rb.rb_right;
4578 		} else if (frag->ofs > offset) {
4579+			dbg_fragtree2("going left from frag %#04x-%#04x, after the region we care about\n",
4580+				  frag->ofs, frag->ofs+frag->size);
4581 			next = frag->rb.rb_left;
4582 		} else {
4583+			dbg_fragtree2("returning frag %#04x-%#04x, matched\n",
4584+				  frag->ofs, frag->ofs+frag->size);
4585 			return frag;
4586 		}
4587 	}
4588@@ -564,10 +575,12 @@ struct jffs2_node_frag *jffs2_lookup_nod
4589    they're killed. */
4590 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
4591 {
4592-	struct jffs2_node_frag *frag, *next;
4593+	struct jffs2_node_frag *frag;
4594+	struct rb_node *tn,*next;
4595
4596 	dbg_fragtree("killing\n");
4597-	rbtree_postorder_for_each_entry_safe(frag, next, root, rb) {
4598+	RB_POSTORDER_FOREACH_SAFE(tn, linux_root, (struct linux_root *)root, next) {
4599+		frag = (struct jffs2_node_frag *)tn;
4600 		if (frag->node && !(--frag->node->frags)) {
4601 			/* Not a hole, and it's the final remaining frag
4602 			   of this node. Free the node */
4603@@ -604,7 +617,7 @@ struct jffs2_raw_node_ref *jffs2_link_no
4604 			ref++;
4605 	}
4606
4607-	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
4608+	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
4609 		    ref->flash_offset, ofs, ref->next_in_ino, len);
4610
4611 	ref->flash_offset = ofs;
4612@@ -617,7 +630,7 @@ struct jffs2_raw_node_ref *jffs2_link_no
4613
4614 		JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
4615 			    ref, ref_offset(ref), ref_offset(ref)+len,
4616-			    ref_offset(jeb->last_node),
4617+			    ref_offset(jeb->last_node),
4618 			    ref_offset(jeb->last_node)+last_len);
4619 		BUG();
4620 	}
4621@@ -734,7 +747,7 @@ uint32_t __jffs2_ref_totlen(struct jffs2
4622 			pr_crit("next %p (0x%08x-0x%08x)\n",
4623 				ref_next(ref), ref_offset(ref_next(ref)),
4624 				ref_offset(ref_next(ref)) + ref->__totlen);
4625-		} else
4626+		} else
4627 			pr_crit("No next ref. jeb->last_node is %p\n",
4628 				jeb->last_node);
4629
4630diff -Nupr old/fs/jffs2/nodelist.h new/fs/jffs2/nodelist.h
4631--- old/fs/jffs2/nodelist.h	2022-05-09 17:22:53.000000000 +0800
4632+++ new/fs/jffs2/nodelist.h	2022-05-09 20:36:25.460000000 +0800
4633@@ -12,20 +12,28 @@
4634 #ifndef __JFFS2_NODELIST_H__
4635 #define __JFFS2_NODELIST_H__
4636
4637-#include <linux/fs.h>
4638+#include <linux/stat.h>
4639 #include <linux/types.h>
4640-#include <linux/jffs2.h>
4641+#include <linux/list.h>
4642+#include "jffs2.h"
4643 #include "jffs2_fs_sb.h"
4644 #include "jffs2_fs_i.h"
4645 #include "xattr.h"
4646 #include "acl.h"
4647 #include "summary.h"
4648-
4649-#ifdef __ECOS
4650-#include "os-ecos.h"
4651-#else
4652+#include "vfs_jffs2.h"
4653 #include "os-linux.h"
4654-#endif
4655+
4656+#ifdef __cplusplus
4657+#if __cplusplus
4658+extern "C" {
4659+#endif /* __cplusplus */
4660+#endif /* __cplusplus */
4661+
4662+struct kvec {
4663+	void *iov_base;
4664+	long iov_len;
4665+};
4666
4667 #define JFFS2_NATIVE_ENDIAN
4668
4669@@ -193,6 +201,8 @@ struct jffs2_inode_cache {
4670 #define INO_STATE_READING	5	/* In read_inode() */
4671 #define INO_STATE_CLEARING	6	/* In clear_inode() */
4672
4673+#define INOCACHE_HASHSIZE 128
4674+
4675 #define INO_FLAGS_XATTR_CHECKED	0x01	/* has no duplicate xattr_ref */
4676 #define INO_FLAGS_IS_DIR	0x02	/* is a directory */
4677
4678@@ -250,10 +260,7 @@ struct jffs2_readinode_info
4679
4680 struct jffs2_full_dirent
4681 {
4682-	union {
4683-		struct jffs2_raw_node_ref *raw;
4684-		struct jffs2_inode_cache *ic; /* Just during part of build */
4685-	};
4686+	struct jffs2_raw_node_ref *raw;
4687 	struct jffs2_full_dirent *next;
4688 	uint32_t version;
4689 	uint32_t ino; /* == zero for unlink */
4690@@ -313,34 +320,26 @@ static inline int jffs2_blocks_use_vmall
4691
4692 #define PAD(x) (((x)+3)&~3)
4693
4694-static inline int jffs2_encode_dev(union jffs2_device_node *jdev, dev_t rdev)
4695-{
4696-	if (old_valid_dev(rdev)) {
4697-		jdev->old_id = cpu_to_je16(old_encode_dev(rdev));
4698-		return sizeof(jdev->old_id);
4699-	} else {
4700-		jdev->new_id = cpu_to_je32(new_encode_dev(rdev));
4701-		return sizeof(jdev->new_id);
4702-	}
4703-}
4704
4705 static inline struct jffs2_node_frag *frag_first(struct rb_root *root)
4706 {
4707-	struct rb_node *node = rb_first(root);
4708+	struct rb_node *node = root->rb_node;
4709
4710 	if (!node)
4711 		return NULL;
4712-
4713+	while(node->rb_left)
4714+		node = node->rb_left;
4715 	return rb_entry(node, struct jffs2_node_frag, rb);
4716 }
4717
4718 static inline struct jffs2_node_frag *frag_last(struct rb_root *root)
4719 {
4720-	struct rb_node *node = rb_last(root);
4721+	struct rb_node *node = root->rb_node;
4722
4723 	if (!node)
4724 		return NULL;
4725-
4726+	while(node->rb_right)
4727+		node = node->rb_right;
4728 	return rb_entry(node, struct jffs2_node_frag, rb);
4729 }
4730
4731@@ -404,8 +403,9 @@ struct jffs2_full_dirent *jffs2_write_di
4732 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
4733 			    struct jffs2_raw_inode *ri, unsigned char *buf,
4734 			    uint32_t offset, uint32_t writelen, uint32_t *retlen);
4735-int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f,
4736-		    struct jffs2_raw_inode *ri, const struct qstr *qstr);
4737+int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
4738+		    struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
4739+		    const char *name, int namelen);
4740 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, const char *name,
4741 		    int namelen, struct jffs2_inode_info *dead_f, uint32_t time);
4742 int jffs2_do_link(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino,
4743@@ -481,4 +481,10 @@ int jffs2_write_nand_cleanmarker(struct
4744
4745 #include "debug.h"
4746
4747+#ifdef __cplusplus
4748+#if __cplusplus
4749+}
4750+#endif /* __cplusplus */
4751+#endif /* __cplusplus */
4752+
4753 #endif /* __JFFS2_NODELIST_H__ */
4754diff -Nupr old/fs/jffs2/nodemgmt.c new/fs/jffs2/nodemgmt.c
4755--- old/fs/jffs2/nodemgmt.c	2022-05-09 17:22:53.000000000 +0800
4756+++ new/fs/jffs2/nodemgmt.c	2022-05-09 20:35:50.910000000 +0800
4757@@ -9,46 +9,14 @@
4758  *
4759  */
4760
4761-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4762-
4763 #include <linux/kernel.h>
4764-#include <linux/mtd/mtd.h>
4765 #include <linux/compiler.h>
4766-#include <linux/sched/signal.h>
4767+#include <linux/sched.h> /* For cond_resched() */
4768+#include <linux/semaphore.h>
4769+#include <mtd_dev.h>
4770 #include "nodelist.h"
4771 #include "debug.h"
4772
4773-/*
4774- * Check whether the user is allowed to write.
4775- */
4776-static int jffs2_rp_can_write(struct jffs2_sb_info *c)
4777-{
4778-	uint32_t avail;
4779-	struct jffs2_mount_opts *opts = &c->mount_opts;
4780-
4781-	avail = c->dirty_size + c->free_size + c->unchecked_size +
4782-		c->erasing_size - c->resv_blocks_write * c->sector_size
4783-		- c->nospc_dirty_size;
4784-
4785-	if (avail < 2 * opts->rp_size)
4786-		jffs2_dbg(1, "rpsize %u, dirty_size %u, free_size %u, "
4787-			  "erasing_size %u, unchecked_size %u, "
4788-			  "nr_erasing_blocks %u, avail %u, resrv %u\n",
4789-			  opts->rp_size, c->dirty_size, c->free_size,
4790-			  c->erasing_size, c->unchecked_size,
4791-			  c->nr_erasing_blocks, avail, c->nospc_dirty_size);
4792-
4793-	if (avail > opts->rp_size)
4794-		return 1;
4795-
4796-	/* Always allow root */
4797-	if (capable(CAP_SYS_RESOURCE))
4798-		return 1;
4799-
4800-	jffs2_dbg(1, "forbid writing\n");
4801-	return 0;
4802-}
4803-
4804 /**
4805  *	jffs2_reserve_space - request physical space to write nodes to flash
4806  *	@c: superblock info
4807@@ -57,8 +25,8 @@ static int jffs2_rp_can_write(struct jff
4808  *	@prio: Allocation type - ALLOC_{NORMAL,DELETION}
4809  *
4810  *	Requests a block of physical space on the flash. Returns zero for success
4811- *	and puts 'len' into the appropriate place, or returns -ENOSPC or other
4812- *	error if appropriate. Doesn't return len since that's
4813+ *	and puts 'len' into the appropriate place, or returns -ENOSPC or other
4814+ *	error if appropriate. Doesn't return len since that's
4815  *
4816  *	If it returns zero, jffs2_reserve_space() also downs the per-filesystem
4817  *	allocation semaphore, to prevent more than one allocation from being
4818@@ -86,15 +54,6 @@ int jffs2_reserve_space(struct jffs2_sb_
4819
4820 	spin_lock(&c->erase_completion_lock);
4821
4822-	/*
4823-	 * Check if the free space is greater then size of the reserved pool.
4824-	 * If not, only allow root to proceed with writing.
4825-	 */
4826-	if (prio != ALLOC_DELETION && !jffs2_rp_can_write(c)) {
4827-		ret = -ENOSPC;
4828-		goto out;
4829-	}
4830-
4831 	/* this needs a little more thought (true <tglx> :)) */
4832 	while(ret == -EAGAIN) {
4833 		while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
4834@@ -165,24 +124,7 @@ int jffs2_reserve_space(struct jffs2_sb_
4835 			spin_unlock(&c->erase_completion_lock);
4836
4837 			ret = jffs2_garbage_collect_pass(c);
4838-
4839-			if (ret == -EAGAIN) {
4840-				spin_lock(&c->erase_completion_lock);
4841-				if (c->nr_erasing_blocks &&
4842-				    list_empty(&c->erase_pending_list) &&
4843-				    list_empty(&c->erase_complete_list)) {
4844-					DECLARE_WAITQUEUE(wait, current);
4845-					set_current_state(TASK_UNINTERRUPTIBLE);
4846-					add_wait_queue(&c->erase_wait, &wait);
4847-					jffs2_dbg(1, "%s waiting for erase to complete\n",
4848-						  __func__);
4849-					spin_unlock(&c->erase_completion_lock);
4850-
4851-					schedule();
4852-					remove_wait_queue(&c->erase_wait, &wait);
4853-				} else
4854-					spin_unlock(&c->erase_completion_lock);
4855-			} else if (ret)
4856+			if (ret)
4857 				return ret;
4858
4859 			cond_resched();
4860@@ -200,7 +142,6 @@ int jffs2_reserve_space(struct jffs2_sb_
4861 		}
4862 	}
4863
4864-out:
4865 	spin_unlock(&c->erase_completion_lock);
4866 	if (!ret)
4867 		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
4868@@ -509,7 +450,7 @@ struct jffs2_raw_node_ref *jffs2_add_phy
4869 	jffs2_dbg(1, "%s(): Node at 0x%x(%d), size 0x%x\n",
4870 		  __func__, ofs & ~3, ofs & 3, len);
4871 #if 1
4872-	/* Allow non-obsolete nodes only to be added at the end of c->nextblock,
4873+	/* Allow non-obsolete nodes only to be added at the end of c->nextblock,
4874 	   if c->nextblock is set. Note that wbuf.c will file obsolete nodes
4875 	   even after refiling c->nextblock */
4876 	if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
4877@@ -584,6 +525,8 @@ void jffs2_mark_node_obsolete(struct jff
4878 	int ret, addedsize;
4879 	size_t retlen;
4880 	uint32_t freed_len;
4881+	struct super_block *sb;
4882+	struct MtdNorDev *device;
4883
4884 	if(unlikely(!ref)) {
4885 		pr_notice("EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
4886@@ -595,9 +538,10 @@ void jffs2_mark_node_obsolete(struct jff
4887 		return;
4888 	}
4889 	blocknr = ref->flash_offset / c->sector_size;
4890-	if (blocknr >= c->nr_blocks) {
4891-		pr_notice("raw node at 0x%08x is off the end of device!\n",
4892-			  ref->flash_offset);
4893+	sb = OFNI_BS_2SFFJ(c);
4894+	device = (struct MtdNorDev*)(sb->s_dev);
4895+	if (blocknr >= c->nr_blocks +device->blockStart) {
4896+		pr_notice("raw node at 0x%08x is off the end of device!\n",ref->flash_offset);
4897 		BUG();
4898 	}
4899 	jeb = &c->blocks[blocknr];
4900@@ -778,7 +722,7 @@ void jffs2_mark_node_obsolete(struct jff
4901 	}
4902 	/* XXX FIXME: This is ugly now */
4903 	n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
4904-	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
4905+	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (const u_char *)&n);
4906 	if (ret) {
4907 		pr_warn("Write error in obliterating obsoleted node at 0x%08x: %d\n",
4908 			ref_offset(ref), ret);
4909@@ -846,8 +790,8 @@ int jffs2_thread_should_wake(struct jffs
4910 		return 1;
4911
4912 	if (c->unchecked_size) {
4913-		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, check_ino #%d\n",
4914-			  c->unchecked_size, c->check_ino);
4915+		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
4916+			  c->unchecked_size, c->checked_ino);
4917 		return 1;
4918 	}
4919
4920diff -Nupr old/fs/jffs2/os-linux.h new/fs/jffs2/os-linux.h
4921--- old/fs/jffs2/os-linux.h	2022-05-09 17:22:53.000000000 +0800
4922+++ new/fs/jffs2/os-linux.h	2022-05-09 20:33:21.200000000 +0800
4923@@ -12,59 +12,57 @@
4924 #ifndef __JFFS2_OS_LINUX_H__
4925 #define __JFFS2_OS_LINUX_H__
4926
4927+#include <dirent.h>
4928+#include "fs/fs.h"
4929+#include "jffs2.h"
4930+#include "jffs2_fs_sb.h"
4931+
4932+
4933+/* jffs2 debug output opion */
4934+#define CONFIG_JFFS2_FS_DEBUG       0  /* 1 or 2 */
4935+
4936+/* jffs2 gc thread section */
4937+#define JFFS2_GC_THREAD_PRIORITY  10 /* GC thread's priority */
4938+
4939+/* zlib section*/
4940+#define CONFIG_JFFS2_ZLIB
4941+#define CONFIG_JFFS2_RTIME
4942+#define CONFIG_JFFS2_RUBIN
4943+
4944 /* JFFS2 uses Linux mode bits natively -- no need for conversion */
4945 #define os_to_jffs2_mode(x) (x)
4946 #define jffs2_to_os_mode(x) (x)
4947
4948+#ifndef BUG_ON
4949+#define BUG_ON(x) do {if (unlikely(x)) BUG();} while (0)
4950+#endif
4951+
4952 struct kstatfs;
4953 struct kvec;
4954
4955-#define JFFS2_INODE_INFO(i) (container_of(i, struct jffs2_inode_info, vfs_inode))
4956-#define OFNI_EDONI_2SFFJ(f)  (&(f)->vfs_inode)
4957-#define JFFS2_SB_INFO(sb) (sb->s_fs_info)
4958-#define OFNI_BS_2SFFJ(c)  ((struct super_block *)c->os_priv)
4959
4960+#define JFFS2_INODE_INFO(i) (&(i)->jffs2_i)
4961+#define OFNI_EDONI_2SFFJ(f)  \
4962+        ((struct jffs2_inode *) (((char *)f) - ((char *)(&((struct jffs2_inode *)NULL)->jffs2_i))))
4963
4964 #define JFFS2_F_I_SIZE(f) (OFNI_EDONI_2SFFJ(f)->i_size)
4965 #define JFFS2_F_I_MODE(f) (OFNI_EDONI_2SFFJ(f)->i_mode)
4966-#define JFFS2_F_I_UID(f) (i_uid_read(OFNI_EDONI_2SFFJ(f)))
4967-#define JFFS2_F_I_GID(f) (i_gid_read(OFNI_EDONI_2SFFJ(f)))
4968-#define JFFS2_F_I_RDEV(f) (OFNI_EDONI_2SFFJ(f)->i_rdev)
4969-
4970-#define JFFS2_CLAMP_TIME(t) ((uint32_t)clamp_t(time64_t, (t), 0, U32_MAX))
4971-#define ITIME(sec) ((struct timespec64){sec, 0})
4972-#define JFFS2_NOW() JFFS2_CLAMP_TIME(ktime_get_real_seconds())
4973-#define I_SEC(tv) JFFS2_CLAMP_TIME((tv).tv_sec)
4974-#define JFFS2_F_I_CTIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_ctime)
4975-#define JFFS2_F_I_MTIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_mtime)
4976-#define JFFS2_F_I_ATIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_atime)
4977-#define sleep_on_spinunlock(wq, s)				\
4978-	do {							\
4979-		DECLARE_WAITQUEUE(__wait, current);		\
4980-		add_wait_queue((wq), &__wait);			\
4981-		set_current_state(TASK_UNINTERRUPTIBLE);	\
4982-		spin_unlock(s);					\
4983-		schedule();					\
4984-		remove_wait_queue((wq), &__wait);		\
4985-	} while(0)
4986-
4987-static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
4988-{
4989-	f->highest_version = 0;
4990-	f->fragtree = RB_ROOT;
4991-	f->metadata = NULL;
4992-	f->dents = NULL;
4993-	f->target = NULL;
4994-	f->flags = 0;
4995-	f->usercompr = 0;
4996-}
4997+#define JFFS2_F_I_UID(f) (OFNI_EDONI_2SFFJ(f)->i_uid)
4998+#define JFFS2_F_I_GID(f) (OFNI_EDONI_2SFFJ(f)->i_gid)
4999+#define JFFS2_F_I_CTIME(f) (OFNI_EDONI_2SFFJ(f)->i_ctime)
5000+#define JFFS2_F_I_MTIME(f) (OFNI_EDONI_2SFFJ(f)->i_mtime)
5001+#define JFFS2_F_I_ATIME(f) (OFNI_EDONI_2SFFJ(f)->i_atime)
5002+
5003+#define ITIME(sec) ((struct timespec){sec, 0})
5004+#define I_SEC(tv) ((tv).tv_sec)
5005
5006+#define sleep_on_spinunlock(wq, sl) do {spin_unlock(sl); msleep(100);} while (0)
5007
5008-#define jffs2_is_readonly(c) (OFNI_BS_2SFFJ(c)->s_flags & SB_RDONLY)
5009+#define jffs2_is_readonly(c) (0)
5010
5011 #define SECTOR_ADDR(x) ( (((unsigned long)(x) / c->sector_size) * c->sector_size) )
5012-#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
5013
5014+#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
5015
5016 #ifdef CONFIG_JFFS2_SUMMARY
5017 #define jffs2_can_mark_obsolete(c) (0)
5018@@ -77,10 +75,10 @@ static inline void jffs2_init_inode_info
5019 #define jffs2_write_nand_cleanmarker(c,jeb) (-EIO)
5020
5021 #define jffs2_flash_write(c, ofs, len, retlen, buf) jffs2_flash_direct_write(c, ofs, len, retlen, buf)
5022-#define jffs2_flash_read(c, ofs, len, retlen, buf) (mtd_read((c)->mtd, ofs, len, retlen, buf))
5023-#define jffs2_flush_wbuf_pad(c) ({ do{} while(0); (void)(c), 0; })
5024+#define jffs2_flash_read(c, ofs, len, retlen, buf) jffs2_flash_direct_read(c, ofs, len, retlen, buf)
5025+#define jffs2_flush_wbuf_pad(c) (c=c)
5026 #define jffs2_flush_wbuf_gc(c, i) ({ do{} while(0); (void)(c), (void) i, 0; })
5027-#define jffs2_write_nand_badblock(c,jeb,bad_offset) (1)
5028+#define jffs2_write_nand_badblock(c,jeb,p) (0)
5029 #define jffs2_nand_flash_setup(c) (0)
5030 #define jffs2_nand_flash_cleanup(c) do {} while(0)
5031 #define jffs2_wbuf_dirty(c) (0)
5032@@ -100,7 +98,8 @@ static inline void jffs2_init_inode_info
5033
5034 #else /* NAND and/or ECC'd NOR support present */
5035
5036-#define jffs2_is_writebuffered(c) (c->wbuf != NULL)
5037+/* current not support */
5038+#define jffs2_is_writebuffered(c) (0)
5039
5040 #ifdef CONFIG_JFFS2_SUMMARY
5041 #define jffs2_can_mark_obsolete(c) (0)
5042@@ -142,38 +141,28 @@ void jffs2_dirty_trigger(struct jffs2_sb
5043 #endif /* WRITEBUFFER */
5044
5045 /* background.c */
5046-int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
5047+void jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
5048 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
5049 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
5050
5051 /* dir.c */
5052-extern const struct file_operations jffs2_dir_operations;
5053-extern const struct inode_operations jffs2_dir_inode_operations;
5054-
5055-/* file.c */
5056-extern const struct file_operations jffs2_file_operations;
5057-extern const struct inode_operations jffs2_file_inode_operations;
5058-extern const struct address_space_operations jffs2_file_address_operations;
5059-int jffs2_fsync(struct file *, loff_t, loff_t, int);
5060-int jffs2_do_readpage_unlock(void *data, struct page *pg);
5061-
5062-/* ioctl.c */
5063-long jffs2_ioctl(struct file *, unsigned int, unsigned long);
5064-
5065-/* symlink.c */
5066-extern const struct inode_operations jffs2_symlink_inode_operations;
5067+struct jffs2_inode *jffs2_lookup(struct jffs2_inode *dir_i, const unsigned char *name, int namelen);
5068+int jffs2_create(struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i);
5069+int jffs2_mkdir (struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i);
5070+int jffs2_link (struct jffs2_inode *old_d_inode, struct jffs2_inode *dir_i, const unsigned char *d_name);
5071+int jffs2_symlink(struct jffs2_inode *dir_i, struct jffs2_inode **d_inode, const unsigned char *d_name, const char *target);
5072+int jffs2_unlink(struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name);
5073+int jffs2_rmdir (struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name);
5074+int jffs2_rename (struct jffs2_inode *old_dir_i, struct jffs2_inode *d_inode, const unsigned char *old_d_name,
5075+		  struct jffs2_inode *new_dir_i, const unsigned char *new_d_name);
5076+int jffs2_readdir(struct jffs2_inode *inode, off_t *offset, off_t *int_off, struct dirent *ent);
5077
5078 /* fs.c */
5079-int jffs2_setattr (struct dentry *, struct iattr *);
5080-int jffs2_do_setattr (struct inode *, struct iattr *);
5081-struct inode *jffs2_iget(struct super_block *, unsigned long);
5082-void jffs2_evict_inode (struct inode *);
5083-void jffs2_dirty_inode(struct inode *inode, int flags);
5084-struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode,
5085-			       struct jffs2_raw_inode *ri);
5086-int jffs2_statfs (struct dentry *, struct kstatfs *);
5087-int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc);
5088-int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc);
5089+int jffs2_setattr (struct jffs2_inode *inode, struct IATTR *attr);
5090+struct jffs2_inode *jffs2_iget(struct super_block *sb, uint32_t ino);
5091+int jffs2_iput(struct jffs2_inode * i);
5092+struct jffs2_inode *jffs2_new_inode (struct jffs2_inode *dir_i, int mode, struct jffs2_raw_inode *ri);
5093+
5094 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
5095 			    struct jffs2_inode_info *f);
5096 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
5097@@ -183,15 +172,25 @@ unsigned char *jffs2_gc_fetch_page(struc
5098 				   struct jffs2_inode_info *f,
5099 				   unsigned long offset,
5100 				   unsigned long *priv);
5101+void jffs2_gc_release_page(struct jffs2_sb_info *c,
5102+			   unsigned char *pg,
5103+			   unsigned long *priv);
5104 void jffs2_flash_cleanup(struct jffs2_sb_info *c);
5105
5106+int calculate_inocache_hashsize(uint32_t flash_size);
5107
5108 /* writev.c */
5109 int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,
5110 		       unsigned long count, loff_t to, size_t *retlen);
5111 int jffs2_flash_direct_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5112 			size_t *retlen, const u_char *buf);
5113+int jffs2_flash_direct_read(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5114+			size_t *retlen, const char *buf);
5115
5116-#endif /* __JFFS2_OS_LINUX_H__ */
5117+/* super.c */
5118+int jffs2_fill_super(struct super_block *sb);
5119+int jffs2_mount(int part_no, struct jffs2_inode **root_node, unsigned long mountflags);
5120+int jffs2_umount(struct jffs2_inode *root_node);
5121
5122+#endif /* __JFFS2_OS_LINUX_H__ */
5123
5124diff -Nupr old/fs/jffs2/read.c new/fs/jffs2/read.c
5125--- old/fs/jffs2/read.c	2022-05-09 17:22:53.000000000 +0800
5126+++ new/fs/jffs2/read.c	2022-05-09 20:27:15.580000000 +0800
5127@@ -9,16 +9,15 @@
5128  *
5129  */
5130
5131-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5132-
5133 #include <linux/kernel.h>
5134 #include <linux/slab.h>
5135-#include <linux/crc32.h>
5136 #include <linux/pagemap.h>
5137-#include <linux/mtd/mtd.h>
5138 #include <linux/compiler.h>
5139+#include <mtd_dev.h>
5140 #include "nodelist.h"
5141 #include "compr.h"
5142+#include "los_crc32.h"
5143+#include "user_copy.h"
5144
5145 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
5146 		     struct jffs2_full_dnode *fd, unsigned char *buf,
5147@@ -57,6 +56,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5148 	if (crc != je32_to_cpu(ri->node_crc)) {
5149 		pr_warn("Node CRC %08x != calculated CRC %08x for node at %08x\n",
5150 			je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
5151+			jffs2_dbg_dump_node(c, ref_offset(fd->raw));
5152 		ret = -EIO;
5153 		goto out_ri;
5154 	}
5155@@ -75,9 +75,8 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5156 		goto out_ri;
5157 	});
5158
5159-
5160 	if (ri->compr == JFFS2_COMPR_ZERO) {
5161-		memset(buf, 0, len);
5162+		ret = LOS_UserMemClear(buf, len);
5163 		goto out_ri;
5164 	}
5165
5166@@ -88,7 +87,11 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5167 	   Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
5168 	*/
5169 	if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
5170-		readbuf = buf;
5171+		readbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5172+		if (!readbuf) {
5173+			ret = -ENOMEM;
5174+			goto out_ri;
5175+		}
5176 	} else {
5177 		readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
5178 		if (!readbuf) {
5179@@ -97,14 +100,10 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5180 		}
5181 	}
5182 	if (ri->compr != JFFS2_COMPR_NONE) {
5183-		if (len < je32_to_cpu(ri->dsize)) {
5184-			decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5185-			if (!decomprbuf) {
5186-				ret = -ENOMEM;
5187-				goto out_readbuf;
5188-			}
5189-		} else {
5190-			decomprbuf = buf;
5191+		decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5192+		if (!decomprbuf) {
5193+			ret = -ENOMEM;
5194+			goto out_readbuf;
5195 		}
5196 	} else {
5197 		decomprbuf = readbuf;
5198@@ -113,7 +112,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5199 	jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
5200 		  readbuf);
5201 	ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
5202-			       je32_to_cpu(ri->csize), &readlen, readbuf);
5203+			       je32_to_cpu(ri->csize), &readlen, (char *)readbuf);
5204
5205 	if (!ret && readlen != je32_to_cpu(ri->csize))
5206 		ret = -EIO;
5207@@ -124,6 +123,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5208 	if (crc != je32_to_cpu(ri->data_crc)) {
5209 		pr_warn("Data CRC %08x != calculated CRC %08x for node at %08x\n",
5210 			je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
5211+		jffs2_dbg_dump_node(c, ref_offset(fd->raw));
5212 		ret = -EIO;
5213 		goto out_decomprbuf;
5214 	}
5215@@ -139,8 +139,8 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5216 		}
5217 	}
5218
5219-	if (len < je32_to_cpu(ri->dsize)) {
5220-		memcpy(buf, decomprbuf+ofs, len);
5221+	if (LOS_CopyFromKernel(buf, len, decomprbuf + ofs, len) != 0) {
5222+		ret = -EFAULT;
5223 	}
5224  out_decomprbuf:
5225 	if(decomprbuf != buf && decomprbuf != readbuf)
5226@@ -184,7 +184,10 @@ int jffs2_read_inode_range(struct jffs2_
5227 			}
5228 			jffs2_dbg(1, "Filling non-frag hole from %d-%d\n",
5229 				  offset, offset + holesize);
5230-			memset(buf, 0, holesize);
5231+			ret = LOS_UserMemClear(buf, holesize);
5232+			if (ret != 0) {
5233+				return ret;
5234+			}
5235 			buf += holesize;
5236 			offset += holesize;
5237 			continue;
5238@@ -193,7 +196,10 @@ int jffs2_read_inode_range(struct jffs2_
5239 			jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n",
5240 				  offset, holeend, frag->ofs,
5241 				  frag->ofs + frag->size);
5242-			memset(buf, 0, holeend - offset);
5243+			ret = LOS_UserMemClear(buf, holeend - offset);
5244+			if (ret != 0) {
5245+				return ret;
5246+			}
5247 			buf += holeend - offset;
5248 			offset = holeend;
5249 			frag = frag_next(frag);
5250@@ -214,7 +220,7 @@ int jffs2_read_inode_range(struct jffs2_
5251 			if (ret) {
5252 				jffs2_dbg(1, "%s(): error %d\n",
5253 					  __func__, ret);
5254-				memset(buf, 0, readlen);
5255+				(void)LOS_UserMemClear(buf, readlen);
5256 				return ret;
5257 			}
5258 			buf += readlen;
5259@@ -226,3 +232,15 @@ int jffs2_read_inode_range(struct jffs2_
5260 	return 0;
5261 }
5262
5263+int jffs2_flash_direct_read(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5264+			size_t *retlen, const char *buf)
5265+{
5266+	int ret;
5267+	ret = c->mtd->read(c->mtd, ofs, len, (char *)buf);
5268+	if (ret >= 0) {
5269+		*retlen = ret;
5270+		return 0;
5271+	}
5272+	*retlen = 0;
5273+	return ret;
5274+}
5275\ No newline at end of file
5276diff -Nupr old/fs/jffs2/readinode.c new/fs/jffs2/readinode.c
5277--- old/fs/jffs2/readinode.c	2022-05-09 17:22:53.000000000 +0800
5278+++ new/fs/jffs2/readinode.c	2022-05-09 20:26:31.030000000 +0800
5279@@ -9,17 +9,18 @@
5280  *
5281  */
5282
5283-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5284-
5285 #include <linux/kernel.h>
5286 #include <linux/sched.h>
5287 #include <linux/slab.h>
5288 #include <linux/fs.h>
5289-#include <linux/crc32.h>
5290+#include <linux/delay.h>
5291+#include <linux/semaphore.h>
5292 #include <linux/pagemap.h>
5293-#include <linux/mtd/mtd.h>
5294 #include <linux/compiler.h>
5295+#include <mtd_dev.h>
5296 #include "nodelist.h"
5297+#include "os-linux.h"
5298+#include "los_crc32.h"
5299
5300 /*
5301  * Check the data CRC of the node.
5302@@ -31,9 +32,9 @@
5303 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
5304 {
5305 	struct jffs2_raw_node_ref *ref = tn->fn->raw;
5306-	int err = 0, pointed = 0;
5307+	int err = 0;
5308 	struct jffs2_eraseblock *jeb;
5309-	unsigned char *buffer;
5310+	unsigned char *buffer = NULL;
5311 	uint32_t crc, ofs, len;
5312 	size_t retlen;
5313
5314@@ -61,48 +62,28 @@ static int check_node_data(struct jffs2_
5315 	dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
5316 		ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
5317
5318-#ifndef __ECOS
5319-	/* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
5320-	 * adding and jffs2_flash_read_end() interface. */
5321-	err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
5322-	if (!err && retlen < len) {
5323-		JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
5324-		mtd_unpoint(c->mtd, ofs, retlen);
5325-	} else if (err) {
5326-		if (err != -EOPNOTSUPP)
5327-			JFFS2_WARNING("MTD point failed: error code %d.\n", err);
5328-	} else
5329-		pointed = 1; /* succefully pointed to device */
5330-#endif
5331-
5332-	if (!pointed) {
5333-		buffer = kmalloc(len, GFP_KERNEL);
5334-		if (unlikely(!buffer))
5335-			return -ENOMEM;
5336+	buffer = kmalloc(len, GFP_KERNEL);
5337+	if (unlikely(!buffer))
5338+		return -ENOMEM;
5339
5340-		/* TODO: this is very frequent pattern, make it a separate
5341-		 * routine */
5342-		err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
5343-		if (err) {
5344-			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
5345-			goto free_out;
5346-		}
5347+	/* TODO: this is very frequent pattern, make it a separate
5348+		* routine */
5349+	err = jffs2_flash_read(c, ofs, len, &retlen, (char *)buffer);
5350+	if (err) {
5351+		JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
5352+		goto free_out;
5353+	}
5354
5355-		if (retlen != len) {
5356-			JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
5357-			err = -EIO;
5358-			goto free_out;
5359-		}
5360+	if (retlen != len) {
5361+		JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
5362+		err = -EIO;
5363+		goto free_out;
5364 	}
5365
5366 	/* Continue calculating CRC */
5367 	crc = crc32(tn->partial_crc, buffer, len);
5368-	if(!pointed)
5369-		kfree(buffer);
5370-#ifndef __ECOS
5371-	else
5372-		mtd_unpoint(c->mtd, ofs, len);
5373-#endif
5374+
5375+	kfree(buffer);
5376
5377 	if (crc != tn->data_crc) {
5378 		JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
5379@@ -133,12 +114,7 @@ adj_acc:
5380 	return 0;
5381
5382 free_out:
5383-	if(!pointed)
5384-		kfree(buffer);
5385-#ifndef __ECOS
5386-	else
5387-		mtd_unpoint(c->mtd, ofs, len);
5388-#endif
5389+	kfree(buffer);
5390 	return err;
5391 }
5392
5393@@ -415,8 +391,12 @@ static void eat_last(struct rb_root *roo
5394 		link = &parent->rb_right;
5395
5396 	*link = node->rb_left;
5397-	if (node->rb_left)
5398-		node->rb_left->__rb_parent_color = node->__rb_parent_color;
5399+	if (node->rb_left) {
5400+		node->rb_left->rb_parent_color = node->rb_parent_color;
5401+		// set child parent only
5402+		rb_parent(node->rb_left) = parent;
5403+		node->rb_left = NULL;
5404+	}
5405 }
5406
5407 /* We put the version tree in reverse order, so we can use the same eat_last()
5408@@ -464,8 +444,8 @@ static int jffs2_build_inode_fragtree(st
5409 #ifdef JFFS2_DBG_READINODE_MESSAGES
5410 	this = tn_last(&rii->tn_root);
5411 	while (this) {
5412-		dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
5413-			      this->fn->ofs+this->fn->size, this->overlapped);
5414+		dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d,left %p,right %p ,parent %p\n", this, this->version, this->fn->ofs,
5415+			      this->fn->ofs+this->fn->size, this->overlapped,this->rb.rb_left,this->rb.rb_right,rb_parent(&(this->rb)));
5416 		this = tn_prev(this);
5417 	}
5418 #endif
5419@@ -543,11 +523,13 @@ static int jffs2_build_inode_fragtree(st
5420
5421 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
5422 {
5423-	struct jffs2_tmp_dnode_info *tn, *next;
5424+	struct jffs2_tmp_dnode_info *tn;
5425+	struct rb_node *rbn,*next;
5426
5427-	rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
5428-			jffs2_free_full_dnode(tn->fn);
5429-			jffs2_free_tmp_dnode_info(tn);
5430+	RB_POSTORDER_FOREACH_SAFE(rbn, linux_root, (struct linux_root *)list, next) {
5431+		tn = (struct jffs2_tmp_dnode_info *)rbn;
5432+		jffs2_free_full_dnode(tn->fn);
5433+		jffs2_free_tmp_dnode_info(tn);
5434 	}
5435
5436 	*list = RB_ROOT;
5437@@ -659,7 +641,7 @@ static inline int read_direntry(struct j
5438 		int already = read - sizeof(*rd);
5439
5440 		err = jffs2_flash_read(c, (ref_offset(ref)) + read,
5441-				rd->nsize - already, &read, &fd->name[already]);
5442+				rd->nsize - already, &read, (char *)&fd->name[already]);
5443 		if (unlikely(read != rd->nsize - already) && likely(!err)) {
5444 			jffs2_free_full_dirent(fd);
5445 			JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
5446@@ -690,7 +672,7 @@ static inline int read_direntry(struct j
5447 #endif
5448 	}
5449
5450-	fd->nhash = full_name_hash(NULL, fd->name, rd->nsize);
5451+	fd->nhash = full_name_hash(fd->name, rd->nsize);
5452 	fd->next = NULL;
5453 	fd->name[rd->nsize] = '\0';
5454
5455@@ -956,7 +938,7 @@ static int read_more(struct jffs2_sb_inf
5456
5457 	dbg_readinode("read more %d bytes\n", to_read);
5458
5459-	err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
5460+	err = jffs2_flash_read(c, offs, to_read, &retlen, (char *)(buf + *rdlen));
5461 	if (err) {
5462 		JFFS2_ERROR("can not read %d bytes from 0x%08x, "
5463 			"error code: %d.\n", to_read, offs, err);
5464@@ -1042,7 +1024,7 @@ static int jffs2_get_inode_nodes(struct
5465 		dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
5466
5467 		/* FIXME: point() */
5468-		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
5469+		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, (char *)buf);
5470 		if (err) {
5471 			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
5472 			goto free_out;
5473@@ -1079,6 +1061,7 @@ static int jffs2_get_inode_nodes(struct
5474
5475 		case JFFS2_NODETYPE_DIRENT:
5476
5477+			dbg_readinode("node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref));
5478 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
5479 			    len < sizeof(struct jffs2_raw_dirent)) {
5480 				err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
5481@@ -1094,6 +1077,7 @@ static int jffs2_get_inode_nodes(struct
5482
5483 		case JFFS2_NODETYPE_INODE:
5484
5485+			dbg_readinode("node at %08x (%d) is a data node\n", ref_offset(ref), ref_flags(ref));
5486 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
5487 			    len < sizeof(struct jffs2_raw_inode)) {
5488 				err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
5489@@ -1289,7 +1273,7 @@ static int jffs2_do_read_inode_internal(
5490 			dbg_readinode("symlink's target '%s' cached\n", f->target);
5491 		}
5492
5493-		fallthrough;
5494+		/* fall through... */
5495
5496 	case S_IFBLK:
5497 	case S_IFCHR:
5498@@ -1315,7 +1299,7 @@ static int jffs2_do_read_inode_internal(
5499 		/* OK. We're happy */
5500 		f->metadata = frag_first(&f->fragtree)->node;
5501 		jffs2_free_node_frag(frag_first(&f->fragtree));
5502-		f->fragtree = RB_ROOT;
5503+		f->fragtree.rb_node = NULL;
5504 		break;
5505 	}
5506 	if (f->inocache->state == INO_STATE_READING)
5507@@ -1362,6 +1346,7 @@ int jffs2_do_read_inode(struct jffs2_sb_
5508 			break;
5509
5510 		default:
5511+		    JFFS2_ERROR("Unknown f->inocache->state %d!\n", f->inocache->state);
5512 			BUG();
5513 		}
5514 	}
5515@@ -1375,14 +1360,13 @@ int jffs2_do_read_inode(struct jffs2_sb_
5516 			return -ENOMEM;
5517 		}
5518 		dbg_readinode("creating inocache for root inode\n");
5519-		memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
5520 		f->inocache->ino = f->inocache->pino_nlink = 1;
5521 		f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
5522 		f->inocache->state = INO_STATE_READING;
5523 		jffs2_add_ino_cache(c, f->inocache);
5524 	}
5525 	if (!f->inocache) {
5526-		JFFS2_ERROR("requested to read a nonexistent ino %u\n", ino);
5527+		JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
5528 		return -ENOENT;
5529 	}
5530
5531@@ -1430,6 +1414,11 @@ void jffs2_do_clear_inode(struct jffs2_s
5532
5533 	jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
5534
5535+	if (f->target) {
5536+		kfree(f->target);
5537+		f->target = NULL;
5538+	}
5539+
5540 	fds = f->dents;
5541 	while(fds) {
5542 		fd = fds;
5543diff -Nupr old/fs/jffs2/scan.c new/fs/jffs2/scan.c
5544--- old/fs/jffs2/scan.c	2022-05-09 17:22:53.000000000 +0800
5545+++ new/fs/jffs2/scan.c	2022-05-09 20:23:02.230000000 +0800
5546@@ -9,18 +9,17 @@
5547  *
5548  */
5549
5550-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5551-
5552 #include <linux/kernel.h>
5553 #include <linux/sched.h>
5554 #include <linux/slab.h>
5555-#include <linux/mtd/mtd.h>
5556 #include <linux/pagemap.h>
5557-#include <linux/crc32.h>
5558 #include <linux/compiler.h>
5559 #include "nodelist.h"
5560 #include "summary.h"
5561 #include "debug.h"
5562+#include "mtd_dev.h"
5563+#include "los_typedef.h"
5564+#include "los_crc32.h"
5565
5566 #define DEFAULT_EMPTY_SCAN_SIZE 256
5567
5568@@ -74,7 +73,7 @@ static int file_dirty(struct jffs2_sb_in
5569 		return ret;
5570 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
5571 		return ret;
5572-	/* Turned wasted size into dirty, since we apparently
5573+	/* Turned wasted size into dirty, since we apparently
5574 	   think it's recoverable now. */
5575 	jeb->dirty_size += jeb->wasted_size;
5576 	c->dirty_size += jeb->wasted_size;
5577@@ -95,40 +94,26 @@ int jffs2_scan_medium(struct jffs2_sb_in
5578 	unsigned char *flashbuf = NULL;
5579 	uint32_t buf_size = 0;
5580 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
5581-#ifndef __ECOS
5582-	size_t pointlen, try_size;
5583+	struct super_block *sb = NULL;
5584+	struct MtdNorDev *device = NULL;
5585
5586-	ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
5587-			(void **)&flashbuf, NULL);
5588-	if (!ret && pointlen < c->mtd->size) {
5589-		/* Don't muck about if it won't let us point to the whole flash */
5590-		jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
5591-			  pointlen);
5592-		mtd_unpoint(c->mtd, 0, pointlen);
5593-		flashbuf = NULL;
5594-	}
5595-	if (ret && ret != -EOPNOTSUPP)
5596-		jffs2_dbg(1, "MTD point failed %d\n", ret);
5597-#endif
5598 	if (!flashbuf) {
5599 		/* For NAND it's quicker to read a whole eraseblock at a time,
5600 		   apparently */
5601 		if (jffs2_cleanmarker_oob(c))
5602-			try_size = c->sector_size;
5603+			buf_size = c->sector_size;
5604 		else
5605-			try_size = PAGE_SIZE;
5606+			buf_size = PAGE_SIZE;
5607
5608 		jffs2_dbg(1, "Trying to allocate readbuf of %zu "
5609-			  "bytes\n", try_size);
5610+			  "bytes\n", buf_size);
5611
5612-		flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
5613+		flashbuf = kmalloc(buf_size, GFP_KERNEL);
5614 		if (!flashbuf)
5615 			return -ENOMEM;
5616
5617 		jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
5618-			  try_size);
5619-
5620-		buf_size = (uint32_t)try_size;
5621+			  buf_size);
5622 	}
5623
5624 	if (jffs2_sum_active()) {
5625@@ -140,7 +125,9 @@ int jffs2_scan_medium(struct jffs2_sb_in
5626 		}
5627 	}
5628
5629-	for (i=0; i<c->nr_blocks; i++) {
5630+	sb = OFNI_BS_2SFFJ(c);
5631+	device = (struct MtdNorDev*)(sb->s_dev);
5632+	for (i=device->blockStart; i<c->nr_blocks + device->blockStart; i++) {
5633 		struct jffs2_eraseblock *jeb = &c->blocks[i];
5634
5635 		cond_resched();
5636@@ -269,19 +256,12 @@ int jffs2_scan_medium(struct jffs2_sb_in
5637 			ret = -EIO;
5638 			goto out;
5639 		}
5640-		spin_lock(&c->erase_completion_lock);
5641-		jffs2_garbage_collect_trigger(c);
5642-		spin_unlock(&c->erase_completion_lock);
5643 	}
5644 	ret = 0;
5645  out:
5646-	if (buf_size)
5647-		kfree(flashbuf);
5648-#ifndef __ECOS
5649-	else
5650-		mtd_unpoint(c->mtd, 0, c->mtd->size);
5651-#endif
5652-	kfree(s);
5653+
5654+	kfree(flashbuf);
5655+
5656 	return ret;
5657 }
5658
5659@@ -411,7 +391,7 @@ static int jffs2_scan_xref_node(struct j
5660 	if (!ref)
5661 		return -ENOMEM;
5662
5663-	/* BEFORE jffs2_build_xattr_subsystem() called,
5664+	/* BEFORE jffs2_build_xattr_subsystem() called,
5665 	 * and AFTER xattr_ref is marked as a dead xref,
5666 	 * ref->xid is used to store 32bit xid, xd is not used
5667 	 * ref->ino is used to store 32bit inode-number, ic is not used
5668@@ -484,10 +464,10 @@ static int jffs2_scan_eraseblock (struct
5669 		struct jffs2_sum_marker *sm;
5670 		void *sumptr = NULL;
5671 		uint32_t sumlen;
5672-
5673+
5674 		if (!buf_size) {
5675 			/* XIP case. Just look, point at the summary if it's there */
5676-			sm = (void *)buf + c->sector_size - sizeof(*sm);
5677+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + c->sector_size - sizeof(*sm));
5678 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5679 				sumptr = buf + je32_to_cpu(sm->offset);
5680 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5681@@ -500,13 +480,13 @@ static int jffs2_scan_eraseblock (struct
5682 				buf_len = sizeof(*sm);
5683
5684 			/* Read as much as we want into the _end_ of the preallocated buffer */
5685-			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5686+			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5687 						  jeb->offset + c->sector_size - buf_len,
5688-						  buf_len);
5689+						  buf_len);
5690 			if (err)
5691 				return err;
5692
5693-			sm = (void *)buf + buf_size - sizeof(*sm);
5694+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + buf_size - sizeof(*sm));
5695 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5696 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5697 				sumptr = buf + buf_size - sumlen;
5698@@ -521,18 +501,15 @@ static int jffs2_scan_eraseblock (struct
5699 					sumptr = kmalloc(sumlen, GFP_KERNEL);
5700 					if (!sumptr)
5701 						return -ENOMEM;
5702-					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5703+					memcpy((uint8_t *)sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5704 				}
5705 				if (buf_len < sumlen) {
5706 					/* Need to read more so that the entire summary node is present */
5707-					err = jffs2_fill_scan_buf(c, sumptr,
5708+					err = jffs2_fill_scan_buf(c, sumptr,
5709 								  jeb->offset + c->sector_size - sumlen,
5710-								  sumlen - buf_len);
5711-					if (err) {
5712-						if (sumlen > buf_size)
5713-							kfree(sumptr);
5714+								  sumlen - buf_len);
5715+					if (err)
5716 						return err;
5717-					}
5718 				}
5719 			}
5720
5721@@ -543,7 +520,7 @@ static int jffs2_scan_eraseblock (struct
5722
5723 			if (buf_size && sumlen > buf_size)
5724 				kfree(sumptr);
5725-			/* If it returns with a real error, bail.
5726+			/* If it returns with a real error, bail.
5727 			   If it returns positive, that's a block classification
5728 			   (i.e. BLK_STATE_xxx) so return that too.
5729 			   If it returns zero, fall through to full scan. */
5730@@ -605,7 +582,7 @@ full_scan:
5731 	/* Now ofs is a complete physical flash offset as it always was... */
5732 	ofs += jeb->offset;
5733
5734-	noise = 10;
5735+	noise = 1;
5736
5737 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
5738
5739@@ -698,7 +675,7 @@ scan_more:
5740 				scan_end = buf_len;
5741 				goto more_empty;
5742 			}
5743-
5744+
5745 			/* See how much more there is to read in this eraseblock... */
5746 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
5747 			if (!buf_len) {
5748@@ -948,7 +925,7 @@ scan_more:
5749 	jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
5750 		  jeb->offset, jeb->free_size, jeb->dirty_size,
5751 		  jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
5752-
5753+
5754 	/* mark_node_obsolete can add to wasted !! */
5755 	if (jeb->wasted_size) {
5756 		jeb->dirty_size += jeb->wasted_size;
5757@@ -976,7 +953,6 @@ struct jffs2_inode_cache *jffs2_scan_mak
5758 		pr_notice("%s(): allocation of inode cache failed\n", __func__);
5759 		return NULL;
5760 	}
5761-	memset(ic, 0, sizeof(*ic));
5762
5763 	ic->ino = ino;
5764 	ic->nodes = (void *)ic;
5765@@ -1067,7 +1043,7 @@ static int jffs2_scan_dirent_node(struct
5766 	pseudo_random += je32_to_cpu(rd->version);
5767
5768 	/* Should never happen. Did. (OLPC trac #4184)*/
5769-	checkedlen = strnlen(rd->name, rd->nsize);
5770+	checkedlen = strnlen((const char *)rd->name, rd->nsize);
5771 	if (checkedlen < rd->nsize) {
5772 		pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
5773 		       ofs, checkedlen);
5774@@ -1079,7 +1055,7 @@ static int jffs2_scan_dirent_node(struct
5775 	memcpy(&fd->name, rd->name, checkedlen);
5776 	fd->name[checkedlen] = 0;
5777
5778-	crc = crc32(0, fd->name, checkedlen);
5779+	crc = crc32(0, fd->name, rd->nsize);
5780 	if (crc != je32_to_cpu(rd->name_crc)) {
5781 		pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
5782 			  __func__, ofs, je32_to_cpu(rd->name_crc), crc);
5783@@ -1104,7 +1080,7 @@ static int jffs2_scan_dirent_node(struct
5784 	fd->next = NULL;
5785 	fd->version = je32_to_cpu(rd->version);
5786 	fd->ino = je32_to_cpu(rd->ino);
5787-	fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
5788+	fd->nhash = full_name_hash(fd->name, checkedlen);
5789 	fd->type = rd->type;
5790 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
5791
5792diff -Nupr old/fs/jffs2/security.c new/fs/jffs2/security.c
5793--- old/fs/jffs2/security.c	2022-05-09 17:15:24.350000000 +0800
5794+++ new/fs/jffs2/security.c	1970-01-01 08:00:00.000000000 +0800
5795@@ -1,72 +0,0 @@
5796-/*
5797- * JFFS2 -- Journalling Flash File System, Version 2.
5798- *
5799- * Copyright © 2006  NEC Corporation
5800- *
5801- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
5802- *
5803- * For licensing information, see the file 'LICENCE' in this directory.
5804- *
5805- */
5806-
5807-#include <linux/kernel.h>
5808-#include <linux/slab.h>
5809-#include <linux/fs.h>
5810-#include <linux/time.h>
5811-#include <linux/pagemap.h>
5812-#include <linux/highmem.h>
5813-#include <linux/crc32.h>
5814-#include <linux/jffs2.h>
5815-#include <linux/xattr.h>
5816-#include <linux/mtd/mtd.h>
5817-#include <linux/security.h>
5818-#include "nodelist.h"
5819-
5820-/* ---- Initial Security Label(s) Attachment callback --- */
5821-static int jffs2_initxattrs(struct inode *inode,
5822-			    const struct xattr *xattr_array, void *fs_info)
5823-{
5824-	const struct xattr *xattr;
5825-	int err = 0;
5826-
5827-	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
5828-		err = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
5829-					xattr->name, xattr->value,
5830-					xattr->value_len, 0);
5831-		if (err < 0)
5832-			break;
5833-	}
5834-	return err;
5835-}
5836-
5837-/* ---- Initial Security Label(s) Attachment ----------- */
5838-int jffs2_init_security(struct inode *inode, struct inode *dir,
5839-			const struct qstr *qstr)
5840-{
5841-	return security_inode_init_security(inode, dir, qstr,
5842-					    &jffs2_initxattrs, NULL);
5843-}
5844-
5845-/* ---- XATTR Handler for "security.*" ----------------- */
5846-static int jffs2_security_getxattr(const struct xattr_handler *handler,
5847-				   struct dentry *unused, struct inode *inode,
5848-				   const char *name, void *buffer, size_t size)
5849-{
5850-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_SECURITY,
5851-				 name, buffer, size);
5852-}
5853-
5854-static int jffs2_security_setxattr(const struct xattr_handler *handler,
5855-				   struct dentry *unused, struct inode *inode,
5856-				   const char *name, const void *buffer,
5857-				   size_t size, int flags)
5858-{
5859-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
5860-				 name, buffer, size, flags);
5861-}
5862-
5863-const struct xattr_handler jffs2_security_xattr_handler = {
5864-	.prefix = XATTR_SECURITY_PREFIX,
5865-	.set = jffs2_security_setxattr,
5866-	.get = jffs2_security_getxattr
5867-};
5868diff -Nupr old/fs/jffs2/summary.c new/fs/jffs2/summary.c
5869--- old/fs/jffs2/summary.c	2022-05-09 17:22:53.000000000 +0800
5870+++ new/fs/jffs2/summary.c	2022-05-09 20:13:24.440000000 +0800
5871@@ -10,16 +10,20 @@
5872  * For licensing information, see the file 'LICENCE' in this directory.
5873  *
5874  */
5875+#include "summary.h"
5876
5877+#ifdef CONFIG_JFFS2_SUMMARY
5878+
5879+#ifndef pr_fmt
5880 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5881+#endif
5882
5883 #include <linux/kernel.h>
5884 #include <linux/slab.h>
5885-#include <linux/mtd/mtd.h>
5886+#include <mtd_dev.h>
5887 #include <linux/pagemap.h>
5888-#include <linux/crc32.h>
5889+#include "los_crc32.h"
5890 #include <linux/compiler.h>
5891-#include <linux/vmalloc.h>
5892 #include "nodelist.h"
5893 #include "debug.h"
5894
5895@@ -388,11 +392,25 @@ static int jffs2_sum_process_sum_data(st
5896 {
5897 	struct jffs2_inode_cache *ic;
5898 	struct jffs2_full_dirent *fd;
5899-	void *sp;
5900+	uintptr_t sp;
5901 	int i, ino;
5902 	int err;
5903
5904-	sp = summary->sum;
5905+	sp = (uintptr_t)summary->sum;
5906+
5907+#if 0
5908+	PRINTK("summary: %x %x %d %d %x %x %d %x %x  %p %p\n",
5909+	je16_to_cpu(summary->magic),
5910+	je16_to_cpu(summary->nodetype),
5911+	je32_to_cpu(summary->totlen),
5912+	je32_to_cpu(summary->hdr_crc),
5913+	je32_to_cpu(summary->sum_num),
5914+	je32_to_cpu(summary->cln_mkr),
5915+	je32_to_cpu(summary->padded),
5916+	je32_to_cpu(summary->sum_crc),
5917+	je32_to_cpu(summary->node_crc),
5918+	sp, summary->sum);
5919+#endif
5920
5921 	for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
5922 		dbg_summary("processing summary index %d\n", i);
5923@@ -404,10 +422,12 @@ static int jffs2_sum_process_sum_data(st
5924 		if (err)
5925 			return err;
5926
5927+		//PRINTK("sum type %d \n", je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype));
5928+
5929 		switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
5930 			case JFFS2_NODETYPE_INODE: {
5931 				struct jffs2_sum_inode_flash *spi;
5932-				spi = sp;
5933+				spi = (struct jffs2_sum_inode_flash *)sp;
5934
5935 				ino = je32_to_cpu(spi->inode);
5936
5937@@ -428,13 +448,29 @@ static int jffs2_sum_process_sum_data(st
5938
5939 				sp += JFFS2_SUMMARY_INODE_SIZE;
5940
5941+				//PRINTK("1 sp + %d %p\n", JFFS2_SUMMARY_INODE_SIZE, sp);
5942+
5943 				break;
5944 			}
5945
5946 			case JFFS2_NODETYPE_DIRENT: {
5947 				struct jffs2_sum_dirent_flash *spd;
5948 				int checkedlen;
5949-				spd = sp;
5950+				spd = (struct jffs2_sum_dirent_flash *)sp;
5951+
5952+
5953+#if 0
5954+					PRINTK("dir: %x %d %d %d %d %d %d %d %d\n",
5955+	je16_to_cpu(spd->nodetype),
5956+	je32_to_cpu(spd->totlen),
5957+	je32_to_cpu(spd->offset),
5958+	je32_to_cpu(spd->pino),
5959+	je32_to_cpu(spd->version),
5960+	je32_to_cpu(spd->ino),
5961+	spd->nsize,
5962+	spd->type,
5963+	spd->name);
5964+#endif
5965
5966 				dbg_summary("Dirent at 0x%08x-0x%08x\n",
5967 					    jeb->offset + je32_to_cpu(spd->offset),
5968@@ -442,7 +478,7 @@ static int jffs2_sum_process_sum_data(st
5969
5970
5971 				/* This should never happen, but https://dev.laptop.org/ticket/4184 */
5972-				checkedlen = strnlen(spd->name, spd->nsize);
5973+				checkedlen = strnlen((const char *)spd->name, spd->nsize);
5974 				if (!checkedlen) {
5975 					pr_err("Dirent at %08x has zero at start of name. Aborting mount.\n",
5976 					       jeb->offset +
5977@@ -463,6 +499,7 @@ static int jffs2_sum_process_sum_data(st
5978
5979 				memcpy(&fd->name, spd->name, checkedlen);
5980 				fd->name[checkedlen] = 0;
5981+				//PRINTK("add %s \n", fd->name);
5982
5983 				ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
5984 				if (!ic) {
5985@@ -476,15 +513,19 @@ static int jffs2_sum_process_sum_data(st
5986 				fd->next = NULL;
5987 				fd->version = je32_to_cpu(spd->version);
5988 				fd->ino = je32_to_cpu(spd->ino);
5989-				fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
5990+				fd->nhash = full_name_hash((const unsigned char *)fd->name, checkedlen);
5991 				fd->type = spd->type;
5992
5993 				jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
5994
5995 				*pseudo_random += je32_to_cpu(spd->version);
5996
5997+				//PRINTK("2 sp before add %p\n", sp);
5998+
5999 				sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
6000
6001+			    //PRINTK("2 sp + %d %p\n", JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize), sp);
6002+
6003 				break;
6004 			}
6005 #ifdef CONFIG_JFFS2_FS_XATTR
6006@@ -493,7 +534,7 @@ static int jffs2_sum_process_sum_data(st
6007 				struct jffs2_sum_xattr_flash *spx;
6008
6009 				spx = (struct jffs2_sum_xattr_flash *)sp;
6010-				dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
6011+				dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
6012 					    jeb->offset + je32_to_cpu(spx->offset),
6013 					    jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen),
6014 					    je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
6015@@ -526,7 +567,7 @@ static int jffs2_sum_process_sum_data(st
6016 				spr = (struct jffs2_sum_xref_flash *)sp;
6017 				dbg_summary("xref at %#08x-%#08x\n",
6018 					    jeb->offset + je32_to_cpu(spr->offset),
6019-					    jeb->offset + je32_to_cpu(spr->offset) +
6020+					    jeb->offset + je32_to_cpu(spr->offset) +
6021 					    (uint32_t)PAD(sizeof(struct jffs2_raw_xref)));
6022
6023 				ref = jffs2_alloc_xattr_ref();
6024@@ -679,7 +720,7 @@ static int jffs2_sum_write_data(struct j
6025 	struct jffs2_sum_marker *sm;
6026 	struct kvec vecs[2];
6027 	uint32_t sum_ofs;
6028-	void *wpage;
6029+	uintptr_t wpage;
6030 	int ret;
6031 	size_t retlen;
6032
6033@@ -713,14 +754,14 @@ static int jffs2_sum_write_data(struct j
6034 	isum.padded = cpu_to_je32(c->summary->sum_padded);
6035 	isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
6036 	isum.sum_num = cpu_to_je32(c->summary->sum_num);
6037-	wpage = c->summary->sum_buf;
6038+	wpage = (uintptr_t)c->summary->sum_buf;
6039
6040 	while (c->summary->sum_num) {
6041 		temp = c->summary->sum_list_head;
6042
6043 		switch (je16_to_cpu(temp->u.nodetype)) {
6044 			case JFFS2_NODETYPE_INODE: {
6045-				struct jffs2_sum_inode_flash *sino_ptr = wpage;
6046+				struct jffs2_sum_inode_flash *sino_ptr = (struct jffs2_sum_inode_flash *)wpage;
6047
6048 				sino_ptr->nodetype = temp->i.nodetype;
6049 				sino_ptr->inode = temp->i.inode;
6050@@ -734,7 +775,7 @@ static int jffs2_sum_write_data(struct j
6051 			}
6052
6053 			case JFFS2_NODETYPE_DIRENT: {
6054-				struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
6055+				struct jffs2_sum_dirent_flash *sdrnt_ptr = (struct jffs2_sum_dirent_flash *)wpage;
6056
6057 				sdrnt_ptr->nodetype = temp->d.nodetype;
6058 				sdrnt_ptr->totlen = temp->d.totlen;
6059@@ -802,7 +843,7 @@ static int jffs2_sum_write_data(struct j
6060
6061 	wpage += padsize;
6062
6063-	sm = wpage;
6064+	sm = (struct jffs2_sum_marker *)wpage;
6065 	sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
6066 	sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
6067
6068@@ -847,7 +888,7 @@ static int jffs2_sum_write_data(struct j
6069 /* Write out summary information - called from jffs2_do_reserve_space */
6070
6071 int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
6072-	__must_hold(&c->erase_completion_block)
6073+	//__must_hold(&c->erase_completion_block)
6074 {
6075 	int datasize, infosize, padsize;
6076 	struct jffs2_eraseblock *jeb;
6077@@ -875,3 +916,5 @@ int jffs2_sum_write_sumnode(struct jffs2
6078 	spin_lock(&c->erase_completion_lock);
6079 	return ret;
6080 }
6081+
6082+#endif
6083diff -Nupr old/fs/jffs2/summary.h new/fs/jffs2/summary.h
6084--- old/fs/jffs2/summary.h	2022-05-09 17:22:53.000000000 +0800
6085+++ new/fs/jffs2/summary.h	2022-05-09 20:35:43.430000000 +0800
6086@@ -19,8 +19,9 @@
6087    anyway. */
6088 #define MAX_SUMMARY_SIZE 65536
6089
6090-#include <linux/uio.h>
6091-#include <linux/jffs2.h>
6092+#include <sys/uio.h>
6093+#include <linux/types.h>
6094+#include "jffs2.h"
6095
6096 #define BLK_STATE_ALLFF		0
6097 #define BLK_STATE_CLEAN		1
6098@@ -169,6 +170,10 @@ struct jffs2_sum_marker
6099
6100 #define JFFS2_SUMMARY_FRAME_SIZE (sizeof(struct jffs2_raw_summary) + sizeof(struct jffs2_sum_marker))
6101
6102+#ifdef LOSCFG_FS_JFFS2_SUMMARY
6103+#define CONFIG_JFFS2_SUMMARY
6104+#endif
6105+
6106 #ifdef CONFIG_JFFS2_SUMMARY	/* SUMMARY SUPPORT ENABLED */
6107
6108 #define jffs2_sum_active() (1)
6109diff -Nupr old/fs/jffs2/super.c new/fs/jffs2/super.c
6110--- old/fs/jffs2/super.c	2022-05-09 17:22:53.000000000 +0800
6111+++ new/fs/jffs2/super.c	2022-05-09 20:09:32.170000000 +0800
6112@@ -9,433 +9,188 @@
6113  *
6114  */
6115
6116-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6117
6118-#include <linux/kernel.h>
6119-#include <linux/module.h>
6120-#include <linux/slab.h>
6121-#include <linux/init.h>
6122-#include <linux/list.h>
6123-#include <linux/fs.h>
6124-#include <linux/err.h>
6125-#include <linux/mount.h>
6126-#include <linux/fs_context.h>
6127-#include <linux/fs_parser.h>
6128-#include <linux/jffs2.h>
6129-#include <linux/pagemap.h>
6130-#include <linux/mtd/super.h>
6131-#include <linux/ctype.h>
6132-#include <linux/namei.h>
6133-#include <linux/seq_file.h>
6134-#include <linux/exportfs.h>
6135-#include "compr.h"
6136+#include "jffs2.h"
6137 #include "nodelist.h"
6138+#include "jffs2_fs_sb.h"
6139+#include "mtd_dev.h"
6140+#include "mtd_partition.h"
6141+#include "compr.h"
6142+#include "jffs2_hash.h"
6143
6144-static void jffs2_put_super(struct super_block *);
6145-
6146-static struct kmem_cache *jffs2_inode_cachep;
6147+static unsigned char jffs2_mounted_number = 0; /* a counter to track the number of jffs2 instances mounted */
6148+struct MtdNorDev jffs2_dev_list[CONFIG_MTD_PATTITION_NUM];
6149
6150-static struct inode *jffs2_alloc_inode(struct super_block *sb)
6151+/*
6152+ * fill in the superblock
6153+ */
6154+int jffs2_fill_super(struct super_block *sb)
6155 {
6156-	struct jffs2_inode_info *f;
6157-
6158-	f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
6159-	if (!f)
6160-		return NULL;
6161-	return &f->vfs_inode;
6162-}
6163+	int ret;
6164+	struct jffs2_sb_info *c;
6165+	struct MtdNorDev *device;
6166
6167-static void jffs2_free_inode(struct inode *inode)
6168-{
6169-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
6170+	c = JFFS2_SB_INFO(sb);
6171+	device = (struct MtdNorDev*)(sb->s_dev);
6172
6173-	kfree(f->target);
6174-	kmem_cache_free(jffs2_inode_cachep, f);
6175-}
6176+	(void)mutex_init(&c->alloc_sem);
6177+	(void)mutex_init(&c->erase_free_sem);
6178+	spin_lock_init(&c->erase_completion_lock);
6179+	spin_lock_init(&c->inocache_lock);
6180
6181-static void jffs2_i_init_once(void *foo)
6182-{
6183-	struct jffs2_inode_info *f = foo;
6184+	/* sector size is the erase block size */
6185+	c->sector_size = device->blockSize;
6186+	c->flash_size  = (device->blockEnd - device->blockStart + 1) * device->blockSize;
6187+	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
6188
6189-	mutex_init(&f->sem);
6190-	inode_init_once(&f->vfs_inode);
6191-}
6192+	ret = jffs2_do_mount_fs(c);
6193+	if (ret) {
6194+		(void)mutex_destroy(&c->alloc_sem);
6195+		(void)mutex_destroy(&c->erase_free_sem);
6196+		return ret;
6197+	}
6198+	D1(printk(KERN_DEBUG "jffs2_fill_super(): Getting root inode\n"));
6199+
6200+	sb->s_root = jffs2_iget(sb, 1);
6201+
6202+	if (IS_ERR(sb->s_root)) {
6203+		D1(printk(KERN_WARNING "get root inode failed\n"));
6204+		ret = PTR_ERR(sb->s_root);
6205+		sb->s_root = NULL;
6206+		jffs2_free_ino_caches(c);
6207+		jffs2_free_raw_node_refs(c);
6208+		free(c->blocks);
6209+		(void)mutex_destroy(&c->alloc_sem);
6210+		(void)mutex_destroy(&c->erase_free_sem);
6211
6212-static const char *jffs2_compr_name(unsigned int compr)
6213-{
6214-	switch (compr) {
6215-	case JFFS2_COMPR_MODE_NONE:
6216-		return "none";
6217-#ifdef CONFIG_JFFS2_LZO
6218-	case JFFS2_COMPR_MODE_FORCELZO:
6219-		return "lzo";
6220-#endif
6221-#ifdef CONFIG_JFFS2_ZLIB
6222-	case JFFS2_COMPR_MODE_FORCEZLIB:
6223-		return "zlib";
6224-#endif
6225-	default:
6226-		/* should never happen; programmer error */
6227-		WARN_ON(1);
6228-		return "";
6229+		return ret;
6230 	}
6231-}
6232-
6233-static int jffs2_show_options(struct seq_file *s, struct dentry *root)
6234-{
6235-	struct jffs2_sb_info *c = JFFS2_SB_INFO(root->d_sb);
6236-	struct jffs2_mount_opts *opts = &c->mount_opts;
6237-
6238-	if (opts->override_compr)
6239-		seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
6240-	if (opts->set_rp_size)
6241-		seq_printf(s, ",rp_size=%u", opts->rp_size / 1024);
6242-
6243-	return 0;
6244-}
6245-
6246-static int jffs2_sync_fs(struct super_block *sb, int wait)
6247-{
6248-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
6249-
6250-#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
6251-	if (jffs2_is_writebuffered(c))
6252-		cancel_delayed_work_sync(&c->wbuf_dwork);
6253-#endif
6254-
6255-	mutex_lock(&c->alloc_sem);
6256-	jffs2_flush_wbuf_pad(c);
6257-	mutex_unlock(&c->alloc_sem);
6258 	return 0;
6259 }
6260
6261-static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
6262-					 uint32_t generation)
6263+int jffs2_mount(int part_no, struct jffs2_inode **root_node, unsigned long mountflags)
6264 {
6265-	/* We don't care about i_generation. We'll destroy the flash
6266-	   before we start re-using inode numbers anyway. And even
6267-	   if that wasn't true, we'd have other problems...*/
6268-	return jffs2_iget(sb, ino);
6269-}
6270-
6271-static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
6272-					 int fh_len, int fh_type)
6273-{
6274-        return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
6275-                                    jffs2_nfs_get_inode);
6276-}
6277-
6278-static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
6279-					 int fh_len, int fh_type)
6280-{
6281-        return generic_fh_to_parent(sb, fid, fh_len, fh_type,
6282-                                    jffs2_nfs_get_inode);
6283-}
6284-
6285-static struct dentry *jffs2_get_parent(struct dentry *child)
6286-{
6287-	struct jffs2_inode_info *f;
6288-	uint32_t pino;
6289-
6290-	BUG_ON(!d_is_dir(child));
6291-
6292-	f = JFFS2_INODE_INFO(d_inode(child));
6293-
6294-	pino = f->inocache->pino_nlink;
6295-
6296-	JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
6297-		    f->inocache->ino, pino);
6298-
6299-	return d_obtain_alias(jffs2_iget(child->d_sb, pino));
6300-}
6301-
6302-static const struct export_operations jffs2_export_ops = {
6303-	.get_parent = jffs2_get_parent,
6304-	.fh_to_dentry = jffs2_fh_to_dentry,
6305-	.fh_to_parent = jffs2_fh_to_parent,
6306-};
6307-
6308-/*
6309- * JFFS2 mount options.
6310- *
6311- * Opt_source: The source device
6312- * Opt_override_compr: override default compressor
6313- * Opt_rp_size: size of reserved pool in KiB
6314- */
6315-enum {
6316-	Opt_override_compr,
6317-	Opt_rp_size,
6318-};
6319-
6320-static const struct constant_table jffs2_param_compr[] = {
6321-	{"none",	JFFS2_COMPR_MODE_NONE },
6322-#ifdef CONFIG_JFFS2_LZO
6323-	{"lzo",		JFFS2_COMPR_MODE_FORCELZO },
6324-#endif
6325-#ifdef CONFIG_JFFS2_ZLIB
6326-	{"zlib",	JFFS2_COMPR_MODE_FORCEZLIB },
6327-#endif
6328-	{}
6329-};
6330+	struct super_block *sb = NULL;
6331+	struct jffs2_sb_info *c = NULL;
6332+	LOS_DL_LIST *part_head = NULL;
6333+	struct MtdDev *spinor_mtd = NULL;
6334+	mtd_partition *mtd_part = GetSpinorPartitionHead();
6335+	int ret;
6336
6337-static const struct fs_parameter_spec jffs2_fs_parameters[] = {
6338-	fsparam_enum	("compr",	Opt_override_compr, jffs2_param_compr),
6339-	fsparam_u32	("rp_size",	Opt_rp_size),
6340-	{}
6341-};
6342+	jffs2_dbg(1, "begin los_jffs2_mount:%d\n", part_no);
6343
6344-static int jffs2_parse_param(struct fs_context *fc, struct fs_parameter *param)
6345-{
6346-	struct fs_parse_result result;
6347-	struct jffs2_sb_info *c = fc->s_fs_info;
6348-	int opt;
6349-
6350-	opt = fs_parse(fc, jffs2_fs_parameters, param, &result);
6351-	if (opt < 0)
6352-		return opt;
6353-
6354-	switch (opt) {
6355-	case Opt_override_compr:
6356-		c->mount_opts.compr = result.uint_32;
6357-		c->mount_opts.override_compr = true;
6358-		break;
6359-	case Opt_rp_size:
6360-		if (result.uint_32 > UINT_MAX / 1024)
6361-			return invalf(fc, "jffs2: rp_size unrepresentable");
6362-		c->mount_opts.rp_size = result.uint_32 * 1024;
6363-		c->mount_opts.set_rp_size = true;
6364-		break;
6365-	default:
6366-		return -EINVAL;
6367+	sb = zalloc(sizeof(struct super_block));
6368+	if (sb == NULL) {
6369+		return -ENOMEM;
6370 	}
6371
6372-	return 0;
6373-}
6374-
6375-static inline void jffs2_update_mount_opts(struct fs_context *fc)
6376-{
6377-	struct jffs2_sb_info *new_c = fc->s_fs_info;
6378-	struct jffs2_sb_info *c = JFFS2_SB_INFO(fc->root->d_sb);
6379-
6380-	mutex_lock(&c->alloc_sem);
6381-	if (new_c->mount_opts.override_compr) {
6382-		c->mount_opts.override_compr = new_c->mount_opts.override_compr;
6383-		c->mount_opts.compr = new_c->mount_opts.compr;
6384-	}
6385-	if (new_c->mount_opts.set_rp_size) {
6386-		c->mount_opts.set_rp_size = new_c->mount_opts.set_rp_size;
6387-		c->mount_opts.rp_size = new_c->mount_opts.rp_size;
6388+	ret = Jffs2HashInit(&sb->s_node_hash_lock, &sb->s_node_hash[0]);
6389+	if (ret) {
6390+		free(sb);
6391+		return ret;
6392+	}
6393+	part_head = &(GetSpinorPartitionHead()->node_info);
6394+	LOS_DL_LIST_FOR_EACH_ENTRY(mtd_part,part_head, mtd_partition, node_info) {
6395+		if (mtd_part->patitionnum == part_no)
6396+			break;
6397+	}
6398+#ifndef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
6399+	spinor_mtd = GetMtd("spinor");
6400+#else
6401+	spinor_mtd = (struct MtdDev *)LOS_DL_LIST_ENTRY(part_head->pstNext, mtd_partition, node_info)->mtd_info;
6402+#endif
6403+	if (spinor_mtd == NULL) {
6404+		free(sb);
6405+		return -EPERM;
6406+	}
6407+	jffs2_dev_list[part_no].blockEnd = mtd_part->end_block;
6408+	jffs2_dev_list[part_no].blockSize = spinor_mtd->eraseSize;
6409+	jffs2_dev_list[part_no].blockStart = mtd_part->start_block;
6410+#ifndef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
6411+	(void)FreeMtd(spinor_mtd);
6412+#endif
6413+	sb->jffs2_sb.mtd = mtd_part->mtd_info;
6414+	sb->s_dev = &jffs2_dev_list[part_no];
6415+
6416+	c = JFFS2_SB_INFO(sb);
6417+	c->flash_size  = (mtd_part->end_block - mtd_part->start_block + 1) * spinor_mtd->eraseSize;
6418+	c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
6419+	c->sector_size = spinor_mtd->eraseSize;
6420+
6421+	jffs2_dbg(1, "C mtd_size:%d,mtd-erase:%d,blocks:%d,hashsize:%d\n",
6422+		c->flash_size, c->sector_size, c->flash_size / c->sector_size, c->inocache_hashsize);
6423+
6424+	c->inocache_list = zalloc(sizeof(struct jffs2_inode_cache *) * c->inocache_hashsize);
6425+	if (c->inocache_list == NULL) {
6426+		free(sb);
6427+		return -ENOMEM;
6428+	}
6429+	if (jffs2_mounted_number++ == 0) {
6430+		(void)jffs2_create_slab_caches(); // No error check, cannot fail
6431+		(void)jffs2_compressors_init();
6432 	}
6433-	mutex_unlock(&c->alloc_sem);
6434-}
6435-
6436-static int jffs2_reconfigure(struct fs_context *fc)
6437-{
6438-	struct super_block *sb = fc->root->d_sb;
6439-
6440-	sync_filesystem(sb);
6441-	jffs2_update_mount_opts(fc);
6442-
6443-	return jffs2_do_remount_fs(sb, fc);
6444-}
6445-
6446-static const struct super_operations jffs2_super_operations =
6447-{
6448-	.alloc_inode =	jffs2_alloc_inode,
6449-	.free_inode =	jffs2_free_inode,
6450-	.put_super =	jffs2_put_super,
6451-	.statfs =	jffs2_statfs,
6452-	.evict_inode =	jffs2_evict_inode,
6453-	.dirty_inode =	jffs2_dirty_inode,
6454-	.show_options =	jffs2_show_options,
6455-	.sync_fs =	jffs2_sync_fs,
6456-};
6457-
6458-/*
6459- * fill in the superblock
6460- */
6461-static int jffs2_fill_super(struct super_block *sb, struct fs_context *fc)
6462-{
6463-	struct jffs2_sb_info *c = sb->s_fs_info;
6464-
6465-	jffs2_dbg(1, "jffs2_get_sb_mtd():"
6466-		  " New superblock for device %d (\"%s\")\n",
6467-		  sb->s_mtd->index, sb->s_mtd->name);
6468-
6469-	c->mtd = sb->s_mtd;
6470-	c->os_priv = sb;
6471-
6472-	if (c->mount_opts.rp_size > c->mtd->size)
6473-		return invalf(fc, "jffs2: Too large reserve pool specified, max is %llu KB",
6474-			      c->mtd->size / 1024);
6475-
6476-	/* Initialize JFFS2 superblock locks, the further initialization will
6477-	 * be done later */
6478-	mutex_init(&c->alloc_sem);
6479-	mutex_init(&c->erase_free_sem);
6480-	init_waitqueue_head(&c->erase_wait);
6481-	init_waitqueue_head(&c->inocache_wq);
6482-	spin_lock_init(&c->erase_completion_lock);
6483-	spin_lock_init(&c->inocache_lock);
6484-
6485-	sb->s_op = &jffs2_super_operations;
6486-	sb->s_export_op = &jffs2_export_ops;
6487-	sb->s_flags = sb->s_flags | SB_NOATIME;
6488-	sb->s_xattr = jffs2_xattr_handlers;
6489-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
6490-	sb->s_flags |= SB_POSIXACL;
6491-#endif
6492-	return jffs2_do_fill_super(sb, fc);
6493-}
6494-
6495-static int jffs2_get_tree(struct fs_context *fc)
6496-{
6497-	return get_tree_mtd(fc, jffs2_fill_super);
6498-}
6499-
6500-static void jffs2_free_fc(struct fs_context *fc)
6501-{
6502-	kfree(fc->s_fs_info);
6503-}
6504
6505-static const struct fs_context_operations jffs2_context_ops = {
6506-	.free		= jffs2_free_fc,
6507-	.parse_param	= jffs2_parse_param,
6508-	.get_tree	= jffs2_get_tree,
6509-	.reconfigure	= jffs2_reconfigure,
6510-};
6511+	ret = jffs2_fill_super(sb);
6512+	if (ret) {
6513+		if (--jffs2_mounted_number == 0) {
6514+			jffs2_destroy_slab_caches();
6515+			(void)jffs2_compressors_exit();
6516+		}
6517
6518-static int jffs2_init_fs_context(struct fs_context *fc)
6519-{
6520-	struct jffs2_sb_info *ctx;
6521+		free(sb);
6522+		free(c->inocache_list);
6523+		c->inocache_list = NULL;
6524+		return ret;
6525+	}
6526
6527-	ctx = kzalloc(sizeof(struct jffs2_sb_info), GFP_KERNEL);
6528-	if (!ctx)
6529-		return -ENOMEM;
6530+	if (!(mountflags & MS_RDONLY)) {
6531+		jffs2_start_garbage_collect_thread(c);
6532+	}
6533
6534-	fc->s_fs_info = ctx;
6535-	fc->ops = &jffs2_context_ops;
6536+	sb->s_mount_flags = mountflags;
6537+	*root_node = sb->s_root;
6538 	return 0;
6539 }
6540
6541-static void jffs2_put_super (struct super_block *sb)
6542+int jffs2_umount(struct jffs2_inode *root_node)
6543 {
6544+	struct super_block *sb = root_node->i_sb;
6545 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
6546+	struct jffs2_full_dirent *fd, *next;
6547
6548-	jffs2_dbg(2, "%s()\n", __func__);
6549+	D2(PRINTK("Jffs2Umount\n"));
6550
6551-	mutex_lock(&c->alloc_sem);
6552-	jffs2_flush_wbuf_pad(c);
6553-	mutex_unlock(&c->alloc_sem);
6554-
6555-	jffs2_sum_exit(c);
6556-
6557-	jffs2_free_ino_caches(c);
6558-	jffs2_free_raw_node_refs(c);
6559-	kvfree(c->blocks);
6560-	jffs2_flash_cleanup(c);
6561-	kfree(c->inocache_list);
6562-	jffs2_clear_xattr_subsystem(c);
6563-	mtd_sync(c->mtd);
6564-	jffs2_dbg(1, "%s(): returning\n", __func__);
6565-}
6566-
6567-static void jffs2_kill_sb(struct super_block *sb)
6568-{
6569-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
6570-	if (c && !sb_rdonly(sb))
6571+	// Only really umount if this is the only mount
6572+	if (!(sb->s_mount_flags & MS_RDONLY)) {
6573 		jffs2_stop_garbage_collect_thread(c);
6574-	kill_mtd_super(sb);
6575-	kfree(c);
6576-}
6577-
6578-static struct file_system_type jffs2_fs_type = {
6579-	.owner =	THIS_MODULE,
6580-	.name =		"jffs2",
6581-	.init_fs_context = jffs2_init_fs_context,
6582-	.parameters =	jffs2_fs_parameters,
6583-	.kill_sb =	jffs2_kill_sb,
6584-};
6585-MODULE_ALIAS_FS("jffs2");
6586+	}
6587
6588-static int __init init_jffs2_fs(void)
6589-{
6590-	int ret;
6591+	// free directory entries
6592+	for (fd = root_node->jffs2_i.dents; fd; fd = next) {
6593+		next = fd->next;
6594+		jffs2_free_full_dirent(fd);
6595+	}
6596
6597-	/* Paranoia checks for on-medium structures. If we ask GCC
6598-	   to pack them with __attribute__((packed)) then it _also_
6599-	   assumes that they're not aligned -- so it emits crappy
6600-	   code on some architectures. Ideally we want an attribute
6601-	   which means just 'no padding', without the alignment
6602-	   thing. But GCC doesn't have that -- we have to just
6603-	   hope the structs are the right sizes, instead. */
6604-	BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
6605-	BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
6606-	BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
6607-	BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
6608-
6609-	pr_info("version 2.2."
6610-#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
6611-	       " (NAND)"
6612-#endif
6613-#ifdef CONFIG_JFFS2_SUMMARY
6614-	       " (SUMMARY) "
6615-#endif
6616-	       " © 2001-2006 Red Hat, Inc.\n");
6617+	free(root_node);
6618
6619-	jffs2_inode_cachep = kmem_cache_create("jffs2_i",
6620-					     sizeof(struct jffs2_inode_info),
6621-					     0, (SLAB_RECLAIM_ACCOUNT|
6622-						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
6623-					     jffs2_i_init_once);
6624-	if (!jffs2_inode_cachep) {
6625-		pr_err("error: Failed to initialise inode cache\n");
6626-		return -ENOMEM;
6627-	}
6628-	ret = jffs2_compressors_init();
6629-	if (ret) {
6630-		pr_err("error: Failed to initialise compressors\n");
6631-		goto out;
6632-	}
6633-	ret = jffs2_create_slab_caches();
6634-	if (ret) {
6635-		pr_err("error: Failed to initialise slab caches\n");
6636-		goto out_compressors;
6637-	}
6638-	ret = register_filesystem(&jffs2_fs_type);
6639-	if (ret) {
6640-		pr_err("error: Failed to register filesystem\n");
6641-		goto out_slab;
6642+	// Clean up the super block and root_node inode
6643+	jffs2_free_ino_caches(c);
6644+	jffs2_free_raw_node_refs(c);
6645+	free(c->blocks);
6646+	c->blocks = NULL;
6647+	free(c->inocache_list);
6648+	c->inocache_list = NULL;
6649+	(void)Jffs2HashDeinit(&sb->s_node_hash_lock);
6650+
6651+	(void)mutex_destroy(&c->alloc_sem);
6652+	(void)mutex_destroy(&c->erase_free_sem);
6653+	free(sb);
6654+	// That's all folks.
6655+	D2(PRINTK("Jffs2Umount No current mounts\n"));
6656+
6657+	if (--jffs2_mounted_number == 0) {
6658+		jffs2_destroy_slab_caches();
6659+		(void)jffs2_compressors_exit();
6660 	}
6661 	return 0;
6662-
6663- out_slab:
6664-	jffs2_destroy_slab_caches();
6665- out_compressors:
6666-	jffs2_compressors_exit();
6667- out:
6668-	kmem_cache_destroy(jffs2_inode_cachep);
6669-	return ret;
6670 }
6671-
6672-static void __exit exit_jffs2_fs(void)
6673-{
6674-	unregister_filesystem(&jffs2_fs_type);
6675-	jffs2_destroy_slab_caches();
6676-	jffs2_compressors_exit();
6677-
6678-	/*
6679-	 * Make sure all delayed rcu free inodes are flushed before we
6680-	 * destroy cache.
6681-	 */
6682-	rcu_barrier();
6683-	kmem_cache_destroy(jffs2_inode_cachep);
6684-}
6685-
6686-module_init(init_jffs2_fs);
6687-module_exit(exit_jffs2_fs);
6688-
6689-MODULE_DESCRIPTION("The Journalling Flash File System, v2");
6690-MODULE_AUTHOR("Red Hat, Inc.");
6691-MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
6692-		       // the sake of this tag. It's Free Software.
6693diff -Nupr old/fs/jffs2/symlink.c new/fs/jffs2/symlink.c
6694--- old/fs/jffs2/symlink.c	2022-05-09 17:15:24.350000000 +0800
6695+++ new/fs/jffs2/symlink.c	1970-01-01 08:00:00.000000000 +0800
6696@@ -1,19 +0,0 @@
6697-/*
6698- * JFFS2 -- Journalling Flash File System, Version 2.
6699- *
6700- * Copyright © 2001-2007 Red Hat, Inc.
6701- *
6702- * Created by David Woodhouse <dwmw2@infradead.org>
6703- *
6704- * For licensing information, see the file 'LICENCE' in this directory.
6705- *
6706- */
6707-
6708-#include "nodelist.h"
6709-
6710-const struct inode_operations jffs2_symlink_inode_operations =
6711-{
6712-	.get_link =	simple_get_link,
6713-	.setattr =	jffs2_setattr,
6714-	.listxattr =	jffs2_listxattr,
6715-};
6716diff -Nupr old/fs/jffs2/wbuf.c new/fs/jffs2/wbuf.c
6717--- old/fs/jffs2/wbuf.c	2022-05-09 17:15:24.350000000 +0800
6718+++ new/fs/jffs2/wbuf.c	1970-01-01 08:00:00.000000000 +0800
6719@@ -1,1350 +0,0 @@
6720-/*
6721- * JFFS2 -- Journalling Flash File System, Version 2.
6722- *
6723- * Copyright © 2001-2007 Red Hat, Inc.
6724- * Copyright © 2004 Thomas Gleixner <tglx@linutronix.de>
6725- *
6726- * Created by David Woodhouse <dwmw2@infradead.org>
6727- * Modified debugged and enhanced by Thomas Gleixner <tglx@linutronix.de>
6728- *
6729- * For licensing information, see the file 'LICENCE' in this directory.
6730- *
6731- */
6732-
6733-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6734-
6735-#include <linux/kernel.h>
6736-#include <linux/slab.h>
6737-#include <linux/mtd/mtd.h>
6738-#include <linux/crc32.h>
6739-#include <linux/mtd/rawnand.h>
6740-#include <linux/jiffies.h>
6741-#include <linux/sched.h>
6742-#include <linux/writeback.h>
6743-
6744-#include "nodelist.h"
6745-
6746-/* For testing write failures */
6747-#undef BREAKME
6748-#undef BREAKMEHEADER
6749-
6750-#ifdef BREAKME
6751-static unsigned char *brokenbuf;
6752-#endif
6753-
6754-#define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) )
6755-#define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) )
6756-
6757-/* max. erase failures before we mark a block bad */
6758-#define MAX_ERASE_FAILURES 	2
6759-
6760-struct jffs2_inodirty {
6761-	uint32_t ino;
6762-	struct jffs2_inodirty *next;
6763-};
6764-
6765-static struct jffs2_inodirty inodirty_nomem;
6766-
6767-static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino)
6768-{
6769-	struct jffs2_inodirty *this = c->wbuf_inodes;
6770-
6771-	/* If a malloc failed, consider _everything_ dirty */
6772-	if (this == &inodirty_nomem)
6773-		return 1;
6774-
6775-	/* If ino == 0, _any_ non-GC writes mean 'yes' */
6776-	if (this && !ino)
6777-		return 1;
6778-
6779-	/* Look to see if the inode in question is pending in the wbuf */
6780-	while (this) {
6781-		if (this->ino == ino)
6782-			return 1;
6783-		this = this->next;
6784-	}
6785-	return 0;
6786-}
6787-
6788-static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c)
6789-{
6790-	struct jffs2_inodirty *this;
6791-
6792-	this = c->wbuf_inodes;
6793-
6794-	if (this != &inodirty_nomem) {
6795-		while (this) {
6796-			struct jffs2_inodirty *next = this->next;
6797-			kfree(this);
6798-			this = next;
6799-		}
6800-	}
6801-	c->wbuf_inodes = NULL;
6802-}
6803-
6804-static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino)
6805-{
6806-	struct jffs2_inodirty *new;
6807-
6808-	/* Schedule delayed write-buffer write-out */
6809-	jffs2_dirty_trigger(c);
6810-
6811-	if (jffs2_wbuf_pending_for_ino(c, ino))
6812-		return;
6813-
6814-	new = kmalloc(sizeof(*new), GFP_KERNEL);
6815-	if (!new) {
6816-		jffs2_dbg(1, "No memory to allocate inodirty. Fallback to all considered dirty\n");
6817-		jffs2_clear_wbuf_ino_list(c);
6818-		c->wbuf_inodes = &inodirty_nomem;
6819-		return;
6820-	}
6821-	new->ino = ino;
6822-	new->next = c->wbuf_inodes;
6823-	c->wbuf_inodes = new;
6824-	return;
6825-}
6826-
6827-static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c)
6828-{
6829-	struct list_head *this, *next;
6830-	static int n;
6831-
6832-	if (list_empty(&c->erasable_pending_wbuf_list))
6833-		return;
6834-
6835-	list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) {
6836-		struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
6837-
6838-		jffs2_dbg(1, "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n",
6839-			  jeb->offset);
6840-		list_del(this);
6841-		if ((jiffies + (n++)) & 127) {
6842-			/* Most of the time, we just erase it immediately. Otherwise we
6843-			   spend ages scanning it on mount, etc. */
6844-			jffs2_dbg(1, "...and adding to erase_pending_list\n");
6845-			list_add_tail(&jeb->list, &c->erase_pending_list);
6846-			c->nr_erasing_blocks++;
6847-			jffs2_garbage_collect_trigger(c);
6848-		} else {
6849-			/* Sometimes, however, we leave it elsewhere so it doesn't get
6850-			   immediately reused, and we spread the load a bit. */
6851-			jffs2_dbg(1, "...and adding to erasable_list\n");
6852-			list_add_tail(&jeb->list, &c->erasable_list);
6853-		}
6854-	}
6855-}
6856-
6857-#define REFILE_NOTEMPTY 0
6858-#define REFILE_ANYWAY   1
6859-
6860-static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty)
6861-{
6862-	jffs2_dbg(1, "About to refile bad block at %08x\n", jeb->offset);
6863-
6864-	/* File the existing block on the bad_used_list.... */
6865-	if (c->nextblock == jeb)
6866-		c->nextblock = NULL;
6867-	else /* Not sure this should ever happen... need more coffee */
6868-		list_del(&jeb->list);
6869-	if (jeb->first_node) {
6870-		jffs2_dbg(1, "Refiling block at %08x to bad_used_list\n",
6871-			  jeb->offset);
6872-		list_add(&jeb->list, &c->bad_used_list);
6873-	} else {
6874-		BUG_ON(allow_empty == REFILE_NOTEMPTY);
6875-		/* It has to have had some nodes or we couldn't be here */
6876-		jffs2_dbg(1, "Refiling block at %08x to erase_pending_list\n",
6877-			  jeb->offset);
6878-		list_add(&jeb->list, &c->erase_pending_list);
6879-		c->nr_erasing_blocks++;
6880-		jffs2_garbage_collect_trigger(c);
6881-	}
6882-
6883-	if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) {
6884-		uint32_t oldfree = jeb->free_size;
6885-
6886-		jffs2_link_node_ref(c, jeb,
6887-				    (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE,
6888-				    oldfree, NULL);
6889-		/* convert to wasted */
6890-		c->wasted_size += oldfree;
6891-		jeb->wasted_size += oldfree;
6892-		c->dirty_size -= oldfree;
6893-		jeb->dirty_size -= oldfree;
6894-	}
6895-
6896-	jffs2_dbg_dump_block_lists_nolock(c);
6897-	jffs2_dbg_acct_sanity_check_nolock(c,jeb);
6898-	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
6899-}
6900-
6901-static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c,
6902-							    struct jffs2_inode_info *f,
6903-							    struct jffs2_raw_node_ref *raw,
6904-							    union jffs2_node_union *node)
6905-{
6906-	struct jffs2_node_frag *frag;
6907-	struct jffs2_full_dirent *fd;
6908-
6909-	dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n",
6910-		    node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype));
6911-
6912-	BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 &&
6913-	       je16_to_cpu(node->u.magic) != 0);
6914-
6915-	switch (je16_to_cpu(node->u.nodetype)) {
6916-	case JFFS2_NODETYPE_INODE:
6917-		if (f->metadata && f->metadata->raw == raw) {
6918-			dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata);
6919-			return &f->metadata->raw;
6920-		}
6921-		frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset));
6922-		BUG_ON(!frag);
6923-		/* Find a frag which refers to the full_dnode we want to modify */
6924-		while (!frag->node || frag->node->raw != raw) {
6925-			frag = frag_next(frag);
6926-			BUG_ON(!frag);
6927-		}
6928-		dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node);
6929-		return &frag->node->raw;
6930-
6931-	case JFFS2_NODETYPE_DIRENT:
6932-		for (fd = f->dents; fd; fd = fd->next) {
6933-			if (fd->raw == raw) {
6934-				dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd);
6935-				return &fd->raw;
6936-			}
6937-		}
6938-		BUG();
6939-
6940-	default:
6941-		dbg_noderef("Don't care about replacing raw for nodetype %x\n",
6942-			    je16_to_cpu(node->u.nodetype));
6943-		break;
6944-	}
6945-	return NULL;
6946-}
6947-
6948-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
6949-static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf,
6950-			      uint32_t ofs)
6951-{
6952-	int ret;
6953-	size_t retlen;
6954-	char *eccstr;
6955-
6956-	ret = mtd_read(c->mtd, ofs, c->wbuf_pagesize, &retlen, c->wbuf_verify);
6957-	if (ret && ret != -EUCLEAN && ret != -EBADMSG) {
6958-		pr_warn("%s(): Read back of page at %08x failed: %d\n",
6959-			__func__, c->wbuf_ofs, ret);
6960-		return ret;
6961-	} else if (retlen != c->wbuf_pagesize) {
6962-		pr_warn("%s(): Read back of page at %08x gave short read: %zd not %d\n",
6963-			__func__, ofs, retlen, c->wbuf_pagesize);
6964-		return -EIO;
6965-	}
6966-	if (!memcmp(buf, c->wbuf_verify, c->wbuf_pagesize))
6967-		return 0;
6968-
6969-	if (ret == -EUCLEAN)
6970-		eccstr = "corrected";
6971-	else if (ret == -EBADMSG)
6972-		eccstr = "correction failed";
6973-	else
6974-		eccstr = "OK or unused";
6975-
6976-	pr_warn("Write verify error (ECC %s) at %08x. Wrote:\n",
6977-		eccstr, c->wbuf_ofs);
6978-	print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1,
6979-		       c->wbuf, c->wbuf_pagesize, 0);
6980-
6981-	pr_warn("Read back:\n");
6982-	print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1,
6983-		       c->wbuf_verify, c->wbuf_pagesize, 0);
6984-
6985-	return -EIO;
6986-}
6987-#else
6988-#define jffs2_verify_write(c,b,o) (0)
6989-#endif
6990-
6991-/* Recover from failure to write wbuf. Recover the nodes up to the
6992- * wbuf, not the one which we were starting to try to write. */
6993-
6994-static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
6995-{
6996-	struct jffs2_eraseblock *jeb, *new_jeb;
6997-	struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL;
6998-	size_t retlen;
6999-	int ret;
7000-	int nr_refile = 0;
7001-	unsigned char *buf;
7002-	uint32_t start, end, ofs, len;
7003-
7004-	jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
7005-
7006-	spin_lock(&c->erase_completion_lock);
7007-	if (c->wbuf_ofs % c->mtd->erasesize)
7008-		jffs2_block_refile(c, jeb, REFILE_NOTEMPTY);
7009-	else
7010-		jffs2_block_refile(c, jeb, REFILE_ANYWAY);
7011-	spin_unlock(&c->erase_completion_lock);
7012-
7013-	BUG_ON(!ref_obsolete(jeb->last_node));
7014-
7015-	/* Find the first node to be recovered, by skipping over every
7016-	   node which ends before the wbuf starts, or which is obsolete. */
7017-	for (next = raw = jeb->first_node; next; raw = next) {
7018-		next = ref_next(raw);
7019-
7020-		if (ref_obsolete(raw) ||
7021-		    (next && ref_offset(next) <= c->wbuf_ofs)) {
7022-			dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
7023-				    ref_offset(raw), ref_flags(raw),
7024-				    (ref_offset(raw) + ref_totlen(c, jeb, raw)),
7025-				    c->wbuf_ofs);
7026-			continue;
7027-		}
7028-		dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n",
7029-			    ref_offset(raw), ref_flags(raw),
7030-			    (ref_offset(raw) + ref_totlen(c, jeb, raw)));
7031-
7032-		first_raw = raw;
7033-		break;
7034-	}
7035-
7036-	if (!first_raw) {
7037-		/* All nodes were obsolete. Nothing to recover. */
7038-		jffs2_dbg(1, "No non-obsolete nodes to be recovered. Just filing block bad\n");
7039-		c->wbuf_len = 0;
7040-		return;
7041-	}
7042-
7043-	start = ref_offset(first_raw);
7044-	end = ref_offset(jeb->last_node);
7045-	nr_refile = 1;
7046-
7047-	/* Count the number of refs which need to be copied */
7048-	while ((raw = ref_next(raw)) != jeb->last_node)
7049-		nr_refile++;
7050-
7051-	dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
7052-		    start, end, end - start, nr_refile);
7053-
7054-	buf = NULL;
7055-	if (start < c->wbuf_ofs) {
7056-		/* First affected node was already partially written.
7057-		 * Attempt to reread the old data into our buffer. */
7058-
7059-		buf = kmalloc(end - start, GFP_KERNEL);
7060-		if (!buf) {
7061-			pr_crit("Malloc failure in wbuf recovery. Data loss ensues.\n");
7062-
7063-			goto read_failed;
7064-		}
7065-
7066-		/* Do the read... */
7067-		ret = mtd_read(c->mtd, start, c->wbuf_ofs - start, &retlen,
7068-			       buf);
7069-
7070-		/* ECC recovered ? */
7071-		if ((ret == -EUCLEAN || ret == -EBADMSG) &&
7072-		    (retlen == c->wbuf_ofs - start))
7073-			ret = 0;
7074-
7075-		if (ret || retlen != c->wbuf_ofs - start) {
7076-			pr_crit("Old data are already lost in wbuf recovery. Data loss ensues.\n");
7077-
7078-			kfree(buf);
7079-			buf = NULL;
7080-		read_failed:
7081-			first_raw = ref_next(first_raw);
7082-			nr_refile--;
7083-			while (first_raw && ref_obsolete(first_raw)) {
7084-				first_raw = ref_next(first_raw);
7085-				nr_refile--;
7086-			}
7087-
7088-			/* If this was the only node to be recovered, give up */
7089-			if (!first_raw) {
7090-				c->wbuf_len = 0;
7091-				return;
7092-			}
7093-
7094-			/* It wasn't. Go on and try to recover nodes complete in the wbuf */
7095-			start = ref_offset(first_raw);
7096-			dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
7097-				    start, end, end - start, nr_refile);
7098-
7099-		} else {
7100-			/* Read succeeded. Copy the remaining data from the wbuf */
7101-			memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
7102-		}
7103-	}
7104-	/* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
7105-	   Either 'buf' contains the data, or we find it in the wbuf */
7106-
7107-	/* ... and get an allocation of space from a shiny new block instead */
7108-	ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE);
7109-	if (ret) {
7110-		pr_warn("Failed to allocate space for wbuf recovery. Data loss ensues.\n");
7111-		kfree(buf);
7112-		return;
7113-	}
7114-
7115-	/* The summary is not recovered, so it must be disabled for this erase block */
7116-	jffs2_sum_disable_collecting(c->summary);
7117-
7118-	ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile);
7119-	if (ret) {
7120-		pr_warn("Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
7121-		kfree(buf);
7122-		return;
7123-	}
7124-
7125-	ofs = write_ofs(c);
7126-
7127-	if (end-start >= c->wbuf_pagesize) {
7128-		/* Need to do another write immediately, but it's possible
7129-		   that this is just because the wbuf itself is completely
7130-		   full, and there's nothing earlier read back from the
7131-		   flash. Hence 'buf' isn't necessarily what we're writing
7132-		   from. */
7133-		unsigned char *rewrite_buf = buf?:c->wbuf;
7134-		uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);
7135-
7136-		jffs2_dbg(1, "Write 0x%x bytes at 0x%08x in wbuf recover\n",
7137-			  towrite, ofs);
7138-
7139-#ifdef BREAKMEHEADER
7140-		static int breakme;
7141-		if (breakme++ == 20) {
7142-			pr_notice("Faking write error at 0x%08x\n", ofs);
7143-			breakme = 0;
7144-			mtd_write(c->mtd, ofs, towrite, &retlen, brokenbuf);
7145-			ret = -EIO;
7146-		} else
7147-#endif
7148-			ret = mtd_write(c->mtd, ofs, towrite, &retlen,
7149-					rewrite_buf);
7150-
7151-		if (ret || retlen != towrite || jffs2_verify_write(c, rewrite_buf, ofs)) {
7152-			/* Argh. We tried. Really we did. */
7153-			pr_crit("Recovery of wbuf failed due to a second write error\n");
7154-			kfree(buf);
7155-
7156-			if (retlen)
7157-				jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
7158-
7159-			return;
7160-		}
7161-		pr_notice("Recovery of wbuf succeeded to %08x\n", ofs);
7162-
7163-		c->wbuf_len = (end - start) - towrite;
7164-		c->wbuf_ofs = ofs + towrite;
7165-		memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
7166-		/* Don't muck about with c->wbuf_inodes. False positives are harmless. */
7167-	} else {
7168-		/* OK, now we're left with the dregs in whichever buffer we're using */
7169-		if (buf) {
7170-			memcpy(c->wbuf, buf, end-start);
7171-		} else {
7172-			memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
7173-		}
7174-		c->wbuf_ofs = ofs;
7175-		c->wbuf_len = end - start;
7176-	}
7177-
7178-	/* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */
7179-	new_jeb = &c->blocks[ofs / c->sector_size];
7180-
7181-	spin_lock(&c->erase_completion_lock);
7182-	for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) {
7183-		uint32_t rawlen = ref_totlen(c, jeb, raw);
7184-		struct jffs2_inode_cache *ic;
7185-		struct jffs2_raw_node_ref *new_ref;
7186-		struct jffs2_raw_node_ref **adjust_ref = NULL;
7187-		struct jffs2_inode_info *f = NULL;
7188-
7189-		jffs2_dbg(1, "Refiling block of %08x at %08x(%d) to %08x\n",
7190-			  rawlen, ref_offset(raw), ref_flags(raw), ofs);
7191-
7192-		ic = jffs2_raw_ref_to_ic(raw);
7193-
7194-		/* Ick. This XATTR mess should be fixed shortly... */
7195-		if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) {
7196-			struct jffs2_xattr_datum *xd = (void *)ic;
7197-			BUG_ON(xd->node != raw);
7198-			adjust_ref = &xd->node;
7199-			raw->next_in_ino = NULL;
7200-			ic = NULL;
7201-		} else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) {
7202-			struct jffs2_xattr_datum *xr = (void *)ic;
7203-			BUG_ON(xr->node != raw);
7204-			adjust_ref = &xr->node;
7205-			raw->next_in_ino = NULL;
7206-			ic = NULL;
7207-		} else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) {
7208-			struct jffs2_raw_node_ref **p = &ic->nodes;
7209-
7210-			/* Remove the old node from the per-inode list */
7211-			while (*p && *p != (void *)ic) {
7212-				if (*p == raw) {
7213-					(*p) = (raw->next_in_ino);
7214-					raw->next_in_ino = NULL;
7215-					break;
7216-				}
7217-				p = &((*p)->next_in_ino);
7218-			}
7219-
7220-			if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) {
7221-				/* If it's an in-core inode, then we have to adjust any
7222-				   full_dirent or full_dnode structure to point to the
7223-				   new version instead of the old */
7224-				f = jffs2_gc_fetch_inode(c, ic->ino, !ic->pino_nlink);
7225-				if (IS_ERR(f)) {
7226-					/* Should never happen; it _must_ be present */
7227-					JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n",
7228-						    ic->ino, PTR_ERR(f));
7229-					BUG();
7230-				}
7231-				/* We don't lock f->sem. There's a number of ways we could
7232-				   end up in here with it already being locked, and nobody's
7233-				   going to modify it on us anyway because we hold the
7234-				   alloc_sem. We're only changing one ->raw pointer too,
7235-				   which we can get away with without upsetting readers. */
7236-				adjust_ref = jffs2_incore_replace_raw(c, f, raw,
7237-								      (void *)(buf?:c->wbuf) + (ref_offset(raw) - start));
7238-			} else if (unlikely(ic->state != INO_STATE_PRESENT &&
7239-					    ic->state != INO_STATE_CHECKEDABSENT &&
7240-					    ic->state != INO_STATE_GC)) {
7241-				JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state);
7242-				BUG();
7243-			}
7244-		}
7245-
7246-		new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic);
7247-
7248-		if (adjust_ref) {
7249-			BUG_ON(*adjust_ref != raw);
7250-			*adjust_ref = new_ref;
7251-		}
7252-		if (f)
7253-			jffs2_gc_release_inode(c, f);
7254-
7255-		if (!ref_obsolete(raw)) {
7256-			jeb->dirty_size += rawlen;
7257-			jeb->used_size  -= rawlen;
7258-			c->dirty_size += rawlen;
7259-			c->used_size -= rawlen;
7260-			raw->flash_offset = ref_offset(raw) | REF_OBSOLETE;
7261-			BUG_ON(raw->next_in_ino);
7262-		}
7263-		ofs += rawlen;
7264-	}
7265-
7266-	kfree(buf);
7267-
7268-	/* Fix up the original jeb now it's on the bad_list */
7269-	if (first_raw == jeb->first_node) {
7270-		jffs2_dbg(1, "Failing block at %08x is now empty. Moving to erase_pending_list\n",
7271-			  jeb->offset);
7272-		list_move(&jeb->list, &c->erase_pending_list);
7273-		c->nr_erasing_blocks++;
7274-		jffs2_garbage_collect_trigger(c);
7275-	}
7276-
7277-	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
7278-	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
7279-
7280-	jffs2_dbg_acct_sanity_check_nolock(c, new_jeb);
7281-	jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb);
7282-
7283-	spin_unlock(&c->erase_completion_lock);
7284-
7285-	jffs2_dbg(1, "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n",
7286-		  c->wbuf_ofs, c->wbuf_len);
7287-
7288-}
7289-
7290-/* Meaning of pad argument:
7291-   0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway.
7292-   1: Pad, do not adjust nextblock free_size
7293-   2: Pad, adjust nextblock free_size
7294-*/
7295-#define NOPAD		0
7296-#define PAD_NOACCOUNT	1
7297-#define PAD_ACCOUNTING	2
7298-
7299-static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
7300-{
7301-	struct jffs2_eraseblock *wbuf_jeb;
7302-	int ret;
7303-	size_t retlen;
7304-
7305-	/* Nothing to do if not write-buffering the flash. In particular, we shouldn't
7306-	   del_timer() the timer we never initialised. */
7307-	if (!jffs2_is_writebuffered(c))
7308-		return 0;
7309-
7310-	if (!mutex_is_locked(&c->alloc_sem)) {
7311-		pr_crit("jffs2_flush_wbuf() called with alloc_sem not locked!\n");
7312-		BUG();
7313-	}
7314-
7315-	if (!c->wbuf_len)	/* already checked c->wbuf above */
7316-		return 0;
7317-
7318-	wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
7319-	if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1))
7320-		return -ENOMEM;
7321-
7322-	/* claim remaining space on the page
7323-	   this happens, if we have a change to a new block,
7324-	   or if fsync forces us to flush the writebuffer.
7325-	   if we have a switch to next page, we will not have
7326-	   enough remaining space for this.
7327-	*/
7328-	if (pad ) {
7329-		c->wbuf_len = PAD(c->wbuf_len);
7330-
7331-		/* Pad with JFFS2_DIRTY_BITMASK initially.  this helps out ECC'd NOR
7332-		   with 8 byte page size */
7333-		memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
7334-
7335-		if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) {
7336-			struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
7337-			padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
7338-			padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
7339-			padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
7340-			padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
7341-		}
7342-	}
7343-	/* else jffs2_flash_writev has actually filled in the rest of the
7344-	   buffer for us, and will deal with the node refs etc. later. */
7345-
7346-#ifdef BREAKME
7347-	static int breakme;
7348-	if (breakme++ == 20) {
7349-		pr_notice("Faking write error at 0x%08x\n", c->wbuf_ofs);
7350-		breakme = 0;
7351-		mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen,
7352-			  brokenbuf);
7353-		ret = -EIO;
7354-	} else
7355-#endif
7356-
7357-		ret = mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize,
7358-				&retlen, c->wbuf);
7359-
7360-	if (ret) {
7361-		pr_warn("jffs2_flush_wbuf(): Write failed with %d\n", ret);
7362-		goto wfail;
7363-	} else if (retlen != c->wbuf_pagesize) {
7364-		pr_warn("jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
7365-			retlen, c->wbuf_pagesize);
7366-		ret = -EIO;
7367-		goto wfail;
7368-	} else if ((ret = jffs2_verify_write(c, c->wbuf, c->wbuf_ofs))) {
7369-	wfail:
7370-		jffs2_wbuf_recover(c);
7371-
7372-		return ret;
7373-	}
7374-
7375-	/* Adjust free size of the block if we padded. */
7376-	if (pad) {
7377-		uint32_t waste = c->wbuf_pagesize - c->wbuf_len;
7378-
7379-		jffs2_dbg(1, "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
7380-			  (wbuf_jeb == c->nextblock) ? "next" : "",
7381-			  wbuf_jeb->offset);
7382-
7383-		/* wbuf_pagesize - wbuf_len is the amount of space that's to be
7384-		   padded. If there is less free space in the block than that,
7385-		   something screwed up */
7386-		if (wbuf_jeb->free_size < waste) {
7387-			pr_crit("jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
7388-				c->wbuf_ofs, c->wbuf_len, waste);
7389-			pr_crit("jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
7390-				wbuf_jeb->offset, wbuf_jeb->free_size);
7391-			BUG();
7392-		}
7393-
7394-		spin_lock(&c->erase_completion_lock);
7395-
7396-		jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL);
7397-		/* FIXME: that made it count as dirty. Convert to wasted */
7398-		wbuf_jeb->dirty_size -= waste;
7399-		c->dirty_size -= waste;
7400-		wbuf_jeb->wasted_size += waste;
7401-		c->wasted_size += waste;
7402-	} else
7403-		spin_lock(&c->erase_completion_lock);
7404-
7405-	/* Stick any now-obsoleted blocks on the erase_pending_list */
7406-	jffs2_refile_wbuf_blocks(c);
7407-	jffs2_clear_wbuf_ino_list(c);
7408-	spin_unlock(&c->erase_completion_lock);
7409-
7410-	memset(c->wbuf,0xff,c->wbuf_pagesize);
7411-	/* adjust write buffer offset, else we get a non contiguous write bug */
7412-	c->wbuf_ofs += c->wbuf_pagesize;
7413-	c->wbuf_len = 0;
7414-	return 0;
7415-}
7416-
7417-/* Trigger garbage collection to flush the write-buffer.
7418-   If ino arg is zero, do it if _any_ real (i.e. not GC) writes are
7419-   outstanding. If ino arg non-zero, do it only if a write for the
7420-   given inode is outstanding. */
7421-int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
7422-{
7423-	uint32_t old_wbuf_ofs;
7424-	uint32_t old_wbuf_len;
7425-	int ret = 0;
7426-
7427-	jffs2_dbg(1, "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino);
7428-
7429-	if (!c->wbuf)
7430-		return 0;
7431-
7432-	mutex_lock(&c->alloc_sem);
7433-	if (!jffs2_wbuf_pending_for_ino(c, ino)) {
7434-		jffs2_dbg(1, "Ino #%d not pending in wbuf. Returning\n", ino);
7435-		mutex_unlock(&c->alloc_sem);
7436-		return 0;
7437-	}
7438-
7439-	old_wbuf_ofs = c->wbuf_ofs;
7440-	old_wbuf_len = c->wbuf_len;
7441-
7442-	if (c->unchecked_size) {
7443-		/* GC won't make any progress for a while */
7444-		jffs2_dbg(1, "%s(): padding. Not finished checking\n",
7445-			  __func__);
7446-		down_write(&c->wbuf_sem);
7447-		ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7448-		/* retry flushing wbuf in case jffs2_wbuf_recover
7449-		   left some data in the wbuf */
7450-		if (ret)
7451-			ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7452-		up_write(&c->wbuf_sem);
7453-	} else while (old_wbuf_len &&
7454-		      old_wbuf_ofs == c->wbuf_ofs) {
7455-
7456-		mutex_unlock(&c->alloc_sem);
7457-
7458-		jffs2_dbg(1, "%s(): calls gc pass\n", __func__);
7459-
7460-		ret = jffs2_garbage_collect_pass(c);
7461-		if (ret) {
7462-			/* GC failed. Flush it with padding instead */
7463-			mutex_lock(&c->alloc_sem);
7464-			down_write(&c->wbuf_sem);
7465-			ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7466-			/* retry flushing wbuf in case jffs2_wbuf_recover
7467-			   left some data in the wbuf */
7468-			if (ret)
7469-				ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7470-			up_write(&c->wbuf_sem);
7471-			break;
7472-		}
7473-		mutex_lock(&c->alloc_sem);
7474-	}
7475-
7476-	jffs2_dbg(1, "%s(): ends...\n", __func__);
7477-
7478-	mutex_unlock(&c->alloc_sem);
7479-	return ret;
7480-}
7481-
7482-/* Pad write-buffer to end and write it, wasting space. */
7483-int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
7484-{
7485-	int ret;
7486-
7487-	if (!c->wbuf)
7488-		return 0;
7489-
7490-	down_write(&c->wbuf_sem);
7491-	ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
7492-	/* retry - maybe wbuf recover left some data in wbuf. */
7493-	if (ret)
7494-		ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
7495-	up_write(&c->wbuf_sem);
7496-
7497-	return ret;
7498-}
7499-
7500-static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf,
7501-			      size_t len)
7502-{
7503-	if (len && !c->wbuf_len && (len >= c->wbuf_pagesize))
7504-		return 0;
7505-
7506-	if (len > (c->wbuf_pagesize - c->wbuf_len))
7507-		len = c->wbuf_pagesize - c->wbuf_len;
7508-	memcpy(c->wbuf + c->wbuf_len, buf, len);
7509-	c->wbuf_len += (uint32_t) len;
7510-	return len;
7511-}
7512-
7513-int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs,
7514-		       unsigned long count, loff_t to, size_t *retlen,
7515-		       uint32_t ino)
7516-{
7517-	struct jffs2_eraseblock *jeb;
7518-	size_t wbuf_retlen, donelen = 0;
7519-	uint32_t outvec_to = to;
7520-	int ret, invec;
7521-
7522-	/* If not writebuffered flash, don't bother */
7523-	if (!jffs2_is_writebuffered(c))
7524-		return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
7525-
7526-	down_write(&c->wbuf_sem);
7527-
7528-	/* If wbuf_ofs is not initialized, set it to target address */
7529-	if (c->wbuf_ofs == 0xFFFFFFFF) {
7530-		c->wbuf_ofs = PAGE_DIV(to);
7531-		c->wbuf_len = PAGE_MOD(to);
7532-		memset(c->wbuf,0xff,c->wbuf_pagesize);
7533-	}
7534-
7535-	/*
7536-	 * Sanity checks on target address.  It's permitted to write
7537-	 * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to
7538-	 * write at the beginning of a new erase block. Anything else,
7539-	 * and you die.  New block starts at xxx000c (0-b = block
7540-	 * header)
7541-	 */
7542-	if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) {
7543-		/* It's a write to a new block */
7544-		if (c->wbuf_len) {
7545-			jffs2_dbg(1, "%s(): to 0x%lx causes flush of wbuf at 0x%08x\n",
7546-				  __func__, (unsigned long)to, c->wbuf_ofs);
7547-			ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
7548-			if (ret)
7549-				goto outerr;
7550-		}
7551-		/* set pointer to new block */
7552-		c->wbuf_ofs = PAGE_DIV(to);
7553-		c->wbuf_len = PAGE_MOD(to);
7554-	}
7555-
7556-	if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
7557-		/* We're not writing immediately after the writebuffer. Bad. */
7558-		pr_crit("%s(): Non-contiguous write to %08lx\n",
7559-			__func__, (unsigned long)to);
7560-		if (c->wbuf_len)
7561-			pr_crit("wbuf was previously %08x-%08x\n",
7562-				c->wbuf_ofs, c->wbuf_ofs + c->wbuf_len);
7563-		BUG();
7564-	}
7565-
7566-	/* adjust alignment offset */
7567-	if (c->wbuf_len != PAGE_MOD(to)) {
7568-		c->wbuf_len = PAGE_MOD(to);
7569-		/* take care of alignment to next page */
7570-		if (!c->wbuf_len) {
7571-			c->wbuf_len = c->wbuf_pagesize;
7572-			ret = __jffs2_flush_wbuf(c, NOPAD);
7573-			if (ret)
7574-				goto outerr;
7575-		}
7576-	}
7577-
7578-	for (invec = 0; invec < count; invec++) {
7579-		int vlen = invecs[invec].iov_len;
7580-		uint8_t *v = invecs[invec].iov_base;
7581-
7582-		wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
7583-
7584-		if (c->wbuf_len == c->wbuf_pagesize) {
7585-			ret = __jffs2_flush_wbuf(c, NOPAD);
7586-			if (ret)
7587-				goto outerr;
7588-		}
7589-		vlen -= wbuf_retlen;
7590-		outvec_to += wbuf_retlen;
7591-		donelen += wbuf_retlen;
7592-		v += wbuf_retlen;
7593-
7594-		if (vlen >= c->wbuf_pagesize) {
7595-			ret = mtd_write(c->mtd, outvec_to, PAGE_DIV(vlen),
7596-					&wbuf_retlen, v);
7597-			if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen))
7598-				goto outfile;
7599-
7600-			vlen -= wbuf_retlen;
7601-			outvec_to += wbuf_retlen;
7602-			c->wbuf_ofs = outvec_to;
7603-			donelen += wbuf_retlen;
7604-			v += wbuf_retlen;
7605-		}
7606-
7607-		wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
7608-		if (c->wbuf_len == c->wbuf_pagesize) {
7609-			ret = __jffs2_flush_wbuf(c, NOPAD);
7610-			if (ret)
7611-				goto outerr;
7612-		}
7613-
7614-		outvec_to += wbuf_retlen;
7615-		donelen += wbuf_retlen;
7616-	}
7617-
7618-	/*
7619-	 * If there's a remainder in the wbuf and it's a non-GC write,
7620-	 * remember that the wbuf affects this ino
7621-	 */
7622-	*retlen = donelen;
7623-
7624-	if (jffs2_sum_active()) {
7625-		int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to);
7626-		if (res)
7627-			return res;
7628-	}
7629-
7630-	if (c->wbuf_len && ino)
7631-		jffs2_wbuf_dirties_inode(c, ino);
7632-
7633-	ret = 0;
7634-	up_write(&c->wbuf_sem);
7635-	return ret;
7636-
7637-outfile:
7638-	/*
7639-	 * At this point we have no problem, c->wbuf is empty. However
7640-	 * refile nextblock to avoid writing again to same address.
7641-	 */
7642-
7643-	spin_lock(&c->erase_completion_lock);
7644-
7645-	jeb = &c->blocks[outvec_to / c->sector_size];
7646-	jffs2_block_refile(c, jeb, REFILE_ANYWAY);
7647-
7648-	spin_unlock(&c->erase_completion_lock);
7649-
7650-outerr:
7651-	*retlen = 0;
7652-	up_write(&c->wbuf_sem);
7653-	return ret;
7654-}
7655-
7656-/*
7657- *	This is the entry for flash write.
7658- *	Check, if we work on NAND FLASH, if so build an kvec and write it via vritev
7659-*/
7660-int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
7661-		      size_t *retlen, const u_char *buf)
7662-{
7663-	struct kvec vecs[1];
7664-
7665-	if (!jffs2_is_writebuffered(c))
7666-		return jffs2_flash_direct_write(c, ofs, len, retlen, buf);
7667-
7668-	vecs[0].iov_base = (unsigned char *) buf;
7669-	vecs[0].iov_len = len;
7670-	return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0);
7671-}
7672-
7673-/*
7674-	Handle readback from writebuffer and ECC failure return
7675-*/
7676-int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf)
7677-{
7678-	loff_t	orbf = 0, owbf = 0, lwbf = 0;
7679-	int	ret;
7680-
7681-	if (!jffs2_is_writebuffered(c))
7682-		return mtd_read(c->mtd, ofs, len, retlen, buf);
7683-
7684-	/* Read flash */
7685-	down_read(&c->wbuf_sem);
7686-	ret = mtd_read(c->mtd, ofs, len, retlen, buf);
7687-
7688-	if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {
7689-		if (ret == -EBADMSG)
7690-			pr_warn("mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
7691-				len, ofs);
7692-		/*
7693-		 * We have the raw data without ECC correction in the buffer,
7694-		 * maybe we are lucky and all data or parts are correct. We
7695-		 * check the node.  If data are corrupted node check will sort
7696-		 * it out.  We keep this block, it will fail on write or erase
7697-		 * and the we mark it bad. Or should we do that now? But we
7698-		 * should give him a chance.  Maybe we had a system crash or
7699-		 * power loss before the ecc write or a erase was completed.
7700-		 * So we return success. :)
7701-		 */
7702-		ret = 0;
7703-	}
7704-
7705-	/* if no writebuffer available or write buffer empty, return */
7706-	if (!c->wbuf_pagesize || !c->wbuf_len)
7707-		goto exit;
7708-
7709-	/* if we read in a different block, return */
7710-	if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs))
7711-		goto exit;
7712-
7713-	if (ofs >= c->wbuf_ofs) {
7714-		owbf = (ofs - c->wbuf_ofs);	/* offset in write buffer */
7715-		if (owbf > c->wbuf_len)		/* is read beyond write buffer ? */
7716-			goto exit;
7717-		lwbf = c->wbuf_len - owbf;	/* number of bytes to copy */
7718-		if (lwbf > len)
7719-			lwbf = len;
7720-	} else {
7721-		orbf = (c->wbuf_ofs - ofs);	/* offset in read buffer */
7722-		if (orbf > len)			/* is write beyond write buffer ? */
7723-			goto exit;
7724-		lwbf = len - orbf;		/* number of bytes to copy */
7725-		if (lwbf > c->wbuf_len)
7726-			lwbf = c->wbuf_len;
7727-	}
7728-	if (lwbf > 0)
7729-		memcpy(buf+orbf,c->wbuf+owbf,lwbf);
7730-
7731-exit:
7732-	up_read(&c->wbuf_sem);
7733-	return ret;
7734-}
7735-
7736-#define NR_OOB_SCAN_PAGES 4
7737-
7738-/* For historical reasons we use only 8 bytes for OOB clean marker */
7739-#define OOB_CM_SIZE 8
7740-
7741-static const struct jffs2_unknown_node oob_cleanmarker =
7742-{
7743-	.magic = constant_cpu_to_je16(JFFS2_MAGIC_BITMASK),
7744-	.nodetype = constant_cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
7745-	.totlen = constant_cpu_to_je32(8)
7746-};
7747-
7748-/*
7749- * Check, if the out of band area is empty. This function knows about the clean
7750- * marker and if it is present in OOB, treats the OOB as empty anyway.
7751- */
7752-int jffs2_check_oob_empty(struct jffs2_sb_info *c,
7753-			  struct jffs2_eraseblock *jeb, int mode)
7754-{
7755-	int i, ret;
7756-	int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
7757-	struct mtd_oob_ops ops;
7758-
7759-	ops.mode = MTD_OPS_AUTO_OOB;
7760-	ops.ooblen = NR_OOB_SCAN_PAGES * c->oobavail;
7761-	ops.oobbuf = c->oobbuf;
7762-	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
7763-	ops.datbuf = NULL;
7764-
7765-	ret = mtd_read_oob(c->mtd, jeb->offset, &ops);
7766-	if ((ret && !mtd_is_bitflip(ret)) || ops.oobretlen != ops.ooblen) {
7767-		pr_err("cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
7768-		       jeb->offset, ops.ooblen, ops.oobretlen, ret);
7769-		if (!ret || mtd_is_bitflip(ret))
7770-			ret = -EIO;
7771-		return ret;
7772-	}
7773-
7774-	for(i = 0; i < ops.ooblen; i++) {
7775-		if (mode && i < cmlen)
7776-			/* Yeah, we know about the cleanmarker */
7777-			continue;
7778-
7779-		if (ops.oobbuf[i] != 0xFF) {
7780-			jffs2_dbg(2, "Found %02x at %x in OOB for "
7781-				  "%08x\n", ops.oobbuf[i], i, jeb->offset);
7782-			return 1;
7783-		}
7784-	}
7785-
7786-	return 0;
7787-}
7788-
7789-/*
7790- * Check for a valid cleanmarker.
7791- * Returns: 0 if a valid cleanmarker was found
7792- *	    1 if no cleanmarker was found
7793- *	    negative error code if an error occurred
7794- */
7795-int jffs2_check_nand_cleanmarker(struct jffs2_sb_info *c,
7796-				 struct jffs2_eraseblock *jeb)
7797-{
7798-	struct mtd_oob_ops ops;
7799-	int ret, cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
7800-
7801-	ops.mode = MTD_OPS_AUTO_OOB;
7802-	ops.ooblen = cmlen;
7803-	ops.oobbuf = c->oobbuf;
7804-	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
7805-	ops.datbuf = NULL;
7806-
7807-	ret = mtd_read_oob(c->mtd, jeb->offset, &ops);
7808-	if ((ret && !mtd_is_bitflip(ret)) || ops.oobretlen != ops.ooblen) {
7809-		pr_err("cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
7810-		       jeb->offset, ops.ooblen, ops.oobretlen, ret);
7811-		if (!ret || mtd_is_bitflip(ret))
7812-			ret = -EIO;
7813-		return ret;
7814-	}
7815-
7816-	return !!memcmp(&oob_cleanmarker, c->oobbuf, cmlen);
7817-}
7818-
7819-int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
7820-				 struct jffs2_eraseblock *jeb)
7821-{
7822-	int ret;
7823-	struct mtd_oob_ops ops;
7824-	int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
7825-
7826-	ops.mode = MTD_OPS_AUTO_OOB;
7827-	ops.ooblen = cmlen;
7828-	ops.oobbuf = (uint8_t *)&oob_cleanmarker;
7829-	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
7830-	ops.datbuf = NULL;
7831-
7832-	ret = mtd_write_oob(c->mtd, jeb->offset, &ops);
7833-	if (ret || ops.oobretlen != ops.ooblen) {
7834-		pr_err("cannot write OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
7835-		       jeb->offset, ops.ooblen, ops.oobretlen, ret);
7836-		if (!ret)
7837-			ret = -EIO;
7838-		return ret;
7839-	}
7840-
7841-	return 0;
7842-}
7843-
7844-/*
7845- * On NAND we try to mark this block bad. If the block was erased more
7846- * than MAX_ERASE_FAILURES we mark it finally bad.
7847- * Don't care about failures. This block remains on the erase-pending
7848- * or badblock list as long as nobody manipulates the flash with
7849- * a bootloader or something like that.
7850- */
7851-
7852-int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
7853-{
7854-	int 	ret;
7855-
7856-	/* if the count is < max, we try to write the counter to the 2nd page oob area */
7857-	if( ++jeb->bad_count < MAX_ERASE_FAILURES)
7858-		return 0;
7859-
7860-	pr_warn("marking eraseblock at %08x as bad\n", bad_offset);
7861-	ret = mtd_block_markbad(c->mtd, bad_offset);
7862-
7863-	if (ret) {
7864-		jffs2_dbg(1, "%s(): Write failed for block at %08x: error %d\n",
7865-			  __func__, jeb->offset, ret);
7866-		return ret;
7867-	}
7868-	return 1;
7869-}
7870-
7871-static struct jffs2_sb_info *work_to_sb(struct work_struct *work)
7872-{
7873-	struct delayed_work *dwork;
7874-
7875-	dwork = to_delayed_work(work);
7876-	return container_of(dwork, struct jffs2_sb_info, wbuf_dwork);
7877-}
7878-
7879-static void delayed_wbuf_sync(struct work_struct *work)
7880-{
7881-	struct jffs2_sb_info *c = work_to_sb(work);
7882-	struct super_block *sb = OFNI_BS_2SFFJ(c);
7883-
7884-	if (!sb_rdonly(sb)) {
7885-		jffs2_dbg(1, "%s()\n", __func__);
7886-		jffs2_flush_wbuf_gc(c, 0);
7887-	}
7888-}
7889-
7890-void jffs2_dirty_trigger(struct jffs2_sb_info *c)
7891-{
7892-	struct super_block *sb = OFNI_BS_2SFFJ(c);
7893-	unsigned long delay;
7894-
7895-	if (sb_rdonly(sb))
7896-		return;
7897-
7898-	delay = msecs_to_jiffies(dirty_writeback_interval * 10);
7899-	if (queue_delayed_work(system_long_wq, &c->wbuf_dwork, delay))
7900-		jffs2_dbg(1, "%s()\n", __func__);
7901-}
7902-
7903-int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
7904-{
7905-	if (!c->mtd->oobsize)
7906-		return 0;
7907-
7908-	/* Cleanmarker is out-of-band, so inline size zero */
7909-	c->cleanmarker_size = 0;
7910-
7911-	if (c->mtd->oobavail == 0) {
7912-		pr_err("inconsistent device description\n");
7913-		return -EINVAL;
7914-	}
7915-
7916-	jffs2_dbg(1, "using OOB on NAND\n");
7917-
7918-	c->oobavail = c->mtd->oobavail;
7919-
7920-	/* Initialise write buffer */
7921-	init_rwsem(&c->wbuf_sem);
7922-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
7923-	c->wbuf_pagesize = c->mtd->writesize;
7924-	c->wbuf_ofs = 0xFFFFFFFF;
7925-
7926-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
7927-	if (!c->wbuf)
7928-		return -ENOMEM;
7929-
7930-	c->oobbuf = kmalloc_array(NR_OOB_SCAN_PAGES, c->oobavail, GFP_KERNEL);
7931-	if (!c->oobbuf) {
7932-		kfree(c->wbuf);
7933-		return -ENOMEM;
7934-	}
7935-
7936-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
7937-	c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
7938-	if (!c->wbuf_verify) {
7939-		kfree(c->oobbuf);
7940-		kfree(c->wbuf);
7941-		return -ENOMEM;
7942-	}
7943-#endif
7944-	return 0;
7945-}
7946-
7947-void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
7948-{
7949-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
7950-	kfree(c->wbuf_verify);
7951-#endif
7952-	kfree(c->wbuf);
7953-	kfree(c->oobbuf);
7954-}
7955-
7956-int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
7957-	c->cleanmarker_size = 0;		/* No cleanmarkers needed */
7958-
7959-	/* Initialize write buffer */
7960-	init_rwsem(&c->wbuf_sem);
7961-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
7962-	c->wbuf_pagesize =  c->mtd->erasesize;
7963-
7964-	/* Find a suitable c->sector_size
7965-	 * - Not too much sectors
7966-	 * - Sectors have to be at least 4 K + some bytes
7967-	 * - All known dataflashes have erase sizes of 528 or 1056
7968-	 * - we take at least 8 eraseblocks and want to have at least 8K size
7969-	 * - The concatenation should be a power of 2
7970-	*/
7971-
7972-	c->sector_size = 8 * c->mtd->erasesize;
7973-
7974-	while (c->sector_size < 8192) {
7975-		c->sector_size *= 2;
7976-	}
7977-
7978-	/* It may be necessary to adjust the flash size */
7979-	c->flash_size = c->mtd->size;
7980-
7981-	if ((c->flash_size % c->sector_size) != 0) {
7982-		c->flash_size = (c->flash_size / c->sector_size) * c->sector_size;
7983-		pr_warn("flash size adjusted to %dKiB\n", c->flash_size);
7984-	}
7985-
7986-	c->wbuf_ofs = 0xFFFFFFFF;
7987-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
7988-	if (!c->wbuf)
7989-		return -ENOMEM;
7990-
7991-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
7992-	c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
7993-	if (!c->wbuf_verify) {
7994-		kfree(c->wbuf);
7995-		return -ENOMEM;
7996-	}
7997-#endif
7998-
7999-	pr_info("write-buffering enabled buffer (%d) erasesize (%d)\n",
8000-		c->wbuf_pagesize, c->sector_size);
8001-
8002-	return 0;
8003-}
8004-
8005-void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) {
8006-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8007-	kfree(c->wbuf_verify);
8008-#endif
8009-	kfree(c->wbuf);
8010-}
8011-
8012-int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) {
8013-	/* Cleanmarker currently occupies whole programming regions,
8014-	 * either one or 2 for 8Byte STMicro flashes. */
8015-	c->cleanmarker_size = max(16u, c->mtd->writesize);
8016-
8017-	/* Initialize write buffer */
8018-	init_rwsem(&c->wbuf_sem);
8019-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
8020-
8021-	c->wbuf_pagesize = c->mtd->writesize;
8022-	c->wbuf_ofs = 0xFFFFFFFF;
8023-
8024-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8025-	if (!c->wbuf)
8026-		return -ENOMEM;
8027-
8028-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8029-	c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8030-	if (!c->wbuf_verify) {
8031-		kfree(c->wbuf);
8032-		return -ENOMEM;
8033-	}
8034-#endif
8035-	return 0;
8036-}
8037-
8038-void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) {
8039-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8040-	kfree(c->wbuf_verify);
8041-#endif
8042-	kfree(c->wbuf);
8043-}
8044-
8045-int jffs2_ubivol_setup(struct jffs2_sb_info *c) {
8046-	c->cleanmarker_size = 0;
8047-
8048-	if (c->mtd->writesize == 1)
8049-		/* We do not need write-buffer */
8050-		return 0;
8051-
8052-	init_rwsem(&c->wbuf_sem);
8053-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
8054-
8055-	c->wbuf_pagesize =  c->mtd->writesize;
8056-	c->wbuf_ofs = 0xFFFFFFFF;
8057-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8058-	if (!c->wbuf)
8059-		return -ENOMEM;
8060-
8061-	pr_info("write-buffering enabled buffer (%d) erasesize (%d)\n",
8062-		c->wbuf_pagesize, c->sector_size);
8063-
8064-	return 0;
8065-}
8066-
8067-void jffs2_ubivol_cleanup(struct jffs2_sb_info *c) {
8068-	kfree(c->wbuf);
8069-}
8070diff -Nupr old/fs/jffs2/write.c new/fs/jffs2/write.c
8071--- old/fs/jffs2/write.c	2022-05-09 17:22:53.000000000 +0800
8072+++ new/fs/jffs2/write.c	2022-05-09 20:07:33.520000000 +0800
8073@@ -9,16 +9,15 @@
8074  *
8075  */
8076
8077-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8078-
8079+#include <dirent.h>
8080 #include <linux/kernel.h>
8081 #include <linux/fs.h>
8082-#include <linux/crc32.h>
8083 #include <linux/pagemap.h>
8084-#include <linux/mtd/mtd.h>
8085+#include <linux/semaphore.h>
8086+#include "mtd_dev.h"
8087 #include "nodelist.h"
8088 #include "compr.h"
8089-
8090+#include "los_crc32.h"
8091
8092 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
8093 		       uint32_t mode, struct jffs2_raw_inode *ri)
8094@@ -30,8 +29,6 @@ int jffs2_do_new_inode(struct jffs2_sb_i
8095 		return -ENOMEM;
8096 	}
8097
8098-	memset(ic, 0, sizeof(*ic));
8099-
8100 	f->inocache = ic;
8101 	f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
8102 	f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
8103@@ -69,8 +66,10 @@ struct jffs2_full_dnode *jffs2_write_dno
8104 	int retried = 0;
8105 	unsigned long cnt = 2;
8106
8107-	D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
8108-		pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n");
8109+	D1(if (je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
8110+		printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode(), je32_to_cpu(ri->hdr_crc):%d, "
8111+			"crc32(0, ri, sizeof(struct jffs2_unknown_node) - 4):%d\n", je32_to_cpu(ri->hdr_crc),
8112+			crc32(0, ri, sizeof(struct jffs2_unknown_node) - 4));
8113 		BUG();
8114 	}
8115 	   );
8116@@ -172,8 +171,8 @@ struct jffs2_full_dnode *jffs2_write_dno
8117 	   beginning of a page and runs to the end of the file, or if
8118 	   it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
8119 	*/
8120-	if ((je32_to_cpu(ri->dsize) >= PAGE_SIZE) ||
8121-	    ( ((je32_to_cpu(ri->offset)&(PAGE_SIZE-1))==0) &&
8122+	if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
8123+	    ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
8124 	      (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) ==  je32_to_cpu(ri->isize)))) {
8125 		flash_ofs |= REF_PRISTINE;
8126 	} else {
8127@@ -219,11 +218,13 @@ struct jffs2_full_dirent *jffs2_write_di
8128 		  je32_to_cpu(rd->name_crc));
8129
8130 	D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
8131-		pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n");
8132+	    printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent(), je32_to_cpu(rd->hdr_crc):%d, "
8133+		    "crc32(0, rd, sizeof(struct jffs2_unknown_node) - 4):%d\n", je32_to_cpu(rd->hdr_crc),
8134+			crc32(0, rd, sizeof(struct jffs2_unknown_node) - 4));
8135 		BUG();
8136 	   });
8137
8138-	if (strnlen(name, namelen) != namelen) {
8139+	if (strnlen((const char *)name, namelen) != namelen) {
8140 		/* This should never happen, but seems to have done on at least one
8141 		   occasion: https://dev.laptop.org/ticket/4184 */
8142 		pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n");
8143@@ -245,7 +246,7 @@ struct jffs2_full_dirent *jffs2_write_di
8144
8145 	fd->version = je32_to_cpu(rd->version);
8146 	fd->ino = je32_to_cpu(rd->ino);
8147-	fd->nhash = full_name_hash(NULL, name, namelen);
8148+	fd->nhash = full_name_hash(name, namelen);
8149 	fd->type = rd->type;
8150 	memcpy(fd->name, name, namelen);
8151 	fd->name[namelen]=0;
8152@@ -343,10 +344,24 @@ int jffs2_write_inode_range(struct jffs2
8153 {
8154 	int ret = 0;
8155 	uint32_t writtenlen = 0;
8156+	unsigned char *bufRet = NULL;
8157+	unsigned char *bufRetBak = NULL;
8158
8159 	jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
8160 		  __func__, f->inocache->ino, offset, writelen);
8161
8162+	if (writelen > 0) {
8163+		bufRet = kmalloc(writelen, GFP_KERNEL);
8164+		if (bufRet == NULL) {
8165+			return -ENOMEM;
8166+		}
8167+		bufRetBak = bufRet;
8168+		if (LOS_CopyToKernel(bufRet, writelen, buf, writelen) != 0) {
8169+			kfree(bufRet);
8170+			return -EFAULT;
8171+		}
8172+	}
8173+
8174 	while(writelen) {
8175 		struct jffs2_full_dnode *fn;
8176 		unsigned char *comprbuf = NULL;
8177@@ -366,11 +381,10 @@ int jffs2_write_inode_range(struct jffs2
8178 			break;
8179 		}
8180 		mutex_lock(&f->sem);
8181-		datalen = min_t(uint32_t, writelen,
8182-				PAGE_SIZE - (offset & (PAGE_SIZE-1)));
8183+		datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
8184 		cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
8185
8186-		comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
8187+		comprtype = jffs2_compress(c, f, bufRet, &comprbuf, &datalen, &cdatalen);
8188
8189 		ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8190 		ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
8191@@ -390,7 +404,7 @@ int jffs2_write_inode_range(struct jffs2
8192
8193 		fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
8194
8195-		jffs2_free_comprbuf(comprbuf, buf);
8196+		jffs2_free_comprbuf(comprbuf, bufRet);
8197
8198 		if (IS_ERR(fn)) {
8199 			ret = PTR_ERR(fn);
8200@@ -432,15 +446,18 @@ int jffs2_write_inode_range(struct jffs2
8201 		writtenlen += datalen;
8202 		offset += datalen;
8203 		writelen -= datalen;
8204-		buf += datalen;
8205+		bufRet += datalen;
8206 	}
8207 	*retlen = writtenlen;
8208+	if (bufRetBak != NULL) {
8209+		kfree(bufRetBak);
8210+	}
8211 	return ret;
8212 }
8213
8214 int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
8215 		    struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
8216-		    const struct qstr *qstr)
8217+		    const char *name, int namelen)
8218 {
8219 	struct jffs2_raw_dirent *rd;
8220 	struct jffs2_full_dnode *fn;
8221@@ -468,7 +485,8 @@ int jffs2_do_create(struct jffs2_sb_info
8222 		  jemode_to_cpu(ri->mode));
8223
8224 	if (IS_ERR(fn)) {
8225-		jffs2_dbg(1, "jffs2_write_dnode() failed\n");
8226+		jffs2_dbg(1, "jffs2_write_dnode() failed,error:%ld\n",
8227+		    PTR_ERR(fn));
8228 		/* Eeek. Wave bye bye */
8229 		mutex_unlock(&f->sem);
8230 		jffs2_complete_reservation(c);
8231@@ -482,19 +500,12 @@ int jffs2_do_create(struct jffs2_sb_info
8232 	mutex_unlock(&f->sem);
8233 	jffs2_complete_reservation(c);
8234
8235-	ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
8236-	if (ret)
8237-		return ret;
8238-	ret = jffs2_init_acl_post(&f->vfs_inode);
8239-	if (ret)
8240-		return ret;
8241-
8242-	ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
8243-				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
8244+	ret = jffs2_reserve_space(c, sizeof(*rd)+ namelen, &alloclen,
8245+				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
8246
8247 	if (ret) {
8248 		/* Eep. */
8249-		jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
8250+		jffs2_dbg(1, "jffs2_reserve_space() for dirent failed,ret:%d\n",ret);
8251 		return ret;
8252 	}
8253
8254@@ -509,19 +520,19 @@ int jffs2_do_create(struct jffs2_sb_info
8255
8256 	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8257 	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
8258-	rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
8259+	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
8260 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
8261
8262 	rd->pino = cpu_to_je32(dir_f->inocache->ino);
8263 	rd->version = cpu_to_je32(++dir_f->highest_version);
8264 	rd->ino = ri->ino;
8265 	rd->mctime = ri->ctime;
8266-	rd->nsize = qstr->len;
8267+	rd->nsize = namelen;
8268 	rd->type = DT_REG;
8269 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
8270-	rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
8271+	rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
8272
8273-	fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
8274+	fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)name, namelen, ALLOC_NORMAL);
8275
8276 	jffs2_free_raw_dirent(rd);
8277
8278@@ -553,7 +564,7 @@ int jffs2_do_unlink(struct jffs2_sb_info
8279 	uint32_t alloclen;
8280 	int ret;
8281
8282-	if (!jffs2_can_mark_obsolete(c)) {
8283+	if (jffs2_can_mark_obsolete(c)) {
8284 		/* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
8285
8286 		rd = jffs2_alloc_raw_dirent();
8287@@ -584,7 +595,7 @@ int jffs2_do_unlink(struct jffs2_sb_info
8288 		rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
8289 		rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
8290
8291-		fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
8292+		fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)name, namelen, ALLOC_DELETION);
8293
8294 		jffs2_free_raw_dirent(rd);
8295
8296@@ -598,7 +609,7 @@ int jffs2_do_unlink(struct jffs2_sb_info
8297 		jffs2_add_fd_to_list(c, fd, &dir_f->dents);
8298 		mutex_unlock(&dir_f->sem);
8299 	} else {
8300-		uint32_t nhash = full_name_hash(NULL, name, namelen);
8301+		uint32_t nhash = full_name_hash((const unsigned char *)name, namelen);
8302
8303 		fd = dir_f->dents;
8304 		/* We don't actually want to reserve any space, but we do
8305@@ -703,7 +714,7 @@ int jffs2_do_link (struct jffs2_sb_info
8306 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
8307 	rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
8308
8309-	fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
8310+	fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)name, namelen, ALLOC_NORMAL);
8311
8312 	jffs2_free_raw_dirent(rd);
8313
8314diff -Nupr old/fs/jffs2/writev.c new/fs/jffs2/writev.c
8315--- old/fs/jffs2/writev.c	2022-05-09 17:22:53.000000000 +0800
8316+++ new/fs/jffs2/writev.c	2022-05-09 20:05:36.440000000 +0800
8317@@ -10,42 +10,97 @@
8318  */
8319
8320 #include <linux/kernel.h>
8321-#include <linux/mtd/mtd.h>
8322+#include "mtd_dev.h"
8323 #include "nodelist.h"
8324
8325 int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,
8326 			      unsigned long count, loff_t to, size_t *retlen)
8327 {
8328-	if (!jffs2_is_writebuffered(c)) {
8329-		if (jffs2_sum_active()) {
8330-			int res;
8331-			res = jffs2_sum_add_kvec(c, vecs, count, (uint32_t) to);
8332-			if (res) {
8333-				return res;
8334+	unsigned long i;
8335+	size_t totlen = 0, thislen;
8336+	int ret = 0;
8337+
8338+	for (i = 0; i < count; i++) {
8339+		// writes need to be aligned but the data we're passed may not be
8340+		// Observation suggests most unaligned writes are small, so we
8341+		// optimize for that case.
8342+
8343+		if (((vecs[i].iov_len & (sizeof(int) - 1))) ||
8344+		(((unsigned long) vecs[i].iov_base & (sizeof(unsigned long) - 1)))) {
8345+			// are there iov's after this one? Or is it so much we'd need
8346+			// to do multiple writes anyway?
8347+			if ((i + 1) < count || vecs[i].iov_len > 256) {
8348+				// cop out and malloc
8349+				unsigned long j;
8350+				size_t sizetomalloc = 0, totvecsize = 0;
8351+				char *cbuf, *cbufptr;
8352+
8353+				for (j = i; j < count; j++)
8354+					totvecsize += vecs[j].iov_len;
8355+
8356+				// pad up in case unaligned
8357+				sizetomalloc = totvecsize + sizeof(int) - 1;
8358+				sizetomalloc &= ~(sizeof(int) - 1);
8359+				cbuf = (char *) malloc(sizetomalloc);
8360+				// malloc returns aligned memory
8361+				if (!cbuf) {
8362+					ret = -ENOMEM;
8363+					goto writev_out;
8364+				}
8365+				cbufptr = cbuf;
8366+				for (j = i; j < count; j++) {
8367+					(void)memcpy_s(cbufptr, vecs[j].iov_len, vecs[j].iov_base, vecs[j].iov_len);
8368+					cbufptr += vecs[j].iov_len;
8369+				}
8370+				ret = jffs2_flash_write(c, to, sizetomalloc, &thislen,
8371+					(unsigned char *) cbuf);
8372+				if (thislen > totvecsize) // in case it was aligned up
8373+					thislen = totvecsize;
8374+				totlen += thislen;
8375+				free(cbuf);
8376+				goto writev_out;
8377+			} else {
8378+				// otherwise optimize for the common case
8379+				int buf[256/sizeof(int)]; // int, so int aligned
8380+				size_t lentowrite;
8381+
8382+				lentowrite = vecs[i].iov_len;
8383+				// pad up in case its unaligned
8384+				lentowrite += sizeof(int) - 1;
8385+				lentowrite &= ~(sizeof(int) - 1);
8386+				ret = memcpy_s(buf, sizeof(buf), vecs[i].iov_base, vecs[i].iov_len);
8387+				if (ret != EOK)
8388+					goto writev_out;
8389+
8390+				ret = jffs2_flash_write(c, to, lentowrite, &thislen,
8391+					(unsigned char *) &buf[0]);
8392+				if (thislen > vecs[i].iov_len)
8393+					thislen = vecs[i].iov_len;
8394 			}
8395+		} else {
8396+				ret = jffs2_flash_write(c, to, vecs[i].iov_len, &thislen,
8397+					vecs[i].iov_base);
8398 		}
8399+		totlen += thislen;
8400+		if (ret || thislen != vecs[i].iov_len) break;
8401+		to += vecs[i].iov_len;
8402 	}
8403
8404-	return mtd_writev(c->mtd, vecs, count, to, retlen);
8405+writev_out:
8406+	if (retlen) *retlen = totlen;
8407+
8408+	return ret;
8409 }
8410
8411 int jffs2_flash_direct_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
8412 			size_t *retlen, const u_char *buf)
8413 {
8414 	int ret;
8415-	ret = mtd_write(c->mtd, ofs, len, retlen, buf);
8416-
8417-	if (jffs2_sum_active()) {
8418-		struct kvec vecs[1];
8419-		int res;
8420-
8421-		vecs[0].iov_base = (unsigned char *) buf;
8422-		vecs[0].iov_len = len;
8423-
8424-		res = jffs2_sum_add_kvec(c, vecs, 1, (uint32_t) ofs);
8425-		if (res) {
8426-			return res;
8427-		}
8428+	ret = c->mtd->write(c->mtd, ofs, len, (char *)buf);
8429+	if (ret >= 0) {
8430+		*retlen = ret;
8431+		return 0;
8432 	}
8433+	*retlen = 0;
8434 	return ret;
8435 }
8436diff -Nupr old/fs/jffs2/xattr.c new/fs/jffs2/xattr.c
8437--- old/fs/jffs2/xattr.c	2022-05-09 17:15:24.360000000 +0800
8438+++ new/fs/jffs2/xattr.c	1970-01-01 08:00:00.000000000 +0800
8439@@ -1,1347 +0,0 @@
8440-/*
8441- * JFFS2 -- Journalling Flash File System, Version 2.
8442- *
8443- * Copyright © 2006  NEC Corporation
8444- *
8445- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
8446- *
8447- * For licensing information, see the file 'LICENCE' in this directory.
8448- *
8449- */
8450-
8451-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8452-
8453-#define JFFS2_XATTR_IS_CORRUPTED	1
8454-
8455-#include <linux/kernel.h>
8456-#include <linux/slab.h>
8457-#include <linux/fs.h>
8458-#include <linux/time.h>
8459-#include <linux/pagemap.h>
8460-#include <linux/highmem.h>
8461-#include <linux/crc32.h>
8462-#include <linux/jffs2.h>
8463-#include <linux/xattr.h>
8464-#include <linux/posix_acl_xattr.h>
8465-#include <linux/mtd/mtd.h>
8466-#include "nodelist.h"
8467-/* -------- xdatum related functions ----------------
8468- * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
8469- *   is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
8470- *   the index of the xattr name/value pair cache (c->xattrindex).
8471- * is_xattr_datum_unchecked(c, xd)
8472- *   returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
8473- *   unchecked, it returns 0.
8474- * unload_xattr_datum(c, xd)
8475- *   is used to release xattr name/value pair and detach from c->xattrindex.
8476- * reclaim_xattr_datum(c)
8477- *   is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
8478- *   memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
8479- *   is hard coded as 32KiB.
8480- * do_verify_xattr_datum(c, xd)
8481- *   is used to load the xdatum informations without name/value pair from the medium.
8482- *   It's necessary once, because those informations are not collected during mounting
8483- *   process when EBS is enabled.
8484- *   0 will be returned, if success. An negative return value means recoverable error, and
8485- *   positive return value means unrecoverable error. Thus, caller must remove this xdatum
8486- *   and xref when it returned positive value.
8487- * do_load_xattr_datum(c, xd)
8488- *   is used to load name/value pair from the medium.
8489- *   The meanings of return value is same as do_verify_xattr_datum().
8490- * load_xattr_datum(c, xd)
8491- *   is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
8492- *   If xd need to call do_verify_xattr_datum() at first, it's called before calling
8493- *   do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
8494- * save_xattr_datum(c, xd)
8495- *   is used to write xdatum to medium. xd->version will be incremented.
8496- * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
8497- *   is used to create new xdatum and write to medium.
8498- * unrefer_xattr_datum(c, xd)
8499- *   is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
8500- *   is set on xd->flags and chained xattr_dead_list or release it immediately.
8501- *   In the first case, the garbage collector release it later.
8502- * -------------------------------------------------- */
8503-static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
8504-{
8505-	int name_len = strlen(xname);
8506-
8507-	return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
8508-}
8509-
8510-static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8511-{
8512-	struct jffs2_raw_node_ref *raw;
8513-	int rc = 0;
8514-
8515-	spin_lock(&c->erase_completion_lock);
8516-	for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
8517-		if (ref_flags(raw) == REF_UNCHECKED) {
8518-			rc = 1;
8519-			break;
8520-		}
8521-	}
8522-	spin_unlock(&c->erase_completion_lock);
8523-	return rc;
8524-}
8525-
8526-static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8527-{
8528-	/* must be called under down_write(xattr_sem) */
8529-	D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
8530-	if (xd->xname) {
8531-		c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
8532-		kfree(xd->xname);
8533-	}
8534-
8535-	list_del_init(&xd->xindex);
8536-	xd->hashkey = 0;
8537-	xd->xname = NULL;
8538-	xd->xvalue = NULL;
8539-}
8540-
8541-static void reclaim_xattr_datum(struct jffs2_sb_info *c)
8542-{
8543-	/* must be called under down_write(xattr_sem) */
8544-	struct jffs2_xattr_datum *xd, *_xd;
8545-	uint32_t target, before;
8546-	static int index = 0;
8547-	int count;
8548-
8549-	if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
8550-		return;
8551-
8552-	before = c->xdatum_mem_usage;
8553-	target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
8554-	for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
8555-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
8556-			if (xd->flags & JFFS2_XFLAGS_HOT) {
8557-				xd->flags &= ~JFFS2_XFLAGS_HOT;
8558-			} else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
8559-				unload_xattr_datum(c, xd);
8560-			}
8561-			if (c->xdatum_mem_usage <= target)
8562-				goto out;
8563-		}
8564-		index = (index+1) % XATTRINDEX_HASHSIZE;
8565-	}
8566- out:
8567-	JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
8568-		     before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
8569-}
8570-
8571-static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8572-{
8573-	/* must be called under down_write(xattr_sem) */
8574-	struct jffs2_eraseblock *jeb;
8575-	struct jffs2_raw_node_ref *raw;
8576-	struct jffs2_raw_xattr rx;
8577-	size_t readlen;
8578-	uint32_t crc, offset, totlen;
8579-	int rc;
8580-
8581-	spin_lock(&c->erase_completion_lock);
8582-	offset = ref_offset(xd->node);
8583-	if (ref_flags(xd->node) == REF_PRISTINE)
8584-		goto complete;
8585-	spin_unlock(&c->erase_completion_lock);
8586-
8587-	rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
8588-	if (rc || readlen != sizeof(rx)) {
8589-		JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
8590-			      rc, sizeof(rx), readlen, offset);
8591-		return rc ? rc : -EIO;
8592-	}
8593-	crc = crc32(0, &rx, sizeof(rx) - 4);
8594-	if (crc != je32_to_cpu(rx.node_crc)) {
8595-		JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
8596-			    offset, je32_to_cpu(rx.hdr_crc), crc);
8597-		xd->flags |= JFFS2_XFLAGS_INVALID;
8598-		return JFFS2_XATTR_IS_CORRUPTED;
8599-	}
8600-	totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
8601-	if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
8602-	    || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
8603-	    || je32_to_cpu(rx.totlen) != totlen
8604-	    || je32_to_cpu(rx.xid) != xd->xid
8605-	    || je32_to_cpu(rx.version) != xd->version) {
8606-		JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
8607-			    "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
8608-			    offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
8609-			    je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
8610-			    je32_to_cpu(rx.totlen), totlen,
8611-			    je32_to_cpu(rx.xid), xd->xid,
8612-			    je32_to_cpu(rx.version), xd->version);
8613-		xd->flags |= JFFS2_XFLAGS_INVALID;
8614-		return JFFS2_XATTR_IS_CORRUPTED;
8615-	}
8616-	xd->xprefix = rx.xprefix;
8617-	xd->name_len = rx.name_len;
8618-	xd->value_len = je16_to_cpu(rx.value_len);
8619-	xd->data_crc = je32_to_cpu(rx.data_crc);
8620-
8621-	spin_lock(&c->erase_completion_lock);
8622- complete:
8623-	for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
8624-		jeb = &c->blocks[ref_offset(raw) / c->sector_size];
8625-		totlen = PAD(ref_totlen(c, jeb, raw));
8626-		if (ref_flags(raw) == REF_UNCHECKED) {
8627-			c->unchecked_size -= totlen; c->used_size += totlen;
8628-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
8629-		}
8630-		raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
8631-	}
8632-	spin_unlock(&c->erase_completion_lock);
8633-
8634-	/* unchecked xdatum is chained with c->xattr_unchecked */
8635-	list_del_init(&xd->xindex);
8636-
8637-	dbg_xattr("success on verifying xdatum (xid=%u, version=%u)\n",
8638-		  xd->xid, xd->version);
8639-
8640-	return 0;
8641-}
8642-
8643-static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8644-{
8645-	/* must be called under down_write(xattr_sem) */
8646-	char *data;
8647-	size_t readlen;
8648-	uint32_t crc, length;
8649-	int i, ret, retry = 0;
8650-
8651-	BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
8652-	BUG_ON(!list_empty(&xd->xindex));
8653- retry:
8654-	length = xd->name_len + 1 + xd->value_len;
8655-	data = kmalloc(length, GFP_KERNEL);
8656-	if (!data)
8657-		return -ENOMEM;
8658-
8659-	ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
8660-			       length, &readlen, data);
8661-
8662-	if (ret || length!=readlen) {
8663-		JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
8664-			      ret, length, readlen, ref_offset(xd->node));
8665-		kfree(data);
8666-		return ret ? ret : -EIO;
8667-	}
8668-
8669-	data[xd->name_len] = '\0';
8670-	crc = crc32(0, data, length);
8671-	if (crc != xd->data_crc) {
8672-		JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
8673-			      " at %#08x, read: 0x%08x calculated: 0x%08x\n",
8674-			      ref_offset(xd->node), xd->data_crc, crc);
8675-		kfree(data);
8676-		xd->flags |= JFFS2_XFLAGS_INVALID;
8677-		return JFFS2_XATTR_IS_CORRUPTED;
8678-	}
8679-
8680-	xd->flags |= JFFS2_XFLAGS_HOT;
8681-	xd->xname = data;
8682-	xd->xvalue = data + xd->name_len+1;
8683-
8684-	c->xdatum_mem_usage += length;
8685-
8686-	xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
8687-	i = xd->hashkey % XATTRINDEX_HASHSIZE;
8688-	list_add(&xd->xindex, &c->xattrindex[i]);
8689-	if (!retry) {
8690-		retry = 1;
8691-		reclaim_xattr_datum(c);
8692-		if (!xd->xname)
8693-			goto retry;
8694-	}
8695-
8696-	dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
8697-		  xd->xid, xd->xprefix, xd->xname);
8698-
8699-	return 0;
8700-}
8701-
8702-static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8703-{
8704-	/* must be called under down_write(xattr_sem);
8705-	 * rc < 0 : recoverable error, try again
8706-	 * rc = 0 : success
8707-	 * rc > 0 : Unrecoverable error, this node should be deleted.
8708-	 */
8709-	int rc = 0;
8710-
8711-	BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
8712-	if (xd->xname)
8713-		return 0;
8714-	if (xd->flags & JFFS2_XFLAGS_INVALID)
8715-		return JFFS2_XATTR_IS_CORRUPTED;
8716-	if (unlikely(is_xattr_datum_unchecked(c, xd)))
8717-		rc = do_verify_xattr_datum(c, xd);
8718-	if (!rc)
8719-		rc = do_load_xattr_datum(c, xd);
8720-	return rc;
8721-}
8722-
8723-static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8724-{
8725-	/* must be called under down_write(xattr_sem) */
8726-	struct jffs2_raw_xattr rx;
8727-	struct kvec vecs[2];
8728-	size_t length;
8729-	int rc, totlen;
8730-	uint32_t phys_ofs = write_ofs(c);
8731-
8732-	BUG_ON(!xd->xname);
8733-	BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
8734-
8735-	vecs[0].iov_base = &rx;
8736-	vecs[0].iov_len = sizeof(rx);
8737-	vecs[1].iov_base = xd->xname;
8738-	vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
8739-	totlen = vecs[0].iov_len + vecs[1].iov_len;
8740-
8741-	/* Setup raw-xattr */
8742-	memset(&rx, 0, sizeof(rx));
8743-	rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8744-	rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
8745-	rx.totlen = cpu_to_je32(PAD(totlen));
8746-	rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
8747-
8748-	rx.xid = cpu_to_je32(xd->xid);
8749-	rx.version = cpu_to_je32(++xd->version);
8750-	rx.xprefix = xd->xprefix;
8751-	rx.name_len = xd->name_len;
8752-	rx.value_len = cpu_to_je16(xd->value_len);
8753-	rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
8754-	rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
8755-
8756-	rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
8757-	if (rc || totlen != length) {
8758-		JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
8759-			      rc, totlen, length, phys_ofs);
8760-		rc = rc ? rc : -EIO;
8761-		if (length)
8762-			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
8763-
8764-		return rc;
8765-	}
8766-	/* success */
8767-	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
8768-
8769-	dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
8770-		  xd->xid, xd->version, xd->xprefix, xd->xname);
8771-
8772-	return 0;
8773-}
8774-
8775-static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
8776-						    int xprefix, const char *xname,
8777-						    const char *xvalue, int xsize)
8778-{
8779-	/* must be called under down_write(xattr_sem) */
8780-	struct jffs2_xattr_datum *xd;
8781-	uint32_t hashkey, name_len;
8782-	char *data;
8783-	int i, rc;
8784-
8785-	/* Search xattr_datum has same xname/xvalue by index */
8786-	hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
8787-	i = hashkey % XATTRINDEX_HASHSIZE;
8788-	list_for_each_entry(xd, &c->xattrindex[i], xindex) {
8789-		if (xd->hashkey==hashkey
8790-		    && xd->xprefix==xprefix
8791-		    && xd->value_len==xsize
8792-		    && !strcmp(xd->xname, xname)
8793-		    && !memcmp(xd->xvalue, xvalue, xsize)) {
8794-			atomic_inc(&xd->refcnt);
8795-			return xd;
8796-		}
8797-	}
8798-
8799-	/* Not found, Create NEW XATTR-Cache */
8800-	name_len = strlen(xname);
8801-
8802-	xd = jffs2_alloc_xattr_datum();
8803-	if (!xd)
8804-		return ERR_PTR(-ENOMEM);
8805-
8806-	data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
8807-	if (!data) {
8808-		jffs2_free_xattr_datum(xd);
8809-		return ERR_PTR(-ENOMEM);
8810-	}
8811-	strcpy(data, xname);
8812-	memcpy(data + name_len + 1, xvalue, xsize);
8813-
8814-	atomic_set(&xd->refcnt, 1);
8815-	xd->xid = ++c->highest_xid;
8816-	xd->flags |= JFFS2_XFLAGS_HOT;
8817-	xd->xprefix = xprefix;
8818-
8819-	xd->hashkey = hashkey;
8820-	xd->xname = data;
8821-	xd->xvalue = data + name_len + 1;
8822-	xd->name_len = name_len;
8823-	xd->value_len = xsize;
8824-	xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
8825-
8826-	rc = save_xattr_datum(c, xd);
8827-	if (rc) {
8828-		kfree(xd->xname);
8829-		jffs2_free_xattr_datum(xd);
8830-		return ERR_PTR(rc);
8831-	}
8832-
8833-	/* Insert Hash Index */
8834-	i = hashkey % XATTRINDEX_HASHSIZE;
8835-	list_add(&xd->xindex, &c->xattrindex[i]);
8836-
8837-	c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
8838-	reclaim_xattr_datum(c);
8839-
8840-	return xd;
8841-}
8842-
8843-static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8844-{
8845-	/* must be called under down_write(xattr_sem) */
8846-	if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
8847-		unload_xattr_datum(c, xd);
8848-		xd->flags |= JFFS2_XFLAGS_DEAD;
8849-		if (xd->node == (void *)xd) {
8850-			BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
8851-			jffs2_free_xattr_datum(xd);
8852-		} else {
8853-			list_add(&xd->xindex, &c->xattr_dead_list);
8854-		}
8855-		spin_unlock(&c->erase_completion_lock);
8856-
8857-		dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
8858-			  xd->xid, xd->version);
8859-	}
8860-}
8861-
8862-/* -------- xref related functions ------------------
8863- * verify_xattr_ref(c, ref)
8864- *   is used to load xref information from medium. Because summary data does not
8865- *   contain xid/ino, it's necessary to verify once while mounting process.
8866- * save_xattr_ref(c, ref)
8867- *   is used to write xref to medium. If delete marker is marked, it write
8868- *   a delete marker of xref into medium.
8869- * create_xattr_ref(c, ic, xd)
8870- *   is used to create a new xref and write to medium.
8871- * delete_xattr_ref(c, ref)
8872- *   is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
8873- *   and allows GC to reclaim those physical nodes.
8874- * jffs2_xattr_delete_inode(c, ic)
8875- *   is called to remove xrefs related to obsolete inode when inode is unlinked.
8876- * jffs2_xattr_free_inode(c, ic)
8877- *   is called to release xattr related objects when unmounting.
8878- * check_xattr_ref_inode(c, ic)
8879- *   is used to confirm inode does not have duplicate xattr name/value pair.
8880- * jffs2_xattr_do_crccheck_inode(c, ic)
8881- *   is used to force xattr data integrity check during the initial gc scan.
8882- * -------------------------------------------------- */
8883-static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
8884-{
8885-	struct jffs2_eraseblock *jeb;
8886-	struct jffs2_raw_node_ref *raw;
8887-	struct jffs2_raw_xref rr;
8888-	size_t readlen;
8889-	uint32_t crc, offset, totlen;
8890-	int rc;
8891-
8892-	spin_lock(&c->erase_completion_lock);
8893-	if (ref_flags(ref->node) != REF_UNCHECKED)
8894-		goto complete;
8895-	offset = ref_offset(ref->node);
8896-	spin_unlock(&c->erase_completion_lock);
8897-
8898-	rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
8899-	if (rc || sizeof(rr) != readlen) {
8900-		JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
8901-			      rc, sizeof(rr), readlen, offset);
8902-		return rc ? rc : -EIO;
8903-	}
8904-	/* obsolete node */
8905-	crc = crc32(0, &rr, sizeof(rr) - 4);
8906-	if (crc != je32_to_cpu(rr.node_crc)) {
8907-		JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
8908-			    offset, je32_to_cpu(rr.node_crc), crc);
8909-		return JFFS2_XATTR_IS_CORRUPTED;
8910-	}
8911-	if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
8912-	    || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
8913-	    || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
8914-		JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
8915-			    "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
8916-			    offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
8917-			    je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
8918-			    je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
8919-		return JFFS2_XATTR_IS_CORRUPTED;
8920-	}
8921-	ref->ino = je32_to_cpu(rr.ino);
8922-	ref->xid = je32_to_cpu(rr.xid);
8923-	ref->xseqno = je32_to_cpu(rr.xseqno);
8924-	if (ref->xseqno > c->highest_xseqno)
8925-		c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
8926-
8927-	spin_lock(&c->erase_completion_lock);
8928- complete:
8929-	for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
8930-		jeb = &c->blocks[ref_offset(raw) / c->sector_size];
8931-		totlen = PAD(ref_totlen(c, jeb, raw));
8932-		if (ref_flags(raw) == REF_UNCHECKED) {
8933-			c->unchecked_size -= totlen; c->used_size += totlen;
8934-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
8935-		}
8936-		raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
8937-	}
8938-	spin_unlock(&c->erase_completion_lock);
8939-
8940-	dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
8941-		  ref->ino, ref->xid, ref_offset(ref->node));
8942-	return 0;
8943-}
8944-
8945-static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
8946-{
8947-	/* must be called under down_write(xattr_sem) */
8948-	struct jffs2_raw_xref rr;
8949-	size_t length;
8950-	uint32_t xseqno, phys_ofs = write_ofs(c);
8951-	int ret;
8952-
8953-	rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8954-	rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
8955-	rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
8956-	rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
8957-
8958-	xseqno = (c->highest_xseqno += 2);
8959-	if (is_xattr_ref_dead(ref)) {
8960-		xseqno |= XREF_DELETE_MARKER;
8961-		rr.ino = cpu_to_je32(ref->ino);
8962-		rr.xid = cpu_to_je32(ref->xid);
8963-	} else {
8964-		rr.ino = cpu_to_je32(ref->ic->ino);
8965-		rr.xid = cpu_to_je32(ref->xd->xid);
8966-	}
8967-	rr.xseqno = cpu_to_je32(xseqno);
8968-	rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
8969-
8970-	ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
8971-	if (ret || sizeof(rr) != length) {
8972-		JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
8973-			      ret, sizeof(rr), length, phys_ofs);
8974-		ret = ret ? ret : -EIO;
8975-		if (length)
8976-			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
8977-
8978-		return ret;
8979-	}
8980-	/* success */
8981-	ref->xseqno = xseqno;
8982-	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
8983-
8984-	dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
8985-
8986-	return 0;
8987-}
8988-
8989-static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
8990-						struct jffs2_xattr_datum *xd)
8991-{
8992-	/* must be called under down_write(xattr_sem) */
8993-	struct jffs2_xattr_ref *ref;
8994-	int ret;
8995-
8996-	ref = jffs2_alloc_xattr_ref();
8997-	if (!ref)
8998-		return ERR_PTR(-ENOMEM);
8999-	ref->ic = ic;
9000-	ref->xd = xd;
9001-
9002-	ret = save_xattr_ref(c, ref);
9003-	if (ret) {
9004-		jffs2_free_xattr_ref(ref);
9005-		return ERR_PTR(ret);
9006-	}
9007-
9008-	/* Chain to inode */
9009-	ref->next = ic->xref;
9010-	ic->xref = ref;
9011-
9012-	return ref; /* success */
9013-}
9014-
9015-static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
9016-{
9017-	/* must be called under down_write(xattr_sem) */
9018-	struct jffs2_xattr_datum *xd;
9019-
9020-	xd = ref->xd;
9021-	ref->xseqno |= XREF_DELETE_MARKER;
9022-	ref->ino = ref->ic->ino;
9023-	ref->xid = ref->xd->xid;
9024-	spin_lock(&c->erase_completion_lock);
9025-	ref->next = c->xref_dead_list;
9026-	c->xref_dead_list = ref;
9027-	spin_unlock(&c->erase_completion_lock);
9028-
9029-	dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
9030-		  ref->ino, ref->xid, ref->xseqno);
9031-
9032-	unrefer_xattr_datum(c, xd);
9033-}
9034-
9035-void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9036-{
9037-	/* It's called from jffs2_evict_inode() on inode removing.
9038-	   When an inode with XATTR is removed, those XATTRs must be removed. */
9039-	struct jffs2_xattr_ref *ref, *_ref;
9040-
9041-	if (!ic || ic->pino_nlink > 0)
9042-		return;
9043-
9044-	down_write(&c->xattr_sem);
9045-	for (ref = ic->xref; ref; ref = _ref) {
9046-		_ref = ref->next;
9047-		delete_xattr_ref(c, ref);
9048-	}
9049-	ic->xref = NULL;
9050-	up_write(&c->xattr_sem);
9051-}
9052-
9053-void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9054-{
9055-	/* It's called from jffs2_free_ino_caches() until unmounting FS. */
9056-	struct jffs2_xattr_datum *xd;
9057-	struct jffs2_xattr_ref *ref, *_ref;
9058-
9059-	down_write(&c->xattr_sem);
9060-	for (ref = ic->xref; ref; ref = _ref) {
9061-		_ref = ref->next;
9062-		xd = ref->xd;
9063-		if (atomic_dec_and_test(&xd->refcnt)) {
9064-			unload_xattr_datum(c, xd);
9065-			jffs2_free_xattr_datum(xd);
9066-		}
9067-		jffs2_free_xattr_ref(ref);
9068-	}
9069-	ic->xref = NULL;
9070-	up_write(&c->xattr_sem);
9071-}
9072-
9073-static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9074-{
9075-	/* success of check_xattr_ref_inode() means that inode (ic) dose not have
9076-	 * duplicate name/value pairs. If duplicate name/value pair would be found,
9077-	 * one will be removed.
9078-	 */
9079-	struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
9080-	int rc = 0;
9081-
9082-	if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
9083-		return 0;
9084-	down_write(&c->xattr_sem);
9085- retry:
9086-	rc = 0;
9087-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9088-		if (!ref->xd->xname) {
9089-			rc = load_xattr_datum(c, ref->xd);
9090-			if (unlikely(rc > 0)) {
9091-				*pref = ref->next;
9092-				delete_xattr_ref(c, ref);
9093-				goto retry;
9094-			} else if (unlikely(rc < 0))
9095-				goto out;
9096-		}
9097-		for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
9098-			if (!cmp->xd->xname) {
9099-				ref->xd->flags |= JFFS2_XFLAGS_BIND;
9100-				rc = load_xattr_datum(c, cmp->xd);
9101-				ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
9102-				if (unlikely(rc > 0)) {
9103-					*pcmp = cmp->next;
9104-					delete_xattr_ref(c, cmp);
9105-					goto retry;
9106-				} else if (unlikely(rc < 0))
9107-					goto out;
9108-			}
9109-			if (ref->xd->xprefix == cmp->xd->xprefix
9110-			    && !strcmp(ref->xd->xname, cmp->xd->xname)) {
9111-				if (ref->xseqno > cmp->xseqno) {
9112-					*pcmp = cmp->next;
9113-					delete_xattr_ref(c, cmp);
9114-				} else {
9115-					*pref = ref->next;
9116-					delete_xattr_ref(c, ref);
9117-				}
9118-				goto retry;
9119-			}
9120-		}
9121-	}
9122-	ic->flags |= INO_FLAGS_XATTR_CHECKED;
9123- out:
9124-	up_write(&c->xattr_sem);
9125-
9126-	return rc;
9127-}
9128-
9129-void jffs2_xattr_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9130-{
9131-	check_xattr_ref_inode(c, ic);
9132-}
9133-
9134-/* -------- xattr subsystem functions ---------------
9135- * jffs2_init_xattr_subsystem(c)
9136- *   is used to initialize semaphore and list_head, and some variables.
9137- * jffs2_find_xattr_datum(c, xid)
9138- *   is used to lookup xdatum while scanning process.
9139- * jffs2_clear_xattr_subsystem(c)
9140- *   is used to release any xattr related objects.
9141- * jffs2_build_xattr_subsystem(c)
9142- *   is used to associate xdatum and xref while super block building process.
9143- * jffs2_setup_xattr_datum(c, xid, version)
9144- *   is used to insert xdatum while scanning process.
9145- * -------------------------------------------------- */
9146-void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
9147-{
9148-	int i;
9149-
9150-	for (i=0; i < XATTRINDEX_HASHSIZE; i++)
9151-		INIT_LIST_HEAD(&c->xattrindex[i]);
9152-	INIT_LIST_HEAD(&c->xattr_unchecked);
9153-	INIT_LIST_HEAD(&c->xattr_dead_list);
9154-	c->xref_dead_list = NULL;
9155-	c->xref_temp = NULL;
9156-
9157-	init_rwsem(&c->xattr_sem);
9158-	c->highest_xid = 0;
9159-	c->highest_xseqno = 0;
9160-	c->xdatum_mem_usage = 0;
9161-	c->xdatum_mem_threshold = 32 * 1024;	/* Default 32KB */
9162-}
9163-
9164-static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
9165-{
9166-	struct jffs2_xattr_datum *xd;
9167-	int i = xid % XATTRINDEX_HASHSIZE;
9168-
9169-	/* It's only used in scanning/building process. */
9170-	BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
9171-
9172-	list_for_each_entry(xd, &c->xattrindex[i], xindex) {
9173-		if (xd->xid==xid)
9174-			return xd;
9175-	}
9176-	return NULL;
9177-}
9178-
9179-void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
9180-{
9181-	struct jffs2_xattr_datum *xd, *_xd;
9182-	struct jffs2_xattr_ref *ref, *_ref;
9183-	int i;
9184-
9185-	for (ref=c->xref_temp; ref; ref = _ref) {
9186-		_ref = ref->next;
9187-		jffs2_free_xattr_ref(ref);
9188-	}
9189-
9190-	for (ref=c->xref_dead_list; ref; ref = _ref) {
9191-		_ref = ref->next;
9192-		jffs2_free_xattr_ref(ref);
9193-	}
9194-
9195-	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
9196-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
9197-			list_del(&xd->xindex);
9198-			kfree(xd->xname);
9199-			jffs2_free_xattr_datum(xd);
9200-		}
9201-	}
9202-
9203-	list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
9204-		list_del(&xd->xindex);
9205-		jffs2_free_xattr_datum(xd);
9206-	}
9207-	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
9208-		list_del(&xd->xindex);
9209-		jffs2_free_xattr_datum(xd);
9210-	}
9211-}
9212-
9213-#define XREF_TMPHASH_SIZE	(128)
9214-void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
9215-{
9216-	struct jffs2_xattr_ref *ref, *_ref;
9217-	struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
9218-	struct jffs2_xattr_datum *xd, *_xd;
9219-	struct jffs2_inode_cache *ic;
9220-	struct jffs2_raw_node_ref *raw;
9221-	int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
9222-	int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
9223-
9224-	BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
9225-
9226-	/* Phase.1 : Merge same xref */
9227-	for (i=0; i < XREF_TMPHASH_SIZE; i++)
9228-		xref_tmphash[i] = NULL;
9229-	for (ref=c->xref_temp; ref; ref=_ref) {
9230-		struct jffs2_xattr_ref *tmp;
9231-
9232-		_ref = ref->next;
9233-		if (ref_flags(ref->node) != REF_PRISTINE) {
9234-			if (verify_xattr_ref(c, ref)) {
9235-				BUG_ON(ref->node->next_in_ino != (void *)ref);
9236-				ref->node->next_in_ino = NULL;
9237-				jffs2_mark_node_obsolete(c, ref->node);
9238-				jffs2_free_xattr_ref(ref);
9239-				continue;
9240-			}
9241-		}
9242-
9243-		i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
9244-		for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
9245-			if (tmp->ino == ref->ino && tmp->xid == ref->xid)
9246-				break;
9247-		}
9248-		if (tmp) {
9249-			raw = ref->node;
9250-			if (ref->xseqno > tmp->xseqno) {
9251-				tmp->xseqno = ref->xseqno;
9252-				raw->next_in_ino = tmp->node;
9253-				tmp->node = raw;
9254-			} else {
9255-				raw->next_in_ino = tmp->node->next_in_ino;
9256-				tmp->node->next_in_ino = raw;
9257-			}
9258-			jffs2_free_xattr_ref(ref);
9259-			continue;
9260-		} else {
9261-			ref->next = xref_tmphash[i];
9262-			xref_tmphash[i] = ref;
9263-		}
9264-	}
9265-	c->xref_temp = NULL;
9266-
9267-	/* Phase.2 : Bind xref with inode_cache and xattr_datum */
9268-	for (i=0; i < XREF_TMPHASH_SIZE; i++) {
9269-		for (ref=xref_tmphash[i]; ref; ref=_ref) {
9270-			xref_count++;
9271-			_ref = ref->next;
9272-			if (is_xattr_ref_dead(ref)) {
9273-				ref->next = c->xref_dead_list;
9274-				c->xref_dead_list = ref;
9275-				xref_dead_count++;
9276-				continue;
9277-			}
9278-			/* At this point, ref->xid and ref->ino contain XID and inode number.
9279-			   ref->xd and ref->ic are not valid yet. */
9280-			xd = jffs2_find_xattr_datum(c, ref->xid);
9281-			ic = jffs2_get_ino_cache(c, ref->ino);
9282-			if (!xd || !ic || !ic->pino_nlink) {
9283-				dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
9284-					  ref->ino, ref->xid, ref->xseqno);
9285-				ref->xseqno |= XREF_DELETE_MARKER;
9286-				ref->next = c->xref_dead_list;
9287-				c->xref_dead_list = ref;
9288-				xref_orphan_count++;
9289-				continue;
9290-			}
9291-			ref->xd = xd;
9292-			ref->ic = ic;
9293-			atomic_inc(&xd->refcnt);
9294-			ref->next = ic->xref;
9295-			ic->xref = ref;
9296-		}
9297-	}
9298-
9299-	/* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
9300-	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
9301-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
9302-			xdatum_count++;
9303-			list_del_init(&xd->xindex);
9304-			if (!atomic_read(&xd->refcnt)) {
9305-				dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
9306-					  xd->xid, xd->version);
9307-				xd->flags |= JFFS2_XFLAGS_DEAD;
9308-				list_add(&xd->xindex, &c->xattr_unchecked);
9309-				xdatum_orphan_count++;
9310-				continue;
9311-			}
9312-			if (is_xattr_datum_unchecked(c, xd)) {
9313-				dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
9314-					  xd->xid, xd->version);
9315-				list_add(&xd->xindex, &c->xattr_unchecked);
9316-				xdatum_unchecked_count++;
9317-			}
9318-		}
9319-	}
9320-	/* build complete */
9321-	JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
9322-		     " (%u unchecked, %u orphan) and "
9323-		     "%u of xref (%u dead, %u orphan) found.\n",
9324-		     xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
9325-		     xref_count, xref_dead_count, xref_orphan_count);
9326-}
9327-
9328-struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
9329-						  uint32_t xid, uint32_t version)
9330-{
9331-	struct jffs2_xattr_datum *xd;
9332-
9333-	xd = jffs2_find_xattr_datum(c, xid);
9334-	if (!xd) {
9335-		xd = jffs2_alloc_xattr_datum();
9336-		if (!xd)
9337-			return ERR_PTR(-ENOMEM);
9338-		xd->xid = xid;
9339-		xd->version = version;
9340-		if (xd->xid > c->highest_xid)
9341-			c->highest_xid = xd->xid;
9342-		list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
9343-	}
9344-	return xd;
9345-}
9346-
9347-/* -------- xattr subsystem functions ---------------
9348- * xprefix_to_handler(xprefix)
9349- *   is used to translate xprefix into xattr_handler.
9350- * jffs2_listxattr(dentry, buffer, size)
9351- *   is an implementation of listxattr handler on jffs2.
9352- * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
9353- *   is an implementation of getxattr handler on jffs2.
9354- * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
9355- *   is an implementation of setxattr handler on jffs2.
9356- * -------------------------------------------------- */
9357-const struct xattr_handler *jffs2_xattr_handlers[] = {
9358-	&jffs2_user_xattr_handler,
9359-#ifdef CONFIG_JFFS2_FS_SECURITY
9360-	&jffs2_security_xattr_handler,
9361-#endif
9362-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
9363-	&posix_acl_access_xattr_handler,
9364-	&posix_acl_default_xattr_handler,
9365-#endif
9366-	&jffs2_trusted_xattr_handler,
9367-	NULL
9368-};
9369-
9370-static const struct xattr_handler *xprefix_to_handler(int xprefix) {
9371-	const struct xattr_handler *ret;
9372-
9373-	switch (xprefix) {
9374-	case JFFS2_XPREFIX_USER:
9375-		ret = &jffs2_user_xattr_handler;
9376-		break;
9377-#ifdef CONFIG_JFFS2_FS_SECURITY
9378-	case JFFS2_XPREFIX_SECURITY:
9379-		ret = &jffs2_security_xattr_handler;
9380-		break;
9381-#endif
9382-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
9383-	case JFFS2_XPREFIX_ACL_ACCESS:
9384-		ret = &posix_acl_access_xattr_handler;
9385-		break;
9386-	case JFFS2_XPREFIX_ACL_DEFAULT:
9387-		ret = &posix_acl_default_xattr_handler;
9388-		break;
9389-#endif
9390-	case JFFS2_XPREFIX_TRUSTED:
9391-		ret = &jffs2_trusted_xattr_handler;
9392-		break;
9393-	default:
9394-		ret = NULL;
9395-		break;
9396-	}
9397-	return ret;
9398-}
9399-
9400-ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
9401-{
9402-	struct inode *inode = d_inode(dentry);
9403-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9404-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9405-	struct jffs2_inode_cache *ic = f->inocache;
9406-	struct jffs2_xattr_ref *ref, **pref;
9407-	struct jffs2_xattr_datum *xd;
9408-	const struct xattr_handler *xhandle;
9409-	const char *prefix;
9410-	ssize_t prefix_len, len, rc;
9411-	int retry = 0;
9412-
9413-	rc = check_xattr_ref_inode(c, ic);
9414-	if (unlikely(rc))
9415-		return rc;
9416-
9417-	down_read(&c->xattr_sem);
9418- retry:
9419-	len = 0;
9420-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9421-		BUG_ON(ref->ic != ic);
9422-		xd = ref->xd;
9423-		if (!xd->xname) {
9424-			/* xdatum is unchached */
9425-			if (!retry) {
9426-				retry = 1;
9427-				up_read(&c->xattr_sem);
9428-				down_write(&c->xattr_sem);
9429-				goto retry;
9430-			} else {
9431-				rc = load_xattr_datum(c, xd);
9432-				if (unlikely(rc > 0)) {
9433-					*pref = ref->next;
9434-					delete_xattr_ref(c, ref);
9435-					goto retry;
9436-				} else if (unlikely(rc < 0))
9437-					goto out;
9438-			}
9439-		}
9440-		xhandle = xprefix_to_handler(xd->xprefix);
9441-		if (!xhandle || (xhandle->list && !xhandle->list(dentry)))
9442-			continue;
9443-		prefix = xhandle->prefix ?: xhandle->name;
9444-		prefix_len = strlen(prefix);
9445-		rc = prefix_len + xd->name_len + 1;
9446-
9447-		if (buffer) {
9448-			if (rc > size - len) {
9449-				rc = -ERANGE;
9450-				goto out;
9451-			}
9452-			memcpy(buffer, prefix, prefix_len);
9453-			buffer += prefix_len;
9454-			memcpy(buffer, xd->xname, xd->name_len);
9455-			buffer += xd->name_len;
9456-			*buffer++ = 0;
9457-		}
9458-		len += rc;
9459-	}
9460-	rc = len;
9461- out:
9462-	if (!retry) {
9463-		up_read(&c->xattr_sem);
9464-	} else {
9465-		up_write(&c->xattr_sem);
9466-	}
9467-	return rc;
9468-}
9469-
9470-int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
9471-		      char *buffer, size_t size)
9472-{
9473-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9474-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9475-	struct jffs2_inode_cache *ic = f->inocache;
9476-	struct jffs2_xattr_datum *xd;
9477-	struct jffs2_xattr_ref *ref, **pref;
9478-	int rc, retry = 0;
9479-
9480-	rc = check_xattr_ref_inode(c, ic);
9481-	if (unlikely(rc))
9482-		return rc;
9483-
9484-	down_read(&c->xattr_sem);
9485- retry:
9486-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9487-		BUG_ON(ref->ic!=ic);
9488-
9489-		xd = ref->xd;
9490-		if (xd->xprefix != xprefix)
9491-			continue;
9492-		if (!xd->xname) {
9493-			/* xdatum is unchached */
9494-			if (!retry) {
9495-				retry = 1;
9496-				up_read(&c->xattr_sem);
9497-				down_write(&c->xattr_sem);
9498-				goto retry;
9499-			} else {
9500-				rc = load_xattr_datum(c, xd);
9501-				if (unlikely(rc > 0)) {
9502-					*pref = ref->next;
9503-					delete_xattr_ref(c, ref);
9504-					goto retry;
9505-				} else if (unlikely(rc < 0)) {
9506-					goto out;
9507-				}
9508-			}
9509-		}
9510-		if (!strcmp(xname, xd->xname)) {
9511-			rc = xd->value_len;
9512-			if (buffer) {
9513-				if (size < rc) {
9514-					rc = -ERANGE;
9515-				} else {
9516-					memcpy(buffer, xd->xvalue, rc);
9517-				}
9518-			}
9519-			goto out;
9520-		}
9521-	}
9522-	rc = -ENODATA;
9523- out:
9524-	if (!retry) {
9525-		up_read(&c->xattr_sem);
9526-	} else {
9527-		up_write(&c->xattr_sem);
9528-	}
9529-	return rc;
9530-}
9531-
9532-int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
9533-		      const char *buffer, size_t size, int flags)
9534-{
9535-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9536-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9537-	struct jffs2_inode_cache *ic = f->inocache;
9538-	struct jffs2_xattr_datum *xd;
9539-	struct jffs2_xattr_ref *ref, *newref, **pref;
9540-	uint32_t length, request;
9541-	int rc;
9542-
9543-	rc = check_xattr_ref_inode(c, ic);
9544-	if (unlikely(rc))
9545-		return rc;
9546-
9547-	request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
9548-	rc = jffs2_reserve_space(c, request, &length,
9549-				 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
9550-	if (rc) {
9551-		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
9552-		return rc;
9553-	}
9554-
9555-	/* Find existing xattr */
9556-	down_write(&c->xattr_sem);
9557- retry:
9558-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9559-		xd = ref->xd;
9560-		if (xd->xprefix != xprefix)
9561-			continue;
9562-		if (!xd->xname) {
9563-			rc = load_xattr_datum(c, xd);
9564-			if (unlikely(rc > 0)) {
9565-				*pref = ref->next;
9566-				delete_xattr_ref(c, ref);
9567-				goto retry;
9568-			} else if (unlikely(rc < 0))
9569-				goto out;
9570-		}
9571-		if (!strcmp(xd->xname, xname)) {
9572-			if (flags & XATTR_CREATE) {
9573-				rc = -EEXIST;
9574-				goto out;
9575-			}
9576-			if (!buffer) {
9577-				ref->ino = ic->ino;
9578-				ref->xid = xd->xid;
9579-				ref->xseqno |= XREF_DELETE_MARKER;
9580-				rc = save_xattr_ref(c, ref);
9581-				if (!rc) {
9582-					*pref = ref->next;
9583-					spin_lock(&c->erase_completion_lock);
9584-					ref->next = c->xref_dead_list;
9585-					c->xref_dead_list = ref;
9586-					spin_unlock(&c->erase_completion_lock);
9587-					unrefer_xattr_datum(c, xd);
9588-				} else {
9589-					ref->ic = ic;
9590-					ref->xd = xd;
9591-					ref->xseqno &= ~XREF_DELETE_MARKER;
9592-				}
9593-				goto out;
9594-			}
9595-			goto found;
9596-		}
9597-	}
9598-	/* not found */
9599-	if (flags & XATTR_REPLACE) {
9600-		rc = -ENODATA;
9601-		goto out;
9602-	}
9603-	if (!buffer) {
9604-		rc = -ENODATA;
9605-		goto out;
9606-	}
9607- found:
9608-	xd = create_xattr_datum(c, xprefix, xname, buffer, size);
9609-	if (IS_ERR(xd)) {
9610-		rc = PTR_ERR(xd);
9611-		goto out;
9612-	}
9613-	up_write(&c->xattr_sem);
9614-	jffs2_complete_reservation(c);
9615-
9616-	/* create xattr_ref */
9617-	request = PAD(sizeof(struct jffs2_raw_xref));
9618-	rc = jffs2_reserve_space(c, request, &length,
9619-				 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
9620-	down_write(&c->xattr_sem);
9621-	if (rc) {
9622-		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
9623-		unrefer_xattr_datum(c, xd);
9624-		up_write(&c->xattr_sem);
9625-		return rc;
9626-	}
9627-	if (ref)
9628-		*pref = ref->next;
9629-	newref = create_xattr_ref(c, ic, xd);
9630-	if (IS_ERR(newref)) {
9631-		if (ref) {
9632-			ref->next = ic->xref;
9633-			ic->xref = ref;
9634-		}
9635-		rc = PTR_ERR(newref);
9636-		unrefer_xattr_datum(c, xd);
9637-	} else if (ref) {
9638-		delete_xattr_ref(c, ref);
9639-	}
9640- out:
9641-	up_write(&c->xattr_sem);
9642-	jffs2_complete_reservation(c);
9643-	return rc;
9644-}
9645-
9646-/* -------- garbage collector functions -------------
9647- * jffs2_garbage_collect_xattr_datum(c, xd, raw)
9648- *   is used to move xdatum into new node.
9649- * jffs2_garbage_collect_xattr_ref(c, ref, raw)
9650- *   is used to move xref into new node.
9651- * jffs2_verify_xattr(c)
9652- *   is used to call do_verify_xattr_datum() before garbage collecting.
9653- * jffs2_release_xattr_datum(c, xd)
9654- *   is used to release an in-memory object of xdatum.
9655- * jffs2_release_xattr_ref(c, ref)
9656- *   is used to release an in-memory object of xref.
9657- * -------------------------------------------------- */
9658-int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
9659-				      struct jffs2_raw_node_ref *raw)
9660-{
9661-	uint32_t totlen, length, old_ofs;
9662-	int rc = 0;
9663-
9664-	down_write(&c->xattr_sem);
9665-	if (xd->node != raw)
9666-		goto out;
9667-	if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
9668-		goto out;
9669-
9670-	rc = load_xattr_datum(c, xd);
9671-	if (unlikely(rc)) {
9672-		rc = (rc > 0) ? 0 : rc;
9673-		goto out;
9674-	}
9675-	old_ofs = ref_offset(xd->node);
9676-	totlen = PAD(sizeof(struct jffs2_raw_xattr)
9677-			+ xd->name_len + 1 + xd->value_len);
9678-	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
9679-	if (rc) {
9680-		JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
9681-		goto out;
9682-	}
9683-	rc = save_xattr_datum(c, xd);
9684-	if (!rc)
9685-		dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
9686-			  xd->xid, xd->version, old_ofs, ref_offset(xd->node));
9687- out:
9688-	if (!rc)
9689-		jffs2_mark_node_obsolete(c, raw);
9690-	up_write(&c->xattr_sem);
9691-	return rc;
9692-}
9693-
9694-int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
9695-				    struct jffs2_raw_node_ref *raw)
9696-{
9697-	uint32_t totlen, length, old_ofs;
9698-	int rc = 0;
9699-
9700-	down_write(&c->xattr_sem);
9701-	BUG_ON(!ref->node);
9702-
9703-	if (ref->node != raw)
9704-		goto out;
9705-	if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
9706-		goto out;
9707-
9708-	old_ofs = ref_offset(ref->node);
9709-	totlen = ref_totlen(c, c->gcblock, ref->node);
9710-
9711-	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
9712-	if (rc) {
9713-		JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
9714-			      __func__, rc, totlen);
9715-		goto out;
9716-	}
9717-	rc = save_xattr_ref(c, ref);
9718-	if (!rc)
9719-		dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
9720-			  ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
9721- out:
9722-	if (!rc)
9723-		jffs2_mark_node_obsolete(c, raw);
9724-	up_write(&c->xattr_sem);
9725-	return rc;
9726-}
9727-
9728-int jffs2_verify_xattr(struct jffs2_sb_info *c)
9729-{
9730-	struct jffs2_xattr_datum *xd, *_xd;
9731-	struct jffs2_eraseblock *jeb;
9732-	struct jffs2_raw_node_ref *raw;
9733-	uint32_t totlen;
9734-	int rc;
9735-
9736-	down_write(&c->xattr_sem);
9737-	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
9738-		rc = do_verify_xattr_datum(c, xd);
9739-		if (rc < 0)
9740-			continue;
9741-		list_del_init(&xd->xindex);
9742-		spin_lock(&c->erase_completion_lock);
9743-		for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
9744-			if (ref_flags(raw) != REF_UNCHECKED)
9745-				continue;
9746-			jeb = &c->blocks[ref_offset(raw) / c->sector_size];
9747-			totlen = PAD(ref_totlen(c, jeb, raw));
9748-			c->unchecked_size -= totlen; c->used_size += totlen;
9749-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
9750-			raw->flash_offset = ref_offset(raw)
9751-				| ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
9752-		}
9753-		if (xd->flags & JFFS2_XFLAGS_DEAD)
9754-			list_add(&xd->xindex, &c->xattr_dead_list);
9755-		spin_unlock(&c->erase_completion_lock);
9756-	}
9757-	up_write(&c->xattr_sem);
9758-	return list_empty(&c->xattr_unchecked) ? 1 : 0;
9759-}
9760-
9761-void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
9762-{
9763-	/* must be called under spin_lock(&c->erase_completion_lock) */
9764-	if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
9765-		return;
9766-
9767-	list_del(&xd->xindex);
9768-	jffs2_free_xattr_datum(xd);
9769-}
9770-
9771-void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
9772-{
9773-	/* must be called under spin_lock(&c->erase_completion_lock) */
9774-	struct jffs2_xattr_ref *tmp, **ptmp;
9775-
9776-	if (ref->node != (void *)ref)
9777-		return;
9778-
9779-	for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
9780-		if (ref == tmp) {
9781-			*ptmp = tmp->next;
9782-			break;
9783-		}
9784-	}
9785-	jffs2_free_xattr_ref(ref);
9786-}
9787diff -Nupr old/fs/jffs2/xattr.h new/fs/jffs2/xattr.h
9788--- old/fs/jffs2/xattr.h	2022-05-09 17:22:53.000000000 +0800
9789+++ new/fs/jffs2/xattr.h	2022-05-09 20:04:55.580000000 +0800
9790@@ -12,7 +12,6 @@
9791 #ifndef _JFFS2_FS_XATTR_H_
9792 #define _JFFS2_FS_XATTR_H_
9793
9794-#include <linux/xattr.h>
9795 #include <linux/list.h>
9796
9797 #define JFFS2_XFLAGS_HOT	(0x01)	/* This datum is HOT */
9798@@ -48,7 +47,7 @@ struct jffs2_xattr_ref
9799 	struct jffs2_raw_node_ref *node;
9800 	uint8_t class;
9801 	uint8_t flags;		/* Currently unused */
9802-	u16 unused;
9803+	uint16_t unused;
9804
9805 	uint32_t xseqno;
9806 	union {
9807@@ -89,16 +88,14 @@ extern int jffs2_verify_xattr(struct jff
9808 extern void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd);
9809 extern void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref);
9810
9811-extern int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
9812-			     char *buffer, size_t size);
9813-extern int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
9814-			     const char *buffer, size_t size, int flags);
9815-
9816 extern const struct xattr_handler *jffs2_xattr_handlers[];
9817 extern const struct xattr_handler jffs2_user_xattr_handler;
9818 extern const struct xattr_handler jffs2_trusted_xattr_handler;
9819
9820 extern ssize_t jffs2_listxattr(struct dentry *, char *, size_t);
9821+#define jffs2_getxattr		generic_getxattr
9822+#define jffs2_setxattr		generic_setxattr
9823+#define jffs2_removexattr	generic_removexattr
9824
9825 #else
9826
9827@@ -113,12 +110,13 @@ extern ssize_t jffs2_listxattr(struct de
9828
9829 #define jffs2_xattr_handlers	NULL
9830 #define jffs2_listxattr		NULL
9831+#define jffs2_getxattr		NULL
9832+#define jffs2_setxattr		NULL
9833+#define jffs2_removexattr	NULL
9834
9835 #endif /* CONFIG_JFFS2_FS_XATTR */
9836
9837 #ifdef CONFIG_JFFS2_FS_SECURITY
9838-extern int jffs2_init_security(struct inode *inode, struct inode *dir,
9839-			       const struct qstr *qstr);
9840 extern const struct xattr_handler jffs2_security_xattr_handler;
9841 #else
9842 #define jffs2_init_security(inode,dir,qstr)	(0)
9843diff -Nupr old/fs/jffs2/xattr_trusted.c new/fs/jffs2/xattr_trusted.c
9844--- old/fs/jffs2/xattr_trusted.c	2022-05-09 17:15:24.360000000 +0800
9845+++ new/fs/jffs2/xattr_trusted.c	1970-01-01 08:00:00.000000000 +0800
9846@@ -1,46 +0,0 @@
9847-/*
9848- * JFFS2 -- Journalling Flash File System, Version 2.
9849- *
9850- * Copyright © 2006  NEC Corporation
9851- *
9852- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
9853- *
9854- * For licensing information, see the file 'LICENCE' in this directory.
9855- *
9856- */
9857-
9858-#include <linux/kernel.h>
9859-#include <linux/fs.h>
9860-#include <linux/jffs2.h>
9861-#include <linux/xattr.h>
9862-#include <linux/mtd/mtd.h>
9863-#include "nodelist.h"
9864-
9865-static int jffs2_trusted_getxattr(const struct xattr_handler *handler,
9866-				  struct dentry *unused, struct inode *inode,
9867-				  const char *name, void *buffer, size_t size)
9868-{
9869-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_TRUSTED,
9870-				 name, buffer, size);
9871-}
9872-
9873-static int jffs2_trusted_setxattr(const struct xattr_handler *handler,
9874-				  struct dentry *unused, struct inode *inode,
9875-				  const char *name, const void *buffer,
9876-				  size_t size, int flags)
9877-{
9878-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_TRUSTED,
9879-				 name, buffer, size, flags);
9880-}
9881-
9882-static bool jffs2_trusted_listxattr(struct dentry *dentry)
9883-{
9884-	return capable(CAP_SYS_ADMIN);
9885-}
9886-
9887-const struct xattr_handler jffs2_trusted_xattr_handler = {
9888-	.prefix = XATTR_TRUSTED_PREFIX,
9889-	.list = jffs2_trusted_listxattr,
9890-	.set = jffs2_trusted_setxattr,
9891-	.get = jffs2_trusted_getxattr
9892-};
9893diff -Nupr old/fs/jffs2/xattr_user.c new/fs/jffs2/xattr_user.c
9894--- old/fs/jffs2/xattr_user.c	2022-05-09 17:15:24.360000000 +0800
9895+++ new/fs/jffs2/xattr_user.c	1970-01-01 08:00:00.000000000 +0800
9896@@ -1,40 +0,0 @@
9897-/*
9898- * JFFS2 -- Journalling Flash File System, Version 2.
9899- *
9900- * Copyright © 2006  NEC Corporation
9901- *
9902- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
9903- *
9904- * For licensing information, see the file 'LICENCE' in this directory.
9905- *
9906- */
9907-
9908-#include <linux/kernel.h>
9909-#include <linux/fs.h>
9910-#include <linux/jffs2.h>
9911-#include <linux/xattr.h>
9912-#include <linux/mtd/mtd.h>
9913-#include "nodelist.h"
9914-
9915-static int jffs2_user_getxattr(const struct xattr_handler *handler,
9916-			       struct dentry *unused, struct inode *inode,
9917-			       const char *name, void *buffer, size_t size)
9918-{
9919-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_USER,
9920-				 name, buffer, size);
9921-}
9922-
9923-static int jffs2_user_setxattr(const struct xattr_handler *handler,
9924-			       struct dentry *unused, struct inode *inode,
9925-			       const char *name, const void *buffer,
9926-			       size_t size, int flags)
9927-{
9928-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_USER,
9929-				 name, buffer, size, flags);
9930-}
9931-
9932-const struct xattr_handler jffs2_user_xattr_handler = {
9933-	.prefix = XATTR_USER_PREFIX,
9934-	.set = jffs2_user_setxattr,
9935-	.get = jffs2_user_getxattr
9936-};
9937