• 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-	ri->uid = cpu_to_je16((ivalid & ATTR_UID)?
2783-		from_kuid(&init_user_ns, iattr->ia_uid):i_uid_read(inode));
2784-	ri->gid = cpu_to_je16((ivalid & ATTR_GID)?
2785-		from_kgid(&init_user_ns, iattr->ia_gid):i_gid_read(inode));
2786+	if (ivalid & CHG_UID) {
2787+		if (((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) && (!IsCapPermit(CAP_CHOWN))) {
2788+			jffs2_complete_reservation(c);
2789+			jffs2_free_raw_inode(ri);
2790+			mutex_unlock(&f->sem);
2791+			return -EPERM;
2792+		} else {
2793+			ri->uid = cpu_to_je16(attr->attr_chg_uid);
2794+		}
2795+	}
2796+
2797+	if (ivalid & CHG_GID) {
2798+		if (((c_gid != inode->i_gid) || (attr->attr_chg_gid != inode->i_gid)) && (!IsCapPermit(CAP_CHOWN))) {
2799+			jffs2_complete_reservation(c);
2800+			jffs2_free_raw_inode(ri);
2801+			mutex_unlock(&f->sem);
2802+			return -EPERM;
2803+		} else {
2804+			ri->gid = cpu_to_je16(attr->attr_chg_gid);
2805+		}
2806+	}
2807+
2808+	if (ivalid & CHG_MODE) {
2809+		if (!IsCapPermit(CAP_FOWNER) && (c_uid != inode->i_uid)) {
2810+			jffs2_complete_reservation(c);
2811+			jffs2_free_raw_inode(ri);
2812+			mutex_unlock(&f->sem);
2813+			return -EPERM;
2814+		} else {
2815+			attr->attr_chg_mode  &= ~S_IFMT; // delete file type
2816+			tmp_mode &= S_IFMT;
2817+			tmp_mode = attr->attr_chg_mode | tmp_mode; // add old file type
2818+		}
2819+	}
2820
2821-	if (ivalid & ATTR_MODE)
2822-		ri->mode = cpu_to_jemode(iattr->ia_mode);
2823-	else
2824-		ri->mode = cpu_to_jemode(inode->i_mode);
2825+	if (ivalid & CHG_ATIME) {
2826+		if ((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) {
2827+			return -EPERM;
2828+		} else {
2829+			ri->atime = cpu_to_je32(attr->attr_chg_atime);
2830+		}
2831+	} else {
2832+		ri->atime = cpu_to_je32(inode->i_atime);
2833+	}
2834
2835+	if (ivalid & CHG_MTIME) {
2836+		if ((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) {
2837+			return -EPERM;
2838+		} else {
2839+			ri->mtime = cpu_to_je32(attr->attr_chg_mtime);
2840+		}
2841+	} else {
2842+		ri->mtime = cpu_to_je32(Jffs2CurSec());
2843+	}
2844+	ri->mode = cpu_to_jemode(tmp_mode);
2845
2846-	ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
2847-	ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
2848-	ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
2849-	ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
2850+	ri->isize = cpu_to_je32((ivalid & CHG_SIZE) ? attr->attr_chg_size : inode->i_size);
2851+	ri->ctime = cpu_to_je32(Jffs2CurSec());
2852
2853 	ri->offset = cpu_to_je32(0);
2854-	ri->csize = ri->dsize = cpu_to_je32(mdatalen);
2855+	ri->csize = ri->dsize = cpu_to_je32(0);
2856 	ri->compr = JFFS2_COMPR_NONE;
2857-	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
2858+	if (ivalid & CHG_SIZE && inode->i_size < attr->attr_chg_size) {
2859 		/* It's an extension. Make it a hole node */
2860 		ri->compr = JFFS2_COMPR_ZERO;
2861-		ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
2862+		ri->dsize = cpu_to_je32(attr->attr_chg_size - inode->i_size);
2863 		ri->offset = cpu_to_je32(inode->i_size);
2864-	} else if (ivalid & ATTR_SIZE && !iattr->ia_size) {
2865+	} else if (ivalid & CHG_SIZE && !attr->attr_chg_size) {
2866 		/* For truncate-to-zero, treat it as deletion because
2867 		   it'll always be obsoleting all previous nodes */
2868 		alloc_type = ALLOC_DELETION;
2869 	}
2870-	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
2871-	if (mdatalen)
2872-		ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
2873-	else
2874-		ri->data_crc = cpu_to_je32(0);
2875-
2876-	new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, alloc_type);
2877-	if (S_ISLNK(inode->i_mode))
2878-		kfree(mdata);
2879-
2880+	ri->node_crc = cpu_to_je32(crc32(0, ri, (sizeof(*ri)-8)));
2881+	ri->data_crc = cpu_to_je32(0);
2882+	new_metadata = jffs2_write_dnode(c, f, ri, NULL, 0, alloc_type);
2883 	if (IS_ERR(new_metadata)) {
2884 		jffs2_complete_reservation(c);
2885 		jffs2_free_raw_inode(ri);
2886@@ -147,23 +140,20 @@ int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
2887 		return PTR_ERR(new_metadata);
2888 	}
2889 	/* It worked. Update the inode */
2890-	inode->i_atime = ITIME(je32_to_cpu(ri->atime));
2891-	inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
2892-	inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
2893+	inode->i_atime = je32_to_cpu(ri->atime);
2894+	inode->i_ctime = je32_to_cpu(ri->ctime);
2895+	inode->i_mtime = je32_to_cpu(ri->mtime);
2896 	inode->i_mode = jemode_to_cpu(ri->mode);
2897-	i_uid_write(inode, je16_to_cpu(ri->uid));
2898-	i_gid_write(inode, je16_to_cpu(ri->gid));
2899-
2900+	inode->i_uid = je16_to_cpu(ri->uid);
2901+	inode->i_gid = je16_to_cpu(ri->gid);
2902
2903 	old_metadata = f->metadata;
2904+	if (ivalid & CHG_SIZE && inode->i_size > attr->attr_chg_size)
2905+		jffs2_truncate_fragtree (c, &f->fragtree, attr->attr_chg_size);
2906
2907-	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
2908-		jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
2909-
2910-	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
2911+	if (ivalid & CHG_SIZE && inode->i_size < attr->attr_chg_size) {
2912 		jffs2_add_full_dnode_to_inode(c, f, new_metadata);
2913-		inode->i_size = iattr->ia_size;
2914-		inode->i_blocks = (inode->i_size + 511) >> 9;
2915+		inode->i_size = attr->attr_chg_size;
2916 		f->metadata = NULL;
2917 	} else {
2918 		f->metadata = new_metadata;
2919@@ -182,315 +172,201 @@ int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
2920 	   We are protected from a simultaneous write() extending i_size
2921 	   back past iattr->ia_size, because do_truncate() holds the
2922 	   generic inode semaphore. */
2923-	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
2924-		truncate_setsize(inode, iattr->ia_size);
2925-		inode->i_blocks = (inode->i_size + 511) >> 9;
2926+	if (ivalid & CHG_SIZE && inode->i_size > attr->attr_chg_size) {
2927+		inode->i_size = attr->attr_chg_size; // truncate_setsize
2928 	}
2929
2930 	return 0;
2931 }
2932
2933-int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
2934+static void jffs2_clear_inode (struct jffs2_inode *inode)
2935 {
2936-	struct inode *inode = d_inode(dentry);
2937-	int rc;
2938+	/* We can forget about this inode for now - drop all
2939+	 *  the nodelists associated with it, etc.
2940+	 */
2941+	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2942+	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2943+
2944+	jffs2_do_clear_inode(c, f);
2945+}
2946
2947-	rc = setattr_prepare(dentry, iattr);
2948-	if (rc)
2949-		return rc;
2950+static struct jffs2_inode *ilookup(struct super_block *sb, uint32_t ino)
2951+{
2952+	struct jffs2_inode *node = NULL;
2953
2954-	rc = jffs2_do_setattr(inode, iattr);
2955-	if (!rc && (iattr->ia_valid & ATTR_MODE))
2956-		rc = posix_acl_chmod(inode, inode->i_mode);
2957+	if (sb->s_root == NULL) {
2958+		return NULL;
2959+	}
2960
2961-	return rc;
2962+	// Check for this inode in the cache
2963+	Jffs2NodeLock();
2964+	(void)Jffs2HashGet(&sb->s_node_hash_lock, &sb->s_node_hash[0], sb, ino, &node);
2965+	Jffs2NodeUnlock();
2966+	return node;
2967 }
2968
2969-int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
2970+struct jffs2_inode *new_inode(struct super_block *sb)
2971 {
2972-	struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
2973-	unsigned long avail;
2974-
2975-	buf->f_type = JFFS2_SUPER_MAGIC;
2976-	buf->f_bsize = 1 << PAGE_SHIFT;
2977-	buf->f_blocks = c->flash_size >> PAGE_SHIFT;
2978-	buf->f_files = 0;
2979-	buf->f_ffree = 0;
2980-	buf->f_namelen = JFFS2_MAX_NAME_LEN;
2981-	buf->f_fsid.val[0] = JFFS2_SUPER_MAGIC;
2982-	buf->f_fsid.val[1] = c->mtd->index;
2983-
2984-	spin_lock(&c->erase_completion_lock);
2985-	avail = c->dirty_size + c->free_size;
2986-	if (avail > c->sector_size * c->resv_blocks_write)
2987-		avail -= c->sector_size * c->resv_blocks_write;
2988-	else
2989-		avail = 0;
2990-	spin_unlock(&c->erase_completion_lock);
2991-
2992-	buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
2993+	struct jffs2_inode *inode = NULL;
2994
2995-	return 0;
2996-}
2997+	inode = zalloc(sizeof (struct jffs2_inode));
2998+	if (inode == NULL)
2999+		return 0;
3000
3001+	D2(PRINTK("malloc new_inode %x ####################################\n",
3002+		inode));
3003
3004-void jffs2_evict_inode (struct inode *inode)
3005-{
3006-	/* We can forget about this inode for now - drop all
3007-	 *  the nodelists associated with it, etc.
3008-	 */
3009-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
3010-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
3011+	inode->i_sb = sb;
3012+	inode->i_ino = 1;
3013+	inode->i_nlink = 1;    // Let JFFS2 manage the link count
3014+	inode->i_size = 0;
3015+	LOS_ListInit((&(inode->i_hashlist)));
3016
3017-	jffs2_dbg(1, "%s(): ino #%lu mode %o\n",
3018-		  __func__, inode->i_ino, inode->i_mode);
3019-	truncate_inode_pages_final(&inode->i_data);
3020-	clear_inode(inode);
3021-	jffs2_do_clear_inode(c, f);
3022+	return inode;
3023 }
3024
3025-struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
3026+struct jffs2_inode *jffs2_iget(struct super_block *sb, uint32_t ino)
3027 {
3028 	struct jffs2_inode_info *f;
3029 	struct jffs2_sb_info *c;
3030 	struct jffs2_raw_inode latest_node;
3031-	union jffs2_device_node jdev;
3032-	struct inode *inode;
3033-	dev_t rdev = 0;
3034+	struct jffs2_inode *inode;
3035 	int ret;
3036
3037-	jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
3038-
3039-	inode = iget_locked(sb, ino);
3040-	if (!inode)
3041-		return ERR_PTR(-ENOMEM);
3042-	if (!(inode->i_state & I_NEW))
3043+	Jffs2NodeLock();
3044+	inode = ilookup(sb, ino);
3045+	if (inode) {
3046+		Jffs2NodeUnlock();
3047 		return inode;
3048+	}
3049+	inode = new_inode(sb);
3050+	if (inode == NULL) {
3051+		Jffs2NodeUnlock();
3052+		return (struct jffs2_inode *)-ENOMEM;
3053+	}
3054
3055+	inode->i_ino = ino;
3056 	f = JFFS2_INODE_INFO(inode);
3057 	c = JFFS2_SB_INFO(inode->i_sb);
3058
3059-	jffs2_init_inode_info(f);
3060-	mutex_lock(&f->sem);
3061+	(void)mutex_init(&f->sem);
3062+	(void)mutex_lock(&f->sem);
3063
3064 	ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
3065-	if (ret)
3066-		goto error;
3067+	if (ret) {
3068+		(void)mutex_unlock(&f->sem);
3069+        inode->i_nlink = 0;
3070+        free(inode);
3071+		Jffs2NodeUnlock();
3072+		return (struct jffs2_inode *)ret;
3073+	}
3074
3075 	inode->i_mode = jemode_to_cpu(latest_node.mode);
3076-	i_uid_write(inode, je16_to_cpu(latest_node.uid));
3077-	i_gid_write(inode, je16_to_cpu(latest_node.gid));
3078+	inode->i_uid = je16_to_cpu(latest_node.uid);
3079+	inode->i_gid = je16_to_cpu(latest_node.gid);
3080 	inode->i_size = je32_to_cpu(latest_node.isize);
3081-	inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
3082-	inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
3083-	inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
3084-
3085-	set_nlink(inode, f->inocache->pino_nlink);
3086-
3087-	inode->i_blocks = (inode->i_size + 511) >> 9;
3088+	inode->i_atime = je32_to_cpu(latest_node.atime);
3089+	inode->i_mtime = je32_to_cpu(latest_node.mtime);
3090+	inode->i_ctime = je32_to_cpu(latest_node.ctime);
3091+	inode->i_nlink = f->inocache->pino_nlink;
3092
3093-	switch (inode->i_mode & S_IFMT) {
3094+	(void)mutex_unlock(&f->sem);
3095
3096-	case S_IFLNK:
3097-		inode->i_op = &jffs2_symlink_inode_operations;
3098-		inode->i_link = f->target;
3099-		break;
3100-
3101-	case S_IFDIR:
3102-	{
3103-		struct jffs2_full_dirent *fd;
3104-		set_nlink(inode, 2); /* parent and '.' */
3105-
3106-		for (fd=f->dents; fd; fd = fd->next) {
3107-			if (fd->type == DT_DIR && fd->ino)
3108-				inc_nlink(inode);
3109-		}
3110-		/* Root dir gets i_nlink 3 for some reason */
3111-		if (inode->i_ino == 1)
3112-			inc_nlink(inode);
3113-
3114-		inode->i_op = &jffs2_dir_inode_operations;
3115-		inode->i_fop = &jffs2_dir_operations;
3116-		break;
3117-	}
3118-	case S_IFREG:
3119-		inode->i_op = &jffs2_file_inode_operations;
3120-		inode->i_fop = &jffs2_file_operations;
3121-		inode->i_mapping->a_ops = &jffs2_file_address_operations;
3122-		inode->i_mapping->nrpages = 0;
3123-		break;
3124-
3125-	case S_IFBLK:
3126-	case S_IFCHR:
3127-		/* Read the device numbers from the media */
3128-		if (f->metadata->size != sizeof(jdev.old_id) &&
3129-		    f->metadata->size != sizeof(jdev.new_id)) {
3130-			pr_notice("Device node has strange size %d\n",
3131-				  f->metadata->size);
3132-			goto error_io;
3133-		}
3134-		jffs2_dbg(1, "Reading device numbers from flash\n");
3135-		ret = jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size);
3136-		if (ret < 0) {
3137-			/* Eep */
3138-			pr_notice("Read device numbers for inode %lu failed\n",
3139-				  (unsigned long)inode->i_ino);
3140-			goto error;
3141-		}
3142-		if (f->metadata->size == sizeof(jdev.old_id))
3143-			rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
3144-		else
3145-			rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
3146-		fallthrough;
3147-
3148-	case S_IFSOCK:
3149-	case S_IFIFO:
3150-		inode->i_op = &jffs2_file_inode_operations;
3151-		init_special_inode(inode, inode->i_mode, rdev);
3152-		break;
3153-
3154-	default:
3155-		pr_warn("%s(): Bogus i_mode %o for ino %lu\n",
3156-			__func__, inode->i_mode, (unsigned long)inode->i_ino);
3157-	}
3158-
3159-	mutex_unlock(&f->sem);
3160+	(void)Jffs2HashInsert(&sb->s_node_hash_lock, &sb->s_node_hash[0], inode, ino);
3161
3162 	jffs2_dbg(1, "jffs2_read_inode() returning\n");
3163-	unlock_new_inode(inode);
3164-	return inode;
3165+	Jffs2NodeUnlock();
3166
3167-error_io:
3168-	ret = -EIO;
3169-error:
3170-	mutex_unlock(&f->sem);
3171-	iget_failed(inode);
3172-	return ERR_PTR(ret);
3173+	return inode;
3174 }
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-}
3199+// -------------------------------------------------------------------------
3200+// Decrement the reference count on an inode. If this makes the ref count
3201+// zero, then this inode can be freed.
3202
3203-int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc)
3204+int jffs2_iput(struct jffs2_inode *i)
3205 {
3206-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
3207-
3208-	if (c->flags & JFFS2_SB_FLAG_RO && !sb_rdonly(sb))
3209-		return -EROFS;
3210-
3211-	/* We stop if it was running, then restart if it needs to.
3212-	   This also catches the case where it was stopped and this
3213-	   is just a remount to restart it.
3214-	   Flush the writebuffer, if neccecary, else we loose it */
3215-	if (!sb_rdonly(sb)) {
3216-		jffs2_stop_garbage_collect_thread(c);
3217-		mutex_lock(&c->alloc_sem);
3218-		jffs2_flush_wbuf_pad(c);
3219-		mutex_unlock(&c->alloc_sem);
3220-	}
3221-
3222-	if (!(fc->sb_flags & SB_RDONLY))
3223-		jffs2_start_garbage_collect_thread(c);
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-	fc->sb_flags |= SB_NOATIME;
3246 	return 0;
3247 }
3248
3249+
3250 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
3251    fill in the raw_inode while you're at it. */
3252-struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode, struct jffs2_raw_inode *ri)
3253+struct jffs2_inode *jffs2_new_inode (struct jffs2_inode *dir_i, int mode, struct jffs2_raw_inode *ri)
3254 {
3255-	struct inode *inode;
3256+	struct jffs2_inode *inode;
3257 	struct super_block *sb = dir_i->i_sb;
3258 	struct jffs2_sb_info *c;
3259 	struct jffs2_inode_info *f;
3260 	int ret;
3261
3262-	jffs2_dbg(1, "%s(): dir_i %ld, mode 0x%x\n",
3263-		  __func__, dir_i->i_ino, mode);
3264-
3265 	c = JFFS2_SB_INFO(sb);
3266
3267+	Jffs2NodeLock();
3268 	inode = new_inode(sb);
3269
3270 	if (!inode)
3271-		return ERR_PTR(-ENOMEM);
3272+		return (struct jffs2_inode *)-ENOMEM;
3273
3274 	f = JFFS2_INODE_INFO(inode);
3275-	jffs2_init_inode_info(f);
3276-	mutex_lock(&f->sem);
3277+	(void)mutex_init(&f->sem);
3278+	(void)mutex_lock(&f->sem);;
3279
3280 	memset(ri, 0, sizeof(*ri));
3281 	/* Set OS-specific defaults for new inodes */
3282-	ri->uid = cpu_to_je16(from_kuid(&init_user_ns, current_fsuid()));
3283+	ri->uid = cpu_to_je16(OsCurrUserGet()->effUserID);
3284+	ri->gid = cpu_to_je16(OsCurrUserGet()->effGid);
3285
3286-	if (dir_i->i_mode & S_ISGID) {
3287-		ri->gid = cpu_to_je16(i_gid_read(dir_i));
3288-		if (S_ISDIR(mode))
3289-			mode |= S_ISGID;
3290-	} else {
3291-		ri->gid = cpu_to_je16(from_kgid(&init_user_ns, current_fsgid()));
3292-	}
3293-
3294-	/* POSIX ACLs have to be processed now, at least partly.
3295-	   The umask is only applied if there's no default ACL */
3296-	ret = jffs2_init_acl_pre(dir_i, inode, &mode);
3297-	if (ret) {
3298-		mutex_unlock(&f->sem);
3299-		make_bad_inode(inode);
3300-		iput(inode);
3301-		return ERR_PTR(ret);
3302-	}
3303 	ret = jffs2_do_new_inode (c, f, mode, ri);
3304 	if (ret) {
3305-		mutex_unlock(&f->sem);
3306-		make_bad_inode(inode);
3307-		iput(inode);
3308-		return ERR_PTR(ret);
3309+		mutex_unlock(&(f->sem));
3310+		jffs2_clear_inode(inode);
3311+		(void)mutex_destroy(&(f->sem));
3312+		(void)memset_s(inode, sizeof(*inode), 0x6a, sizeof(*inode));
3313+		free(inode);
3314+		Jffs2NodeUnlock();
3315+		return (struct jffs2_inode *)ret;
3316+
3317 	}
3318-	set_nlink(inode, 1);
3319+	inode->i_nlink = 1;
3320 	inode->i_ino = je32_to_cpu(ri->ino);
3321 	inode->i_mode = jemode_to_cpu(ri->mode);
3322-	i_gid_write(inode, je16_to_cpu(ri->gid));
3323-	i_uid_write(inode, je16_to_cpu(ri->uid));
3324-	inode->i_atime = inode->i_ctime = inode->i_mtime = current_time(inode);
3325-	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
3326+	inode->i_gid = je16_to_cpu(ri->gid);
3327+	inode->i_uid = je16_to_cpu(ri->uid);
3328+	inode->i_atime = inode->i_ctime = inode->i_mtime = Jffs2CurSec();
3329+	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(inode->i_mtime);
3330
3331-	inode->i_blocks = 0;
3332 	inode->i_size = 0;
3333
3334-	if (insert_inode_locked(inode) < 0) {
3335-		mutex_unlock(&f->sem);
3336-		make_bad_inode(inode);
3337-		iput(inode);
3338-		return ERR_PTR(-EINVAL);
3339-	}
3340+	(void)Jffs2HashInsert(&sb->s_node_hash_lock, &sb->s_node_hash[0], inode, inode->i_ino);
3341+	Jffs2NodeUnlock();
3342
3343 	return inode;
3344 }
3345
3346-static int calculate_inocache_hashsize(uint32_t flash_size)
3347+int calculate_inocache_hashsize(uint32_t flash_size)
3348 {
3349 	/*
3350 	 * Pick a inocache hash size based on the size of the medium.
3351@@ -510,118 +386,17 @@ static int calculate_inocache_hashsize(uint32_t flash_size)
3352 	return hashsize;
3353 }
3354
3355-int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc)
3356-{
3357-	struct jffs2_sb_info *c;
3358-	struct inode *root_i;
3359-	int ret;
3360-	size_t blocks;
3361-
3362-	c = JFFS2_SB_INFO(sb);
3363-
3364-	/* Do not support the MLC nand */
3365-	if (c->mtd->type == MTD_MLCNANDFLASH)
3366-		return -EINVAL;
3367-
3368-#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
3369-	if (c->mtd->type == MTD_NANDFLASH) {
3370-		errorf(fc, "Cannot operate on NAND flash unless jffs2 NAND support is compiled in");
3371-		return -EINVAL;
3372-	}
3373-	if (c->mtd->type == MTD_DATAFLASH) {
3374-		errorf(fc, "Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in");
3375-		return -EINVAL;
3376-	}
3377-#endif
3378-
3379-	c->flash_size = c->mtd->size;
3380-	c->sector_size = c->mtd->erasesize;
3381-	blocks = c->flash_size / c->sector_size;
3382-
3383-	/*
3384-	 * Size alignment check
3385-	 */
3386-	if ((c->sector_size * blocks) != c->flash_size) {
3387-		c->flash_size = c->sector_size * blocks;
3388-		infof(fc, "Flash size not aligned to erasesize, reducing to %dKiB",
3389-		      c->flash_size / 1024);
3390-	}
3391-
3392-	if (c->flash_size < 5*c->sector_size) {
3393-		errorf(fc, "Too few erase blocks (%d)",
3394-		       c->flash_size / c->sector_size);
3395-		return -EINVAL;
3396-	}
3397-
3398-	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
3399-
3400-	/* NAND (or other bizarre) flash... do setup accordingly */
3401-	ret = jffs2_flash_setup(c);
3402-	if (ret)
3403-		return ret;
3404-
3405-	c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
3406-	c->inocache_list = kcalloc(c->inocache_hashsize, sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
3407-	if (!c->inocache_list) {
3408-		ret = -ENOMEM;
3409-		goto out_wbuf;
3410-	}
3411-
3412-	jffs2_init_xattr_subsystem(c);
3413-
3414-	if ((ret = jffs2_do_mount_fs(c)))
3415-		goto out_inohash;
3416-
3417-	jffs2_dbg(1, "%s(): Getting root inode\n", __func__);
3418-	root_i = jffs2_iget(sb, 1);
3419-	if (IS_ERR(root_i)) {
3420-		jffs2_dbg(1, "get root inode failed\n");
3421-		ret = PTR_ERR(root_i);
3422-		goto out_root;
3423-	}
3424-
3425-	ret = -ENOMEM;
3426-
3427-	jffs2_dbg(1, "%s(): d_make_root()\n", __func__);
3428-	sb->s_root = d_make_root(root_i);
3429-	if (!sb->s_root)
3430-		goto out_root;
3431-
3432-	sb->s_maxbytes = 0xFFFFFFFF;
3433-	sb->s_blocksize = PAGE_SIZE;
3434-	sb->s_blocksize_bits = PAGE_SHIFT;
3435-	sb->s_magic = JFFS2_SUPER_MAGIC;
3436-	sb->s_time_min = 0;
3437-	sb->s_time_max = U32_MAX;
3438-
3439-	if (!sb_rdonly(sb))
3440-		jffs2_start_garbage_collect_thread(c);
3441-	return 0;
3442-
3443-out_root:
3444-	jffs2_free_ino_caches(c);
3445-	jffs2_free_raw_node_refs(c);
3446-	kvfree(c->blocks);
3447-	jffs2_clear_xattr_subsystem(c);
3448-	jffs2_sum_exit(c);
3449- out_inohash:
3450-	kfree(c->inocache_list);
3451- out_wbuf:
3452-	jffs2_flash_cleanup(c);
3453-
3454-	return ret;
3455-}
3456-
3457 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
3458 				   struct jffs2_inode_info *f)
3459 {
3460-	iput(OFNI_EDONI_2SFFJ(f));
3461+	struct jffs2_inode *node = OFNI_EDONI_2SFFJ(f);
3462+	jffs2_iput(node);
3463 }
3464
3465 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
3466 					      int inum, int unlinked)
3467 {
3468-	struct inode *inode;
3469+	struct jffs2_inode *inode;
3470 	struct jffs2_inode_cache *ic;
3471
3472 	if (unlinked) {
3473@@ -669,72 +444,9 @@ struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
3474 		   Just iget() it, and if read_inode() is necessary that's OK.
3475 		*/
3476 		inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
3477-		if (IS_ERR(inode))
3478-			return ERR_CAST(inode);
3479-	}
3480-	if (is_bad_inode(inode)) {
3481-		pr_notice("Eep. read_inode() failed for ino #%u. unlinked %d\n",
3482-			  inum, unlinked);
3483-		/* NB. This will happen again. We need to do something appropriate here. */
3484-		iput(inode);
3485-		return ERR_PTR(-EIO);
3486+		if (inode <= 0)
3487+			return (struct jffs2_inode_info *)inode;
3488 	}
3489
3490 	return JFFS2_INODE_INFO(inode);
3491 }
3492-
3493-static int jffs2_flash_setup(struct jffs2_sb_info *c) {
3494-	int ret = 0;
3495-
3496-	if (jffs2_cleanmarker_oob(c)) {
3497-		/* NAND flash... do setup accordingly */
3498-		ret = jffs2_nand_flash_setup(c);
3499-		if (ret)
3500-			return ret;
3501-	}
3502-
3503-	/* and Dataflash */
3504-	if (jffs2_dataflash(c)) {
3505-		ret = jffs2_dataflash_setup(c);
3506-		if (ret)
3507-			return ret;
3508-	}
3509-
3510-	/* and Intel "Sibley" flash */
3511-	if (jffs2_nor_wbuf_flash(c)) {
3512-		ret = jffs2_nor_wbuf_flash_setup(c);
3513-		if (ret)
3514-			return ret;
3515-	}
3516-
3517-	/* and an UBI volume */
3518-	if (jffs2_ubivol(c)) {
3519-		ret = jffs2_ubivol_setup(c);
3520-		if (ret)
3521-			return ret;
3522-	}
3523-
3524-	return ret;
3525-}
3526-
3527-void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
3528-
3529-	if (jffs2_cleanmarker_oob(c)) {
3530-		jffs2_nand_flash_cleanup(c);
3531-	}
3532-
3533-	/* and DataFlash */
3534-	if (jffs2_dataflash(c)) {
3535-		jffs2_dataflash_cleanup(c);
3536-	}
3537-
3538-	/* and Intel "Sibley" flash */
3539-	if (jffs2_nor_wbuf_flash(c)) {
3540-		jffs2_nor_wbuf_flash_cleanup(c);
3541-	}
3542-
3543-	/* and an UBI volume */
3544-	if (jffs2_ubivol(c)) {
3545-		jffs2_ubivol_cleanup(c);
3546-	}
3547-}
3548diff -Nupr old/fs/jffs2/gc.c new/fs/jffs2/gc.c
3549--- old/fs/jffs2/gc.c	2022-05-09 17:22:53.000000000 +0800
3550+++ new/fs/jffs2/gc.c	2022-05-10 16:11:42.090000000 +0800
3551@@ -10,17 +10,17 @@
3552  *
3553  */
3554
3555-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3556-
3557 #include <linux/kernel.h>
3558-#include <linux/mtd/mtd.h>
3559 #include <linux/slab.h>
3560 #include <linux/pagemap.h>
3561-#include <linux/crc32.h>
3562+#include <linux/delay.h>
3563+#include <linux/semaphore.h>
3564 #include <linux/compiler.h>
3565 #include <linux/stat.h>
3566+#include "mtd_dev.h"
3567 #include "nodelist.h"
3568 #include "compr.h"
3569+#include "los_crc32.h"
3570
3571 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
3572 					  struct jffs2_inode_cache *ic,
3573@@ -43,7 +43,7 @@ static int jffs2_garbage_collect_live(st
3574 /* Called with erase_completion_lock held */
3575 static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
3576 {
3577-	struct jffs2_eraseblock *ret;
3578+	struct jffs2_eraseblock *ret = NULL;
3579 	struct list_head *nextlist = NULL;
3580 	int n = jiffies % 128;
3581
3582@@ -131,62 +131,40 @@ int jffs2_garbage_collect_pass(struct jf
3583 	int ret = 0, inum, nlink;
3584 	int xattr = 0;
3585
3586-	if (mutex_lock_interruptible(&c->alloc_sem))
3587+	if (mutex_lock(&c->alloc_sem))
3588 		return -EINTR;
3589
3590-
3591 	for (;;) {
3592-		/* We can't start doing GC until we've finished checking
3593-		   the node CRCs etc. */
3594-		int bucket, want_ino;
3595-
3596 		spin_lock(&c->erase_completion_lock);
3597 		if (!c->unchecked_size)
3598 			break;
3599+
3600+		/* We can't start doing GC yet. We haven't finished checking
3601+		   the node CRCs etc. Do it now. */
3602+
3603+		/* checked_ino is protected by the alloc_sem */
3604+		if (c->checked_ino > c->highest_ino && xattr) {
3605+			pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
3606+				c->unchecked_size);
3607+			jffs2_dbg_dump_block_lists_nolock(c);
3608+			spin_unlock(&c->erase_completion_lock);
3609+			mutex_unlock(&c->alloc_sem);
3610+			return -ENOSPC;
3611+		}
3612+
3613 		spin_unlock(&c->erase_completion_lock);
3614
3615 		if (!xattr)
3616 			xattr = jffs2_verify_xattr(c);
3617
3618 		spin_lock(&c->inocache_lock);
3619-		/* Instead of doing the inodes in numeric order, doing a lookup
3620-		 * in the hash for each possible number, just walk the hash
3621-		 * buckets of *existing* inodes. This means that we process
3622-		 * them out-of-order, but it can be a lot faster if there's
3623-		 * a sparse inode# space. Which there often is. */
3624-		want_ino = c->check_ino;
3625-		for (bucket = c->check_ino % c->inocache_hashsize ; bucket < c->inocache_hashsize; bucket++) {
3626-			for (ic = c->inocache_list[bucket]; ic; ic = ic->next) {
3627-				if (ic->ino < want_ino)
3628-					continue;
3629-
3630-				if (ic->state != INO_STATE_CHECKEDABSENT &&
3631-				    ic->state != INO_STATE_PRESENT)
3632-					goto got_next; /* with inocache_lock held */
3633
3634-				jffs2_dbg(1, "Skipping ino #%u already checked\n",
3635-					  ic->ino);
3636-			}
3637-			want_ino = 0;
3638-		}
3639-
3640-		/* Point c->check_ino past the end of the last bucket. */
3641-		c->check_ino = ((c->highest_ino + c->inocache_hashsize + 1) &
3642-				~c->inocache_hashsize) - 1;
3643-
3644-		spin_unlock(&c->inocache_lock);
3645-
3646-		pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
3647-			c->unchecked_size);
3648-		jffs2_dbg_dump_block_lists_nolock(c);
3649-		mutex_unlock(&c->alloc_sem);
3650-		return -ENOSPC;
3651+		ic = jffs2_get_ino_cache(c, c->checked_ino++);
3652
3653-	got_next:
3654-		/* For next time round the loop, we want c->checked_ino to indicate
3655-		 * the *next* one we want to check. And since we're walking the
3656-		 * buckets rather than doing it sequentially, it's: */
3657-		c->check_ino = ic->ino + c->inocache_hashsize;
3658+		if (!ic) {
3659+			spin_unlock(&c->inocache_lock);
3660+			continue;
3661+		}
3662
3663 		if (!ic->pino_nlink) {
3664 			jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
3665@@ -198,6 +176,8 @@ int jffs2_garbage_collect_pass(struct jf
3666 		switch(ic->state) {
3667 		case INO_STATE_CHECKEDABSENT:
3668 		case INO_STATE_PRESENT:
3669+			jffs2_dbg(1, "Skipping ino #%u already checked\n",
3670+				  ic->ino);
3671 			spin_unlock(&c->inocache_lock);
3672 			continue;
3673
3674@@ -207,6 +187,7 @@ int jffs2_garbage_collect_pass(struct jf
3675 				ic->ino, ic->state);
3676 			spin_unlock(&c->inocache_lock);
3677 			BUG();
3678+			break;
3679
3680 		case INO_STATE_READING:
3681 			/* We need to wait for it to finish, lest we move on
3682@@ -216,7 +197,7 @@ int jffs2_garbage_collect_pass(struct jf
3683 				  ic->ino);
3684 			/* We need to come back again for the _same_ inode. We've
3685 			 made no progress in this case, but that should be OK */
3686-			c->check_ino = ic->ino;
3687+			c->checked_ino--;
3688
3689 			mutex_unlock(&c->alloc_sem);
3690 			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
3691@@ -224,6 +205,7 @@ int jffs2_garbage_collect_pass(struct jf
3692
3693 		default:
3694 			BUG();
3695+			break;
3696
3697 		case INO_STATE_UNCHECKED:
3698 			;
3699@@ -290,7 +272,7 @@ int jffs2_garbage_collect_pass(struct jf
3700 	raw = jeb->gc_node;
3701 	gcblock_dirty = jeb->dirty_size;
3702
3703-	while(ref_obsolete(raw)) {
3704+	while(raw && ref_obsolete(raw)) {
3705 		jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
3706 			  ref_offset(raw));
3707 		raw = ref_next(raw);
3708@@ -310,7 +292,7 @@ int jffs2_garbage_collect_pass(struct jf
3709 	jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
3710 		  ref_offset(raw));
3711
3712-	if (!raw->next_in_ino) {
3713+	if (raw &&!raw->next_in_ino) {
3714 		/* Inode-less node. Clean marker, snapshot or something like that */
3715 		spin_unlock(&c->erase_completion_lock);
3716 		if (ref_flags(raw) == REF_PRISTINE) {
3717@@ -368,7 +350,7 @@ int jffs2_garbage_collect_pass(struct jf
3718 		   We can just copy any pristine nodes, but have
3719 		   to prevent anyone else from doing read_inode() while
3720 		   we're at it, so we set the state accordingly */
3721-		if (ref_flags(raw) == REF_PRISTINE)
3722+		if (raw && ref_flags(raw) == REF_PRISTINE)
3723 			ic->state = INO_STATE_GC;
3724 		else {
3725 			jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
3726@@ -393,6 +375,7 @@ int jffs2_garbage_collect_pass(struct jf
3727 		mutex_unlock(&c->alloc_sem);
3728 		spin_unlock(&c->inocache_lock);
3729 		BUG();
3730+		break;
3731
3732 	case INO_STATE_READING:
3733 		/* Someone's currently trying to read it. We must wait for
3734@@ -430,7 +413,6 @@ int jffs2_garbage_collect_pass(struct jf
3735
3736 		spin_lock(&c->inocache_lock);
3737 		ic->state = INO_STATE_CHECKEDABSENT;
3738-		wake_up(&c->inocache_wq);
3739
3740 		if (ret != -EBADFD) {
3741 			spin_unlock(&c->inocache_lock);
3742@@ -460,9 +442,7 @@ int jffs2_garbage_collect_pass(struct jf
3743 		ret = 0;
3744 		goto release_sem;
3745 	}
3746-
3747 	ret = jffs2_garbage_collect_live(c, jeb, raw, f);
3748-
3749 	jffs2_gc_release_inode(c, f);
3750
3751  test_gcnode:
3752@@ -541,7 +521,7 @@ static int jffs2_garbage_collect_live(st
3753 				break; /* We've found them all */
3754 		}
3755 	}
3756-	if (fn) {
3757+	if (fn != NULL && frag != NULL) {
3758 		if (ref_flags(raw) == REF_PRISTINE) {
3759 			ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
3760 			if (!ret) {
3761@@ -552,7 +532,7 @@ static int jffs2_garbage_collect_live(st
3762 				goto upnout;
3763 		}
3764 		/* We found a datanode. Do the GC */
3765-		if((start >> PAGE_SHIFT) < ((end-1) >> PAGE_SHIFT)) {
3766+		if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
3767 			/* It crosses a page boundary. Therefore, it must be a hole. */
3768 			ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
3769 		} else {
3770@@ -635,6 +615,7 @@ static int jffs2_garbage_collect_pristin
3771 	if (je32_to_cpu(node->u.hdr_crc) != crc) {
3772 		pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3773 			ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
3774+		jffs2_dbg_dump_node(c, ref_offset(raw));
3775 		goto bail;
3776 	}
3777
3778@@ -645,6 +626,7 @@ static int jffs2_garbage_collect_pristin
3779 			pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3780 				ref_offset(raw), je32_to_cpu(node->i.node_crc),
3781 				crc);
3782+			jffs2_dbg_dump_node(c, ref_offset(raw));
3783 			goto bail;
3784 		}
3785
3786@@ -654,6 +636,7 @@ static int jffs2_garbage_collect_pristin
3787 				pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3788 					ref_offset(raw),
3789 					je32_to_cpu(node->i.data_crc), crc);
3790+				jffs2_dbg_dump_node(c, ref_offset(raw));
3791 				goto bail;
3792 			}
3793 		}
3794@@ -665,12 +648,14 @@ static int jffs2_garbage_collect_pristin
3795 			pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3796 				ref_offset(raw),
3797 				je32_to_cpu(node->d.node_crc), crc);
3798+			jffs2_dbg_dump_node(c, ref_offset(raw));
3799 			goto bail;
3800 		}
3801
3802-		if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
3803+		if (strnlen((const char *)node->d.name, node->d.nsize) != node->d.nsize) {
3804 			pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
3805 				ref_offset(raw));
3806+			jffs2_dbg_dump_node(c, ref_offset(raw));
3807 			goto bail;
3808 		}
3809
3810@@ -680,6 +665,7 @@ static int jffs2_garbage_collect_pristin
3811 				pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3812 					ref_offset(raw),
3813 					je32_to_cpu(node->d.name_crc), crc);
3814+				jffs2_dbg_dump_node(c, ref_offset(raw));
3815 				goto bail;
3816 			}
3817 		}
3818@@ -689,6 +675,7 @@ static int jffs2_garbage_collect_pristin
3819 		if (ic) {
3820 			pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
3821 				ref_offset(raw), je16_to_cpu(node->u.nodetype));
3822+			jffs2_dbg_dump_node(c, ref_offset(raw));
3823 			goto bail;
3824 		}
3825 	}
3826@@ -697,7 +684,7 @@ static int jffs2_garbage_collect_pristin
3827  retry:
3828 	phys_ofs = write_ofs(c);
3829
3830-	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
3831+	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (const u_char *)node);
3832
3833 	if (ret || (retlen != rawlen)) {
3834 		pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
3835@@ -761,7 +748,7 @@ static int jffs2_garbage_collect_metadat
3836 	struct jffs2_full_dnode *new_fn;
3837 	struct jffs2_raw_inode ri;
3838 	struct jffs2_node_frag *last_frag;
3839-	union jffs2_device_node dev;
3840+	jint16_t dev;
3841 	char *mdata = NULL;
3842 	int mdatalen = 0;
3843 	uint32_t alloclen, ilen;
3844@@ -770,8 +757,9 @@ static int jffs2_garbage_collect_metadat
3845 	if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
3846 	    S_ISCHR(JFFS2_F_I_MODE(f)) ) {
3847 		/* For these, we don't actually need to read the old node */
3848-		mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
3849+		dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) | JFFS2_F_I_RDEV_MIN(f)));
3850 		mdata = (char *)&dev;
3851+		mdatalen = sizeof(dev);
3852 		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
3853 			  __func__, mdatalen);
3854 	} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
3855@@ -781,7 +769,7 @@ static int jffs2_garbage_collect_metadat
3856 			pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
3857 			return -ENOMEM;
3858 		}
3859-		ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
3860+		ret = jffs2_read_dnode(c, f, fn, (unsigned char *)mdata, 0, mdatalen);
3861 		if (ret) {
3862 			pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
3863 				ret);
3864@@ -831,7 +819,7 @@ static int jffs2_garbage_collect_metadat
3865 	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
3866 	ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
3867
3868-	new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
3869+	new_fn = jffs2_write_dnode(c, f, &ri, (unsigned char *)mdata, mdatalen, ALLOC_GC);
3870
3871 	if (IS_ERR(new_fn)) {
3872 		pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
3873@@ -857,7 +845,7 @@ static int jffs2_garbage_collect_dirent(
3874
3875 	rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
3876 	rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
3877-	rd.nsize = strlen(fd->name);
3878+	rd.nsize = strlen((const char *)fd->name);
3879 	rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
3880 	rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
3881
3882@@ -908,7 +896,7 @@ static int jffs2_garbage_collect_deletio
3883 		struct jffs2_raw_node_ref *raw;
3884 		int ret;
3885 		size_t retlen;
3886-		int name_len = strlen(fd->name);
3887+		int name_len = strlen((const char *)fd->name);
3888 		uint32_t name_crc = crc32(0, fd->name, name_len);
3889 		uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
3890
3891@@ -1053,6 +1041,7 @@ static int jffs2_garbage_collect_hole(st
3892 			pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
3893 				__func__, ref_offset(fn->raw),
3894 				je32_to_cpu(ri.node_crc), crc);
3895+				jffs2_dbg_dump_node(c, ref_offset(fn->raw));
3896 			/* FIXME: We could possibly deal with this by writing new holes for each frag */
3897 			pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
3898 				start, end, f->inocache->ino);
3899@@ -1165,13 +1154,12 @@ static int jffs2_garbage_collect_dnode(s
3900 				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
3901 				       uint32_t start, uint32_t end)
3902 {
3903-	struct inode *inode = OFNI_EDONI_2SFFJ(f);
3904 	struct jffs2_full_dnode *new_fn;
3905 	struct jffs2_raw_inode ri;
3906 	uint32_t alloclen, offset, orig_end, orig_start;
3907 	int ret = 0;
3908 	unsigned char *comprbuf = NULL, *writebuf;
3909-	struct page *page;
3910+	unsigned long pg;
3911 	unsigned char *pg_ptr;
3912
3913 	memset(&ri, 0, sizeof(ri));
3914@@ -1193,8 +1181,8 @@ static int jffs2_garbage_collect_dnode(s
3915 		struct jffs2_node_frag *frag;
3916 		uint32_t min, max;
3917
3918-		min = start & ~(PAGE_SIZE-1);
3919-		max = min + PAGE_SIZE;
3920+		min = start & ~(PAGE_CACHE_SIZE-1);
3921+		max = min + PAGE_CACHE_SIZE;
3922
3923 		frag = jffs2_lookup_node_frag(&f->fragtree, start);
3924
3925@@ -1203,7 +1191,7 @@ static int jffs2_garbage_collect_dnode(s
3926 		BUG_ON(frag->ofs != start);
3927
3928 		/* First grow down... */
3929-		while((frag = frag_prev(frag)) && frag->ofs >= min) {
3930+		while(frag && (frag = frag_prev(frag)) && frag->ofs >= min) {
3931
3932 			/* If the previous frag doesn't even reach the beginning, there's
3933 			   excessive fragmentation. Just merge. */
3934@@ -1259,7 +1247,7 @@ static int jffs2_garbage_collect_dnode(s
3935 		/* Find last frag which is actually part of the node we're to GC. */
3936 		frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
3937
3938-		while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
3939+		while(frag && (frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
3940
3941 			/* If the previous frag doesn't even reach the beginning, there's lots
3942 			   of fragmentation. Just merge. */
3943@@ -1317,27 +1305,21 @@ static int jffs2_garbage_collect_dnode(s
3944 		BUG_ON(start > orig_start);
3945 	}
3946
3947-	/* The rules state that we must obtain the page lock *before* f->sem, so
3948-	 * drop f->sem temporarily. Since we also hold c->alloc_sem, nothing's
3949-	 * actually going to *change* so we're safe; we only allow reading.
3950-	 *
3951-	 * It is important to note that jffs2_write_begin() will ensure that its
3952-	 * page is marked Uptodate before allocating space. That means that if we
3953-	 * end up here trying to GC the *same* page that jffs2_write_begin() is
3954-	 * trying to write out, read_cache_page() will not deadlock. */
3955-	mutex_unlock(&f->sem);
3956-	page = read_cache_page(inode->i_mapping, start >> PAGE_SHIFT,
3957-			       jffs2_do_readpage_unlock, inode);
3958-	if (IS_ERR(page)) {
3959+	/* First, use readpage() to read the appropriate page into the page cache */
3960+	/* Q: What happens if we actually try to GC the _same_ page for which commit_write()
3961+	 *    triggered garbage collection in the first place?
3962+	 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
3963+	 *    page OK. We'll actually write it out again in commit_write, which is a little
3964+	 *    suboptimal, but at least we're correct.
3965+	 */
3966+	pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
3967+
3968+	if (IS_ERR(pg_ptr)) {
3969 		pr_warn("read_cache_page() returned error: %ld\n",
3970-			PTR_ERR(page));
3971-		mutex_lock(&f->sem);
3972-		return PTR_ERR(page);
3973+			PTR_ERR(pg_ptr));
3974+		return PTR_ERR(pg_ptr);
3975 	}
3976
3977-	pg_ptr = kmap(page);
3978-	mutex_lock(&f->sem);
3979-
3980 	offset = start;
3981 	while(offset < orig_end) {
3982 		uint32_t datalen;
3983@@ -1355,7 +1337,7 @@ static int jffs2_garbage_collect_dnode(s
3984 		cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
3985 		datalen = end - offset;
3986
3987-		writebuf = pg_ptr + (offset & (PAGE_SIZE -1));
3988+		writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
3989
3990 		comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
3991
3992@@ -1400,7 +1382,6 @@ static int jffs2_garbage_collect_dnode(s
3993 		}
3994 	}
3995
3996-	kunmap(page);
3997-	put_page(page);
3998+	jffs2_gc_release_page(c, pg_ptr, &pg);
3999 	return ret;
4000 }
4001diff -Nupr old/fs/jffs2/ioctl.c new/fs/jffs2/ioctl.c
4002--- old/fs/jffs2/ioctl.c	2022-05-09 17:15:24.350000000 +0800
4003+++ new/fs/jffs2/ioctl.c	1970-01-01 08:00:00.000000000 +0800
4004@@ -1,22 +0,0 @@
4005-/*
4006- * JFFS2 -- Journalling Flash File System, Version 2.
4007- *
4008- * Copyright © 2001-2007 Red Hat, Inc.
4009- * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
4010- *
4011- * Created by David Woodhouse <dwmw2@infradead.org>
4012- *
4013- * For licensing information, see the file 'LICENCE' in this directory.
4014- *
4015- */
4016-
4017-#include <linux/fs.h>
4018-#include "nodelist.h"
4019-
4020-long jffs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4021-{
4022-	/* Later, this will provide for lsattr.jffs2 and chattr.jffs2, which
4023-	   will include compression support etc. */
4024-	return -ENOTTY;
4025-}
4026-
4027diff -Nupr old/fs/jffs2/jffs2_fs_i.h new/fs/jffs2/jffs2_fs_i.h
4028--- old/fs/jffs2/jffs2_fs_i.h	2022-05-09 17:22:53.000000000 +0800
4029+++ new/fs/jffs2/jffs2_fs_i.h	2022-05-09 20:50:05.810000000 +0800
4030@@ -14,8 +14,13 @@
4031 #define _JFFS2_FS_I
4032
4033 #include <linux/rbtree.h>
4034-#include <linux/posix_acl.h>
4035-#include <linux/mutex.h>
4036+#include <linux/kernel.h>
4037+
4038+#ifdef __cplusplus
4039+#if __cplusplus
4040+extern "C" {
4041+#endif /* __cplusplus */
4042+#endif /* __cplusplus */
4043
4044 struct jffs2_inode_info {
4045 	/* We need an internal mutex similar to inode->i_mutex.
4046@@ -24,7 +29,7 @@ struct jffs2_inode_info {
4047 	   before letting GC proceed. Or we'd have to put ugliness
4048 	   into the GC code so it didn't attempt to obtain the i_mutex
4049 	   for the inode(s) which are already locked */
4050-	struct mutex sem;
4051+	struct pthread_mutex sem;
4052
4053 	/* The highest (datanode) version number used for this ino */
4054 	uint32_t highest_version;
4055@@ -50,7 +55,29 @@ struct jffs2_inode_info {
4056
4057 	uint16_t flags;
4058 	uint8_t usercompr;
4059-	struct inode vfs_inode;
4060 };
4061
4062+struct super_block;
4063+
4064+struct jffs2_inode {
4065+	uint32_t i_ino;
4066+	mode_t i_mode;
4067+	nlink_t i_nlink;
4068+	uid_t i_uid;
4069+	gid_t i_gid;
4070+	time_t i_atime;
4071+	time_t i_mtime;
4072+	time_t i_ctime;
4073+	off_t i_size;
4074+	struct super_block *i_sb;
4075+	LOS_DL_LIST i_hashlist;
4076+	struct jffs2_inode_info jffs2_i;
4077+};
4078+
4079+#ifdef __cplusplus
4080+#if __cplusplus
4081+}
4082+#endif /* __cplusplus */
4083+#endif /* __cplusplus */
4084+
4085 #endif /* _JFFS2_FS_I */
4086diff -Nupr old/fs/jffs2/jffs2_fs_sb.h new/fs/jffs2/jffs2_fs_sb.h
4087--- old/fs/jffs2/jffs2_fs_sb.h	2022-05-09 17:22:53.000000000 +0800
4088+++ new/fs/jffs2/jffs2_fs_sb.h	2022-05-09 20:49:43.100000000 +0800
4089@@ -17,11 +17,18 @@
4090 #include <linux/spinlock.h>
4091 #include <linux/workqueue.h>
4092 #include <linux/completion.h>
4093-#include <linux/mutex.h>
4094 #include <linux/timer.h>
4095 #include <linux/wait.h>
4096 #include <linux/list.h>
4097 #include <linux/rwsem.h>
4098+#include "vfs_jffs2.h"
4099+#include "mtd_dev.h"
4100+
4101+#ifdef __cplusplus
4102+#if __cplusplus
4103+extern "C" {
4104+#endif /* __cplusplus */
4105+#endif /* __cplusplus */
4106
4107 #define JFFS2_SB_FLAG_RO 1
4108 #define JFFS2_SB_FLAG_SCANNING 2 /* Flash scanning is in progress */
4109@@ -47,10 +54,10 @@ struct jffs2_mount_opts {
4110    Nee jffs_control
4111 */
4112 struct jffs2_sb_info {
4113-	struct mtd_info *mtd;
4114+	struct MtdDev *mtd;
4115
4116 	uint32_t highest_ino;
4117-	uint32_t check_ino;		/* *NEXT* inode to be checked */
4118+	uint32_t checked_ino;
4119
4120 	unsigned int flags;
4121
4122@@ -58,7 +65,7 @@ struct jffs2_sb_info {
4123 	struct completion gc_thread_start; /* GC thread start completion */
4124 	struct completion gc_thread_exit; /* GC thread exit completion port */
4125
4126-	struct mutex alloc_sem;		/* Used to protect all the following
4127+	struct pthread_mutex alloc_sem;	/* Used to protect all the following
4128 					   fields, and also to protect against
4129 					   out-of-order writing of nodes. And GC. */
4130 	uint32_t cleanmarker_size;	/* Size of an _inline_ CLEANMARKER
4131@@ -120,7 +127,7 @@ struct jffs2_sb_info {
4132 	/* Sem to allow jffs2_garbage_collect_deletion_dirent to
4133 	   drop the erase_completion_lock while it's holding a pointer
4134 	   to an obsoleted node. I don't like this. Alternatives welcomed. */
4135-	struct mutex erase_free_sem;
4136+	struct pthread_mutex  erase_free_sem;
4137
4138 	uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */
4139
4140@@ -160,4 +167,27 @@ struct jffs2_sb_info {
4141 	void *os_priv;
4142 };
4143
4144+struct super_block {
4145+	struct jffs2_sb_info	jffs2_sb;
4146+	LIST_HEAD		s_node_hash[JFFS2_NODE_HASH_BUCKETS];
4147+	LosMux			s_node_hash_lock;
4148+	struct jffs2_inode 	*s_root;
4149+	void			*s_dev;
4150+
4151+	UINT32			s_lock;			/* Lock the inode cache */
4152+	EVENT_CB_S		s_gc_thread_flags;	/* Communication with the gcthread */
4153+	unsigned int		s_gc_thread;
4154+	unsigned long		s_mount_flags;
4155+};
4156+
4157+#define JFFS2_SB_INFO(sb) (&(sb)->jffs2_sb)
4158+#define OFNI_BS_2SFFJ(c)  \
4159+        ((struct super_block *) ( ((char *)c) - ((char *)(&((struct super_block *)NULL)->jffs2_sb)) ) )
4160+
4161+#ifdef __cplusplus
4162+#if __cplusplus
4163+}
4164+#endif /* __cplusplus */
4165+#endif /* __cplusplus */
4166+
4167 #endif /* _JFFS2_FS_SB */
4168diff -Nupr old/fs/jffs2/malloc.c new/fs/jffs2/malloc.c
4169--- old/fs/jffs2/malloc.c	2022-05-09 17:22:53.000000000 +0800
4170+++ new/fs/jffs2/malloc.c	2022-05-10 09:43:16.720000000 +0800
4171@@ -9,111 +9,31 @@
4172  *
4173  */
4174
4175-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4176-
4177 #include <linux/kernel.h>
4178 #include <linux/slab.h>
4179-#include <linux/init.h>
4180-#include <linux/jffs2.h>
4181+#include <stdlib.h>
4182 #include "nodelist.h"
4183
4184-/* These are initialised to NULL in the kernel startup code.
4185-   If you're porting to other operating systems, beware */
4186-static struct kmem_cache *full_dnode_slab;
4187-static struct kmem_cache *raw_dirent_slab;
4188-static struct kmem_cache *raw_inode_slab;
4189-static struct kmem_cache *tmp_dnode_info_slab;
4190-static struct kmem_cache *raw_node_ref_slab;
4191-static struct kmem_cache *node_frag_slab;
4192-static struct kmem_cache *inode_cache_slab;
4193-#ifdef CONFIG_JFFS2_FS_XATTR
4194-static struct kmem_cache *xattr_datum_cache;
4195-static struct kmem_cache *xattr_ref_cache;
4196+#if !defined(JFFS2NUM_FS_JFFS2_RAW_NODE_REF_CACHE_POOL_SIZE)
4197+# define JFFS2NUM_FS_JFFS2_RAW_NODE_REF_CACHE_POOL_SIZE 0
4198 #endif
4199
4200 int __init jffs2_create_slab_caches(void)
4201 {
4202-	full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
4203-					    sizeof(struct jffs2_full_dnode),
4204-					    0, 0, NULL);
4205-	if (!full_dnode_slab)
4206-		goto err;
4207-
4208-	raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
4209-					    sizeof(struct jffs2_raw_dirent),
4210-					    0, SLAB_HWCACHE_ALIGN, NULL);
4211-	if (!raw_dirent_slab)
4212-		goto err;
4213-
4214-	raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
4215-					   sizeof(struct jffs2_raw_inode),
4216-					   0, SLAB_HWCACHE_ALIGN, NULL);
4217-	if (!raw_inode_slab)
4218-		goto err;
4219-
4220-	tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
4221-						sizeof(struct jffs2_tmp_dnode_info),
4222-						0, 0, NULL);
4223-	if (!tmp_dnode_info_slab)
4224-		goto err;
4225-
4226-	raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
4227-					      sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
4228-					      0, 0, NULL);
4229-	if (!raw_node_ref_slab)
4230-		goto err;
4231-
4232-	node_frag_slab = kmem_cache_create("jffs2_node_frag",
4233-					   sizeof(struct jffs2_node_frag),
4234-					   0, 0, NULL);
4235-	if (!node_frag_slab)
4236-		goto err;
4237-
4238-	inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
4239-					     sizeof(struct jffs2_inode_cache),
4240-					     0, 0, NULL);
4241-	if (!inode_cache_slab)
4242-		goto err;
4243-
4244-#ifdef CONFIG_JFFS2_FS_XATTR
4245-	xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum",
4246-					     sizeof(struct jffs2_xattr_datum),
4247-					     0, 0, NULL);
4248-	if (!xattr_datum_cache)
4249-		goto err;
4250-
4251-	xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
4252-					   sizeof(struct jffs2_xattr_ref),
4253-					   0, 0, NULL);
4254-	if (!xattr_ref_cache)
4255-		goto err;
4256-#endif
4257-
4258 	return 0;
4259- err:
4260-	jffs2_destroy_slab_caches();
4261-	return -ENOMEM;
4262+
4263 }
4264
4265 void jffs2_destroy_slab_caches(void)
4266 {
4267-	kmem_cache_destroy(full_dnode_slab);
4268-	kmem_cache_destroy(raw_dirent_slab);
4269-	kmem_cache_destroy(raw_inode_slab);
4270-	kmem_cache_destroy(tmp_dnode_info_slab);
4271-	kmem_cache_destroy(raw_node_ref_slab);
4272-	kmem_cache_destroy(node_frag_slab);
4273-	kmem_cache_destroy(inode_cache_slab);
4274-#ifdef CONFIG_JFFS2_FS_XATTR
4275-	kmem_cache_destroy(xattr_datum_cache);
4276-	kmem_cache_destroy(xattr_ref_cache);
4277-#endif
4278+	return;
4279 }
4280
4281+
4282 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
4283 {
4284 	struct jffs2_full_dirent *ret;
4285-	ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
4286+	ret = zalloc(sizeof(struct jffs2_full_dirent) + namesize);
4287 	dbg_memalloc("%p\n", ret);
4288 	return ret;
4289 }
4290@@ -127,7 +47,7 @@ void jffs2_free_full_dirent(struct jffs2
4291 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
4292 {
4293 	struct jffs2_full_dnode *ret;
4294-	ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
4295+	ret = zalloc(sizeof(struct jffs2_full_dnode));
4296 	dbg_memalloc("%p\n", ret);
4297 	return ret;
4298 }
4299@@ -135,13 +55,13 @@ struct jffs2_full_dnode *jffs2_alloc_ful
4300 void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
4301 {
4302 	dbg_memalloc("%p\n", x);
4303-	kmem_cache_free(full_dnode_slab, x);
4304+	free(x);
4305 }
4306
4307 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
4308 {
4309 	struct jffs2_raw_dirent *ret;
4310-	ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
4311+	ret = zalloc(sizeof(struct jffs2_raw_dirent));
4312 	dbg_memalloc("%p\n", ret);
4313 	return ret;
4314 }
4315@@ -149,13 +69,13 @@ struct jffs2_raw_dirent *jffs2_alloc_raw
4316 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
4317 {
4318 	dbg_memalloc("%p\n", x);
4319-	kmem_cache_free(raw_dirent_slab, x);
4320+	free(x);
4321 }
4322
4323 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
4324 {
4325 	struct jffs2_raw_inode *ret;
4326-	ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
4327+	ret = zalloc(sizeof(struct jffs2_raw_inode));
4328 	dbg_memalloc("%p\n", ret);
4329 	return ret;
4330 }
4331@@ -163,13 +83,13 @@ struct jffs2_raw_inode *jffs2_alloc_raw_
4332 void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
4333 {
4334 	dbg_memalloc("%p\n", x);
4335-	kmem_cache_free(raw_inode_slab, x);
4336+	free(x);
4337 }
4338
4339 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
4340 {
4341 	struct jffs2_tmp_dnode_info *ret;
4342-	ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
4343+	ret = zalloc(sizeof(struct jffs2_tmp_dnode_info));
4344 	dbg_memalloc("%p\n",
4345 		ret);
4346 	return ret;
4347@@ -178,14 +98,14 @@ struct jffs2_tmp_dnode_info *jffs2_alloc
4348 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
4349 {
4350 	dbg_memalloc("%p\n", x);
4351-	kmem_cache_free(tmp_dnode_info_slab, x);
4352+	free(x);
4353 }
4354
4355 static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
4356 {
4357 	struct jffs2_raw_node_ref *ret;
4358
4359-	ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
4360+	ret = malloc(sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK+1));
4361 	if (ret) {
4362 		int i = 0;
4363 		for (i=0; i < REFS_PER_BLOCK; i++) {
4364@@ -242,13 +162,13 @@ int jffs2_prealloc_raw_node_refs(struct
4365 void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
4366 {
4367 	dbg_memalloc("%p\n", x);
4368-	kmem_cache_free(raw_node_ref_slab, x);
4369+	free(x);
4370 }
4371
4372 struct jffs2_node_frag *jffs2_alloc_node_frag(void)
4373 {
4374 	struct jffs2_node_frag *ret;
4375-	ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
4376+	ret = malloc(sizeof(struct jffs2_node_frag));
4377 	dbg_memalloc("%p\n", ret);
4378 	return ret;
4379 }
4380@@ -256,13 +176,14 @@ struct jffs2_node_frag *jffs2_alloc_node
4381 void jffs2_free_node_frag(struct jffs2_node_frag *x)
4382 {
4383 	dbg_memalloc("%p\n", x);
4384-	kmem_cache_free(node_frag_slab, x);
4385+	free(x);
4386 }
4387
4388+
4389 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
4390 {
4391 	struct jffs2_inode_cache *ret;
4392-	ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
4393+	ret = zalloc(sizeof(struct jffs2_inode_cache));;
4394 	dbg_memalloc("%p\n", ret);
4395 	return ret;
4396 }
4397@@ -270,14 +191,14 @@ struct jffs2_inode_cache *jffs2_alloc_in
4398 void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
4399 {
4400 	dbg_memalloc("%p\n", x);
4401-	kmem_cache_free(inode_cache_slab, x);
4402+	kfree(x);
4403 }
4404
4405 #ifdef CONFIG_JFFS2_FS_XATTR
4406 struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
4407 {
4408 	struct jffs2_xattr_datum *xd;
4409-	xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
4410+	xd = malloc(sizeof(struct jffs2_xattr_datum));
4411 	dbg_memalloc("%p\n", xd);
4412 	if (!xd)
4413 		return NULL;
4414@@ -291,13 +212,13 @@ struct jffs2_xattr_datum *jffs2_alloc_xa
4415 void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
4416 {
4417 	dbg_memalloc("%p\n", xd);
4418-	kmem_cache_free(xattr_datum_cache, xd);
4419+	kfree(xd);
4420 }
4421
4422 struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
4423 {
4424 	struct jffs2_xattr_ref *ref;
4425-	ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
4426+	ref = malloc(sizeof(struct jffs2_xattr_ref));
4427 	dbg_memalloc("%p\n", ref);
4428 	if (!ref)
4429 		return NULL;
4430@@ -310,6 +231,6 @@ struct jffs2_xattr_ref *jffs2_alloc_xatt
4431 void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
4432 {
4433 	dbg_memalloc("%p\n", ref);
4434-	kmem_cache_free(xattr_ref_cache, ref);
4435+	kfree(ref);
4436 }
4437 #endif
4438diff -Nupr old/fs/jffs2/nodelist.c new/fs/jffs2/nodelist.c
4439--- old/fs/jffs2/nodelist.c	2022-05-09 17:22:53.000000000 +0800
4440+++ new/fs/jffs2/nodelist.c	2022-05-09 20:37:35.680000000 +0800
4441@@ -9,16 +9,15 @@
4442  *
4443  */
4444
4445-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4446-
4447 #include <linux/kernel.h>
4448 #include <linux/sched.h>
4449 #include <linux/fs.h>
4450-#include <linux/mtd/mtd.h>
4451 #include <linux/rbtree.h>
4452-#include <linux/crc32.h>
4453 #include <linux/pagemap.h>
4454+#include <mtd_dev.h>
4455 #include "nodelist.h"
4456+#include "jffs2.h"
4457+#include "los_crc32.h"
4458
4459 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
4460 				     struct jffs2_node_frag *this);
4461@@ -30,7 +29,7 @@ void jffs2_add_fd_to_list(struct jffs2_s
4462 	dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
4463
4464 	while ((*prev) && (*prev)->nhash <= new->nhash) {
4465-		if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
4466+		if ((*prev)->nhash == new->nhash && !strcmp((const char *)((*prev)->name), (const char *)new->name)) {
4467 			/* Duplicate. Free one */
4468 			if (new->version < (*prev)->version) {
4469 				dbg_dentlist("Eep! Marking new dirent node obsolete, old is \"%s\", ino #%u\n",
4470@@ -41,7 +40,7 @@ void jffs2_add_fd_to_list(struct jffs2_s
4471 				dbg_dentlist("marking old dirent \"%s\", ino #%u obsolete\n",
4472 					(*prev)->name, (*prev)->ino);
4473 				new->next = (*prev)->next;
4474-				/* It may have been a 'placeholder' deletion dirent,
4475+				/* It may have been a 'placeholder' deletion dirent,
4476 				   if jffs2_can_mark_obsolete() (see jffs2_do_unlink()) */
4477 				if ((*prev)->raw)
4478 					jffs2_mark_node_obsolete(c, ((*prev)->raw));
4479@@ -65,13 +64,14 @@ uint32_t jffs2_truncate_fragtree(struct
4480 	/* We know frag->ofs <= size. That's what lookup does for us */
4481 	if (frag && frag->ofs != size) {
4482 		if (frag->ofs+frag->size > size) {
4483+		    dbg_fragtree("truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
4484 			frag->size = size - frag->ofs;
4485 		}
4486 		frag = frag_next(frag);
4487 	}
4488 	while (frag && frag->ofs >= size) {
4489 		struct jffs2_node_frag *next = frag_next(frag);
4490-
4491+        dbg_fragtree("removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
4492 		frag_erase(frag, list);
4493 		jffs2_obsolete_node_frag(c, frag);
4494 		frag = next;
4495@@ -90,7 +90,7 @@ uint32_t jffs2_truncate_fragtree(struct
4496
4497 	/* If the last fragment starts at the RAM page boundary, it is
4498 	 * REF_PRISTINE irrespective of its size. */
4499-	if (frag->node && (frag->ofs & (PAGE_SIZE - 1)) == 0) {
4500+	if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
4501 		dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
4502 			frag->ofs, frag->ofs + frag->size);
4503 		frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
4504@@ -237,7 +237,7 @@ static int jffs2_add_frag_to_fragtree(st
4505 		   If so, both 'this' and the new node get marked REF_NORMAL so
4506 		   the GC can take a look.
4507 		*/
4508-		if (lastend && (lastend-1) >> PAGE_SHIFT == newfrag->ofs >> PAGE_SHIFT) {
4509+		if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
4510 			if (this->node)
4511 				mark_ref_normal(this->node->raw);
4512 			mark_ref_normal(newfrag->node->raw);
4513@@ -382,7 +382,7 @@ int jffs2_add_full_dnode_to_inode(struct
4514
4515 	/* If we now share a page with other nodes, mark either previous
4516 	   or next node REF_NORMAL, as appropriate.  */
4517-	if (newfrag->ofs & (PAGE_SIZE-1)) {
4518+	if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
4519 		struct jffs2_node_frag *prev = frag_prev(newfrag);
4520
4521 		mark_ref_normal(fn->raw);
4522@@ -391,7 +391,7 @@ int jffs2_add_full_dnode_to_inode(struct
4523 			mark_ref_normal(prev->node->raw);
4524 	}
4525
4526-	if ((newfrag->ofs+newfrag->size) & (PAGE_SIZE-1)) {
4527+	if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
4528 		struct jffs2_node_frag *next = frag_next(newfrag);
4529
4530 		if (next) {
4531@@ -401,7 +401,7 @@ int jffs2_add_full_dnode_to_inode(struct
4532 		}
4533 	}
4534 	jffs2_dbg_fragtree_paranoia_check_nolock(f);
4535-
4536+	jffs2_dbg_dump_fragtree_nolock(f);
4537 	return 0;
4538 }
4539
4540@@ -409,7 +409,6 @@ void jffs2_set_inocache_state(struct jff
4541 {
4542 	spin_lock(&c->inocache_lock);
4543 	ic->state = state;
4544-	wake_up(&c->inocache_wq);
4545 	spin_unlock(&c->inocache_lock);
4546 }
4547
4548@@ -505,8 +504,12 @@ void jffs2_free_raw_node_refs(struct jff
4549 {
4550 	int i;
4551 	struct jffs2_raw_node_ref *this, *next;
4552+	struct super_block *sb = NULL;
4553+	struct MtdNorDev *device = NULL;
4554+	sb = OFNI_BS_2SFFJ(c);
4555+	device = (struct MtdNorDev*)(sb->s_dev);
4556
4557-	for (i=0; i<c->nr_blocks; i++) {
4558+	for (i=device->blockStart; i<c->nr_blocks+device->blockStart; i++) {
4559 		this = c->blocks[i].first_node;
4560 		while (this) {
4561 			if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
4562@@ -536,14 +539,22 @@ struct jffs2_node_frag *jffs2_lookup_nod
4563 	while(next) {
4564 		frag = rb_entry(next, struct jffs2_node_frag, rb);
4565
4566+		dbg_fragtree2("considering frag %#04x-%#04x (%p). left %p, right %p\n",
4567+			  frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right);
4568 		if (frag->ofs + frag->size <= offset) {
4569+			dbg_fragtree2("going right from frag %#04x-%#04x, before the region we care about\n",
4570+				  frag->ofs, frag->ofs+frag->size);
4571 			/* Remember the closest smaller match on the way down */
4572 			if (!prev || frag->ofs > prev->ofs)
4573 				prev = frag;
4574 			next = frag->rb.rb_right;
4575 		} else if (frag->ofs > offset) {
4576+			dbg_fragtree2("going left from frag %#04x-%#04x, after the region we care about\n",
4577+				  frag->ofs, frag->ofs+frag->size);
4578 			next = frag->rb.rb_left;
4579 		} else {
4580+			dbg_fragtree2("returning frag %#04x-%#04x, matched\n",
4581+				  frag->ofs, frag->ofs+frag->size);
4582 			return frag;
4583 		}
4584 	}
4585@@ -564,10 +575,12 @@ struct jffs2_node_frag *jffs2_lookup_nod
4586    they're killed. */
4587 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
4588 {
4589-	struct jffs2_node_frag *frag, *next;
4590+	struct jffs2_node_frag *frag;
4591+	struct rb_node *tn,*next;
4592
4593 	dbg_fragtree("killing\n");
4594-	rbtree_postorder_for_each_entry_safe(frag, next, root, rb) {
4595+	RB_POSTORDER_FOREACH_SAFE(tn, linux_root, (struct linux_root *)root, next) {
4596+		frag = (struct jffs2_node_frag *)tn;
4597 		if (frag->node && !(--frag->node->frags)) {
4598 			/* Not a hole, and it's the final remaining frag
4599 			   of this node. Free the node */
4600@@ -604,7 +617,7 @@ struct jffs2_raw_node_ref *jffs2_link_no
4601 			ref++;
4602 	}
4603
4604-	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
4605+	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
4606 		    ref->flash_offset, ofs, ref->next_in_ino, len);
4607
4608 	ref->flash_offset = ofs;
4609@@ -617,7 +630,7 @@ struct jffs2_raw_node_ref *jffs2_link_no
4610
4611 		JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
4612 			    ref, ref_offset(ref), ref_offset(ref)+len,
4613-			    ref_offset(jeb->last_node),
4614+			    ref_offset(jeb->last_node),
4615 			    ref_offset(jeb->last_node)+last_len);
4616 		BUG();
4617 	}
4618@@ -734,7 +747,7 @@ uint32_t __jffs2_ref_totlen(struct jffs2
4619 			pr_crit("next %p (0x%08x-0x%08x)\n",
4620 				ref_next(ref), ref_offset(ref_next(ref)),
4621 				ref_offset(ref_next(ref)) + ref->__totlen);
4622-		} else
4623+		} else
4624 			pr_crit("No next ref. jeb->last_node is %p\n",
4625 				jeb->last_node);
4626
4627diff -Nupr old/fs/jffs2/nodelist.h new/fs/jffs2/nodelist.h
4628--- old/fs/jffs2/nodelist.h	2022-05-09 17:22:53.000000000 +0800
4629+++ new/fs/jffs2/nodelist.h	2022-05-09 20:36:25.460000000 +0800
4630@@ -12,20 +12,28 @@
4631 #ifndef __JFFS2_NODELIST_H__
4632 #define __JFFS2_NODELIST_H__
4633
4634-#include <linux/fs.h>
4635+#include <linux/stat.h>
4636 #include <linux/types.h>
4637-#include <linux/jffs2.h>
4638+#include <linux/list.h>
4639+#include "jffs2.h"
4640 #include "jffs2_fs_sb.h"
4641 #include "jffs2_fs_i.h"
4642 #include "xattr.h"
4643 #include "acl.h"
4644 #include "summary.h"
4645-
4646-#ifdef __ECOS
4647-#include "os-ecos.h"
4648-#else
4649+#include "vfs_jffs2.h"
4650 #include "os-linux.h"
4651-#endif
4652+
4653+#ifdef __cplusplus
4654+#if __cplusplus
4655+extern "C" {
4656+#endif /* __cplusplus */
4657+#endif /* __cplusplus */
4658+
4659+struct kvec {
4660+	void *iov_base;
4661+	long iov_len;
4662+};
4663
4664 #define JFFS2_NATIVE_ENDIAN
4665
4666@@ -193,6 +201,8 @@ struct jffs2_inode_cache {
4667 #define INO_STATE_READING	5	/* In read_inode() */
4668 #define INO_STATE_CLEARING	6	/* In clear_inode() */
4669
4670+#define INOCACHE_HASHSIZE 128
4671+
4672 #define INO_FLAGS_XATTR_CHECKED	0x01	/* has no duplicate xattr_ref */
4673 #define INO_FLAGS_IS_DIR	0x02	/* is a directory */
4674
4675@@ -250,10 +260,7 @@ struct jffs2_readinode_info
4676
4677 struct jffs2_full_dirent
4678 {
4679-	union {
4680-		struct jffs2_raw_node_ref *raw;
4681-		struct jffs2_inode_cache *ic; /* Just during part of build */
4682-	};
4683+	struct jffs2_raw_node_ref *raw;
4684 	struct jffs2_full_dirent *next;
4685 	uint32_t version;
4686 	uint32_t ino; /* == zero for unlink */
4687@@ -313,34 +320,26 @@ static inline int jffs2_blocks_use_vmall
4688
4689 #define PAD(x) (((x)+3)&~3)
4690
4691-static inline int jffs2_encode_dev(union jffs2_device_node *jdev, dev_t rdev)
4692-{
4693-	if (old_valid_dev(rdev)) {
4694-		jdev->old_id = cpu_to_je16(old_encode_dev(rdev));
4695-		return sizeof(jdev->old_id);
4696-	} else {
4697-		jdev->new_id = cpu_to_je32(new_encode_dev(rdev));
4698-		return sizeof(jdev->new_id);
4699-	}
4700-}
4701
4702 static inline struct jffs2_node_frag *frag_first(struct rb_root *root)
4703 {
4704-	struct rb_node *node = rb_first(root);
4705+	struct rb_node *node = root->rb_node;
4706
4707 	if (!node)
4708 		return NULL;
4709-
4710+	while(node->rb_left)
4711+		node = node->rb_left;
4712 	return rb_entry(node, struct jffs2_node_frag, rb);
4713 }
4714
4715 static inline struct jffs2_node_frag *frag_last(struct rb_root *root)
4716 {
4717-	struct rb_node *node = rb_last(root);
4718+	struct rb_node *node = root->rb_node;
4719
4720 	if (!node)
4721 		return NULL;
4722-
4723+	while(node->rb_right)
4724+		node = node->rb_right;
4725 	return rb_entry(node, struct jffs2_node_frag, rb);
4726 }
4727
4728@@ -404,8 +403,9 @@ struct jffs2_full_dirent *jffs2_write_di
4729 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
4730 			    struct jffs2_raw_inode *ri, unsigned char *buf,
4731 			    uint32_t offset, uint32_t writelen, uint32_t *retlen);
4732-int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f,
4733-		    struct jffs2_raw_inode *ri, const struct qstr *qstr);
4734+int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
4735+		    struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
4736+		    const char *name, int namelen);
4737 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, const char *name,
4738 		    int namelen, struct jffs2_inode_info *dead_f, uint32_t time);
4739 int jffs2_do_link(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino,
4740@@ -481,4 +481,10 @@ int jffs2_write_nand_cleanmarker(struct
4741
4742 #include "debug.h"
4743
4744+#ifdef __cplusplus
4745+#if __cplusplus
4746+}
4747+#endif /* __cplusplus */
4748+#endif /* __cplusplus */
4749+
4750 #endif /* __JFFS2_NODELIST_H__ */
4751diff -Nupr old/fs/jffs2/nodemgmt.c new/fs/jffs2/nodemgmt.c
4752--- old/fs/jffs2/nodemgmt.c	2022-05-09 17:22:53.000000000 +0800
4753+++ new/fs/jffs2/nodemgmt.c	2022-05-09 20:35:50.910000000 +0800
4754@@ -9,46 +9,14 @@
4755  *
4756  */
4757
4758-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4759-
4760 #include <linux/kernel.h>
4761-#include <linux/mtd/mtd.h>
4762 #include <linux/compiler.h>
4763-#include <linux/sched/signal.h>
4764+#include <linux/sched.h> /* For cond_resched() */
4765+#include <linux/semaphore.h>
4766+#include <mtd_dev.h>
4767 #include "nodelist.h"
4768 #include "debug.h"
4769
4770-/*
4771- * Check whether the user is allowed to write.
4772- */
4773-static int jffs2_rp_can_write(struct jffs2_sb_info *c)
4774-{
4775-	uint32_t avail;
4776-	struct jffs2_mount_opts *opts = &c->mount_opts;
4777-
4778-	avail = c->dirty_size + c->free_size + c->unchecked_size +
4779-		c->erasing_size - c->resv_blocks_write * c->sector_size
4780-		- c->nospc_dirty_size;
4781-
4782-	if (avail < 2 * opts->rp_size)
4783-		jffs2_dbg(1, "rpsize %u, dirty_size %u, free_size %u, "
4784-			  "erasing_size %u, unchecked_size %u, "
4785-			  "nr_erasing_blocks %u, avail %u, resrv %u\n",
4786-			  opts->rp_size, c->dirty_size, c->free_size,
4787-			  c->erasing_size, c->unchecked_size,
4788-			  c->nr_erasing_blocks, avail, c->nospc_dirty_size);
4789-
4790-	if (avail > opts->rp_size)
4791-		return 1;
4792-
4793-	/* Always allow root */
4794-	if (capable(CAP_SYS_RESOURCE))
4795-		return 1;
4796-
4797-	jffs2_dbg(1, "forbid writing\n");
4798-	return 0;
4799-}
4800-
4801 /**
4802  *	jffs2_reserve_space - request physical space to write nodes to flash
4803  *	@c: superblock info
4804@@ -57,8 +25,8 @@ static int jffs2_rp_can_write(struct jff
4805  *	@prio: Allocation type - ALLOC_{NORMAL,DELETION}
4806  *
4807  *	Requests a block of physical space on the flash. Returns zero for success
4808- *	and puts 'len' into the appropriate place, or returns -ENOSPC or other
4809- *	error if appropriate. Doesn't return len since that's
4810+ *	and puts 'len' into the appropriate place, or returns -ENOSPC or other
4811+ *	error if appropriate. Doesn't return len since that's
4812  *
4813  *	If it returns zero, jffs2_reserve_space() also downs the per-filesystem
4814  *	allocation semaphore, to prevent more than one allocation from being
4815@@ -86,15 +54,6 @@ int jffs2_reserve_space(struct jffs2_sb_
4816
4817 	spin_lock(&c->erase_completion_lock);
4818
4819-	/*
4820-	 * Check if the free space is greater then size of the reserved pool.
4821-	 * If not, only allow root to proceed with writing.
4822-	 */
4823-	if (prio != ALLOC_DELETION && !jffs2_rp_can_write(c)) {
4824-		ret = -ENOSPC;
4825-		goto out;
4826-	}
4827-
4828 	/* this needs a little more thought (true <tglx> :)) */
4829 	while(ret == -EAGAIN) {
4830 		while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
4831@@ -165,24 +124,7 @@ int jffs2_reserve_space(struct jffs2_sb_
4832 			spin_unlock(&c->erase_completion_lock);
4833
4834 			ret = jffs2_garbage_collect_pass(c);
4835-
4836-			if (ret == -EAGAIN) {
4837-				spin_lock(&c->erase_completion_lock);
4838-				if (c->nr_erasing_blocks &&
4839-				    list_empty(&c->erase_pending_list) &&
4840-				    list_empty(&c->erase_complete_list)) {
4841-					DECLARE_WAITQUEUE(wait, current);
4842-					set_current_state(TASK_UNINTERRUPTIBLE);
4843-					add_wait_queue(&c->erase_wait, &wait);
4844-					jffs2_dbg(1, "%s waiting for erase to complete\n",
4845-						  __func__);
4846-					spin_unlock(&c->erase_completion_lock);
4847-
4848-					schedule();
4849-					remove_wait_queue(&c->erase_wait, &wait);
4850-				} else
4851-					spin_unlock(&c->erase_completion_lock);
4852-			} else if (ret)
4853+			if (ret)
4854 				return ret;
4855
4856 			cond_resched();
4857@@ -200,7 +142,6 @@ int jffs2_reserve_space(struct jffs2_sb_
4858 		}
4859 	}
4860
4861-out:
4862 	spin_unlock(&c->erase_completion_lock);
4863 	if (!ret)
4864 		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
4865@@ -509,7 +450,7 @@ struct jffs2_raw_node_ref *jffs2_add_phy
4866 	jffs2_dbg(1, "%s(): Node at 0x%x(%d), size 0x%x\n",
4867 		  __func__, ofs & ~3, ofs & 3, len);
4868 #if 1
4869-	/* Allow non-obsolete nodes only to be added at the end of c->nextblock,
4870+	/* Allow non-obsolete nodes only to be added at the end of c->nextblock,
4871 	   if c->nextblock is set. Note that wbuf.c will file obsolete nodes
4872 	   even after refiling c->nextblock */
4873 	if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
4874@@ -584,6 +525,8 @@ void jffs2_mark_node_obsolete(struct jff
4875 	int ret, addedsize;
4876 	size_t retlen;
4877 	uint32_t freed_len;
4878+	struct super_block *sb;
4879+	struct MtdNorDev *device;
4880
4881 	if(unlikely(!ref)) {
4882 		pr_notice("EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
4883@@ -595,9 +538,10 @@ void jffs2_mark_node_obsolete(struct jff
4884 		return;
4885 	}
4886 	blocknr = ref->flash_offset / c->sector_size;
4887-	if (blocknr >= c->nr_blocks) {
4888-		pr_notice("raw node at 0x%08x is off the end of device!\n",
4889-			  ref->flash_offset);
4890+	sb = OFNI_BS_2SFFJ(c);
4891+	device = (struct MtdNorDev*)(sb->s_dev);
4892+	if (blocknr >= c->nr_blocks +device->blockStart) {
4893+		pr_notice("raw node at 0x%08x is off the end of device!\n",ref->flash_offset);
4894 		BUG();
4895 	}
4896 	jeb = &c->blocks[blocknr];
4897@@ -778,7 +722,7 @@ void jffs2_mark_node_obsolete(struct jff
4898 	}
4899 	/* XXX FIXME: This is ugly now */
4900 	n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
4901-	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
4902+	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (const u_char *)&n);
4903 	if (ret) {
4904 		pr_warn("Write error in obliterating obsoleted node at 0x%08x: %d\n",
4905 			ref_offset(ref), ret);
4906@@ -846,8 +790,8 @@ int jffs2_thread_should_wake(struct jffs
4907 		return 1;
4908
4909 	if (c->unchecked_size) {
4910-		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, check_ino #%d\n",
4911-			  c->unchecked_size, c->check_ino);
4912+		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
4913+			  c->unchecked_size, c->checked_ino);
4914 		return 1;
4915 	}
4916
4917diff -Nupr old/fs/jffs2/os-linux.h new/fs/jffs2/os-linux.h
4918--- old/fs/jffs2/os-linux.h	2022-05-09 17:22:53.000000000 +0800
4919+++ new/fs/jffs2/os-linux.h	2022-05-09 20:33:21.200000000 +0800
4920@@ -12,59 +12,57 @@
4921 #ifndef __JFFS2_OS_LINUX_H__
4922 #define __JFFS2_OS_LINUX_H__
4923
4924+#include <dirent.h>
4925+#include "fs/fs.h"
4926+#include "jffs2.h"
4927+#include "jffs2_fs_sb.h"
4928+
4929+
4930+/* jffs2 debug output opion */
4931+#define CONFIG_JFFS2_FS_DEBUG       0  /* 1 or 2 */
4932+
4933+/* jffs2 gc thread section */
4934+#define JFFS2_GC_THREAD_PRIORITY  10 /* GC thread's priority */
4935+
4936+/* zlib section*/
4937+#define CONFIG_JFFS2_ZLIB
4938+#define CONFIG_JFFS2_RTIME
4939+#define CONFIG_JFFS2_RUBIN
4940+
4941 /* JFFS2 uses Linux mode bits natively -- no need for conversion */
4942 #define os_to_jffs2_mode(x) (x)
4943 #define jffs2_to_os_mode(x) (x)
4944
4945+#ifndef BUG_ON
4946+#define BUG_ON(x) do {if (unlikely(x)) BUG();} while (0)
4947+#endif
4948+
4949 struct kstatfs;
4950 struct kvec;
4951
4952-#define JFFS2_INODE_INFO(i) (container_of(i, struct jffs2_inode_info, vfs_inode))
4953-#define OFNI_EDONI_2SFFJ(f)  (&(f)->vfs_inode)
4954-#define JFFS2_SB_INFO(sb) (sb->s_fs_info)
4955-#define OFNI_BS_2SFFJ(c)  ((struct super_block *)c->os_priv)
4956
4957+#define JFFS2_INODE_INFO(i) (&(i)->jffs2_i)
4958+#define OFNI_EDONI_2SFFJ(f)  \
4959+        ((struct jffs2_inode *) (((char *)f) - ((char *)(&((struct jffs2_inode *)NULL)->jffs2_i))))
4960
4961 #define JFFS2_F_I_SIZE(f) (OFNI_EDONI_2SFFJ(f)->i_size)
4962 #define JFFS2_F_I_MODE(f) (OFNI_EDONI_2SFFJ(f)->i_mode)
4963-#define JFFS2_F_I_UID(f) (i_uid_read(OFNI_EDONI_2SFFJ(f)))
4964-#define JFFS2_F_I_GID(f) (i_gid_read(OFNI_EDONI_2SFFJ(f)))
4965-#define JFFS2_F_I_RDEV(f) (OFNI_EDONI_2SFFJ(f)->i_rdev)
4966-
4967-#define JFFS2_CLAMP_TIME(t) ((uint32_t)clamp_t(time64_t, (t), 0, U32_MAX))
4968-#define ITIME(sec) ((struct timespec64){sec, 0})
4969-#define JFFS2_NOW() JFFS2_CLAMP_TIME(ktime_get_real_seconds())
4970-#define I_SEC(tv) JFFS2_CLAMP_TIME((tv).tv_sec)
4971-#define JFFS2_F_I_CTIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_ctime)
4972-#define JFFS2_F_I_MTIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_mtime)
4973-#define JFFS2_F_I_ATIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_atime)
4974-#define sleep_on_spinunlock(wq, s)				\
4975-	do {							\
4976-		DECLARE_WAITQUEUE(__wait, current);		\
4977-		add_wait_queue((wq), &__wait);			\
4978-		set_current_state(TASK_UNINTERRUPTIBLE);	\
4979-		spin_unlock(s);					\
4980-		schedule();					\
4981-		remove_wait_queue((wq), &__wait);		\
4982-	} while(0)
4983-
4984-static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
4985-{
4986-	f->highest_version = 0;
4987-	f->fragtree = RB_ROOT;
4988-	f->metadata = NULL;
4989-	f->dents = NULL;
4990-	f->target = NULL;
4991-	f->flags = 0;
4992-	f->usercompr = 0;
4993-}
4994+#define JFFS2_F_I_UID(f) (OFNI_EDONI_2SFFJ(f)->i_uid)
4995+#define JFFS2_F_I_GID(f) (OFNI_EDONI_2SFFJ(f)->i_gid)
4996+#define JFFS2_F_I_CTIME(f) (OFNI_EDONI_2SFFJ(f)->i_ctime)
4997+#define JFFS2_F_I_MTIME(f) (OFNI_EDONI_2SFFJ(f)->i_mtime)
4998+#define JFFS2_F_I_ATIME(f) (OFNI_EDONI_2SFFJ(f)->i_atime)
4999+
5000+#define ITIME(sec) ((struct timespec){sec, 0})
5001+#define I_SEC(tv) ((tv).tv_sec)
5002
5003+#define sleep_on_spinunlock(wq, sl) do {spin_unlock(sl); msleep(100);} while (0)
5004
5005-#define jffs2_is_readonly(c) (OFNI_BS_2SFFJ(c)->s_flags & SB_RDONLY)
5006+#define jffs2_is_readonly(c) (0)
5007
5008 #define SECTOR_ADDR(x) ( (((unsigned long)(x) / c->sector_size) * c->sector_size) )
5009-#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
5010
5011+#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
5012
5013 #ifdef CONFIG_JFFS2_SUMMARY
5014 #define jffs2_can_mark_obsolete(c) (0)
5015@@ -77,10 +75,10 @@ static inline void jffs2_init_inode_info
5016 #define jffs2_write_nand_cleanmarker(c,jeb) (-EIO)
5017
5018 #define jffs2_flash_write(c, ofs, len, retlen, buf) jffs2_flash_direct_write(c, ofs, len, retlen, buf)
5019-#define jffs2_flash_read(c, ofs, len, retlen, buf) (mtd_read((c)->mtd, ofs, len, retlen, buf))
5020-#define jffs2_flush_wbuf_pad(c) ({ do{} while(0); (void)(c), 0; })
5021+#define jffs2_flash_read(c, ofs, len, retlen, buf) jffs2_flash_direct_read(c, ofs, len, retlen, buf)
5022+#define jffs2_flush_wbuf_pad(c) (c=c)
5023 #define jffs2_flush_wbuf_gc(c, i) ({ do{} while(0); (void)(c), (void) i, 0; })
5024-#define jffs2_write_nand_badblock(c,jeb,bad_offset) (1)
5025+#define jffs2_write_nand_badblock(c,jeb,p) (0)
5026 #define jffs2_nand_flash_setup(c) (0)
5027 #define jffs2_nand_flash_cleanup(c) do {} while(0)
5028 #define jffs2_wbuf_dirty(c) (0)
5029@@ -100,7 +98,8 @@ static inline void jffs2_init_inode_info
5030
5031 #else /* NAND and/or ECC'd NOR support present */
5032
5033-#define jffs2_is_writebuffered(c) (c->wbuf != NULL)
5034+/* current not support */
5035+#define jffs2_is_writebuffered(c) (0)
5036
5037 #ifdef CONFIG_JFFS2_SUMMARY
5038 #define jffs2_can_mark_obsolete(c) (0)
5039@@ -142,38 +141,28 @@ void jffs2_dirty_trigger(struct jffs2_sb
5040 #endif /* WRITEBUFFER */
5041
5042 /* background.c */
5043-int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
5044+void jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
5045 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
5046 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
5047
5048 /* dir.c */
5049-extern const struct file_operations jffs2_dir_operations;
5050-extern const struct inode_operations jffs2_dir_inode_operations;
5051-
5052-/* file.c */
5053-extern const struct file_operations jffs2_file_operations;
5054-extern const struct inode_operations jffs2_file_inode_operations;
5055-extern const struct address_space_operations jffs2_file_address_operations;
5056-int jffs2_fsync(struct file *, loff_t, loff_t, int);
5057-int jffs2_do_readpage_unlock(void *data, struct page *pg);
5058-
5059-/* ioctl.c */
5060-long jffs2_ioctl(struct file *, unsigned int, unsigned long);
5061-
5062-/* symlink.c */
5063-extern const struct inode_operations jffs2_symlink_inode_operations;
5064+struct jffs2_inode *jffs2_lookup(struct jffs2_inode *dir_i, const unsigned char *name, int namelen);
5065+int jffs2_create(struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i);
5066+int jffs2_mkdir (struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i);
5067+int jffs2_link (struct jffs2_inode *old_d_inode, struct jffs2_inode *dir_i, const unsigned char *d_name);
5068+int jffs2_symlink(struct jffs2_inode *dir_i, struct jffs2_inode **d_inode, const unsigned char *d_name, const char *target);
5069+int jffs2_unlink(struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name);
5070+int jffs2_rmdir (struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name);
5071+int jffs2_rename (struct jffs2_inode *old_dir_i, struct jffs2_inode *d_inode, const unsigned char *old_d_name,
5072+		  struct jffs2_inode *new_dir_i, const unsigned char *new_d_name);
5073+int jffs2_readdir(struct jffs2_inode *inode, off_t *offset, off_t *int_off, struct dirent *ent);
5074
5075 /* fs.c */
5076-int jffs2_setattr (struct dentry *, struct iattr *);
5077-int jffs2_do_setattr (struct inode *, struct iattr *);
5078-struct inode *jffs2_iget(struct super_block *, unsigned long);
5079-void jffs2_evict_inode (struct inode *);
5080-void jffs2_dirty_inode(struct inode *inode, int flags);
5081-struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode,
5082-			       struct jffs2_raw_inode *ri);
5083-int jffs2_statfs (struct dentry *, struct kstatfs *);
5084-int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc);
5085-int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc);
5086+int jffs2_setattr (struct jffs2_inode *inode, struct IATTR *attr);
5087+struct jffs2_inode *jffs2_iget(struct super_block *sb, uint32_t ino);
5088+int jffs2_iput(struct jffs2_inode * i);
5089+struct jffs2_inode *jffs2_new_inode (struct jffs2_inode *dir_i, int mode, struct jffs2_raw_inode *ri);
5090+
5091 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
5092 			    struct jffs2_inode_info *f);
5093 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
5094@@ -183,15 +172,25 @@ unsigned char *jffs2_gc_fetch_page(struc
5095 				   struct jffs2_inode_info *f,
5096 				   unsigned long offset,
5097 				   unsigned long *priv);
5098+void jffs2_gc_release_page(struct jffs2_sb_info *c,
5099+			   unsigned char *pg,
5100+			   unsigned long *priv);
5101 void jffs2_flash_cleanup(struct jffs2_sb_info *c);
5102
5103+int calculate_inocache_hashsize(uint32_t flash_size);
5104
5105 /* writev.c */
5106 int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,
5107 		       unsigned long count, loff_t to, size_t *retlen);
5108 int jffs2_flash_direct_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5109 			size_t *retlen, const u_char *buf);
5110+int jffs2_flash_direct_read(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5111+			size_t *retlen, const char *buf);
5112
5113-#endif /* __JFFS2_OS_LINUX_H__ */
5114+/* super.c */
5115+int jffs2_fill_super(struct super_block *sb);
5116+int jffs2_mount(int part_no, struct jffs2_inode **root_node, unsigned long mountflags);
5117+int jffs2_umount(struct jffs2_inode *root_node);
5118
5119+#endif /* __JFFS2_OS_LINUX_H__ */
5120
5121diff -Nupr old/fs/jffs2/read.c new/fs/jffs2/read.c
5122--- old/fs/jffs2/read.c	2022-05-09 17:22:53.000000000 +0800
5123+++ new/fs/jffs2/read.c	2022-05-09 20:27:15.580000000 +0800
5124@@ -9,16 +9,15 @@
5125  *
5126  */
5127
5128-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5129-
5130 #include <linux/kernel.h>
5131 #include <linux/slab.h>
5132-#include <linux/crc32.h>
5133 #include <linux/pagemap.h>
5134-#include <linux/mtd/mtd.h>
5135 #include <linux/compiler.h>
5136+#include <mtd_dev.h>
5137 #include "nodelist.h"
5138 #include "compr.h"
5139+#include "los_crc32.h"
5140+#include "user_copy.h"
5141
5142 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
5143 		     struct jffs2_full_dnode *fd, unsigned char *buf,
5144@@ -57,6 +56,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5145 	if (crc != je32_to_cpu(ri->node_crc)) {
5146 		pr_warn("Node CRC %08x != calculated CRC %08x for node at %08x\n",
5147 			je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
5148+			jffs2_dbg_dump_node(c, ref_offset(fd->raw));
5149 		ret = -EIO;
5150 		goto out_ri;
5151 	}
5152@@ -75,9 +75,8 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5153 		goto out_ri;
5154 	});
5155
5156-
5157 	if (ri->compr == JFFS2_COMPR_ZERO) {
5158-		memset(buf, 0, len);
5159+		ret = LOS_UserMemClear(buf, len);
5160 		goto out_ri;
5161 	}
5162
5163@@ -88,7 +87,11 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5164 	   Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
5165 	*/
5166 	if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
5167-		readbuf = buf;
5168+		readbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5169+		if (!readbuf) {
5170+			ret = -ENOMEM;
5171+			goto out_ri;
5172+		}
5173 	} else {
5174 		readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
5175 		if (!readbuf) {
5176@@ -97,14 +100,10 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5177 		}
5178 	}
5179 	if (ri->compr != JFFS2_COMPR_NONE) {
5180-		if (len < je32_to_cpu(ri->dsize)) {
5181-			decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5182-			if (!decomprbuf) {
5183-				ret = -ENOMEM;
5184-				goto out_readbuf;
5185-			}
5186-		} else {
5187-			decomprbuf = buf;
5188+		decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5189+		if (!decomprbuf) {
5190+			ret = -ENOMEM;
5191+			goto out_readbuf;
5192 		}
5193 	} else {
5194 		decomprbuf = readbuf;
5195@@ -113,7 +112,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5196 	jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
5197 		  readbuf);
5198 	ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
5199-			       je32_to_cpu(ri->csize), &readlen, readbuf);
5200+			       je32_to_cpu(ri->csize), &readlen, (char *)readbuf);
5201
5202 	if (!ret && readlen != je32_to_cpu(ri->csize))
5203 		ret = -EIO;
5204@@ -124,6 +123,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5205 	if (crc != je32_to_cpu(ri->data_crc)) {
5206 		pr_warn("Data CRC %08x != calculated CRC %08x for node at %08x\n",
5207 			je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
5208+		jffs2_dbg_dump_node(c, ref_offset(fd->raw));
5209 		ret = -EIO;
5210 		goto out_decomprbuf;
5211 	}
5212@@ -139,8 +139,8 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5213 		}
5214 	}
5215
5216-	if (len < je32_to_cpu(ri->dsize)) {
5217-		memcpy(buf, decomprbuf+ofs, len);
5218+	if (LOS_CopyFromKernel(buf, len, decomprbuf + ofs, len) != 0) {
5219+		ret = -EFAULT;
5220 	}
5221  out_decomprbuf:
5222 	if(decomprbuf != buf && decomprbuf != readbuf)
5223@@ -184,7 +184,10 @@ int jffs2_read_inode_range(struct jffs2_
5224 			}
5225 			jffs2_dbg(1, "Filling non-frag hole from %d-%d\n",
5226 				  offset, offset + holesize);
5227-			memset(buf, 0, holesize);
5228+			ret = LOS_UserMemClear(buf, holesize);
5229+			if (ret != 0) {
5230+				return ret;
5231+			}
5232 			buf += holesize;
5233 			offset += holesize;
5234 			continue;
5235@@ -193,7 +196,10 @@ int jffs2_read_inode_range(struct jffs2_
5236 			jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n",
5237 				  offset, holeend, frag->ofs,
5238 				  frag->ofs + frag->size);
5239-			memset(buf, 0, holeend - offset);
5240+			ret = LOS_UserMemClear(buf, holeend - offset);
5241+			if (ret != 0) {
5242+				return ret;
5243+			}
5244 			buf += holeend - offset;
5245 			offset = holeend;
5246 			frag = frag_next(frag);
5247@@ -214,7 +220,7 @@ int jffs2_read_inode_range(struct jffs2_
5248 			if (ret) {
5249 				jffs2_dbg(1, "%s(): error %d\n",
5250 					  __func__, ret);
5251-				memset(buf, 0, readlen);
5252+				(void)LOS_UserMemClear(buf, readlen);
5253 				return ret;
5254 			}
5255 			buf += readlen;
5256@@ -226,3 +232,15 @@ int jffs2_read_inode_range(struct jffs2_
5257 	return 0;
5258 }
5259
5260+int jffs2_flash_direct_read(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5261+			size_t *retlen, const char *buf)
5262+{
5263+	int ret;
5264+	ret = c->mtd->read(c->mtd, ofs, len, (char *)buf);
5265+	if (ret >= 0) {
5266+		*retlen = ret;
5267+		return 0;
5268+	}
5269+	*retlen = 0;
5270+	return ret;
5271+}
5272\ No newline at end of file
5273diff -Nupr old/fs/jffs2/readinode.c new/fs/jffs2/readinode.c
5274--- old/fs/jffs2/readinode.c	2022-05-09 17:22:53.000000000 +0800
5275+++ new/fs/jffs2/readinode.c	2022-05-09 20:26:31.030000000 +0800
5276@@ -9,17 +9,18 @@
5277  *
5278  */
5279
5280-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5281-
5282 #include <linux/kernel.h>
5283 #include <linux/sched.h>
5284 #include <linux/slab.h>
5285 #include <linux/fs.h>
5286-#include <linux/crc32.h>
5287+#include <linux/delay.h>
5288+#include <linux/semaphore.h>
5289 #include <linux/pagemap.h>
5290-#include <linux/mtd/mtd.h>
5291 #include <linux/compiler.h>
5292+#include <mtd_dev.h>
5293 #include "nodelist.h"
5294+#include "os-linux.h"
5295+#include "los_crc32.h"
5296
5297 /*
5298  * Check the data CRC of the node.
5299@@ -31,9 +32,9 @@
5300 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
5301 {
5302 	struct jffs2_raw_node_ref *ref = tn->fn->raw;
5303-	int err = 0, pointed = 0;
5304+	int err = 0;
5305 	struct jffs2_eraseblock *jeb;
5306-	unsigned char *buffer;
5307+	unsigned char *buffer = NULL;
5308 	uint32_t crc, ofs, len;
5309 	size_t retlen;
5310
5311@@ -61,48 +62,28 @@ static int check_node_data(struct jffs2_
5312 	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",
5313 		ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
5314
5315-#ifndef __ECOS
5316-	/* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
5317-	 * adding and jffs2_flash_read_end() interface. */
5318-	err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
5319-	if (!err && retlen < len) {
5320-		JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
5321-		mtd_unpoint(c->mtd, ofs, retlen);
5322-	} else if (err) {
5323-		if (err != -EOPNOTSUPP)
5324-			JFFS2_WARNING("MTD point failed: error code %d.\n", err);
5325-	} else
5326-		pointed = 1; /* succefully pointed to device */
5327-#endif
5328-
5329-	if (!pointed) {
5330-		buffer = kmalloc(len, GFP_KERNEL);
5331-		if (unlikely(!buffer))
5332-			return -ENOMEM;
5333+	buffer = kmalloc(len, GFP_KERNEL);
5334+	if (unlikely(!buffer))
5335+		return -ENOMEM;
5336
5337-		/* TODO: this is very frequent pattern, make it a separate
5338-		 * routine */
5339-		err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
5340-		if (err) {
5341-			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
5342-			goto free_out;
5343-		}
5344+	/* TODO: this is very frequent pattern, make it a separate
5345+		* routine */
5346+	err = jffs2_flash_read(c, ofs, len, &retlen, (char *)buffer);
5347+	if (err) {
5348+		JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
5349+		goto free_out;
5350+	}
5351
5352-		if (retlen != len) {
5353-			JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
5354-			err = -EIO;
5355-			goto free_out;
5356-		}
5357+	if (retlen != len) {
5358+		JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
5359+		err = -EIO;
5360+		goto free_out;
5361 	}
5362
5363 	/* Continue calculating CRC */
5364 	crc = crc32(tn->partial_crc, buffer, len);
5365-	if(!pointed)
5366-		kfree(buffer);
5367-#ifndef __ECOS
5368-	else
5369-		mtd_unpoint(c->mtd, ofs, len);
5370-#endif
5371+
5372+	kfree(buffer);
5373
5374 	if (crc != tn->data_crc) {
5375 		JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
5376@@ -133,12 +114,7 @@ adj_acc:
5377 	return 0;
5378
5379 free_out:
5380-	if(!pointed)
5381-		kfree(buffer);
5382-#ifndef __ECOS
5383-	else
5384-		mtd_unpoint(c->mtd, ofs, len);
5385-#endif
5386+	kfree(buffer);
5387 	return err;
5388 }
5389
5390@@ -415,8 +391,12 @@ static void eat_last(struct rb_root *roo
5391 		link = &parent->rb_right;
5392
5393 	*link = node->rb_left;
5394-	if (node->rb_left)
5395-		node->rb_left->__rb_parent_color = node->__rb_parent_color;
5396+	if (node->rb_left) {
5397+		node->rb_left->rb_parent_color = node->rb_parent_color;
5398+		// set child parent only
5399+		rb_parent(node->rb_left) = parent;
5400+		node->rb_left = NULL;
5401+	}
5402 }
5403
5404 /* We put the version tree in reverse order, so we can use the same eat_last()
5405@@ -464,8 +444,8 @@ static int jffs2_build_inode_fragtree(st
5406 #ifdef JFFS2_DBG_READINODE_MESSAGES
5407 	this = tn_last(&rii->tn_root);
5408 	while (this) {
5409-		dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
5410-			      this->fn->ofs+this->fn->size, this->overlapped);
5411+		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,
5412+			      this->fn->ofs+this->fn->size, this->overlapped,this->rb.rb_left,this->rb.rb_right,rb_parent(&(this->rb)));
5413 		this = tn_prev(this);
5414 	}
5415 #endif
5416@@ -543,11 +523,13 @@ static int jffs2_build_inode_fragtree(st
5417
5418 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
5419 {
5420-	struct jffs2_tmp_dnode_info *tn, *next;
5421+	struct jffs2_tmp_dnode_info *tn;
5422+	struct rb_node *rbn,*next;
5423
5424-	rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
5425-			jffs2_free_full_dnode(tn->fn);
5426-			jffs2_free_tmp_dnode_info(tn);
5427+	RB_POSTORDER_FOREACH_SAFE(rbn, linux_root, (struct linux_root *)list, next) {
5428+		tn = (struct jffs2_tmp_dnode_info *)rbn;
5429+		jffs2_free_full_dnode(tn->fn);
5430+		jffs2_free_tmp_dnode_info(tn);
5431 	}
5432
5433 	*list = RB_ROOT;
5434@@ -659,7 +641,7 @@ static inline int read_direntry(struct j
5435 		int already = read - sizeof(*rd);
5436
5437 		err = jffs2_flash_read(c, (ref_offset(ref)) + read,
5438-				rd->nsize - already, &read, &fd->name[already]);
5439+				rd->nsize - already, &read, (char *)&fd->name[already]);
5440 		if (unlikely(read != rd->nsize - already) && likely(!err)) {
5441 			jffs2_free_full_dirent(fd);
5442 			JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
5443@@ -690,7 +672,7 @@ static inline int read_direntry(struct j
5444 #endif
5445 	}
5446
5447-	fd->nhash = full_name_hash(NULL, fd->name, rd->nsize);
5448+	fd->nhash = full_name_hash(fd->name, rd->nsize);
5449 	fd->next = NULL;
5450 	fd->name[rd->nsize] = '\0';
5451
5452@@ -956,7 +938,7 @@ static int read_more(struct jffs2_sb_inf
5453
5454 	dbg_readinode("read more %d bytes\n", to_read);
5455
5456-	err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
5457+	err = jffs2_flash_read(c, offs, to_read, &retlen, (char *)(buf + *rdlen));
5458 	if (err) {
5459 		JFFS2_ERROR("can not read %d bytes from 0x%08x, "
5460 			"error code: %d.\n", to_read, offs, err);
5461@@ -1042,7 +1024,7 @@ static int jffs2_get_inode_nodes(struct
5462 		dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
5463
5464 		/* FIXME: point() */
5465-		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
5466+		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, (char *)buf);
5467 		if (err) {
5468 			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
5469 			goto free_out;
5470@@ -1079,6 +1061,7 @@ static int jffs2_get_inode_nodes(struct
5471
5472 		case JFFS2_NODETYPE_DIRENT:
5473
5474+			dbg_readinode("node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref));
5475 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
5476 			    len < sizeof(struct jffs2_raw_dirent)) {
5477 				err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
5478@@ -1094,6 +1077,7 @@ static int jffs2_get_inode_nodes(struct
5479
5480 		case JFFS2_NODETYPE_INODE:
5481
5482+			dbg_readinode("node at %08x (%d) is a data node\n", ref_offset(ref), ref_flags(ref));
5483 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
5484 			    len < sizeof(struct jffs2_raw_inode)) {
5485 				err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
5486@@ -1289,7 +1273,7 @@ static int jffs2_do_read_inode_internal(
5487 			dbg_readinode("symlink's target '%s' cached\n", f->target);
5488 		}
5489
5490-		fallthrough;
5491+		/* fall through... */
5492
5493 	case S_IFBLK:
5494 	case S_IFCHR:
5495@@ -1315,7 +1299,7 @@ static int jffs2_do_read_inode_internal(
5496 		/* OK. We're happy */
5497 		f->metadata = frag_first(&f->fragtree)->node;
5498 		jffs2_free_node_frag(frag_first(&f->fragtree));
5499-		f->fragtree = RB_ROOT;
5500+		f->fragtree.rb_node = NULL;
5501 		break;
5502 	}
5503 	if (f->inocache->state == INO_STATE_READING)
5504@@ -1362,6 +1346,7 @@ int jffs2_do_read_inode(struct jffs2_sb_
5505 			break;
5506
5507 		default:
5508+		    JFFS2_ERROR("Unknown f->inocache->state %d!\n", f->inocache->state);
5509 			BUG();
5510 		}
5511 	}
5512@@ -1375,14 +1360,13 @@ int jffs2_do_read_inode(struct jffs2_sb_
5513 			return -ENOMEM;
5514 		}
5515 		dbg_readinode("creating inocache for root inode\n");
5516-		memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
5517 		f->inocache->ino = f->inocache->pino_nlink = 1;
5518 		f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
5519 		f->inocache->state = INO_STATE_READING;
5520 		jffs2_add_ino_cache(c, f->inocache);
5521 	}
5522 	if (!f->inocache) {
5523-		JFFS2_ERROR("requested to read a nonexistent ino %u\n", ino);
5524+		JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
5525 		return -ENOENT;
5526 	}
5527
5528@@ -1430,6 +1414,11 @@ void jffs2_do_clear_inode(struct jffs2_s
5529
5530 	jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
5531
5532+	if (f->target) {
5533+		kfree(f->target);
5534+		f->target = NULL;
5535+	}
5536+
5537 	fds = f->dents;
5538 	while(fds) {
5539 		fd = fds;
5540diff -Nupr old/fs/jffs2/scan.c new/fs/jffs2/scan.c
5541--- old/fs/jffs2/scan.c	2022-05-09 17:22:53.000000000 +0800
5542+++ new/fs/jffs2/scan.c	2022-05-09 20:23:02.230000000 +0800
5543@@ -9,18 +9,17 @@
5544  *
5545  */
5546
5547-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5548-
5549 #include <linux/kernel.h>
5550 #include <linux/sched.h>
5551 #include <linux/slab.h>
5552-#include <linux/mtd/mtd.h>
5553 #include <linux/pagemap.h>
5554-#include <linux/crc32.h>
5555 #include <linux/compiler.h>
5556 #include "nodelist.h"
5557 #include "summary.h"
5558 #include "debug.h"
5559+#include "mtd_dev.h"
5560+#include "los_typedef.h"
5561+#include "los_crc32.h"
5562
5563 #define DEFAULT_EMPTY_SCAN_SIZE 256
5564
5565@@ -74,7 +73,7 @@ static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
5566 		return ret;
5567 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
5568 		return ret;
5569-	/* Turned wasted size into dirty, since we apparently
5570+	/* Turned wasted size into dirty, since we apparently
5571 	   think it's recoverable now. */
5572 	jeb->dirty_size += jeb->wasted_size;
5573 	c->dirty_size += jeb->wasted_size;
5574@@ -95,40 +94,26 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5575 	unsigned char *flashbuf = NULL;
5576 	uint32_t buf_size = 0;
5577 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
5578-#ifndef __ECOS
5579-	size_t pointlen, try_size;
5580-
5581-	ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
5582-			(void **)&flashbuf, NULL);
5583-	if (!ret && pointlen < c->mtd->size) {
5584-		/* Don't muck about if it won't let us point to the whole flash */
5585-		jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
5586-			  pointlen);
5587-		mtd_unpoint(c->mtd, 0, pointlen);
5588-		flashbuf = NULL;
5589-	}
5590-	if (ret && ret != -EOPNOTSUPP)
5591-		jffs2_dbg(1, "MTD point failed %d\n", ret);
5592-#endif
5593+	struct super_block *sb = NULL;
5594+	struct MtdNorDev *device = NULL;
5595+
5596 	if (!flashbuf) {
5597 		/* For NAND it's quicker to read a whole eraseblock at a time,
5598 		   apparently */
5599 		if (jffs2_cleanmarker_oob(c))
5600-			try_size = c->sector_size;
5601+			buf_size = c->sector_size;
5602 		else
5603-			try_size = PAGE_SIZE;
5604+			buf_size = PAGE_SIZE;
5605
5606 		jffs2_dbg(1, "Trying to allocate readbuf of %zu "
5607-			  "bytes\n", try_size);
5608+			  "bytes\n", buf_size);
5609
5610-		flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
5611+		flashbuf = kmalloc(buf_size, GFP_KERNEL);
5612 		if (!flashbuf)
5613 			return -ENOMEM;
5614
5615 		jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
5616-			  try_size);
5617-
5618-		buf_size = (uint32_t)try_size;
5619+			  buf_size);
5620 	}
5621
5622 	if (jffs2_sum_active()) {
5623@@ -140,7 +125,9 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5624 		}
5625 	}
5626
5627-	for (i=0; i<c->nr_blocks; i++) {
5628+	sb = OFNI_BS_2SFFJ(c);
5629+	device = (struct MtdNorDev*)(sb->s_dev);
5630+	for (i=device->blockStart; i<c->nr_blocks + device->blockStart; i++) {
5631 		struct jffs2_eraseblock *jeb = &c->blocks[i];
5632
5633 		cond_resched();
5634@@ -269,14 +256,10 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5635 			ret = -EIO;
5636 			goto out;
5637 		}
5638-		spin_lock(&c->erase_completion_lock);
5639-		jffs2_garbage_collect_trigger(c);
5640-		spin_unlock(&c->erase_completion_lock);
5641 	}
5642 	ret = 0;
5643  out:
5644-	jffs2_sum_reset_collected(s);
5645-	kfree(s);
5646+	kfree(flashbuf);
5647  out_buf:
5648 	if (buf_size)
5649 		kfree(flashbuf);
5650@@ -413,7 +396,7 @@ static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock
5651 	if (!ref)
5652 		return -ENOMEM;
5653
5654-	/* BEFORE jffs2_build_xattr_subsystem() called,
5655+	/* BEFORE jffs2_build_xattr_subsystem() called,
5656 	 * and AFTER xattr_ref is marked as a dead xref,
5657 	 * ref->xid is used to store 32bit xid, xd is not used
5658 	 * ref->ino is used to store 32bit inode-number, ic is not used
5659@@ -486,10 +469,10 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5660 		struct jffs2_sum_marker *sm;
5661 		void *sumptr = NULL;
5662 		uint32_t sumlen;
5663-
5664+
5665 		if (!buf_size) {
5666 			/* XIP case. Just look, point at the summary if it's there */
5667-			sm = (void *)buf + c->sector_size - sizeof(*sm);
5668+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + c->sector_size - sizeof(*sm));
5669 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5670 				sumptr = buf + je32_to_cpu(sm->offset);
5671 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5672@@ -502,13 +485,13 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5673 				buf_len = sizeof(*sm);
5674
5675 			/* Read as much as we want into the _end_ of the preallocated buffer */
5676-			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5677+			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5678 						  jeb->offset + c->sector_size - buf_len,
5679-						  buf_len);
5680+						  buf_len);
5681 			if (err)
5682 				return err;
5683
5684-			sm = (void *)buf + buf_size - sizeof(*sm);
5685+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + buf_size - sizeof(*sm));
5686 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5687 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5688 				sumptr = buf + buf_size - sumlen;
5689@@ -523,18 +506,15 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5690 					sumptr = kmalloc(sumlen, GFP_KERNEL);
5691 					if (!sumptr)
5692 						return -ENOMEM;
5693-					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5694+					memcpy((uint8_t *)sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5695 				}
5696 				if (buf_len < sumlen) {
5697 					/* Need to read more so that the entire summary node is present */
5698-					err = jffs2_fill_scan_buf(c, sumptr,
5699+					err = jffs2_fill_scan_buf(c, sumptr,
5700 								  jeb->offset + c->sector_size - sumlen,
5701-								  sumlen - buf_len);
5702-					if (err) {
5703-						if (sumlen > buf_size)
5704-							kfree(sumptr);
5705+								  sumlen - buf_len);
5706+					if (err)
5707 						return err;
5708-					}
5709 				}
5710 			}
5711
5712@@ -545,7 +525,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5713
5714 			if (buf_size && sumlen > buf_size)
5715 				kfree(sumptr);
5716-			/* If it returns with a real error, bail.
5717+			/* If it returns with a real error, bail.
5718 			   If it returns positive, that's a block classification
5719 			   (i.e. BLK_STATE_xxx) so return that too.
5720 			   If it returns zero, fall through to full scan. */
5721@@ -607,7 +587,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5722 	/* Now ofs is a complete physical flash offset as it always was... */
5723 	ofs += jeb->offset;
5724
5725-	noise = 10;
5726+	noise = 1;
5727
5728 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
5729
5730@@ -700,7 +680,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5731 				scan_end = buf_len;
5732 				goto more_empty;
5733 			}
5734-
5735+
5736 			/* See how much more there is to read in this eraseblock... */
5737 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
5738 			if (!buf_len) {
5739@@ -950,7 +930,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5740 	jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
5741 		  jeb->offset, jeb->free_size, jeb->dirty_size,
5742 		  jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
5743-
5744+
5745 	/* mark_node_obsolete can add to wasted !! */
5746 	if (jeb->wasted_size) {
5747 		jeb->dirty_size += jeb->wasted_size;
5748@@ -978,7 +958,6 @@ struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uin
5749 		pr_notice("%s(): allocation of inode cache failed\n", __func__);
5750 		return NULL;
5751 	}
5752-	memset(ic, 0, sizeof(*ic));
5753
5754 	ic->ino = ino;
5755 	ic->nodes = (void *)ic;
5756@@ -1069,7 +1048,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5757 	pseudo_random += je32_to_cpu(rd->version);
5758
5759 	/* Should never happen. Did. (OLPC trac #4184)*/
5760-	checkedlen = strnlen(rd->name, rd->nsize);
5761+	checkedlen = strnlen((const char *)rd->name, rd->nsize);
5762 	if (checkedlen < rd->nsize) {
5763 		pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
5764 		       ofs, checkedlen);
5765@@ -1081,7 +1060,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5766 	memcpy(&fd->name, rd->name, checkedlen);
5767 	fd->name[checkedlen] = 0;
5768
5769-	crc = crc32(0, fd->name, checkedlen);
5770+	crc = crc32(0, fd->name, rd->nsize);
5771 	if (crc != je32_to_cpu(rd->name_crc)) {
5772 		pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
5773 			  __func__, ofs, je32_to_cpu(rd->name_crc), crc);
5774@@ -1106,7 +1085,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5775 	fd->next = NULL;
5776 	fd->version = je32_to_cpu(rd->version);
5777 	fd->ino = je32_to_cpu(rd->ino);
5778-	fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
5779+	fd->nhash = full_name_hash(fd->name, checkedlen);
5780 	fd->type = rd->type;
5781 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
5782
5783diff
5784@@ -9,18 +9,17 @@
5785  *
5786  */
5787
5788-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5789-
5790 #include <linux/kernel.h>
5791 #include <linux/sched.h>
5792 #include <linux/slab.h>
5793-#include <linux/mtd/mtd.h>
5794 #include <linux/pagemap.h>
5795-#include <linux/crc32.h>
5796 #include <linux/compiler.h>
5797 #include "nodelist.h"
5798 #include "summary.h"
5799 #include "debug.h"
5800+#include "mtd_dev.h"
5801+#include "los_typedef.h"
5802+#include "los_crc32.h"
5803
5804 #define DEFAULT_EMPTY_SCAN_SIZE 256
5805
5806@@ -74,7 +73,7 @@ static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
5807 		return ret;
5808 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
5809 		return ret;
5810-	/* Turned wasted size into dirty, since we apparently
5811+	/* Turned wasted size into dirty, since we apparently
5812 	   think it's recoverable now. */
5813 	jeb->dirty_size += jeb->wasted_size;
5814 	c->dirty_size += jeb->wasted_size;
5815@@ -95,40 +94,26 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5816 	unsigned char *flashbuf = NULL;
5817 	uint32_t buf_size = 0;
5818 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
5819-#ifndef __ECOS
5820-	size_t pointlen, try_size;
5821-
5822-	ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
5823-			(void **)&flashbuf, NULL);
5824-	if (!ret && pointlen < c->mtd->size) {
5825-		/* Don't muck about if it won't let us point to the whole flash */
5826-		jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
5827-			  pointlen);
5828-		mtd_unpoint(c->mtd, 0, pointlen);
5829-		flashbuf = NULL;
5830-	}
5831-	if (ret && ret != -EOPNOTSUPP)
5832-		jffs2_dbg(1, "MTD point failed %d\n", ret);
5833-#endif
5834+	struct super_block *sb = NULL;
5835+	struct MtdNorDev *device = NULL;
5836+
5837 	if (!flashbuf) {
5838 		/* For NAND it's quicker to read a whole eraseblock at a time,
5839 		   apparently */
5840 		if (jffs2_cleanmarker_oob(c))
5841-			try_size = c->sector_size;
5842+			buf_size = c->sector_size;
5843 		else
5844-			try_size = PAGE_SIZE;
5845+			buf_size = PAGE_SIZE;
5846
5847 		jffs2_dbg(1, "Trying to allocate readbuf of %zu "
5848-			  "bytes\n", try_size);
5849+			  "bytes\n", buf_size);
5850
5851-		flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
5852+		flashbuf = kmalloc(buf_size, GFP_KERNEL);
5853 		if (!flashbuf)
5854 			return -ENOMEM;
5855
5856 		jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
5857-			  try_size);
5858-
5859-		buf_size = (uint32_t)try_size;
5860+			  buf_size);
5861 	}
5862
5863 	if (jffs2_sum_active()) {
5864@@ -140,7 +125,9 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5865 		}
5866 	}
5867
5868-	for (i=0; i<c->nr_blocks; i++) {
5869+	sb = OFNI_BS_2SFFJ(c);
5870+	device = (struct MtdNorDev*)(sb->s_dev);
5871+	for (i=device->blockStart; i<c->nr_blocks + device->blockStart; i++) {
5872 		struct jffs2_eraseblock *jeb = &c->blocks[i];
5873
5874 		cond_resched();
5875@@ -269,14 +256,10 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5876 			ret = -EIO;
5877 			goto out;
5878 		}
5879-		spin_lock(&c->erase_completion_lock);
5880-		jffs2_garbage_collect_trigger(c);
5881-		spin_unlock(&c->erase_completion_lock);
5882 	}
5883 	ret = 0;
5884  out:
5885-	jffs2_sum_reset_collected(s);
5886-	kfree(s);
5887+	kfree(flashbuf);
5888  out_buf:
5889 	if (buf_size)
5890 		kfree(flashbuf);
5891@@ -413,7 +396,7 @@ static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock
5892 	if (!ref)
5893 		return -ENOMEM;
5894
5895-	/* BEFORE jffs2_build_xattr_subsystem() called,
5896+	/* BEFORE jffs2_build_xattr_subsystem() called,
5897 	 * and AFTER xattr_ref is marked as a dead xref,
5898 	 * ref->xid is used to store 32bit xid, xd is not used
5899 	 * ref->ino is used to store 32bit inode-number, ic is not used
5900@@ -486,10 +469,10 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5901 		struct jffs2_sum_marker *sm;
5902 		void *sumptr = NULL;
5903 		uint32_t sumlen;
5904-
5905+
5906 		if (!buf_size) {
5907 			/* XIP case. Just look, point at the summary if it's there */
5908-			sm = (void *)buf + c->sector_size - sizeof(*sm);
5909+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + c->sector_size - sizeof(*sm));
5910 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5911 				sumptr = buf + je32_to_cpu(sm->offset);
5912 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5913@@ -502,13 +485,13 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5914 				buf_len = sizeof(*sm);
5915
5916 			/* Read as much as we want into the _end_ of the preallocated buffer */
5917-			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5918+			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5919 						  jeb->offset + c->sector_size - buf_len,
5920-						  buf_len);
5921+						  buf_len);
5922 			if (err)
5923 				return err;
5924
5925-			sm = (void *)buf + buf_size - sizeof(*sm);
5926+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + buf_size - sizeof(*sm));
5927 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5928 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5929 				sumptr = buf + buf_size - sumlen;
5930@@ -523,18 +506,15 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5931 					sumptr = kmalloc(sumlen, GFP_KERNEL);
5932 					if (!sumptr)
5933 						return -ENOMEM;
5934-					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5935+					memcpy((uint8_t *)sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5936 				}
5937 				if (buf_len < sumlen) {
5938 					/* Need to read more so that the entire summary node is present */
5939-					err = jffs2_fill_scan_buf(c, sumptr,
5940+					err = jffs2_fill_scan_buf(c, sumptr,
5941 								  jeb->offset + c->sector_size - sumlen,
5942-								  sumlen - buf_len);
5943-					if (err) {
5944-						if (sumlen > buf_size)
5945-							kfree(sumptr);
5946+								  sumlen - buf_len);
5947+					if (err)
5948 						return err;
5949-					}
5950 				}
5951 			}
5952
5953@@ -545,7 +525,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5954
5955 			if (buf_size && sumlen > buf_size)
5956 				kfree(sumptr);
5957-			/* If it returns with a real error, bail.
5958+			/* If it returns with a real error, bail.
5959 			   If it returns positive, that's a block classification
5960 			   (i.e. BLK_STATE_xxx) so return that too.
5961 			   If it returns zero, fall through to full scan. */
5962@@ -607,7 +587,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5963 	/* Now ofs is a complete physical flash offset as it always was... */
5964 	ofs += jeb->offset;
5965
5966-	noise = 10;
5967+	noise = 1;
5968
5969 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
5970
5971@@ -700,7 +680,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5972 				scan_end = buf_len;
5973 				goto more_empty;
5974 			}
5975-
5976+
5977 			/* See how much more there is to read in this eraseblock... */
5978 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
5979 			if (!buf_len) {
5980@@ -950,7 +930,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5981 	jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
5982 		  jeb->offset, jeb->free_size, jeb->dirty_size,
5983 		  jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
5984-
5985+
5986 	/* mark_node_obsolete can add to wasted !! */
5987 	if (jeb->wasted_size) {
5988 		jeb->dirty_size += jeb->wasted_size;
5989@@ -978,7 +958,6 @@ struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uin
5990 		pr_notice("%s(): allocation of inode cache failed\n", __func__);
5991 		return NULL;
5992 	}
5993-	memset(ic, 0, sizeof(*ic));
5994
5995 	ic->ino = ino;
5996 	ic->nodes = (void *)ic;
5997@@ -1069,7 +1048,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5998 	pseudo_random += je32_to_cpu(rd->version);
5999
6000 	/* Should never happen. Did. (OLPC trac #4184)*/
6001-	checkedlen = strnlen(rd->name, rd->nsize);
6002+	checkedlen = strnlen((const char *)rd->name, rd->nsize);
6003 	if (checkedlen < rd->nsize) {
6004 		pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
6005 		       ofs, checkedlen);
6006@@ -1081,7 +1060,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
6007 	memcpy(&fd->name, rd->name, checkedlen);
6008 	fd->name[checkedlen] = 0;
6009
6010-	crc = crc32(0, fd->name, checkedlen);
6011+	crc = crc32(0, fd->name, rd->nsize);
6012 	if (crc != je32_to_cpu(rd->name_crc)) {
6013 		pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
6014 			  __func__, ofs, je32_to_cpu(rd->name_crc), crc);
6015@@ -1106,7 +1085,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
6016 	fd->next = NULL;
6017 	fd->version = je32_to_cpu(rd->version);
6018 	fd->ino = je32_to_cpu(rd->ino);
6019-	fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
6020+	fd->nhash = full_name_hash(fd->name, checkedlen);
6021 	fd->type = rd->type;
6022 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
6023
6024diff -Nupr old/fs/jffs2/security.c new/fs/jffs2/security.c
6025--- old/fs/jffs2/security.c	2022-05-09 17:15:24.350000000 +0800
6026+++ new/fs/jffs2/security.c	1970-01-01 08:00:00.000000000 +0800
6027@@ -1,72 +0,0 @@
6028-/*
6029- * JFFS2 -- Journalling Flash File System, Version 2.
6030- *
6031- * Copyright © 2006  NEC Corporation
6032- *
6033- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
6034- *
6035- * For licensing information, see the file 'LICENCE' in this directory.
6036- *
6037- */
6038-
6039-#include <linux/kernel.h>
6040-#include <linux/slab.h>
6041-#include <linux/fs.h>
6042-#include <linux/time.h>
6043-#include <linux/pagemap.h>
6044-#include <linux/highmem.h>
6045-#include <linux/crc32.h>
6046-#include <linux/jffs2.h>
6047-#include <linux/xattr.h>
6048-#include <linux/mtd/mtd.h>
6049-#include <linux/security.h>
6050-#include "nodelist.h"
6051-
6052-/* ---- Initial Security Label(s) Attachment callback --- */
6053-static int jffs2_initxattrs(struct inode *inode,
6054-			    const struct xattr *xattr_array, void *fs_info)
6055-{
6056-	const struct xattr *xattr;
6057-	int err = 0;
6058-
6059-	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
6060-		err = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
6061-					xattr->name, xattr->value,
6062-					xattr->value_len, 0);
6063-		if (err < 0)
6064-			break;
6065-	}
6066-	return err;
6067-}
6068-
6069-/* ---- Initial Security Label(s) Attachment ----------- */
6070-int jffs2_init_security(struct inode *inode, struct inode *dir,
6071-			const struct qstr *qstr)
6072-{
6073-	return security_inode_init_security(inode, dir, qstr,
6074-					    &jffs2_initxattrs, NULL);
6075-}
6076-
6077-/* ---- XATTR Handler for "security.*" ----------------- */
6078-static int jffs2_security_getxattr(const struct xattr_handler *handler,
6079-				   struct dentry *unused, struct inode *inode,
6080-				   const char *name, void *buffer, size_t size)
6081-{
6082-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_SECURITY,
6083-				 name, buffer, size);
6084-}
6085-
6086-static int jffs2_security_setxattr(const struct xattr_handler *handler,
6087-				   struct dentry *unused, struct inode *inode,
6088-				   const char *name, const void *buffer,
6089-				   size_t size, int flags)
6090-{
6091-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
6092-				 name, buffer, size, flags);
6093-}
6094-
6095-const struct xattr_handler jffs2_security_xattr_handler = {
6096-	.prefix = XATTR_SECURITY_PREFIX,
6097-	.set = jffs2_security_setxattr,
6098-	.get = jffs2_security_getxattr
6099-};
6100diff -Nupr old/fs/jffs2/summary.c new/fs/jffs2/summary.c
6101--- old/fs/jffs2/summary.c	2022-05-09 17:22:53.000000000 +0800
6102+++ new/fs/jffs2/summary.c	2022-05-09 20:13:24.440000000 +0800
6103@@ -10,16 +10,20 @@
6104  * For licensing information, see the file 'LICENCE' in this directory.
6105  *
6106  */
6107+#include "summary.h"
6108
6109+#ifdef CONFIG_JFFS2_SUMMARY
6110+
6111+#ifndef pr_fmt
6112 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6113+#endif
6114
6115 #include <linux/kernel.h>
6116 #include <linux/slab.h>
6117-#include <linux/mtd/mtd.h>
6118+#include <mtd_dev.h>
6119 #include <linux/pagemap.h>
6120-#include <linux/crc32.h>
6121+#include "los_crc32.h"
6122 #include <linux/compiler.h>
6123-#include <linux/vmalloc.h>
6124 #include "nodelist.h"
6125 #include "debug.h"
6126
6127@@ -388,11 +392,25 @@ static int jffs2_sum_process_sum_data(st
6128 {
6129 	struct jffs2_inode_cache *ic;
6130 	struct jffs2_full_dirent *fd;
6131-	void *sp;
6132+	uintptr_t sp;
6133 	int i, ino;
6134 	int err;
6135
6136-	sp = summary->sum;
6137+	sp = (uintptr_t)summary->sum;
6138+
6139+#if 0
6140+	PRINTK("summary: %x %x %d %d %x %x %d %x %x  %p %p\n",
6141+	je16_to_cpu(summary->magic),
6142+	je16_to_cpu(summary->nodetype),
6143+	je32_to_cpu(summary->totlen),
6144+	je32_to_cpu(summary->hdr_crc),
6145+	je32_to_cpu(summary->sum_num),
6146+	je32_to_cpu(summary->cln_mkr),
6147+	je32_to_cpu(summary->padded),
6148+	je32_to_cpu(summary->sum_crc),
6149+	je32_to_cpu(summary->node_crc),
6150+	sp, summary->sum);
6151+#endif
6152
6153 	for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
6154 		dbg_summary("processing summary index %d\n", i);
6155@@ -404,10 +422,12 @@ static int jffs2_sum_process_sum_data(st
6156 		if (err)
6157 			return err;
6158
6159+		//PRINTK("sum type %d \n", je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype));
6160+
6161 		switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
6162 			case JFFS2_NODETYPE_INODE: {
6163 				struct jffs2_sum_inode_flash *spi;
6164-				spi = sp;
6165+				spi = (struct jffs2_sum_inode_flash *)sp;
6166
6167 				ino = je32_to_cpu(spi->inode);
6168
6169@@ -428,13 +448,29 @@ static int jffs2_sum_process_sum_data(st
6170
6171 				sp += JFFS2_SUMMARY_INODE_SIZE;
6172
6173+				//PRINTK("1 sp + %d %p\n", JFFS2_SUMMARY_INODE_SIZE, sp);
6174+
6175 				break;
6176 			}
6177
6178 			case JFFS2_NODETYPE_DIRENT: {
6179 				struct jffs2_sum_dirent_flash *spd;
6180 				int checkedlen;
6181-				spd = sp;
6182+				spd = (struct jffs2_sum_dirent_flash *)sp;
6183+
6184+
6185+#if 0
6186+					PRINTK("dir: %x %d %d %d %d %d %d %d %d\n",
6187+	je16_to_cpu(spd->nodetype),
6188+	je32_to_cpu(spd->totlen),
6189+	je32_to_cpu(spd->offset),
6190+	je32_to_cpu(spd->pino),
6191+	je32_to_cpu(spd->version),
6192+	je32_to_cpu(spd->ino),
6193+	spd->nsize,
6194+	spd->type,
6195+	spd->name);
6196+#endif
6197
6198 				dbg_summary("Dirent at 0x%08x-0x%08x\n",
6199 					    jeb->offset + je32_to_cpu(spd->offset),
6200@@ -442,7 +478,7 @@ static int jffs2_sum_process_sum_data(st
6201
6202
6203 				/* This should never happen, but https://dev.laptop.org/ticket/4184 */
6204-				checkedlen = strnlen(spd->name, spd->nsize);
6205+				checkedlen = strnlen((const char *)spd->name, spd->nsize);
6206 				if (!checkedlen) {
6207 					pr_err("Dirent at %08x has zero at start of name. Aborting mount.\n",
6208 					       jeb->offset +
6209@@ -463,6 +499,7 @@ static int jffs2_sum_process_sum_data(st
6210
6211 				memcpy(&fd->name, spd->name, checkedlen);
6212 				fd->name[checkedlen] = 0;
6213+				//PRINTK("add %s \n", fd->name);
6214
6215 				ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
6216 				if (!ic) {
6217@@ -476,15 +513,19 @@ static int jffs2_sum_process_sum_data(st
6218 				fd->next = NULL;
6219 				fd->version = je32_to_cpu(spd->version);
6220 				fd->ino = je32_to_cpu(spd->ino);
6221-				fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
6222+				fd->nhash = full_name_hash((const unsigned char *)fd->name, checkedlen);
6223 				fd->type = spd->type;
6224
6225 				jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
6226
6227 				*pseudo_random += je32_to_cpu(spd->version);
6228
6229+				//PRINTK("2 sp before add %p\n", sp);
6230+
6231 				sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
6232
6233+			    //PRINTK("2 sp + %d %p\n", JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize), sp);
6234+
6235 				break;
6236 			}
6237 #ifdef CONFIG_JFFS2_FS_XATTR
6238@@ -493,7 +534,7 @@ static int jffs2_sum_process_sum_data(st
6239 				struct jffs2_sum_xattr_flash *spx;
6240
6241 				spx = (struct jffs2_sum_xattr_flash *)sp;
6242-				dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
6243+				dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
6244 					    jeb->offset + je32_to_cpu(spx->offset),
6245 					    jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen),
6246 					    je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
6247@@ -526,7 +567,7 @@ static int jffs2_sum_process_sum_data(st
6248 				spr = (struct jffs2_sum_xref_flash *)sp;
6249 				dbg_summary("xref at %#08x-%#08x\n",
6250 					    jeb->offset + je32_to_cpu(spr->offset),
6251-					    jeb->offset + je32_to_cpu(spr->offset) +
6252+					    jeb->offset + je32_to_cpu(spr->offset) +
6253 					    (uint32_t)PAD(sizeof(struct jffs2_raw_xref)));
6254
6255 				ref = jffs2_alloc_xattr_ref();
6256@@ -679,7 +720,7 @@ static int jffs2_sum_write_data(struct j
6257 	struct jffs2_sum_marker *sm;
6258 	struct kvec vecs[2];
6259 	uint32_t sum_ofs;
6260-	void *wpage;
6261+	uintptr_t wpage;
6262 	int ret;
6263 	size_t retlen;
6264
6265@@ -713,14 +754,14 @@ static int jffs2_sum_write_data(struct j
6266 	isum.padded = cpu_to_je32(c->summary->sum_padded);
6267 	isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
6268 	isum.sum_num = cpu_to_je32(c->summary->sum_num);
6269-	wpage = c->summary->sum_buf;
6270+	wpage = (uintptr_t)c->summary->sum_buf;
6271
6272 	while (c->summary->sum_num) {
6273 		temp = c->summary->sum_list_head;
6274
6275 		switch (je16_to_cpu(temp->u.nodetype)) {
6276 			case JFFS2_NODETYPE_INODE: {
6277-				struct jffs2_sum_inode_flash *sino_ptr = wpage;
6278+				struct jffs2_sum_inode_flash *sino_ptr = (struct jffs2_sum_inode_flash *)wpage;
6279
6280 				sino_ptr->nodetype = temp->i.nodetype;
6281 				sino_ptr->inode = temp->i.inode;
6282@@ -734,7 +775,7 @@ static int jffs2_sum_write_data(struct j
6283 			}
6284
6285 			case JFFS2_NODETYPE_DIRENT: {
6286-				struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
6287+				struct jffs2_sum_dirent_flash *sdrnt_ptr = (struct jffs2_sum_dirent_flash *)wpage;
6288
6289 				sdrnt_ptr->nodetype = temp->d.nodetype;
6290 				sdrnt_ptr->totlen = temp->d.totlen;
6291@@ -802,7 +843,7 @@ static int jffs2_sum_write_data(struct j
6292
6293 	wpage += padsize;
6294
6295-	sm = wpage;
6296+	sm = (struct jffs2_sum_marker *)wpage;
6297 	sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
6298 	sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
6299
6300@@ -847,7 +888,7 @@ static int jffs2_sum_write_data(struct j
6301 /* Write out summary information - called from jffs2_do_reserve_space */
6302
6303 int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
6304-	__must_hold(&c->erase_completion_block)
6305+	//__must_hold(&c->erase_completion_block)
6306 {
6307 	int datasize, infosize, padsize;
6308 	struct jffs2_eraseblock *jeb;
6309@@ -875,3 +916,5 @@ int jffs2_sum_write_sumnode(struct jffs2
6310 	spin_lock(&c->erase_completion_lock);
6311 	return ret;
6312 }
6313+
6314+#endif
6315diff -Nupr old/fs/jffs2/summary.h new/fs/jffs2/summary.h
6316--- old/fs/jffs2/summary.h	2022-05-09 17:22:53.000000000 +0800
6317+++ new/fs/jffs2/summary.h	2022-05-09 20:35:43.430000000 +0800
6318@@ -19,8 +19,9 @@
6319    anyway. */
6320 #define MAX_SUMMARY_SIZE 65536
6321
6322-#include <linux/uio.h>
6323-#include <linux/jffs2.h>
6324+#include <sys/uio.h>
6325+#include <linux/types.h>
6326+#include "jffs2.h"
6327
6328 #define BLK_STATE_ALLFF		0
6329 #define BLK_STATE_CLEAN		1
6330@@ -169,6 +170,10 @@ struct jffs2_sum_marker
6331
6332 #define JFFS2_SUMMARY_FRAME_SIZE (sizeof(struct jffs2_raw_summary) + sizeof(struct jffs2_sum_marker))
6333
6334+#ifdef LOSCFG_FS_JFFS2_SUMMARY
6335+#define CONFIG_JFFS2_SUMMARY
6336+#endif
6337+
6338 #ifdef CONFIG_JFFS2_SUMMARY	/* SUMMARY SUPPORT ENABLED */
6339
6340 #define jffs2_sum_active() (1)
6341diff -Nupr old/fs/jffs2/super.c new/fs/jffs2/super.c
6342--- old/fs/jffs2/super.c	2022-05-09 17:22:53.000000000 +0800
6343+++ new/fs/jffs2/super.c	2022-05-09 20:09:32.170000000 +0800
6344@@ -9,433 +9,188 @@
6345  *
6346  */
6347
6348-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6349
6350-#include <linux/kernel.h>
6351-#include <linux/module.h>
6352-#include <linux/slab.h>
6353-#include <linux/init.h>
6354-#include <linux/list.h>
6355-#include <linux/fs.h>
6356-#include <linux/err.h>
6357-#include <linux/mount.h>
6358-#include <linux/fs_context.h>
6359-#include <linux/fs_parser.h>
6360-#include <linux/jffs2.h>
6361-#include <linux/pagemap.h>
6362-#include <linux/mtd/super.h>
6363-#include <linux/ctype.h>
6364-#include <linux/namei.h>
6365-#include <linux/seq_file.h>
6366-#include <linux/exportfs.h>
6367-#include "compr.h"
6368+#include "jffs2.h"
6369 #include "nodelist.h"
6370+#include "jffs2_fs_sb.h"
6371+#include "mtd_dev.h"
6372+#include "mtd_partition.h"
6373+#include "compr.h"
6374+#include "jffs2_hash.h"
6375
6376-static void jffs2_put_super(struct super_block *);
6377-
6378-static struct kmem_cache *jffs2_inode_cachep;
6379+static unsigned char jffs2_mounted_number = 0; /* a counter to track the number of jffs2 instances mounted */
6380+struct MtdNorDev jffs2_dev_list[CONFIG_MTD_PATTITION_NUM];
6381
6382-static struct inode *jffs2_alloc_inode(struct super_block *sb)
6383+/*
6384+ * fill in the superblock
6385+ */
6386+int jffs2_fill_super(struct super_block *sb)
6387 {
6388-	struct jffs2_inode_info *f;
6389-
6390-	f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
6391-	if (!f)
6392-		return NULL;
6393-	return &f->vfs_inode;
6394-}
6395+	int ret;
6396+	struct jffs2_sb_info *c;
6397+	struct MtdNorDev *device;
6398
6399-static void jffs2_free_inode(struct inode *inode)
6400-{
6401-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
6402+	c = JFFS2_SB_INFO(sb);
6403+	device = (struct MtdNorDev*)(sb->s_dev);
6404
6405-	kfree(f->target);
6406-	kmem_cache_free(jffs2_inode_cachep, f);
6407-}
6408+	(void)mutex_init(&c->alloc_sem);
6409+	(void)mutex_init(&c->erase_free_sem);
6410+	spin_lock_init(&c->erase_completion_lock);
6411+	spin_lock_init(&c->inocache_lock);
6412
6413-static void jffs2_i_init_once(void *foo)
6414-{
6415-	struct jffs2_inode_info *f = foo;
6416+	/* sector size is the erase block size */
6417+	c->sector_size = device->blockSize;
6418+	c->flash_size  = (device->blockEnd - device->blockStart + 1) * device->blockSize;
6419+	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
6420
6421-	mutex_init(&f->sem);
6422-	inode_init_once(&f->vfs_inode);
6423-}
6424+	ret = jffs2_do_mount_fs(c);
6425+	if (ret) {
6426+		(void)mutex_destroy(&c->alloc_sem);
6427+		(void)mutex_destroy(&c->erase_free_sem);
6428+		return ret;
6429+	}
6430+	D1(printk(KERN_DEBUG "jffs2_fill_super(): Getting root inode\n"));
6431+
6432+	sb->s_root = jffs2_iget(sb, 1);
6433+
6434+	if (IS_ERR(sb->s_root)) {
6435+		D1(printk(KERN_WARNING "get root inode failed\n"));
6436+		ret = PTR_ERR(sb->s_root);
6437+		sb->s_root = NULL;
6438+		jffs2_free_ino_caches(c);
6439+		jffs2_free_raw_node_refs(c);
6440+		free(c->blocks);
6441+		(void)mutex_destroy(&c->alloc_sem);
6442+		(void)mutex_destroy(&c->erase_free_sem);
6443
6444-static const char *jffs2_compr_name(unsigned int compr)
6445-{
6446-	switch (compr) {
6447-	case JFFS2_COMPR_MODE_NONE:
6448-		return "none";
6449-#ifdef CONFIG_JFFS2_LZO
6450-	case JFFS2_COMPR_MODE_FORCELZO:
6451-		return "lzo";
6452-#endif
6453-#ifdef CONFIG_JFFS2_ZLIB
6454-	case JFFS2_COMPR_MODE_FORCEZLIB:
6455-		return "zlib";
6456-#endif
6457-	default:
6458-		/* should never happen; programmer error */
6459-		WARN_ON(1);
6460-		return "";
6461+		return ret;
6462 	}
6463-}
6464-
6465-static int jffs2_show_options(struct seq_file *s, struct dentry *root)
6466-{
6467-	struct jffs2_sb_info *c = JFFS2_SB_INFO(root->d_sb);
6468-	struct jffs2_mount_opts *opts = &c->mount_opts;
6469-
6470-	if (opts->override_compr)
6471-		seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
6472-	if (opts->set_rp_size)
6473-		seq_printf(s, ",rp_size=%u", opts->rp_size / 1024);
6474-
6475-	return 0;
6476-}
6477-
6478-static int jffs2_sync_fs(struct super_block *sb, int wait)
6479-{
6480-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
6481-
6482-#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
6483-	if (jffs2_is_writebuffered(c))
6484-		cancel_delayed_work_sync(&c->wbuf_dwork);
6485-#endif
6486-
6487-	mutex_lock(&c->alloc_sem);
6488-	jffs2_flush_wbuf_pad(c);
6489-	mutex_unlock(&c->alloc_sem);
6490 	return 0;
6491 }
6492
6493-static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
6494-					 uint32_t generation)
6495+int jffs2_mount(int part_no, struct jffs2_inode **root_node, unsigned long mountflags)
6496 {
6497-	/* We don't care about i_generation. We'll destroy the flash
6498-	   before we start re-using inode numbers anyway. And even
6499-	   if that wasn't true, we'd have other problems...*/
6500-	return jffs2_iget(sb, ino);
6501-}
6502-
6503-static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
6504-					 int fh_len, int fh_type)
6505-{
6506-        return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
6507-                                    jffs2_nfs_get_inode);
6508-}
6509-
6510-static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
6511-					 int fh_len, int fh_type)
6512-{
6513-        return generic_fh_to_parent(sb, fid, fh_len, fh_type,
6514-                                    jffs2_nfs_get_inode);
6515-}
6516-
6517-static struct dentry *jffs2_get_parent(struct dentry *child)
6518-{
6519-	struct jffs2_inode_info *f;
6520-	uint32_t pino;
6521-
6522-	BUG_ON(!d_is_dir(child));
6523-
6524-	f = JFFS2_INODE_INFO(d_inode(child));
6525-
6526-	pino = f->inocache->pino_nlink;
6527-
6528-	JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
6529-		    f->inocache->ino, pino);
6530-
6531-	return d_obtain_alias(jffs2_iget(child->d_sb, pino));
6532-}
6533-
6534-static const struct export_operations jffs2_export_ops = {
6535-	.get_parent = jffs2_get_parent,
6536-	.fh_to_dentry = jffs2_fh_to_dentry,
6537-	.fh_to_parent = jffs2_fh_to_parent,
6538-};
6539-
6540-/*
6541- * JFFS2 mount options.
6542- *
6543- * Opt_source: The source device
6544- * Opt_override_compr: override default compressor
6545- * Opt_rp_size: size of reserved pool in KiB
6546- */
6547-enum {
6548-	Opt_override_compr,
6549-	Opt_rp_size,
6550-};
6551-
6552-static const struct constant_table jffs2_param_compr[] = {
6553-	{"none",	JFFS2_COMPR_MODE_NONE },
6554-#ifdef CONFIG_JFFS2_LZO
6555-	{"lzo",		JFFS2_COMPR_MODE_FORCELZO },
6556-#endif
6557-#ifdef CONFIG_JFFS2_ZLIB
6558-	{"zlib",	JFFS2_COMPR_MODE_FORCEZLIB },
6559-#endif
6560-	{}
6561-};
6562+	struct super_block *sb = NULL;
6563+	struct jffs2_sb_info *c = NULL;
6564+	LOS_DL_LIST *part_head = NULL;
6565+	struct MtdDev *spinor_mtd = NULL;
6566+	mtd_partition *mtd_part = GetSpinorPartitionHead();
6567+	int ret;
6568
6569-static const struct fs_parameter_spec jffs2_fs_parameters[] = {
6570-	fsparam_enum	("compr",	Opt_override_compr, jffs2_param_compr),
6571-	fsparam_u32	("rp_size",	Opt_rp_size),
6572-	{}
6573-};
6574+	jffs2_dbg(1, "begin los_jffs2_mount:%d\n", part_no);
6575
6576-static int jffs2_parse_param(struct fs_context *fc, struct fs_parameter *param)
6577-{
6578-	struct fs_parse_result result;
6579-	struct jffs2_sb_info *c = fc->s_fs_info;
6580-	int opt;
6581-
6582-	opt = fs_parse(fc, jffs2_fs_parameters, param, &result);
6583-	if (opt < 0)
6584-		return opt;
6585-
6586-	switch (opt) {
6587-	case Opt_override_compr:
6588-		c->mount_opts.compr = result.uint_32;
6589-		c->mount_opts.override_compr = true;
6590-		break;
6591-	case Opt_rp_size:
6592-		if (result.uint_32 > UINT_MAX / 1024)
6593-			return invalf(fc, "jffs2: rp_size unrepresentable");
6594-		c->mount_opts.rp_size = result.uint_32 * 1024;
6595-		c->mount_opts.set_rp_size = true;
6596-		break;
6597-	default:
6598-		return -EINVAL;
6599+	sb = zalloc(sizeof(struct super_block));
6600+	if (sb == NULL) {
6601+		return -ENOMEM;
6602 	}
6603
6604-	return 0;
6605-}
6606-
6607-static inline void jffs2_update_mount_opts(struct fs_context *fc)
6608-{
6609-	struct jffs2_sb_info *new_c = fc->s_fs_info;
6610-	struct jffs2_sb_info *c = JFFS2_SB_INFO(fc->root->d_sb);
6611-
6612-	mutex_lock(&c->alloc_sem);
6613-	if (new_c->mount_opts.override_compr) {
6614-		c->mount_opts.override_compr = new_c->mount_opts.override_compr;
6615-		c->mount_opts.compr = new_c->mount_opts.compr;
6616-	}
6617-	if (new_c->mount_opts.set_rp_size) {
6618-		c->mount_opts.set_rp_size = new_c->mount_opts.set_rp_size;
6619-		c->mount_opts.rp_size = new_c->mount_opts.rp_size;
6620+	ret = Jffs2HashInit(&sb->s_node_hash_lock, &sb->s_node_hash[0]);
6621+	if (ret) {
6622+		free(sb);
6623+		return ret;
6624+	}
6625+	part_head = &(GetSpinorPartitionHead()->node_info);
6626+	LOS_DL_LIST_FOR_EACH_ENTRY(mtd_part,part_head, mtd_partition, node_info) {
6627+		if (mtd_part->patitionnum == part_no)
6628+			break;
6629+	}
6630+#ifndef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
6631+	spinor_mtd = GetMtd("spinor");
6632+#else
6633+	spinor_mtd = (struct MtdDev *)LOS_DL_LIST_ENTRY(part_head->pstNext, mtd_partition, node_info)->mtd_info;
6634+#endif
6635+	if (spinor_mtd == NULL) {
6636+		free(sb);
6637+		return -EPERM;
6638+	}
6639+	jffs2_dev_list[part_no].blockEnd = mtd_part->end_block;
6640+	jffs2_dev_list[part_no].blockSize = spinor_mtd->eraseSize;
6641+	jffs2_dev_list[part_no].blockStart = mtd_part->start_block;
6642+#ifndef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
6643+	(void)FreeMtd(spinor_mtd);
6644+#endif
6645+	sb->jffs2_sb.mtd = mtd_part->mtd_info;
6646+	sb->s_dev = &jffs2_dev_list[part_no];
6647+
6648+	c = JFFS2_SB_INFO(sb);
6649+	c->flash_size  = (mtd_part->end_block - mtd_part->start_block + 1) * spinor_mtd->eraseSize;
6650+	c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
6651+	c->sector_size = spinor_mtd->eraseSize;
6652+
6653+	jffs2_dbg(1, "C mtd_size:%d,mtd-erase:%d,blocks:%d,hashsize:%d\n",
6654+		c->flash_size, c->sector_size, c->flash_size / c->sector_size, c->inocache_hashsize);
6655+
6656+	c->inocache_list = zalloc(sizeof(struct jffs2_inode_cache *) * c->inocache_hashsize);
6657+	if (c->inocache_list == NULL) {
6658+		free(sb);
6659+		return -ENOMEM;
6660+	}
6661+	if (jffs2_mounted_number++ == 0) {
6662+		(void)jffs2_create_slab_caches(); // No error check, cannot fail
6663+		(void)jffs2_compressors_init();
6664 	}
6665-	mutex_unlock(&c->alloc_sem);
6666-}
6667-
6668-static int jffs2_reconfigure(struct fs_context *fc)
6669-{
6670-	struct super_block *sb = fc->root->d_sb;
6671-
6672-	sync_filesystem(sb);
6673-	jffs2_update_mount_opts(fc);
6674-
6675-	return jffs2_do_remount_fs(sb, fc);
6676-}
6677-
6678-static const struct super_operations jffs2_super_operations =
6679-{
6680-	.alloc_inode =	jffs2_alloc_inode,
6681-	.free_inode =	jffs2_free_inode,
6682-	.put_super =	jffs2_put_super,
6683-	.statfs =	jffs2_statfs,
6684-	.evict_inode =	jffs2_evict_inode,
6685-	.dirty_inode =	jffs2_dirty_inode,
6686-	.show_options =	jffs2_show_options,
6687-	.sync_fs =	jffs2_sync_fs,
6688-};
6689-
6690-/*
6691- * fill in the superblock
6692- */
6693-static int jffs2_fill_super(struct super_block *sb, struct fs_context *fc)
6694-{
6695-	struct jffs2_sb_info *c = sb->s_fs_info;
6696-
6697-	jffs2_dbg(1, "jffs2_get_sb_mtd():"
6698-		  " New superblock for device %d (\"%s\")\n",
6699-		  sb->s_mtd->index, sb->s_mtd->name);
6700-
6701-	c->mtd = sb->s_mtd;
6702-	c->os_priv = sb;
6703-
6704-	if (c->mount_opts.rp_size > c->mtd->size)
6705-		return invalf(fc, "jffs2: Too large reserve pool specified, max is %llu KB",
6706-			      c->mtd->size / 1024);
6707-
6708-	/* Initialize JFFS2 superblock locks, the further initialization will
6709-	 * be done later */
6710-	mutex_init(&c->alloc_sem);
6711-	mutex_init(&c->erase_free_sem);
6712-	init_waitqueue_head(&c->erase_wait);
6713-	init_waitqueue_head(&c->inocache_wq);
6714-	spin_lock_init(&c->erase_completion_lock);
6715-	spin_lock_init(&c->inocache_lock);
6716-
6717-	sb->s_op = &jffs2_super_operations;
6718-	sb->s_export_op = &jffs2_export_ops;
6719-	sb->s_flags = sb->s_flags | SB_NOATIME;
6720-	sb->s_xattr = jffs2_xattr_handlers;
6721-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
6722-	sb->s_flags |= SB_POSIXACL;
6723-#endif
6724-	return jffs2_do_fill_super(sb, fc);
6725-}
6726-
6727-static int jffs2_get_tree(struct fs_context *fc)
6728-{
6729-	return get_tree_mtd(fc, jffs2_fill_super);
6730-}
6731-
6732-static void jffs2_free_fc(struct fs_context *fc)
6733-{
6734-	kfree(fc->s_fs_info);
6735-}
6736
6737-static const struct fs_context_operations jffs2_context_ops = {
6738-	.free		= jffs2_free_fc,
6739-	.parse_param	= jffs2_parse_param,
6740-	.get_tree	= jffs2_get_tree,
6741-	.reconfigure	= jffs2_reconfigure,
6742-};
6743+	ret = jffs2_fill_super(sb);
6744+	if (ret) {
6745+		if (--jffs2_mounted_number == 0) {
6746+			jffs2_destroy_slab_caches();
6747+			(void)jffs2_compressors_exit();
6748+		}
6749
6750-static int jffs2_init_fs_context(struct fs_context *fc)
6751-{
6752-	struct jffs2_sb_info *ctx;
6753+		free(sb);
6754+		free(c->inocache_list);
6755+		c->inocache_list = NULL;
6756+		return ret;
6757+	}
6758
6759-	ctx = kzalloc(sizeof(struct jffs2_sb_info), GFP_KERNEL);
6760-	if (!ctx)
6761-		return -ENOMEM;
6762+	if (!(mountflags & MS_RDONLY)) {
6763+		jffs2_start_garbage_collect_thread(c);
6764+	}
6765
6766-	fc->s_fs_info = ctx;
6767-	fc->ops = &jffs2_context_ops;
6768+	sb->s_mount_flags = mountflags;
6769+	*root_node = sb->s_root;
6770 	return 0;
6771 }
6772
6773-static void jffs2_put_super (struct super_block *sb)
6774+int jffs2_umount(struct jffs2_inode *root_node)
6775 {
6776+	struct super_block *sb = root_node->i_sb;
6777 	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
6778+	struct jffs2_full_dirent *fd, *next;
6779
6780-	jffs2_dbg(2, "%s()\n", __func__);
6781+	D2(PRINTK("Jffs2Umount\n"));
6782
6783-	mutex_lock(&c->alloc_sem);
6784-	jffs2_flush_wbuf_pad(c);
6785-	mutex_unlock(&c->alloc_sem);
6786-
6787-	jffs2_sum_exit(c);
6788-
6789-	jffs2_free_ino_caches(c);
6790-	jffs2_free_raw_node_refs(c);
6791-	kvfree(c->blocks);
6792-	jffs2_flash_cleanup(c);
6793-	kfree(c->inocache_list);
6794-	jffs2_clear_xattr_subsystem(c);
6795-	mtd_sync(c->mtd);
6796-	jffs2_dbg(1, "%s(): returning\n", __func__);
6797-}
6798-
6799-static void jffs2_kill_sb(struct super_block *sb)
6800-{
6801-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
6802-	if (c && !sb_rdonly(sb))
6803+	// Only really umount if this is the only mount
6804+	if (!(sb->s_mount_flags & MS_RDONLY)) {
6805 		jffs2_stop_garbage_collect_thread(c);
6806-	kill_mtd_super(sb);
6807-	kfree(c);
6808-}
6809-
6810-static struct file_system_type jffs2_fs_type = {
6811-	.owner =	THIS_MODULE,
6812-	.name =		"jffs2",
6813-	.init_fs_context = jffs2_init_fs_context,
6814-	.parameters =	jffs2_fs_parameters,
6815-	.kill_sb =	jffs2_kill_sb,
6816-};
6817-MODULE_ALIAS_FS("jffs2");
6818+	}
6819
6820-static int __init init_jffs2_fs(void)
6821-{
6822-	int ret;
6823+	// free directory entries
6824+	for (fd = root_node->jffs2_i.dents; fd; fd = next) {
6825+		next = fd->next;
6826+		jffs2_free_full_dirent(fd);
6827+	}
6828
6829-	/* Paranoia checks for on-medium structures. If we ask GCC
6830-	   to pack them with __attribute__((packed)) then it _also_
6831-	   assumes that they're not aligned -- so it emits crappy
6832-	   code on some architectures. Ideally we want an attribute
6833-	   which means just 'no padding', without the alignment
6834-	   thing. But GCC doesn't have that -- we have to just
6835-	   hope the structs are the right sizes, instead. */
6836-	BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
6837-	BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
6838-	BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
6839-	BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
6840-
6841-	pr_info("version 2.2."
6842-#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
6843-	       " (NAND)"
6844-#endif
6845-#ifdef CONFIG_JFFS2_SUMMARY
6846-	       " (SUMMARY) "
6847-#endif
6848-	       " © 2001-2006 Red Hat, Inc.\n");
6849+	free(root_node);
6850
6851-	jffs2_inode_cachep = kmem_cache_create("jffs2_i",
6852-					     sizeof(struct jffs2_inode_info),
6853-					     0, (SLAB_RECLAIM_ACCOUNT|
6854-						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
6855-					     jffs2_i_init_once);
6856-	if (!jffs2_inode_cachep) {
6857-		pr_err("error: Failed to initialise inode cache\n");
6858-		return -ENOMEM;
6859-	}
6860-	ret = jffs2_compressors_init();
6861-	if (ret) {
6862-		pr_err("error: Failed to initialise compressors\n");
6863-		goto out;
6864-	}
6865-	ret = jffs2_create_slab_caches();
6866-	if (ret) {
6867-		pr_err("error: Failed to initialise slab caches\n");
6868-		goto out_compressors;
6869-	}
6870-	ret = register_filesystem(&jffs2_fs_type);
6871-	if (ret) {
6872-		pr_err("error: Failed to register filesystem\n");
6873-		goto out_slab;
6874+	// Clean up the super block and root_node inode
6875+	jffs2_free_ino_caches(c);
6876+	jffs2_free_raw_node_refs(c);
6877+	free(c->blocks);
6878+	c->blocks = NULL;
6879+	free(c->inocache_list);
6880+	c->inocache_list = NULL;
6881+	(void)Jffs2HashDeinit(&sb->s_node_hash_lock);
6882+
6883+	(void)mutex_destroy(&c->alloc_sem);
6884+	(void)mutex_destroy(&c->erase_free_sem);
6885+	free(sb);
6886+	// That's all folks.
6887+	D2(PRINTK("Jffs2Umount No current mounts\n"));
6888+
6889+	if (--jffs2_mounted_number == 0) {
6890+		jffs2_destroy_slab_caches();
6891+		(void)jffs2_compressors_exit();
6892 	}
6893 	return 0;
6894-
6895- out_slab:
6896-	jffs2_destroy_slab_caches();
6897- out_compressors:
6898-	jffs2_compressors_exit();
6899- out:
6900-	kmem_cache_destroy(jffs2_inode_cachep);
6901-	return ret;
6902 }
6903-
6904-static void __exit exit_jffs2_fs(void)
6905-{
6906-	unregister_filesystem(&jffs2_fs_type);
6907-	jffs2_destroy_slab_caches();
6908-	jffs2_compressors_exit();
6909-
6910-	/*
6911-	 * Make sure all delayed rcu free inodes are flushed before we
6912-	 * destroy cache.
6913-	 */
6914-	rcu_barrier();
6915-	kmem_cache_destroy(jffs2_inode_cachep);
6916-}
6917-
6918-module_init(init_jffs2_fs);
6919-module_exit(exit_jffs2_fs);
6920-
6921-MODULE_DESCRIPTION("The Journalling Flash File System, v2");
6922-MODULE_AUTHOR("Red Hat, Inc.");
6923-MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
6924-		       // the sake of this tag. It's Free Software.
6925diff -Nupr old/fs/jffs2/symlink.c new/fs/jffs2/symlink.c
6926--- old/fs/jffs2/symlink.c	2022-05-09 17:15:24.350000000 +0800
6927+++ new/fs/jffs2/symlink.c	1970-01-01 08:00:00.000000000 +0800
6928@@ -1,19 +0,0 @@
6929-/*
6930- * JFFS2 -- Journalling Flash File System, Version 2.
6931- *
6932- * Copyright © 2001-2007 Red Hat, Inc.
6933- *
6934- * Created by David Woodhouse <dwmw2@infradead.org>
6935- *
6936- * For licensing information, see the file 'LICENCE' in this directory.
6937- *
6938- */
6939-
6940-#include "nodelist.h"
6941-
6942-const struct inode_operations jffs2_symlink_inode_operations =
6943-{
6944-	.get_link =	simple_get_link,
6945-	.setattr =	jffs2_setattr,
6946-	.listxattr =	jffs2_listxattr,
6947-};
6948diff -Nupr old/fs/jffs2/wbuf.c new/fs/jffs2/wbuf.c
6949--- old/fs/jffs2/wbuf.c	2022-05-09 17:15:24.350000000 +0800
6950+++ new/fs/jffs2/wbuf.c	1970-01-01 08:00:00.000000000 +0800
6951@@ -1,1350 +0,0 @@
6952-/*
6953- * JFFS2 -- Journalling Flash File System, Version 2.
6954- *
6955- * Copyright © 2001-2007 Red Hat, Inc.
6956- * Copyright © 2004 Thomas Gleixner <tglx@linutronix.de>
6957- *
6958- * Created by David Woodhouse <dwmw2@infradead.org>
6959- * Modified debugged and enhanced by Thomas Gleixner <tglx@linutronix.de>
6960- *
6961- * For licensing information, see the file 'LICENCE' in this directory.
6962- *
6963- */
6964-
6965-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6966-
6967-#include <linux/kernel.h>
6968-#include <linux/slab.h>
6969-#include <linux/mtd/mtd.h>
6970-#include <linux/crc32.h>
6971-#include <linux/mtd/rawnand.h>
6972-#include <linux/jiffies.h>
6973-#include <linux/sched.h>
6974-#include <linux/writeback.h>
6975-
6976-#include "nodelist.h"
6977-
6978-/* For testing write failures */
6979-#undef BREAKME
6980-#undef BREAKMEHEADER
6981-
6982-#ifdef BREAKME
6983-static unsigned char *brokenbuf;
6984-#endif
6985-
6986-#define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) )
6987-#define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) )
6988-
6989-/* max. erase failures before we mark a block bad */
6990-#define MAX_ERASE_FAILURES 	2
6991-
6992-struct jffs2_inodirty {
6993-	uint32_t ino;
6994-	struct jffs2_inodirty *next;
6995-};
6996-
6997-static struct jffs2_inodirty inodirty_nomem;
6998-
6999-static int jffs2_wbuf_pending_for_ino(struct jffs2_sb_info *c, uint32_t ino)
7000-{
7001-	struct jffs2_inodirty *this = c->wbuf_inodes;
7002-
7003-	/* If a malloc failed, consider _everything_ dirty */
7004-	if (this == &inodirty_nomem)
7005-		return 1;
7006-
7007-	/* If ino == 0, _any_ non-GC writes mean 'yes' */
7008-	if (this && !ino)
7009-		return 1;
7010-
7011-	/* Look to see if the inode in question is pending in the wbuf */
7012-	while (this) {
7013-		if (this->ino == ino)
7014-			return 1;
7015-		this = this->next;
7016-	}
7017-	return 0;
7018-}
7019-
7020-static void jffs2_clear_wbuf_ino_list(struct jffs2_sb_info *c)
7021-{
7022-	struct jffs2_inodirty *this;
7023-
7024-	this = c->wbuf_inodes;
7025-
7026-	if (this != &inodirty_nomem) {
7027-		while (this) {
7028-			struct jffs2_inodirty *next = this->next;
7029-			kfree(this);
7030-			this = next;
7031-		}
7032-	}
7033-	c->wbuf_inodes = NULL;
7034-}
7035-
7036-static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino)
7037-{
7038-	struct jffs2_inodirty *new;
7039-
7040-	/* Schedule delayed write-buffer write-out */
7041-	jffs2_dirty_trigger(c);
7042-
7043-	if (jffs2_wbuf_pending_for_ino(c, ino))
7044-		return;
7045-
7046-	new = kmalloc(sizeof(*new), GFP_KERNEL);
7047-	if (!new) {
7048-		jffs2_dbg(1, "No memory to allocate inodirty. Fallback to all considered dirty\n");
7049-		jffs2_clear_wbuf_ino_list(c);
7050-		c->wbuf_inodes = &inodirty_nomem;
7051-		return;
7052-	}
7053-	new->ino = ino;
7054-	new->next = c->wbuf_inodes;
7055-	c->wbuf_inodes = new;
7056-	return;
7057-}
7058-
7059-static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c)
7060-{
7061-	struct list_head *this, *next;
7062-	static int n;
7063-
7064-	if (list_empty(&c->erasable_pending_wbuf_list))
7065-		return;
7066-
7067-	list_for_each_safe(this, next, &c->erasable_pending_wbuf_list) {
7068-		struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
7069-
7070-		jffs2_dbg(1, "Removing eraseblock at 0x%08x from erasable_pending_wbuf_list...\n",
7071-			  jeb->offset);
7072-		list_del(this);
7073-		if ((jiffies + (n++)) & 127) {
7074-			/* Most of the time, we just erase it immediately. Otherwise we
7075-			   spend ages scanning it on mount, etc. */
7076-			jffs2_dbg(1, "...and adding to erase_pending_list\n");
7077-			list_add_tail(&jeb->list, &c->erase_pending_list);
7078-			c->nr_erasing_blocks++;
7079-			jffs2_garbage_collect_trigger(c);
7080-		} else {
7081-			/* Sometimes, however, we leave it elsewhere so it doesn't get
7082-			   immediately reused, and we spread the load a bit. */
7083-			jffs2_dbg(1, "...and adding to erasable_list\n");
7084-			list_add_tail(&jeb->list, &c->erasable_list);
7085-		}
7086-	}
7087-}
7088-
7089-#define REFILE_NOTEMPTY 0
7090-#define REFILE_ANYWAY   1
7091-
7092-static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty)
7093-{
7094-	jffs2_dbg(1, "About to refile bad block at %08x\n", jeb->offset);
7095-
7096-	/* File the existing block on the bad_used_list.... */
7097-	if (c->nextblock == jeb)
7098-		c->nextblock = NULL;
7099-	else /* Not sure this should ever happen... need more coffee */
7100-		list_del(&jeb->list);
7101-	if (jeb->first_node) {
7102-		jffs2_dbg(1, "Refiling block at %08x to bad_used_list\n",
7103-			  jeb->offset);
7104-		list_add(&jeb->list, &c->bad_used_list);
7105-	} else {
7106-		BUG_ON(allow_empty == REFILE_NOTEMPTY);
7107-		/* It has to have had some nodes or we couldn't be here */
7108-		jffs2_dbg(1, "Refiling block at %08x to erase_pending_list\n",
7109-			  jeb->offset);
7110-		list_add(&jeb->list, &c->erase_pending_list);
7111-		c->nr_erasing_blocks++;
7112-		jffs2_garbage_collect_trigger(c);
7113-	}
7114-
7115-	if (!jffs2_prealloc_raw_node_refs(c, jeb, 1)) {
7116-		uint32_t oldfree = jeb->free_size;
7117-
7118-		jffs2_link_node_ref(c, jeb,
7119-				    (jeb->offset+c->sector_size-oldfree) | REF_OBSOLETE,
7120-				    oldfree, NULL);
7121-		/* convert to wasted */
7122-		c->wasted_size += oldfree;
7123-		jeb->wasted_size += oldfree;
7124-		c->dirty_size -= oldfree;
7125-		jeb->dirty_size -= oldfree;
7126-	}
7127-
7128-	jffs2_dbg_dump_block_lists_nolock(c);
7129-	jffs2_dbg_acct_sanity_check_nolock(c,jeb);
7130-	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
7131-}
7132-
7133-static struct jffs2_raw_node_ref **jffs2_incore_replace_raw(struct jffs2_sb_info *c,
7134-							    struct jffs2_inode_info *f,
7135-							    struct jffs2_raw_node_ref *raw,
7136-							    union jffs2_node_union *node)
7137-{
7138-	struct jffs2_node_frag *frag;
7139-	struct jffs2_full_dirent *fd;
7140-
7141-	dbg_noderef("incore_replace_raw: node at %p is {%04x,%04x}\n",
7142-		    node, je16_to_cpu(node->u.magic), je16_to_cpu(node->u.nodetype));
7143-
7144-	BUG_ON(je16_to_cpu(node->u.magic) != 0x1985 &&
7145-	       je16_to_cpu(node->u.magic) != 0);
7146-
7147-	switch (je16_to_cpu(node->u.nodetype)) {
7148-	case JFFS2_NODETYPE_INODE:
7149-		if (f->metadata && f->metadata->raw == raw) {
7150-			dbg_noderef("Will replace ->raw in f->metadata at %p\n", f->metadata);
7151-			return &f->metadata->raw;
7152-		}
7153-		frag = jffs2_lookup_node_frag(&f->fragtree, je32_to_cpu(node->i.offset));
7154-		BUG_ON(!frag);
7155-		/* Find a frag which refers to the full_dnode we want to modify */
7156-		while (!frag->node || frag->node->raw != raw) {
7157-			frag = frag_next(frag);
7158-			BUG_ON(!frag);
7159-		}
7160-		dbg_noderef("Will replace ->raw in full_dnode at %p\n", frag->node);
7161-		return &frag->node->raw;
7162-
7163-	case JFFS2_NODETYPE_DIRENT:
7164-		for (fd = f->dents; fd; fd = fd->next) {
7165-			if (fd->raw == raw) {
7166-				dbg_noderef("Will replace ->raw in full_dirent at %p\n", fd);
7167-				return &fd->raw;
7168-			}
7169-		}
7170-		BUG();
7171-
7172-	default:
7173-		dbg_noderef("Don't care about replacing raw for nodetype %x\n",
7174-			    je16_to_cpu(node->u.nodetype));
7175-		break;
7176-	}
7177-	return NULL;
7178-}
7179-
7180-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
7181-static int jffs2_verify_write(struct jffs2_sb_info *c, unsigned char *buf,
7182-			      uint32_t ofs)
7183-{
7184-	int ret;
7185-	size_t retlen;
7186-	char *eccstr;
7187-
7188-	ret = mtd_read(c->mtd, ofs, c->wbuf_pagesize, &retlen, c->wbuf_verify);
7189-	if (ret && ret != -EUCLEAN && ret != -EBADMSG) {
7190-		pr_warn("%s(): Read back of page at %08x failed: %d\n",
7191-			__func__, c->wbuf_ofs, ret);
7192-		return ret;
7193-	} else if (retlen != c->wbuf_pagesize) {
7194-		pr_warn("%s(): Read back of page at %08x gave short read: %zd not %d\n",
7195-			__func__, ofs, retlen, c->wbuf_pagesize);
7196-		return -EIO;
7197-	}
7198-	if (!memcmp(buf, c->wbuf_verify, c->wbuf_pagesize))
7199-		return 0;
7200-
7201-	if (ret == -EUCLEAN)
7202-		eccstr = "corrected";
7203-	else if (ret == -EBADMSG)
7204-		eccstr = "correction failed";
7205-	else
7206-		eccstr = "OK or unused";
7207-
7208-	pr_warn("Write verify error (ECC %s) at %08x. Wrote:\n",
7209-		eccstr, c->wbuf_ofs);
7210-	print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1,
7211-		       c->wbuf, c->wbuf_pagesize, 0);
7212-
7213-	pr_warn("Read back:\n");
7214-	print_hex_dump(KERN_WARNING, "", DUMP_PREFIX_OFFSET, 16, 1,
7215-		       c->wbuf_verify, c->wbuf_pagesize, 0);
7216-
7217-	return -EIO;
7218-}
7219-#else
7220-#define jffs2_verify_write(c,b,o) (0)
7221-#endif
7222-
7223-/* Recover from failure to write wbuf. Recover the nodes up to the
7224- * wbuf, not the one which we were starting to try to write. */
7225-
7226-static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
7227-{
7228-	struct jffs2_eraseblock *jeb, *new_jeb;
7229-	struct jffs2_raw_node_ref *raw, *next, *first_raw = NULL;
7230-	size_t retlen;
7231-	int ret;
7232-	int nr_refile = 0;
7233-	unsigned char *buf;
7234-	uint32_t start, end, ofs, len;
7235-
7236-	jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
7237-
7238-	spin_lock(&c->erase_completion_lock);
7239-	if (c->wbuf_ofs % c->mtd->erasesize)
7240-		jffs2_block_refile(c, jeb, REFILE_NOTEMPTY);
7241-	else
7242-		jffs2_block_refile(c, jeb, REFILE_ANYWAY);
7243-	spin_unlock(&c->erase_completion_lock);
7244-
7245-	BUG_ON(!ref_obsolete(jeb->last_node));
7246-
7247-	/* Find the first node to be recovered, by skipping over every
7248-	   node which ends before the wbuf starts, or which is obsolete. */
7249-	for (next = raw = jeb->first_node; next; raw = next) {
7250-		next = ref_next(raw);
7251-
7252-		if (ref_obsolete(raw) ||
7253-		    (next && ref_offset(next) <= c->wbuf_ofs)) {
7254-			dbg_noderef("Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
7255-				    ref_offset(raw), ref_flags(raw),
7256-				    (ref_offset(raw) + ref_totlen(c, jeb, raw)),
7257-				    c->wbuf_ofs);
7258-			continue;
7259-		}
7260-		dbg_noderef("First node to be recovered is at 0x%08x(%d)-0x%08x\n",
7261-			    ref_offset(raw), ref_flags(raw),
7262-			    (ref_offset(raw) + ref_totlen(c, jeb, raw)));
7263-
7264-		first_raw = raw;
7265-		break;
7266-	}
7267-
7268-	if (!first_raw) {
7269-		/* All nodes were obsolete. Nothing to recover. */
7270-		jffs2_dbg(1, "No non-obsolete nodes to be recovered. Just filing block bad\n");
7271-		c->wbuf_len = 0;
7272-		return;
7273-	}
7274-
7275-	start = ref_offset(first_raw);
7276-	end = ref_offset(jeb->last_node);
7277-	nr_refile = 1;
7278-
7279-	/* Count the number of refs which need to be copied */
7280-	while ((raw = ref_next(raw)) != jeb->last_node)
7281-		nr_refile++;
7282-
7283-	dbg_noderef("wbuf recover %08x-%08x (%d bytes in %d nodes)\n",
7284-		    start, end, end - start, nr_refile);
7285-
7286-	buf = NULL;
7287-	if (start < c->wbuf_ofs) {
7288-		/* First affected node was already partially written.
7289-		 * Attempt to reread the old data into our buffer. */
7290-
7291-		buf = kmalloc(end - start, GFP_KERNEL);
7292-		if (!buf) {
7293-			pr_crit("Malloc failure in wbuf recovery. Data loss ensues.\n");
7294-
7295-			goto read_failed;
7296-		}
7297-
7298-		/* Do the read... */
7299-		ret = mtd_read(c->mtd, start, c->wbuf_ofs - start, &retlen,
7300-			       buf);
7301-
7302-		/* ECC recovered ? */
7303-		if ((ret == -EUCLEAN || ret == -EBADMSG) &&
7304-		    (retlen == c->wbuf_ofs - start))
7305-			ret = 0;
7306-
7307-		if (ret || retlen != c->wbuf_ofs - start) {
7308-			pr_crit("Old data are already lost in wbuf recovery. Data loss ensues.\n");
7309-
7310-			kfree(buf);
7311-			buf = NULL;
7312-		read_failed:
7313-			first_raw = ref_next(first_raw);
7314-			nr_refile--;
7315-			while (first_raw && ref_obsolete(first_raw)) {
7316-				first_raw = ref_next(first_raw);
7317-				nr_refile--;
7318-			}
7319-
7320-			/* If this was the only node to be recovered, give up */
7321-			if (!first_raw) {
7322-				c->wbuf_len = 0;
7323-				return;
7324-			}
7325-
7326-			/* It wasn't. Go on and try to recover nodes complete in the wbuf */
7327-			start = ref_offset(first_raw);
7328-			dbg_noderef("wbuf now recover %08x-%08x (%d bytes in %d nodes)\n",
7329-				    start, end, end - start, nr_refile);
7330-
7331-		} else {
7332-			/* Read succeeded. Copy the remaining data from the wbuf */
7333-			memcpy(buf + (c->wbuf_ofs - start), c->wbuf, end - c->wbuf_ofs);
7334-		}
7335-	}
7336-	/* OK... we're to rewrite (end-start) bytes of data from first_raw onwards.
7337-	   Either 'buf' contains the data, or we find it in the wbuf */
7338-
7339-	/* ... and get an allocation of space from a shiny new block instead */
7340-	ret = jffs2_reserve_space_gc(c, end-start, &len, JFFS2_SUMMARY_NOSUM_SIZE);
7341-	if (ret) {
7342-		pr_warn("Failed to allocate space for wbuf recovery. Data loss ensues.\n");
7343-		kfree(buf);
7344-		return;
7345-	}
7346-
7347-	/* The summary is not recovered, so it must be disabled for this erase block */
7348-	jffs2_sum_disable_collecting(c->summary);
7349-
7350-	ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, nr_refile);
7351-	if (ret) {
7352-		pr_warn("Failed to allocate node refs for wbuf recovery. Data loss ensues.\n");
7353-		kfree(buf);
7354-		return;
7355-	}
7356-
7357-	ofs = write_ofs(c);
7358-
7359-	if (end-start >= c->wbuf_pagesize) {
7360-		/* Need to do another write immediately, but it's possible
7361-		   that this is just because the wbuf itself is completely
7362-		   full, and there's nothing earlier read back from the
7363-		   flash. Hence 'buf' isn't necessarily what we're writing
7364-		   from. */
7365-		unsigned char *rewrite_buf = buf?:c->wbuf;
7366-		uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);
7367-
7368-		jffs2_dbg(1, "Write 0x%x bytes at 0x%08x in wbuf recover\n",
7369-			  towrite, ofs);
7370-
7371-#ifdef BREAKMEHEADER
7372-		static int breakme;
7373-		if (breakme++ == 20) {
7374-			pr_notice("Faking write error at 0x%08x\n", ofs);
7375-			breakme = 0;
7376-			mtd_write(c->mtd, ofs, towrite, &retlen, brokenbuf);
7377-			ret = -EIO;
7378-		} else
7379-#endif
7380-			ret = mtd_write(c->mtd, ofs, towrite, &retlen,
7381-					rewrite_buf);
7382-
7383-		if (ret || retlen != towrite || jffs2_verify_write(c, rewrite_buf, ofs)) {
7384-			/* Argh. We tried. Really we did. */
7385-			pr_crit("Recovery of wbuf failed due to a second write error\n");
7386-			kfree(buf);
7387-
7388-			if (retlen)
7389-				jffs2_add_physical_node_ref(c, ofs | REF_OBSOLETE, ref_totlen(c, jeb, first_raw), NULL);
7390-
7391-			return;
7392-		}
7393-		pr_notice("Recovery of wbuf succeeded to %08x\n", ofs);
7394-
7395-		c->wbuf_len = (end - start) - towrite;
7396-		c->wbuf_ofs = ofs + towrite;
7397-		memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
7398-		/* Don't muck about with c->wbuf_inodes. False positives are harmless. */
7399-	} else {
7400-		/* OK, now we're left with the dregs in whichever buffer we're using */
7401-		if (buf) {
7402-			memcpy(c->wbuf, buf, end-start);
7403-		} else {
7404-			memmove(c->wbuf, c->wbuf + (start - c->wbuf_ofs), end - start);
7405-		}
7406-		c->wbuf_ofs = ofs;
7407-		c->wbuf_len = end - start;
7408-	}
7409-
7410-	/* Now sort out the jffs2_raw_node_refs, moving them from the old to the next block */
7411-	new_jeb = &c->blocks[ofs / c->sector_size];
7412-
7413-	spin_lock(&c->erase_completion_lock);
7414-	for (raw = first_raw; raw != jeb->last_node; raw = ref_next(raw)) {
7415-		uint32_t rawlen = ref_totlen(c, jeb, raw);
7416-		struct jffs2_inode_cache *ic;
7417-		struct jffs2_raw_node_ref *new_ref;
7418-		struct jffs2_raw_node_ref **adjust_ref = NULL;
7419-		struct jffs2_inode_info *f = NULL;
7420-
7421-		jffs2_dbg(1, "Refiling block of %08x at %08x(%d) to %08x\n",
7422-			  rawlen, ref_offset(raw), ref_flags(raw), ofs);
7423-
7424-		ic = jffs2_raw_ref_to_ic(raw);
7425-
7426-		/* Ick. This XATTR mess should be fixed shortly... */
7427-		if (ic && ic->class == RAWNODE_CLASS_XATTR_DATUM) {
7428-			struct jffs2_xattr_datum *xd = (void *)ic;
7429-			BUG_ON(xd->node != raw);
7430-			adjust_ref = &xd->node;
7431-			raw->next_in_ino = NULL;
7432-			ic = NULL;
7433-		} else if (ic && ic->class == RAWNODE_CLASS_XATTR_REF) {
7434-			struct jffs2_xattr_datum *xr = (void *)ic;
7435-			BUG_ON(xr->node != raw);
7436-			adjust_ref = &xr->node;
7437-			raw->next_in_ino = NULL;
7438-			ic = NULL;
7439-		} else if (ic && ic->class == RAWNODE_CLASS_INODE_CACHE) {
7440-			struct jffs2_raw_node_ref **p = &ic->nodes;
7441-
7442-			/* Remove the old node from the per-inode list */
7443-			while (*p && *p != (void *)ic) {
7444-				if (*p == raw) {
7445-					(*p) = (raw->next_in_ino);
7446-					raw->next_in_ino = NULL;
7447-					break;
7448-				}
7449-				p = &((*p)->next_in_ino);
7450-			}
7451-
7452-			if (ic->state == INO_STATE_PRESENT && !ref_obsolete(raw)) {
7453-				/* If it's an in-core inode, then we have to adjust any
7454-				   full_dirent or full_dnode structure to point to the
7455-				   new version instead of the old */
7456-				f = jffs2_gc_fetch_inode(c, ic->ino, !ic->pino_nlink);
7457-				if (IS_ERR(f)) {
7458-					/* Should never happen; it _must_ be present */
7459-					JFFS2_ERROR("Failed to iget() ino #%u, err %ld\n",
7460-						    ic->ino, PTR_ERR(f));
7461-					BUG();
7462-				}
7463-				/* We don't lock f->sem. There's a number of ways we could
7464-				   end up in here with it already being locked, and nobody's
7465-				   going to modify it on us anyway because we hold the
7466-				   alloc_sem. We're only changing one ->raw pointer too,
7467-				   which we can get away with without upsetting readers. */
7468-				adjust_ref = jffs2_incore_replace_raw(c, f, raw,
7469-								      (void *)(buf?:c->wbuf) + (ref_offset(raw) - start));
7470-			} else if (unlikely(ic->state != INO_STATE_PRESENT &&
7471-					    ic->state != INO_STATE_CHECKEDABSENT &&
7472-					    ic->state != INO_STATE_GC)) {
7473-				JFFS2_ERROR("Inode #%u is in strange state %d!\n", ic->ino, ic->state);
7474-				BUG();
7475-			}
7476-		}
7477-
7478-		new_ref = jffs2_link_node_ref(c, new_jeb, ofs | ref_flags(raw), rawlen, ic);
7479-
7480-		if (adjust_ref) {
7481-			BUG_ON(*adjust_ref != raw);
7482-			*adjust_ref = new_ref;
7483-		}
7484-		if (f)
7485-			jffs2_gc_release_inode(c, f);
7486-
7487-		if (!ref_obsolete(raw)) {
7488-			jeb->dirty_size += rawlen;
7489-			jeb->used_size  -= rawlen;
7490-			c->dirty_size += rawlen;
7491-			c->used_size -= rawlen;
7492-			raw->flash_offset = ref_offset(raw) | REF_OBSOLETE;
7493-			BUG_ON(raw->next_in_ino);
7494-		}
7495-		ofs += rawlen;
7496-	}
7497-
7498-	kfree(buf);
7499-
7500-	/* Fix up the original jeb now it's on the bad_list */
7501-	if (first_raw == jeb->first_node) {
7502-		jffs2_dbg(1, "Failing block at %08x is now empty. Moving to erase_pending_list\n",
7503-			  jeb->offset);
7504-		list_move(&jeb->list, &c->erase_pending_list);
7505-		c->nr_erasing_blocks++;
7506-		jffs2_garbage_collect_trigger(c);
7507-	}
7508-
7509-	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
7510-	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
7511-
7512-	jffs2_dbg_acct_sanity_check_nolock(c, new_jeb);
7513-	jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb);
7514-
7515-	spin_unlock(&c->erase_completion_lock);
7516-
7517-	jffs2_dbg(1, "wbuf recovery completed OK. wbuf_ofs 0x%08x, len 0x%x\n",
7518-		  c->wbuf_ofs, c->wbuf_len);
7519-
7520-}
7521-
7522-/* Meaning of pad argument:
7523-   0: Do not pad. Probably pointless - we only ever use this when we can't pad anyway.
7524-   1: Pad, do not adjust nextblock free_size
7525-   2: Pad, adjust nextblock free_size
7526-*/
7527-#define NOPAD		0
7528-#define PAD_NOACCOUNT	1
7529-#define PAD_ACCOUNTING	2
7530-
7531-static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
7532-{
7533-	struct jffs2_eraseblock *wbuf_jeb;
7534-	int ret;
7535-	size_t retlen;
7536-
7537-	/* Nothing to do if not write-buffering the flash. In particular, we shouldn't
7538-	   del_timer() the timer we never initialised. */
7539-	if (!jffs2_is_writebuffered(c))
7540-		return 0;
7541-
7542-	if (!mutex_is_locked(&c->alloc_sem)) {
7543-		pr_crit("jffs2_flush_wbuf() called with alloc_sem not locked!\n");
7544-		BUG();
7545-	}
7546-
7547-	if (!c->wbuf_len)	/* already checked c->wbuf above */
7548-		return 0;
7549-
7550-	wbuf_jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
7551-	if (jffs2_prealloc_raw_node_refs(c, wbuf_jeb, c->nextblock->allocated_refs + 1))
7552-		return -ENOMEM;
7553-
7554-	/* claim remaining space on the page
7555-	   this happens, if we have a change to a new block,
7556-	   or if fsync forces us to flush the writebuffer.
7557-	   if we have a switch to next page, we will not have
7558-	   enough remaining space for this.
7559-	*/
7560-	if (pad ) {
7561-		c->wbuf_len = PAD(c->wbuf_len);
7562-
7563-		/* Pad with JFFS2_DIRTY_BITMASK initially.  this helps out ECC'd NOR
7564-		   with 8 byte page size */
7565-		memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
7566-
7567-		if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) {
7568-			struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
7569-			padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
7570-			padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
7571-			padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
7572-			padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
7573-		}
7574-	}
7575-	/* else jffs2_flash_writev has actually filled in the rest of the
7576-	   buffer for us, and will deal with the node refs etc. later. */
7577-
7578-#ifdef BREAKME
7579-	static int breakme;
7580-	if (breakme++ == 20) {
7581-		pr_notice("Faking write error at 0x%08x\n", c->wbuf_ofs);
7582-		breakme = 0;
7583-		mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen,
7584-			  brokenbuf);
7585-		ret = -EIO;
7586-	} else
7587-#endif
7588-
7589-		ret = mtd_write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize,
7590-				&retlen, c->wbuf);
7591-
7592-	if (ret) {
7593-		pr_warn("jffs2_flush_wbuf(): Write failed with %d\n", ret);
7594-		goto wfail;
7595-	} else if (retlen != c->wbuf_pagesize) {
7596-		pr_warn("jffs2_flush_wbuf(): Write was short: %zd instead of %d\n",
7597-			retlen, c->wbuf_pagesize);
7598-		ret = -EIO;
7599-		goto wfail;
7600-	} else if ((ret = jffs2_verify_write(c, c->wbuf, c->wbuf_ofs))) {
7601-	wfail:
7602-		jffs2_wbuf_recover(c);
7603-
7604-		return ret;
7605-	}
7606-
7607-	/* Adjust free size of the block if we padded. */
7608-	if (pad) {
7609-		uint32_t waste = c->wbuf_pagesize - c->wbuf_len;
7610-
7611-		jffs2_dbg(1, "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
7612-			  (wbuf_jeb == c->nextblock) ? "next" : "",
7613-			  wbuf_jeb->offset);
7614-
7615-		/* wbuf_pagesize - wbuf_len is the amount of space that's to be
7616-		   padded. If there is less free space in the block than that,
7617-		   something screwed up */
7618-		if (wbuf_jeb->free_size < waste) {
7619-			pr_crit("jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
7620-				c->wbuf_ofs, c->wbuf_len, waste);
7621-			pr_crit("jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
7622-				wbuf_jeb->offset, wbuf_jeb->free_size);
7623-			BUG();
7624-		}
7625-
7626-		spin_lock(&c->erase_completion_lock);
7627-
7628-		jffs2_link_node_ref(c, wbuf_jeb, (c->wbuf_ofs + c->wbuf_len) | REF_OBSOLETE, waste, NULL);
7629-		/* FIXME: that made it count as dirty. Convert to wasted */
7630-		wbuf_jeb->dirty_size -= waste;
7631-		c->dirty_size -= waste;
7632-		wbuf_jeb->wasted_size += waste;
7633-		c->wasted_size += waste;
7634-	} else
7635-		spin_lock(&c->erase_completion_lock);
7636-
7637-	/* Stick any now-obsoleted blocks on the erase_pending_list */
7638-	jffs2_refile_wbuf_blocks(c);
7639-	jffs2_clear_wbuf_ino_list(c);
7640-	spin_unlock(&c->erase_completion_lock);
7641-
7642-	memset(c->wbuf,0xff,c->wbuf_pagesize);
7643-	/* adjust write buffer offset, else we get a non contiguous write bug */
7644-	c->wbuf_ofs += c->wbuf_pagesize;
7645-	c->wbuf_len = 0;
7646-	return 0;
7647-}
7648-
7649-/* Trigger garbage collection to flush the write-buffer.
7650-   If ino arg is zero, do it if _any_ real (i.e. not GC) writes are
7651-   outstanding. If ino arg non-zero, do it only if a write for the
7652-   given inode is outstanding. */
7653-int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
7654-{
7655-	uint32_t old_wbuf_ofs;
7656-	uint32_t old_wbuf_len;
7657-	int ret = 0;
7658-
7659-	jffs2_dbg(1, "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino);
7660-
7661-	if (!c->wbuf)
7662-		return 0;
7663-
7664-	mutex_lock(&c->alloc_sem);
7665-	if (!jffs2_wbuf_pending_for_ino(c, ino)) {
7666-		jffs2_dbg(1, "Ino #%d not pending in wbuf. Returning\n", ino);
7667-		mutex_unlock(&c->alloc_sem);
7668-		return 0;
7669-	}
7670-
7671-	old_wbuf_ofs = c->wbuf_ofs;
7672-	old_wbuf_len = c->wbuf_len;
7673-
7674-	if (c->unchecked_size) {
7675-		/* GC won't make any progress for a while */
7676-		jffs2_dbg(1, "%s(): padding. Not finished checking\n",
7677-			  __func__);
7678-		down_write(&c->wbuf_sem);
7679-		ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7680-		/* retry flushing wbuf in case jffs2_wbuf_recover
7681-		   left some data in the wbuf */
7682-		if (ret)
7683-			ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7684-		up_write(&c->wbuf_sem);
7685-	} else while (old_wbuf_len &&
7686-		      old_wbuf_ofs == c->wbuf_ofs) {
7687-
7688-		mutex_unlock(&c->alloc_sem);
7689-
7690-		jffs2_dbg(1, "%s(): calls gc pass\n", __func__);
7691-
7692-		ret = jffs2_garbage_collect_pass(c);
7693-		if (ret) {
7694-			/* GC failed. Flush it with padding instead */
7695-			mutex_lock(&c->alloc_sem);
7696-			down_write(&c->wbuf_sem);
7697-			ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7698-			/* retry flushing wbuf in case jffs2_wbuf_recover
7699-			   left some data in the wbuf */
7700-			if (ret)
7701-				ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
7702-			up_write(&c->wbuf_sem);
7703-			break;
7704-		}
7705-		mutex_lock(&c->alloc_sem);
7706-	}
7707-
7708-	jffs2_dbg(1, "%s(): ends...\n", __func__);
7709-
7710-	mutex_unlock(&c->alloc_sem);
7711-	return ret;
7712-}
7713-
7714-/* Pad write-buffer to end and write it, wasting space. */
7715-int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
7716-{
7717-	int ret;
7718-
7719-	if (!c->wbuf)
7720-		return 0;
7721-
7722-	down_write(&c->wbuf_sem);
7723-	ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
7724-	/* retry - maybe wbuf recover left some data in wbuf. */
7725-	if (ret)
7726-		ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
7727-	up_write(&c->wbuf_sem);
7728-
7729-	return ret;
7730-}
7731-
7732-static size_t jffs2_fill_wbuf(struct jffs2_sb_info *c, const uint8_t *buf,
7733-			      size_t len)
7734-{
7735-	if (len && !c->wbuf_len && (len >= c->wbuf_pagesize))
7736-		return 0;
7737-
7738-	if (len > (c->wbuf_pagesize - c->wbuf_len))
7739-		len = c->wbuf_pagesize - c->wbuf_len;
7740-	memcpy(c->wbuf + c->wbuf_len, buf, len);
7741-	c->wbuf_len += (uint32_t) len;
7742-	return len;
7743-}
7744-
7745-int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs,
7746-		       unsigned long count, loff_t to, size_t *retlen,
7747-		       uint32_t ino)
7748-{
7749-	struct jffs2_eraseblock *jeb;
7750-	size_t wbuf_retlen, donelen = 0;
7751-	uint32_t outvec_to = to;
7752-	int ret, invec;
7753-
7754-	/* If not writebuffered flash, don't bother */
7755-	if (!jffs2_is_writebuffered(c))
7756-		return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
7757-
7758-	down_write(&c->wbuf_sem);
7759-
7760-	/* If wbuf_ofs is not initialized, set it to target address */
7761-	if (c->wbuf_ofs == 0xFFFFFFFF) {
7762-		c->wbuf_ofs = PAGE_DIV(to);
7763-		c->wbuf_len = PAGE_MOD(to);
7764-		memset(c->wbuf,0xff,c->wbuf_pagesize);
7765-	}
7766-
7767-	/*
7768-	 * Sanity checks on target address.  It's permitted to write
7769-	 * at PAD(c->wbuf_len+c->wbuf_ofs), and it's permitted to
7770-	 * write at the beginning of a new erase block. Anything else,
7771-	 * and you die.  New block starts at xxx000c (0-b = block
7772-	 * header)
7773-	 */
7774-	if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) {
7775-		/* It's a write to a new block */
7776-		if (c->wbuf_len) {
7777-			jffs2_dbg(1, "%s(): to 0x%lx causes flush of wbuf at 0x%08x\n",
7778-				  __func__, (unsigned long)to, c->wbuf_ofs);
7779-			ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
7780-			if (ret)
7781-				goto outerr;
7782-		}
7783-		/* set pointer to new block */
7784-		c->wbuf_ofs = PAGE_DIV(to);
7785-		c->wbuf_len = PAGE_MOD(to);
7786-	}
7787-
7788-	if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
7789-		/* We're not writing immediately after the writebuffer. Bad. */
7790-		pr_crit("%s(): Non-contiguous write to %08lx\n",
7791-			__func__, (unsigned long)to);
7792-		if (c->wbuf_len)
7793-			pr_crit("wbuf was previously %08x-%08x\n",
7794-				c->wbuf_ofs, c->wbuf_ofs + c->wbuf_len);
7795-		BUG();
7796-	}
7797-
7798-	/* adjust alignment offset */
7799-	if (c->wbuf_len != PAGE_MOD(to)) {
7800-		c->wbuf_len = PAGE_MOD(to);
7801-		/* take care of alignment to next page */
7802-		if (!c->wbuf_len) {
7803-			c->wbuf_len = c->wbuf_pagesize;
7804-			ret = __jffs2_flush_wbuf(c, NOPAD);
7805-			if (ret)
7806-				goto outerr;
7807-		}
7808-	}
7809-
7810-	for (invec = 0; invec < count; invec++) {
7811-		int vlen = invecs[invec].iov_len;
7812-		uint8_t *v = invecs[invec].iov_base;
7813-
7814-		wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
7815-
7816-		if (c->wbuf_len == c->wbuf_pagesize) {
7817-			ret = __jffs2_flush_wbuf(c, NOPAD);
7818-			if (ret)
7819-				goto outerr;
7820-		}
7821-		vlen -= wbuf_retlen;
7822-		outvec_to += wbuf_retlen;
7823-		donelen += wbuf_retlen;
7824-		v += wbuf_retlen;
7825-
7826-		if (vlen >= c->wbuf_pagesize) {
7827-			ret = mtd_write(c->mtd, outvec_to, PAGE_DIV(vlen),
7828-					&wbuf_retlen, v);
7829-			if (ret < 0 || wbuf_retlen != PAGE_DIV(vlen))
7830-				goto outfile;
7831-
7832-			vlen -= wbuf_retlen;
7833-			outvec_to += wbuf_retlen;
7834-			c->wbuf_ofs = outvec_to;
7835-			donelen += wbuf_retlen;
7836-			v += wbuf_retlen;
7837-		}
7838-
7839-		wbuf_retlen = jffs2_fill_wbuf(c, v, vlen);
7840-		if (c->wbuf_len == c->wbuf_pagesize) {
7841-			ret = __jffs2_flush_wbuf(c, NOPAD);
7842-			if (ret)
7843-				goto outerr;
7844-		}
7845-
7846-		outvec_to += wbuf_retlen;
7847-		donelen += wbuf_retlen;
7848-	}
7849-
7850-	/*
7851-	 * If there's a remainder in the wbuf and it's a non-GC write,
7852-	 * remember that the wbuf affects this ino
7853-	 */
7854-	*retlen = donelen;
7855-
7856-	if (jffs2_sum_active()) {
7857-		int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to);
7858-		if (res)
7859-			return res;
7860-	}
7861-
7862-	if (c->wbuf_len && ino)
7863-		jffs2_wbuf_dirties_inode(c, ino);
7864-
7865-	ret = 0;
7866-	up_write(&c->wbuf_sem);
7867-	return ret;
7868-
7869-outfile:
7870-	/*
7871-	 * At this point we have no problem, c->wbuf is empty. However
7872-	 * refile nextblock to avoid writing again to same address.
7873-	 */
7874-
7875-	spin_lock(&c->erase_completion_lock);
7876-
7877-	jeb = &c->blocks[outvec_to / c->sector_size];
7878-	jffs2_block_refile(c, jeb, REFILE_ANYWAY);
7879-
7880-	spin_unlock(&c->erase_completion_lock);
7881-
7882-outerr:
7883-	*retlen = 0;
7884-	up_write(&c->wbuf_sem);
7885-	return ret;
7886-}
7887-
7888-/*
7889- *	This is the entry for flash write.
7890- *	Check, if we work on NAND FLASH, if so build an kvec and write it via vritev
7891-*/
7892-int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
7893-		      size_t *retlen, const u_char *buf)
7894-{
7895-	struct kvec vecs[1];
7896-
7897-	if (!jffs2_is_writebuffered(c))
7898-		return jffs2_flash_direct_write(c, ofs, len, retlen, buf);
7899-
7900-	vecs[0].iov_base = (unsigned char *) buf;
7901-	vecs[0].iov_len = len;
7902-	return jffs2_flash_writev(c, vecs, 1, ofs, retlen, 0);
7903-}
7904-
7905-/*
7906-	Handle readback from writebuffer and ECC failure return
7907-*/
7908-int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *retlen, u_char *buf)
7909-{
7910-	loff_t	orbf = 0, owbf = 0, lwbf = 0;
7911-	int	ret;
7912-
7913-	if (!jffs2_is_writebuffered(c))
7914-		return mtd_read(c->mtd, ofs, len, retlen, buf);
7915-
7916-	/* Read flash */
7917-	down_read(&c->wbuf_sem);
7918-	ret = mtd_read(c->mtd, ofs, len, retlen, buf);
7919-
7920-	if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {
7921-		if (ret == -EBADMSG)
7922-			pr_warn("mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
7923-				len, ofs);
7924-		/*
7925-		 * We have the raw data without ECC correction in the buffer,
7926-		 * maybe we are lucky and all data or parts are correct. We
7927-		 * check the node.  If data are corrupted node check will sort
7928-		 * it out.  We keep this block, it will fail on write or erase
7929-		 * and the we mark it bad. Or should we do that now? But we
7930-		 * should give him a chance.  Maybe we had a system crash or
7931-		 * power loss before the ecc write or a erase was completed.
7932-		 * So we return success. :)
7933-		 */
7934-		ret = 0;
7935-	}
7936-
7937-	/* if no writebuffer available or write buffer empty, return */
7938-	if (!c->wbuf_pagesize || !c->wbuf_len)
7939-		goto exit;
7940-
7941-	/* if we read in a different block, return */
7942-	if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs))
7943-		goto exit;
7944-
7945-	if (ofs >= c->wbuf_ofs) {
7946-		owbf = (ofs - c->wbuf_ofs);	/* offset in write buffer */
7947-		if (owbf > c->wbuf_len)		/* is read beyond write buffer ? */
7948-			goto exit;
7949-		lwbf = c->wbuf_len - owbf;	/* number of bytes to copy */
7950-		if (lwbf > len)
7951-			lwbf = len;
7952-	} else {
7953-		orbf = (c->wbuf_ofs - ofs);	/* offset in read buffer */
7954-		if (orbf > len)			/* is write beyond write buffer ? */
7955-			goto exit;
7956-		lwbf = len - orbf;		/* number of bytes to copy */
7957-		if (lwbf > c->wbuf_len)
7958-			lwbf = c->wbuf_len;
7959-	}
7960-	if (lwbf > 0)
7961-		memcpy(buf+orbf,c->wbuf+owbf,lwbf);
7962-
7963-exit:
7964-	up_read(&c->wbuf_sem);
7965-	return ret;
7966-}
7967-
7968-#define NR_OOB_SCAN_PAGES 4
7969-
7970-/* For historical reasons we use only 8 bytes for OOB clean marker */
7971-#define OOB_CM_SIZE 8
7972-
7973-static const struct jffs2_unknown_node oob_cleanmarker =
7974-{
7975-	.magic = constant_cpu_to_je16(JFFS2_MAGIC_BITMASK),
7976-	.nodetype = constant_cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
7977-	.totlen = constant_cpu_to_je32(8)
7978-};
7979-
7980-/*
7981- * Check, if the out of band area is empty. This function knows about the clean
7982- * marker and if it is present in OOB, treats the OOB as empty anyway.
7983- */
7984-int jffs2_check_oob_empty(struct jffs2_sb_info *c,
7985-			  struct jffs2_eraseblock *jeb, int mode)
7986-{
7987-	int i, ret;
7988-	int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
7989-	struct mtd_oob_ops ops;
7990-
7991-	ops.mode = MTD_OPS_AUTO_OOB;
7992-	ops.ooblen = NR_OOB_SCAN_PAGES * c->oobavail;
7993-	ops.oobbuf = c->oobbuf;
7994-	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
7995-	ops.datbuf = NULL;
7996-
7997-	ret = mtd_read_oob(c->mtd, jeb->offset, &ops);
7998-	if ((ret && !mtd_is_bitflip(ret)) || ops.oobretlen != ops.ooblen) {
7999-		pr_err("cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
8000-		       jeb->offset, ops.ooblen, ops.oobretlen, ret);
8001-		if (!ret || mtd_is_bitflip(ret))
8002-			ret = -EIO;
8003-		return ret;
8004-	}
8005-
8006-	for(i = 0; i < ops.ooblen; i++) {
8007-		if (mode && i < cmlen)
8008-			/* Yeah, we know about the cleanmarker */
8009-			continue;
8010-
8011-		if (ops.oobbuf[i] != 0xFF) {
8012-			jffs2_dbg(2, "Found %02x at %x in OOB for "
8013-				  "%08x\n", ops.oobbuf[i], i, jeb->offset);
8014-			return 1;
8015-		}
8016-	}
8017-
8018-	return 0;
8019-}
8020-
8021-/*
8022- * Check for a valid cleanmarker.
8023- * Returns: 0 if a valid cleanmarker was found
8024- *	    1 if no cleanmarker was found
8025- *	    negative error code if an error occurred
8026- */
8027-int jffs2_check_nand_cleanmarker(struct jffs2_sb_info *c,
8028-				 struct jffs2_eraseblock *jeb)
8029-{
8030-	struct mtd_oob_ops ops;
8031-	int ret, cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
8032-
8033-	ops.mode = MTD_OPS_AUTO_OOB;
8034-	ops.ooblen = cmlen;
8035-	ops.oobbuf = c->oobbuf;
8036-	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
8037-	ops.datbuf = NULL;
8038-
8039-	ret = mtd_read_oob(c->mtd, jeb->offset, &ops);
8040-	if ((ret && !mtd_is_bitflip(ret)) || ops.oobretlen != ops.ooblen) {
8041-		pr_err("cannot read OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
8042-		       jeb->offset, ops.ooblen, ops.oobretlen, ret);
8043-		if (!ret || mtd_is_bitflip(ret))
8044-			ret = -EIO;
8045-		return ret;
8046-	}
8047-
8048-	return !!memcmp(&oob_cleanmarker, c->oobbuf, cmlen);
8049-}
8050-
8051-int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
8052-				 struct jffs2_eraseblock *jeb)
8053-{
8054-	int ret;
8055-	struct mtd_oob_ops ops;
8056-	int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
8057-
8058-	ops.mode = MTD_OPS_AUTO_OOB;
8059-	ops.ooblen = cmlen;
8060-	ops.oobbuf = (uint8_t *)&oob_cleanmarker;
8061-	ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
8062-	ops.datbuf = NULL;
8063-
8064-	ret = mtd_write_oob(c->mtd, jeb->offset, &ops);
8065-	if (ret || ops.oobretlen != ops.ooblen) {
8066-		pr_err("cannot write OOB for EB at %08x, requested %zd bytes, read %zd bytes, error %d\n",
8067-		       jeb->offset, ops.ooblen, ops.oobretlen, ret);
8068-		if (!ret)
8069-			ret = -EIO;
8070-		return ret;
8071-	}
8072-
8073-	return 0;
8074-}
8075-
8076-/*
8077- * On NAND we try to mark this block bad. If the block was erased more
8078- * than MAX_ERASE_FAILURES we mark it finally bad.
8079- * Don't care about failures. This block remains on the erase-pending
8080- * or badblock list as long as nobody manipulates the flash with
8081- * a bootloader or something like that.
8082- */
8083-
8084-int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
8085-{
8086-	int 	ret;
8087-
8088-	/* if the count is < max, we try to write the counter to the 2nd page oob area */
8089-	if( ++jeb->bad_count < MAX_ERASE_FAILURES)
8090-		return 0;
8091-
8092-	pr_warn("marking eraseblock at %08x as bad\n", bad_offset);
8093-	ret = mtd_block_markbad(c->mtd, bad_offset);
8094-
8095-	if (ret) {
8096-		jffs2_dbg(1, "%s(): Write failed for block at %08x: error %d\n",
8097-			  __func__, jeb->offset, ret);
8098-		return ret;
8099-	}
8100-	return 1;
8101-}
8102-
8103-static struct jffs2_sb_info *work_to_sb(struct work_struct *work)
8104-{
8105-	struct delayed_work *dwork;
8106-
8107-	dwork = to_delayed_work(work);
8108-	return container_of(dwork, struct jffs2_sb_info, wbuf_dwork);
8109-}
8110-
8111-static void delayed_wbuf_sync(struct work_struct *work)
8112-{
8113-	struct jffs2_sb_info *c = work_to_sb(work);
8114-	struct super_block *sb = OFNI_BS_2SFFJ(c);
8115-
8116-	if (!sb_rdonly(sb)) {
8117-		jffs2_dbg(1, "%s()\n", __func__);
8118-		jffs2_flush_wbuf_gc(c, 0);
8119-	}
8120-}
8121-
8122-void jffs2_dirty_trigger(struct jffs2_sb_info *c)
8123-{
8124-	struct super_block *sb = OFNI_BS_2SFFJ(c);
8125-	unsigned long delay;
8126-
8127-	if (sb_rdonly(sb))
8128-		return;
8129-
8130-	delay = msecs_to_jiffies(dirty_writeback_interval * 10);
8131-	if (queue_delayed_work(system_long_wq, &c->wbuf_dwork, delay))
8132-		jffs2_dbg(1, "%s()\n", __func__);
8133-}
8134-
8135-int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
8136-{
8137-	if (!c->mtd->oobsize)
8138-		return 0;
8139-
8140-	/* Cleanmarker is out-of-band, so inline size zero */
8141-	c->cleanmarker_size = 0;
8142-
8143-	if (c->mtd->oobavail == 0) {
8144-		pr_err("inconsistent device description\n");
8145-		return -EINVAL;
8146-	}
8147-
8148-	jffs2_dbg(1, "using OOB on NAND\n");
8149-
8150-	c->oobavail = c->mtd->oobavail;
8151-
8152-	/* Initialise write buffer */
8153-	init_rwsem(&c->wbuf_sem);
8154-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
8155-	c->wbuf_pagesize = c->mtd->writesize;
8156-	c->wbuf_ofs = 0xFFFFFFFF;
8157-
8158-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8159-	if (!c->wbuf)
8160-		return -ENOMEM;
8161-
8162-	c->oobbuf = kmalloc_array(NR_OOB_SCAN_PAGES, c->oobavail, GFP_KERNEL);
8163-	if (!c->oobbuf) {
8164-		kfree(c->wbuf);
8165-		return -ENOMEM;
8166-	}
8167-
8168-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8169-	c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8170-	if (!c->wbuf_verify) {
8171-		kfree(c->oobbuf);
8172-		kfree(c->wbuf);
8173-		return -ENOMEM;
8174-	}
8175-#endif
8176-	return 0;
8177-}
8178-
8179-void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
8180-{
8181-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8182-	kfree(c->wbuf_verify);
8183-#endif
8184-	kfree(c->wbuf);
8185-	kfree(c->oobbuf);
8186-}
8187-
8188-int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
8189-	c->cleanmarker_size = 0;		/* No cleanmarkers needed */
8190-
8191-	/* Initialize write buffer */
8192-	init_rwsem(&c->wbuf_sem);
8193-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
8194-	c->wbuf_pagesize =  c->mtd->erasesize;
8195-
8196-	/* Find a suitable c->sector_size
8197-	 * - Not too much sectors
8198-	 * - Sectors have to be at least 4 K + some bytes
8199-	 * - All known dataflashes have erase sizes of 528 or 1056
8200-	 * - we take at least 8 eraseblocks and want to have at least 8K size
8201-	 * - The concatenation should be a power of 2
8202-	*/
8203-
8204-	c->sector_size = 8 * c->mtd->erasesize;
8205-
8206-	while (c->sector_size < 8192) {
8207-		c->sector_size *= 2;
8208-	}
8209-
8210-	/* It may be necessary to adjust the flash size */
8211-	c->flash_size = c->mtd->size;
8212-
8213-	if ((c->flash_size % c->sector_size) != 0) {
8214-		c->flash_size = (c->flash_size / c->sector_size) * c->sector_size;
8215-		pr_warn("flash size adjusted to %dKiB\n", c->flash_size);
8216-	}
8217-
8218-	c->wbuf_ofs = 0xFFFFFFFF;
8219-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8220-	if (!c->wbuf)
8221-		return -ENOMEM;
8222-
8223-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8224-	c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8225-	if (!c->wbuf_verify) {
8226-		kfree(c->wbuf);
8227-		return -ENOMEM;
8228-	}
8229-#endif
8230-
8231-	pr_info("write-buffering enabled buffer (%d) erasesize (%d)\n",
8232-		c->wbuf_pagesize, c->sector_size);
8233-
8234-	return 0;
8235-}
8236-
8237-void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) {
8238-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8239-	kfree(c->wbuf_verify);
8240-#endif
8241-	kfree(c->wbuf);
8242-}
8243-
8244-int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) {
8245-	/* Cleanmarker currently occupies whole programming regions,
8246-	 * either one or 2 for 8Byte STMicro flashes. */
8247-	c->cleanmarker_size = max(16u, c->mtd->writesize);
8248-
8249-	/* Initialize write buffer */
8250-	init_rwsem(&c->wbuf_sem);
8251-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
8252-
8253-	c->wbuf_pagesize = c->mtd->writesize;
8254-	c->wbuf_ofs = 0xFFFFFFFF;
8255-
8256-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8257-	if (!c->wbuf)
8258-		return -ENOMEM;
8259-
8260-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8261-	c->wbuf_verify = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8262-	if (!c->wbuf_verify) {
8263-		kfree(c->wbuf);
8264-		return -ENOMEM;
8265-	}
8266-#endif
8267-	return 0;
8268-}
8269-
8270-void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) {
8271-#ifdef CONFIG_JFFS2_FS_WBUF_VERIFY
8272-	kfree(c->wbuf_verify);
8273-#endif
8274-	kfree(c->wbuf);
8275-}
8276-
8277-int jffs2_ubivol_setup(struct jffs2_sb_info *c) {
8278-	c->cleanmarker_size = 0;
8279-
8280-	if (c->mtd->writesize == 1)
8281-		/* We do not need write-buffer */
8282-		return 0;
8283-
8284-	init_rwsem(&c->wbuf_sem);
8285-	INIT_DELAYED_WORK(&c->wbuf_dwork, delayed_wbuf_sync);
8286-
8287-	c->wbuf_pagesize =  c->mtd->writesize;
8288-	c->wbuf_ofs = 0xFFFFFFFF;
8289-	c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
8290-	if (!c->wbuf)
8291-		return -ENOMEM;
8292-
8293-	pr_info("write-buffering enabled buffer (%d) erasesize (%d)\n",
8294-		c->wbuf_pagesize, c->sector_size);
8295-
8296-	return 0;
8297-}
8298-
8299-void jffs2_ubivol_cleanup(struct jffs2_sb_info *c) {
8300-	kfree(c->wbuf);
8301-}
8302diff -Nupr old/fs/jffs2/write.c new/fs/jffs2/write.c
8303--- old/fs/jffs2/write.c	2022-05-09 17:22:53.000000000 +0800
8304+++ new/fs/jffs2/write.c	2022-05-09 20:07:33.520000000 +0800
8305@@ -9,16 +9,15 @@
8306  *
8307  */
8308
8309-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8310-
8311+#include <dirent.h>
8312 #include <linux/kernel.h>
8313 #include <linux/fs.h>
8314-#include <linux/crc32.h>
8315 #include <linux/pagemap.h>
8316-#include <linux/mtd/mtd.h>
8317+#include <linux/semaphore.h>
8318+#include "mtd_dev.h"
8319 #include "nodelist.h"
8320 #include "compr.h"
8321-
8322+#include "los_crc32.h"
8323
8324 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
8325 		       uint32_t mode, struct jffs2_raw_inode *ri)
8326@@ -30,8 +29,6 @@ int jffs2_do_new_inode(struct jffs2_sb_i
8327 		return -ENOMEM;
8328 	}
8329
8330-	memset(ic, 0, sizeof(*ic));
8331-
8332 	f->inocache = ic;
8333 	f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
8334 	f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
8335@@ -69,8 +66,10 @@ struct jffs2_full_dnode *jffs2_write_dno
8336 	int retried = 0;
8337 	unsigned long cnt = 2;
8338
8339-	D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
8340-		pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n");
8341+	D1(if (je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
8342+		printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode(), je32_to_cpu(ri->hdr_crc):%d, "
8343+			"crc32(0, ri, sizeof(struct jffs2_unknown_node) - 4):%d\n", je32_to_cpu(ri->hdr_crc),
8344+			crc32(0, ri, sizeof(struct jffs2_unknown_node) - 4));
8345 		BUG();
8346 	}
8347 	   );
8348@@ -172,8 +171,8 @@ struct jffs2_full_dnode *jffs2_write_dno
8349 	   beginning of a page and runs to the end of the file, or if
8350 	   it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
8351 	*/
8352-	if ((je32_to_cpu(ri->dsize) >= PAGE_SIZE) ||
8353-	    ( ((je32_to_cpu(ri->offset)&(PAGE_SIZE-1))==0) &&
8354+	if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
8355+	    ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
8356 	      (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) ==  je32_to_cpu(ri->isize)))) {
8357 		flash_ofs |= REF_PRISTINE;
8358 	} else {
8359@@ -219,11 +218,13 @@ struct jffs2_full_dirent *jffs2_write_di
8360 		  je32_to_cpu(rd->name_crc));
8361
8362 	D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
8363-		pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n");
8364+	    printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent(), je32_to_cpu(rd->hdr_crc):%d, "
8365+		    "crc32(0, rd, sizeof(struct jffs2_unknown_node) - 4):%d\n", je32_to_cpu(rd->hdr_crc),
8366+			crc32(0, rd, sizeof(struct jffs2_unknown_node) - 4));
8367 		BUG();
8368 	   });
8369
8370-	if (strnlen(name, namelen) != namelen) {
8371+	if (strnlen((const char *)name, namelen) != namelen) {
8372 		/* This should never happen, but seems to have done on at least one
8373 		   occasion: https://dev.laptop.org/ticket/4184 */
8374 		pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n");
8375@@ -245,7 +246,7 @@ struct jffs2_full_dirent *jffs2_write_di
8376
8377 	fd->version = je32_to_cpu(rd->version);
8378 	fd->ino = je32_to_cpu(rd->ino);
8379-	fd->nhash = full_name_hash(NULL, name, namelen);
8380+	fd->nhash = full_name_hash(name, namelen);
8381 	fd->type = rd->type;
8382 	memcpy(fd->name, name, namelen);
8383 	fd->name[namelen]=0;
8384@@ -343,10 +344,24 @@ int jffs2_write_inode_range(struct jffs2
8385 {
8386 	int ret = 0;
8387 	uint32_t writtenlen = 0;
8388+	unsigned char *bufRet = NULL;
8389+	unsigned char *bufRetBak = NULL;
8390
8391 	jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
8392 		  __func__, f->inocache->ino, offset, writelen);
8393
8394+	if (writelen > 0) {
8395+		bufRet = kmalloc(writelen, GFP_KERNEL);
8396+		if (bufRet == NULL) {
8397+			return -ENOMEM;
8398+		}
8399+		bufRetBak = bufRet;
8400+		if (LOS_CopyToKernel(bufRet, writelen, buf, writelen) != 0) {
8401+			kfree(bufRet);
8402+			return -EFAULT;
8403+		}
8404+	}
8405+
8406 	while(writelen) {
8407 		struct jffs2_full_dnode *fn;
8408 		unsigned char *comprbuf = NULL;
8409@@ -366,11 +381,10 @@ int jffs2_write_inode_range(struct jffs2
8410 			break;
8411 		}
8412 		mutex_lock(&f->sem);
8413-		datalen = min_t(uint32_t, writelen,
8414-				PAGE_SIZE - (offset & (PAGE_SIZE-1)));
8415+		datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
8416 		cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
8417
8418-		comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
8419+		comprtype = jffs2_compress(c, f, bufRet, &comprbuf, &datalen, &cdatalen);
8420
8421 		ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8422 		ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
8423@@ -390,7 +404,7 @@ int jffs2_write_inode_range(struct jffs2
8424
8425 		fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
8426
8427-		jffs2_free_comprbuf(comprbuf, buf);
8428+		jffs2_free_comprbuf(comprbuf, bufRet);
8429
8430 		if (IS_ERR(fn)) {
8431 			ret = PTR_ERR(fn);
8432@@ -432,15 +446,18 @@ int jffs2_write_inode_range(struct jffs2
8433 		writtenlen += datalen;
8434 		offset += datalen;
8435 		writelen -= datalen;
8436-		buf += datalen;
8437+		bufRet += datalen;
8438 	}
8439 	*retlen = writtenlen;
8440+	if (bufRetBak != NULL) {
8441+		kfree(bufRetBak);
8442+	}
8443 	return ret;
8444 }
8445
8446 int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
8447 		    struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
8448-		    const struct qstr *qstr)
8449+		    const char *name, int namelen)
8450 {
8451 	struct jffs2_raw_dirent *rd;
8452 	struct jffs2_full_dnode *fn;
8453@@ -468,7 +485,8 @@ int jffs2_do_create(struct jffs2_sb_info
8454 		  jemode_to_cpu(ri->mode));
8455
8456 	if (IS_ERR(fn)) {
8457-		jffs2_dbg(1, "jffs2_write_dnode() failed\n");
8458+		jffs2_dbg(1, "jffs2_write_dnode() failed,error:%ld\n",
8459+		    PTR_ERR(fn));
8460 		/* Eeek. Wave bye bye */
8461 		mutex_unlock(&f->sem);
8462 		jffs2_complete_reservation(c);
8463@@ -482,19 +500,12 @@ int jffs2_do_create(struct jffs2_sb_info
8464 	mutex_unlock(&f->sem);
8465 	jffs2_complete_reservation(c);
8466
8467-	ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
8468-	if (ret)
8469-		return ret;
8470-	ret = jffs2_init_acl_post(&f->vfs_inode);
8471-	if (ret)
8472-		return ret;
8473-
8474-	ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
8475-				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
8476+	ret = jffs2_reserve_space(c, sizeof(*rd)+ namelen, &alloclen,
8477+				ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
8478
8479 	if (ret) {
8480 		/* Eep. */
8481-		jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
8482+		jffs2_dbg(1, "jffs2_reserve_space() for dirent failed,ret:%d\n",ret);
8483 		return ret;
8484 	}
8485
8486@@ -509,19 +520,19 @@ int jffs2_do_create(struct jffs2_sb_info
8487
8488 	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8489 	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
8490-	rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
8491+	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
8492 	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
8493
8494 	rd->pino = cpu_to_je32(dir_f->inocache->ino);
8495 	rd->version = cpu_to_je32(++dir_f->highest_version);
8496 	rd->ino = ri->ino;
8497 	rd->mctime = ri->ctime;
8498-	rd->nsize = qstr->len;
8499+	rd->nsize = namelen;
8500 	rd->type = DT_REG;
8501 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
8502-	rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
8503+	rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
8504
8505-	fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
8506+	fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)name, namelen, ALLOC_NORMAL);
8507
8508 	jffs2_free_raw_dirent(rd);
8509
8510@@ -553,7 +564,7 @@ int jffs2_do_unlink(struct jffs2_sb_info
8511 	uint32_t alloclen;
8512 	int ret;
8513
8514-	if (!jffs2_can_mark_obsolete(c)) {
8515+	if (jffs2_can_mark_obsolete(c)) {
8516 		/* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
8517
8518 		rd = jffs2_alloc_raw_dirent();
8519@@ -584,7 +595,7 @@ int jffs2_do_unlink(struct jffs2_sb_info
8520 		rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
8521 		rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
8522
8523-		fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
8524+		fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)name, namelen, ALLOC_DELETION);
8525
8526 		jffs2_free_raw_dirent(rd);
8527
8528@@ -598,7 +609,7 @@ int jffs2_do_unlink(struct jffs2_sb_info
8529 		jffs2_add_fd_to_list(c, fd, &dir_f->dents);
8530 		mutex_unlock(&dir_f->sem);
8531 	} else {
8532-		uint32_t nhash = full_name_hash(NULL, name, namelen);
8533+		uint32_t nhash = full_name_hash((const unsigned char *)name, namelen);
8534
8535 		fd = dir_f->dents;
8536 		/* We don't actually want to reserve any space, but we do
8537@@ -703,7 +714,7 @@ int jffs2_do_link (struct jffs2_sb_info
8538 	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
8539 	rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
8540
8541-	fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
8542+	fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *)name, namelen, ALLOC_NORMAL);
8543
8544 	jffs2_free_raw_dirent(rd);
8545
8546diff -Nupr old/fs/jffs2/writev.c new/fs/jffs2/writev.c
8547--- old/fs/jffs2/writev.c	2022-05-09 17:22:53.000000000 +0800
8548+++ new/fs/jffs2/writev.c	2022-05-09 20:05:36.440000000 +0800
8549@@ -10,42 +10,97 @@
8550  */
8551
8552 #include <linux/kernel.h>
8553-#include <linux/mtd/mtd.h>
8554+#include "mtd_dev.h"
8555 #include "nodelist.h"
8556
8557 int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,
8558 			      unsigned long count, loff_t to, size_t *retlen)
8559 {
8560-	if (!jffs2_is_writebuffered(c)) {
8561-		if (jffs2_sum_active()) {
8562-			int res;
8563-			res = jffs2_sum_add_kvec(c, vecs, count, (uint32_t) to);
8564-			if (res) {
8565-				return res;
8566+	unsigned long i;
8567+	size_t totlen = 0, thislen;
8568+	int ret = 0;
8569+
8570+	for (i = 0; i < count; i++) {
8571+		// writes need to be aligned but the data we're passed may not be
8572+		// Observation suggests most unaligned writes are small, so we
8573+		// optimize for that case.
8574+
8575+		if (((vecs[i].iov_len & (sizeof(int) - 1))) ||
8576+		(((unsigned long) vecs[i].iov_base & (sizeof(unsigned long) - 1)))) {
8577+			// are there iov's after this one? Or is it so much we'd need
8578+			// to do multiple writes anyway?
8579+			if ((i + 1) < count || vecs[i].iov_len > 256) {
8580+				// cop out and malloc
8581+				unsigned long j;
8582+				size_t sizetomalloc = 0, totvecsize = 0;
8583+				char *cbuf, *cbufptr;
8584+
8585+				for (j = i; j < count; j++)
8586+					totvecsize += vecs[j].iov_len;
8587+
8588+				// pad up in case unaligned
8589+				sizetomalloc = totvecsize + sizeof(int) - 1;
8590+				sizetomalloc &= ~(sizeof(int) - 1);
8591+				cbuf = (char *) malloc(sizetomalloc);
8592+				// malloc returns aligned memory
8593+				if (!cbuf) {
8594+					ret = -ENOMEM;
8595+					goto writev_out;
8596+				}
8597+				cbufptr = cbuf;
8598+				for (j = i; j < count; j++) {
8599+					(void)memcpy_s(cbufptr, vecs[j].iov_len, vecs[j].iov_base, vecs[j].iov_len);
8600+					cbufptr += vecs[j].iov_len;
8601+				}
8602+				ret = jffs2_flash_write(c, to, sizetomalloc, &thislen,
8603+					(unsigned char *) cbuf);
8604+				if (thislen > totvecsize) // in case it was aligned up
8605+					thislen = totvecsize;
8606+				totlen += thislen;
8607+				free(cbuf);
8608+				goto writev_out;
8609+			} else {
8610+				// otherwise optimize for the common case
8611+				int buf[256/sizeof(int)]; // int, so int aligned
8612+				size_t lentowrite;
8613+
8614+				lentowrite = vecs[i].iov_len;
8615+				// pad up in case its unaligned
8616+				lentowrite += sizeof(int) - 1;
8617+				lentowrite &= ~(sizeof(int) - 1);
8618+				ret = memcpy_s(buf, sizeof(buf), vecs[i].iov_base, vecs[i].iov_len);
8619+				if (ret != EOK)
8620+					goto writev_out;
8621+
8622+				ret = jffs2_flash_write(c, to, lentowrite, &thislen,
8623+					(unsigned char *) &buf[0]);
8624+				if (thislen > vecs[i].iov_len)
8625+					thislen = vecs[i].iov_len;
8626 			}
8627+		} else {
8628+				ret = jffs2_flash_write(c, to, vecs[i].iov_len, &thislen,
8629+					vecs[i].iov_base);
8630 		}
8631+		totlen += thislen;
8632+		if (ret || thislen != vecs[i].iov_len) break;
8633+		to += vecs[i].iov_len;
8634 	}
8635
8636-	return mtd_writev(c->mtd, vecs, count, to, retlen);
8637+writev_out:
8638+	if (retlen) *retlen = totlen;
8639+
8640+	return ret;
8641 }
8642
8643 int jffs2_flash_direct_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
8644 			size_t *retlen, const u_char *buf)
8645 {
8646 	int ret;
8647-	ret = mtd_write(c->mtd, ofs, len, retlen, buf);
8648-
8649-	if (jffs2_sum_active()) {
8650-		struct kvec vecs[1];
8651-		int res;
8652-
8653-		vecs[0].iov_base = (unsigned char *) buf;
8654-		vecs[0].iov_len = len;
8655-
8656-		res = jffs2_sum_add_kvec(c, vecs, 1, (uint32_t) ofs);
8657-		if (res) {
8658-			return res;
8659-		}
8660+	ret = c->mtd->write(c->mtd, ofs, len, (char *)buf);
8661+	if (ret >= 0) {
8662+		*retlen = ret;
8663+		return 0;
8664 	}
8665+	*retlen = 0;
8666 	return ret;
8667 }
8668diff -Nupr old/fs/jffs2/xattr.c new/fs/jffs2/xattr.c
8669--- old/fs/jffs2/xattr.c	2022-05-09 17:15:24.360000000 +0800
8670+++ new/fs/jffs2/xattr.c	1970-01-01 08:00:00.000000000 +0800
8671@@ -1,1347 +0,0 @@
8672-/*
8673- * JFFS2 -- Journalling Flash File System, Version 2.
8674- *
8675- * Copyright © 2006  NEC Corporation
8676- *
8677- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
8678- *
8679- * For licensing information, see the file 'LICENCE' in this directory.
8680- *
8681- */
8682-
8683-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8684-
8685-#define JFFS2_XATTR_IS_CORRUPTED	1
8686-
8687-#include <linux/kernel.h>
8688-#include <linux/slab.h>
8689-#include <linux/fs.h>
8690-#include <linux/time.h>
8691-#include <linux/pagemap.h>
8692-#include <linux/highmem.h>
8693-#include <linux/crc32.h>
8694-#include <linux/jffs2.h>
8695-#include <linux/xattr.h>
8696-#include <linux/posix_acl_xattr.h>
8697-#include <linux/mtd/mtd.h>
8698-#include "nodelist.h"
8699-/* -------- xdatum related functions ----------------
8700- * xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
8701- *   is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
8702- *   the index of the xattr name/value pair cache (c->xattrindex).
8703- * is_xattr_datum_unchecked(c, xd)
8704- *   returns 1, if xdatum contains any unchecked raw nodes. if all raw nodes are not
8705- *   unchecked, it returns 0.
8706- * unload_xattr_datum(c, xd)
8707- *   is used to release xattr name/value pair and detach from c->xattrindex.
8708- * reclaim_xattr_datum(c)
8709- *   is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
8710- *   memory usage by cache is over c->xdatum_mem_threshold. Currently, this threshold
8711- *   is hard coded as 32KiB.
8712- * do_verify_xattr_datum(c, xd)
8713- *   is used to load the xdatum informations without name/value pair from the medium.
8714- *   It's necessary once, because those informations are not collected during mounting
8715- *   process when EBS is enabled.
8716- *   0 will be returned, if success. An negative return value means recoverable error, and
8717- *   positive return value means unrecoverable error. Thus, caller must remove this xdatum
8718- *   and xref when it returned positive value.
8719- * do_load_xattr_datum(c, xd)
8720- *   is used to load name/value pair from the medium.
8721- *   The meanings of return value is same as do_verify_xattr_datum().
8722- * load_xattr_datum(c, xd)
8723- *   is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
8724- *   If xd need to call do_verify_xattr_datum() at first, it's called before calling
8725- *   do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
8726- * save_xattr_datum(c, xd)
8727- *   is used to write xdatum to medium. xd->version will be incremented.
8728- * create_xattr_datum(c, xprefix, xname, xvalue, xsize)
8729- *   is used to create new xdatum and write to medium.
8730- * unrefer_xattr_datum(c, xd)
8731- *   is used to delete a xdatum. When nobody refers this xdatum, JFFS2_XFLAGS_DEAD
8732- *   is set on xd->flags and chained xattr_dead_list or release it immediately.
8733- *   In the first case, the garbage collector release it later.
8734- * -------------------------------------------------- */
8735-static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
8736-{
8737-	int name_len = strlen(xname);
8738-
8739-	return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
8740-}
8741-
8742-static int is_xattr_datum_unchecked(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8743-{
8744-	struct jffs2_raw_node_ref *raw;
8745-	int rc = 0;
8746-
8747-	spin_lock(&c->erase_completion_lock);
8748-	for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
8749-		if (ref_flags(raw) == REF_UNCHECKED) {
8750-			rc = 1;
8751-			break;
8752-		}
8753-	}
8754-	spin_unlock(&c->erase_completion_lock);
8755-	return rc;
8756-}
8757-
8758-static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8759-{
8760-	/* must be called under down_write(xattr_sem) */
8761-	D1(dbg_xattr("%s: xid=%u, version=%u\n", __func__, xd->xid, xd->version));
8762-	if (xd->xname) {
8763-		c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
8764-		kfree(xd->xname);
8765-	}
8766-
8767-	list_del_init(&xd->xindex);
8768-	xd->hashkey = 0;
8769-	xd->xname = NULL;
8770-	xd->xvalue = NULL;
8771-}
8772-
8773-static void reclaim_xattr_datum(struct jffs2_sb_info *c)
8774-{
8775-	/* must be called under down_write(xattr_sem) */
8776-	struct jffs2_xattr_datum *xd, *_xd;
8777-	uint32_t target, before;
8778-	static int index = 0;
8779-	int count;
8780-
8781-	if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
8782-		return;
8783-
8784-	before = c->xdatum_mem_usage;
8785-	target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
8786-	for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
8787-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex) {
8788-			if (xd->flags & JFFS2_XFLAGS_HOT) {
8789-				xd->flags &= ~JFFS2_XFLAGS_HOT;
8790-			} else if (!(xd->flags & JFFS2_XFLAGS_BIND)) {
8791-				unload_xattr_datum(c, xd);
8792-			}
8793-			if (c->xdatum_mem_usage <= target)
8794-				goto out;
8795-		}
8796-		index = (index+1) % XATTRINDEX_HASHSIZE;
8797-	}
8798- out:
8799-	JFFS2_NOTICE("xdatum_mem_usage from %u byte to %u byte (%u byte reclaimed)\n",
8800-		     before, c->xdatum_mem_usage, before - c->xdatum_mem_usage);
8801-}
8802-
8803-static int do_verify_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8804-{
8805-	/* must be called under down_write(xattr_sem) */
8806-	struct jffs2_eraseblock *jeb;
8807-	struct jffs2_raw_node_ref *raw;
8808-	struct jffs2_raw_xattr rx;
8809-	size_t readlen;
8810-	uint32_t crc, offset, totlen;
8811-	int rc;
8812-
8813-	spin_lock(&c->erase_completion_lock);
8814-	offset = ref_offset(xd->node);
8815-	if (ref_flags(xd->node) == REF_PRISTINE)
8816-		goto complete;
8817-	spin_unlock(&c->erase_completion_lock);
8818-
8819-	rc = jffs2_flash_read(c, offset, sizeof(rx), &readlen, (char *)&rx);
8820-	if (rc || readlen != sizeof(rx)) {
8821-		JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu at %#08x\n",
8822-			      rc, sizeof(rx), readlen, offset);
8823-		return rc ? rc : -EIO;
8824-	}
8825-	crc = crc32(0, &rx, sizeof(rx) - 4);
8826-	if (crc != je32_to_cpu(rx.node_crc)) {
8827-		JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
8828-			    offset, je32_to_cpu(rx.hdr_crc), crc);
8829-		xd->flags |= JFFS2_XFLAGS_INVALID;
8830-		return JFFS2_XATTR_IS_CORRUPTED;
8831-	}
8832-	totlen = PAD(sizeof(rx) + rx.name_len + 1 + je16_to_cpu(rx.value_len));
8833-	if (je16_to_cpu(rx.magic) != JFFS2_MAGIC_BITMASK
8834-	    || je16_to_cpu(rx.nodetype) != JFFS2_NODETYPE_XATTR
8835-	    || je32_to_cpu(rx.totlen) != totlen
8836-	    || je32_to_cpu(rx.xid) != xd->xid
8837-	    || je32_to_cpu(rx.version) != xd->version) {
8838-		JFFS2_ERROR("inconsistent xdatum at %#08x, magic=%#04x/%#04x, "
8839-			    "nodetype=%#04x/%#04x, totlen=%u/%u, xid=%u/%u, version=%u/%u\n",
8840-			    offset, je16_to_cpu(rx.magic), JFFS2_MAGIC_BITMASK,
8841-			    je16_to_cpu(rx.nodetype), JFFS2_NODETYPE_XATTR,
8842-			    je32_to_cpu(rx.totlen), totlen,
8843-			    je32_to_cpu(rx.xid), xd->xid,
8844-			    je32_to_cpu(rx.version), xd->version);
8845-		xd->flags |= JFFS2_XFLAGS_INVALID;
8846-		return JFFS2_XATTR_IS_CORRUPTED;
8847-	}
8848-	xd->xprefix = rx.xprefix;
8849-	xd->name_len = rx.name_len;
8850-	xd->value_len = je16_to_cpu(rx.value_len);
8851-	xd->data_crc = je32_to_cpu(rx.data_crc);
8852-
8853-	spin_lock(&c->erase_completion_lock);
8854- complete:
8855-	for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
8856-		jeb = &c->blocks[ref_offset(raw) / c->sector_size];
8857-		totlen = PAD(ref_totlen(c, jeb, raw));
8858-		if (ref_flags(raw) == REF_UNCHECKED) {
8859-			c->unchecked_size -= totlen; c->used_size += totlen;
8860-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
8861-		}
8862-		raw->flash_offset = ref_offset(raw) | ((xd->node==raw) ? REF_PRISTINE : REF_NORMAL);
8863-	}
8864-	spin_unlock(&c->erase_completion_lock);
8865-
8866-	/* unchecked xdatum is chained with c->xattr_unchecked */
8867-	list_del_init(&xd->xindex);
8868-
8869-	dbg_xattr("success on verifying xdatum (xid=%u, version=%u)\n",
8870-		  xd->xid, xd->version);
8871-
8872-	return 0;
8873-}
8874-
8875-static int do_load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8876-{
8877-	/* must be called under down_write(xattr_sem) */
8878-	char *data;
8879-	size_t readlen;
8880-	uint32_t crc, length;
8881-	int i, ret, retry = 0;
8882-
8883-	BUG_ON(ref_flags(xd->node) != REF_PRISTINE);
8884-	BUG_ON(!list_empty(&xd->xindex));
8885- retry:
8886-	length = xd->name_len + 1 + xd->value_len;
8887-	data = kmalloc(length, GFP_KERNEL);
8888-	if (!data)
8889-		return -ENOMEM;
8890-
8891-	ret = jffs2_flash_read(c, ref_offset(xd->node)+sizeof(struct jffs2_raw_xattr),
8892-			       length, &readlen, data);
8893-
8894-	if (ret || length!=readlen) {
8895-		JFFS2_WARNING("jffs2_flash_read() returned %d, request=%d, readlen=%zu, at %#08x\n",
8896-			      ret, length, readlen, ref_offset(xd->node));
8897-		kfree(data);
8898-		return ret ? ret : -EIO;
8899-	}
8900-
8901-	data[xd->name_len] = '\0';
8902-	crc = crc32(0, data, length);
8903-	if (crc != xd->data_crc) {
8904-		JFFS2_WARNING("node CRC failed (JFFS2_NODETYPE_XATTR)"
8905-			      " at %#08x, read: 0x%08x calculated: 0x%08x\n",
8906-			      ref_offset(xd->node), xd->data_crc, crc);
8907-		kfree(data);
8908-		xd->flags |= JFFS2_XFLAGS_INVALID;
8909-		return JFFS2_XATTR_IS_CORRUPTED;
8910-	}
8911-
8912-	xd->flags |= JFFS2_XFLAGS_HOT;
8913-	xd->xname = data;
8914-	xd->xvalue = data + xd->name_len+1;
8915-
8916-	c->xdatum_mem_usage += length;
8917-
8918-	xd->hashkey = xattr_datum_hashkey(xd->xprefix, xd->xname, xd->xvalue, xd->value_len);
8919-	i = xd->hashkey % XATTRINDEX_HASHSIZE;
8920-	list_add(&xd->xindex, &c->xattrindex[i]);
8921-	if (!retry) {
8922-		retry = 1;
8923-		reclaim_xattr_datum(c);
8924-		if (!xd->xname)
8925-			goto retry;
8926-	}
8927-
8928-	dbg_xattr("success on loading xdatum (xid=%u, xprefix=%u, xname='%s')\n",
8929-		  xd->xid, xd->xprefix, xd->xname);
8930-
8931-	return 0;
8932-}
8933-
8934-static int load_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8935-{
8936-	/* must be called under down_write(xattr_sem);
8937-	 * rc < 0 : recoverable error, try again
8938-	 * rc = 0 : success
8939-	 * rc > 0 : Unrecoverable error, this node should be deleted.
8940-	 */
8941-	int rc = 0;
8942-
8943-	BUG_ON(xd->flags & JFFS2_XFLAGS_DEAD);
8944-	if (xd->xname)
8945-		return 0;
8946-	if (xd->flags & JFFS2_XFLAGS_INVALID)
8947-		return JFFS2_XATTR_IS_CORRUPTED;
8948-	if (unlikely(is_xattr_datum_unchecked(c, xd)))
8949-		rc = do_verify_xattr_datum(c, xd);
8950-	if (!rc)
8951-		rc = do_load_xattr_datum(c, xd);
8952-	return rc;
8953-}
8954-
8955-static int save_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
8956-{
8957-	/* must be called under down_write(xattr_sem) */
8958-	struct jffs2_raw_xattr rx;
8959-	struct kvec vecs[2];
8960-	size_t length;
8961-	int rc, totlen;
8962-	uint32_t phys_ofs = write_ofs(c);
8963-
8964-	BUG_ON(!xd->xname);
8965-	BUG_ON(xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID));
8966-
8967-	vecs[0].iov_base = &rx;
8968-	vecs[0].iov_len = sizeof(rx);
8969-	vecs[1].iov_base = xd->xname;
8970-	vecs[1].iov_len = xd->name_len + 1 + xd->value_len;
8971-	totlen = vecs[0].iov_len + vecs[1].iov_len;
8972-
8973-	/* Setup raw-xattr */
8974-	memset(&rx, 0, sizeof(rx));
8975-	rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
8976-	rx.nodetype = cpu_to_je16(JFFS2_NODETYPE_XATTR);
8977-	rx.totlen = cpu_to_je32(PAD(totlen));
8978-	rx.hdr_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_unknown_node) - 4));
8979-
8980-	rx.xid = cpu_to_je32(xd->xid);
8981-	rx.version = cpu_to_je32(++xd->version);
8982-	rx.xprefix = xd->xprefix;
8983-	rx.name_len = xd->name_len;
8984-	rx.value_len = cpu_to_je16(xd->value_len);
8985-	rx.data_crc = cpu_to_je32(crc32(0, vecs[1].iov_base, vecs[1].iov_len));
8986-	rx.node_crc = cpu_to_je32(crc32(0, &rx, sizeof(struct jffs2_raw_xattr) - 4));
8987-
8988-	rc = jffs2_flash_writev(c, vecs, 2, phys_ofs, &length, 0);
8989-	if (rc || totlen != length) {
8990-		JFFS2_WARNING("jffs2_flash_writev()=%d, req=%u, wrote=%zu, at %#08x\n",
8991-			      rc, totlen, length, phys_ofs);
8992-		rc = rc ? rc : -EIO;
8993-		if (length)
8994-			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(totlen), NULL);
8995-
8996-		return rc;
8997-	}
8998-	/* success */
8999-	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(totlen), (void *)xd);
9000-
9001-	dbg_xattr("success on saving xdatum (xid=%u, version=%u, xprefix=%u, xname='%s')\n",
9002-		  xd->xid, xd->version, xd->xprefix, xd->xname);
9003-
9004-	return 0;
9005-}
9006-
9007-static struct jffs2_xattr_datum *create_xattr_datum(struct jffs2_sb_info *c,
9008-						    int xprefix, const char *xname,
9009-						    const char *xvalue, int xsize)
9010-{
9011-	/* must be called under down_write(xattr_sem) */
9012-	struct jffs2_xattr_datum *xd;
9013-	uint32_t hashkey, name_len;
9014-	char *data;
9015-	int i, rc;
9016-
9017-	/* Search xattr_datum has same xname/xvalue by index */
9018-	hashkey = xattr_datum_hashkey(xprefix, xname, xvalue, xsize);
9019-	i = hashkey % XATTRINDEX_HASHSIZE;
9020-	list_for_each_entry(xd, &c->xattrindex[i], xindex) {
9021-		if (xd->hashkey==hashkey
9022-		    && xd->xprefix==xprefix
9023-		    && xd->value_len==xsize
9024-		    && !strcmp(xd->xname, xname)
9025-		    && !memcmp(xd->xvalue, xvalue, xsize)) {
9026-			atomic_inc(&xd->refcnt);
9027-			return xd;
9028-		}
9029-	}
9030-
9031-	/* Not found, Create NEW XATTR-Cache */
9032-	name_len = strlen(xname);
9033-
9034-	xd = jffs2_alloc_xattr_datum();
9035-	if (!xd)
9036-		return ERR_PTR(-ENOMEM);
9037-
9038-	data = kmalloc(name_len + 1 + xsize, GFP_KERNEL);
9039-	if (!data) {
9040-		jffs2_free_xattr_datum(xd);
9041-		return ERR_PTR(-ENOMEM);
9042-	}
9043-	strcpy(data, xname);
9044-	memcpy(data + name_len + 1, xvalue, xsize);
9045-
9046-	atomic_set(&xd->refcnt, 1);
9047-	xd->xid = ++c->highest_xid;
9048-	xd->flags |= JFFS2_XFLAGS_HOT;
9049-	xd->xprefix = xprefix;
9050-
9051-	xd->hashkey = hashkey;
9052-	xd->xname = data;
9053-	xd->xvalue = data + name_len + 1;
9054-	xd->name_len = name_len;
9055-	xd->value_len = xsize;
9056-	xd->data_crc = crc32(0, data, xd->name_len + 1 + xd->value_len);
9057-
9058-	rc = save_xattr_datum(c, xd);
9059-	if (rc) {
9060-		kfree(xd->xname);
9061-		jffs2_free_xattr_datum(xd);
9062-		return ERR_PTR(rc);
9063-	}
9064-
9065-	/* Insert Hash Index */
9066-	i = hashkey % XATTRINDEX_HASHSIZE;
9067-	list_add(&xd->xindex, &c->xattrindex[i]);
9068-
9069-	c->xdatum_mem_usage += (xd->name_len + 1 + xd->value_len);
9070-	reclaim_xattr_datum(c);
9071-
9072-	return xd;
9073-}
9074-
9075-static void unrefer_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
9076-{
9077-	/* must be called under down_write(xattr_sem) */
9078-	if (atomic_dec_and_lock(&xd->refcnt, &c->erase_completion_lock)) {
9079-		unload_xattr_datum(c, xd);
9080-		xd->flags |= JFFS2_XFLAGS_DEAD;
9081-		if (xd->node == (void *)xd) {
9082-			BUG_ON(!(xd->flags & JFFS2_XFLAGS_INVALID));
9083-			jffs2_free_xattr_datum(xd);
9084-		} else {
9085-			list_add(&xd->xindex, &c->xattr_dead_list);
9086-		}
9087-		spin_unlock(&c->erase_completion_lock);
9088-
9089-		dbg_xattr("xdatum(xid=%u, version=%u) was removed.\n",
9090-			  xd->xid, xd->version);
9091-	}
9092-}
9093-
9094-/* -------- xref related functions ------------------
9095- * verify_xattr_ref(c, ref)
9096- *   is used to load xref information from medium. Because summary data does not
9097- *   contain xid/ino, it's necessary to verify once while mounting process.
9098- * save_xattr_ref(c, ref)
9099- *   is used to write xref to medium. If delete marker is marked, it write
9100- *   a delete marker of xref into medium.
9101- * create_xattr_ref(c, ic, xd)
9102- *   is used to create a new xref and write to medium.
9103- * delete_xattr_ref(c, ref)
9104- *   is used to delete jffs2_xattr_ref. It marks xref XREF_DELETE_MARKER,
9105- *   and allows GC to reclaim those physical nodes.
9106- * jffs2_xattr_delete_inode(c, ic)
9107- *   is called to remove xrefs related to obsolete inode when inode is unlinked.
9108- * jffs2_xattr_free_inode(c, ic)
9109- *   is called to release xattr related objects when unmounting.
9110- * check_xattr_ref_inode(c, ic)
9111- *   is used to confirm inode does not have duplicate xattr name/value pair.
9112- * jffs2_xattr_do_crccheck_inode(c, ic)
9113- *   is used to force xattr data integrity check during the initial gc scan.
9114- * -------------------------------------------------- */
9115-static int verify_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
9116-{
9117-	struct jffs2_eraseblock *jeb;
9118-	struct jffs2_raw_node_ref *raw;
9119-	struct jffs2_raw_xref rr;
9120-	size_t readlen;
9121-	uint32_t crc, offset, totlen;
9122-	int rc;
9123-
9124-	spin_lock(&c->erase_completion_lock);
9125-	if (ref_flags(ref->node) != REF_UNCHECKED)
9126-		goto complete;
9127-	offset = ref_offset(ref->node);
9128-	spin_unlock(&c->erase_completion_lock);
9129-
9130-	rc = jffs2_flash_read(c, offset, sizeof(rr), &readlen, (char *)&rr);
9131-	if (rc || sizeof(rr) != readlen) {
9132-		JFFS2_WARNING("jffs2_flash_read()=%d, req=%zu, read=%zu, at %#08x\n",
9133-			      rc, sizeof(rr), readlen, offset);
9134-		return rc ? rc : -EIO;
9135-	}
9136-	/* obsolete node */
9137-	crc = crc32(0, &rr, sizeof(rr) - 4);
9138-	if (crc != je32_to_cpu(rr.node_crc)) {
9139-		JFFS2_ERROR("node CRC failed at %#08x, read=%#08x, calc=%#08x\n",
9140-			    offset, je32_to_cpu(rr.node_crc), crc);
9141-		return JFFS2_XATTR_IS_CORRUPTED;
9142-	}
9143-	if (je16_to_cpu(rr.magic) != JFFS2_MAGIC_BITMASK
9144-	    || je16_to_cpu(rr.nodetype) != JFFS2_NODETYPE_XREF
9145-	    || je32_to_cpu(rr.totlen) != PAD(sizeof(rr))) {
9146-		JFFS2_ERROR("inconsistent xref at %#08x, magic=%#04x/%#04x, "
9147-			    "nodetype=%#04x/%#04x, totlen=%u/%zu\n",
9148-			    offset, je16_to_cpu(rr.magic), JFFS2_MAGIC_BITMASK,
9149-			    je16_to_cpu(rr.nodetype), JFFS2_NODETYPE_XREF,
9150-			    je32_to_cpu(rr.totlen), PAD(sizeof(rr)));
9151-		return JFFS2_XATTR_IS_CORRUPTED;
9152-	}
9153-	ref->ino = je32_to_cpu(rr.ino);
9154-	ref->xid = je32_to_cpu(rr.xid);
9155-	ref->xseqno = je32_to_cpu(rr.xseqno);
9156-	if (ref->xseqno > c->highest_xseqno)
9157-		c->highest_xseqno = (ref->xseqno & ~XREF_DELETE_MARKER);
9158-
9159-	spin_lock(&c->erase_completion_lock);
9160- complete:
9161-	for (raw=ref->node; raw != (void *)ref; raw=raw->next_in_ino) {
9162-		jeb = &c->blocks[ref_offset(raw) / c->sector_size];
9163-		totlen = PAD(ref_totlen(c, jeb, raw));
9164-		if (ref_flags(raw) == REF_UNCHECKED) {
9165-			c->unchecked_size -= totlen; c->used_size += totlen;
9166-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
9167-		}
9168-		raw->flash_offset = ref_offset(raw) | ((ref->node==raw) ? REF_PRISTINE : REF_NORMAL);
9169-	}
9170-	spin_unlock(&c->erase_completion_lock);
9171-
9172-	dbg_xattr("success on verifying xref (ino=%u, xid=%u) at %#08x\n",
9173-		  ref->ino, ref->xid, ref_offset(ref->node));
9174-	return 0;
9175-}
9176-
9177-static int save_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
9178-{
9179-	/* must be called under down_write(xattr_sem) */
9180-	struct jffs2_raw_xref rr;
9181-	size_t length;
9182-	uint32_t xseqno, phys_ofs = write_ofs(c);
9183-	int ret;
9184-
9185-	rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
9186-	rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
9187-	rr.totlen = cpu_to_je32(PAD(sizeof(rr)));
9188-	rr.hdr_crc = cpu_to_je32(crc32(0, &rr, sizeof(struct jffs2_unknown_node) - 4));
9189-
9190-	xseqno = (c->highest_xseqno += 2);
9191-	if (is_xattr_ref_dead(ref)) {
9192-		xseqno |= XREF_DELETE_MARKER;
9193-		rr.ino = cpu_to_je32(ref->ino);
9194-		rr.xid = cpu_to_je32(ref->xid);
9195-	} else {
9196-		rr.ino = cpu_to_je32(ref->ic->ino);
9197-		rr.xid = cpu_to_je32(ref->xd->xid);
9198-	}
9199-	rr.xseqno = cpu_to_je32(xseqno);
9200-	rr.node_crc = cpu_to_je32(crc32(0, &rr, sizeof(rr) - 4));
9201-
9202-	ret = jffs2_flash_write(c, phys_ofs, sizeof(rr), &length, (char *)&rr);
9203-	if (ret || sizeof(rr) != length) {
9204-		JFFS2_WARNING("jffs2_flash_write() returned %d, request=%zu, retlen=%zu, at %#08x\n",
9205-			      ret, sizeof(rr), length, phys_ofs);
9206-		ret = ret ? ret : -EIO;
9207-		if (length)
9208-			jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, PAD(sizeof(rr)), NULL);
9209-
9210-		return ret;
9211-	}
9212-	/* success */
9213-	ref->xseqno = xseqno;
9214-	jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, PAD(sizeof(rr)), (void *)ref);
9215-
9216-	dbg_xattr("success on saving xref (ino=%u, xid=%u)\n", ref->ic->ino, ref->xd->xid);
9217-
9218-	return 0;
9219-}
9220-
9221-static struct jffs2_xattr_ref *create_xattr_ref(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic,
9222-						struct jffs2_xattr_datum *xd)
9223-{
9224-	/* must be called under down_write(xattr_sem) */
9225-	struct jffs2_xattr_ref *ref;
9226-	int ret;
9227-
9228-	ref = jffs2_alloc_xattr_ref();
9229-	if (!ref)
9230-		return ERR_PTR(-ENOMEM);
9231-	ref->ic = ic;
9232-	ref->xd = xd;
9233-
9234-	ret = save_xattr_ref(c, ref);
9235-	if (ret) {
9236-		jffs2_free_xattr_ref(ref);
9237-		return ERR_PTR(ret);
9238-	}
9239-
9240-	/* Chain to inode */
9241-	ref->next = ic->xref;
9242-	ic->xref = ref;
9243-
9244-	return ref; /* success */
9245-}
9246-
9247-static void delete_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
9248-{
9249-	/* must be called under down_write(xattr_sem) */
9250-	struct jffs2_xattr_datum *xd;
9251-
9252-	xd = ref->xd;
9253-	ref->xseqno |= XREF_DELETE_MARKER;
9254-	ref->ino = ref->ic->ino;
9255-	ref->xid = ref->xd->xid;
9256-	spin_lock(&c->erase_completion_lock);
9257-	ref->next = c->xref_dead_list;
9258-	c->xref_dead_list = ref;
9259-	spin_unlock(&c->erase_completion_lock);
9260-
9261-	dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) was removed.\n",
9262-		  ref->ino, ref->xid, ref->xseqno);
9263-
9264-	unrefer_xattr_datum(c, xd);
9265-}
9266-
9267-void jffs2_xattr_delete_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9268-{
9269-	/* It's called from jffs2_evict_inode() on inode removing.
9270-	   When an inode with XATTR is removed, those XATTRs must be removed. */
9271-	struct jffs2_xattr_ref *ref, *_ref;
9272-
9273-	if (!ic || ic->pino_nlink > 0)
9274-		return;
9275-
9276-	down_write(&c->xattr_sem);
9277-	for (ref = ic->xref; ref; ref = _ref) {
9278-		_ref = ref->next;
9279-		delete_xattr_ref(c, ref);
9280-	}
9281-	ic->xref = NULL;
9282-	up_write(&c->xattr_sem);
9283-}
9284-
9285-void jffs2_xattr_free_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9286-{
9287-	/* It's called from jffs2_free_ino_caches() until unmounting FS. */
9288-	struct jffs2_xattr_datum *xd;
9289-	struct jffs2_xattr_ref *ref, *_ref;
9290-
9291-	down_write(&c->xattr_sem);
9292-	for (ref = ic->xref; ref; ref = _ref) {
9293-		_ref = ref->next;
9294-		xd = ref->xd;
9295-		if (atomic_dec_and_test(&xd->refcnt)) {
9296-			unload_xattr_datum(c, xd);
9297-			jffs2_free_xattr_datum(xd);
9298-		}
9299-		jffs2_free_xattr_ref(ref);
9300-	}
9301-	ic->xref = NULL;
9302-	up_write(&c->xattr_sem);
9303-}
9304-
9305-static int check_xattr_ref_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9306-{
9307-	/* success of check_xattr_ref_inode() means that inode (ic) dose not have
9308-	 * duplicate name/value pairs. If duplicate name/value pair would be found,
9309-	 * one will be removed.
9310-	 */
9311-	struct jffs2_xattr_ref *ref, *cmp, **pref, **pcmp;
9312-	int rc = 0;
9313-
9314-	if (likely(ic->flags & INO_FLAGS_XATTR_CHECKED))
9315-		return 0;
9316-	down_write(&c->xattr_sem);
9317- retry:
9318-	rc = 0;
9319-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9320-		if (!ref->xd->xname) {
9321-			rc = load_xattr_datum(c, ref->xd);
9322-			if (unlikely(rc > 0)) {
9323-				*pref = ref->next;
9324-				delete_xattr_ref(c, ref);
9325-				goto retry;
9326-			} else if (unlikely(rc < 0))
9327-				goto out;
9328-		}
9329-		for (cmp=ref->next, pcmp=&ref->next; cmp; pcmp=&cmp->next, cmp=cmp->next) {
9330-			if (!cmp->xd->xname) {
9331-				ref->xd->flags |= JFFS2_XFLAGS_BIND;
9332-				rc = load_xattr_datum(c, cmp->xd);
9333-				ref->xd->flags &= ~JFFS2_XFLAGS_BIND;
9334-				if (unlikely(rc > 0)) {
9335-					*pcmp = cmp->next;
9336-					delete_xattr_ref(c, cmp);
9337-					goto retry;
9338-				} else if (unlikely(rc < 0))
9339-					goto out;
9340-			}
9341-			if (ref->xd->xprefix == cmp->xd->xprefix
9342-			    && !strcmp(ref->xd->xname, cmp->xd->xname)) {
9343-				if (ref->xseqno > cmp->xseqno) {
9344-					*pcmp = cmp->next;
9345-					delete_xattr_ref(c, cmp);
9346-				} else {
9347-					*pref = ref->next;
9348-					delete_xattr_ref(c, ref);
9349-				}
9350-				goto retry;
9351-			}
9352-		}
9353-	}
9354-	ic->flags |= INO_FLAGS_XATTR_CHECKED;
9355- out:
9356-	up_write(&c->xattr_sem);
9357-
9358-	return rc;
9359-}
9360-
9361-void jffs2_xattr_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
9362-{
9363-	check_xattr_ref_inode(c, ic);
9364-}
9365-
9366-/* -------- xattr subsystem functions ---------------
9367- * jffs2_init_xattr_subsystem(c)
9368- *   is used to initialize semaphore and list_head, and some variables.
9369- * jffs2_find_xattr_datum(c, xid)
9370- *   is used to lookup xdatum while scanning process.
9371- * jffs2_clear_xattr_subsystem(c)
9372- *   is used to release any xattr related objects.
9373- * jffs2_build_xattr_subsystem(c)
9374- *   is used to associate xdatum and xref while super block building process.
9375- * jffs2_setup_xattr_datum(c, xid, version)
9376- *   is used to insert xdatum while scanning process.
9377- * -------------------------------------------------- */
9378-void jffs2_init_xattr_subsystem(struct jffs2_sb_info *c)
9379-{
9380-	int i;
9381-
9382-	for (i=0; i < XATTRINDEX_HASHSIZE; i++)
9383-		INIT_LIST_HEAD(&c->xattrindex[i]);
9384-	INIT_LIST_HEAD(&c->xattr_unchecked);
9385-	INIT_LIST_HEAD(&c->xattr_dead_list);
9386-	c->xref_dead_list = NULL;
9387-	c->xref_temp = NULL;
9388-
9389-	init_rwsem(&c->xattr_sem);
9390-	c->highest_xid = 0;
9391-	c->highest_xseqno = 0;
9392-	c->xdatum_mem_usage = 0;
9393-	c->xdatum_mem_threshold = 32 * 1024;	/* Default 32KB */
9394-}
9395-
9396-static struct jffs2_xattr_datum *jffs2_find_xattr_datum(struct jffs2_sb_info *c, uint32_t xid)
9397-{
9398-	struct jffs2_xattr_datum *xd;
9399-	int i = xid % XATTRINDEX_HASHSIZE;
9400-
9401-	/* It's only used in scanning/building process. */
9402-	BUG_ON(!(c->flags & (JFFS2_SB_FLAG_SCANNING|JFFS2_SB_FLAG_BUILDING)));
9403-
9404-	list_for_each_entry(xd, &c->xattrindex[i], xindex) {
9405-		if (xd->xid==xid)
9406-			return xd;
9407-	}
9408-	return NULL;
9409-}
9410-
9411-void jffs2_clear_xattr_subsystem(struct jffs2_sb_info *c)
9412-{
9413-	struct jffs2_xattr_datum *xd, *_xd;
9414-	struct jffs2_xattr_ref *ref, *_ref;
9415-	int i;
9416-
9417-	for (ref=c->xref_temp; ref; ref = _ref) {
9418-		_ref = ref->next;
9419-		jffs2_free_xattr_ref(ref);
9420-	}
9421-
9422-	for (ref=c->xref_dead_list; ref; ref = _ref) {
9423-		_ref = ref->next;
9424-		jffs2_free_xattr_ref(ref);
9425-	}
9426-
9427-	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
9428-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
9429-			list_del(&xd->xindex);
9430-			kfree(xd->xname);
9431-			jffs2_free_xattr_datum(xd);
9432-		}
9433-	}
9434-
9435-	list_for_each_entry_safe(xd, _xd, &c->xattr_dead_list, xindex) {
9436-		list_del(&xd->xindex);
9437-		jffs2_free_xattr_datum(xd);
9438-	}
9439-	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
9440-		list_del(&xd->xindex);
9441-		jffs2_free_xattr_datum(xd);
9442-	}
9443-}
9444-
9445-#define XREF_TMPHASH_SIZE	(128)
9446-void jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
9447-{
9448-	struct jffs2_xattr_ref *ref, *_ref;
9449-	struct jffs2_xattr_ref *xref_tmphash[XREF_TMPHASH_SIZE];
9450-	struct jffs2_xattr_datum *xd, *_xd;
9451-	struct jffs2_inode_cache *ic;
9452-	struct jffs2_raw_node_ref *raw;
9453-	int i, xdatum_count = 0, xdatum_unchecked_count = 0, xref_count = 0;
9454-	int xdatum_orphan_count = 0, xref_orphan_count = 0, xref_dead_count = 0;
9455-
9456-	BUG_ON(!(c->flags & JFFS2_SB_FLAG_BUILDING));
9457-
9458-	/* Phase.1 : Merge same xref */
9459-	for (i=0; i < XREF_TMPHASH_SIZE; i++)
9460-		xref_tmphash[i] = NULL;
9461-	for (ref=c->xref_temp; ref; ref=_ref) {
9462-		struct jffs2_xattr_ref *tmp;
9463-
9464-		_ref = ref->next;
9465-		if (ref_flags(ref->node) != REF_PRISTINE) {
9466-			if (verify_xattr_ref(c, ref)) {
9467-				BUG_ON(ref->node->next_in_ino != (void *)ref);
9468-				ref->node->next_in_ino = NULL;
9469-				jffs2_mark_node_obsolete(c, ref->node);
9470-				jffs2_free_xattr_ref(ref);
9471-				continue;
9472-			}
9473-		}
9474-
9475-		i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
9476-		for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
9477-			if (tmp->ino == ref->ino && tmp->xid == ref->xid)
9478-				break;
9479-		}
9480-		if (tmp) {
9481-			raw = ref->node;
9482-			if (ref->xseqno > tmp->xseqno) {
9483-				tmp->xseqno = ref->xseqno;
9484-				raw->next_in_ino = tmp->node;
9485-				tmp->node = raw;
9486-			} else {
9487-				raw->next_in_ino = tmp->node->next_in_ino;
9488-				tmp->node->next_in_ino = raw;
9489-			}
9490-			jffs2_free_xattr_ref(ref);
9491-			continue;
9492-		} else {
9493-			ref->next = xref_tmphash[i];
9494-			xref_tmphash[i] = ref;
9495-		}
9496-	}
9497-	c->xref_temp = NULL;
9498-
9499-	/* Phase.2 : Bind xref with inode_cache and xattr_datum */
9500-	for (i=0; i < XREF_TMPHASH_SIZE; i++) {
9501-		for (ref=xref_tmphash[i]; ref; ref=_ref) {
9502-			xref_count++;
9503-			_ref = ref->next;
9504-			if (is_xattr_ref_dead(ref)) {
9505-				ref->next = c->xref_dead_list;
9506-				c->xref_dead_list = ref;
9507-				xref_dead_count++;
9508-				continue;
9509-			}
9510-			/* At this point, ref->xid and ref->ino contain XID and inode number.
9511-			   ref->xd and ref->ic are not valid yet. */
9512-			xd = jffs2_find_xattr_datum(c, ref->xid);
9513-			ic = jffs2_get_ino_cache(c, ref->ino);
9514-			if (!xd || !ic || !ic->pino_nlink) {
9515-				dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
9516-					  ref->ino, ref->xid, ref->xseqno);
9517-				ref->xseqno |= XREF_DELETE_MARKER;
9518-				ref->next = c->xref_dead_list;
9519-				c->xref_dead_list = ref;
9520-				xref_orphan_count++;
9521-				continue;
9522-			}
9523-			ref->xd = xd;
9524-			ref->ic = ic;
9525-			atomic_inc(&xd->refcnt);
9526-			ref->next = ic->xref;
9527-			ic->xref = ref;
9528-		}
9529-	}
9530-
9531-	/* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
9532-	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
9533-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
9534-			xdatum_count++;
9535-			list_del_init(&xd->xindex);
9536-			if (!atomic_read(&xd->refcnt)) {
9537-				dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
9538-					  xd->xid, xd->version);
9539-				xd->flags |= JFFS2_XFLAGS_DEAD;
9540-				list_add(&xd->xindex, &c->xattr_unchecked);
9541-				xdatum_orphan_count++;
9542-				continue;
9543-			}
9544-			if (is_xattr_datum_unchecked(c, xd)) {
9545-				dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
9546-					  xd->xid, xd->version);
9547-				list_add(&xd->xindex, &c->xattr_unchecked);
9548-				xdatum_unchecked_count++;
9549-			}
9550-		}
9551-	}
9552-	/* build complete */
9553-	JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
9554-		     " (%u unchecked, %u orphan) and "
9555-		     "%u of xref (%u dead, %u orphan) found.\n",
9556-		     xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
9557-		     xref_count, xref_dead_count, xref_orphan_count);
9558-}
9559-
9560-struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
9561-						  uint32_t xid, uint32_t version)
9562-{
9563-	struct jffs2_xattr_datum *xd;
9564-
9565-	xd = jffs2_find_xattr_datum(c, xid);
9566-	if (!xd) {
9567-		xd = jffs2_alloc_xattr_datum();
9568-		if (!xd)
9569-			return ERR_PTR(-ENOMEM);
9570-		xd->xid = xid;
9571-		xd->version = version;
9572-		if (xd->xid > c->highest_xid)
9573-			c->highest_xid = xd->xid;
9574-		list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
9575-	}
9576-	return xd;
9577-}
9578-
9579-/* -------- xattr subsystem functions ---------------
9580- * xprefix_to_handler(xprefix)
9581- *   is used to translate xprefix into xattr_handler.
9582- * jffs2_listxattr(dentry, buffer, size)
9583- *   is an implementation of listxattr handler on jffs2.
9584- * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
9585- *   is an implementation of getxattr handler on jffs2.
9586- * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
9587- *   is an implementation of setxattr handler on jffs2.
9588- * -------------------------------------------------- */
9589-const struct xattr_handler *jffs2_xattr_handlers[] = {
9590-	&jffs2_user_xattr_handler,
9591-#ifdef CONFIG_JFFS2_FS_SECURITY
9592-	&jffs2_security_xattr_handler,
9593-#endif
9594-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
9595-	&posix_acl_access_xattr_handler,
9596-	&posix_acl_default_xattr_handler,
9597-#endif
9598-	&jffs2_trusted_xattr_handler,
9599-	NULL
9600-};
9601-
9602-static const struct xattr_handler *xprefix_to_handler(int xprefix) {
9603-	const struct xattr_handler *ret;
9604-
9605-	switch (xprefix) {
9606-	case JFFS2_XPREFIX_USER:
9607-		ret = &jffs2_user_xattr_handler;
9608-		break;
9609-#ifdef CONFIG_JFFS2_FS_SECURITY
9610-	case JFFS2_XPREFIX_SECURITY:
9611-		ret = &jffs2_security_xattr_handler;
9612-		break;
9613-#endif
9614-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
9615-	case JFFS2_XPREFIX_ACL_ACCESS:
9616-		ret = &posix_acl_access_xattr_handler;
9617-		break;
9618-	case JFFS2_XPREFIX_ACL_DEFAULT:
9619-		ret = &posix_acl_default_xattr_handler;
9620-		break;
9621-#endif
9622-	case JFFS2_XPREFIX_TRUSTED:
9623-		ret = &jffs2_trusted_xattr_handler;
9624-		break;
9625-	default:
9626-		ret = NULL;
9627-		break;
9628-	}
9629-	return ret;
9630-}
9631-
9632-ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
9633-{
9634-	struct inode *inode = d_inode(dentry);
9635-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9636-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9637-	struct jffs2_inode_cache *ic = f->inocache;
9638-	struct jffs2_xattr_ref *ref, **pref;
9639-	struct jffs2_xattr_datum *xd;
9640-	const struct xattr_handler *xhandle;
9641-	const char *prefix;
9642-	ssize_t prefix_len, len, rc;
9643-	int retry = 0;
9644-
9645-	rc = check_xattr_ref_inode(c, ic);
9646-	if (unlikely(rc))
9647-		return rc;
9648-
9649-	down_read(&c->xattr_sem);
9650- retry:
9651-	len = 0;
9652-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9653-		BUG_ON(ref->ic != ic);
9654-		xd = ref->xd;
9655-		if (!xd->xname) {
9656-			/* xdatum is unchached */
9657-			if (!retry) {
9658-				retry = 1;
9659-				up_read(&c->xattr_sem);
9660-				down_write(&c->xattr_sem);
9661-				goto retry;
9662-			} else {
9663-				rc = load_xattr_datum(c, xd);
9664-				if (unlikely(rc > 0)) {
9665-					*pref = ref->next;
9666-					delete_xattr_ref(c, ref);
9667-					goto retry;
9668-				} else if (unlikely(rc < 0))
9669-					goto out;
9670-			}
9671-		}
9672-		xhandle = xprefix_to_handler(xd->xprefix);
9673-		if (!xhandle || (xhandle->list && !xhandle->list(dentry)))
9674-			continue;
9675-		prefix = xhandle->prefix ?: xhandle->name;
9676-		prefix_len = strlen(prefix);
9677-		rc = prefix_len + xd->name_len + 1;
9678-
9679-		if (buffer) {
9680-			if (rc > size - len) {
9681-				rc = -ERANGE;
9682-				goto out;
9683-			}
9684-			memcpy(buffer, prefix, prefix_len);
9685-			buffer += prefix_len;
9686-			memcpy(buffer, xd->xname, xd->name_len);
9687-			buffer += xd->name_len;
9688-			*buffer++ = 0;
9689-		}
9690-		len += rc;
9691-	}
9692-	rc = len;
9693- out:
9694-	if (!retry) {
9695-		up_read(&c->xattr_sem);
9696-	} else {
9697-		up_write(&c->xattr_sem);
9698-	}
9699-	return rc;
9700-}
9701-
9702-int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
9703-		      char *buffer, size_t size)
9704-{
9705-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9706-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9707-	struct jffs2_inode_cache *ic = f->inocache;
9708-	struct jffs2_xattr_datum *xd;
9709-	struct jffs2_xattr_ref *ref, **pref;
9710-	int rc, retry = 0;
9711-
9712-	rc = check_xattr_ref_inode(c, ic);
9713-	if (unlikely(rc))
9714-		return rc;
9715-
9716-	down_read(&c->xattr_sem);
9717- retry:
9718-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9719-		BUG_ON(ref->ic!=ic);
9720-
9721-		xd = ref->xd;
9722-		if (xd->xprefix != xprefix)
9723-			continue;
9724-		if (!xd->xname) {
9725-			/* xdatum is unchached */
9726-			if (!retry) {
9727-				retry = 1;
9728-				up_read(&c->xattr_sem);
9729-				down_write(&c->xattr_sem);
9730-				goto retry;
9731-			} else {
9732-				rc = load_xattr_datum(c, xd);
9733-				if (unlikely(rc > 0)) {
9734-					*pref = ref->next;
9735-					delete_xattr_ref(c, ref);
9736-					goto retry;
9737-				} else if (unlikely(rc < 0)) {
9738-					goto out;
9739-				}
9740-			}
9741-		}
9742-		if (!strcmp(xname, xd->xname)) {
9743-			rc = xd->value_len;
9744-			if (buffer) {
9745-				if (size < rc) {
9746-					rc = -ERANGE;
9747-				} else {
9748-					memcpy(buffer, xd->xvalue, rc);
9749-				}
9750-			}
9751-			goto out;
9752-		}
9753-	}
9754-	rc = -ENODATA;
9755- out:
9756-	if (!retry) {
9757-		up_read(&c->xattr_sem);
9758-	} else {
9759-		up_write(&c->xattr_sem);
9760-	}
9761-	return rc;
9762-}
9763-
9764-int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
9765-		      const char *buffer, size_t size, int flags)
9766-{
9767-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9768-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9769-	struct jffs2_inode_cache *ic = f->inocache;
9770-	struct jffs2_xattr_datum *xd;
9771-	struct jffs2_xattr_ref *ref, *newref, **pref;
9772-	uint32_t length, request;
9773-	int rc;
9774-
9775-	rc = check_xattr_ref_inode(c, ic);
9776-	if (unlikely(rc))
9777-		return rc;
9778-
9779-	request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
9780-	rc = jffs2_reserve_space(c, request, &length,
9781-				 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
9782-	if (rc) {
9783-		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
9784-		return rc;
9785-	}
9786-
9787-	/* Find existing xattr */
9788-	down_write(&c->xattr_sem);
9789- retry:
9790-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9791-		xd = ref->xd;
9792-		if (xd->xprefix != xprefix)
9793-			continue;
9794-		if (!xd->xname) {
9795-			rc = load_xattr_datum(c, xd);
9796-			if (unlikely(rc > 0)) {
9797-				*pref = ref->next;
9798-				delete_xattr_ref(c, ref);
9799-				goto retry;
9800-			} else if (unlikely(rc < 0))
9801-				goto out;
9802-		}
9803-		if (!strcmp(xd->xname, xname)) {
9804-			if (flags & XATTR_CREATE) {
9805-				rc = -EEXIST;
9806-				goto out;
9807-			}
9808-			if (!buffer) {
9809-				ref->ino = ic->ino;
9810-				ref->xid = xd->xid;
9811-				ref->xseqno |= XREF_DELETE_MARKER;
9812-				rc = save_xattr_ref(c, ref);
9813-				if (!rc) {
9814-					*pref = ref->next;
9815-					spin_lock(&c->erase_completion_lock);
9816-					ref->next = c->xref_dead_list;
9817-					c->xref_dead_list = ref;
9818-					spin_unlock(&c->erase_completion_lock);
9819-					unrefer_xattr_datum(c, xd);
9820-				} else {
9821-					ref->ic = ic;
9822-					ref->xd = xd;
9823-					ref->xseqno &= ~XREF_DELETE_MARKER;
9824-				}
9825-				goto out;
9826-			}
9827-			goto found;
9828-		}
9829-	}
9830-	/* not found */
9831-	if (flags & XATTR_REPLACE) {
9832-		rc = -ENODATA;
9833-		goto out;
9834-	}
9835-	if (!buffer) {
9836-		rc = -ENODATA;
9837-		goto out;
9838-	}
9839- found:
9840-	xd = create_xattr_datum(c, xprefix, xname, buffer, size);
9841-	if (IS_ERR(xd)) {
9842-		rc = PTR_ERR(xd);
9843-		goto out;
9844-	}
9845-	up_write(&c->xattr_sem);
9846-	jffs2_complete_reservation(c);
9847-
9848-	/* create xattr_ref */
9849-	request = PAD(sizeof(struct jffs2_raw_xref));
9850-	rc = jffs2_reserve_space(c, request, &length,
9851-				 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
9852-	down_write(&c->xattr_sem);
9853-	if (rc) {
9854-		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
9855-		unrefer_xattr_datum(c, xd);
9856-		up_write(&c->xattr_sem);
9857-		return rc;
9858-	}
9859-	if (ref)
9860-		*pref = ref->next;
9861-	newref = create_xattr_ref(c, ic, xd);
9862-	if (IS_ERR(newref)) {
9863-		if (ref) {
9864-			ref->next = ic->xref;
9865-			ic->xref = ref;
9866-		}
9867-		rc = PTR_ERR(newref);
9868-		unrefer_xattr_datum(c, xd);
9869-	} else if (ref) {
9870-		delete_xattr_ref(c, ref);
9871-	}
9872- out:
9873-	up_write(&c->xattr_sem);
9874-	jffs2_complete_reservation(c);
9875-	return rc;
9876-}
9877-
9878-/* -------- garbage collector functions -------------
9879- * jffs2_garbage_collect_xattr_datum(c, xd, raw)
9880- *   is used to move xdatum into new node.
9881- * jffs2_garbage_collect_xattr_ref(c, ref, raw)
9882- *   is used to move xref into new node.
9883- * jffs2_verify_xattr(c)
9884- *   is used to call do_verify_xattr_datum() before garbage collecting.
9885- * jffs2_release_xattr_datum(c, xd)
9886- *   is used to release an in-memory object of xdatum.
9887- * jffs2_release_xattr_ref(c, ref)
9888- *   is used to release an in-memory object of xref.
9889- * -------------------------------------------------- */
9890-int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
9891-				      struct jffs2_raw_node_ref *raw)
9892-{
9893-	uint32_t totlen, length, old_ofs;
9894-	int rc = 0;
9895-
9896-	down_write(&c->xattr_sem);
9897-	if (xd->node != raw)
9898-		goto out;
9899-	if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
9900-		goto out;
9901-
9902-	rc = load_xattr_datum(c, xd);
9903-	if (unlikely(rc)) {
9904-		rc = (rc > 0) ? 0 : rc;
9905-		goto out;
9906-	}
9907-	old_ofs = ref_offset(xd->node);
9908-	totlen = PAD(sizeof(struct jffs2_raw_xattr)
9909-			+ xd->name_len + 1 + xd->value_len);
9910-	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
9911-	if (rc) {
9912-		JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
9913-		goto out;
9914-	}
9915-	rc = save_xattr_datum(c, xd);
9916-	if (!rc)
9917-		dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
9918-			  xd->xid, xd->version, old_ofs, ref_offset(xd->node));
9919- out:
9920-	if (!rc)
9921-		jffs2_mark_node_obsolete(c, raw);
9922-	up_write(&c->xattr_sem);
9923-	return rc;
9924-}
9925-
9926-int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
9927-				    struct jffs2_raw_node_ref *raw)
9928-{
9929-	uint32_t totlen, length, old_ofs;
9930-	int rc = 0;
9931-
9932-	down_write(&c->xattr_sem);
9933-	BUG_ON(!ref->node);
9934-
9935-	if (ref->node != raw)
9936-		goto out;
9937-	if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
9938-		goto out;
9939-
9940-	old_ofs = ref_offset(ref->node);
9941-	totlen = ref_totlen(c, c->gcblock, ref->node);
9942-
9943-	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
9944-	if (rc) {
9945-		JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
9946-			      __func__, rc, totlen);
9947-		goto out;
9948-	}
9949-	rc = save_xattr_ref(c, ref);
9950-	if (!rc)
9951-		dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
9952-			  ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
9953- out:
9954-	if (!rc)
9955-		jffs2_mark_node_obsolete(c, raw);
9956-	up_write(&c->xattr_sem);
9957-	return rc;
9958-}
9959-
9960-int jffs2_verify_xattr(struct jffs2_sb_info *c)
9961-{
9962-	struct jffs2_xattr_datum *xd, *_xd;
9963-	struct jffs2_eraseblock *jeb;
9964-	struct jffs2_raw_node_ref *raw;
9965-	uint32_t totlen;
9966-	int rc;
9967-
9968-	down_write(&c->xattr_sem);
9969-	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
9970-		rc = do_verify_xattr_datum(c, xd);
9971-		if (rc < 0)
9972-			continue;
9973-		list_del_init(&xd->xindex);
9974-		spin_lock(&c->erase_completion_lock);
9975-		for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
9976-			if (ref_flags(raw) != REF_UNCHECKED)
9977-				continue;
9978-			jeb = &c->blocks[ref_offset(raw) / c->sector_size];
9979-			totlen = PAD(ref_totlen(c, jeb, raw));
9980-			c->unchecked_size -= totlen; c->used_size += totlen;
9981-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
9982-			raw->flash_offset = ref_offset(raw)
9983-				| ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
9984-		}
9985-		if (xd->flags & JFFS2_XFLAGS_DEAD)
9986-			list_add(&xd->xindex, &c->xattr_dead_list);
9987-		spin_unlock(&c->erase_completion_lock);
9988-	}
9989-	up_write(&c->xattr_sem);
9990-	return list_empty(&c->xattr_unchecked) ? 1 : 0;
9991-}
9992-
9993-void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
9994-{
9995-	/* must be called under spin_lock(&c->erase_completion_lock) */
9996-	if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
9997-		return;
9998-
9999-	list_del(&xd->xindex);
10000-	jffs2_free_xattr_datum(xd);
10001-}
10002-
10003-void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
10004-{
10005-	/* must be called under spin_lock(&c->erase_completion_lock) */
10006-	struct jffs2_xattr_ref *tmp, **ptmp;
10007-
10008-	if (ref->node != (void *)ref)
10009-		return;
10010-
10011-	for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
10012-		if (ref == tmp) {
10013-			*ptmp = tmp->next;
10014-			break;
10015-		}
10016-	}
10017-	jffs2_free_xattr_ref(ref);
10018-}
10019diff -Nupr old/fs/jffs2/xattr.h new/fs/jffs2/xattr.h
10020--- old/fs/jffs2/xattr.h	2022-05-09 17:22:53.000000000 +0800
10021+++ new/fs/jffs2/xattr.h	2022-05-09 20:04:55.580000000 +0800
10022@@ -12,7 +12,6 @@
10023 #ifndef _JFFS2_FS_XATTR_H_
10024 #define _JFFS2_FS_XATTR_H_
10025
10026-#include <linux/xattr.h>
10027 #include <linux/list.h>
10028
10029 #define JFFS2_XFLAGS_HOT	(0x01)	/* This datum is HOT */
10030@@ -48,7 +47,7 @@ struct jffs2_xattr_ref
10031 	struct jffs2_raw_node_ref *node;
10032 	uint8_t class;
10033 	uint8_t flags;		/* Currently unused */
10034-	u16 unused;
10035+	uint16_t unused;
10036
10037 	uint32_t xseqno;
10038 	union {
10039@@ -89,16 +88,14 @@ extern int jffs2_verify_xattr(struct jff
10040 extern void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd);
10041 extern void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref);
10042
10043-extern int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
10044-			     char *buffer, size_t size);
10045-extern int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
10046-			     const char *buffer, size_t size, int flags);
10047-
10048 extern const struct xattr_handler *jffs2_xattr_handlers[];
10049 extern const struct xattr_handler jffs2_user_xattr_handler;
10050 extern const struct xattr_handler jffs2_trusted_xattr_handler;
10051
10052 extern ssize_t jffs2_listxattr(struct dentry *, char *, size_t);
10053+#define jffs2_getxattr		generic_getxattr
10054+#define jffs2_setxattr		generic_setxattr
10055+#define jffs2_removexattr	generic_removexattr
10056
10057 #else
10058
10059@@ -113,12 +110,13 @@ extern ssize_t jffs2_listxattr(struct de
10060
10061 #define jffs2_xattr_handlers	NULL
10062 #define jffs2_listxattr		NULL
10063+#define jffs2_getxattr		NULL
10064+#define jffs2_setxattr		NULL
10065+#define jffs2_removexattr	NULL
10066
10067 #endif /* CONFIG_JFFS2_FS_XATTR */
10068
10069 #ifdef CONFIG_JFFS2_FS_SECURITY
10070-extern int jffs2_init_security(struct inode *inode, struct inode *dir,
10071-			       const struct qstr *qstr);
10072 extern const struct xattr_handler jffs2_security_xattr_handler;
10073 #else
10074 #define jffs2_init_security(inode,dir,qstr)	(0)
10075diff -Nupr old/fs/jffs2/xattr_trusted.c new/fs/jffs2/xattr_trusted.c
10076--- old/fs/jffs2/xattr_trusted.c	2022-05-09 17:15:24.360000000 +0800
10077+++ new/fs/jffs2/xattr_trusted.c	1970-01-01 08:00:00.000000000 +0800
10078@@ -1,46 +0,0 @@
10079-/*
10080- * JFFS2 -- Journalling Flash File System, Version 2.
10081- *
10082- * Copyright © 2006  NEC Corporation
10083- *
10084- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
10085- *
10086- * For licensing information, see the file 'LICENCE' in this directory.
10087- *
10088- */
10089-
10090-#include <linux/kernel.h>
10091-#include <linux/fs.h>
10092-#include <linux/jffs2.h>
10093-#include <linux/xattr.h>
10094-#include <linux/mtd/mtd.h>
10095-#include "nodelist.h"
10096-
10097-static int jffs2_trusted_getxattr(const struct xattr_handler *handler,
10098-				  struct dentry *unused, struct inode *inode,
10099-				  const char *name, void *buffer, size_t size)
10100-{
10101-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_TRUSTED,
10102-				 name, buffer, size);
10103-}
10104-
10105-static int jffs2_trusted_setxattr(const struct xattr_handler *handler,
10106-				  struct dentry *unused, struct inode *inode,
10107-				  const char *name, const void *buffer,
10108-				  size_t size, int flags)
10109-{
10110-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_TRUSTED,
10111-				 name, buffer, size, flags);
10112-}
10113-
10114-static bool jffs2_trusted_listxattr(struct dentry *dentry)
10115-{
10116-	return capable(CAP_SYS_ADMIN);
10117-}
10118-
10119-const struct xattr_handler jffs2_trusted_xattr_handler = {
10120-	.prefix = XATTR_TRUSTED_PREFIX,
10121-	.list = jffs2_trusted_listxattr,
10122-	.set = jffs2_trusted_setxattr,
10123-	.get = jffs2_trusted_getxattr
10124-};
10125diff -Nupr old/fs/jffs2/xattr_user.c new/fs/jffs2/xattr_user.c
10126--- old/fs/jffs2/xattr_user.c	2022-05-09 17:15:24.360000000 +0800
10127+++ new/fs/jffs2/xattr_user.c	1970-01-01 08:00:00.000000000 +0800
10128@@ -1,40 +0,0 @@
10129-/*
10130- * JFFS2 -- Journalling Flash File System, Version 2.
10131- *
10132- * Copyright © 2006  NEC Corporation
10133- *
10134- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
10135- *
10136- * For licensing information, see the file 'LICENCE' in this directory.
10137- *
10138- */
10139-
10140-#include <linux/kernel.h>
10141-#include <linux/fs.h>
10142-#include <linux/jffs2.h>
10143-#include <linux/xattr.h>
10144-#include <linux/mtd/mtd.h>
10145-#include "nodelist.h"
10146-
10147-static int jffs2_user_getxattr(const struct xattr_handler *handler,
10148-			       struct dentry *unused, struct inode *inode,
10149-			       const char *name, void *buffer, size_t size)
10150-{
10151-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_USER,
10152-				 name, buffer, size);
10153-}
10154-
10155-static int jffs2_user_setxattr(const struct xattr_handler *handler,
10156-			       struct dentry *unused, struct inode *inode,
10157-			       const char *name, const void *buffer,
10158-			       size_t size, int flags)
10159-{
10160-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_USER,
10161-				 name, buffer, size, flags);
10162-}
10163-
10164-const struct xattr_handler jffs2_user_xattr_handler = {
10165-	.prefix = XATTR_USER_PREFIX,
10166-	.set = jffs2_user_setxattr,
10167-	.get = jffs2_user_getxattr
10168-};
10169