• 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,334 +9,31 @@
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+		return ERR_PTR(ret);
2365+	return gc_buffer;
2366 }
2367
2368-const struct file_operations jffs2_file_operations =
2369-{
2370-	.llseek =	generic_file_llseek,
2371-	.open =		generic_file_open,
2372- 	.read_iter =	generic_file_read_iter,
2373- 	.write_iter =	generic_file_write_iter,
2374-	.unlocked_ioctl=jffs2_ioctl,
2375-	.mmap =		generic_file_readonly_mmap,
2376-	.fsync =	jffs2_fsync,
2377-	.splice_read =	generic_file_splice_read,
2378-	.splice_write = iter_file_splice_write,
2379-};
2380-
2381-/* jffs2_file_inode_operations */
2382-
2383-const struct inode_operations jffs2_file_inode_operations =
2384-{
2385-	.get_acl =	jffs2_get_acl,
2386-	.set_acl =	jffs2_set_acl,
2387-	.setattr =	jffs2_setattr,
2388-	.listxattr =	jffs2_listxattr,
2389-};
2390-
2391-const struct address_space_operations jffs2_file_address_operations =
2392+void jffs2_gc_release_page(struct jffs2_sb_info *c,
2393+			   unsigned char *ptr,
2394+			   unsigned long *priv)
2395 {
2396-	.readpage =	jffs2_readpage,
2397-	.write_begin =	jffs2_write_begin,
2398-	.write_end =	jffs2_write_end,
2399-};
2400-
2401-static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
2402-{
2403-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2404-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2405-	unsigned char *pg_buf;
2406-	int ret;
2407-
2408-	jffs2_dbg(2, "%s(): ino #%lu, page at offset 0x%lx\n",
2409-		  __func__, inode->i_ino, pg->index << PAGE_SHIFT);
2410-
2411-	BUG_ON(!PageLocked(pg));
2412-
2413-	pg_buf = kmap(pg);
2414-	/* FIXME: Can kmap fail? */
2415-
2416-	ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_SHIFT,
2417-				     PAGE_SIZE);
2418-
2419-	if (ret) {
2420-		ClearPageUptodate(pg);
2421-		SetPageError(pg);
2422-	} else {
2423-		SetPageUptodate(pg);
2424-		ClearPageError(pg);
2425-	}
2426-
2427-	flush_dcache_page(pg);
2428-	kunmap(pg);
2429-
2430-	jffs2_dbg(2, "readpage finished\n");
2431-	return ret;
2432-}
2433-
2434-int jffs2_do_readpage_unlock(void *data, struct page *pg)
2435-{
2436-	int ret = jffs2_do_readpage_nolock(data, pg);
2437-	unlock_page(pg);
2438-	return ret;
2439-}
2440-
2441-
2442-static int jffs2_readpage (struct file *filp, struct page *pg)
2443-{
2444-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
2445-	int ret;
2446-
2447-	mutex_lock(&f->sem);
2448-	ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
2449-	mutex_unlock(&f->sem);
2450-	return ret;
2451-}
2452-
2453-static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
2454-			loff_t pos, unsigned len, unsigned flags,
2455-			struct page **pagep, void **fsdata)
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-	int ret = 0;
2463-
2464-	jffs2_dbg(1, "%s()\n", __func__);
2465-
2466-	if (pos > inode->i_size) {
2467-		/* Make new hole frag from old EOF to new position */
2468-		struct jffs2_raw_inode ri;
2469-		struct jffs2_full_dnode *fn;
2470-		uint32_t alloc_len;
2471-
2472-		jffs2_dbg(1, "Writing new hole frag 0x%x-0x%x between current EOF and new position\n",
2473-			  (unsigned int)inode->i_size, (uint32_t)pos);
2474-
2475-		ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
2476-					  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
2477-		if (ret)
2478-			goto out_err;
2479-
2480-		mutex_lock(&f->sem);
2481-		memset(&ri, 0, sizeof(ri));
2482-
2483-		ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
2484-		ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
2485-		ri.totlen = cpu_to_je32(sizeof(ri));
2486-		ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
2487-
2488-		ri.ino = cpu_to_je32(f->inocache->ino);
2489-		ri.version = cpu_to_je32(++f->highest_version);
2490-		ri.mode = cpu_to_jemode(inode->i_mode);
2491-		ri.uid = cpu_to_je16(i_uid_read(inode));
2492-		ri.gid = cpu_to_je16(i_gid_read(inode));
2493-		ri.isize = cpu_to_je32((uint32_t)pos);
2494-		ri.atime = ri.ctime = ri.mtime = cpu_to_je32(JFFS2_NOW());
2495-		ri.offset = cpu_to_je32(inode->i_size);
2496-		ri.dsize = cpu_to_je32((uint32_t)pos - inode->i_size);
2497-		ri.csize = cpu_to_je32(0);
2498-		ri.compr = JFFS2_COMPR_ZERO;
2499-		ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
2500-		ri.data_crc = cpu_to_je32(0);
2501-
2502-		fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
2503-
2504-		if (IS_ERR(fn)) {
2505-			ret = PTR_ERR(fn);
2506-			jffs2_complete_reservation(c);
2507-			mutex_unlock(&f->sem);
2508-			goto out_err;
2509-		}
2510-		ret = jffs2_add_full_dnode_to_inode(c, f, fn);
2511-		if (f->metadata) {
2512-			jffs2_mark_node_obsolete(c, f->metadata->raw);
2513-			jffs2_free_full_dnode(f->metadata);
2514-			f->metadata = NULL;
2515-		}
2516-		if (ret) {
2517-			jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n",
2518-				  ret);
2519-			jffs2_mark_node_obsolete(c, fn->raw);
2520-			jffs2_free_full_dnode(fn);
2521-			jffs2_complete_reservation(c);
2522-			mutex_unlock(&f->sem);
2523-			goto out_err;
2524-		}
2525-		jffs2_complete_reservation(c);
2526-		inode->i_size = pos;
2527-		mutex_unlock(&f->sem);
2528-	}
2529-
2530-	/*
2531-	 * While getting a page and reading data in, lock c->alloc_sem until
2532-	 * the page is Uptodate. Otherwise GC task may attempt to read the same
2533-	 * page in read_cache_page(), which causes a deadlock.
2534-	 */
2535-	mutex_lock(&c->alloc_sem);
2536-	pg = grab_cache_page_write_begin(mapping, index, flags);
2537-	if (!pg) {
2538-		ret = -ENOMEM;
2539-		goto release_sem;
2540-	}
2541-	*pagep = pg;
2542-
2543-	/*
2544-	 * Read in the page if it wasn't already present. Cannot optimize away
2545-	 * the whole page write case until jffs2_write_end can handle the
2546-	 * case of a short-copy.
2547-	 */
2548-	if (!PageUptodate(pg)) {
2549-		mutex_lock(&f->sem);
2550-		ret = jffs2_do_readpage_nolock(inode, pg);
2551-		mutex_unlock(&f->sem);
2552-		if (ret) {
2553-			unlock_page(pg);
2554-			put_page(pg);
2555-			goto release_sem;
2556-		}
2557-	}
2558-	jffs2_dbg(1, "end write_begin(). pg->flags %lx\n", pg->flags);
2559-
2560-release_sem:
2561-	mutex_unlock(&c->alloc_sem);
2562-out_err:
2563-	return ret;
2564-}
2565-
2566-static int jffs2_write_end(struct file *filp, struct address_space *mapping,
2567-			loff_t pos, unsigned len, unsigned copied,
2568-			struct page *pg, void *fsdata)
2569-{
2570-	/* Actually commit the write from the page cache page we're looking at.
2571-	 * For now, we write the full page out each time. It sucks, but it's simple
2572-	 */
2573-	struct inode *inode = mapping->host;
2574-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2575-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2576-	struct jffs2_raw_inode *ri;
2577-	unsigned start = pos & (PAGE_SIZE - 1);
2578-	unsigned end = start + copied;
2579-	unsigned aligned_start = start & ~3;
2580-	int ret = 0;
2581-	uint32_t writtenlen = 0;
2582-
2583-	jffs2_dbg(1, "%s(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
2584-		  __func__, inode->i_ino, pg->index << PAGE_SHIFT,
2585-		  start, end, pg->flags);
2586-
2587-	/* We need to avoid deadlock with page_cache_read() in
2588-	   jffs2_garbage_collect_pass(). So the page must be
2589-	   up to date to prevent page_cache_read() from trying
2590-	   to re-lock it. */
2591-	BUG_ON(!PageUptodate(pg));
2592-
2593-	if (end == PAGE_SIZE) {
2594-		/* When writing out the end of a page, write out the
2595-		   _whole_ page. This helps to reduce the number of
2596-		   nodes in files which have many short writes, like
2597-		   syslog files. */
2598-		aligned_start = 0;
2599-	}
2600-
2601-	ri = jffs2_alloc_raw_inode();
2602-
2603-	if (!ri) {
2604-		jffs2_dbg(1, "%s(): Allocation of raw inode failed\n",
2605-			  __func__);
2606-		unlock_page(pg);
2607-		put_page(pg);
2608-		return -ENOMEM;
2609-	}
2610-
2611-	/* Set the fields that the generic jffs2_write_inode_range() code can't find */
2612-	ri->ino = cpu_to_je32(inode->i_ino);
2613-	ri->mode = cpu_to_jemode(inode->i_mode);
2614-	ri->uid = cpu_to_je16(i_uid_read(inode));
2615-	ri->gid = cpu_to_je16(i_gid_read(inode));
2616-	ri->isize = cpu_to_je32((uint32_t)inode->i_size);
2617-	ri->atime = ri->ctime = ri->mtime = cpu_to_je32(JFFS2_NOW());
2618-
2619-	/* In 2.4, it was already kmapped by generic_file_write(). Doesn't
2620-	   hurt to do it again. The alternative is ifdefs, which are ugly. */
2621-	kmap(pg);
2622-
2623-	ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
2624-				      (pg->index << PAGE_SHIFT) + aligned_start,
2625-				      end - aligned_start, &writtenlen);
2626-
2627-	kunmap(pg);
2628-
2629-	if (ret) {
2630-		/* There was an error writing. */
2631-		SetPageError(pg);
2632-	}
2633-
2634-	/* Adjust writtenlen for the padding we did, so we don't confuse our caller */
2635-	writtenlen -= min(writtenlen, (start - aligned_start));
2636-
2637-	if (writtenlen) {
2638-		if (inode->i_size < pos + writtenlen) {
2639-			inode->i_size = pos + writtenlen;
2640-			inode->i_blocks = (inode->i_size + 511) >> 9;
2641-
2642-			inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
2643-		}
2644-	}
2645-
2646-	jffs2_free_raw_inode(ri);
2647-
2648-	if (start+writtenlen < end) {
2649-		/* generic_file_write has written more to the page cache than we've
2650-		   actually written to the medium. Mark the page !Uptodate so that
2651-		   it gets reread */
2652-		jffs2_dbg(1, "%s(): Not all bytes written. Marking page !uptodate\n",
2653-			__func__);
2654-		SetPageError(pg);
2655-		ClearPageUptodate(pg);
2656-	}
2657-
2658-	jffs2_dbg(1, "%s() returning %d\n",
2659-		  __func__, writtenlen > 0 ? writtenlen : ret);
2660-	unlock_page(pg);
2661-	put_page(pg);
2662-	return writtenlen > 0 ? writtenlen : ret;
2663+	/* Do nothing */
2664 }
2665diff -Nupr old/fs/jffs2/fs.c new/fs/jffs2/fs.c
2666--- old/fs/jffs2/fs.c	2022-05-09 17:22:53.000000000 +0800
2667+++ new/fs/jffs2/fs.c	2022-05-10 16:13:37.830000000 +0800
2668@@ -10,136 +10,129 @@
2669  *
2670  */
2671
2672-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2673-
2674-#include <linux/capability.h>
2675-#include <linux/kernel.h>
2676-#include <linux/sched.h>
2677-#include <linux/cred.h>
2678-#include <linux/fs.h>
2679-#include <linux/fs_context.h>
2680-#include <linux/list.h>
2681-#include <linux/mtd/mtd.h>
2682-#include <linux/pagemap.h>
2683-#include <linux/slab.h>
2684-#include <linux/vmalloc.h>
2685-#include <linux/vfs.h>
2686-#include <linux/crc32.h>
2687+#include <linux/delay.h>
2688 #include "nodelist.h"
2689+#include "os-linux.h"
2690+#include "los_crc32.h"
2691+#include "jffs2_hash.h"
2692+#include "capability_type.h"
2693+#include "capability_api.h"
2694
2695-static int jffs2_flash_setup(struct jffs2_sb_info *c);
2696-
2697-int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
2698+int jffs2_setattr (struct jffs2_inode *inode, struct IATTR *attr)
2699 {
2700 	struct jffs2_full_dnode *old_metadata, *new_metadata;
2701 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2702 	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2703 	struct jffs2_raw_inode *ri;
2704-	union jffs2_device_node dev;
2705-	unsigned char *mdata = NULL;
2706-	int mdatalen = 0;
2707 	unsigned int ivalid;
2708+	mode_t tmp_mode;
2709+	uint c_uid = OsCurrUserGet()->effUserID;
2710+	uint c_gid = OsCurrUserGet()->effGid;
2711 	uint32_t alloclen;
2712 	int ret;
2713 	int alloc_type = ALLOC_NORMAL;
2714
2715 	jffs2_dbg(1, "%s(): ino #%lu\n", __func__, inode->i_ino);
2716-
2717-	/* Special cases - we don't want more than one data node
2718-	   for these types on the medium at any time. So setattr
2719-	   must read the original data associated with the node
2720-	   (i.e. the device numbers or the target name) and write
2721-	   it out again with the appropriate data attached */
2722-	if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
2723-		/* For these, we don't actually need to read the old node */
2724-		mdatalen = jffs2_encode_dev(&dev, inode->i_rdev);
2725-		mdata = (char *)&dev;
2726-		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
2727-			  __func__, mdatalen);
2728-	} else if (S_ISLNK(inode->i_mode)) {
2729-		mutex_lock(&f->sem);
2730-		mdatalen = f->metadata->size;
2731-		mdata = kmalloc(f->metadata->size, GFP_USER);
2732-		if (!mdata) {
2733-			mutex_unlock(&f->sem);
2734-			return -ENOMEM;
2735-		}
2736-		ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
2737-		if (ret) {
2738-			mutex_unlock(&f->sem);
2739-			kfree(mdata);
2740-			return ret;
2741-		}
2742-		mutex_unlock(&f->sem);
2743-		jffs2_dbg(1, "%s(): Writing %d bytes of symlink target\n",
2744-			  __func__, mdatalen);
2745-	}
2746-
2747 	ri = jffs2_alloc_raw_inode();
2748 	if (!ri) {
2749-		if (S_ISLNK(inode->i_mode))
2750-			kfree(mdata);
2751 		return -ENOMEM;
2752 	}
2753
2754-	ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &alloclen,
2755-				  ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
2756+	ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
2757+
2758 	if (ret) {
2759 		jffs2_free_raw_inode(ri);
2760-		if (S_ISLNK(inode->i_mode))
2761-			 kfree(mdata);
2762 		return ret;
2763 	}
2764 	mutex_lock(&f->sem);
2765-	ivalid = iattr->ia_valid;
2766+	ivalid = attr->attr_chg_valid;
2767+	tmp_mode = inode->i_mode;
2768
2769 	ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
2770 	ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
2771-	ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
2772-	ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
2773+	ri->totlen = cpu_to_je32(sizeof(*ri));
2774+	ri->hdr_crc = cpu_to_je32(crc32(0, ri, (sizeof(struct jffs2_unknown_node)-4)));
2775
2776 	ri->ino = cpu_to_je32(inode->i_ino);
2777 	ri->version = cpu_to_je32(++f->highest_version);
2778+	ri->uid = cpu_to_je16(inode->i_uid);
2779+	ri->gid = cpu_to_je16(inode->i_gid);
2780
2781-	ri->uid = cpu_to_je16((ivalid & ATTR_UID)?
2782-		from_kuid(&init_user_ns, iattr->ia_uid):i_uid_read(inode));
2783-	ri->gid = cpu_to_je16((ivalid & ATTR_GID)?
2784-		from_kgid(&init_user_ns, iattr->ia_gid):i_gid_read(inode));
2785+	if (ivalid & CHG_UID) {
2786+		if (((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) && (!IsCapPermit(CAP_CHOWN))) {
2787+			jffs2_complete_reservation(c);
2788+			jffs2_free_raw_inode(ri);
2789+			mutex_unlock(&f->sem);
2790+			return -EPERM;
2791+		} else {
2792+			ri->uid = cpu_to_je16(attr->attr_chg_uid);
2793+		}
2794+	}
2795+
2796+	if (ivalid & CHG_GID) {
2797+		if (((c_gid != inode->i_gid) || (attr->attr_chg_gid != inode->i_gid)) && (!IsCapPermit(CAP_CHOWN))) {
2798+			jffs2_complete_reservation(c);
2799+			jffs2_free_raw_inode(ri);
2800+			mutex_unlock(&f->sem);
2801+			return -EPERM;
2802+		} else {
2803+			ri->gid = cpu_to_je16(attr->attr_chg_gid);
2804+		}
2805+	}
2806+
2807+	if (ivalid & CHG_MODE) {
2808+		if (!IsCapPermit(CAP_FOWNER) && (c_uid != inode->i_uid)) {
2809+			jffs2_complete_reservation(c);
2810+			jffs2_free_raw_inode(ri);
2811+			mutex_unlock(&f->sem);
2812+			return -EPERM;
2813+		} else {
2814+			attr->attr_chg_mode  &= ~S_IFMT; // delete file type
2815+			tmp_mode &= S_IFMT;
2816+			tmp_mode = attr->attr_chg_mode | tmp_mode; // add old file type
2817+		}
2818+	}
2819
2820-	if (ivalid & ATTR_MODE)
2821-		ri->mode = cpu_to_jemode(iattr->ia_mode);
2822-	else
2823-		ri->mode = cpu_to_jemode(inode->i_mode);
2824+	if (ivalid & CHG_ATIME) {
2825+		if ((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) {
2826+			return -EPERM;
2827+		} else {
2828+			ri->atime = cpu_to_je32(attr->attr_chg_atime);
2829+		}
2830+	} else {
2831+		ri->atime = cpu_to_je32(inode->i_atime);
2832+	}
2833
2834+	if (ivalid & CHG_MTIME) {
2835+		if ((c_uid != inode->i_uid) || (attr->attr_chg_uid != inode->i_uid)) {
2836+			return -EPERM;
2837+		} else {
2838+			ri->mtime = cpu_to_je32(attr->attr_chg_mtime);
2839+		}
2840+	} else {
2841+		ri->mtime = cpu_to_je32(Jffs2CurSec());
2842+	}
2843+	ri->mode = cpu_to_jemode(tmp_mode);
2844
2845-	ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
2846-	ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
2847-	ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
2848-	ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
2849+	ri->isize = cpu_to_je32((ivalid & CHG_SIZE) ? attr->attr_chg_size : inode->i_size);
2850+	ri->ctime = cpu_to_je32(Jffs2CurSec());
2851
2852 	ri->offset = cpu_to_je32(0);
2853-	ri->csize = ri->dsize = cpu_to_je32(mdatalen);
2854+	ri->csize = ri->dsize = cpu_to_je32(0);
2855 	ri->compr = JFFS2_COMPR_NONE;
2856-	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
2857+	if (ivalid & CHG_SIZE && inode->i_size < attr->attr_chg_size) {
2858 		/* It's an extension. Make it a hole node */
2859 		ri->compr = JFFS2_COMPR_ZERO;
2860-		ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
2861+		ri->dsize = cpu_to_je32(attr->attr_chg_size - inode->i_size);
2862 		ri->offset = cpu_to_je32(inode->i_size);
2863-	} else if (ivalid & ATTR_SIZE && !iattr->ia_size) {
2864+	} else if (ivalid & CHG_SIZE && !attr->attr_chg_size) {
2865 		/* For truncate-to-zero, treat it as deletion because
2866 		   it'll always be obsoleting all previous nodes */
2867 		alloc_type = ALLOC_DELETION;
2868 	}
2869-	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
2870-	if (mdatalen)
2871-		ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
2872-	else
2873-		ri->data_crc = cpu_to_je32(0);
2874-
2875-	new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, alloc_type);
2876-	if (S_ISLNK(inode->i_mode))
2877-		kfree(mdata);
2878-
2879+	ri->node_crc = cpu_to_je32(crc32(0, ri, (sizeof(*ri)-8)));
2880+	ri->data_crc = cpu_to_je32(0);
2881+	new_metadata = jffs2_write_dnode(c, f, ri, NULL, 0, alloc_type);
2882 	if (IS_ERR(new_metadata)) {
2883 		jffs2_complete_reservation(c);
2884 		jffs2_free_raw_inode(ri);
2885@@ -147,23 +140,20 @@ int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
2886 		return PTR_ERR(new_metadata);
2887 	}
2888 	/* It worked. Update the inode */
2889-	inode->i_atime = ITIME(je32_to_cpu(ri->atime));
2890-	inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
2891-	inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
2892+	inode->i_atime = je32_to_cpu(ri->atime);
2893+	inode->i_ctime = je32_to_cpu(ri->ctime);
2894+	inode->i_mtime = je32_to_cpu(ri->mtime);
2895 	inode->i_mode = jemode_to_cpu(ri->mode);
2896-	i_uid_write(inode, je16_to_cpu(ri->uid));
2897-	i_gid_write(inode, je16_to_cpu(ri->gid));
2898-
2899+	inode->i_uid = je16_to_cpu(ri->uid);
2900+	inode->i_gid = je16_to_cpu(ri->gid);
2901
2902 	old_metadata = f->metadata;
2903+	if (ivalid & CHG_SIZE && inode->i_size > attr->attr_chg_size)
2904+		jffs2_truncate_fragtree (c, &f->fragtree, attr->attr_chg_size);
2905
2906-	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
2907-		jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
2908-
2909-	if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
2910+	if (ivalid & CHG_SIZE && inode->i_size < attr->attr_chg_size) {
2911 		jffs2_add_full_dnode_to_inode(c, f, new_metadata);
2912-		inode->i_size = iattr->ia_size;
2913-		inode->i_blocks = (inode->i_size + 511) >> 9;
2914+		inode->i_size = attr->attr_chg_size;
2915 		f->metadata = NULL;
2916 	} else {
2917 		f->metadata = new_metadata;
2918@@ -182,315 +172,201 @@ int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
2919 	   We are protected from a simultaneous write() extending i_size
2920 	   back past iattr->ia_size, because do_truncate() holds the
2921 	   generic inode semaphore. */
2922-	if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
2923-		truncate_setsize(inode, iattr->ia_size);
2924-		inode->i_blocks = (inode->i_size + 511) >> 9;
2925+	if (ivalid & CHG_SIZE && inode->i_size > attr->attr_chg_size) {
2926+		inode->i_size = attr->attr_chg_size; // truncate_setsize
2927 	}
2928
2929 	return 0;
2930 }
2931
2932-int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
2933+static void jffs2_clear_inode (struct jffs2_inode *inode)
2934 {
2935-	struct inode *inode = d_inode(dentry);
2936-	int rc;
2937+	/* We can forget about this inode for now - drop all
2938+	 *  the nodelists associated with it, etc.
2939+	 */
2940+	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
2941+	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
2942+
2943+	jffs2_do_clear_inode(c, f);
2944+}
2945
2946-	rc = setattr_prepare(dentry, iattr);
2947-	if (rc)
2948-		return rc;
2949+static struct jffs2_inode *ilookup(struct super_block *sb, uint32_t ino)
2950+{
2951+	struct jffs2_inode *node = NULL;
2952
2953-	rc = jffs2_do_setattr(inode, iattr);
2954-	if (!rc && (iattr->ia_valid & ATTR_MODE))
2955-		rc = posix_acl_chmod(inode, inode->i_mode);
2956+	if (sb->s_root == NULL) {
2957+		return NULL;
2958+	}
2959
2960-	return rc;
2961+	// Check for this inode in the cache
2962+	Jffs2NodeLock();
2963+	(void)Jffs2HashGet(&sb->s_node_hash_lock, &sb->s_node_hash[0], sb, ino, &node);
2964+	Jffs2NodeUnlock();
2965+	return node;
2966 }
2967
2968-int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
2969+struct jffs2_inode *new_inode(struct super_block *sb)
2970 {
2971-	struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
2972-	unsigned long avail;
2973-
2974-	buf->f_type = JFFS2_SUPER_MAGIC;
2975-	buf->f_bsize = 1 << PAGE_SHIFT;
2976-	buf->f_blocks = c->flash_size >> PAGE_SHIFT;
2977-	buf->f_files = 0;
2978-	buf->f_ffree = 0;
2979-	buf->f_namelen = JFFS2_MAX_NAME_LEN;
2980-	buf->f_fsid.val[0] = JFFS2_SUPER_MAGIC;
2981-	buf->f_fsid.val[1] = c->mtd->index;
2982-
2983-	spin_lock(&c->erase_completion_lock);
2984-	avail = c->dirty_size + c->free_size;
2985-	if (avail > c->sector_size * c->resv_blocks_write)
2986-		avail -= c->sector_size * c->resv_blocks_write;
2987-	else
2988-		avail = 0;
2989-	spin_unlock(&c->erase_completion_lock);
2990-
2991-	buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
2992+	struct jffs2_inode *inode = NULL;
2993
2994-	return 0;
2995-}
2996+	inode = zalloc(sizeof (struct jffs2_inode));
2997+	if (inode == NULL)
2998+		return 0;
2999
3000+	D2(PRINTK("malloc new_inode %x ####################################\n",
3001+		inode));
3002
3003-void jffs2_evict_inode (struct inode *inode)
3004-{
3005-	/* We can forget about this inode for now - drop all
3006-	 *  the nodelists associated with it, etc.
3007-	 */
3008-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
3009-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
3010+	inode->i_sb = sb;
3011+	inode->i_ino = 1;
3012+	inode->i_nlink = 1;    // Let JFFS2 manage the link count
3013+	inode->i_size = 0;
3014+	LOS_ListInit((&(inode->i_hashlist)));
3015
3016-	jffs2_dbg(1, "%s(): ino #%lu mode %o\n",
3017-		  __func__, inode->i_ino, inode->i_mode);
3018-	truncate_inode_pages_final(&inode->i_data);
3019-	clear_inode(inode);
3020-	jffs2_do_clear_inode(c, f);
3021+	return inode;
3022 }
3023
3024-struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
3025+struct jffs2_inode *jffs2_iget(struct super_block *sb, uint32_t ino)
3026 {
3027 	struct jffs2_inode_info *f;
3028 	struct jffs2_sb_info *c;
3029 	struct jffs2_raw_inode latest_node;
3030-	union jffs2_device_node jdev;
3031-	struct inode *inode;
3032-	dev_t rdev = 0;
3033+	struct jffs2_inode *inode;
3034 	int ret;
3035
3036-	jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
3037-
3038-	inode = iget_locked(sb, ino);
3039-	if (!inode)
3040-		return ERR_PTR(-ENOMEM);
3041-	if (!(inode->i_state & I_NEW))
3042+	Jffs2NodeLock();
3043+	inode = ilookup(sb, ino);
3044+	if (inode) {
3045+		Jffs2NodeUnlock();
3046 		return inode;
3047+	}
3048+	inode = new_inode(sb);
3049+	if (inode == NULL) {
3050+		Jffs2NodeUnlock();
3051+		return (struct jffs2_inode *)-ENOMEM;
3052+	}
3053
3054+	inode->i_ino = ino;
3055 	f = JFFS2_INODE_INFO(inode);
3056 	c = JFFS2_SB_INFO(inode->i_sb);
3057
3058-	jffs2_init_inode_info(f);
3059-	mutex_lock(&f->sem);
3060+	(void)mutex_init(&f->sem);
3061+	(void)mutex_lock(&f->sem);
3062
3063 	ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
3064-	if (ret)
3065-		goto error;
3066+	if (ret) {
3067+		(void)mutex_unlock(&f->sem);
3068+        inode->i_nlink = 0;
3069+        free(inode);
3070+		Jffs2NodeUnlock();
3071+		return (struct jffs2_inode *)ret;
3072+	}
3073
3074 	inode->i_mode = jemode_to_cpu(latest_node.mode);
3075-	i_uid_write(inode, je16_to_cpu(latest_node.uid));
3076-	i_gid_write(inode, je16_to_cpu(latest_node.gid));
3077+	inode->i_uid = je16_to_cpu(latest_node.uid);
3078+	inode->i_gid = je16_to_cpu(latest_node.gid);
3079 	inode->i_size = je32_to_cpu(latest_node.isize);
3080-	inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
3081-	inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
3082-	inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
3083-
3084-	set_nlink(inode, f->inocache->pino_nlink);
3085-
3086-	inode->i_blocks = (inode->i_size + 511) >> 9;
3087+	inode->i_atime = je32_to_cpu(latest_node.atime);
3088+	inode->i_mtime = je32_to_cpu(latest_node.mtime);
3089+	inode->i_ctime = je32_to_cpu(latest_node.ctime);
3090+	inode->i_nlink = f->inocache->pino_nlink;
3091
3092-	switch (inode->i_mode & S_IFMT) {
3093+	(void)mutex_unlock(&f->sem);
3094
3095-	case S_IFLNK:
3096-		inode->i_op = &jffs2_symlink_inode_operations;
3097-		inode->i_link = f->target;
3098-		break;
3099-
3100-	case S_IFDIR:
3101-	{
3102-		struct jffs2_full_dirent *fd;
3103-		set_nlink(inode, 2); /* parent and '.' */
3104-
3105-		for (fd=f->dents; fd; fd = fd->next) {
3106-			if (fd->type == DT_DIR && fd->ino)
3107-				inc_nlink(inode);
3108-		}
3109-		/* Root dir gets i_nlink 3 for some reason */
3110-		if (inode->i_ino == 1)
3111-			inc_nlink(inode);
3112-
3113-		inode->i_op = &jffs2_dir_inode_operations;
3114-		inode->i_fop = &jffs2_dir_operations;
3115-		break;
3116-	}
3117-	case S_IFREG:
3118-		inode->i_op = &jffs2_file_inode_operations;
3119-		inode->i_fop = &jffs2_file_operations;
3120-		inode->i_mapping->a_ops = &jffs2_file_address_operations;
3121-		inode->i_mapping->nrpages = 0;
3122-		break;
3123-
3124-	case S_IFBLK:
3125-	case S_IFCHR:
3126-		/* Read the device numbers from the media */
3127-		if (f->metadata->size != sizeof(jdev.old_id) &&
3128-		    f->metadata->size != sizeof(jdev.new_id)) {
3129-			pr_notice("Device node has strange size %d\n",
3130-				  f->metadata->size);
3131-			goto error_io;
3132-		}
3133-		jffs2_dbg(1, "Reading device numbers from flash\n");
3134-		ret = jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size);
3135-		if (ret < 0) {
3136-			/* Eep */
3137-			pr_notice("Read device numbers for inode %lu failed\n",
3138-				  (unsigned long)inode->i_ino);
3139-			goto error;
3140-		}
3141-		if (f->metadata->size == sizeof(jdev.old_id))
3142-			rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
3143-		else
3144-			rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
3145-		fallthrough;
3146-
3147-	case S_IFSOCK:
3148-	case S_IFIFO:
3149-		inode->i_op = &jffs2_file_inode_operations;
3150-		init_special_inode(inode, inode->i_mode, rdev);
3151-		break;
3152-
3153-	default:
3154-		pr_warn("%s(): Bogus i_mode %o for ino %lu\n",
3155-			__func__, inode->i_mode, (unsigned long)inode->i_ino);
3156-	}
3157-
3158-	mutex_unlock(&f->sem);
3159+	(void)Jffs2HashInsert(&sb->s_node_hash_lock, &sb->s_node_hash[0], inode, ino);
3160
3161 	jffs2_dbg(1, "jffs2_read_inode() returning\n");
3162-	unlock_new_inode(inode);
3163-	return inode;
3164+	Jffs2NodeUnlock();
3165
3166-error_io:
3167-	ret = -EIO;
3168-error:
3169-	mutex_unlock(&f->sem);
3170-	iget_failed(inode);
3171-	return ERR_PTR(ret);
3172+	return inode;
3173 }
3174
3175-void jffs2_dirty_inode(struct inode *inode, int flags)
3176-{
3177-	struct iattr iattr;
3178
3179-	if (!(inode->i_state & I_DIRTY_DATASYNC)) {
3180-		jffs2_dbg(2, "%s(): not calling setattr() for ino #%lu\n",
3181-			  __func__, inode->i_ino);
3182-		return;
3183-	}
3184-
3185-	jffs2_dbg(1, "%s(): calling setattr() for ino #%lu\n",
3186-		  __func__, inode->i_ino);
3187-
3188-	iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
3189-	iattr.ia_mode = inode->i_mode;
3190-	iattr.ia_uid = inode->i_uid;
3191-	iattr.ia_gid = inode->i_gid;
3192-	iattr.ia_atime = inode->i_atime;
3193-	iattr.ia_mtime = inode->i_mtime;
3194-	iattr.ia_ctime = inode->i_ctime;
3195-
3196-	jffs2_do_setattr(inode, &iattr);
3197-}
3198+// -------------------------------------------------------------------------
3199+// Decrement the reference count on an inode. If this makes the ref count
3200+// zero, then this inode can be freed.
3201
3202-int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc)
3203+int jffs2_iput(struct jffs2_inode *i)
3204 {
3205-	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
3206-
3207-	if (c->flags & JFFS2_SB_FLAG_RO && !sb_rdonly(sb))
3208-		return -EROFS;
3209-
3210-	/* We stop if it was running, then restart if it needs to.
3211-	   This also catches the case where it was stopped and this
3212-	   is just a remount to restart it.
3213-	   Flush the writebuffer, if neccecary, else we loose it */
3214-	if (!sb_rdonly(sb)) {
3215-		jffs2_stop_garbage_collect_thread(c);
3216-		mutex_lock(&c->alloc_sem);
3217-		jffs2_flush_wbuf_pad(c);
3218-		mutex_unlock(&c->alloc_sem);
3219-	}
3220-
3221-	if (!(fc->sb_flags & SB_RDONLY))
3222-		jffs2_start_garbage_collect_thread(c);
3223+	// Called in jffs2_find
3224+	// (and jffs2_open and jffs2_ops_mkdir?)
3225+	// super.c jffs2_fill_super,
3226+	// and gc.c jffs2_garbage_collect_pass
3227+	struct jffs2_inode_info *f = NULL;
3228+
3229+	Jffs2NodeLock();
3230+	if (!i || i->i_nlink) {
3231+		// and let it fault...
3232+		Jffs2NodeUnlock();
3233+		return -EBUSY;
3234+	}
3235+
3236+	jffs2_clear_inode(i);
3237+	f = JFFS2_INODE_INFO(i);
3238+	(void)mutex_destroy(&(f->sem));
3239+	(void)Jffs2HashRemove(&i->i_sb->s_node_hash_lock, i);
3240+	(void)memset_s(i, sizeof(*i), 0x5a, sizeof(*i));
3241+	free(i);
3242+	Jffs2NodeUnlock();
3243
3244-	fc->sb_flags |= SB_NOATIME;
3245 	return 0;
3246 }
3247
3248+
3249 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
3250    fill in the raw_inode while you're at it. */
3251-struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode, struct jffs2_raw_inode *ri)
3252+struct jffs2_inode *jffs2_new_inode (struct jffs2_inode *dir_i, int mode, struct jffs2_raw_inode *ri)
3253 {
3254-	struct inode *inode;
3255+	struct jffs2_inode *inode;
3256 	struct super_block *sb = dir_i->i_sb;
3257 	struct jffs2_sb_info *c;
3258 	struct jffs2_inode_info *f;
3259 	int ret;
3260
3261-	jffs2_dbg(1, "%s(): dir_i %ld, mode 0x%x\n",
3262-		  __func__, dir_i->i_ino, mode);
3263-
3264 	c = JFFS2_SB_INFO(sb);
3265
3266+	Jffs2NodeLock();
3267 	inode = new_inode(sb);
3268
3269 	if (!inode)
3270-		return ERR_PTR(-ENOMEM);
3271+		return (struct jffs2_inode *)-ENOMEM;
3272
3273 	f = JFFS2_INODE_INFO(inode);
3274-	jffs2_init_inode_info(f);
3275-	mutex_lock(&f->sem);
3276+	(void)mutex_init(&f->sem);
3277+	(void)mutex_lock(&f->sem);;
3278
3279 	memset(ri, 0, sizeof(*ri));
3280 	/* Set OS-specific defaults for new inodes */
3281-	ri->uid = cpu_to_je16(from_kuid(&init_user_ns, current_fsuid()));
3282+	ri->uid = cpu_to_je16(OsCurrUserGet()->effUserID);
3283+	ri->gid = cpu_to_je16(OsCurrUserGet()->effGid);
3284
3285-	if (dir_i->i_mode & S_ISGID) {
3286-		ri->gid = cpu_to_je16(i_gid_read(dir_i));
3287-		if (S_ISDIR(mode))
3288-			mode |= S_ISGID;
3289-	} else {
3290-		ri->gid = cpu_to_je16(from_kgid(&init_user_ns, current_fsgid()));
3291-	}
3292-
3293-	/* POSIX ACLs have to be processed now, at least partly.
3294-	   The umask is only applied if there's no default ACL */
3295-	ret = jffs2_init_acl_pre(dir_i, inode, &mode);
3296-	if (ret) {
3297-		mutex_unlock(&f->sem);
3298-		make_bad_inode(inode);
3299-		iput(inode);
3300-		return ERR_PTR(ret);
3301-	}
3302 	ret = jffs2_do_new_inode (c, f, mode, ri);
3303 	if (ret) {
3304-		mutex_unlock(&f->sem);
3305-		make_bad_inode(inode);
3306-		iput(inode);
3307-		return ERR_PTR(ret);
3308+		mutex_unlock(&(f->sem));
3309+		jffs2_clear_inode(inode);
3310+		(void)mutex_destroy(&(f->sem));
3311+		(void)memset_s(inode, sizeof(*inode), 0x6a, sizeof(*inode));
3312+		free(inode);
3313+		Jffs2NodeUnlock();
3314+		return (struct jffs2_inode *)ret;
3315+
3316 	}
3317-	set_nlink(inode, 1);
3318+	inode->i_nlink = 1;
3319 	inode->i_ino = je32_to_cpu(ri->ino);
3320 	inode->i_mode = jemode_to_cpu(ri->mode);
3321-	i_gid_write(inode, je16_to_cpu(ri->gid));
3322-	i_uid_write(inode, je16_to_cpu(ri->uid));
3323-	inode->i_atime = inode->i_ctime = inode->i_mtime = current_time(inode);
3324-	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
3325+	inode->i_gid = je16_to_cpu(ri->gid);
3326+	inode->i_uid = je16_to_cpu(ri->uid);
3327+	inode->i_atime = inode->i_ctime = inode->i_mtime = Jffs2CurSec();
3328+	ri->atime = ri->mtime = ri->ctime = cpu_to_je32(inode->i_mtime);
3329
3330-	inode->i_blocks = 0;
3331 	inode->i_size = 0;
3332
3333-	if (insert_inode_locked(inode) < 0) {
3334-		mutex_unlock(&f->sem);
3335-		make_bad_inode(inode);
3336-		iput(inode);
3337-		return ERR_PTR(-EINVAL);
3338-	}
3339+	(void)Jffs2HashInsert(&sb->s_node_hash_lock, &sb->s_node_hash[0], inode, inode->i_ino);
3340+	Jffs2NodeUnlock();
3341
3342 	return inode;
3343 }
3344
3345-static int calculate_inocache_hashsize(uint32_t flash_size)
3346+int calculate_inocache_hashsize(uint32_t flash_size)
3347 {
3348 	/*
3349 	 * Pick a inocache hash size based on the size of the medium.
3350@@ -510,118 +386,17 @@ static int calculate_inocache_hashsize(uint32_t flash_size)
3351 	return hashsize;
3352 }
3353
3354-int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc)
3355-{
3356-	struct jffs2_sb_info *c;
3357-	struct inode *root_i;
3358-	int ret;
3359-	size_t blocks;
3360-
3361-	c = JFFS2_SB_INFO(sb);
3362-
3363-	/* Do not support the MLC nand */
3364-	if (c->mtd->type == MTD_MLCNANDFLASH)
3365-		return -EINVAL;
3366-
3367-#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
3368-	if (c->mtd->type == MTD_NANDFLASH) {
3369-		errorf(fc, "Cannot operate on NAND flash unless jffs2 NAND support is compiled in");
3370-		return -EINVAL;
3371-	}
3372-	if (c->mtd->type == MTD_DATAFLASH) {
3373-		errorf(fc, "Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in");
3374-		return -EINVAL;
3375-	}
3376-#endif
3377-
3378-	c->flash_size = c->mtd->size;
3379-	c->sector_size = c->mtd->erasesize;
3380-	blocks = c->flash_size / c->sector_size;
3381-
3382-	/*
3383-	 * Size alignment check
3384-	 */
3385-	if ((c->sector_size * blocks) != c->flash_size) {
3386-		c->flash_size = c->sector_size * blocks;
3387-		infof(fc, "Flash size not aligned to erasesize, reducing to %dKiB",
3388-		      c->flash_size / 1024);
3389-	}
3390-
3391-	if (c->flash_size < 5*c->sector_size) {
3392-		errorf(fc, "Too few erase blocks (%d)",
3393-		       c->flash_size / c->sector_size);
3394-		return -EINVAL;
3395-	}
3396-
3397-	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
3398-
3399-	/* NAND (or other bizarre) flash... do setup accordingly */
3400-	ret = jffs2_flash_setup(c);
3401-	if (ret)
3402-		return ret;
3403-
3404-	c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
3405-	c->inocache_list = kcalloc(c->inocache_hashsize, sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
3406-	if (!c->inocache_list) {
3407-		ret = -ENOMEM;
3408-		goto out_wbuf;
3409-	}
3410-
3411-	jffs2_init_xattr_subsystem(c);
3412-
3413-	if ((ret = jffs2_do_mount_fs(c)))
3414-		goto out_inohash;
3415-
3416-	jffs2_dbg(1, "%s(): Getting root inode\n", __func__);
3417-	root_i = jffs2_iget(sb, 1);
3418-	if (IS_ERR(root_i)) {
3419-		jffs2_dbg(1, "get root inode failed\n");
3420-		ret = PTR_ERR(root_i);
3421-		goto out_root;
3422-	}
3423-
3424-	ret = -ENOMEM;
3425-
3426-	jffs2_dbg(1, "%s(): d_make_root()\n", __func__);
3427-	sb->s_root = d_make_root(root_i);
3428-	if (!sb->s_root)
3429-		goto out_root;
3430-
3431-	sb->s_maxbytes = 0xFFFFFFFF;
3432-	sb->s_blocksize = PAGE_SIZE;
3433-	sb->s_blocksize_bits = PAGE_SHIFT;
3434-	sb->s_magic = JFFS2_SUPER_MAGIC;
3435-	sb->s_time_min = 0;
3436-	sb->s_time_max = U32_MAX;
3437-
3438-	if (!sb_rdonly(sb))
3439-		jffs2_start_garbage_collect_thread(c);
3440-	return 0;
3441-
3442-out_root:
3443-	jffs2_free_ino_caches(c);
3444-	jffs2_free_raw_node_refs(c);
3445-	kvfree(c->blocks);
3446-	jffs2_clear_xattr_subsystem(c);
3447-	jffs2_sum_exit(c);
3448- out_inohash:
3449-	kfree(c->inocache_list);
3450- out_wbuf:
3451-	jffs2_flash_cleanup(c);
3452-
3453-	return ret;
3454-}
3455-
3456 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
3457 				   struct jffs2_inode_info *f)
3458 {
3459-	iput(OFNI_EDONI_2SFFJ(f));
3460+	struct jffs2_inode *node = OFNI_EDONI_2SFFJ(f);
3461+	jffs2_iput(node);
3462 }
3463
3464 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
3465 					      int inum, int unlinked)
3466 {
3467-	struct inode *inode;
3468+	struct jffs2_inode *inode;
3469 	struct jffs2_inode_cache *ic;
3470
3471 	if (unlinked) {
3472@@ -669,72 +444,9 @@ struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
3473 		   Just iget() it, and if read_inode() is necessary that's OK.
3474 		*/
3475 		inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
3476-		if (IS_ERR(inode))
3477-			return ERR_CAST(inode);
3478-	}
3479-	if (is_bad_inode(inode)) {
3480-		pr_notice("Eep. read_inode() failed for ino #%u. unlinked %d\n",
3481-			  inum, unlinked);
3482-		/* NB. This will happen again. We need to do something appropriate here. */
3483-		iput(inode);
3484-		return ERR_PTR(-EIO);
3485+		if (inode <= 0)
3486+			return (struct jffs2_inode_info *)inode;
3487 	}
3488
3489 	return JFFS2_INODE_INFO(inode);
3490 }
3491-
3492-static int jffs2_flash_setup(struct jffs2_sb_info *c) {
3493-	int ret = 0;
3494-
3495-	if (jffs2_cleanmarker_oob(c)) {
3496-		/* NAND flash... do setup accordingly */
3497-		ret = jffs2_nand_flash_setup(c);
3498-		if (ret)
3499-			return ret;
3500-	}
3501-
3502-	/* and Dataflash */
3503-	if (jffs2_dataflash(c)) {
3504-		ret = jffs2_dataflash_setup(c);
3505-		if (ret)
3506-			return ret;
3507-	}
3508-
3509-	/* and Intel "Sibley" flash */
3510-	if (jffs2_nor_wbuf_flash(c)) {
3511-		ret = jffs2_nor_wbuf_flash_setup(c);
3512-		if (ret)
3513-			return ret;
3514-	}
3515-
3516-	/* and an UBI volume */
3517-	if (jffs2_ubivol(c)) {
3518-		ret = jffs2_ubivol_setup(c);
3519-		if (ret)
3520-			return ret;
3521-	}
3522-
3523-	return ret;
3524-}
3525-
3526-void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
3527-
3528-	if (jffs2_cleanmarker_oob(c)) {
3529-		jffs2_nand_flash_cleanup(c);
3530-	}
3531-
3532-	/* and DataFlash */
3533-	if (jffs2_dataflash(c)) {
3534-		jffs2_dataflash_cleanup(c);
3535-	}
3536-
3537-	/* and Intel "Sibley" flash */
3538-	if (jffs2_nor_wbuf_flash(c)) {
3539-		jffs2_nor_wbuf_flash_cleanup(c);
3540-	}
3541-
3542-	/* and an UBI volume */
3543-	if (jffs2_ubivol(c)) {
3544-		jffs2_ubivol_cleanup(c);
3545-	}
3546-}
3547diff -Nupr old/fs/jffs2/gc.c new/fs/jffs2/gc.c
3548--- old/fs/jffs2/gc.c	2022-05-09 17:22:53.000000000 +0800
3549+++ new/fs/jffs2/gc.c	2022-05-10 16:11:42.090000000 +0800
3550@@ -10,17 +10,17 @@
3551  *
3552  */
3553
3554-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3555-
3556 #include <linux/kernel.h>
3557-#include <linux/mtd/mtd.h>
3558 #include <linux/slab.h>
3559 #include <linux/pagemap.h>
3560-#include <linux/crc32.h>
3561+#include <linux/delay.h>
3562+#include <linux/semaphore.h>
3563 #include <linux/compiler.h>
3564 #include <linux/stat.h>
3565+#include "mtd_dev.h"
3566 #include "nodelist.h"
3567 #include "compr.h"
3568+#include "los_crc32.h"
3569
3570 static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
3571 					  struct jffs2_inode_cache *ic,
3572@@ -43,7 +43,7 @@ static int jffs2_garbage_collect_live(st
3573 /* Called with erase_completion_lock held */
3574 static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
3575 {
3576-	struct jffs2_eraseblock *ret;
3577+	struct jffs2_eraseblock *ret = NULL;
3578 	struct list_head *nextlist = NULL;
3579 	int n = jiffies % 128;
3580
3581@@ -131,62 +131,40 @@ int jffs2_garbage_collect_pass(struct jf
3582 	int ret = 0, inum, nlink;
3583 	int xattr = 0;
3584
3585-	if (mutex_lock_interruptible(&c->alloc_sem))
3586+	if (mutex_lock(&c->alloc_sem))
3587 		return -EINTR;
3588
3589-
3590 	for (;;) {
3591-		/* We can't start doing GC until we've finished checking
3592-		   the node CRCs etc. */
3593-		int bucket, want_ino;
3594-
3595 		spin_lock(&c->erase_completion_lock);
3596 		if (!c->unchecked_size)
3597 			break;
3598+
3599+		/* We can't start doing GC yet. We haven't finished checking
3600+		   the node CRCs etc. Do it now. */
3601+
3602+		/* checked_ino is protected by the alloc_sem */
3603+		if (c->checked_ino > c->highest_ino && xattr) {
3604+			pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
3605+				c->unchecked_size);
3606+			jffs2_dbg_dump_block_lists_nolock(c);
3607+			spin_unlock(&c->erase_completion_lock);
3608+			mutex_unlock(&c->alloc_sem);
3609+			return -ENOSPC;
3610+		}
3611+
3612 		spin_unlock(&c->erase_completion_lock);
3613
3614 		if (!xattr)
3615 			xattr = jffs2_verify_xattr(c);
3616
3617 		spin_lock(&c->inocache_lock);
3618-		/* Instead of doing the inodes in numeric order, doing a lookup
3619-		 * in the hash for each possible number, just walk the hash
3620-		 * buckets of *existing* inodes. This means that we process
3621-		 * them out-of-order, but it can be a lot faster if there's
3622-		 * a sparse inode# space. Which there often is. */
3623-		want_ino = c->check_ino;
3624-		for (bucket = c->check_ino % c->inocache_hashsize ; bucket < c->inocache_hashsize; bucket++) {
3625-			for (ic = c->inocache_list[bucket]; ic; ic = ic->next) {
3626-				if (ic->ino < want_ino)
3627-					continue;
3628-
3629-				if (ic->state != INO_STATE_CHECKEDABSENT &&
3630-				    ic->state != INO_STATE_PRESENT)
3631-					goto got_next; /* with inocache_lock held */
3632
3633-				jffs2_dbg(1, "Skipping ino #%u already checked\n",
3634-					  ic->ino);
3635-			}
3636-			want_ino = 0;
3637-		}
3638-
3639-		/* Point c->check_ino past the end of the last bucket. */
3640-		c->check_ino = ((c->highest_ino + c->inocache_hashsize + 1) &
3641-				~c->inocache_hashsize) - 1;
3642-
3643-		spin_unlock(&c->inocache_lock);
3644-
3645-		pr_crit("Checked all inodes but still 0x%x bytes of unchecked space?\n",
3646-			c->unchecked_size);
3647-		jffs2_dbg_dump_block_lists_nolock(c);
3648-		mutex_unlock(&c->alloc_sem);
3649-		return -ENOSPC;
3650+		ic = jffs2_get_ino_cache(c, c->checked_ino++);
3651
3652-	got_next:
3653-		/* For next time round the loop, we want c->checked_ino to indicate
3654-		 * the *next* one we want to check. And since we're walking the
3655-		 * buckets rather than doing it sequentially, it's: */
3656-		c->check_ino = ic->ino + c->inocache_hashsize;
3657+		if (!ic) {
3658+			spin_unlock(&c->inocache_lock);
3659+			continue;
3660+		}
3661
3662 		if (!ic->pino_nlink) {
3663 			jffs2_dbg(1, "Skipping check of ino #%d with nlink/pino zero\n",
3664@@ -198,6 +176,8 @@ int jffs2_garbage_collect_pass(struct jf
3665 		switch(ic->state) {
3666 		case INO_STATE_CHECKEDABSENT:
3667 		case INO_STATE_PRESENT:
3668+			jffs2_dbg(1, "Skipping ino #%u already checked\n",
3669+				  ic->ino);
3670 			spin_unlock(&c->inocache_lock);
3671 			continue;
3672
3673@@ -207,6 +187,7 @@ int jffs2_garbage_collect_pass(struct jf
3674 				ic->ino, ic->state);
3675 			spin_unlock(&c->inocache_lock);
3676 			BUG();
3677+			break;
3678
3679 		case INO_STATE_READING:
3680 			/* We need to wait for it to finish, lest we move on
3681@@ -216,7 +197,7 @@ int jffs2_garbage_collect_pass(struct jf
3682 				  ic->ino);
3683 			/* We need to come back again for the _same_ inode. We've
3684 			 made no progress in this case, but that should be OK */
3685-			c->check_ino = ic->ino;
3686+			c->checked_ino--;
3687
3688 			mutex_unlock(&c->alloc_sem);
3689 			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
3690@@ -224,6 +205,7 @@ int jffs2_garbage_collect_pass(struct jf
3691
3692 		default:
3693 			BUG();
3694+			break;
3695
3696 		case INO_STATE_UNCHECKED:
3697 			;
3698@@ -290,7 +272,7 @@ int jffs2_garbage_collect_pass(struct jf
3699 	raw = jeb->gc_node;
3700 	gcblock_dirty = jeb->dirty_size;
3701
3702-	while(ref_obsolete(raw)) {
3703+	while(raw && ref_obsolete(raw)) {
3704 		jffs2_dbg(1, "Node at 0x%08x is obsolete... skipping\n",
3705 			  ref_offset(raw));
3706 		raw = ref_next(raw);
3707@@ -310,7 +292,7 @@ int jffs2_garbage_collect_pass(struct jf
3708 	jffs2_dbg(1, "Going to garbage collect node at 0x%08x\n",
3709 		  ref_offset(raw));
3710
3711-	if (!raw->next_in_ino) {
3712+	if (raw &&!raw->next_in_ino) {
3713 		/* Inode-less node. Clean marker, snapshot or something like that */
3714 		spin_unlock(&c->erase_completion_lock);
3715 		if (ref_flags(raw) == REF_PRISTINE) {
3716@@ -368,7 +350,7 @@ int jffs2_garbage_collect_pass(struct jf
3717 		   We can just copy any pristine nodes, but have
3718 		   to prevent anyone else from doing read_inode() while
3719 		   we're at it, so we set the state accordingly */
3720-		if (ref_flags(raw) == REF_PRISTINE)
3721+		if (raw && ref_flags(raw) == REF_PRISTINE)
3722 			ic->state = INO_STATE_GC;
3723 		else {
3724 			jffs2_dbg(1, "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
3725@@ -393,6 +375,7 @@ int jffs2_garbage_collect_pass(struct jf
3726 		mutex_unlock(&c->alloc_sem);
3727 		spin_unlock(&c->inocache_lock);
3728 		BUG();
3729+		break;
3730
3731 	case INO_STATE_READING:
3732 		/* Someone's currently trying to read it. We must wait for
3733@@ -430,7 +413,6 @@ int jffs2_garbage_collect_pass(struct jf
3734
3735 		spin_lock(&c->inocache_lock);
3736 		ic->state = INO_STATE_CHECKEDABSENT;
3737-		wake_up(&c->inocache_wq);
3738
3739 		if (ret != -EBADFD) {
3740 			spin_unlock(&c->inocache_lock);
3741@@ -460,9 +442,7 @@ int jffs2_garbage_collect_pass(struct jf
3742 		ret = 0;
3743 		goto release_sem;
3744 	}
3745-
3746 	ret = jffs2_garbage_collect_live(c, jeb, raw, f);
3747-
3748 	jffs2_gc_release_inode(c, f);
3749
3750  test_gcnode:
3751@@ -541,7 +521,7 @@ static int jffs2_garbage_collect_live(st
3752 				break; /* We've found them all */
3753 		}
3754 	}
3755-	if (fn) {
3756+	if (fn != NULL && frag != NULL) {
3757 		if (ref_flags(raw) == REF_PRISTINE) {
3758 			ret = jffs2_garbage_collect_pristine(c, f->inocache, raw);
3759 			if (!ret) {
3760@@ -552,7 +532,7 @@ static int jffs2_garbage_collect_live(st
3761 				goto upnout;
3762 		}
3763 		/* We found a datanode. Do the GC */
3764-		if((start >> PAGE_SHIFT) < ((end-1) >> PAGE_SHIFT)) {
3765+		if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
3766 			/* It crosses a page boundary. Therefore, it must be a hole. */
3767 			ret = jffs2_garbage_collect_hole(c, jeb, f, fn, start, end);
3768 		} else {
3769@@ -635,6 +615,7 @@ static int jffs2_garbage_collect_pristin
3770 	if (je32_to_cpu(node->u.hdr_crc) != crc) {
3771 		pr_warn("Header CRC failed on REF_PRISTINE node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3772 			ref_offset(raw), je32_to_cpu(node->u.hdr_crc), crc);
3773+		jffs2_dbg_dump_node(c, ref_offset(raw));
3774 		goto bail;
3775 	}
3776
3777@@ -645,6 +626,7 @@ static int jffs2_garbage_collect_pristin
3778 			pr_warn("Node CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3779 				ref_offset(raw), je32_to_cpu(node->i.node_crc),
3780 				crc);
3781+			jffs2_dbg_dump_node(c, ref_offset(raw));
3782 			goto bail;
3783 		}
3784
3785@@ -654,6 +636,7 @@ static int jffs2_garbage_collect_pristin
3786 				pr_warn("Data CRC failed on REF_PRISTINE data node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3787 					ref_offset(raw),
3788 					je32_to_cpu(node->i.data_crc), crc);
3789+				jffs2_dbg_dump_node(c, ref_offset(raw));
3790 				goto bail;
3791 			}
3792 		}
3793@@ -665,12 +648,14 @@ static int jffs2_garbage_collect_pristin
3794 			pr_warn("Node CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3795 				ref_offset(raw),
3796 				je32_to_cpu(node->d.node_crc), crc);
3797+			jffs2_dbg_dump_node(c, ref_offset(raw));
3798 			goto bail;
3799 		}
3800
3801-		if (strnlen(node->d.name, node->d.nsize) != node->d.nsize) {
3802+		if (strnlen((const char *)node->d.name, node->d.nsize) != node->d.nsize) {
3803 			pr_warn("Name in dirent node at 0x%08x contains zeroes\n",
3804 				ref_offset(raw));
3805+			jffs2_dbg_dump_node(c, ref_offset(raw));
3806 			goto bail;
3807 		}
3808
3809@@ -680,6 +665,7 @@ static int jffs2_garbage_collect_pristin
3810 				pr_warn("Name CRC failed on REF_PRISTINE dirent node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
3811 					ref_offset(raw),
3812 					je32_to_cpu(node->d.name_crc), crc);
3813+				jffs2_dbg_dump_node(c, ref_offset(raw));
3814 				goto bail;
3815 			}
3816 		}
3817@@ -689,6 +675,7 @@ static int jffs2_garbage_collect_pristin
3818 		if (ic) {
3819 			pr_warn("Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
3820 				ref_offset(raw), je16_to_cpu(node->u.nodetype));
3821+			jffs2_dbg_dump_node(c, ref_offset(raw));
3822 			goto bail;
3823 		}
3824 	}
3825@@ -697,7 +684,7 @@ static int jffs2_garbage_collect_pristin
3826  retry:
3827 	phys_ofs = write_ofs(c);
3828
3829-	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
3830+	ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (const u_char *)node);
3831
3832 	if (ret || (retlen != rawlen)) {
3833 		pr_notice("Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
3834@@ -761,7 +748,7 @@ static int jffs2_garbage_collect_metadat
3835 	struct jffs2_full_dnode *new_fn;
3836 	struct jffs2_raw_inode ri;
3837 	struct jffs2_node_frag *last_frag;
3838-	union jffs2_device_node dev;
3839+	jint16_t dev;
3840 	char *mdata = NULL;
3841 	int mdatalen = 0;
3842 	uint32_t alloclen, ilen;
3843@@ -770,8 +757,9 @@ static int jffs2_garbage_collect_metadat
3844 	if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
3845 	    S_ISCHR(JFFS2_F_I_MODE(f)) ) {
3846 		/* For these, we don't actually need to read the old node */
3847-		mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
3848+		dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) | JFFS2_F_I_RDEV_MIN(f)));
3849 		mdata = (char *)&dev;
3850+		mdatalen = sizeof(dev);
3851 		jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
3852 			  __func__, mdatalen);
3853 	} else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
3854@@ -781,7 +769,7 @@ static int jffs2_garbage_collect_metadat
3855 			pr_warn("kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
3856 			return -ENOMEM;
3857 		}
3858-		ret = jffs2_read_dnode(c, f, fn, mdata, 0, mdatalen);
3859+		ret = jffs2_read_dnode(c, f, fn, (unsigned char *)mdata, 0, mdatalen);
3860 		if (ret) {
3861 			pr_warn("read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n",
3862 				ret);
3863@@ -831,7 +819,7 @@ static int jffs2_garbage_collect_metadat
3864 	ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
3865 	ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
3866
3867-	new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
3868+	new_fn = jffs2_write_dnode(c, f, &ri, (unsigned char *)mdata, mdatalen, ALLOC_GC);
3869
3870 	if (IS_ERR(new_fn)) {
3871 		pr_warn("Error writing new dnode: %ld\n", PTR_ERR(new_fn));
3872@@ -857,7 +845,7 @@ static int jffs2_garbage_collect_dirent(
3873
3874 	rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
3875 	rd.nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
3876-	rd.nsize = strlen(fd->name);
3877+	rd.nsize = strlen((const char *)fd->name);
3878 	rd.totlen = cpu_to_je32(sizeof(rd) + rd.nsize);
3879 	rd.hdr_crc = cpu_to_je32(crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4));
3880
3881@@ -908,7 +896,7 @@ static int jffs2_garbage_collect_deletio
3882 		struct jffs2_raw_node_ref *raw;
3883 		int ret;
3884 		size_t retlen;
3885-		int name_len = strlen(fd->name);
3886+		int name_len = strlen((const char *)fd->name);
3887 		uint32_t name_crc = crc32(0, fd->name, name_len);
3888 		uint32_t rawlen = ref_totlen(c, jeb, fd->raw);
3889
3890@@ -1053,6 +1041,7 @@ static int jffs2_garbage_collect_hole(st
3891 			pr_warn("%s: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
3892 				__func__, ref_offset(fn->raw),
3893 				je32_to_cpu(ri.node_crc), crc);
3894+				jffs2_dbg_dump_node(c, ref_offset(fn->raw));
3895 			/* FIXME: We could possibly deal with this by writing new holes for each frag */
3896 			pr_warn("Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
3897 				start, end, f->inocache->ino);
3898@@ -1165,13 +1154,12 @@ static int jffs2_garbage_collect_dnode(s
3899 				       struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
3900 				       uint32_t start, uint32_t end)
3901 {
3902-	struct inode *inode = OFNI_EDONI_2SFFJ(f);
3903 	struct jffs2_full_dnode *new_fn;
3904 	struct jffs2_raw_inode ri;
3905 	uint32_t alloclen, offset, orig_end, orig_start;
3906 	int ret = 0;
3907 	unsigned char *comprbuf = NULL, *writebuf;
3908-	struct page *page;
3909+	unsigned long pg;
3910 	unsigned char *pg_ptr;
3911
3912 	memset(&ri, 0, sizeof(ri));
3913@@ -1193,8 +1181,8 @@ static int jffs2_garbage_collect_dnode(s
3914 		struct jffs2_node_frag *frag;
3915 		uint32_t min, max;
3916
3917-		min = start & ~(PAGE_SIZE-1);
3918-		max = min + PAGE_SIZE;
3919+		min = start & ~(PAGE_CACHE_SIZE-1);
3920+		max = min + PAGE_CACHE_SIZE;
3921
3922 		frag = jffs2_lookup_node_frag(&f->fragtree, start);
3923
3924@@ -1203,7 +1191,7 @@ static int jffs2_garbage_collect_dnode(s
3925 		BUG_ON(frag->ofs != start);
3926
3927 		/* First grow down... */
3928-		while((frag = frag_prev(frag)) && frag->ofs >= min) {
3929+		while(frag && (frag = frag_prev(frag)) && frag->ofs >= min) {
3930
3931 			/* If the previous frag doesn't even reach the beginning, there's
3932 			   excessive fragmentation. Just merge. */
3933@@ -1259,7 +1247,7 @@ static int jffs2_garbage_collect_dnode(s
3934 		/* Find last frag which is actually part of the node we're to GC. */
3935 		frag = jffs2_lookup_node_frag(&f->fragtree, end-1);
3936
3937-		while((frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
3938+		while(frag && (frag = frag_next(frag)) && frag->ofs+frag->size <= max) {
3939
3940 			/* If the previous frag doesn't even reach the beginning, there's lots
3941 			   of fragmentation. Just merge. */
3942@@ -1317,27 +1305,21 @@ static int jffs2_garbage_collect_dnode(s
3943 		BUG_ON(start > orig_start);
3944 	}
3945
3946-	/* The rules state that we must obtain the page lock *before* f->sem, so
3947-	 * drop f->sem temporarily. Since we also hold c->alloc_sem, nothing's
3948-	 * actually going to *change* so we're safe; we only allow reading.
3949-	 *
3950-	 * It is important to note that jffs2_write_begin() will ensure that its
3951-	 * page is marked Uptodate before allocating space. That means that if we
3952-	 * end up here trying to GC the *same* page that jffs2_write_begin() is
3953-	 * trying to write out, read_cache_page() will not deadlock. */
3954-	mutex_unlock(&f->sem);
3955-	page = read_cache_page(inode->i_mapping, start >> PAGE_SHIFT,
3956-			       jffs2_do_readpage_unlock, inode);
3957-	if (IS_ERR(page)) {
3958+	/* First, use readpage() to read the appropriate page into the page cache */
3959+	/* Q: What happens if we actually try to GC the _same_ page for which commit_write()
3960+	 *    triggered garbage collection in the first place?
3961+	 * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
3962+	 *    page OK. We'll actually write it out again in commit_write, which is a little
3963+	 *    suboptimal, but at least we're correct.
3964+	 */
3965+	pg_ptr = jffs2_gc_fetch_page(c, f, start, &pg);
3966+
3967+	if (IS_ERR(pg_ptr)) {
3968 		pr_warn("read_cache_page() returned error: %ld\n",
3969-			PTR_ERR(page));
3970-		mutex_lock(&f->sem);
3971-		return PTR_ERR(page);
3972+			PTR_ERR(pg_ptr));
3973+		return PTR_ERR(pg_ptr);
3974 	}
3975
3976-	pg_ptr = kmap(page);
3977-	mutex_lock(&f->sem);
3978-
3979 	offset = start;
3980 	while(offset < orig_end) {
3981 		uint32_t datalen;
3982@@ -1355,7 +1337,7 @@ static int jffs2_garbage_collect_dnode(s
3983 		cdatalen = min_t(uint32_t, alloclen - sizeof(ri), end - offset);
3984 		datalen = end - offset;
3985
3986-		writebuf = pg_ptr + (offset & (PAGE_SIZE -1));
3987+		writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
3988
3989 		comprtype = jffs2_compress(c, f, writebuf, &comprbuf, &datalen, &cdatalen);
3990
3991@@ -1400,7 +1382,6 @@ static int jffs2_garbage_collect_dnode(s
3992 		}
3993 	}
3994
3995-	kunmap(page);
3996-	put_page(page);
3997+	jffs2_gc_release_page(c, pg_ptr, &pg);
3998 	return ret;
3999 }
4000diff -Nupr old/fs/jffs2/ioctl.c new/fs/jffs2/ioctl.c
4001--- old/fs/jffs2/ioctl.c	2022-05-09 17:15:24.350000000 +0800
4002+++ new/fs/jffs2/ioctl.c	1970-01-01 08:00:00.000000000 +0800
4003@@ -1,22 +0,0 @@
4004-/*
4005- * JFFS2 -- Journalling Flash File System, Version 2.
4006- *
4007- * Copyright © 2001-2007 Red Hat, Inc.
4008- * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
4009- *
4010- * Created by David Woodhouse <dwmw2@infradead.org>
4011- *
4012- * For licensing information, see the file 'LICENCE' in this directory.
4013- *
4014- */
4015-
4016-#include <linux/fs.h>
4017-#include "nodelist.h"
4018-
4019-long jffs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
4020-{
4021-	/* Later, this will provide for lsattr.jffs2 and chattr.jffs2, which
4022-	   will include compression support etc. */
4023-	return -ENOTTY;
4024-}
4025-
4026diff -Nupr old/fs/jffs2/jffs2_fs_i.h new/fs/jffs2/jffs2_fs_i.h
4027--- old/fs/jffs2/jffs2_fs_i.h	2022-05-09 17:22:53.000000000 +0800
4028+++ new/fs/jffs2/jffs2_fs_i.h	2022-05-09 20:50:05.810000000 +0800
4029@@ -14,8 +14,13 @@
4030 #define _JFFS2_FS_I
4031
4032 #include <linux/rbtree.h>
4033-#include <linux/posix_acl.h>
4034-#include <linux/mutex.h>
4035+#include <linux/kernel.h>
4036+
4037+#ifdef __cplusplus
4038+#if __cplusplus
4039+extern "C" {
4040+#endif /* __cplusplus */
4041+#endif /* __cplusplus */
4042
4043 struct jffs2_inode_info {
4044 	/* We need an internal mutex similar to inode->i_mutex.
4045@@ -24,7 +29,7 @@ struct jffs2_inode_info {
4046 	   before letting GC proceed. Or we'd have to put ugliness
4047 	   into the GC code so it didn't attempt to obtain the i_mutex
4048 	   for the inode(s) which are already locked */
4049-	struct mutex sem;
4050+	struct pthread_mutex sem;
4051
4052 	/* The highest (datanode) version number used for this ino */
4053 	uint32_t highest_version;
4054@@ -50,7 +55,29 @@ struct jffs2_inode_info {
4055
4056 	uint16_t flags;
4057 	uint8_t usercompr;
4058-	struct inode vfs_inode;
4059 };
4060
4061+struct super_block;
4062+
4063+struct jffs2_inode {
4064+	uint32_t i_ino;
4065+	mode_t i_mode;
4066+	nlink_t i_nlink;
4067+	uid_t i_uid;
4068+	gid_t i_gid;
4069+	time_t i_atime;
4070+	time_t i_mtime;
4071+	time_t i_ctime;
4072+	off_t i_size;
4073+	struct super_block *i_sb;
4074+	LOS_DL_LIST i_hashlist;
4075+	struct jffs2_inode_info jffs2_i;
4076+};
4077+
4078+#ifdef __cplusplus
4079+#if __cplusplus
4080+}
4081+#endif /* __cplusplus */
4082+#endif /* __cplusplus */
4083+
4084 #endif /* _JFFS2_FS_I */
4085diff -Nupr old/fs/jffs2/jffs2_fs_sb.h new/fs/jffs2/jffs2_fs_sb.h
4086--- old/fs/jffs2/jffs2_fs_sb.h	2022-05-09 17:22:53.000000000 +0800
4087+++ new/fs/jffs2/jffs2_fs_sb.h	2022-05-09 20:49:43.100000000 +0800
4088@@ -17,11 +17,18 @@
4089 #include <linux/spinlock.h>
4090 #include <linux/workqueue.h>
4091 #include <linux/completion.h>
4092-#include <linux/mutex.h>
4093 #include <linux/timer.h>
4094 #include <linux/wait.h>
4095 #include <linux/list.h>
4096 #include <linux/rwsem.h>
4097+#include "vfs_jffs2.h"
4098+#include "mtd_dev.h"
4099+
4100+#ifdef __cplusplus
4101+#if __cplusplus
4102+extern "C" {
4103+#endif /* __cplusplus */
4104+#endif /* __cplusplus */
4105
4106 #define JFFS2_SB_FLAG_RO 1
4107 #define JFFS2_SB_FLAG_SCANNING 2 /* Flash scanning is in progress */
4108@@ -47,10 +54,10 @@ struct jffs2_mount_opts {
4109    Nee jffs_control
4110 */
4111 struct jffs2_sb_info {
4112-	struct mtd_info *mtd;
4113+	struct MtdDev *mtd;
4114
4115 	uint32_t highest_ino;
4116-	uint32_t check_ino;		/* *NEXT* inode to be checked */
4117+	uint32_t checked_ino;
4118
4119 	unsigned int flags;
4120
4121@@ -58,7 +65,7 @@ struct jffs2_sb_info {
4122 	struct completion gc_thread_start; /* GC thread start completion */
4123 	struct completion gc_thread_exit; /* GC thread exit completion port */
4124
4125-	struct mutex alloc_sem;		/* Used to protect all the following
4126+	struct pthread_mutex alloc_sem;	/* Used to protect all the following
4127 					   fields, and also to protect against
4128 					   out-of-order writing of nodes. And GC. */
4129 	uint32_t cleanmarker_size;	/* Size of an _inline_ CLEANMARKER
4130@@ -120,7 +127,7 @@ struct jffs2_sb_info {
4131 	/* Sem to allow jffs2_garbage_collect_deletion_dirent to
4132 	   drop the erase_completion_lock while it's holding a pointer
4133 	   to an obsoleted node. I don't like this. Alternatives welcomed. */
4134-	struct mutex erase_free_sem;
4135+	struct pthread_mutex  erase_free_sem;
4136
4137 	uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */
4138
4139@@ -160,4 +167,27 @@ struct jffs2_sb_info {
4140 	void *os_priv;
4141 };
4142
4143+struct super_block {
4144+	struct jffs2_sb_info	jffs2_sb;
4145+	LIST_HEAD		s_node_hash[JFFS2_NODE_HASH_BUCKETS];
4146+	LosMux			s_node_hash_lock;
4147+	struct jffs2_inode 	*s_root;
4148+	void			*s_dev;
4149+
4150+	UINT32			s_lock;			/* Lock the inode cache */
4151+	EVENT_CB_S		s_gc_thread_flags;	/* Communication with the gcthread */
4152+	unsigned int		s_gc_thread;
4153+	unsigned long		s_mount_flags;
4154+};
4155+
4156+#define JFFS2_SB_INFO(sb) (&(sb)->jffs2_sb)
4157+#define OFNI_BS_2SFFJ(c)  \
4158+        ((struct super_block *) ( ((char *)c) - ((char *)(&((struct super_block *)NULL)->jffs2_sb)) ) )
4159+
4160+#ifdef __cplusplus
4161+#if __cplusplus
4162+}
4163+#endif /* __cplusplus */
4164+#endif /* __cplusplus */
4165+
4166 #endif /* _JFFS2_FS_SB */
4167diff -Nupr old/fs/jffs2/malloc.c new/fs/jffs2/malloc.c
4168--- old/fs/jffs2/malloc.c	2022-05-09 17:22:53.000000000 +0800
4169+++ new/fs/jffs2/malloc.c	2022-05-10 09:43:16.720000000 +0800
4170@@ -9,111 +9,31 @@
4171  *
4172  */
4173
4174-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4175-
4176 #include <linux/kernel.h>
4177 #include <linux/slab.h>
4178-#include <linux/init.h>
4179-#include <linux/jffs2.h>
4180+#include <stdlib.h>
4181 #include "nodelist.h"
4182
4183-/* These are initialised to NULL in the kernel startup code.
4184-   If you're porting to other operating systems, beware */
4185-static struct kmem_cache *full_dnode_slab;
4186-static struct kmem_cache *raw_dirent_slab;
4187-static struct kmem_cache *raw_inode_slab;
4188-static struct kmem_cache *tmp_dnode_info_slab;
4189-static struct kmem_cache *raw_node_ref_slab;
4190-static struct kmem_cache *node_frag_slab;
4191-static struct kmem_cache *inode_cache_slab;
4192-#ifdef CONFIG_JFFS2_FS_XATTR
4193-static struct kmem_cache *xattr_datum_cache;
4194-static struct kmem_cache *xattr_ref_cache;
4195+#if !defined(JFFS2NUM_FS_JFFS2_RAW_NODE_REF_CACHE_POOL_SIZE)
4196+# define JFFS2NUM_FS_JFFS2_RAW_NODE_REF_CACHE_POOL_SIZE 0
4197 #endif
4198
4199 int __init jffs2_create_slab_caches(void)
4200 {
4201-	full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
4202-					    sizeof(struct jffs2_full_dnode),
4203-					    0, 0, NULL);
4204-	if (!full_dnode_slab)
4205-		goto err;
4206-
4207-	raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
4208-					    sizeof(struct jffs2_raw_dirent),
4209-					    0, SLAB_HWCACHE_ALIGN, NULL);
4210-	if (!raw_dirent_slab)
4211-		goto err;
4212-
4213-	raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
4214-					   sizeof(struct jffs2_raw_inode),
4215-					   0, SLAB_HWCACHE_ALIGN, NULL);
4216-	if (!raw_inode_slab)
4217-		goto err;
4218-
4219-	tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
4220-						sizeof(struct jffs2_tmp_dnode_info),
4221-						0, 0, NULL);
4222-	if (!tmp_dnode_info_slab)
4223-		goto err;
4224-
4225-	raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
4226-					      sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
4227-					      0, 0, NULL);
4228-	if (!raw_node_ref_slab)
4229-		goto err;
4230-
4231-	node_frag_slab = kmem_cache_create("jffs2_node_frag",
4232-					   sizeof(struct jffs2_node_frag),
4233-					   0, 0, NULL);
4234-	if (!node_frag_slab)
4235-		goto err;
4236-
4237-	inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
4238-					     sizeof(struct jffs2_inode_cache),
4239-					     0, 0, NULL);
4240-	if (!inode_cache_slab)
4241-		goto err;
4242-
4243-#ifdef CONFIG_JFFS2_FS_XATTR
4244-	xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum",
4245-					     sizeof(struct jffs2_xattr_datum),
4246-					     0, 0, NULL);
4247-	if (!xattr_datum_cache)
4248-		goto err;
4249-
4250-	xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
4251-					   sizeof(struct jffs2_xattr_ref),
4252-					   0, 0, NULL);
4253-	if (!xattr_ref_cache)
4254-		goto err;
4255-#endif
4256-
4257 	return 0;
4258- err:
4259-	jffs2_destroy_slab_caches();
4260-	return -ENOMEM;
4261+
4262 }
4263
4264 void jffs2_destroy_slab_caches(void)
4265 {
4266-	kmem_cache_destroy(full_dnode_slab);
4267-	kmem_cache_destroy(raw_dirent_slab);
4268-	kmem_cache_destroy(raw_inode_slab);
4269-	kmem_cache_destroy(tmp_dnode_info_slab);
4270-	kmem_cache_destroy(raw_node_ref_slab);
4271-	kmem_cache_destroy(node_frag_slab);
4272-	kmem_cache_destroy(inode_cache_slab);
4273-#ifdef CONFIG_JFFS2_FS_XATTR
4274-	kmem_cache_destroy(xattr_datum_cache);
4275-	kmem_cache_destroy(xattr_ref_cache);
4276-#endif
4277+	return;
4278 }
4279
4280+
4281 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
4282 {
4283 	struct jffs2_full_dirent *ret;
4284-	ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
4285+	ret = zalloc(sizeof(struct jffs2_full_dirent) + namesize);
4286 	dbg_memalloc("%p\n", ret);
4287 	return ret;
4288 }
4289@@ -127,7 +47,7 @@ void jffs2_free_full_dirent(struct jffs2
4290 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
4291 {
4292 	struct jffs2_full_dnode *ret;
4293-	ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
4294+	ret = zalloc(sizeof(struct jffs2_full_dnode));
4295 	dbg_memalloc("%p\n", ret);
4296 	return ret;
4297 }
4298@@ -135,13 +55,13 @@ struct jffs2_full_dnode *jffs2_alloc_ful
4299 void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
4300 {
4301 	dbg_memalloc("%p\n", x);
4302-	kmem_cache_free(full_dnode_slab, x);
4303+	free(x);
4304 }
4305
4306 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
4307 {
4308 	struct jffs2_raw_dirent *ret;
4309-	ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
4310+	ret = zalloc(sizeof(struct jffs2_raw_dirent));
4311 	dbg_memalloc("%p\n", ret);
4312 	return ret;
4313 }
4314@@ -149,13 +69,13 @@ struct jffs2_raw_dirent *jffs2_alloc_raw
4315 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
4316 {
4317 	dbg_memalloc("%p\n", x);
4318-	kmem_cache_free(raw_dirent_slab, x);
4319+	free(x);
4320 }
4321
4322 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
4323 {
4324 	struct jffs2_raw_inode *ret;
4325-	ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
4326+	ret = zalloc(sizeof(struct jffs2_raw_inode));
4327 	dbg_memalloc("%p\n", ret);
4328 	return ret;
4329 }
4330@@ -163,13 +83,13 @@ struct jffs2_raw_inode *jffs2_alloc_raw_
4331 void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
4332 {
4333 	dbg_memalloc("%p\n", x);
4334-	kmem_cache_free(raw_inode_slab, x);
4335+	free(x);
4336 }
4337
4338 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
4339 {
4340 	struct jffs2_tmp_dnode_info *ret;
4341-	ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
4342+	ret = zalloc(sizeof(struct jffs2_tmp_dnode_info));
4343 	dbg_memalloc("%p\n",
4344 		ret);
4345 	return ret;
4346@@ -178,14 +98,14 @@ struct jffs2_tmp_dnode_info *jffs2_alloc
4347 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
4348 {
4349 	dbg_memalloc("%p\n", x);
4350-	kmem_cache_free(tmp_dnode_info_slab, x);
4351+	free(x);
4352 }
4353
4354 static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
4355 {
4356 	struct jffs2_raw_node_ref *ret;
4357
4358-	ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
4359+	ret = malloc(sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK+1));
4360 	if (ret) {
4361 		int i = 0;
4362 		for (i=0; i < REFS_PER_BLOCK; i++) {
4363@@ -242,13 +162,13 @@ int jffs2_prealloc_raw_node_refs(struct
4364 void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
4365 {
4366 	dbg_memalloc("%p\n", x);
4367-	kmem_cache_free(raw_node_ref_slab, x);
4368+	free(x);
4369 }
4370
4371 struct jffs2_node_frag *jffs2_alloc_node_frag(void)
4372 {
4373 	struct jffs2_node_frag *ret;
4374-	ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
4375+	ret = malloc(sizeof(struct jffs2_node_frag));
4376 	dbg_memalloc("%p\n", ret);
4377 	return ret;
4378 }
4379@@ -256,13 +176,14 @@ struct jffs2_node_frag *jffs2_alloc_node
4380 void jffs2_free_node_frag(struct jffs2_node_frag *x)
4381 {
4382 	dbg_memalloc("%p\n", x);
4383-	kmem_cache_free(node_frag_slab, x);
4384+	free(x);
4385 }
4386
4387+
4388 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
4389 {
4390 	struct jffs2_inode_cache *ret;
4391-	ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
4392+	ret = zalloc(sizeof(struct jffs2_inode_cache));;
4393 	dbg_memalloc("%p\n", ret);
4394 	return ret;
4395 }
4396@@ -270,14 +191,14 @@ struct jffs2_inode_cache *jffs2_alloc_in
4397 void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
4398 {
4399 	dbg_memalloc("%p\n", x);
4400-	kmem_cache_free(inode_cache_slab, x);
4401+	kfree(x);
4402 }
4403
4404 #ifdef CONFIG_JFFS2_FS_XATTR
4405 struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
4406 {
4407 	struct jffs2_xattr_datum *xd;
4408-	xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
4409+	xd = malloc(sizeof(struct jffs2_xattr_datum));
4410 	dbg_memalloc("%p\n", xd);
4411 	if (!xd)
4412 		return NULL;
4413@@ -291,13 +212,13 @@ struct jffs2_xattr_datum *jffs2_alloc_xa
4414 void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
4415 {
4416 	dbg_memalloc("%p\n", xd);
4417-	kmem_cache_free(xattr_datum_cache, xd);
4418+	kfree(xd);
4419 }
4420
4421 struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
4422 {
4423 	struct jffs2_xattr_ref *ref;
4424-	ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
4425+	ref = malloc(sizeof(struct jffs2_xattr_ref));
4426 	dbg_memalloc("%p\n", ref);
4427 	if (!ref)
4428 		return NULL;
4429@@ -310,6 +231,6 @@ struct jffs2_xattr_ref *jffs2_alloc_xatt
4430 void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
4431 {
4432 	dbg_memalloc("%p\n", ref);
4433-	kmem_cache_free(xattr_ref_cache, ref);
4434+	kfree(ref);
4435 }
4436 #endif
4437diff -Nupr old/fs/jffs2/nodelist.c new/fs/jffs2/nodelist.c
4438--- old/fs/jffs2/nodelist.c	2022-05-09 17:22:53.000000000 +0800
4439+++ new/fs/jffs2/nodelist.c	2022-05-09 20:37:35.680000000 +0800
4440@@ -9,16 +9,15 @@
4441  *
4442  */
4443
4444-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4445-
4446 #include <linux/kernel.h>
4447 #include <linux/sched.h>
4448 #include <linux/fs.h>
4449-#include <linux/mtd/mtd.h>
4450 #include <linux/rbtree.h>
4451-#include <linux/crc32.h>
4452 #include <linux/pagemap.h>
4453+#include <mtd_dev.h>
4454 #include "nodelist.h"
4455+#include "jffs2.h"
4456+#include "los_crc32.h"
4457
4458 static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c,
4459 				     struct jffs2_node_frag *this);
4460@@ -30,7 +29,7 @@ void jffs2_add_fd_to_list(struct jffs2_s
4461 	dbg_dentlist("add dirent \"%s\", ino #%u\n", new->name, new->ino);
4462
4463 	while ((*prev) && (*prev)->nhash <= new->nhash) {
4464-		if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
4465+		if ((*prev)->nhash == new->nhash && !strcmp((const char *)((*prev)->name), (const char *)new->name)) {
4466 			/* Duplicate. Free one */
4467 			if (new->version < (*prev)->version) {
4468 				dbg_dentlist("Eep! Marking new dirent node obsolete, old is \"%s\", ino #%u\n",
4469@@ -41,7 +40,7 @@ void jffs2_add_fd_to_list(struct jffs2_s
4470 				dbg_dentlist("marking old dirent \"%s\", ino #%u obsolete\n",
4471 					(*prev)->name, (*prev)->ino);
4472 				new->next = (*prev)->next;
4473-				/* It may have been a 'placeholder' deletion dirent,
4474+				/* It may have been a 'placeholder' deletion dirent,
4475 				   if jffs2_can_mark_obsolete() (see jffs2_do_unlink()) */
4476 				if ((*prev)->raw)
4477 					jffs2_mark_node_obsolete(c, ((*prev)->raw));
4478@@ -65,13 +64,14 @@ uint32_t jffs2_truncate_fragtree(struct
4479 	/* We know frag->ofs <= size. That's what lookup does for us */
4480 	if (frag && frag->ofs != size) {
4481 		if (frag->ofs+frag->size > size) {
4482+		    dbg_fragtree("truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
4483 			frag->size = size - frag->ofs;
4484 		}
4485 		frag = frag_next(frag);
4486 	}
4487 	while (frag && frag->ofs >= size) {
4488 		struct jffs2_node_frag *next = frag_next(frag);
4489-
4490+        dbg_fragtree("removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size);
4491 		frag_erase(frag, list);
4492 		jffs2_obsolete_node_frag(c, frag);
4493 		frag = next;
4494@@ -90,7 +90,7 @@ uint32_t jffs2_truncate_fragtree(struct
4495
4496 	/* If the last fragment starts at the RAM page boundary, it is
4497 	 * REF_PRISTINE irrespective of its size. */
4498-	if (frag->node && (frag->ofs & (PAGE_SIZE - 1)) == 0) {
4499+	if (frag->node && (frag->ofs & (PAGE_CACHE_SIZE - 1)) == 0) {
4500 		dbg_fragtree2("marking the last fragment 0x%08x-0x%08x REF_PRISTINE.\n",
4501 			frag->ofs, frag->ofs + frag->size);
4502 		frag->node->raw->flash_offset = ref_offset(frag->node->raw) | REF_PRISTINE;
4503@@ -237,7 +237,7 @@ static int jffs2_add_frag_to_fragtree(st
4504 		   If so, both 'this' and the new node get marked REF_NORMAL so
4505 		   the GC can take a look.
4506 		*/
4507-		if (lastend && (lastend-1) >> PAGE_SHIFT == newfrag->ofs >> PAGE_SHIFT) {
4508+		if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
4509 			if (this->node)
4510 				mark_ref_normal(this->node->raw);
4511 			mark_ref_normal(newfrag->node->raw);
4512@@ -382,7 +382,7 @@ int jffs2_add_full_dnode_to_inode(struct
4513
4514 	/* If we now share a page with other nodes, mark either previous
4515 	   or next node REF_NORMAL, as appropriate.  */
4516-	if (newfrag->ofs & (PAGE_SIZE-1)) {
4517+	if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
4518 		struct jffs2_node_frag *prev = frag_prev(newfrag);
4519
4520 		mark_ref_normal(fn->raw);
4521@@ -391,7 +391,7 @@ int jffs2_add_full_dnode_to_inode(struct
4522 			mark_ref_normal(prev->node->raw);
4523 	}
4524
4525-	if ((newfrag->ofs+newfrag->size) & (PAGE_SIZE-1)) {
4526+	if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
4527 		struct jffs2_node_frag *next = frag_next(newfrag);
4528
4529 		if (next) {
4530@@ -401,7 +401,7 @@ int jffs2_add_full_dnode_to_inode(struct
4531 		}
4532 	}
4533 	jffs2_dbg_fragtree_paranoia_check_nolock(f);
4534-
4535+	jffs2_dbg_dump_fragtree_nolock(f);
4536 	return 0;
4537 }
4538
4539@@ -409,7 +409,6 @@ void jffs2_set_inocache_state(struct jff
4540 {
4541 	spin_lock(&c->inocache_lock);
4542 	ic->state = state;
4543-	wake_up(&c->inocache_wq);
4544 	spin_unlock(&c->inocache_lock);
4545 }
4546
4547@@ -505,8 +504,12 @@ void jffs2_free_raw_node_refs(struct jff
4548 {
4549 	int i;
4550 	struct jffs2_raw_node_ref *this, *next;
4551+	struct super_block *sb = NULL;
4552+	struct MtdNorDev *device = NULL;
4553+	sb = OFNI_BS_2SFFJ(c);
4554+	device = (struct MtdNorDev*)(sb->s_dev);
4555
4556-	for (i=0; i<c->nr_blocks; i++) {
4557+	for (i=device->blockStart; i<c->nr_blocks+device->blockStart; i++) {
4558 		this = c->blocks[i].first_node;
4559 		while (this) {
4560 			if (this[REFS_PER_BLOCK].flash_offset == REF_LINK_NODE)
4561@@ -536,14 +539,22 @@ struct jffs2_node_frag *jffs2_lookup_nod
4562 	while(next) {
4563 		frag = rb_entry(next, struct jffs2_node_frag, rb);
4564
4565+		dbg_fragtree2("considering frag %#04x-%#04x (%p). left %p, right %p\n",
4566+			  frag->ofs, frag->ofs+frag->size, frag, frag->rb.rb_left, frag->rb.rb_right);
4567 		if (frag->ofs + frag->size <= offset) {
4568+			dbg_fragtree2("going right from frag %#04x-%#04x, before the region we care about\n",
4569+				  frag->ofs, frag->ofs+frag->size);
4570 			/* Remember the closest smaller match on the way down */
4571 			if (!prev || frag->ofs > prev->ofs)
4572 				prev = frag;
4573 			next = frag->rb.rb_right;
4574 		} else if (frag->ofs > offset) {
4575+			dbg_fragtree2("going left from frag %#04x-%#04x, after the region we care about\n",
4576+				  frag->ofs, frag->ofs+frag->size);
4577 			next = frag->rb.rb_left;
4578 		} else {
4579+			dbg_fragtree2("returning frag %#04x-%#04x, matched\n",
4580+				  frag->ofs, frag->ofs+frag->size);
4581 			return frag;
4582 		}
4583 	}
4584@@ -564,10 +575,12 @@ struct jffs2_node_frag *jffs2_lookup_nod
4585    they're killed. */
4586 void jffs2_kill_fragtree(struct rb_root *root, struct jffs2_sb_info *c)
4587 {
4588-	struct jffs2_node_frag *frag, *next;
4589+	struct jffs2_node_frag *frag;
4590+	struct rb_node *tn,*next;
4591
4592 	dbg_fragtree("killing\n");
4593-	rbtree_postorder_for_each_entry_safe(frag, next, root, rb) {
4594+	RB_POSTORDER_FOREACH_SAFE(tn, linux_root, (struct linux_root *)root, next) {
4595+		frag = (struct jffs2_node_frag *)tn;
4596 		if (frag->node && !(--frag->node->frags)) {
4597 			/* Not a hole, and it's the final remaining frag
4598 			   of this node. Free the node */
4599@@ -604,7 +617,7 @@ struct jffs2_raw_node_ref *jffs2_link_no
4600 			ref++;
4601 	}
4602
4603-	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
4604+	dbg_noderef("New ref is %p (%08x becomes %08x,%p) len 0x%x\n", ref,
4605 		    ref->flash_offset, ofs, ref->next_in_ino, len);
4606
4607 	ref->flash_offset = ofs;
4608@@ -617,7 +630,7 @@ struct jffs2_raw_node_ref *jffs2_link_no
4609
4610 		JFFS2_ERROR("Adding new ref %p at (0x%08x-0x%08x) not immediately after previous (0x%08x-0x%08x)\n",
4611 			    ref, ref_offset(ref), ref_offset(ref)+len,
4612-			    ref_offset(jeb->last_node),
4613+			    ref_offset(jeb->last_node),
4614 			    ref_offset(jeb->last_node)+last_len);
4615 		BUG();
4616 	}
4617@@ -734,7 +747,7 @@ uint32_t __jffs2_ref_totlen(struct jffs2
4618 			pr_crit("next %p (0x%08x-0x%08x)\n",
4619 				ref_next(ref), ref_offset(ref_next(ref)),
4620 				ref_offset(ref_next(ref)) + ref->__totlen);
4621-		} else
4622+		} else
4623 			pr_crit("No next ref. jeb->last_node is %p\n",
4624 				jeb->last_node);
4625
4626diff -Nupr old/fs/jffs2/nodelist.h new/fs/jffs2/nodelist.h
4627--- old/fs/jffs2/nodelist.h	2022-05-09 17:22:53.000000000 +0800
4628+++ new/fs/jffs2/nodelist.h	2022-05-09 20:36:25.460000000 +0800
4629@@ -12,20 +12,28 @@
4630 #ifndef __JFFS2_NODELIST_H__
4631 #define __JFFS2_NODELIST_H__
4632
4633-#include <linux/fs.h>
4634+#include <linux/stat.h>
4635 #include <linux/types.h>
4636-#include <linux/jffs2.h>
4637+#include <linux/list.h>
4638+#include "jffs2.h"
4639 #include "jffs2_fs_sb.h"
4640 #include "jffs2_fs_i.h"
4641 #include "xattr.h"
4642 #include "acl.h"
4643 #include "summary.h"
4644-
4645-#ifdef __ECOS
4646-#include "os-ecos.h"
4647-#else
4648+#include "vfs_jffs2.h"
4649 #include "os-linux.h"
4650-#endif
4651+
4652+#ifdef __cplusplus
4653+#if __cplusplus
4654+extern "C" {
4655+#endif /* __cplusplus */
4656+#endif /* __cplusplus */
4657+
4658+struct kvec {
4659+	void *iov_base;
4660+	long iov_len;
4661+};
4662
4663 #define JFFS2_NATIVE_ENDIAN
4664
4665@@ -193,6 +201,8 @@ struct jffs2_inode_cache {
4666 #define INO_STATE_READING	5	/* In read_inode() */
4667 #define INO_STATE_CLEARING	6	/* In clear_inode() */
4668
4669+#define INOCACHE_HASHSIZE 128
4670+
4671 #define INO_FLAGS_XATTR_CHECKED	0x01	/* has no duplicate xattr_ref */
4672 #define INO_FLAGS_IS_DIR	0x02	/* is a directory */
4673
4674@@ -250,10 +260,7 @@ struct jffs2_readinode_info
4675
4676 struct jffs2_full_dirent
4677 {
4678-	union {
4679-		struct jffs2_raw_node_ref *raw;
4680-		struct jffs2_inode_cache *ic; /* Just during part of build */
4681-	};
4682+	struct jffs2_raw_node_ref *raw;
4683 	struct jffs2_full_dirent *next;
4684 	uint32_t version;
4685 	uint32_t ino; /* == zero for unlink */
4686@@ -313,34 +320,26 @@ static inline int jffs2_blocks_use_vmall
4687
4688 #define PAD(x) (((x)+3)&~3)
4689
4690-static inline int jffs2_encode_dev(union jffs2_device_node *jdev, dev_t rdev)
4691-{
4692-	if (old_valid_dev(rdev)) {
4693-		jdev->old_id = cpu_to_je16(old_encode_dev(rdev));
4694-		return sizeof(jdev->old_id);
4695-	} else {
4696-		jdev->new_id = cpu_to_je32(new_encode_dev(rdev));
4697-		return sizeof(jdev->new_id);
4698-	}
4699-}
4700
4701 static inline struct jffs2_node_frag *frag_first(struct rb_root *root)
4702 {
4703-	struct rb_node *node = rb_first(root);
4704+	struct rb_node *node = root->rb_node;
4705
4706 	if (!node)
4707 		return NULL;
4708-
4709+	while(node->rb_left)
4710+		node = node->rb_left;
4711 	return rb_entry(node, struct jffs2_node_frag, rb);
4712 }
4713
4714 static inline struct jffs2_node_frag *frag_last(struct rb_root *root)
4715 {
4716-	struct rb_node *node = rb_last(root);
4717+	struct rb_node *node = root->rb_node;
4718
4719 	if (!node)
4720 		return NULL;
4721-
4722+	while(node->rb_right)
4723+		node = node->rb_right;
4724 	return rb_entry(node, struct jffs2_node_frag, rb);
4725 }
4726
4727@@ -404,8 +403,9 @@ struct jffs2_full_dirent *jffs2_write_di
4728 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
4729 			    struct jffs2_raw_inode *ri, unsigned char *buf,
4730 			    uint32_t offset, uint32_t writelen, uint32_t *retlen);
4731-int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f,
4732-		    struct jffs2_raw_inode *ri, const struct qstr *qstr);
4733+int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
4734+		    struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
4735+		    const char *name, int namelen);
4736 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, const char *name,
4737 		    int namelen, struct jffs2_inode_info *dead_f, uint32_t time);
4738 int jffs2_do_link(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino,
4739@@ -481,4 +481,10 @@ int jffs2_write_nand_cleanmarker(struct
4740
4741 #include "debug.h"
4742
4743+#ifdef __cplusplus
4744+#if __cplusplus
4745+}
4746+#endif /* __cplusplus */
4747+#endif /* __cplusplus */
4748+
4749 #endif /* __JFFS2_NODELIST_H__ */
4750diff -Nupr old/fs/jffs2/nodemgmt.c new/fs/jffs2/nodemgmt.c
4751--- old/fs/jffs2/nodemgmt.c	2022-05-09 17:22:53.000000000 +0800
4752+++ new/fs/jffs2/nodemgmt.c	2022-05-09 20:35:50.910000000 +0800
4753@@ -9,46 +9,14 @@
4754  *
4755  */
4756
4757-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4758-
4759 #include <linux/kernel.h>
4760-#include <linux/mtd/mtd.h>
4761 #include <linux/compiler.h>
4762-#include <linux/sched/signal.h>
4763+#include <linux/sched.h> /* For cond_resched() */
4764+#include <linux/semaphore.h>
4765+#include <mtd_dev.h>
4766 #include "nodelist.h"
4767 #include "debug.h"
4768
4769-/*
4770- * Check whether the user is allowed to write.
4771- */
4772-static int jffs2_rp_can_write(struct jffs2_sb_info *c)
4773-{
4774-	uint32_t avail;
4775-	struct jffs2_mount_opts *opts = &c->mount_opts;
4776-
4777-	avail = c->dirty_size + c->free_size + c->unchecked_size +
4778-		c->erasing_size - c->resv_blocks_write * c->sector_size
4779-		- c->nospc_dirty_size;
4780-
4781-	if (avail < 2 * opts->rp_size)
4782-		jffs2_dbg(1, "rpsize %u, dirty_size %u, free_size %u, "
4783-			  "erasing_size %u, unchecked_size %u, "
4784-			  "nr_erasing_blocks %u, avail %u, resrv %u\n",
4785-			  opts->rp_size, c->dirty_size, c->free_size,
4786-			  c->erasing_size, c->unchecked_size,
4787-			  c->nr_erasing_blocks, avail, c->nospc_dirty_size);
4788-
4789-	if (avail > opts->rp_size)
4790-		return 1;
4791-
4792-	/* Always allow root */
4793-	if (capable(CAP_SYS_RESOURCE))
4794-		return 1;
4795-
4796-	jffs2_dbg(1, "forbid writing\n");
4797-	return 0;
4798-}
4799-
4800 /**
4801  *	jffs2_reserve_space - request physical space to write nodes to flash
4802  *	@c: superblock info
4803@@ -57,8 +25,8 @@ static int jffs2_rp_can_write(struct jff
4804  *	@prio: Allocation type - ALLOC_{NORMAL,DELETION}
4805  *
4806  *	Requests a block of physical space on the flash. Returns zero for success
4807- *	and puts 'len' into the appropriate place, or returns -ENOSPC or other
4808- *	error if appropriate. Doesn't return len since that's
4809+ *	and puts 'len' into the appropriate place, or returns -ENOSPC or other
4810+ *	error if appropriate. Doesn't return len since that's
4811  *
4812  *	If it returns zero, jffs2_reserve_space() also downs the per-filesystem
4813  *	allocation semaphore, to prevent more than one allocation from being
4814@@ -86,15 +54,6 @@ int jffs2_reserve_space(struct jffs2_sb_
4815
4816 	spin_lock(&c->erase_completion_lock);
4817
4818-	/*
4819-	 * Check if the free space is greater then size of the reserved pool.
4820-	 * If not, only allow root to proceed with writing.
4821-	 */
4822-	if (prio != ALLOC_DELETION && !jffs2_rp_can_write(c)) {
4823-		ret = -ENOSPC;
4824-		goto out;
4825-	}
4826-
4827 	/* this needs a little more thought (true <tglx> :)) */
4828 	while(ret == -EAGAIN) {
4829 		while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
4830@@ -165,24 +124,7 @@ int jffs2_reserve_space(struct jffs2_sb_
4831 			spin_unlock(&c->erase_completion_lock);
4832
4833 			ret = jffs2_garbage_collect_pass(c);
4834-
4835-			if (ret == -EAGAIN) {
4836-				spin_lock(&c->erase_completion_lock);
4837-				if (c->nr_erasing_blocks &&
4838-				    list_empty(&c->erase_pending_list) &&
4839-				    list_empty(&c->erase_complete_list)) {
4840-					DECLARE_WAITQUEUE(wait, current);
4841-					set_current_state(TASK_UNINTERRUPTIBLE);
4842-					add_wait_queue(&c->erase_wait, &wait);
4843-					jffs2_dbg(1, "%s waiting for erase to complete\n",
4844-						  __func__);
4845-					spin_unlock(&c->erase_completion_lock);
4846-
4847-					schedule();
4848-					remove_wait_queue(&c->erase_wait, &wait);
4849-				} else
4850-					spin_unlock(&c->erase_completion_lock);
4851-			} else if (ret)
4852+			if (ret)
4853 				return ret;
4854
4855 			cond_resched();
4856@@ -200,7 +142,6 @@ int jffs2_reserve_space(struct jffs2_sb_
4857 		}
4858 	}
4859
4860-out:
4861 	spin_unlock(&c->erase_completion_lock);
4862 	if (!ret)
4863 		ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
4864@@ -509,7 +450,7 @@ struct jffs2_raw_node_ref *jffs2_add_phy
4865 	jffs2_dbg(1, "%s(): Node at 0x%x(%d), size 0x%x\n",
4866 		  __func__, ofs & ~3, ofs & 3, len);
4867 #if 1
4868-	/* Allow non-obsolete nodes only to be added at the end of c->nextblock,
4869+	/* Allow non-obsolete nodes only to be added at the end of c->nextblock,
4870 	   if c->nextblock is set. Note that wbuf.c will file obsolete nodes
4871 	   even after refiling c->nextblock */
4872 	if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
4873@@ -584,6 +525,8 @@ void jffs2_mark_node_obsolete(struct jff
4874 	int ret, addedsize;
4875 	size_t retlen;
4876 	uint32_t freed_len;
4877+	struct super_block *sb;
4878+	struct MtdNorDev *device;
4879
4880 	if(unlikely(!ref)) {
4881 		pr_notice("EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
4882@@ -595,9 +538,10 @@ void jffs2_mark_node_obsolete(struct jff
4883 		return;
4884 	}
4885 	blocknr = ref->flash_offset / c->sector_size;
4886-	if (blocknr >= c->nr_blocks) {
4887-		pr_notice("raw node at 0x%08x is off the end of device!\n",
4888-			  ref->flash_offset);
4889+	sb = OFNI_BS_2SFFJ(c);
4890+	device = (struct MtdNorDev*)(sb->s_dev);
4891+	if (blocknr >= c->nr_blocks +device->blockStart) {
4892+		pr_notice("raw node at 0x%08x is off the end of device!\n",ref->flash_offset);
4893 		BUG();
4894 	}
4895 	jeb = &c->blocks[blocknr];
4896@@ -778,7 +722,7 @@ void jffs2_mark_node_obsolete(struct jff
4897 	}
4898 	/* XXX FIXME: This is ugly now */
4899 	n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
4900-	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
4901+	ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (const u_char *)&n);
4902 	if (ret) {
4903 		pr_warn("Write error in obliterating obsoleted node at 0x%08x: %d\n",
4904 			ref_offset(ref), ret);
4905@@ -846,8 +790,8 @@ int jffs2_thread_should_wake(struct jffs
4906 		return 1;
4907
4908 	if (c->unchecked_size) {
4909-		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, check_ino #%d\n",
4910-			  c->unchecked_size, c->check_ino);
4911+		jffs2_dbg(1, "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
4912+			  c->unchecked_size, c->checked_ino);
4913 		return 1;
4914 	}
4915
4916diff -Nupr old/fs/jffs2/os-linux.h new/fs/jffs2/os-linux.h
4917--- old/fs/jffs2/os-linux.h	2022-05-09 17:22:53.000000000 +0800
4918+++ new/fs/jffs2/os-linux.h	2022-05-09 20:33:21.200000000 +0800
4919@@ -12,59 +12,57 @@
4920 #ifndef __JFFS2_OS_LINUX_H__
4921 #define __JFFS2_OS_LINUX_H__
4922
4923+#include <dirent.h>
4924+#include "fs/fs.h"
4925+#include "jffs2.h"
4926+#include "jffs2_fs_sb.h"
4927+
4928+
4929+/* jffs2 debug output opion */
4930+#define CONFIG_JFFS2_FS_DEBUG       0  /* 1 or 2 */
4931+
4932+/* jffs2 gc thread section */
4933+#define JFFS2_GC_THREAD_PRIORITY  10 /* GC thread's priority */
4934+
4935+/* zlib section*/
4936+#define CONFIG_JFFS2_ZLIB
4937+#define CONFIG_JFFS2_RTIME
4938+#define CONFIG_JFFS2_RUBIN
4939+
4940 /* JFFS2 uses Linux mode bits natively -- no need for conversion */
4941 #define os_to_jffs2_mode(x) (x)
4942 #define jffs2_to_os_mode(x) (x)
4943
4944+#ifndef BUG_ON
4945+#define BUG_ON(x) do {if (unlikely(x)) BUG();} while (0)
4946+#endif
4947+
4948 struct kstatfs;
4949 struct kvec;
4950
4951-#define JFFS2_INODE_INFO(i) (container_of(i, struct jffs2_inode_info, vfs_inode))
4952-#define OFNI_EDONI_2SFFJ(f)  (&(f)->vfs_inode)
4953-#define JFFS2_SB_INFO(sb) (sb->s_fs_info)
4954-#define OFNI_BS_2SFFJ(c)  ((struct super_block *)c->os_priv)
4955
4956+#define JFFS2_INODE_INFO(i) (&(i)->jffs2_i)
4957+#define OFNI_EDONI_2SFFJ(f)  \
4958+        ((struct jffs2_inode *) (((char *)f) - ((char *)(&((struct jffs2_inode *)NULL)->jffs2_i))))
4959
4960 #define JFFS2_F_I_SIZE(f) (OFNI_EDONI_2SFFJ(f)->i_size)
4961 #define JFFS2_F_I_MODE(f) (OFNI_EDONI_2SFFJ(f)->i_mode)
4962-#define JFFS2_F_I_UID(f) (i_uid_read(OFNI_EDONI_2SFFJ(f)))
4963-#define JFFS2_F_I_GID(f) (i_gid_read(OFNI_EDONI_2SFFJ(f)))
4964-#define JFFS2_F_I_RDEV(f) (OFNI_EDONI_2SFFJ(f)->i_rdev)
4965-
4966-#define JFFS2_CLAMP_TIME(t) ((uint32_t)clamp_t(time64_t, (t), 0, U32_MAX))
4967-#define ITIME(sec) ((struct timespec64){sec, 0})
4968-#define JFFS2_NOW() JFFS2_CLAMP_TIME(ktime_get_real_seconds())
4969-#define I_SEC(tv) JFFS2_CLAMP_TIME((tv).tv_sec)
4970-#define JFFS2_F_I_CTIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_ctime)
4971-#define JFFS2_F_I_MTIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_mtime)
4972-#define JFFS2_F_I_ATIME(f) I_SEC(OFNI_EDONI_2SFFJ(f)->i_atime)
4973-#define sleep_on_spinunlock(wq, s)				\
4974-	do {							\
4975-		DECLARE_WAITQUEUE(__wait, current);		\
4976-		add_wait_queue((wq), &__wait);			\
4977-		set_current_state(TASK_UNINTERRUPTIBLE);	\
4978-		spin_unlock(s);					\
4979-		schedule();					\
4980-		remove_wait_queue((wq), &__wait);		\
4981-	} while(0)
4982-
4983-static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
4984-{
4985-	f->highest_version = 0;
4986-	f->fragtree = RB_ROOT;
4987-	f->metadata = NULL;
4988-	f->dents = NULL;
4989-	f->target = NULL;
4990-	f->flags = 0;
4991-	f->usercompr = 0;
4992-}
4993+#define JFFS2_F_I_UID(f) (OFNI_EDONI_2SFFJ(f)->i_uid)
4994+#define JFFS2_F_I_GID(f) (OFNI_EDONI_2SFFJ(f)->i_gid)
4995+#define JFFS2_F_I_CTIME(f) (OFNI_EDONI_2SFFJ(f)->i_ctime)
4996+#define JFFS2_F_I_MTIME(f) (OFNI_EDONI_2SFFJ(f)->i_mtime)
4997+#define JFFS2_F_I_ATIME(f) (OFNI_EDONI_2SFFJ(f)->i_atime)
4998+
4999+#define ITIME(sec) ((struct timespec){sec, 0})
5000+#define I_SEC(tv) ((tv).tv_sec)
5001
5002+#define sleep_on_spinunlock(wq, sl) do {spin_unlock(sl); msleep(100);} while (0)
5003
5004-#define jffs2_is_readonly(c) (OFNI_BS_2SFFJ(c)->s_flags & SB_RDONLY)
5005+#define jffs2_is_readonly(c) (0)
5006
5007 #define SECTOR_ADDR(x) ( (((unsigned long)(x) / c->sector_size) * c->sector_size) )
5008-#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
5009
5010+#ifndef CONFIG_JFFS2_FS_WRITEBUFFER
5011
5012 #ifdef CONFIG_JFFS2_SUMMARY
5013 #define jffs2_can_mark_obsolete(c) (0)
5014@@ -77,10 +75,10 @@ static inline void jffs2_init_inode_info
5015 #define jffs2_write_nand_cleanmarker(c,jeb) (-EIO)
5016
5017 #define jffs2_flash_write(c, ofs, len, retlen, buf) jffs2_flash_direct_write(c, ofs, len, retlen, buf)
5018-#define jffs2_flash_read(c, ofs, len, retlen, buf) (mtd_read((c)->mtd, ofs, len, retlen, buf))
5019-#define jffs2_flush_wbuf_pad(c) ({ do{} while(0); (void)(c), 0; })
5020+#define jffs2_flash_read(c, ofs, len, retlen, buf) jffs2_flash_direct_read(c, ofs, len, retlen, buf)
5021+#define jffs2_flush_wbuf_pad(c) (c=c)
5022 #define jffs2_flush_wbuf_gc(c, i) ({ do{} while(0); (void)(c), (void) i, 0; })
5023-#define jffs2_write_nand_badblock(c,jeb,bad_offset) (1)
5024+#define jffs2_write_nand_badblock(c,jeb,p) (0)
5025 #define jffs2_nand_flash_setup(c) (0)
5026 #define jffs2_nand_flash_cleanup(c) do {} while(0)
5027 #define jffs2_wbuf_dirty(c) (0)
5028@@ -100,7 +98,8 @@ static inline void jffs2_init_inode_info
5029
5030 #else /* NAND and/or ECC'd NOR support present */
5031
5032-#define jffs2_is_writebuffered(c) (c->wbuf != NULL)
5033+/* current not support */
5034+#define jffs2_is_writebuffered(c) (0)
5035
5036 #ifdef CONFIG_JFFS2_SUMMARY
5037 #define jffs2_can_mark_obsolete(c) (0)
5038@@ -142,38 +141,28 @@ void jffs2_dirty_trigger(struct jffs2_sb
5039 #endif /* WRITEBUFFER */
5040
5041 /* background.c */
5042-int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
5043+void jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
5044 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
5045 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
5046
5047 /* dir.c */
5048-extern const struct file_operations jffs2_dir_operations;
5049-extern const struct inode_operations jffs2_dir_inode_operations;
5050-
5051-/* file.c */
5052-extern const struct file_operations jffs2_file_operations;
5053-extern const struct inode_operations jffs2_file_inode_operations;
5054-extern const struct address_space_operations jffs2_file_address_operations;
5055-int jffs2_fsync(struct file *, loff_t, loff_t, int);
5056-int jffs2_do_readpage_unlock(void *data, struct page *pg);
5057-
5058-/* ioctl.c */
5059-long jffs2_ioctl(struct file *, unsigned int, unsigned long);
5060-
5061-/* symlink.c */
5062-extern const struct inode_operations jffs2_symlink_inode_operations;
5063+struct jffs2_inode *jffs2_lookup(struct jffs2_inode *dir_i, const unsigned char *name, int namelen);
5064+int jffs2_create(struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i);
5065+int jffs2_mkdir (struct jffs2_inode *dir_i, const unsigned char *d_name, int mode, struct jffs2_inode **new_i);
5066+int jffs2_link (struct jffs2_inode *old_d_inode, struct jffs2_inode *dir_i, const unsigned char *d_name);
5067+int jffs2_symlink(struct jffs2_inode *dir_i, struct jffs2_inode **d_inode, const unsigned char *d_name, const char *target);
5068+int jffs2_unlink(struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name);
5069+int jffs2_rmdir (struct jffs2_inode *dir_i, struct jffs2_inode *d_inode, const unsigned char *d_name);
5070+int jffs2_rename (struct jffs2_inode *old_dir_i, struct jffs2_inode *d_inode, const unsigned char *old_d_name,
5071+		  struct jffs2_inode *new_dir_i, const unsigned char *new_d_name);
5072+int jffs2_readdir(struct jffs2_inode *inode, off_t *offset, off_t *int_off, struct dirent *ent);
5073
5074 /* fs.c */
5075-int jffs2_setattr (struct dentry *, struct iattr *);
5076-int jffs2_do_setattr (struct inode *, struct iattr *);
5077-struct inode *jffs2_iget(struct super_block *, unsigned long);
5078-void jffs2_evict_inode (struct inode *);
5079-void jffs2_dirty_inode(struct inode *inode, int flags);
5080-struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode,
5081-			       struct jffs2_raw_inode *ri);
5082-int jffs2_statfs (struct dentry *, struct kstatfs *);
5083-int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc);
5084-int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc);
5085+int jffs2_setattr (struct jffs2_inode *inode, struct IATTR *attr);
5086+struct jffs2_inode *jffs2_iget(struct super_block *sb, uint32_t ino);
5087+int jffs2_iput(struct jffs2_inode * i);
5088+struct jffs2_inode *jffs2_new_inode (struct jffs2_inode *dir_i, int mode, struct jffs2_raw_inode *ri);
5089+
5090 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
5091 			    struct jffs2_inode_info *f);
5092 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
5093@@ -183,15 +172,25 @@ unsigned char *jffs2_gc_fetch_page(struc
5094 				   struct jffs2_inode_info *f,
5095 				   unsigned long offset,
5096 				   unsigned long *priv);
5097+void jffs2_gc_release_page(struct jffs2_sb_info *c,
5098+			   unsigned char *pg,
5099+			   unsigned long *priv);
5100 void jffs2_flash_cleanup(struct jffs2_sb_info *c);
5101
5102+int calculate_inocache_hashsize(uint32_t flash_size);
5103
5104 /* writev.c */
5105 int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,
5106 		       unsigned long count, loff_t to, size_t *retlen);
5107 int jffs2_flash_direct_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5108 			size_t *retlen, const u_char *buf);
5109+int jffs2_flash_direct_read(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5110+			size_t *retlen, const char *buf);
5111
5112-#endif /* __JFFS2_OS_LINUX_H__ */
5113+/* super.c */
5114+int jffs2_fill_super(struct super_block *sb);
5115+int jffs2_mount(int part_no, struct jffs2_inode **root_node, unsigned long mountflags);
5116+int jffs2_umount(struct jffs2_inode *root_node);
5117
5118+#endif /* __JFFS2_OS_LINUX_H__ */
5119
5120diff -Nupr old/fs/jffs2/read.c new/fs/jffs2/read.c
5121--- old/fs/jffs2/read.c	2022-05-09 17:22:53.000000000 +0800
5122+++ new/fs/jffs2/read.c	2022-05-09 20:27:15.580000000 +0800
5123@@ -9,16 +9,15 @@
5124  *
5125  */
5126
5127-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5128-
5129 #include <linux/kernel.h>
5130 #include <linux/slab.h>
5131-#include <linux/crc32.h>
5132 #include <linux/pagemap.h>
5133-#include <linux/mtd/mtd.h>
5134 #include <linux/compiler.h>
5135+#include <mtd_dev.h>
5136 #include "nodelist.h"
5137 #include "compr.h"
5138+#include "los_crc32.h"
5139+#include "user_copy.h"
5140
5141 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
5142 		     struct jffs2_full_dnode *fd, unsigned char *buf,
5143@@ -57,6 +56,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5144 	if (crc != je32_to_cpu(ri->node_crc)) {
5145 		pr_warn("Node CRC %08x != calculated CRC %08x for node at %08x\n",
5146 			je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
5147+			jffs2_dbg_dump_node(c, ref_offset(fd->raw));
5148 		ret = -EIO;
5149 		goto out_ri;
5150 	}
5151@@ -75,9 +75,8 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5152 		goto out_ri;
5153 	});
5154
5155-
5156 	if (ri->compr == JFFS2_COMPR_ZERO) {
5157-		memset(buf, 0, len);
5158+		ret = LOS_UserMemClear(buf, len);
5159 		goto out_ri;
5160 	}
5161
5162@@ -88,7 +87,11 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5163 	   Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
5164 	*/
5165 	if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
5166-		readbuf = buf;
5167+		readbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5168+		if (!readbuf) {
5169+			ret = -ENOMEM;
5170+			goto out_ri;
5171+		}
5172 	} else {
5173 		readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
5174 		if (!readbuf) {
5175@@ -97,14 +100,10 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5176 		}
5177 	}
5178 	if (ri->compr != JFFS2_COMPR_NONE) {
5179-		if (len < je32_to_cpu(ri->dsize)) {
5180-			decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5181-			if (!decomprbuf) {
5182-				ret = -ENOMEM;
5183-				goto out_readbuf;
5184-			}
5185-		} else {
5186-			decomprbuf = buf;
5187+		decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
5188+		if (!decomprbuf) {
5189+			ret = -ENOMEM;
5190+			goto out_readbuf;
5191 		}
5192 	} else {
5193 		decomprbuf = readbuf;
5194@@ -113,7 +112,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5195 	jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
5196 		  readbuf);
5197 	ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
5198-			       je32_to_cpu(ri->csize), &readlen, readbuf);
5199+			       je32_to_cpu(ri->csize), &readlen, (char *)readbuf);
5200
5201 	if (!ret && readlen != je32_to_cpu(ri->csize))
5202 		ret = -EIO;
5203@@ -124,6 +123,7 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5204 	if (crc != je32_to_cpu(ri->data_crc)) {
5205 		pr_warn("Data CRC %08x != calculated CRC %08x for node at %08x\n",
5206 			je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
5207+		jffs2_dbg_dump_node(c, ref_offset(fd->raw));
5208 		ret = -EIO;
5209 		goto out_decomprbuf;
5210 	}
5211@@ -139,8 +139,8 @@ int jffs2_read_dnode(struct jffs2_sb_inf
5212 		}
5213 	}
5214
5215-	if (len < je32_to_cpu(ri->dsize)) {
5216-		memcpy(buf, decomprbuf+ofs, len);
5217+	if (LOS_CopyFromKernel(buf, len, decomprbuf + ofs, len) != 0) {
5218+		ret = -EFAULT;
5219 	}
5220  out_decomprbuf:
5221 	if(decomprbuf != buf && decomprbuf != readbuf)
5222@@ -184,7 +184,10 @@ int jffs2_read_inode_range(struct jffs2_
5223 			}
5224 			jffs2_dbg(1, "Filling non-frag hole from %d-%d\n",
5225 				  offset, offset + holesize);
5226-			memset(buf, 0, holesize);
5227+			ret = LOS_UserMemClear(buf, holesize);
5228+			if (ret != 0) {
5229+				return ret;
5230+			}
5231 			buf += holesize;
5232 			offset += holesize;
5233 			continue;
5234@@ -193,7 +196,10 @@ int jffs2_read_inode_range(struct jffs2_
5235 			jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n",
5236 				  offset, holeend, frag->ofs,
5237 				  frag->ofs + frag->size);
5238-			memset(buf, 0, holeend - offset);
5239+			ret = LOS_UserMemClear(buf, holeend - offset);
5240+			if (ret != 0) {
5241+				return ret;
5242+			}
5243 			buf += holeend - offset;
5244 			offset = holeend;
5245 			frag = frag_next(frag);
5246@@ -214,7 +220,7 @@ int jffs2_read_inode_range(struct jffs2_
5247 			if (ret) {
5248 				jffs2_dbg(1, "%s(): error %d\n",
5249 					  __func__, ret);
5250-				memset(buf, 0, readlen);
5251+				(void)LOS_UserMemClear(buf, readlen);
5252 				return ret;
5253 			}
5254 			buf += readlen;
5255@@ -226,3 +232,15 @@ int jffs2_read_inode_range(struct jffs2_
5256 	return 0;
5257 }
5258
5259+int jffs2_flash_direct_read(struct jffs2_sb_info *c, loff_t ofs, size_t len,
5260+			size_t *retlen, const char *buf)
5261+{
5262+	int ret;
5263+	ret = c->mtd->read(c->mtd, ofs, len, (char *)buf);
5264+	if (ret >= 0) {
5265+		*retlen = ret;
5266+		return 0;
5267+	}
5268+	*retlen = 0;
5269+	return ret;
5270+}
5271\ No newline at end of file
5272diff -Nupr old/fs/jffs2/readinode.c new/fs/jffs2/readinode.c
5273--- old/fs/jffs2/readinode.c	2022-05-09 17:22:53.000000000 +0800
5274+++ new/fs/jffs2/readinode.c	2022-05-09 20:26:31.030000000 +0800
5275@@ -9,17 +9,18 @@
5276  *
5277  */
5278
5279-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5280-
5281 #include <linux/kernel.h>
5282 #include <linux/sched.h>
5283 #include <linux/slab.h>
5284 #include <linux/fs.h>
5285-#include <linux/crc32.h>
5286+#include <linux/delay.h>
5287+#include <linux/semaphore.h>
5288 #include <linux/pagemap.h>
5289-#include <linux/mtd/mtd.h>
5290 #include <linux/compiler.h>
5291+#include <mtd_dev.h>
5292 #include "nodelist.h"
5293+#include "os-linux.h"
5294+#include "los_crc32.h"
5295
5296 /*
5297  * Check the data CRC of the node.
5298@@ -31,9 +32,9 @@
5299 static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
5300 {
5301 	struct jffs2_raw_node_ref *ref = tn->fn->raw;
5302-	int err = 0, pointed = 0;
5303+	int err = 0;
5304 	struct jffs2_eraseblock *jeb;
5305-	unsigned char *buffer;
5306+	unsigned char *buffer = NULL;
5307 	uint32_t crc, ofs, len;
5308 	size_t retlen;
5309
5310@@ -61,48 +62,28 @@ static int check_node_data(struct jffs2_
5311 	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",
5312 		ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
5313
5314-#ifndef __ECOS
5315-	/* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
5316-	 * adding and jffs2_flash_read_end() interface. */
5317-	err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
5318-	if (!err && retlen < len) {
5319-		JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
5320-		mtd_unpoint(c->mtd, ofs, retlen);
5321-	} else if (err) {
5322-		if (err != -EOPNOTSUPP)
5323-			JFFS2_WARNING("MTD point failed: error code %d.\n", err);
5324-	} else
5325-		pointed = 1; /* succefully pointed to device */
5326-#endif
5327-
5328-	if (!pointed) {
5329-		buffer = kmalloc(len, GFP_KERNEL);
5330-		if (unlikely(!buffer))
5331-			return -ENOMEM;
5332+	buffer = kmalloc(len, GFP_KERNEL);
5333+	if (unlikely(!buffer))
5334+		return -ENOMEM;
5335
5336-		/* TODO: this is very frequent pattern, make it a separate
5337-		 * routine */
5338-		err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
5339-		if (err) {
5340-			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
5341-			goto free_out;
5342-		}
5343+	/* TODO: this is very frequent pattern, make it a separate
5344+		* routine */
5345+	err = jffs2_flash_read(c, ofs, len, &retlen, (char *)buffer);
5346+	if (err) {
5347+		JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
5348+		goto free_out;
5349+	}
5350
5351-		if (retlen != len) {
5352-			JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
5353-			err = -EIO;
5354-			goto free_out;
5355-		}
5356+	if (retlen != len) {
5357+		JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
5358+		err = -EIO;
5359+		goto free_out;
5360 	}
5361
5362 	/* Continue calculating CRC */
5363 	crc = crc32(tn->partial_crc, buffer, len);
5364-	if(!pointed)
5365-		kfree(buffer);
5366-#ifndef __ECOS
5367-	else
5368-		mtd_unpoint(c->mtd, ofs, len);
5369-#endif
5370+
5371+	kfree(buffer);
5372
5373 	if (crc != tn->data_crc) {
5374 		JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
5375@@ -133,12 +114,7 @@ adj_acc:
5376 	return 0;
5377
5378 free_out:
5379-	if(!pointed)
5380-		kfree(buffer);
5381-#ifndef __ECOS
5382-	else
5383-		mtd_unpoint(c->mtd, ofs, len);
5384-#endif
5385+	kfree(buffer);
5386 	return err;
5387 }
5388
5389@@ -415,8 +391,12 @@ static void eat_last(struct rb_root *roo
5390 		link = &parent->rb_right;
5391
5392 	*link = node->rb_left;
5393-	if (node->rb_left)
5394-		node->rb_left->__rb_parent_color = node->__rb_parent_color;
5395+	if (node->rb_left) {
5396+		node->rb_left->rb_parent_color = node->rb_parent_color;
5397+		// set child parent only
5398+		rb_parent(node->rb_left) = parent;
5399+		node->rb_left = NULL;
5400+	}
5401 }
5402
5403 /* We put the version tree in reverse order, so we can use the same eat_last()
5404@@ -464,8 +444,8 @@ static int jffs2_build_inode_fragtree(st
5405 #ifdef JFFS2_DBG_READINODE_MESSAGES
5406 	this = tn_last(&rii->tn_root);
5407 	while (this) {
5408-		dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
5409-			      this->fn->ofs+this->fn->size, this->overlapped);
5410+		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,
5411+			      this->fn->ofs+this->fn->size, this->overlapped,this->rb.rb_left,this->rb.rb_right,rb_parent(&(this->rb)));
5412 		this = tn_prev(this);
5413 	}
5414 #endif
5415@@ -543,11 +523,13 @@ static int jffs2_build_inode_fragtree(st
5416
5417 static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
5418 {
5419-	struct jffs2_tmp_dnode_info *tn, *next;
5420+	struct jffs2_tmp_dnode_info *tn;
5421+	struct rb_node *rbn,*next;
5422
5423-	rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
5424-			jffs2_free_full_dnode(tn->fn);
5425-			jffs2_free_tmp_dnode_info(tn);
5426+	RB_POSTORDER_FOREACH_SAFE(rbn, linux_root, (struct linux_root *)list, next) {
5427+		tn = (struct jffs2_tmp_dnode_info *)rbn;
5428+		jffs2_free_full_dnode(tn->fn);
5429+		jffs2_free_tmp_dnode_info(tn);
5430 	}
5431
5432 	*list = RB_ROOT;
5433@@ -659,7 +641,7 @@ static inline int read_direntry(struct j
5434 		int already = read - sizeof(*rd);
5435
5436 		err = jffs2_flash_read(c, (ref_offset(ref)) + read,
5437-				rd->nsize - already, &read, &fd->name[already]);
5438+				rd->nsize - already, &read, (char *)&fd->name[already]);
5439 		if (unlikely(read != rd->nsize - already) && likely(!err)) {
5440 			jffs2_free_full_dirent(fd);
5441 			JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
5442@@ -690,7 +672,7 @@ static inline int read_direntry(struct j
5443 #endif
5444 	}
5445
5446-	fd->nhash = full_name_hash(NULL, fd->name, rd->nsize);
5447+	fd->nhash = full_name_hash(fd->name, rd->nsize);
5448 	fd->next = NULL;
5449 	fd->name[rd->nsize] = '\0';
5450
5451@@ -956,7 +938,7 @@ static int read_more(struct jffs2_sb_inf
5452
5453 	dbg_readinode("read more %d bytes\n", to_read);
5454
5455-	err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
5456+	err = jffs2_flash_read(c, offs, to_read, &retlen, (char *)(buf + *rdlen));
5457 	if (err) {
5458 		JFFS2_ERROR("can not read %d bytes from 0x%08x, "
5459 			"error code: %d.\n", to_read, offs, err);
5460@@ -1042,7 +1024,7 @@ static int jffs2_get_inode_nodes(struct
5461 		dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
5462
5463 		/* FIXME: point() */
5464-		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
5465+		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, (char *)buf);
5466 		if (err) {
5467 			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
5468 			goto free_out;
5469@@ -1079,6 +1061,7 @@ static int jffs2_get_inode_nodes(struct
5470
5471 		case JFFS2_NODETYPE_DIRENT:
5472
5473+			dbg_readinode("node at %08x (%d) is a dirent node\n", ref_offset(ref), ref_flags(ref));
5474 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
5475 			    len < sizeof(struct jffs2_raw_dirent)) {
5476 				err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
5477@@ -1094,6 +1077,7 @@ static int jffs2_get_inode_nodes(struct
5478
5479 		case JFFS2_NODETYPE_INODE:
5480
5481+			dbg_readinode("node at %08x (%d) is a data node\n", ref_offset(ref), ref_flags(ref));
5482 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
5483 			    len < sizeof(struct jffs2_raw_inode)) {
5484 				err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
5485@@ -1289,7 +1273,7 @@ static int jffs2_do_read_inode_internal(
5486 			dbg_readinode("symlink's target '%s' cached\n", f->target);
5487 		}
5488
5489-		fallthrough;
5490+		/* fall through... */
5491
5492 	case S_IFBLK:
5493 	case S_IFCHR:
5494@@ -1315,7 +1299,7 @@ static int jffs2_do_read_inode_internal(
5495 		/* OK. We're happy */
5496 		f->metadata = frag_first(&f->fragtree)->node;
5497 		jffs2_free_node_frag(frag_first(&f->fragtree));
5498-		f->fragtree = RB_ROOT;
5499+		f->fragtree.rb_node = NULL;
5500 		break;
5501 	}
5502 	if (f->inocache->state == INO_STATE_READING)
5503@@ -1362,6 +1346,7 @@ int jffs2_do_read_inode(struct jffs2_sb_
5504 			break;
5505
5506 		default:
5507+		    JFFS2_ERROR("Unknown f->inocache->state %d!\n", f->inocache->state);
5508 			BUG();
5509 		}
5510 	}
5511@@ -1375,14 +1360,13 @@ int jffs2_do_read_inode(struct jffs2_sb_
5512 			return -ENOMEM;
5513 		}
5514 		dbg_readinode("creating inocache for root inode\n");
5515-		memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
5516 		f->inocache->ino = f->inocache->pino_nlink = 1;
5517 		f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
5518 		f->inocache->state = INO_STATE_READING;
5519 		jffs2_add_ino_cache(c, f->inocache);
5520 	}
5521 	if (!f->inocache) {
5522-		JFFS2_ERROR("requested to read a nonexistent ino %u\n", ino);
5523+		JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
5524 		return -ENOENT;
5525 	}
5526
5527@@ -1430,6 +1414,11 @@ void jffs2_do_clear_inode(struct jffs2_s
5528
5529 	jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
5530
5531+	if (f->target) {
5532+		kfree(f->target);
5533+		f->target = NULL;
5534+	}
5535+
5536 	fds = f->dents;
5537 	while(fds) {
5538 		fd = fds;
5539diff -Nupr old/fs/jffs2/scan.c new/fs/jffs2/scan.c
5540--- old/fs/jffs2/scan.c	2022-05-09 17:22:53.000000000 +0800
5541+++ new/fs/jffs2/scan.c	2022-05-09 20:23:02.230000000 +0800
5542@@ -9,18 +9,17 @@
5543  *
5544  */
5545
5546-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5547-
5548 #include <linux/kernel.h>
5549 #include <linux/sched.h>
5550 #include <linux/slab.h>
5551-#include <linux/mtd/mtd.h>
5552 #include <linux/pagemap.h>
5553-#include <linux/crc32.h>
5554 #include <linux/compiler.h>
5555 #include "nodelist.h"
5556 #include "summary.h"
5557 #include "debug.h"
5558+#include "mtd_dev.h"
5559+#include "los_typedef.h"
5560+#include "los_crc32.h"
5561
5562 #define DEFAULT_EMPTY_SCAN_SIZE 256
5563
5564@@ -74,7 +73,7 @@ static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
5565 		return ret;
5566 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
5567 		return ret;
5568-	/* Turned wasted size into dirty, since we apparently
5569+	/* Turned wasted size into dirty, since we apparently
5570 	   think it's recoverable now. */
5571 	jeb->dirty_size += jeb->wasted_size;
5572 	c->dirty_size += jeb->wasted_size;
5573@@ -95,40 +94,26 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5574 	unsigned char *flashbuf = NULL;
5575 	uint32_t buf_size = 0;
5576 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
5577-#ifndef __ECOS
5578-	size_t pointlen, try_size;
5579-
5580-	ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
5581-			(void **)&flashbuf, NULL);
5582-	if (!ret && pointlen < c->mtd->size) {
5583-		/* Don't muck about if it won't let us point to the whole flash */
5584-		jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
5585-			  pointlen);
5586-		mtd_unpoint(c->mtd, 0, pointlen);
5587-		flashbuf = NULL;
5588-	}
5589-	if (ret && ret != -EOPNOTSUPP)
5590-		jffs2_dbg(1, "MTD point failed %d\n", ret);
5591-#endif
5592+	struct super_block *sb = NULL;
5593+	struct MtdNorDev *device = NULL;
5594+
5595 	if (!flashbuf) {
5596 		/* For NAND it's quicker to read a whole eraseblock at a time,
5597 		   apparently */
5598 		if (jffs2_cleanmarker_oob(c))
5599-			try_size = c->sector_size;
5600+			buf_size = c->sector_size;
5601 		else
5602-			try_size = PAGE_SIZE;
5603+			buf_size = PAGE_SIZE;
5604
5605 		jffs2_dbg(1, "Trying to allocate readbuf of %zu "
5606-			  "bytes\n", try_size);
5607+			  "bytes\n", buf_size);
5608
5609-		flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
5610+		flashbuf = kmalloc(buf_size, GFP_KERNEL);
5611 		if (!flashbuf)
5612 			return -ENOMEM;
5613
5614 		jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
5615-			  try_size);
5616-
5617-		buf_size = (uint32_t)try_size;
5618+			  buf_size);
5619 	}
5620
5621 	if (jffs2_sum_active()) {
5622@@ -140,7 +125,9 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5623 		}
5624 	}
5625
5626-	for (i=0; i<c->nr_blocks; i++) {
5627+	sb = OFNI_BS_2SFFJ(c);
5628+	device = (struct MtdNorDev*)(sb->s_dev);
5629+	for (i=device->blockStart; i<c->nr_blocks + device->blockStart; i++) {
5630 		struct jffs2_eraseblock *jeb = &c->blocks[i];
5631
5632 		cond_resched();
5633@@ -269,14 +256,10 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5634 			ret = -EIO;
5635 			goto out;
5636 		}
5637-		spin_lock(&c->erase_completion_lock);
5638-		jffs2_garbage_collect_trigger(c);
5639-		spin_unlock(&c->erase_completion_lock);
5640 	}
5641 	ret = 0;
5642  out:
5643-	jffs2_sum_reset_collected(s);
5644-	kfree(s);
5645+	kfree(flashbuf);
5646  out_buf:
5647 	if (buf_size)
5648 		kfree(flashbuf);
5649@@ -413,7 +396,7 @@ static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock
5650 	if (!ref)
5651 		return -ENOMEM;
5652
5653-	/* BEFORE jffs2_build_xattr_subsystem() called,
5654+	/* BEFORE jffs2_build_xattr_subsystem() called,
5655 	 * and AFTER xattr_ref is marked as a dead xref,
5656 	 * ref->xid is used to store 32bit xid, xd is not used
5657 	 * ref->ino is used to store 32bit inode-number, ic is not used
5658@@ -486,10 +469,10 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5659 		struct jffs2_sum_marker *sm;
5660 		void *sumptr = NULL;
5661 		uint32_t sumlen;
5662-
5663+
5664 		if (!buf_size) {
5665 			/* XIP case. Just look, point at the summary if it's there */
5666-			sm = (void *)buf + c->sector_size - sizeof(*sm);
5667+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + c->sector_size - sizeof(*sm));
5668 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5669 				sumptr = buf + je32_to_cpu(sm->offset);
5670 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5671@@ -502,13 +485,13 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5672 				buf_len = sizeof(*sm);
5673
5674 			/* Read as much as we want into the _end_ of the preallocated buffer */
5675-			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5676+			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5677 						  jeb->offset + c->sector_size - buf_len,
5678-						  buf_len);
5679+						  buf_len);
5680 			if (err)
5681 				return err;
5682
5683-			sm = (void *)buf + buf_size - sizeof(*sm);
5684+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + buf_size - sizeof(*sm));
5685 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5686 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5687 				sumptr = buf + buf_size - sumlen;
5688@@ -523,18 +506,15 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5689 					sumptr = kmalloc(sumlen, GFP_KERNEL);
5690 					if (!sumptr)
5691 						return -ENOMEM;
5692-					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5693+					memcpy((uint8_t *)sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5694 				}
5695 				if (buf_len < sumlen) {
5696 					/* Need to read more so that the entire summary node is present */
5697-					err = jffs2_fill_scan_buf(c, sumptr,
5698+					err = jffs2_fill_scan_buf(c, sumptr,
5699 								  jeb->offset + c->sector_size - sumlen,
5700-								  sumlen - buf_len);
5701-					if (err) {
5702-						if (sumlen > buf_size)
5703-							kfree(sumptr);
5704+								  sumlen - buf_len);
5705+					if (err)
5706 						return err;
5707-					}
5708 				}
5709 			}
5710
5711@@ -545,7 +525,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5712
5713 			if (buf_size && sumlen > buf_size)
5714 				kfree(sumptr);
5715-			/* If it returns with a real error, bail.
5716+			/* If it returns with a real error, bail.
5717 			   If it returns positive, that's a block classification
5718 			   (i.e. BLK_STATE_xxx) so return that too.
5719 			   If it returns zero, fall through to full scan. */
5720@@ -607,7 +587,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5721 	/* Now ofs is a complete physical flash offset as it always was... */
5722 	ofs += jeb->offset;
5723
5724-	noise = 10;
5725+	noise = 1;
5726
5727 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
5728
5729@@ -700,7 +680,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5730 				scan_end = buf_len;
5731 				goto more_empty;
5732 			}
5733-
5734+
5735 			/* See how much more there is to read in this eraseblock... */
5736 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
5737 			if (!buf_len) {
5738@@ -950,7 +930,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5739 	jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
5740 		  jeb->offset, jeb->free_size, jeb->dirty_size,
5741 		  jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
5742-
5743+
5744 	/* mark_node_obsolete can add to wasted !! */
5745 	if (jeb->wasted_size) {
5746 		jeb->dirty_size += jeb->wasted_size;
5747@@ -978,7 +958,6 @@ struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uin
5748 		pr_notice("%s(): allocation of inode cache failed\n", __func__);
5749 		return NULL;
5750 	}
5751-	memset(ic, 0, sizeof(*ic));
5752
5753 	ic->ino = ino;
5754 	ic->nodes = (void *)ic;
5755@@ -1069,7 +1048,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5756 	pseudo_random += je32_to_cpu(rd->version);
5757
5758 	/* Should never happen. Did. (OLPC trac #4184)*/
5759-	checkedlen = strnlen(rd->name, rd->nsize);
5760+	checkedlen = strnlen((const char *)rd->name, rd->nsize);
5761 	if (checkedlen < rd->nsize) {
5762 		pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
5763 		       ofs, checkedlen);
5764@@ -1081,7 +1060,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5765 	memcpy(&fd->name, rd->name, checkedlen);
5766 	fd->name[checkedlen] = 0;
5767
5768-	crc = crc32(0, fd->name, checkedlen);
5769+	crc = crc32(0, fd->name, rd->nsize);
5770 	if (crc != je32_to_cpu(rd->name_crc)) {
5771 		pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
5772 			  __func__, ofs, je32_to_cpu(rd->name_crc), crc);
5773@@ -1106,7 +1085,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5774 	fd->next = NULL;
5775 	fd->version = je32_to_cpu(rd->version);
5776 	fd->ino = je32_to_cpu(rd->ino);
5777-	fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
5778+	fd->nhash = full_name_hash(fd->name, checkedlen);
5779 	fd->type = rd->type;
5780 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
5781
5782diff
5783@@ -9,18 +9,17 @@
5784  *
5785  */
5786
5787-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5788-
5789 #include <linux/kernel.h>
5790 #include <linux/sched.h>
5791 #include <linux/slab.h>
5792-#include <linux/mtd/mtd.h>
5793 #include <linux/pagemap.h>
5794-#include <linux/crc32.h>
5795 #include <linux/compiler.h>
5796 #include "nodelist.h"
5797 #include "summary.h"
5798 #include "debug.h"
5799+#include "mtd_dev.h"
5800+#include "los_typedef.h"
5801+#include "los_crc32.h"
5802
5803 #define DEFAULT_EMPTY_SCAN_SIZE 256
5804
5805@@ -74,7 +73,7 @@ static int file_dirty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
5806 		return ret;
5807 	if ((ret = jffs2_scan_dirty_space(c, jeb, jeb->free_size)))
5808 		return ret;
5809-	/* Turned wasted size into dirty, since we apparently
5810+	/* Turned wasted size into dirty, since we apparently
5811 	   think it's recoverable now. */
5812 	jeb->dirty_size += jeb->wasted_size;
5813 	c->dirty_size += jeb->wasted_size;
5814@@ -95,40 +94,26 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5815 	unsigned char *flashbuf = NULL;
5816 	uint32_t buf_size = 0;
5817 	struct jffs2_summary *s = NULL; /* summary info collected by the scan process */
5818-#ifndef __ECOS
5819-	size_t pointlen, try_size;
5820-
5821-	ret = mtd_point(c->mtd, 0, c->mtd->size, &pointlen,
5822-			(void **)&flashbuf, NULL);
5823-	if (!ret && pointlen < c->mtd->size) {
5824-		/* Don't muck about if it won't let us point to the whole flash */
5825-		jffs2_dbg(1, "MTD point returned len too short: 0x%zx\n",
5826-			  pointlen);
5827-		mtd_unpoint(c->mtd, 0, pointlen);
5828-		flashbuf = NULL;
5829-	}
5830-	if (ret && ret != -EOPNOTSUPP)
5831-		jffs2_dbg(1, "MTD point failed %d\n", ret);
5832-#endif
5833+	struct super_block *sb = NULL;
5834+	struct MtdNorDev *device = NULL;
5835+
5836 	if (!flashbuf) {
5837 		/* For NAND it's quicker to read a whole eraseblock at a time,
5838 		   apparently */
5839 		if (jffs2_cleanmarker_oob(c))
5840-			try_size = c->sector_size;
5841+			buf_size = c->sector_size;
5842 		else
5843-			try_size = PAGE_SIZE;
5844+			buf_size = PAGE_SIZE;
5845
5846 		jffs2_dbg(1, "Trying to allocate readbuf of %zu "
5847-			  "bytes\n", try_size);
5848+			  "bytes\n", buf_size);
5849
5850-		flashbuf = mtd_kmalloc_up_to(c->mtd, &try_size);
5851+		flashbuf = kmalloc(buf_size, GFP_KERNEL);
5852 		if (!flashbuf)
5853 			return -ENOMEM;
5854
5855 		jffs2_dbg(1, "Allocated readbuf of %zu bytes\n",
5856-			  try_size);
5857-
5858-		buf_size = (uint32_t)try_size;
5859+			  buf_size);
5860 	}
5861
5862 	if (jffs2_sum_active()) {
5863@@ -140,7 +125,9 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5864 		}
5865 	}
5866
5867-	for (i=0; i<c->nr_blocks; i++) {
5868+	sb = OFNI_BS_2SFFJ(c);
5869+	device = (struct MtdNorDev*)(sb->s_dev);
5870+	for (i=device->blockStart; i<c->nr_blocks + device->blockStart; i++) {
5871 		struct jffs2_eraseblock *jeb = &c->blocks[i];
5872
5873 		cond_resched();
5874@@ -269,14 +256,10 @@ int jffs2_scan_medium(struct jffs2_sb_info *c)
5875 			ret = -EIO;
5876 			goto out;
5877 		}
5878-		spin_lock(&c->erase_completion_lock);
5879-		jffs2_garbage_collect_trigger(c);
5880-		spin_unlock(&c->erase_completion_lock);
5881 	}
5882 	ret = 0;
5883  out:
5884-	jffs2_sum_reset_collected(s);
5885-	kfree(s);
5886+	kfree(flashbuf);
5887  out_buf:
5888 	if (buf_size)
5889 		kfree(flashbuf);
5890@@ -413,7 +396,7 @@ static int jffs2_scan_xref_node(struct jffs2_sb_info *c, struct jffs2_eraseblock
5891 	if (!ref)
5892 		return -ENOMEM;
5893
5894-	/* BEFORE jffs2_build_xattr_subsystem() called,
5895+	/* BEFORE jffs2_build_xattr_subsystem() called,
5896 	 * and AFTER xattr_ref is marked as a dead xref,
5897 	 * ref->xid is used to store 32bit xid, xd is not used
5898 	 * ref->ino is used to store 32bit inode-number, ic is not used
5899@@ -486,10 +469,10 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5900 		struct jffs2_sum_marker *sm;
5901 		void *sumptr = NULL;
5902 		uint32_t sumlen;
5903-
5904+
5905 		if (!buf_size) {
5906 			/* XIP case. Just look, point at the summary if it's there */
5907-			sm = (void *)buf + c->sector_size - sizeof(*sm);
5908+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + c->sector_size - sizeof(*sm));
5909 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5910 				sumptr = buf + je32_to_cpu(sm->offset);
5911 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5912@@ -502,13 +485,13 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5913 				buf_len = sizeof(*sm);
5914
5915 			/* Read as much as we want into the _end_ of the preallocated buffer */
5916-			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5917+			err = jffs2_fill_scan_buf(c, buf + buf_size - buf_len,
5918 						  jeb->offset + c->sector_size - buf_len,
5919-						  buf_len);
5920+						  buf_len);
5921 			if (err)
5922 				return err;
5923
5924-			sm = (void *)buf + buf_size - sizeof(*sm);
5925+			sm = (struct jffs2_sum_marker *)((uint8_t *)buf + buf_size - sizeof(*sm));
5926 			if (je32_to_cpu(sm->magic) == JFFS2_SUM_MAGIC) {
5927 				sumlen = c->sector_size - je32_to_cpu(sm->offset);
5928 				sumptr = buf + buf_size - sumlen;
5929@@ -523,18 +506,15 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5930 					sumptr = kmalloc(sumlen, GFP_KERNEL);
5931 					if (!sumptr)
5932 						return -ENOMEM;
5933-					memcpy(sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5934+					memcpy((uint8_t *)sumptr + sumlen - buf_len, buf + buf_size - buf_len, buf_len);
5935 				}
5936 				if (buf_len < sumlen) {
5937 					/* Need to read more so that the entire summary node is present */
5938-					err = jffs2_fill_scan_buf(c, sumptr,
5939+					err = jffs2_fill_scan_buf(c, sumptr,
5940 								  jeb->offset + c->sector_size - sumlen,
5941-								  sumlen - buf_len);
5942-					if (err) {
5943-						if (sumlen > buf_size)
5944-							kfree(sumptr);
5945+								  sumlen - buf_len);
5946+					if (err)
5947 						return err;
5948-					}
5949 				}
5950 			}
5951
5952@@ -545,7 +525,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5953
5954 			if (buf_size && sumlen > buf_size)
5955 				kfree(sumptr);
5956-			/* If it returns with a real error, bail.
5957+			/* If it returns with a real error, bail.
5958 			   If it returns positive, that's a block classification
5959 			   (i.e. BLK_STATE_xxx) so return that too.
5960 			   If it returns zero, fall through to full scan. */
5961@@ -607,7 +587,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5962 	/* Now ofs is a complete physical flash offset as it always was... */
5963 	ofs += jeb->offset;
5964
5965-	noise = 10;
5966+	noise = 1;
5967
5968 	dbg_summary("no summary found in jeb 0x%08x. Apply original scan.\n",jeb->offset);
5969
5970@@ -700,7 +680,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5971 				scan_end = buf_len;
5972 				goto more_empty;
5973 			}
5974-
5975+
5976 			/* See how much more there is to read in this eraseblock... */
5977 			buf_len = min_t(uint32_t, buf_size, jeb->offset + c->sector_size - ofs);
5978 			if (!buf_len) {
5979@@ -950,7 +930,7 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
5980 	jffs2_dbg(1, "Block at 0x%08x: free 0x%08x, dirty 0x%08x, unchecked 0x%08x, used 0x%08x, wasted 0x%08x\n",
5981 		  jeb->offset, jeb->free_size, jeb->dirty_size,
5982 		  jeb->unchecked_size, jeb->used_size, jeb->wasted_size);
5983-
5984+
5985 	/* mark_node_obsolete can add to wasted !! */
5986 	if (jeb->wasted_size) {
5987 		jeb->dirty_size += jeb->wasted_size;
5988@@ -978,7 +958,6 @@ struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, uin
5989 		pr_notice("%s(): allocation of inode cache failed\n", __func__);
5990 		return NULL;
5991 	}
5992-	memset(ic, 0, sizeof(*ic));
5993
5994 	ic->ino = ino;
5995 	ic->nodes = (void *)ic;
5996@@ -1069,7 +1048,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
5997 	pseudo_random += je32_to_cpu(rd->version);
5998
5999 	/* Should never happen. Did. (OLPC trac #4184)*/
6000-	checkedlen = strnlen(rd->name, rd->nsize);
6001+	checkedlen = strnlen((const char *)rd->name, rd->nsize);
6002 	if (checkedlen < rd->nsize) {
6003 		pr_err("Dirent at %08x has zeroes in name. Truncating to %d chars\n",
6004 		       ofs, checkedlen);
6005@@ -1081,7 +1060,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
6006 	memcpy(&fd->name, rd->name, checkedlen);
6007 	fd->name[checkedlen] = 0;
6008
6009-	crc = crc32(0, fd->name, checkedlen);
6010+	crc = crc32(0, fd->name, rd->nsize);
6011 	if (crc != je32_to_cpu(rd->name_crc)) {
6012 		pr_notice("%s(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
6013 			  __func__, ofs, je32_to_cpu(rd->name_crc), crc);
6014@@ -1106,7 +1085,7 @@ static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblo
6015 	fd->next = NULL;
6016 	fd->version = je32_to_cpu(rd->version);
6017 	fd->ino = je32_to_cpu(rd->ino);
6018-	fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
6019+	fd->nhash = full_name_hash(fd->name, checkedlen);
6020 	fd->type = rd->type;
6021 	jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
6022
6023diff -Nupr old/fs/jffs2/security.c new/fs/jffs2/security.c
6024--- old/fs/jffs2/security.c	2022-05-09 17:15:24.350000000 +0800
6025+++ new/fs/jffs2/security.c	1970-01-01 08:00:00.000000000 +0800
6026@@ -1,72 +0,0 @@
6027-/*
6028- * JFFS2 -- Journalling Flash File System, Version 2.
6029- *
6030- * Copyright © 2006  NEC Corporation
6031- *
6032- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
6033- *
6034- * For licensing information, see the file 'LICENCE' in this directory.
6035- *
6036- */
6037-
6038-#include <linux/kernel.h>
6039-#include <linux/slab.h>
6040-#include <linux/fs.h>
6041-#include <linux/time.h>
6042-#include <linux/pagemap.h>
6043-#include <linux/highmem.h>
6044-#include <linux/crc32.h>
6045-#include <linux/jffs2.h>
6046-#include <linux/xattr.h>
6047-#include <linux/mtd/mtd.h>
6048-#include <linux/security.h>
6049-#include "nodelist.h"
6050-
6051-/* ---- Initial Security Label(s) Attachment callback --- */
6052-static int jffs2_initxattrs(struct inode *inode,
6053-			    const struct xattr *xattr_array, void *fs_info)
6054-{
6055-	const struct xattr *xattr;
6056-	int err = 0;
6057-
6058-	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
6059-		err = do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
6060-					xattr->name, xattr->value,
6061-					xattr->value_len, 0);
6062-		if (err < 0)
6063-			break;
6064-	}
6065-	return err;
6066-}
6067-
6068-/* ---- Initial Security Label(s) Attachment ----------- */
6069-int jffs2_init_security(struct inode *inode, struct inode *dir,
6070-			const struct qstr *qstr)
6071-{
6072-	return security_inode_init_security(inode, dir, qstr,
6073-					    &jffs2_initxattrs, NULL);
6074-}
6075-
6076-/* ---- XATTR Handler for "security.*" ----------------- */
6077-static int jffs2_security_getxattr(const struct xattr_handler *handler,
6078-				   struct dentry *unused, struct inode *inode,
6079-				   const char *name, void *buffer, size_t size)
6080-{
6081-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_SECURITY,
6082-				 name, buffer, size);
6083-}
6084-
6085-static int jffs2_security_setxattr(const struct xattr_handler *handler,
6086-				   struct dentry *unused, struct inode *inode,
6087-				   const char *name, const void *buffer,
6088-				   size_t size, int flags)
6089-{
6090-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_SECURITY,
6091-				 name, buffer, size, flags);
6092-}
6093-
6094-const struct xattr_handler jffs2_security_xattr_handler = {
6095-	.prefix = XATTR_SECURITY_PREFIX,
6096-	.set = jffs2_security_setxattr,
6097-	.get = jffs2_security_getxattr
6098-};
6099diff -Nupr old/fs/jffs2/summary.c new/fs/jffs2/summary.c
6100--- old/fs/jffs2/summary.c	2022-05-09 17:22:53.000000000 +0800
6101+++ new/fs/jffs2/summary.c	2022-05-09 20:13:24.440000000 +0800
6102@@ -10,16 +10,20 @@
6103  * For licensing information, see the file 'LICENCE' in this directory.
6104  *
6105  */
6106+#include "summary.h"
6107
6108+#ifdef CONFIG_JFFS2_SUMMARY
6109+
6110+#ifndef pr_fmt
6111 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6112+#endif
6113
6114 #include <linux/kernel.h>
6115 #include <linux/slab.h>
6116-#include <linux/mtd/mtd.h>
6117+#include <mtd_dev.h>
6118 #include <linux/pagemap.h>
6119-#include <linux/crc32.h>
6120+#include "los_crc32.h"
6121 #include <linux/compiler.h>
6122-#include <linux/vmalloc.h>
6123 #include "nodelist.h"
6124 #include "debug.h"
6125
6126@@ -388,11 +392,25 @@ static int jffs2_sum_process_sum_data(st
6127 {
6128 	struct jffs2_inode_cache *ic;
6129 	struct jffs2_full_dirent *fd;
6130-	void *sp;
6131+	uintptr_t sp;
6132 	int i, ino;
6133 	int err;
6134
6135-	sp = summary->sum;
6136+	sp = (uintptr_t)summary->sum;
6137+
6138+#if 0
6139+	PRINTK("summary: %x %x %d %d %x %x %d %x %x  %p %p\n",
6140+	je16_to_cpu(summary->magic),
6141+	je16_to_cpu(summary->nodetype),
6142+	je32_to_cpu(summary->totlen),
6143+	je32_to_cpu(summary->hdr_crc),
6144+	je32_to_cpu(summary->sum_num),
6145+	je32_to_cpu(summary->cln_mkr),
6146+	je32_to_cpu(summary->padded),
6147+	je32_to_cpu(summary->sum_crc),
6148+	je32_to_cpu(summary->node_crc),
6149+	sp, summary->sum);
6150+#endif
6151
6152 	for (i=0; i<je32_to_cpu(summary->sum_num); i++) {
6153 		dbg_summary("processing summary index %d\n", i);
6154@@ -404,10 +422,12 @@ static int jffs2_sum_process_sum_data(st
6155 		if (err)
6156 			return err;
6157
6158+		//PRINTK("sum type %d \n", je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype));
6159+
6160 		switch (je16_to_cpu(((struct jffs2_sum_unknown_flash *)sp)->nodetype)) {
6161 			case JFFS2_NODETYPE_INODE: {
6162 				struct jffs2_sum_inode_flash *spi;
6163-				spi = sp;
6164+				spi = (struct jffs2_sum_inode_flash *)sp;
6165
6166 				ino = je32_to_cpu(spi->inode);
6167
6168@@ -428,13 +448,29 @@ static int jffs2_sum_process_sum_data(st
6169
6170 				sp += JFFS2_SUMMARY_INODE_SIZE;
6171
6172+				//PRINTK("1 sp + %d %p\n", JFFS2_SUMMARY_INODE_SIZE, sp);
6173+
6174 				break;
6175 			}
6176
6177 			case JFFS2_NODETYPE_DIRENT: {
6178 				struct jffs2_sum_dirent_flash *spd;
6179 				int checkedlen;
6180-				spd = sp;
6181+				spd = (struct jffs2_sum_dirent_flash *)sp;
6182+
6183+
6184+#if 0
6185+					PRINTK("dir: %x %d %d %d %d %d %d %d %d\n",
6186+	je16_to_cpu(spd->nodetype),
6187+	je32_to_cpu(spd->totlen),
6188+	je32_to_cpu(spd->offset),
6189+	je32_to_cpu(spd->pino),
6190+	je32_to_cpu(spd->version),
6191+	je32_to_cpu(spd->ino),
6192+	spd->nsize,
6193+	spd->type,
6194+	spd->name);
6195+#endif
6196
6197 				dbg_summary("Dirent at 0x%08x-0x%08x\n",
6198 					    jeb->offset + je32_to_cpu(spd->offset),
6199@@ -442,7 +478,7 @@ static int jffs2_sum_process_sum_data(st
6200
6201
6202 				/* This should never happen, but https://dev.laptop.org/ticket/4184 */
6203-				checkedlen = strnlen(spd->name, spd->nsize);
6204+				checkedlen = strnlen((const char *)spd->name, spd->nsize);
6205 				if (!checkedlen) {
6206 					pr_err("Dirent at %08x has zero at start of name. Aborting mount.\n",
6207 					       jeb->offset +
6208@@ -463,6 +499,7 @@ static int jffs2_sum_process_sum_data(st
6209
6210 				memcpy(&fd->name, spd->name, checkedlen);
6211 				fd->name[checkedlen] = 0;
6212+				//PRINTK("add %s \n", fd->name);
6213
6214 				ic = jffs2_scan_make_ino_cache(c, je32_to_cpu(spd->pino));
6215 				if (!ic) {
6216@@ -476,15 +513,19 @@ static int jffs2_sum_process_sum_data(st
6217 				fd->next = NULL;
6218 				fd->version = je32_to_cpu(spd->version);
6219 				fd->ino = je32_to_cpu(spd->ino);
6220-				fd->nhash = full_name_hash(NULL, fd->name, checkedlen);
6221+				fd->nhash = full_name_hash((const unsigned char *)fd->name, checkedlen);
6222 				fd->type = spd->type;
6223
6224 				jffs2_add_fd_to_list(c, fd, &ic->scan_dents);
6225
6226 				*pseudo_random += je32_to_cpu(spd->version);
6227
6228+				//PRINTK("2 sp before add %p\n", sp);
6229+
6230 				sp += JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize);
6231
6232+			    //PRINTK("2 sp + %d %p\n", JFFS2_SUMMARY_DIRENT_SIZE(spd->nsize), sp);
6233+
6234 				break;
6235 			}
6236 #ifdef CONFIG_JFFS2_FS_XATTR
6237@@ -493,7 +534,7 @@ static int jffs2_sum_process_sum_data(st
6238 				struct jffs2_sum_xattr_flash *spx;
6239
6240 				spx = (struct jffs2_sum_xattr_flash *)sp;
6241-				dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
6242+				dbg_summary("xattr at %#08x-%#08x (xid=%u, version=%u)\n",
6243 					    jeb->offset + je32_to_cpu(spx->offset),
6244 					    jeb->offset + je32_to_cpu(spx->offset) + je32_to_cpu(spx->totlen),
6245 					    je32_to_cpu(spx->xid), je32_to_cpu(spx->version));
6246@@ -526,7 +567,7 @@ static int jffs2_sum_process_sum_data(st
6247 				spr = (struct jffs2_sum_xref_flash *)sp;
6248 				dbg_summary("xref at %#08x-%#08x\n",
6249 					    jeb->offset + je32_to_cpu(spr->offset),
6250-					    jeb->offset + je32_to_cpu(spr->offset) +
6251+					    jeb->offset + je32_to_cpu(spr->offset) +
6252 					    (uint32_t)PAD(sizeof(struct jffs2_raw_xref)));
6253
6254 				ref = jffs2_alloc_xattr_ref();
6255@@ -679,7 +720,7 @@ static int jffs2_sum_write_data(struct j
6256 	struct jffs2_sum_marker *sm;
6257 	struct kvec vecs[2];
6258 	uint32_t sum_ofs;
6259-	void *wpage;
6260+	uintptr_t wpage;
6261 	int ret;
6262 	size_t retlen;
6263
6264@@ -713,14 +754,14 @@ static int jffs2_sum_write_data(struct j
6265 	isum.padded = cpu_to_je32(c->summary->sum_padded);
6266 	isum.cln_mkr = cpu_to_je32(c->cleanmarker_size);
6267 	isum.sum_num = cpu_to_je32(c->summary->sum_num);
6268-	wpage = c->summary->sum_buf;
6269+	wpage = (uintptr_t)c->summary->sum_buf;
6270
6271 	while (c->summary->sum_num) {
6272 		temp = c->summary->sum_list_head;
6273
6274 		switch (je16_to_cpu(temp->u.nodetype)) {
6275 			case JFFS2_NODETYPE_INODE: {
6276-				struct jffs2_sum_inode_flash *sino_ptr = wpage;
6277+				struct jffs2_sum_inode_flash *sino_ptr = (struct jffs2_sum_inode_flash *)wpage;
6278
6279 				sino_ptr->nodetype = temp->i.nodetype;
6280 				sino_ptr->inode = temp->i.inode;
6281@@ -734,7 +775,7 @@ static int jffs2_sum_write_data(struct j
6282 			}
6283
6284 			case JFFS2_NODETYPE_DIRENT: {
6285-				struct jffs2_sum_dirent_flash *sdrnt_ptr = wpage;
6286+				struct jffs2_sum_dirent_flash *sdrnt_ptr = (struct jffs2_sum_dirent_flash *)wpage;
6287
6288 				sdrnt_ptr->nodetype = temp->d.nodetype;
6289 				sdrnt_ptr->totlen = temp->d.totlen;
6290@@ -802,7 +843,7 @@ static int jffs2_sum_write_data(struct j
6291
6292 	wpage += padsize;
6293
6294-	sm = wpage;
6295+	sm = (struct jffs2_sum_marker *)wpage;
6296 	sm->offset = cpu_to_je32(c->sector_size - jeb->free_size);
6297 	sm->magic = cpu_to_je32(JFFS2_SUM_MAGIC);
6298
6299@@ -847,7 +888,7 @@ static int jffs2_sum_write_data(struct j
6300 /* Write out summary information - called from jffs2_do_reserve_space */
6301
6302 int jffs2_sum_write_sumnode(struct jffs2_sb_info *c)
6303-	__must_hold(&c->erase_completion_block)
6304+	//__must_hold(&c->erase_completion_block)
6305 {
6306 	int datasize, infosize, padsize;
6307 	struct jffs2_eraseblock *jeb;
6308@@ -875,3 +916,5 @@ int jffs2_sum_write_sumnode(struct jffs2
6309 	spin_lock(&c->erase_completion_lock);
6310 	return ret;
6311 }
6312+
6313+#endif
6314diff -Nupr old/fs/jffs2/summary.h new/fs/jffs2/summary.h
6315--- old/fs/jffs2/summary.h	2022-05-09 17:22:53.000000000 +0800
6316+++ new/fs/jffs2/summary.h	2022-05-09 20:35:43.430000000 +0800
6317@@ -19,8 +19,9 @@
6318    anyway. */
6319 #define MAX_SUMMARY_SIZE 65536
6320
6321-#include <linux/uio.h>
6322-#include <linux/jffs2.h>
6323+#include <sys/uio.h>
6324+#include <linux/types.h>
6325+#include "jffs2.h"
6326
6327 #define BLK_STATE_ALLFF		0
6328 #define BLK_STATE_CLEAN		1
6329@@ -169,6 +170,10 @@ struct jffs2_sum_marker
6330
6331 #define JFFS2_SUMMARY_FRAME_SIZE (sizeof(struct jffs2_raw_summary) + sizeof(struct jffs2_sum_marker))
6332
6333+#ifdef LOSCFG_FS_JFFS2_SUMMARY
6334+#define CONFIG_JFFS2_SUMMARY
6335+#endif
6336+
6337 #ifdef CONFIG_JFFS2_SUMMARY	/* SUMMARY SUPPORT ENABLED */
6338
6339 #define jffs2_sum_active() (1)
6340diff -Nupr old/fs/jffs2/super.c new/fs/jffs2/super.c
6341--- old/fs/jffs2/super.c	2022-05-09 17:22:53.000000000 +0800
6342+++ new/fs/jffs2/super.c	2022-05-09 20:09:32.170000000 +0800
6343@@ -9,434 +9,188 @@
6344  *
6345  */
6346
6347-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6348
6349-#include <linux/kernel.h>
6350-#include <linux/module.h>
6351-#include <linux/slab.h>
6352-#include <linux/init.h>
6353-#include <linux/list.h>
6354-#include <linux/fs.h>
6355-#include <linux/err.h>
6356-#include <linux/mount.h>
6357-#include <linux/fs_context.h>
6358-#include <linux/fs_parser.h>
6359-#include <linux/jffs2.h>
6360-#include <linux/pagemap.h>
6361-#include <linux/mtd/super.h>
6362-#include <linux/ctype.h>
6363-#include <linux/namei.h>
6364-#include <linux/seq_file.h>
6365-#include <linux/exportfs.h>
6366-#include "compr.h"
6367+#include "jffs2.h"
6368 #include "nodelist.h"
6369+#include "jffs2_fs_sb.h"
6370+#include "mtd_dev.h"
6371+#include "mtd_partition.h"
6372+#include "compr.h"
6373+#include "jffs2_hash.h"
6374
6375-static void jffs2_put_super(struct super_block *);
6376-
6377-static struct kmem_cache *jffs2_inode_cachep;
6378+static unsigned char jffs2_mounted_number = 0; /* a counter to track the number of jffs2 instances mounted */
6379+struct MtdNorDev jffs2_dev_list[CONFIG_MTD_PATTITION_NUM];
6380
6381-static struct inode *jffs2_alloc_inode(struct super_block *sb)
6382+/*
6383+ * fill in the superblock
6384+ */
6385+int jffs2_fill_super(struct super_block *sb)
6386 {
6387-	struct jffs2_inode_info *f;
6388-
6389-	f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
6390-	if (!f)
6391-		return NULL;
6392-	return &f->vfs_inode;
6393-}
6394+	int ret;
6395+	struct jffs2_sb_info *c;
6396+	struct MtdNorDev *device;
6397
6398-static void jffs2_free_inode(struct inode *inode)
6399-{
6400-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
6401+	c = JFFS2_SB_INFO(sb);
6402+	device = (struct MtdNorDev*)(sb->s_dev);
6403
6404-	kfree(f->target);
6405-	kmem_cache_free(jffs2_inode_cachep, f);
6406-}
6407+	(void)mutex_init(&c->alloc_sem);
6408+	(void)mutex_init(&c->erase_free_sem);
6409+	spin_lock_init(&c->erase_completion_lock);
6410+	spin_lock_init(&c->inocache_lock);
6411
6412-static void jffs2_i_init_once(void *foo)
6413-{
6414-	struct jffs2_inode_info *f = foo;
6415+	/* sector size is the erase block size */
6416+	c->sector_size = device->blockSize;
6417+	c->flash_size  = (device->blockEnd - device->blockStart + 1) * device->blockSize;
6418+	c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
6419
6420-	mutex_init(&f->sem);
6421-	f->target = NULL;
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,1352 +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-int jffs2_build_xattr_subsystem(struct jffs2_sb_info *c)
9447-{
9448-	struct jffs2_xattr_ref *ref, *_ref;
9449-	struct jffs2_xattr_ref **xref_tmphash;
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-	xref_tmphash = kcalloc(XREF_TMPHASH_SIZE,
9459-			       sizeof(struct jffs2_xattr_ref *), GFP_KERNEL);
9460-	if (!xref_tmphash)
9461-		return -ENOMEM;
9462-
9463-	/* Phase.1 : Merge same xref */
9464-	for (ref=c->xref_temp; ref; ref=_ref) {
9465-		struct jffs2_xattr_ref *tmp;
9466-
9467-		_ref = ref->next;
9468-		if (ref_flags(ref->node) != REF_PRISTINE) {
9469-			if (verify_xattr_ref(c, ref)) {
9470-				BUG_ON(ref->node->next_in_ino != (void *)ref);
9471-				ref->node->next_in_ino = NULL;
9472-				jffs2_mark_node_obsolete(c, ref->node);
9473-				jffs2_free_xattr_ref(ref);
9474-				continue;
9475-			}
9476-		}
9477-
9478-		i = (ref->ino ^ ref->xid) % XREF_TMPHASH_SIZE;
9479-		for (tmp=xref_tmphash[i]; tmp; tmp=tmp->next) {
9480-			if (tmp->ino == ref->ino && tmp->xid == ref->xid)
9481-				break;
9482-		}
9483-		if (tmp) {
9484-			raw = ref->node;
9485-			if (ref->xseqno > tmp->xseqno) {
9486-				tmp->xseqno = ref->xseqno;
9487-				raw->next_in_ino = tmp->node;
9488-				tmp->node = raw;
9489-			} else {
9490-				raw->next_in_ino = tmp->node->next_in_ino;
9491-				tmp->node->next_in_ino = raw;
9492-			}
9493-			jffs2_free_xattr_ref(ref);
9494-			continue;
9495-		} else {
9496-			ref->next = xref_tmphash[i];
9497-			xref_tmphash[i] = ref;
9498-		}
9499-	}
9500-	c->xref_temp = NULL;
9501-
9502-	/* Phase.2 : Bind xref with inode_cache and xattr_datum */
9503-	for (i=0; i < XREF_TMPHASH_SIZE; i++) {
9504-		for (ref=xref_tmphash[i]; ref; ref=_ref) {
9505-			xref_count++;
9506-			_ref = ref->next;
9507-			if (is_xattr_ref_dead(ref)) {
9508-				ref->next = c->xref_dead_list;
9509-				c->xref_dead_list = ref;
9510-				xref_dead_count++;
9511-				continue;
9512-			}
9513-			/* At this point, ref->xid and ref->ino contain XID and inode number.
9514-			   ref->xd and ref->ic are not valid yet. */
9515-			xd = jffs2_find_xattr_datum(c, ref->xid);
9516-			ic = jffs2_get_ino_cache(c, ref->ino);
9517-			if (!xd || !ic || !ic->pino_nlink) {
9518-				dbg_xattr("xref(ino=%u, xid=%u, xseqno=%u) is orphan.\n",
9519-					  ref->ino, ref->xid, ref->xseqno);
9520-				ref->xseqno |= XREF_DELETE_MARKER;
9521-				ref->next = c->xref_dead_list;
9522-				c->xref_dead_list = ref;
9523-				xref_orphan_count++;
9524-				continue;
9525-			}
9526-			ref->xd = xd;
9527-			ref->ic = ic;
9528-			atomic_inc(&xd->refcnt);
9529-			ref->next = ic->xref;
9530-			ic->xref = ref;
9531-		}
9532-	}
9533-
9534-	/* Phase.3 : Link unchecked xdatum to xattr_unchecked list */
9535-	for (i=0; i < XATTRINDEX_HASHSIZE; i++) {
9536-		list_for_each_entry_safe(xd, _xd, &c->xattrindex[i], xindex) {
9537-			xdatum_count++;
9538-			list_del_init(&xd->xindex);
9539-			if (!atomic_read(&xd->refcnt)) {
9540-				dbg_xattr("xdatum(xid=%u, version=%u) is orphan.\n",
9541-					  xd->xid, xd->version);
9542-				xd->flags |= JFFS2_XFLAGS_DEAD;
9543-				list_add(&xd->xindex, &c->xattr_unchecked);
9544-				xdatum_orphan_count++;
9545-				continue;
9546-			}
9547-			if (is_xattr_datum_unchecked(c, xd)) {
9548-				dbg_xattr("unchecked xdatum(xid=%u, version=%u)\n",
9549-					  xd->xid, xd->version);
9550-				list_add(&xd->xindex, &c->xattr_unchecked);
9551-				xdatum_unchecked_count++;
9552-			}
9553-		}
9554-	}
9555-	/* build complete */
9556-	JFFS2_NOTICE("complete building xattr subsystem, %u of xdatum"
9557-		     " (%u unchecked, %u orphan) and "
9558-		     "%u of xref (%u dead, %u orphan) found.\n",
9559-		     xdatum_count, xdatum_unchecked_count, xdatum_orphan_count,
9560-		     xref_count, xref_dead_count, xref_orphan_count);
9561-	kfree(xref_tmphash);
9562-	return 0;
9563-}
9564-
9565-struct jffs2_xattr_datum *jffs2_setup_xattr_datum(struct jffs2_sb_info *c,
9566-						  uint32_t xid, uint32_t version)
9567-{
9568-	struct jffs2_xattr_datum *xd;
9569-
9570-	xd = jffs2_find_xattr_datum(c, xid);
9571-	if (!xd) {
9572-		xd = jffs2_alloc_xattr_datum();
9573-		if (!xd)
9574-			return ERR_PTR(-ENOMEM);
9575-		xd->xid = xid;
9576-		xd->version = version;
9577-		if (xd->xid > c->highest_xid)
9578-			c->highest_xid = xd->xid;
9579-		list_add_tail(&xd->xindex, &c->xattrindex[xid % XATTRINDEX_HASHSIZE]);
9580-	}
9581-	return xd;
9582-}
9583-
9584-/* -------- xattr subsystem functions ---------------
9585- * xprefix_to_handler(xprefix)
9586- *   is used to translate xprefix into xattr_handler.
9587- * jffs2_listxattr(dentry, buffer, size)
9588- *   is an implementation of listxattr handler on jffs2.
9589- * do_jffs2_getxattr(inode, xprefix, xname, buffer, size)
9590- *   is an implementation of getxattr handler on jffs2.
9591- * do_jffs2_setxattr(inode, xprefix, xname, buffer, size, flags)
9592- *   is an implementation of setxattr handler on jffs2.
9593- * -------------------------------------------------- */
9594-const struct xattr_handler *jffs2_xattr_handlers[] = {
9595-	&jffs2_user_xattr_handler,
9596-#ifdef CONFIG_JFFS2_FS_SECURITY
9597-	&jffs2_security_xattr_handler,
9598-#endif
9599-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
9600-	&posix_acl_access_xattr_handler,
9601-	&posix_acl_default_xattr_handler,
9602-#endif
9603-	&jffs2_trusted_xattr_handler,
9604-	NULL
9605-};
9606-
9607-static const struct xattr_handler *xprefix_to_handler(int xprefix) {
9608-	const struct xattr_handler *ret;
9609-
9610-	switch (xprefix) {
9611-	case JFFS2_XPREFIX_USER:
9612-		ret = &jffs2_user_xattr_handler;
9613-		break;
9614-#ifdef CONFIG_JFFS2_FS_SECURITY
9615-	case JFFS2_XPREFIX_SECURITY:
9616-		ret = &jffs2_security_xattr_handler;
9617-		break;
9618-#endif
9619-#ifdef CONFIG_JFFS2_FS_POSIX_ACL
9620-	case JFFS2_XPREFIX_ACL_ACCESS:
9621-		ret = &posix_acl_access_xattr_handler;
9622-		break;
9623-	case JFFS2_XPREFIX_ACL_DEFAULT:
9624-		ret = &posix_acl_default_xattr_handler;
9625-		break;
9626-#endif
9627-	case JFFS2_XPREFIX_TRUSTED:
9628-		ret = &jffs2_trusted_xattr_handler;
9629-		break;
9630-	default:
9631-		ret = NULL;
9632-		break;
9633-	}
9634-	return ret;
9635-}
9636-
9637-ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
9638-{
9639-	struct inode *inode = d_inode(dentry);
9640-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9641-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9642-	struct jffs2_inode_cache *ic = f->inocache;
9643-	struct jffs2_xattr_ref *ref, **pref;
9644-	struct jffs2_xattr_datum *xd;
9645-	const struct xattr_handler *xhandle;
9646-	const char *prefix;
9647-	ssize_t prefix_len, len, rc;
9648-	int retry = 0;
9649-
9650-	rc = check_xattr_ref_inode(c, ic);
9651-	if (unlikely(rc))
9652-		return rc;
9653-
9654-	down_read(&c->xattr_sem);
9655- retry:
9656-	len = 0;
9657-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9658-		BUG_ON(ref->ic != ic);
9659-		xd = ref->xd;
9660-		if (!xd->xname) {
9661-			/* xdatum is unchached */
9662-			if (!retry) {
9663-				retry = 1;
9664-				up_read(&c->xattr_sem);
9665-				down_write(&c->xattr_sem);
9666-				goto retry;
9667-			} else {
9668-				rc = load_xattr_datum(c, xd);
9669-				if (unlikely(rc > 0)) {
9670-					*pref = ref->next;
9671-					delete_xattr_ref(c, ref);
9672-					goto retry;
9673-				} else if (unlikely(rc < 0))
9674-					goto out;
9675-			}
9676-		}
9677-		xhandle = xprefix_to_handler(xd->xprefix);
9678-		if (!xhandle || (xhandle->list && !xhandle->list(dentry)))
9679-			continue;
9680-		prefix = xhandle->prefix ?: xhandle->name;
9681-		prefix_len = strlen(prefix);
9682-		rc = prefix_len + xd->name_len + 1;
9683-
9684-		if (buffer) {
9685-			if (rc > size - len) {
9686-				rc = -ERANGE;
9687-				goto out;
9688-			}
9689-			memcpy(buffer, prefix, prefix_len);
9690-			buffer += prefix_len;
9691-			memcpy(buffer, xd->xname, xd->name_len);
9692-			buffer += xd->name_len;
9693-			*buffer++ = 0;
9694-		}
9695-		len += rc;
9696-	}
9697-	rc = len;
9698- out:
9699-	if (!retry) {
9700-		up_read(&c->xattr_sem);
9701-	} else {
9702-		up_write(&c->xattr_sem);
9703-	}
9704-	return rc;
9705-}
9706-
9707-int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
9708-		      char *buffer, size_t size)
9709-{
9710-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9711-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9712-	struct jffs2_inode_cache *ic = f->inocache;
9713-	struct jffs2_xattr_datum *xd;
9714-	struct jffs2_xattr_ref *ref, **pref;
9715-	int rc, retry = 0;
9716-
9717-	rc = check_xattr_ref_inode(c, ic);
9718-	if (unlikely(rc))
9719-		return rc;
9720-
9721-	down_read(&c->xattr_sem);
9722- retry:
9723-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9724-		BUG_ON(ref->ic!=ic);
9725-
9726-		xd = ref->xd;
9727-		if (xd->xprefix != xprefix)
9728-			continue;
9729-		if (!xd->xname) {
9730-			/* xdatum is unchached */
9731-			if (!retry) {
9732-				retry = 1;
9733-				up_read(&c->xattr_sem);
9734-				down_write(&c->xattr_sem);
9735-				goto retry;
9736-			} else {
9737-				rc = load_xattr_datum(c, xd);
9738-				if (unlikely(rc > 0)) {
9739-					*pref = ref->next;
9740-					delete_xattr_ref(c, ref);
9741-					goto retry;
9742-				} else if (unlikely(rc < 0)) {
9743-					goto out;
9744-				}
9745-			}
9746-		}
9747-		if (!strcmp(xname, xd->xname)) {
9748-			rc = xd->value_len;
9749-			if (buffer) {
9750-				if (size < rc) {
9751-					rc = -ERANGE;
9752-				} else {
9753-					memcpy(buffer, xd->xvalue, rc);
9754-				}
9755-			}
9756-			goto out;
9757-		}
9758-	}
9759-	rc = -ENODATA;
9760- out:
9761-	if (!retry) {
9762-		up_read(&c->xattr_sem);
9763-	} else {
9764-		up_write(&c->xattr_sem);
9765-	}
9766-	return rc;
9767-}
9768-
9769-int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
9770-		      const char *buffer, size_t size, int flags)
9771-{
9772-	struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
9773-	struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
9774-	struct jffs2_inode_cache *ic = f->inocache;
9775-	struct jffs2_xattr_datum *xd;
9776-	struct jffs2_xattr_ref *ref, *newref, **pref;
9777-	uint32_t length, request;
9778-	int rc;
9779-
9780-	rc = check_xattr_ref_inode(c, ic);
9781-	if (unlikely(rc))
9782-		return rc;
9783-
9784-	request = PAD(sizeof(struct jffs2_raw_xattr) + strlen(xname) + 1 + size);
9785-	rc = jffs2_reserve_space(c, request, &length,
9786-				 ALLOC_NORMAL, JFFS2_SUMMARY_XATTR_SIZE);
9787-	if (rc) {
9788-		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
9789-		return rc;
9790-	}
9791-
9792-	/* Find existing xattr */
9793-	down_write(&c->xattr_sem);
9794- retry:
9795-	for (ref=ic->xref, pref=&ic->xref; ref; pref=&ref->next, ref=ref->next) {
9796-		xd = ref->xd;
9797-		if (xd->xprefix != xprefix)
9798-			continue;
9799-		if (!xd->xname) {
9800-			rc = load_xattr_datum(c, xd);
9801-			if (unlikely(rc > 0)) {
9802-				*pref = ref->next;
9803-				delete_xattr_ref(c, ref);
9804-				goto retry;
9805-			} else if (unlikely(rc < 0))
9806-				goto out;
9807-		}
9808-		if (!strcmp(xd->xname, xname)) {
9809-			if (flags & XATTR_CREATE) {
9810-				rc = -EEXIST;
9811-				goto out;
9812-			}
9813-			if (!buffer) {
9814-				ref->ino = ic->ino;
9815-				ref->xid = xd->xid;
9816-				ref->xseqno |= XREF_DELETE_MARKER;
9817-				rc = save_xattr_ref(c, ref);
9818-				if (!rc) {
9819-					*pref = ref->next;
9820-					spin_lock(&c->erase_completion_lock);
9821-					ref->next = c->xref_dead_list;
9822-					c->xref_dead_list = ref;
9823-					spin_unlock(&c->erase_completion_lock);
9824-					unrefer_xattr_datum(c, xd);
9825-				} else {
9826-					ref->ic = ic;
9827-					ref->xd = xd;
9828-					ref->xseqno &= ~XREF_DELETE_MARKER;
9829-				}
9830-				goto out;
9831-			}
9832-			goto found;
9833-		}
9834-	}
9835-	/* not found */
9836-	if (flags & XATTR_REPLACE) {
9837-		rc = -ENODATA;
9838-		goto out;
9839-	}
9840-	if (!buffer) {
9841-		rc = -ENODATA;
9842-		goto out;
9843-	}
9844- found:
9845-	xd = create_xattr_datum(c, xprefix, xname, buffer, size);
9846-	if (IS_ERR(xd)) {
9847-		rc = PTR_ERR(xd);
9848-		goto out;
9849-	}
9850-	up_write(&c->xattr_sem);
9851-	jffs2_complete_reservation(c);
9852-
9853-	/* create xattr_ref */
9854-	request = PAD(sizeof(struct jffs2_raw_xref));
9855-	rc = jffs2_reserve_space(c, request, &length,
9856-				 ALLOC_NORMAL, JFFS2_SUMMARY_XREF_SIZE);
9857-	down_write(&c->xattr_sem);
9858-	if (rc) {
9859-		JFFS2_WARNING("jffs2_reserve_space()=%d, request=%u\n", rc, request);
9860-		unrefer_xattr_datum(c, xd);
9861-		up_write(&c->xattr_sem);
9862-		return rc;
9863-	}
9864-	if (ref)
9865-		*pref = ref->next;
9866-	newref = create_xattr_ref(c, ic, xd);
9867-	if (IS_ERR(newref)) {
9868-		if (ref) {
9869-			ref->next = ic->xref;
9870-			ic->xref = ref;
9871-		}
9872-		rc = PTR_ERR(newref);
9873-		unrefer_xattr_datum(c, xd);
9874-	} else if (ref) {
9875-		delete_xattr_ref(c, ref);
9876-	}
9877- out:
9878-	up_write(&c->xattr_sem);
9879-	jffs2_complete_reservation(c);
9880-	return rc;
9881-}
9882-
9883-/* -------- garbage collector functions -------------
9884- * jffs2_garbage_collect_xattr_datum(c, xd, raw)
9885- *   is used to move xdatum into new node.
9886- * jffs2_garbage_collect_xattr_ref(c, ref, raw)
9887- *   is used to move xref into new node.
9888- * jffs2_verify_xattr(c)
9889- *   is used to call do_verify_xattr_datum() before garbage collecting.
9890- * jffs2_release_xattr_datum(c, xd)
9891- *   is used to release an in-memory object of xdatum.
9892- * jffs2_release_xattr_ref(c, ref)
9893- *   is used to release an in-memory object of xref.
9894- * -------------------------------------------------- */
9895-int jffs2_garbage_collect_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd,
9896-				      struct jffs2_raw_node_ref *raw)
9897-{
9898-	uint32_t totlen, length, old_ofs;
9899-	int rc = 0;
9900-
9901-	down_write(&c->xattr_sem);
9902-	if (xd->node != raw)
9903-		goto out;
9904-	if (xd->flags & (JFFS2_XFLAGS_DEAD|JFFS2_XFLAGS_INVALID))
9905-		goto out;
9906-
9907-	rc = load_xattr_datum(c, xd);
9908-	if (unlikely(rc)) {
9909-		rc = (rc > 0) ? 0 : rc;
9910-		goto out;
9911-	}
9912-	old_ofs = ref_offset(xd->node);
9913-	totlen = PAD(sizeof(struct jffs2_raw_xattr)
9914-			+ xd->name_len + 1 + xd->value_len);
9915-	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XATTR_SIZE);
9916-	if (rc) {
9917-		JFFS2_WARNING("jffs2_reserve_space_gc()=%d, request=%u\n", rc, totlen);
9918-		goto out;
9919-	}
9920-	rc = save_xattr_datum(c, xd);
9921-	if (!rc)
9922-		dbg_xattr("xdatum (xid=%u, version=%u) GC'ed from %#08x to %08x\n",
9923-			  xd->xid, xd->version, old_ofs, ref_offset(xd->node));
9924- out:
9925-	if (!rc)
9926-		jffs2_mark_node_obsolete(c, raw);
9927-	up_write(&c->xattr_sem);
9928-	return rc;
9929-}
9930-
9931-int jffs2_garbage_collect_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref,
9932-				    struct jffs2_raw_node_ref *raw)
9933-{
9934-	uint32_t totlen, length, old_ofs;
9935-	int rc = 0;
9936-
9937-	down_write(&c->xattr_sem);
9938-	BUG_ON(!ref->node);
9939-
9940-	if (ref->node != raw)
9941-		goto out;
9942-	if (is_xattr_ref_dead(ref) && (raw->next_in_ino == (void *)ref))
9943-		goto out;
9944-
9945-	old_ofs = ref_offset(ref->node);
9946-	totlen = ref_totlen(c, c->gcblock, ref->node);
9947-
9948-	rc = jffs2_reserve_space_gc(c, totlen, &length, JFFS2_SUMMARY_XREF_SIZE);
9949-	if (rc) {
9950-		JFFS2_WARNING("%s: jffs2_reserve_space_gc() = %d, request = %u\n",
9951-			      __func__, rc, totlen);
9952-		goto out;
9953-	}
9954-	rc = save_xattr_ref(c, ref);
9955-	if (!rc)
9956-		dbg_xattr("xref (ino=%u, xid=%u) GC'ed from %#08x to %08x\n",
9957-			  ref->ic->ino, ref->xd->xid, old_ofs, ref_offset(ref->node));
9958- out:
9959-	if (!rc)
9960-		jffs2_mark_node_obsolete(c, raw);
9961-	up_write(&c->xattr_sem);
9962-	return rc;
9963-}
9964-
9965-int jffs2_verify_xattr(struct jffs2_sb_info *c)
9966-{
9967-	struct jffs2_xattr_datum *xd, *_xd;
9968-	struct jffs2_eraseblock *jeb;
9969-	struct jffs2_raw_node_ref *raw;
9970-	uint32_t totlen;
9971-	int rc;
9972-
9973-	down_write(&c->xattr_sem);
9974-	list_for_each_entry_safe(xd, _xd, &c->xattr_unchecked, xindex) {
9975-		rc = do_verify_xattr_datum(c, xd);
9976-		if (rc < 0)
9977-			continue;
9978-		list_del_init(&xd->xindex);
9979-		spin_lock(&c->erase_completion_lock);
9980-		for (raw=xd->node; raw != (void *)xd; raw=raw->next_in_ino) {
9981-			if (ref_flags(raw) != REF_UNCHECKED)
9982-				continue;
9983-			jeb = &c->blocks[ref_offset(raw) / c->sector_size];
9984-			totlen = PAD(ref_totlen(c, jeb, raw));
9985-			c->unchecked_size -= totlen; c->used_size += totlen;
9986-			jeb->unchecked_size -= totlen; jeb->used_size += totlen;
9987-			raw->flash_offset = ref_offset(raw)
9988-				| ((xd->node == (void *)raw) ? REF_PRISTINE : REF_NORMAL);
9989-		}
9990-		if (xd->flags & JFFS2_XFLAGS_DEAD)
9991-			list_add(&xd->xindex, &c->xattr_dead_list);
9992-		spin_unlock(&c->erase_completion_lock);
9993-	}
9994-	up_write(&c->xattr_sem);
9995-	return list_empty(&c->xattr_unchecked) ? 1 : 0;
9996-}
9997-
9998-void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
9999-{
10000-	/* must be called under spin_lock(&c->erase_completion_lock) */
10001-	if (atomic_read(&xd->refcnt) || xd->node != (void *)xd)
10002-		return;
10003-
10004-	list_del(&xd->xindex);
10005-	jffs2_free_xattr_datum(xd);
10006-}
10007-
10008-void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref)
10009-{
10010-	/* must be called under spin_lock(&c->erase_completion_lock) */
10011-	struct jffs2_xattr_ref *tmp, **ptmp;
10012-
10013-	if (ref->node != (void *)ref)
10014-		return;
10015-
10016-	for (tmp=c->xref_dead_list, ptmp=&c->xref_dead_list; tmp; ptmp=&tmp->next, tmp=tmp->next) {
10017-		if (ref == tmp) {
10018-			*ptmp = tmp->next;
10019-			break;
10020-		}
10021-	}
10022-	jffs2_free_xattr_ref(ref);
10023-}
10024diff -Nupr old/fs/jffs2/xattr.h new/fs/jffs2/xattr.h
10025--- old/fs/jffs2/xattr.h	2022-05-09 17:22:53.000000000 +0800
10026+++ new/fs/jffs2/xattr.h	2022-05-09 20:04:55.580000000 +0800
10027@@ -12,7 +12,6 @@
10028 #ifndef _JFFS2_FS_XATTR_H_
10029 #define _JFFS2_FS_XATTR_H_
10030
10031-#include <linux/xattr.h>
10032 #include <linux/list.h>
10033
10034 #define JFFS2_XFLAGS_HOT	(0x01)	/* This datum is HOT */
10035@@ -48,7 +47,7 @@ struct jffs2_xattr_ref
10036 	struct jffs2_raw_node_ref *node;
10037 	uint8_t class;
10038 	uint8_t flags;		/* Currently unused */
10039-	u16 unused;
10040+	uint16_t unused;
10041
10042 	uint32_t xseqno;
10043 	union {
10044@@ -89,16 +88,14 @@ extern int jffs2_verify_xattr(struct jff
10045 extern void jffs2_release_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd);
10046 extern void jffs2_release_xattr_ref(struct jffs2_sb_info *c, struct jffs2_xattr_ref *ref);
10047
10048-extern int do_jffs2_getxattr(struct inode *inode, int xprefix, const char *xname,
10049-			     char *buffer, size_t size);
10050-extern int do_jffs2_setxattr(struct inode *inode, int xprefix, const char *xname,
10051-			     const char *buffer, size_t size, int flags);
10052-
10053 extern const struct xattr_handler *jffs2_xattr_handlers[];
10054 extern const struct xattr_handler jffs2_user_xattr_handler;
10055 extern const struct xattr_handler jffs2_trusted_xattr_handler;
10056
10057 extern ssize_t jffs2_listxattr(struct dentry *, char *, size_t);
10058+#define jffs2_getxattr		generic_getxattr
10059+#define jffs2_setxattr		generic_setxattr
10060+#define jffs2_removexattr	generic_removexattr
10061
10062 #else
10063
10064@@ -113,12 +110,13 @@ extern ssize_t jffs2_listxattr(struct de
10065
10066 #define jffs2_xattr_handlers	NULL
10067 #define jffs2_listxattr		NULL
10068+#define jffs2_getxattr		NULL
10069+#define jffs2_setxattr		NULL
10070+#define jffs2_removexattr	NULL
10071
10072 #endif /* CONFIG_JFFS2_FS_XATTR */
10073
10074 #ifdef CONFIG_JFFS2_FS_SECURITY
10075-extern int jffs2_init_security(struct inode *inode, struct inode *dir,
10076-			       const struct qstr *qstr);
10077 extern const struct xattr_handler jffs2_security_xattr_handler;
10078 #else
10079 #define jffs2_init_security(inode,dir,qstr)	(0)
10080diff -Nupr old/fs/jffs2/xattr_trusted.c new/fs/jffs2/xattr_trusted.c
10081--- old/fs/jffs2/xattr_trusted.c	2022-05-09 17:15:24.360000000 +0800
10082+++ new/fs/jffs2/xattr_trusted.c	1970-01-01 08:00:00.000000000 +0800
10083@@ -1,46 +0,0 @@
10084-/*
10085- * JFFS2 -- Journalling Flash File System, Version 2.
10086- *
10087- * Copyright © 2006  NEC Corporation
10088- *
10089- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
10090- *
10091- * For licensing information, see the file 'LICENCE' in this directory.
10092- *
10093- */
10094-
10095-#include <linux/kernel.h>
10096-#include <linux/fs.h>
10097-#include <linux/jffs2.h>
10098-#include <linux/xattr.h>
10099-#include <linux/mtd/mtd.h>
10100-#include "nodelist.h"
10101-
10102-static int jffs2_trusted_getxattr(const struct xattr_handler *handler,
10103-				  struct dentry *unused, struct inode *inode,
10104-				  const char *name, void *buffer, size_t size)
10105-{
10106-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_TRUSTED,
10107-				 name, buffer, size);
10108-}
10109-
10110-static int jffs2_trusted_setxattr(const struct xattr_handler *handler,
10111-				  struct dentry *unused, struct inode *inode,
10112-				  const char *name, const void *buffer,
10113-				  size_t size, int flags)
10114-{
10115-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_TRUSTED,
10116-				 name, buffer, size, flags);
10117-}
10118-
10119-static bool jffs2_trusted_listxattr(struct dentry *dentry)
10120-{
10121-	return capable(CAP_SYS_ADMIN);
10122-}
10123-
10124-const struct xattr_handler jffs2_trusted_xattr_handler = {
10125-	.prefix = XATTR_TRUSTED_PREFIX,
10126-	.list = jffs2_trusted_listxattr,
10127-	.set = jffs2_trusted_setxattr,
10128-	.get = jffs2_trusted_getxattr
10129-};
10130diff -Nupr old/fs/jffs2/xattr_user.c new/fs/jffs2/xattr_user.c
10131--- old/fs/jffs2/xattr_user.c	2022-05-09 17:15:24.360000000 +0800
10132+++ new/fs/jffs2/xattr_user.c	1970-01-01 08:00:00.000000000 +0800
10133@@ -1,40 +0,0 @@
10134-/*
10135- * JFFS2 -- Journalling Flash File System, Version 2.
10136- *
10137- * Copyright © 2006  NEC Corporation
10138- *
10139- * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
10140- *
10141- * For licensing information, see the file 'LICENCE' in this directory.
10142- *
10143- */
10144-
10145-#include <linux/kernel.h>
10146-#include <linux/fs.h>
10147-#include <linux/jffs2.h>
10148-#include <linux/xattr.h>
10149-#include <linux/mtd/mtd.h>
10150-#include "nodelist.h"
10151-
10152-static int jffs2_user_getxattr(const struct xattr_handler *handler,
10153-			       struct dentry *unused, struct inode *inode,
10154-			       const char *name, void *buffer, size_t size)
10155-{
10156-	return do_jffs2_getxattr(inode, JFFS2_XPREFIX_USER,
10157-				 name, buffer, size);
10158-}
10159-
10160-static int jffs2_user_setxattr(const struct xattr_handler *handler,
10161-			       struct dentry *unused, struct inode *inode,
10162-			       const char *name, const void *buffer,
10163-			       size_t size, int flags)
10164-{
10165-	return do_jffs2_setxattr(inode, JFFS2_XPREFIX_USER,
10166-				 name, buffer, size, flags);
10167-}
10168-
10169-const struct xattr_handler jffs2_user_xattr_handler = {
10170-	.prefix = XATTR_USER_PREFIX,
10171-	.set = jffs2_user_setxattr,
10172-	.get = jffs2_user_getxattr
10173-};
10174