^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * JFFS2 -- Journalling Flash File System, Version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright © 2001-2007 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Created by David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * For licensing information, see the file 'LICENCE' in this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mm.h> /* kvfree() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "nodelist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct jffs2_inode_cache *, struct jffs2_full_dirent **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static inline struct jffs2_inode_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) first_inode_chain(int *i, struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (; *i < c->inocache_hashsize; (*i)++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (c->inocache_list[*i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return c->inocache_list[*i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static inline struct jffs2_inode_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) next_inode(int *i, struct jffs2_inode_cache *ic, struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* More in this chain? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (ic->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return ic->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) (*i)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return first_inode_chain(i, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define for_each_inode(i, c, ic) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (i = 0, ic = first_inode_chain(&i, (c)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ic; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ic = next_inode(&i, ic, (c)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static void jffs2_build_inode_pass1(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct jffs2_inode_cache *ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int *dir_hardlinks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) dbg_fsbuild("building directory inode #%u\n", ic->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* For each child, increase nlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) for(fd = ic->scan_dents; fd; fd = fd->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct jffs2_inode_cache *child_ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!fd->ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* we can get high latency here with huge directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) child_ic = jffs2_get_ino_cache(c, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!child_ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dbg_fsbuild("child \"%s\" (ino #%u) of dir ino #%u doesn't exist!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fd->name, fd->ino, ic->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) jffs2_mark_node_obsolete(c, fd->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Clear the ic/raw union so it doesn't cause problems later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fd->ic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* From this point, fd->raw is no longer used so we can set fd->ic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) fd->ic = child_ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) child_ic->pino_nlink++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* If we appear (at this stage) to have hard-linked directories,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * set a flag to trigger a scan later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (fd->type == DT_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) child_ic->flags |= INO_FLAGS_IS_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (child_ic->pino_nlink > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *dir_hardlinks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dbg_fsbuild("increased nlink for child \"%s\" (ino #%u)\n", fd->name, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* Can't free scan_dents so far. We might need them in pass 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* Scan plan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) - Scan physical nodes. Build map of inodes/dirents. Allocate inocaches as we go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) - Scan directory tree from top down, setting nlink in inocaches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) - Scan inocaches for inodes with nlink==0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static int jffs2_build_filesystem(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int ret, i, dir_hardlinks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct jffs2_inode_cache *ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct jffs2_full_dirent *dead_fds = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dbg_fsbuild("build FS data structures\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* First, scan the medium and build all the inode caches with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) lists of physical nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) c->flags |= JFFS2_SB_FLAG_SCANNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ret = jffs2_scan_medium(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) c->flags &= ~JFFS2_SB_FLAG_SCANNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dbg_fsbuild("scanned flash completely\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) jffs2_dbg_dump_block_lists_nolock(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) dbg_fsbuild("pass 1 starting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) c->flags |= JFFS2_SB_FLAG_BUILDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Now scan the directory tree, increasing nlink according to every dirent found. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) for_each_inode(i, c, ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (ic->scan_dents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) jffs2_build_inode_pass1(c, ic, &dir_hardlinks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) dbg_fsbuild("pass 1 complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Next, scan for inodes with nlink == 0 and remove them. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) they were directories, then decrement the nlink of their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) children too, and repeat the scan. As that's going to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) a fairly uncommon occurrence, it's not so evil to do it this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) way. Recursion bad. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dbg_fsbuild("pass 2 starting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) for_each_inode(i, c, ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (ic->pino_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dbg_fsbuild("pass 2a starting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) while (dead_fds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) fd = dead_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dead_fds = fd->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ic = jffs2_get_ino_cache(c, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) dbg_fsbuild("pass 2a complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (dir_hardlinks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* If we detected directory hardlinks earlier, *hopefully*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * they are gone now because some of the links were from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * dead directories which still had some old dirents lying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * around and not yet garbage-collected, but which have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * been discarded above. So clear the pino_nlink field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * in each directory, so that the final scan below can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * print appropriate warnings. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) for_each_inode(i, c, ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (ic->flags & INO_FLAGS_IS_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ic->pino_nlink = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dbg_fsbuild("freeing temporary data structures\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Finally, we can scan again and free the dirent structs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) for_each_inode(i, c, ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) while(ic->scan_dents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) fd = ic->scan_dents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ic->scan_dents = fd->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* We do use the pino_nlink field to count nlink of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * directories during fs build, so set it to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * parent ino# now. Now that there's hopefully only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (fd->type == DT_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!fd->ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* We'll have complained about it and marked the coresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) raw node obsolete already. Just skip it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* We *have* to have set this in jffs2_build_inode_pass1() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) BUG_ON(!(fd->ic->flags & INO_FLAGS_IS_DIR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* We clear ic->pino_nlink ∀ directories' ic *only* if dir_hardlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * is set. Otherwise, we know this should never trigger anyway, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * we don't do the check. And ic->pino_nlink still contains the nlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * value (which is 1). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (dir_hardlinks && fd->ic->pino_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) JFFS2_ERROR("child dir \"%s\" (ino #%u) of dir ino #%u is also hard linked from dir ino #%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) fd->name, fd->ino, ic->ino, fd->ic->pino_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* Should we unlink it from its previous parent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* For directories, ic->pino_nlink holds that parent inode # */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) fd->ic->pino_nlink = ic->ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ic->scan_dents = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) jffs2_build_xattr_subsystem(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) c->flags &= ~JFFS2_SB_FLAG_BUILDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) dbg_fsbuild("FS build complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* Rotate the lists by some number to ensure wear levelling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) jffs2_rotate_lists(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) for_each_inode(i, c, ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) while(ic->scan_dents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) fd = ic->scan_dents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ic->scan_dents = fd->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) jffs2_clear_xattr_subsystem(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct jffs2_inode_cache *ic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct jffs2_full_dirent **dead_fds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct jffs2_raw_node_ref *raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct jffs2_full_dirent *fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dbg_fsbuild("removing ino #%u with nlink == zero.\n", ic->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) raw = ic->nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) while (raw != (void *)ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct jffs2_raw_node_ref *next = raw->next_in_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) dbg_fsbuild("obsoleting node at 0x%08x\n", ref_offset(raw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) jffs2_mark_node_obsolete(c, raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) raw = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (ic->scan_dents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int whinged = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) dbg_fsbuild("inode #%u was a directory which may have children...\n", ic->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) while(ic->scan_dents) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct jffs2_inode_cache *child_ic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) fd = ic->scan_dents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) ic->scan_dents = fd->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (!fd->ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* It's a deletion dirent. Ignore it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dbg_fsbuild("child \"%s\" is a deletion dirent, skipping...\n", fd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (!whinged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) whinged = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) dbg_fsbuild("removing child \"%s\", ino #%u\n", fd->name, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) child_ic = jffs2_get_ino_cache(c, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!child_ic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) dbg_fsbuild("cannot remove child \"%s\", ino #%u, because it doesn't exist\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) fd->name, fd->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* Reduce nlink of the child. If it's now zero, stick it on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dead_fds list to be cleaned up later. Else just free the fd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) child_ic->pino_nlink--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (!child_ic->pino_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dbg_fsbuild("inode #%u (\"%s\") now has no links; adding to dead_fds list.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) fd->ino, fd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) fd->next = *dead_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *dead_fds = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) dbg_fsbuild("inode #%u (\"%s\") has now got nlink %d. Ignoring.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) fd->ino, fd->name, child_ic->pino_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) jffs2_free_full_dirent(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) We don't delete the inocache from the hash list and free it yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) The erase code will do that, when all the nodes are completely gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static void jffs2_calc_trigger_levels(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) uint32_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /* Deletion should almost _always_ be allowed. We're fairly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) buggered once we stop allowing people to delete stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) because there's not enough free space... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) c->resv_blocks_deletion = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /* Be conservative about how much space we need before we allow writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) On top of that which is required for deletia, require an extra 2%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) of the medium to be available, for overhead caused by nodes being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) split across blocks, etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) size = c->flash_size / 50; /* 2% of flash size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) size += c->nr_blocks * 100; /* And 100 bytes per eraseblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) size += c->sector_size - 1; /* ... and round up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) c->resv_blocks_write = c->resv_blocks_deletion + (size / c->sector_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /* When do we let the GC thread run in the background */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) c->resv_blocks_gctrigger = c->resv_blocks_write + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* When do we allow garbage collection to merge nodes to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) long-term progress at the expense of short-term space exhaustion? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) c->resv_blocks_gcmerge = c->resv_blocks_deletion + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /* When do we allow garbage collection to eat from bad blocks rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) than actually making progress? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) c->resv_blocks_gcbad = 0;//c->resv_blocks_deletion + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /* What number of 'very dirty' eraseblocks do we allow before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) trigger the GC thread even if we don't _need_ the space. When we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) can't mark nodes obsolete on the medium, the old dirty nodes cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) performance problems because we have to inspect and discard them. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) c->vdirty_blocks_gctrigger = c->resv_blocks_gctrigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (jffs2_can_mark_obsolete(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) c->vdirty_blocks_gctrigger *= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /* If there's less than this amount of dirty space, don't bother
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) trying to GC to make more space. It'll be a fruitless task */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) c->nospc_dirty_size = c->sector_size + (c->flash_size / 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) dbg_fsbuild("trigger levels (size %d KiB, block size %d KiB, %d blocks)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) c->flash_size / 1024, c->sector_size / 1024, c->nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) dbg_fsbuild("Blocks required to allow deletion: %d (%d KiB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) c->resv_blocks_deletion, c->resv_blocks_deletion*c->sector_size/1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) dbg_fsbuild("Blocks required to allow writes: %d (%d KiB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) c->resv_blocks_write, c->resv_blocks_write*c->sector_size/1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) dbg_fsbuild("Blocks required to quiesce GC thread: %d (%d KiB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) c->resv_blocks_gctrigger, c->resv_blocks_gctrigger*c->sector_size/1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) dbg_fsbuild("Blocks required to allow GC merges: %d (%d KiB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) c->resv_blocks_gcmerge, c->resv_blocks_gcmerge*c->sector_size/1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) dbg_fsbuild("Blocks required to GC bad blocks: %d (%d KiB)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) c->resv_blocks_gcbad, c->resv_blocks_gcbad*c->sector_size/1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) dbg_fsbuild("Amount of dirty space required to GC: %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) c->nospc_dirty_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) dbg_fsbuild("Very dirty blocks before GC triggered: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) c->vdirty_blocks_gctrigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int jffs2_do_mount_fs(struct jffs2_sb_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) c->free_size = c->flash_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) c->nr_blocks = c->flash_size / c->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) size = sizeof(struct jffs2_eraseblock) * c->nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) #ifndef __ECOS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (jffs2_blocks_use_vmalloc(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) c->blocks = vzalloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) c->blocks = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!c->blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) for (i=0; i<c->nr_blocks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) INIT_LIST_HEAD(&c->blocks[i].list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) c->blocks[i].offset = i * c->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) c->blocks[i].free_size = c->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) INIT_LIST_HEAD(&c->clean_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) INIT_LIST_HEAD(&c->very_dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) INIT_LIST_HEAD(&c->dirty_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) INIT_LIST_HEAD(&c->erasable_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) INIT_LIST_HEAD(&c->erasing_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) INIT_LIST_HEAD(&c->erase_checking_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) INIT_LIST_HEAD(&c->erase_pending_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) INIT_LIST_HEAD(&c->erasable_pending_wbuf_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) INIT_LIST_HEAD(&c->erase_complete_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) INIT_LIST_HEAD(&c->free_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) INIT_LIST_HEAD(&c->bad_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) INIT_LIST_HEAD(&c->bad_used_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) c->highest_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) c->summary = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) ret = jffs2_sum_init(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (jffs2_build_filesystem(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) dbg_fsbuild("build_fs failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) jffs2_free_ino_caches(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) jffs2_free_raw_node_refs(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto out_sum_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) jffs2_calc_trigger_levels(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) out_sum_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) jffs2_sum_exit(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) kvfree(c->blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }