Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 Red Hat, Inc. |
| 5 | * |
| 6 | * Created by David Woodhouse <dwmw2@infradead.org> |
| 7 | * |
| 8 | * For licensing information, see the file 'LICENCE' in this directory. |
| 9 | * |
| 10 | * $Id: build.c,v 1.69 2004/12/16 20:22:18 dmarlin Exp $ |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/vmalloc.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include "nodelist.h" |
| 20 | |
| 21 | static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *, struct jffs2_inode_cache *, struct jffs2_full_dirent **); |
| 22 | |
| 23 | static inline struct jffs2_inode_cache * |
| 24 | first_inode_chain(int *i, struct jffs2_sb_info *c) |
| 25 | { |
| 26 | for (; *i < INOCACHE_HASHSIZE; (*i)++) { |
| 27 | if (c->inocache_list[*i]) |
| 28 | return c->inocache_list[*i]; |
| 29 | } |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | static inline struct jffs2_inode_cache * |
| 34 | next_inode(int *i, struct jffs2_inode_cache *ic, struct jffs2_sb_info *c) |
| 35 | { |
| 36 | /* More in this chain? */ |
| 37 | if (ic->next) |
| 38 | return ic->next; |
| 39 | (*i)++; |
| 40 | return first_inode_chain(i, c); |
| 41 | } |
| 42 | |
| 43 | #define for_each_inode(i, c, ic) \ |
| 44 | for (i = 0, ic = first_inode_chain(&i, (c)); \ |
| 45 | ic; \ |
| 46 | ic = next_inode(&i, ic, (c))) |
| 47 | |
| 48 | |
| 49 | static inline void jffs2_build_inode_pass1(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic) |
| 50 | { |
| 51 | struct jffs2_full_dirent *fd; |
| 52 | |
| 53 | D1(printk(KERN_DEBUG "jffs2_build_inode building directory inode #%u\n", ic->ino)); |
| 54 | |
| 55 | /* For each child, increase nlink */ |
| 56 | for(fd = ic->scan_dents; fd; fd = fd->next) { |
| 57 | struct jffs2_inode_cache *child_ic; |
| 58 | if (!fd->ino) |
| 59 | continue; |
| 60 | |
| 61 | /* XXX: Can get high latency here with huge directories */ |
| 62 | |
| 63 | child_ic = jffs2_get_ino_cache(c, fd->ino); |
| 64 | if (!child_ic) { |
| 65 | printk(KERN_NOTICE "Eep. Child \"%s\" (ino #%u) of dir ino #%u doesn't exist!\n", |
| 66 | fd->name, fd->ino, ic->ino); |
| 67 | jffs2_mark_node_obsolete(c, fd->raw); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if (child_ic->nlink++ && fd->type == DT_DIR) { |
| 72 | printk(KERN_NOTICE "Child dir \"%s\" (ino #%u) of dir ino #%u appears to be a hard link\n", fd->name, fd->ino, ic->ino); |
| 73 | if (fd->ino == 1 && ic->ino == 1) { |
| 74 | printk(KERN_NOTICE "This is mostly harmless, and probably caused by creating a JFFS2 image\n"); |
| 75 | printk(KERN_NOTICE "using a buggy version of mkfs.jffs2. Use at least v1.17.\n"); |
| 76 | } |
| 77 | /* What do we do about it? */ |
| 78 | } |
| 79 | D1(printk(KERN_DEBUG "Increased nlink for child \"%s\" (ino #%u)\n", fd->name, fd->ino)); |
| 80 | /* Can't free them. We might need them in pass 2 */ |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* Scan plan: |
| 85 | - Scan physical nodes. Build map of inodes/dirents. Allocate inocaches as we go |
| 86 | - Scan directory tree from top down, setting nlink in inocaches |
| 87 | - Scan inocaches for inodes with nlink==0 |
| 88 | */ |
| 89 | static int jffs2_build_filesystem(struct jffs2_sb_info *c) |
| 90 | { |
| 91 | int ret; |
| 92 | int i; |
| 93 | struct jffs2_inode_cache *ic; |
| 94 | struct jffs2_full_dirent *fd; |
| 95 | struct jffs2_full_dirent *dead_fds = NULL; |
| 96 | |
| 97 | /* First, scan the medium and build all the inode caches with |
| 98 | lists of physical nodes */ |
| 99 | |
| 100 | c->flags |= JFFS2_SB_FLAG_MOUNTING; |
| 101 | ret = jffs2_scan_medium(c); |
| 102 | if (ret) |
| 103 | goto exit; |
| 104 | |
| 105 | D1(printk(KERN_DEBUG "Scanned flash completely\n")); |
| 106 | D2(jffs2_dump_block_lists(c)); |
| 107 | |
| 108 | /* Now scan the directory tree, increasing nlink according to every dirent found. */ |
| 109 | for_each_inode(i, c, ic) { |
| 110 | D1(printk(KERN_DEBUG "Pass 1: ino #%u\n", ic->ino)); |
| 111 | |
| 112 | D1(BUG_ON(ic->ino > c->highest_ino)); |
| 113 | |
| 114 | if (ic->scan_dents) { |
| 115 | jffs2_build_inode_pass1(c, ic); |
| 116 | cond_resched(); |
| 117 | } |
| 118 | } |
| 119 | c->flags &= ~JFFS2_SB_FLAG_MOUNTING; |
| 120 | |
| 121 | D1(printk(KERN_DEBUG "Pass 1 complete\n")); |
| 122 | |
| 123 | /* Next, scan for inodes with nlink == 0 and remove them. If |
| 124 | they were directories, then decrement the nlink of their |
| 125 | children too, and repeat the scan. As that's going to be |
| 126 | a fairly uncommon occurrence, it's not so evil to do it this |
| 127 | way. Recursion bad. */ |
| 128 | D1(printk(KERN_DEBUG "Pass 2 starting\n")); |
| 129 | |
| 130 | for_each_inode(i, c, ic) { |
| 131 | D1(printk(KERN_DEBUG "Pass 2: ino #%u, nlink %d, ic %p, nodes %p\n", ic->ino, ic->nlink, ic, ic->nodes)); |
| 132 | if (ic->nlink) |
| 133 | continue; |
| 134 | |
| 135 | jffs2_build_remove_unlinked_inode(c, ic, &dead_fds); |
| 136 | cond_resched(); |
| 137 | } |
| 138 | |
| 139 | D1(printk(KERN_DEBUG "Pass 2a starting\n")); |
| 140 | |
| 141 | while (dead_fds) { |
| 142 | fd = dead_fds; |
| 143 | dead_fds = fd->next; |
| 144 | |
| 145 | ic = jffs2_get_ino_cache(c, fd->ino); |
| 146 | D1(printk(KERN_DEBUG "Removing dead_fd ino #%u (\"%s\"), ic at %p\n", fd->ino, fd->name, ic)); |
| 147 | |
| 148 | if (ic) |
| 149 | jffs2_build_remove_unlinked_inode(c, ic, &dead_fds); |
| 150 | jffs2_free_full_dirent(fd); |
| 151 | } |
| 152 | |
| 153 | D1(printk(KERN_DEBUG "Pass 2 complete\n")); |
| 154 | |
| 155 | /* Finally, we can scan again and free the dirent structs */ |
| 156 | for_each_inode(i, c, ic) { |
| 157 | D1(printk(KERN_DEBUG "Pass 3: ino #%u, ic %p, nodes %p\n", ic->ino, ic, ic->nodes)); |
| 158 | |
| 159 | while(ic->scan_dents) { |
| 160 | fd = ic->scan_dents; |
| 161 | ic->scan_dents = fd->next; |
| 162 | jffs2_free_full_dirent(fd); |
| 163 | } |
| 164 | ic->scan_dents = NULL; |
| 165 | cond_resched(); |
| 166 | } |
| 167 | D1(printk(KERN_DEBUG "Pass 3 complete\n")); |
| 168 | D2(jffs2_dump_block_lists(c)); |
| 169 | |
| 170 | /* Rotate the lists by some number to ensure wear levelling */ |
| 171 | jffs2_rotate_lists(c); |
| 172 | |
| 173 | ret = 0; |
| 174 | |
| 175 | exit: |
| 176 | if (ret) { |
| 177 | for_each_inode(i, c, ic) { |
| 178 | while(ic->scan_dents) { |
| 179 | fd = ic->scan_dents; |
| 180 | ic->scan_dents = fd->next; |
| 181 | jffs2_free_full_dirent(fd); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, struct jffs2_full_dirent **dead_fds) |
| 190 | { |
| 191 | struct jffs2_raw_node_ref *raw; |
| 192 | struct jffs2_full_dirent *fd; |
| 193 | |
| 194 | D1(printk(KERN_DEBUG "JFFS2: Removing ino #%u with nlink == zero.\n", ic->ino)); |
| 195 | |
| 196 | raw = ic->nodes; |
| 197 | while (raw != (void *)ic) { |
| 198 | struct jffs2_raw_node_ref *next = raw->next_in_ino; |
| 199 | D1(printk(KERN_DEBUG "obsoleting node at 0x%08x\n", ref_offset(raw))); |
| 200 | jffs2_mark_node_obsolete(c, raw); |
| 201 | raw = next; |
| 202 | } |
| 203 | |
| 204 | if (ic->scan_dents) { |
| 205 | int whinged = 0; |
| 206 | D1(printk(KERN_DEBUG "Inode #%u was a directory which may have children...\n", ic->ino)); |
| 207 | |
| 208 | while(ic->scan_dents) { |
| 209 | struct jffs2_inode_cache *child_ic; |
| 210 | |
| 211 | fd = ic->scan_dents; |
| 212 | ic->scan_dents = fd->next; |
| 213 | |
| 214 | if (!fd->ino) { |
| 215 | /* It's a deletion dirent. Ignore it */ |
| 216 | D1(printk(KERN_DEBUG "Child \"%s\" is a deletion dirent, skipping...\n", fd->name)); |
| 217 | jffs2_free_full_dirent(fd); |
| 218 | continue; |
| 219 | } |
| 220 | if (!whinged) { |
| 221 | whinged = 1; |
| 222 | printk(KERN_NOTICE "Inode #%u was a directory with children - removing those too...\n", ic->ino); |
| 223 | } |
| 224 | |
| 225 | D1(printk(KERN_DEBUG "Removing child \"%s\", ino #%u\n", |
| 226 | fd->name, fd->ino)); |
| 227 | |
| 228 | child_ic = jffs2_get_ino_cache(c, fd->ino); |
| 229 | if (!child_ic) { |
| 230 | printk(KERN_NOTICE "Cannot remove child \"%s\", ino #%u, because it doesn't exist\n", fd->name, fd->ino); |
| 231 | jffs2_free_full_dirent(fd); |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | /* Reduce nlink of the child. If it's now zero, stick it on the |
| 236 | dead_fds list to be cleaned up later. Else just free the fd */ |
| 237 | |
| 238 | child_ic->nlink--; |
| 239 | |
| 240 | if (!child_ic->nlink) { |
| 241 | D1(printk(KERN_DEBUG "Inode #%u (\"%s\") has now got zero nlink. Adding to dead_fds list.\n", |
| 242 | fd->ino, fd->name)); |
| 243 | fd->next = *dead_fds; |
| 244 | *dead_fds = fd; |
| 245 | } else { |
| 246 | D1(printk(KERN_DEBUG "Inode #%u (\"%s\") has now got nlink %d. Ignoring.\n", |
| 247 | fd->ino, fd->name, child_ic->nlink)); |
| 248 | jffs2_free_full_dirent(fd); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | We don't delete the inocache from the hash list and free it yet. |
| 255 | The erase code will do that, when all the nodes are completely gone. |
| 256 | */ |
| 257 | } |
| 258 | |
| 259 | static void jffs2_calc_trigger_levels(struct jffs2_sb_info *c) |
| 260 | { |
| 261 | uint32_t size; |
| 262 | |
| 263 | /* Deletion should almost _always_ be allowed. We're fairly |
| 264 | buggered once we stop allowing people to delete stuff |
| 265 | because there's not enough free space... */ |
| 266 | c->resv_blocks_deletion = 2; |
| 267 | |
| 268 | /* Be conservative about how much space we need before we allow writes. |
| 269 | On top of that which is required for deletia, require an extra 2% |
| 270 | of the medium to be available, for overhead caused by nodes being |
| 271 | split across blocks, etc. */ |
| 272 | |
| 273 | size = c->flash_size / 50; /* 2% of flash size */ |
| 274 | size += c->nr_blocks * 100; /* And 100 bytes per eraseblock */ |
| 275 | size += c->sector_size - 1; /* ... and round up */ |
| 276 | |
| 277 | c->resv_blocks_write = c->resv_blocks_deletion + (size / c->sector_size); |
| 278 | |
| 279 | /* When do we let the GC thread run in the background */ |
| 280 | |
| 281 | c->resv_blocks_gctrigger = c->resv_blocks_write + 1; |
| 282 | |
| 283 | /* When do we allow garbage collection to merge nodes to make |
| 284 | long-term progress at the expense of short-term space exhaustion? */ |
| 285 | c->resv_blocks_gcmerge = c->resv_blocks_deletion + 1; |
| 286 | |
| 287 | /* When do we allow garbage collection to eat from bad blocks rather |
| 288 | than actually making progress? */ |
| 289 | c->resv_blocks_gcbad = 0;//c->resv_blocks_deletion + 2; |
| 290 | |
| 291 | /* If there's less than this amount of dirty space, don't bother |
| 292 | trying to GC to make more space. It'll be a fruitless task */ |
| 293 | c->nospc_dirty_size = c->sector_size + (c->flash_size / 100); |
| 294 | |
| 295 | D1(printk(KERN_DEBUG "JFFS2 trigger levels (size %d KiB, block size %d KiB, %d blocks)\n", |
| 296 | c->flash_size / 1024, c->sector_size / 1024, c->nr_blocks)); |
| 297 | D1(printk(KERN_DEBUG "Blocks required to allow deletion: %d (%d KiB)\n", |
| 298 | c->resv_blocks_deletion, c->resv_blocks_deletion*c->sector_size/1024)); |
| 299 | D1(printk(KERN_DEBUG "Blocks required to allow writes: %d (%d KiB)\n", |
| 300 | c->resv_blocks_write, c->resv_blocks_write*c->sector_size/1024)); |
| 301 | D1(printk(KERN_DEBUG "Blocks required to quiesce GC thread: %d (%d KiB)\n", |
| 302 | c->resv_blocks_gctrigger, c->resv_blocks_gctrigger*c->sector_size/1024)); |
| 303 | D1(printk(KERN_DEBUG "Blocks required to allow GC merges: %d (%d KiB)\n", |
| 304 | c->resv_blocks_gcmerge, c->resv_blocks_gcmerge*c->sector_size/1024)); |
| 305 | D1(printk(KERN_DEBUG "Blocks required to GC bad blocks: %d (%d KiB)\n", |
| 306 | c->resv_blocks_gcbad, c->resv_blocks_gcbad*c->sector_size/1024)); |
| 307 | D1(printk(KERN_DEBUG "Amount of dirty space required to GC: %d bytes\n", |
| 308 | c->nospc_dirty_size)); |
| 309 | } |
| 310 | |
| 311 | int jffs2_do_mount_fs(struct jffs2_sb_info *c) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | c->free_size = c->flash_size; |
| 316 | c->nr_blocks = c->flash_size / c->sector_size; |
| 317 | if (c->mtd->flags & MTD_NO_VIRTBLOCKS) |
| 318 | c->blocks = vmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks); |
| 319 | else |
| 320 | c->blocks = kmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks, GFP_KERNEL); |
| 321 | if (!c->blocks) |
| 322 | return -ENOMEM; |
| 323 | for (i=0; i<c->nr_blocks; i++) { |
| 324 | INIT_LIST_HEAD(&c->blocks[i].list); |
| 325 | c->blocks[i].offset = i * c->sector_size; |
| 326 | c->blocks[i].free_size = c->sector_size; |
| 327 | c->blocks[i].dirty_size = 0; |
| 328 | c->blocks[i].wasted_size = 0; |
| 329 | c->blocks[i].unchecked_size = 0; |
| 330 | c->blocks[i].used_size = 0; |
| 331 | c->blocks[i].first_node = NULL; |
| 332 | c->blocks[i].last_node = NULL; |
| 333 | c->blocks[i].bad_count = 0; |
| 334 | } |
| 335 | |
| 336 | init_MUTEX(&c->alloc_sem); |
| 337 | init_MUTEX(&c->erase_free_sem); |
| 338 | init_waitqueue_head(&c->erase_wait); |
| 339 | init_waitqueue_head(&c->inocache_wq); |
| 340 | spin_lock_init(&c->erase_completion_lock); |
| 341 | spin_lock_init(&c->inocache_lock); |
| 342 | |
| 343 | INIT_LIST_HEAD(&c->clean_list); |
| 344 | INIT_LIST_HEAD(&c->very_dirty_list); |
| 345 | INIT_LIST_HEAD(&c->dirty_list); |
| 346 | INIT_LIST_HEAD(&c->erasable_list); |
| 347 | INIT_LIST_HEAD(&c->erasing_list); |
| 348 | INIT_LIST_HEAD(&c->erase_pending_list); |
| 349 | INIT_LIST_HEAD(&c->erasable_pending_wbuf_list); |
| 350 | INIT_LIST_HEAD(&c->erase_complete_list); |
| 351 | INIT_LIST_HEAD(&c->free_list); |
| 352 | INIT_LIST_HEAD(&c->bad_list); |
| 353 | INIT_LIST_HEAD(&c->bad_used_list); |
| 354 | c->highest_ino = 1; |
| 355 | |
| 356 | if (jffs2_build_filesystem(c)) { |
| 357 | D1(printk(KERN_DEBUG "build_fs failed\n")); |
| 358 | jffs2_free_ino_caches(c); |
| 359 | jffs2_free_raw_node_refs(c); |
| 360 | if (c->mtd->flags & MTD_NO_VIRTBLOCKS) { |
| 361 | vfree(c->blocks); |
| 362 | } else { |
| 363 | kfree(c->blocks); |
| 364 | } |
| 365 | return -EIO; |
| 366 | } |
| 367 | |
| 368 | jffs2_calc_trigger_levels(c); |
| 369 | |
| 370 | return 0; |
| 371 | } |