diff options
Diffstat (limited to 'fs/yaffs2')
43 files changed, 15346 insertions, 0 deletions
diff --git a/fs/yaffs2/Kconfig b/fs/yaffs2/Kconfig new file mode 100644 index 00000000000..63541405999 --- /dev/null +++ b/fs/yaffs2/Kconfig | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | # | ||
| 2 | # YAFFS file system configurations | ||
| 3 | # | ||
| 4 | |||
| 5 | config YAFFS_FS | ||
| 6 | tristate "YAFFS2 file system support" | ||
| 7 | default n | ||
| 8 | depends on MTD_BLOCK | ||
| 9 | select YAFFS_YAFFS1 | ||
| 10 | select YAFFS_YAFFS2 | ||
| 11 | help | ||
| 12 | YAFFS2, or Yet Another Flash Filing System, is a filing system | ||
| 13 | optimised for NAND Flash chips. | ||
| 14 | |||
| 15 | To compile the YAFFS2 file system support as a module, choose M | ||
| 16 | here: the module will be called yaffs2. | ||
| 17 | |||
| 18 | If unsure, say N. | ||
| 19 | |||
| 20 | Further information on YAFFS2 is available at | ||
| 21 | <http://www.aleph1.co.uk/yaffs/>. | ||
| 22 | |||
| 23 | config YAFFS_YAFFS1 | ||
| 24 | bool "512 byte / page devices" | ||
| 25 | depends on YAFFS_FS | ||
| 26 | default y | ||
| 27 | help | ||
| 28 | Enable YAFFS1 support -- yaffs for 512 byte / page devices | ||
| 29 | |||
| 30 | Not needed for 2K-page devices. | ||
| 31 | |||
| 32 | If unsure, say Y. | ||
| 33 | |||
| 34 | config YAFFS_9BYTE_TAGS | ||
| 35 | bool "Use older-style on-NAND data format with pageStatus byte" | ||
| 36 | depends on YAFFS_YAFFS1 | ||
| 37 | default n | ||
| 38 | help | ||
| 39 | |||
| 40 | Older-style on-NAND data format has a "pageStatus" byte to record | ||
| 41 | chunk/page state. This byte is zero when the page is discarded. | ||
| 42 | Choose this option if you have existing on-NAND data using this | ||
| 43 | format that you need to continue to support. New data written | ||
| 44 | also uses the older-style format. Note: Use of this option | ||
| 45 | generally requires that MTD's oob layout be adjusted to use the | ||
| 46 | older-style format. See notes on tags formats and MTD versions | ||
| 47 | in yaffs_mtdif1.c. | ||
| 48 | |||
| 49 | If unsure, say N. | ||
| 50 | |||
| 51 | config YAFFS_DOES_ECC | ||
| 52 | bool "Lets Yaffs do its own ECC" | ||
| 53 | depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS | ||
| 54 | default n | ||
| 55 | help | ||
| 56 | This enables Yaffs to use its own ECC functions instead of using | ||
| 57 | the ones from the generic MTD-NAND driver. | ||
| 58 | |||
| 59 | If unsure, say N. | ||
| 60 | |||
| 61 | config YAFFS_ECC_WRONG_ORDER | ||
| 62 | bool "Use the same ecc byte order as Steven Hill's nand_ecc.c" | ||
| 63 | depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS | ||
| 64 | default n | ||
| 65 | help | ||
| 66 | This makes yaffs_ecc.c use the same ecc byte order as Steven | ||
| 67 | Hill's nand_ecc.c. If not set, then you get the same ecc byte | ||
| 68 | order as SmartMedia. | ||
| 69 | |||
| 70 | If unsure, say N. | ||
| 71 | |||
| 72 | config YAFFS_YAFFS2 | ||
| 73 | bool "2048 byte (or larger) / page devices" | ||
| 74 | depends on YAFFS_FS | ||
| 75 | default y | ||
| 76 | help | ||
| 77 | Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices | ||
| 78 | |||
| 79 | If unsure, say Y. | ||
| 80 | |||
| 81 | config YAFFS_AUTO_YAFFS2 | ||
| 82 | bool "Autoselect yaffs2 format" | ||
| 83 | depends on YAFFS_YAFFS2 | ||
| 84 | default y | ||
| 85 | help | ||
| 86 | Without this, you need to explicitely use yaffs2 as the file | ||
| 87 | system type. With this, you can say "yaffs" and yaffs or yaffs2 | ||
| 88 | will be used depending on the device page size (yaffs on | ||
| 89 | 512-byte page devices, yaffs2 on 2K page devices). | ||
| 90 | |||
| 91 | If unsure, say Y. | ||
| 92 | |||
| 93 | config YAFFS_DISABLE_TAGS_ECC | ||
| 94 | bool "Disable YAFFS from doing ECC on tags by default" | ||
| 95 | depends on YAFFS_FS && YAFFS_YAFFS2 | ||
| 96 | default n | ||
| 97 | help | ||
| 98 | This defaults Yaffs to using its own ECC calculations on tags instead of | ||
| 99 | just relying on the MTD. | ||
| 100 | This behavior can also be overridden with tags_ecc_on and | ||
| 101 | tags_ecc_off mount options. | ||
| 102 | |||
| 103 | If unsure, say N. | ||
| 104 | |||
| 105 | config YAFFS_ALWAYS_CHECK_CHUNK_ERASED | ||
| 106 | bool "Force chunk erase check" | ||
| 107 | depends on YAFFS_FS | ||
| 108 | default n | ||
| 109 | help | ||
| 110 | Normally YAFFS only checks chunks before writing until an erased | ||
| 111 | chunk is found. This helps to detect any partially written | ||
| 112 | chunks that might have happened due to power loss. | ||
| 113 | |||
| 114 | Enabling this forces on the test that chunks are erased in flash | ||
| 115 | before writing to them. This takes more time but is potentially | ||
| 116 | a bit more secure. | ||
| 117 | |||
| 118 | Suggest setting Y during development and ironing out driver | ||
| 119 | issues etc. Suggest setting to N if you want faster writing. | ||
| 120 | |||
| 121 | If unsure, say Y. | ||
| 122 | |||
| 123 | config YAFFS_EMPTY_LOST_AND_FOUND | ||
| 124 | bool "Empty lost and found on boot" | ||
| 125 | depends on YAFFS_FS | ||
| 126 | default n | ||
| 127 | help | ||
| 128 | If this is enabled then the contents of lost and found is | ||
| 129 | automatically dumped at mount. | ||
| 130 | |||
| 131 | If unsure, say N. | ||
| 132 | |||
| 133 | config YAFFS_DISABLE_BLOCK_REFRESHING | ||
| 134 | bool "Disable yaffs2 block refreshing" | ||
| 135 | depends on YAFFS_FS | ||
| 136 | default n | ||
| 137 | help | ||
| 138 | If this is set, then block refreshing is disabled. | ||
| 139 | Block refreshing infrequently refreshes the oldest block in | ||
| 140 | a yaffs2 file system. This mechanism helps to refresh flash to | ||
| 141 | mitigate against data loss. This is particularly useful for MLC. | ||
| 142 | |||
| 143 | If unsure, say N. | ||
| 144 | |||
| 145 | config YAFFS_DISABLE_BACKGROUND | ||
| 146 | bool "Disable yaffs2 background processing" | ||
| 147 | depends on YAFFS_FS | ||
| 148 | default n | ||
| 149 | help | ||
| 150 | If this is set, then background processing is disabled. | ||
| 151 | Background processing makes many foreground activities faster. | ||
| 152 | |||
| 153 | If unsure, say N. | ||
| 154 | |||
| 155 | config YAFFS_XATTR | ||
| 156 | bool "Enable yaffs2 xattr support" | ||
| 157 | depends on YAFFS_FS | ||
| 158 | default y | ||
| 159 | help | ||
| 160 | If this is set then yaffs2 will provide xattr support. | ||
| 161 | If unsure, say Y. | ||
diff --git a/fs/yaffs2/Makefile b/fs/yaffs2/Makefile new file mode 100644 index 00000000000..e63a28aa3ed --- /dev/null +++ b/fs/yaffs2/Makefile | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | # | ||
| 2 | # Makefile for the linux YAFFS filesystem routines. | ||
| 3 | # | ||
| 4 | |||
| 5 | obj-$(CONFIG_YAFFS_FS) += yaffs.o | ||
| 6 | |||
| 7 | yaffs-y := yaffs_ecc.o yaffs_vfs.o yaffs_guts.o yaffs_checkptrw.o | ||
| 8 | yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o | ||
| 9 | yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o | ||
| 10 | yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o | ||
| 11 | yaffs-y += yaffs_nameval.o yaffs_attribs.o | ||
| 12 | yaffs-y += yaffs_allocator.o | ||
| 13 | yaffs-y += yaffs_yaffs1.o | ||
| 14 | yaffs-y += yaffs_yaffs2.o | ||
| 15 | yaffs-y += yaffs_bitmap.o | ||
| 16 | yaffs-y += yaffs_verify.o | ||
| 17 | |||
diff --git a/fs/yaffs2/yaffs_allocator.c b/fs/yaffs2/yaffs_allocator.c new file mode 100644 index 00000000000..f9cd5becd8f --- /dev/null +++ b/fs/yaffs2/yaffs_allocator.c | |||
| @@ -0,0 +1,396 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_allocator.h" | ||
| 15 | #include "yaffs_guts.h" | ||
| 16 | #include "yaffs_trace.h" | ||
| 17 | #include "yportenv.h" | ||
| 18 | |||
| 19 | #ifdef CONFIG_YAFFS_KMALLOC_ALLOCATOR | ||
| 20 | |||
| 21 | void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev) | ||
| 22 | { | ||
| 23 | dev = dev; | ||
| 24 | } | ||
| 25 | |||
| 26 | void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev) | ||
| 27 | { | ||
| 28 | dev = dev; | ||
| 29 | } | ||
| 30 | |||
| 31 | struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev) | ||
| 32 | { | ||
| 33 | return (struct yaffs_tnode *)kmalloc(dev->tnode_size, GFP_NOFS); | ||
| 34 | } | ||
| 35 | |||
| 36 | void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn) | ||
| 37 | { | ||
| 38 | dev = dev; | ||
| 39 | kfree(tn); | ||
| 40 | } | ||
| 41 | |||
| 42 | void yaffs_init_raw_objs(struct yaffs_dev *dev) | ||
| 43 | { | ||
| 44 | dev = dev; | ||
| 45 | } | ||
| 46 | |||
| 47 | void yaffs_deinit_raw_objs(struct yaffs_dev *dev) | ||
| 48 | { | ||
| 49 | dev = dev; | ||
| 50 | } | ||
| 51 | |||
| 52 | struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev) | ||
| 53 | { | ||
| 54 | dev = dev; | ||
| 55 | return (struct yaffs_obj *)kmalloc(sizeof(struct yaffs_obj)); | ||
| 56 | } | ||
| 57 | |||
| 58 | void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj) | ||
| 59 | { | ||
| 60 | |||
| 61 | dev = dev; | ||
| 62 | kfree(obj); | ||
| 63 | } | ||
| 64 | |||
| 65 | #else | ||
| 66 | |||
| 67 | struct yaffs_tnode_list { | ||
| 68 | struct yaffs_tnode_list *next; | ||
| 69 | struct yaffs_tnode *tnodes; | ||
| 70 | }; | ||
| 71 | |||
| 72 | struct yaffs_obj_list { | ||
| 73 | struct yaffs_obj_list *next; | ||
| 74 | struct yaffs_obj *objects; | ||
| 75 | }; | ||
| 76 | |||
| 77 | struct yaffs_allocator { | ||
| 78 | int n_tnodes_created; | ||
| 79 | struct yaffs_tnode *free_tnodes; | ||
| 80 | int n_free_tnodes; | ||
| 81 | struct yaffs_tnode_list *alloc_tnode_list; | ||
| 82 | |||
| 83 | int n_obj_created; | ||
| 84 | struct yaffs_obj *free_objs; | ||
| 85 | int n_free_objects; | ||
| 86 | |||
| 87 | struct yaffs_obj_list *allocated_obj_list; | ||
| 88 | }; | ||
| 89 | |||
| 90 | static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev) | ||
| 91 | { | ||
| 92 | |||
| 93 | struct yaffs_allocator *allocator = | ||
| 94 | (struct yaffs_allocator *)dev->allocator; | ||
| 95 | |||
| 96 | struct yaffs_tnode_list *tmp; | ||
| 97 | |||
| 98 | if (!allocator) { | ||
| 99 | YBUG(); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | while (allocator->alloc_tnode_list) { | ||
| 104 | tmp = allocator->alloc_tnode_list->next; | ||
| 105 | |||
| 106 | kfree(allocator->alloc_tnode_list->tnodes); | ||
| 107 | kfree(allocator->alloc_tnode_list); | ||
| 108 | allocator->alloc_tnode_list = tmp; | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | allocator->free_tnodes = NULL; | ||
| 113 | allocator->n_free_tnodes = 0; | ||
| 114 | allocator->n_tnodes_created = 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | static void yaffs_init_raw_tnodes(struct yaffs_dev *dev) | ||
| 118 | { | ||
| 119 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 120 | |||
| 121 | if (allocator) { | ||
| 122 | allocator->alloc_tnode_list = NULL; | ||
| 123 | allocator->free_tnodes = NULL; | ||
| 124 | allocator->n_free_tnodes = 0; | ||
| 125 | allocator->n_tnodes_created = 0; | ||
| 126 | } else { | ||
| 127 | YBUG(); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes) | ||
| 132 | { | ||
| 133 | struct yaffs_allocator *allocator = | ||
| 134 | (struct yaffs_allocator *)dev->allocator; | ||
| 135 | int i; | ||
| 136 | struct yaffs_tnode *new_tnodes; | ||
| 137 | u8 *mem; | ||
| 138 | struct yaffs_tnode *curr; | ||
| 139 | struct yaffs_tnode *next; | ||
| 140 | struct yaffs_tnode_list *tnl; | ||
| 141 | |||
| 142 | if (!allocator) { | ||
| 143 | YBUG(); | ||
| 144 | return YAFFS_FAIL; | ||
| 145 | } | ||
| 146 | |||
| 147 | if (n_tnodes < 1) | ||
| 148 | return YAFFS_OK; | ||
| 149 | |||
| 150 | /* make these things */ | ||
| 151 | |||
| 152 | new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS); | ||
| 153 | mem = (u8 *) new_tnodes; | ||
| 154 | |||
| 155 | if (!new_tnodes) { | ||
| 156 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 157 | "yaffs: Could not allocate Tnodes"); | ||
| 158 | return YAFFS_FAIL; | ||
| 159 | } | ||
| 160 | |||
| 161 | /* New hookup for wide tnodes */ | ||
| 162 | for (i = 0; i < n_tnodes - 1; i++) { | ||
| 163 | curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size]; | ||
| 164 | next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size]; | ||
| 165 | curr->internal[0] = next; | ||
| 166 | } | ||
| 167 | |||
| 168 | curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size]; | ||
| 169 | curr->internal[0] = allocator->free_tnodes; | ||
| 170 | allocator->free_tnodes = (struct yaffs_tnode *)mem; | ||
| 171 | |||
| 172 | allocator->n_free_tnodes += n_tnodes; | ||
| 173 | allocator->n_tnodes_created += n_tnodes; | ||
| 174 | |||
| 175 | /* Now add this bunch of tnodes to a list for freeing up. | ||
| 176 | * NB If we can't add this to the management list it isn't fatal | ||
| 177 | * but it just means we can't free this bunch of tnodes later. | ||
| 178 | */ | ||
| 179 | |||
| 180 | tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS); | ||
| 181 | if (!tnl) { | ||
| 182 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 183 | "Could not add tnodes to management list"); | ||
| 184 | return YAFFS_FAIL; | ||
| 185 | } else { | ||
| 186 | tnl->tnodes = new_tnodes; | ||
| 187 | tnl->next = allocator->alloc_tnode_list; | ||
| 188 | allocator->alloc_tnode_list = tnl; | ||
| 189 | } | ||
| 190 | |||
| 191 | yaffs_trace(YAFFS_TRACE_ALLOCATE,"Tnodes added"); | ||
| 192 | |||
| 193 | return YAFFS_OK; | ||
| 194 | } | ||
| 195 | |||
| 196 | struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev) | ||
| 197 | { | ||
| 198 | struct yaffs_allocator *allocator = | ||
| 199 | (struct yaffs_allocator *)dev->allocator; | ||
| 200 | struct yaffs_tnode *tn = NULL; | ||
| 201 | |||
| 202 | if (!allocator) { | ||
| 203 | YBUG(); | ||
| 204 | return NULL; | ||
| 205 | } | ||
| 206 | |||
| 207 | /* If there are none left make more */ | ||
| 208 | if (!allocator->free_tnodes) | ||
| 209 | yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES); | ||
| 210 | |||
| 211 | if (allocator->free_tnodes) { | ||
| 212 | tn = allocator->free_tnodes; | ||
| 213 | allocator->free_tnodes = allocator->free_tnodes->internal[0]; | ||
| 214 | allocator->n_free_tnodes--; | ||
| 215 | } | ||
| 216 | |||
| 217 | return tn; | ||
| 218 | } | ||
| 219 | |||
| 220 | /* FreeTnode frees up a tnode and puts it back on the free list */ | ||
| 221 | void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn) | ||
| 222 | { | ||
| 223 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 224 | |||
| 225 | if (!allocator) { | ||
| 226 | YBUG(); | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | if (tn) { | ||
| 231 | tn->internal[0] = allocator->free_tnodes; | ||
| 232 | allocator->free_tnodes = tn; | ||
| 233 | allocator->n_free_tnodes++; | ||
| 234 | } | ||
| 235 | dev->checkpoint_blocks_required = 0; /* force recalculation */ | ||
| 236 | } | ||
| 237 | |||
| 238 | static void yaffs_init_raw_objs(struct yaffs_dev *dev) | ||
| 239 | { | ||
| 240 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 241 | |||
| 242 | if (allocator) { | ||
| 243 | allocator->allocated_obj_list = NULL; | ||
| 244 | allocator->free_objs = NULL; | ||
| 245 | allocator->n_free_objects = 0; | ||
| 246 | } else { | ||
| 247 | YBUG(); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | static void yaffs_deinit_raw_objs(struct yaffs_dev *dev) | ||
| 252 | { | ||
| 253 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 254 | struct yaffs_obj_list *tmp; | ||
| 255 | |||
| 256 | if (!allocator) { | ||
| 257 | YBUG(); | ||
| 258 | return; | ||
| 259 | } | ||
| 260 | |||
| 261 | while (allocator->allocated_obj_list) { | ||
| 262 | tmp = allocator->allocated_obj_list->next; | ||
| 263 | kfree(allocator->allocated_obj_list->objects); | ||
| 264 | kfree(allocator->allocated_obj_list); | ||
| 265 | |||
| 266 | allocator->allocated_obj_list = tmp; | ||
| 267 | } | ||
| 268 | |||
| 269 | allocator->free_objs = NULL; | ||
| 270 | allocator->n_free_objects = 0; | ||
| 271 | allocator->n_obj_created = 0; | ||
| 272 | } | ||
| 273 | |||
| 274 | static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj) | ||
| 275 | { | ||
| 276 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 277 | |||
| 278 | int i; | ||
| 279 | struct yaffs_obj *new_objs; | ||
| 280 | struct yaffs_obj_list *list; | ||
| 281 | |||
| 282 | if (!allocator) { | ||
| 283 | YBUG(); | ||
| 284 | return YAFFS_FAIL; | ||
| 285 | } | ||
| 286 | |||
| 287 | if (n_obj < 1) | ||
| 288 | return YAFFS_OK; | ||
| 289 | |||
| 290 | /* make these things */ | ||
| 291 | new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS); | ||
| 292 | list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS); | ||
| 293 | |||
| 294 | if (!new_objs || !list) { | ||
| 295 | if (new_objs) { | ||
| 296 | kfree(new_objs); | ||
| 297 | new_objs = NULL; | ||
| 298 | } | ||
| 299 | if (list) { | ||
| 300 | kfree(list); | ||
| 301 | list = NULL; | ||
| 302 | } | ||
| 303 | yaffs_trace(YAFFS_TRACE_ALLOCATE, | ||
| 304 | "Could not allocate more objects"); | ||
| 305 | return YAFFS_FAIL; | ||
| 306 | } | ||
| 307 | |||
| 308 | /* Hook them into the free list */ | ||
| 309 | for (i = 0; i < n_obj - 1; i++) { | ||
| 310 | new_objs[i].siblings.next = | ||
| 311 | (struct list_head *)(&new_objs[i + 1]); | ||
| 312 | } | ||
| 313 | |||
| 314 | new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs; | ||
| 315 | allocator->free_objs = new_objs; | ||
| 316 | allocator->n_free_objects += n_obj; | ||
| 317 | allocator->n_obj_created += n_obj; | ||
| 318 | |||
| 319 | /* Now add this bunch of Objects to a list for freeing up. */ | ||
| 320 | |||
| 321 | list->objects = new_objs; | ||
| 322 | list->next = allocator->allocated_obj_list; | ||
| 323 | allocator->allocated_obj_list = list; | ||
| 324 | |||
| 325 | return YAFFS_OK; | ||
| 326 | } | ||
| 327 | |||
| 328 | struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev) | ||
| 329 | { | ||
| 330 | struct yaffs_obj *obj = NULL; | ||
| 331 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 332 | |||
| 333 | if (!allocator) { | ||
| 334 | YBUG(); | ||
| 335 | return obj; | ||
| 336 | } | ||
| 337 | |||
| 338 | /* If there are none left make more */ | ||
| 339 | if (!allocator->free_objs) | ||
| 340 | yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS); | ||
| 341 | |||
| 342 | if (allocator->free_objs) { | ||
| 343 | obj = allocator->free_objs; | ||
| 344 | allocator->free_objs = | ||
| 345 | (struct yaffs_obj *)(allocator->free_objs->siblings.next); | ||
| 346 | allocator->n_free_objects--; | ||
| 347 | } | ||
| 348 | |||
| 349 | return obj; | ||
| 350 | } | ||
| 351 | |||
| 352 | void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj) | ||
| 353 | { | ||
| 354 | |||
| 355 | struct yaffs_allocator *allocator = dev->allocator; | ||
| 356 | |||
| 357 | if (!allocator) | ||
| 358 | YBUG(); | ||
| 359 | else { | ||
| 360 | /* Link into the free list. */ | ||
| 361 | obj->siblings.next = (struct list_head *)(allocator->free_objs); | ||
| 362 | allocator->free_objs = obj; | ||
| 363 | allocator->n_free_objects++; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev) | ||
| 368 | { | ||
| 369 | if (dev->allocator) { | ||
| 370 | yaffs_deinit_raw_tnodes(dev); | ||
| 371 | yaffs_deinit_raw_objs(dev); | ||
| 372 | |||
| 373 | kfree(dev->allocator); | ||
| 374 | dev->allocator = NULL; | ||
| 375 | } else { | ||
| 376 | YBUG(); | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev) | ||
| 381 | { | ||
| 382 | struct yaffs_allocator *allocator; | ||
| 383 | |||
| 384 | if (!dev->allocator) { | ||
| 385 | allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS); | ||
| 386 | if (allocator) { | ||
| 387 | dev->allocator = allocator; | ||
| 388 | yaffs_init_raw_tnodes(dev); | ||
| 389 | yaffs_init_raw_objs(dev); | ||
| 390 | } | ||
| 391 | } else { | ||
| 392 | YBUG(); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | #endif | ||
diff --git a/fs/yaffs2/yaffs_allocator.h b/fs/yaffs2/yaffs_allocator.h new file mode 100644 index 00000000000..4d5f2aec89f --- /dev/null +++ b/fs/yaffs2/yaffs_allocator.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_ALLOCATOR_H__ | ||
| 17 | #define __YAFFS_ALLOCATOR_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev); | ||
| 22 | void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev); | ||
| 23 | |||
| 24 | struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev); | ||
| 25 | void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn); | ||
| 26 | |||
| 27 | struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev); | ||
| 28 | void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj); | ||
| 29 | |||
| 30 | #endif | ||
diff --git a/fs/yaffs2/yaffs_attribs.c b/fs/yaffs2/yaffs_attribs.c new file mode 100644 index 00000000000..9b47d376310 --- /dev/null +++ b/fs/yaffs2/yaffs_attribs.c | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_guts.h" | ||
| 15 | #include "yaffs_attribs.h" | ||
| 16 | |||
| 17 | void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh) | ||
| 18 | { | ||
| 19 | obj->yst_uid = oh->yst_uid; | ||
| 20 | obj->yst_gid = oh->yst_gid; | ||
| 21 | obj->yst_atime = oh->yst_atime; | ||
| 22 | obj->yst_mtime = oh->yst_mtime; | ||
| 23 | obj->yst_ctime = oh->yst_ctime; | ||
| 24 | obj->yst_rdev = oh->yst_rdev; | ||
| 25 | } | ||
| 26 | |||
| 27 | void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj) | ||
| 28 | { | ||
| 29 | oh->yst_uid = obj->yst_uid; | ||
| 30 | oh->yst_gid = obj->yst_gid; | ||
| 31 | oh->yst_atime = obj->yst_atime; | ||
| 32 | oh->yst_mtime = obj->yst_mtime; | ||
| 33 | oh->yst_ctime = obj->yst_ctime; | ||
| 34 | oh->yst_rdev = obj->yst_rdev; | ||
| 35 | |||
| 36 | } | ||
| 37 | |||
| 38 | void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c) | ||
| 39 | { | ||
| 40 | obj->yst_mtime = Y_CURRENT_TIME; | ||
| 41 | if (do_a) | ||
| 42 | obj->yst_atime = obj->yst_mtime; | ||
| 43 | if (do_c) | ||
| 44 | obj->yst_ctime = obj->yst_mtime; | ||
| 45 | } | ||
| 46 | |||
| 47 | void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev) | ||
| 48 | { | ||
| 49 | yaffs_load_current_time(obj, 1, 1); | ||
| 50 | obj->yst_rdev = rdev; | ||
| 51 | obj->yst_uid = uid; | ||
| 52 | obj->yst_gid = gid; | ||
| 53 | } | ||
| 54 | |||
| 55 | loff_t yaffs_get_file_size(struct yaffs_obj *obj) | ||
| 56 | { | ||
| 57 | YCHAR *alias = NULL; | ||
| 58 | obj = yaffs_get_equivalent_obj(obj); | ||
| 59 | |||
| 60 | switch (obj->variant_type) { | ||
| 61 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 62 | return obj->variant.file_variant.file_size; | ||
| 63 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 64 | alias = obj->variant.symlink_variant.alias; | ||
| 65 | if (!alias) | ||
| 66 | return 0; | ||
| 67 | return strnlen(alias, YAFFS_MAX_ALIAS_LENGTH); | ||
| 68 | default: | ||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr) | ||
| 74 | { | ||
| 75 | unsigned int valid = attr->ia_valid; | ||
| 76 | |||
| 77 | if (valid & ATTR_MODE) | ||
| 78 | obj->yst_mode = attr->ia_mode; | ||
| 79 | if (valid & ATTR_UID) | ||
| 80 | obj->yst_uid = attr->ia_uid; | ||
| 81 | if (valid & ATTR_GID) | ||
| 82 | obj->yst_gid = attr->ia_gid; | ||
| 83 | |||
| 84 | if (valid & ATTR_ATIME) | ||
| 85 | obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime); | ||
| 86 | if (valid & ATTR_CTIME) | ||
| 87 | obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime); | ||
| 88 | if (valid & ATTR_MTIME) | ||
| 89 | obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime); | ||
| 90 | |||
| 91 | if (valid & ATTR_SIZE) | ||
| 92 | yaffs_resize_file(obj, attr->ia_size); | ||
| 93 | |||
| 94 | yaffs_update_oh(obj, NULL, 1, 0, 0, NULL); | ||
| 95 | |||
| 96 | return YAFFS_OK; | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr) | ||
| 101 | { | ||
| 102 | unsigned int valid = 0; | ||
| 103 | |||
| 104 | attr->ia_mode = obj->yst_mode; | ||
| 105 | valid |= ATTR_MODE; | ||
| 106 | attr->ia_uid = obj->yst_uid; | ||
| 107 | valid |= ATTR_UID; | ||
| 108 | attr->ia_gid = obj->yst_gid; | ||
| 109 | valid |= ATTR_GID; | ||
| 110 | |||
| 111 | Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime; | ||
| 112 | valid |= ATTR_ATIME; | ||
| 113 | Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime; | ||
| 114 | valid |= ATTR_CTIME; | ||
| 115 | Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime; | ||
| 116 | valid |= ATTR_MTIME; | ||
| 117 | |||
| 118 | attr->ia_size = yaffs_get_file_size(obj); | ||
| 119 | valid |= ATTR_SIZE; | ||
| 120 | |||
| 121 | attr->ia_valid = valid; | ||
| 122 | |||
| 123 | return YAFFS_OK; | ||
| 124 | } | ||
diff --git a/fs/yaffs2/yaffs_attribs.h b/fs/yaffs2/yaffs_attribs.h new file mode 100644 index 00000000000..33d541d6944 --- /dev/null +++ b/fs/yaffs2/yaffs_attribs.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_ATTRIBS_H__ | ||
| 17 | #define __YAFFS_ATTRIBS_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh); | ||
| 22 | void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj); | ||
| 23 | void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev); | ||
| 24 | void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c); | ||
| 25 | int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr); | ||
| 26 | int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr); | ||
| 27 | |||
| 28 | #endif | ||
diff --git a/fs/yaffs2/yaffs_bitmap.c b/fs/yaffs2/yaffs_bitmap.c new file mode 100644 index 00000000000..7df42cd0066 --- /dev/null +++ b/fs/yaffs2/yaffs_bitmap.c | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_bitmap.h" | ||
| 15 | #include "yaffs_trace.h" | ||
| 16 | /* | ||
| 17 | * Chunk bitmap manipulations | ||
| 18 | */ | ||
| 19 | |||
| 20 | static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk) | ||
| 21 | { | ||
| 22 | if (blk < dev->internal_start_block || blk > dev->internal_end_block) { | ||
| 23 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 24 | "BlockBits block %d is not valid", | ||
| 25 | blk); | ||
| 26 | YBUG(); | ||
| 27 | } | ||
| 28 | return dev->chunk_bits + | ||
| 29 | (dev->chunk_bit_stride * (blk - dev->internal_start_block)); | ||
| 30 | } | ||
| 31 | |||
| 32 | void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk) | ||
| 33 | { | ||
| 34 | if (blk < dev->internal_start_block || blk > dev->internal_end_block || | ||
| 35 | chunk < 0 || chunk >= dev->param.chunks_per_block) { | ||
| 36 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 37 | "Chunk Id (%d:%d) invalid", | ||
| 38 | blk, chunk); | ||
| 39 | YBUG(); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk) | ||
| 44 | { | ||
| 45 | u8 *blk_bits = yaffs_block_bits(dev, blk); | ||
| 46 | |||
| 47 | memset(blk_bits, 0, dev->chunk_bit_stride); | ||
| 48 | } | ||
| 49 | |||
| 50 | void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk) | ||
| 51 | { | ||
| 52 | u8 *blk_bits = yaffs_block_bits(dev, blk); | ||
| 53 | |||
| 54 | yaffs_verify_chunk_bit_id(dev, blk, chunk); | ||
| 55 | |||
| 56 | blk_bits[chunk / 8] &= ~(1 << (chunk & 7)); | ||
| 57 | } | ||
| 58 | |||
| 59 | void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk) | ||
| 60 | { | ||
| 61 | u8 *blk_bits = yaffs_block_bits(dev, blk); | ||
| 62 | |||
| 63 | yaffs_verify_chunk_bit_id(dev, blk, chunk); | ||
| 64 | |||
| 65 | blk_bits[chunk / 8] |= (1 << (chunk & 7)); | ||
| 66 | } | ||
| 67 | |||
| 68 | int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk) | ||
| 69 | { | ||
| 70 | u8 *blk_bits = yaffs_block_bits(dev, blk); | ||
| 71 | yaffs_verify_chunk_bit_id(dev, blk, chunk); | ||
| 72 | |||
| 73 | return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk) | ||
| 77 | { | ||
| 78 | u8 *blk_bits = yaffs_block_bits(dev, blk); | ||
| 79 | int i; | ||
| 80 | for (i = 0; i < dev->chunk_bit_stride; i++) { | ||
| 81 | if (*blk_bits) | ||
| 82 | return 1; | ||
| 83 | blk_bits++; | ||
| 84 | } | ||
| 85 | return 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk) | ||
| 89 | { | ||
| 90 | u8 *blk_bits = yaffs_block_bits(dev, blk); | ||
| 91 | int i; | ||
| 92 | int n = 0; | ||
| 93 | |||
| 94 | for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++) | ||
| 95 | n += hweight8(*blk_bits); | ||
| 96 | |||
| 97 | return n; | ||
| 98 | } | ||
diff --git a/fs/yaffs2/yaffs_bitmap.h b/fs/yaffs2/yaffs_bitmap.h new file mode 100644 index 00000000000..cf9ea58da0d --- /dev/null +++ b/fs/yaffs2/yaffs_bitmap.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | /* | ||
| 17 | * Chunk bitmap manipulations | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef __YAFFS_BITMAP_H__ | ||
| 21 | #define __YAFFS_BITMAP_H__ | ||
| 22 | |||
| 23 | #include "yaffs_guts.h" | ||
| 24 | |||
| 25 | void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk); | ||
| 26 | void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk); | ||
| 27 | void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk); | ||
| 28 | void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk); | ||
| 29 | int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk); | ||
| 30 | int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk); | ||
| 31 | int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk); | ||
| 32 | |||
| 33 | #endif | ||
diff --git a/fs/yaffs2/yaffs_checkptrw.c b/fs/yaffs2/yaffs_checkptrw.c new file mode 100644 index 00000000000..4e40f437e65 --- /dev/null +++ b/fs/yaffs2/yaffs_checkptrw.c | |||
| @@ -0,0 +1,415 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_checkptrw.h" | ||
| 15 | #include "yaffs_getblockinfo.h" | ||
| 16 | |||
| 17 | static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev) | ||
| 18 | { | ||
| 19 | int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks; | ||
| 20 | |||
| 21 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 22 | "checkpt blocks_avail = %d", blocks_avail); | ||
| 23 | |||
| 24 | return (blocks_avail <= 0) ? 0 : 1; | ||
| 25 | } | ||
| 26 | |||
| 27 | static int yaffs_checkpt_erase(struct yaffs_dev *dev) | ||
| 28 | { | ||
| 29 | int i; | ||
| 30 | |||
| 31 | if (!dev->param.erase_fn) | ||
| 32 | return 0; | ||
| 33 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 34 | "checking blocks %d to %d", | ||
| 35 | dev->internal_start_block, dev->internal_end_block); | ||
| 36 | |||
| 37 | for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { | ||
| 38 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, i); | ||
| 39 | if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) { | ||
| 40 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 41 | "erasing checkpt block %d", i); | ||
| 42 | |||
| 43 | dev->n_erasures++; | ||
| 44 | |||
| 45 | if (dev->param. | ||
| 46 | erase_fn(dev, | ||
| 47 | i - dev->block_offset /* realign */ )) { | ||
| 48 | bi->block_state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 49 | dev->n_erased_blocks++; | ||
| 50 | dev->n_free_chunks += | ||
| 51 | dev->param.chunks_per_block; | ||
| 52 | } else { | ||
| 53 | dev->param.bad_block_fn(dev, i); | ||
| 54 | bi->block_state = YAFFS_BLOCK_STATE_DEAD; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | dev->blocks_in_checkpt = 0; | ||
| 60 | |||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev) | ||
| 65 | { | ||
| 66 | int i; | ||
| 67 | int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks; | ||
| 68 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 69 | "allocating checkpt block: erased %d reserved %d avail %d next %d ", | ||
| 70 | dev->n_erased_blocks, dev->param.n_reserved_blocks, | ||
| 71 | blocks_avail, dev->checkpt_next_block); | ||
| 72 | |||
| 73 | if (dev->checkpt_next_block >= 0 && | ||
| 74 | dev->checkpt_next_block <= dev->internal_end_block && | ||
| 75 | blocks_avail > 0) { | ||
| 76 | |||
| 77 | for (i = dev->checkpt_next_block; i <= dev->internal_end_block; | ||
| 78 | i++) { | ||
| 79 | struct yaffs_block_info *bi = | ||
| 80 | yaffs_get_block_info(dev, i); | ||
| 81 | if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { | ||
| 82 | dev->checkpt_next_block = i + 1; | ||
| 83 | dev->checkpt_cur_block = i; | ||
| 84 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 85 | "allocating checkpt block %d", i); | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks"); | ||
| 91 | |||
| 92 | dev->checkpt_next_block = -1; | ||
| 93 | dev->checkpt_cur_block = -1; | ||
| 94 | } | ||
| 95 | |||
| 96 | static void yaffs2_checkpt_find_block(struct yaffs_dev *dev) | ||
| 97 | { | ||
| 98 | int i; | ||
| 99 | struct yaffs_ext_tags tags; | ||
| 100 | |||
| 101 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 102 | "find next checkpt block: start: blocks %d next %d", | ||
| 103 | dev->blocks_in_checkpt, dev->checkpt_next_block); | ||
| 104 | |||
| 105 | if (dev->blocks_in_checkpt < dev->checkpt_max_blocks) | ||
| 106 | for (i = dev->checkpt_next_block; i <= dev->internal_end_block; | ||
| 107 | i++) { | ||
| 108 | int chunk = i * dev->param.chunks_per_block; | ||
| 109 | int realigned_chunk = chunk - dev->chunk_offset; | ||
| 110 | |||
| 111 | dev->param.read_chunk_tags_fn(dev, realigned_chunk, | ||
| 112 | NULL, &tags); | ||
| 113 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 114 | "find next checkpt block: search: block %d oid %d seq %d eccr %d", | ||
| 115 | i, tags.obj_id, tags.seq_number, | ||
| 116 | tags.ecc_result); | ||
| 117 | |||
| 118 | if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) { | ||
| 119 | /* Right kind of block */ | ||
| 120 | dev->checkpt_next_block = tags.obj_id; | ||
| 121 | dev->checkpt_cur_block = i; | ||
| 122 | dev->checkpt_block_list[dev-> | ||
| 123 | blocks_in_checkpt] = i; | ||
| 124 | dev->blocks_in_checkpt++; | ||
| 125 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 126 | "found checkpt block %d", i); | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks"); | ||
| 132 | |||
| 133 | dev->checkpt_next_block = -1; | ||
| 134 | dev->checkpt_cur_block = -1; | ||
| 135 | } | ||
| 136 | |||
| 137 | int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing) | ||
| 138 | { | ||
| 139 | |||
| 140 | dev->checkpt_open_write = writing; | ||
| 141 | |||
| 142 | /* Got the functions we need? */ | ||
| 143 | if (!dev->param.write_chunk_tags_fn || | ||
| 144 | !dev->param.read_chunk_tags_fn || | ||
| 145 | !dev->param.erase_fn || !dev->param.bad_block_fn) | ||
| 146 | return 0; | ||
| 147 | |||
| 148 | if (writing && !yaffs2_checkpt_space_ok(dev)) | ||
| 149 | return 0; | ||
| 150 | |||
| 151 | if (!dev->checkpt_buffer) | ||
| 152 | dev->checkpt_buffer = | ||
| 153 | kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); | ||
| 154 | if (!dev->checkpt_buffer) | ||
| 155 | return 0; | ||
| 156 | |||
| 157 | dev->checkpt_page_seq = 0; | ||
| 158 | dev->checkpt_byte_count = 0; | ||
| 159 | dev->checkpt_sum = 0; | ||
| 160 | dev->checkpt_xor = 0; | ||
| 161 | dev->checkpt_cur_block = -1; | ||
| 162 | dev->checkpt_cur_chunk = -1; | ||
| 163 | dev->checkpt_next_block = dev->internal_start_block; | ||
| 164 | |||
| 165 | /* Erase all the blocks in the checkpoint area */ | ||
| 166 | if (writing) { | ||
| 167 | memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk); | ||
| 168 | dev->checkpt_byte_offs = 0; | ||
| 169 | return yaffs_checkpt_erase(dev); | ||
| 170 | } else { | ||
| 171 | int i; | ||
| 172 | /* Set to a value that will kick off a read */ | ||
| 173 | dev->checkpt_byte_offs = dev->data_bytes_per_chunk; | ||
| 174 | /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully) | ||
| 175 | * going to be way more than we need */ | ||
| 176 | dev->blocks_in_checkpt = 0; | ||
| 177 | dev->checkpt_max_blocks = | ||
| 178 | (dev->internal_end_block - dev->internal_start_block) / 16 + | ||
| 179 | 2; | ||
| 180 | dev->checkpt_block_list = | ||
| 181 | kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS); | ||
| 182 | if (!dev->checkpt_block_list) | ||
| 183 | return 0; | ||
| 184 | |||
| 185 | for (i = 0; i < dev->checkpt_max_blocks; i++) | ||
| 186 | dev->checkpt_block_list[i] = -1; | ||
| 187 | } | ||
| 188 | |||
| 189 | return 1; | ||
| 190 | } | ||
| 191 | |||
| 192 | int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum) | ||
| 193 | { | ||
| 194 | u32 composite_sum; | ||
| 195 | composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF); | ||
| 196 | *sum = composite_sum; | ||
| 197 | return 1; | ||
| 198 | } | ||
| 199 | |||
| 200 | static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev) | ||
| 201 | { | ||
| 202 | int chunk; | ||
| 203 | int realigned_chunk; | ||
| 204 | |||
| 205 | struct yaffs_ext_tags tags; | ||
| 206 | |||
| 207 | if (dev->checkpt_cur_block < 0) { | ||
| 208 | yaffs2_checkpt_find_erased_block(dev); | ||
| 209 | dev->checkpt_cur_chunk = 0; | ||
| 210 | } | ||
| 211 | |||
| 212 | if (dev->checkpt_cur_block < 0) | ||
| 213 | return 0; | ||
| 214 | |||
| 215 | tags.is_deleted = 0; | ||
| 216 | tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */ | ||
| 217 | tags.chunk_id = dev->checkpt_page_seq + 1; | ||
| 218 | tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA; | ||
| 219 | tags.n_bytes = dev->data_bytes_per_chunk; | ||
| 220 | if (dev->checkpt_cur_chunk == 0) { | ||
| 221 | /* First chunk we write for the block? Set block state to | ||
| 222 | checkpoint */ | ||
| 223 | struct yaffs_block_info *bi = | ||
| 224 | yaffs_get_block_info(dev, dev->checkpt_cur_block); | ||
| 225 | bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT; | ||
| 226 | dev->blocks_in_checkpt++; | ||
| 227 | } | ||
| 228 | |||
| 229 | chunk = | ||
| 230 | dev->checkpt_cur_block * dev->param.chunks_per_block + | ||
| 231 | dev->checkpt_cur_chunk; | ||
| 232 | |||
| 233 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 234 | "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d", | ||
| 235 | chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, | ||
| 236 | tags.obj_id, tags.chunk_id); | ||
| 237 | |||
| 238 | realigned_chunk = chunk - dev->chunk_offset; | ||
| 239 | |||
| 240 | dev->n_page_writes++; | ||
| 241 | |||
| 242 | dev->param.write_chunk_tags_fn(dev, realigned_chunk, | ||
| 243 | dev->checkpt_buffer, &tags); | ||
| 244 | dev->checkpt_byte_offs = 0; | ||
| 245 | dev->checkpt_page_seq++; | ||
| 246 | dev->checkpt_cur_chunk++; | ||
| 247 | if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) { | ||
| 248 | dev->checkpt_cur_chunk = 0; | ||
| 249 | dev->checkpt_cur_block = -1; | ||
| 250 | } | ||
| 251 | memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk); | ||
| 252 | |||
| 253 | return 1; | ||
| 254 | } | ||
| 255 | |||
| 256 | int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes) | ||
| 257 | { | ||
| 258 | int i = 0; | ||
| 259 | int ok = 1; | ||
| 260 | |||
| 261 | u8 *data_bytes = (u8 *) data; | ||
| 262 | |||
| 263 | if (!dev->checkpt_buffer) | ||
| 264 | return 0; | ||
| 265 | |||
| 266 | if (!dev->checkpt_open_write) | ||
| 267 | return -1; | ||
| 268 | |||
| 269 | while (i < n_bytes && ok) { | ||
| 270 | dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes; | ||
| 271 | dev->checkpt_sum += *data_bytes; | ||
| 272 | dev->checkpt_xor ^= *data_bytes; | ||
| 273 | |||
| 274 | dev->checkpt_byte_offs++; | ||
| 275 | i++; | ||
| 276 | data_bytes++; | ||
| 277 | dev->checkpt_byte_count++; | ||
| 278 | |||
| 279 | if (dev->checkpt_byte_offs < 0 || | ||
| 280 | dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) | ||
| 281 | ok = yaffs2_checkpt_flush_buffer(dev); | ||
| 282 | } | ||
| 283 | |||
| 284 | return i; | ||
| 285 | } | ||
| 286 | |||
| 287 | int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes) | ||
| 288 | { | ||
| 289 | int i = 0; | ||
| 290 | int ok = 1; | ||
| 291 | struct yaffs_ext_tags tags; | ||
| 292 | |||
| 293 | int chunk; | ||
| 294 | int realigned_chunk; | ||
| 295 | |||
| 296 | u8 *data_bytes = (u8 *) data; | ||
| 297 | |||
| 298 | if (!dev->checkpt_buffer) | ||
| 299 | return 0; | ||
| 300 | |||
| 301 | if (dev->checkpt_open_write) | ||
| 302 | return -1; | ||
| 303 | |||
| 304 | while (i < n_bytes && ok) { | ||
| 305 | |||
| 306 | if (dev->checkpt_byte_offs < 0 || | ||
| 307 | dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) { | ||
| 308 | |||
| 309 | if (dev->checkpt_cur_block < 0) { | ||
| 310 | yaffs2_checkpt_find_block(dev); | ||
| 311 | dev->checkpt_cur_chunk = 0; | ||
| 312 | } | ||
| 313 | |||
| 314 | if (dev->checkpt_cur_block < 0) | ||
| 315 | ok = 0; | ||
| 316 | else { | ||
| 317 | chunk = dev->checkpt_cur_block * | ||
| 318 | dev->param.chunks_per_block + | ||
| 319 | dev->checkpt_cur_chunk; | ||
| 320 | |||
| 321 | realigned_chunk = chunk - dev->chunk_offset; | ||
| 322 | |||
| 323 | dev->n_page_reads++; | ||
| 324 | |||
| 325 | /* read in the next chunk */ | ||
| 326 | dev->param.read_chunk_tags_fn(dev, | ||
| 327 | realigned_chunk, | ||
| 328 | dev-> | ||
| 329 | checkpt_buffer, | ||
| 330 | &tags); | ||
| 331 | |||
| 332 | if (tags.chunk_id != (dev->checkpt_page_seq + 1) | ||
| 333 | || tags.ecc_result > YAFFS_ECC_RESULT_FIXED | ||
| 334 | || tags.seq_number != | ||
| 335 | YAFFS_SEQUENCE_CHECKPOINT_DATA) | ||
| 336 | ok = 0; | ||
| 337 | |||
| 338 | dev->checkpt_byte_offs = 0; | ||
| 339 | dev->checkpt_page_seq++; | ||
| 340 | dev->checkpt_cur_chunk++; | ||
| 341 | |||
| 342 | if (dev->checkpt_cur_chunk >= | ||
| 343 | dev->param.chunks_per_block) | ||
| 344 | dev->checkpt_cur_block = -1; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | if (ok) { | ||
| 349 | *data_bytes = | ||
| 350 | dev->checkpt_buffer[dev->checkpt_byte_offs]; | ||
| 351 | dev->checkpt_sum += *data_bytes; | ||
| 352 | dev->checkpt_xor ^= *data_bytes; | ||
| 353 | dev->checkpt_byte_offs++; | ||
| 354 | i++; | ||
| 355 | data_bytes++; | ||
| 356 | dev->checkpt_byte_count++; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | return i; | ||
| 361 | } | ||
| 362 | |||
| 363 | int yaffs_checkpt_close(struct yaffs_dev *dev) | ||
| 364 | { | ||
| 365 | |||
| 366 | if (dev->checkpt_open_write) { | ||
| 367 | if (dev->checkpt_byte_offs != 0) | ||
| 368 | yaffs2_checkpt_flush_buffer(dev); | ||
| 369 | } else if (dev->checkpt_block_list) { | ||
| 370 | int i; | ||
| 371 | for (i = 0; | ||
| 372 | i < dev->blocks_in_checkpt | ||
| 373 | && dev->checkpt_block_list[i] >= 0; i++) { | ||
| 374 | int blk = dev->checkpt_block_list[i]; | ||
| 375 | struct yaffs_block_info *bi = NULL; | ||
| 376 | if (dev->internal_start_block <= blk | ||
| 377 | && blk <= dev->internal_end_block) | ||
| 378 | bi = yaffs_get_block_info(dev, blk); | ||
| 379 | if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY) | ||
| 380 | bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT; | ||
| 381 | else { | ||
| 382 | /* Todo this looks odd... */ | ||
| 383 | } | ||
| 384 | } | ||
| 385 | kfree(dev->checkpt_block_list); | ||
| 386 | dev->checkpt_block_list = NULL; | ||
| 387 | } | ||
| 388 | |||
| 389 | dev->n_free_chunks -= | ||
| 390 | dev->blocks_in_checkpt * dev->param.chunks_per_block; | ||
| 391 | dev->n_erased_blocks -= dev->blocks_in_checkpt; | ||
| 392 | |||
| 393 | yaffs_trace(YAFFS_TRACE_CHECKPOINT,"checkpoint byte count %d", | ||
| 394 | dev->checkpt_byte_count); | ||
| 395 | |||
| 396 | if (dev->checkpt_buffer) { | ||
| 397 | /* free the buffer */ | ||
| 398 | kfree(dev->checkpt_buffer); | ||
| 399 | dev->checkpt_buffer = NULL; | ||
| 400 | return 1; | ||
| 401 | } else { | ||
| 402 | return 0; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev) | ||
| 407 | { | ||
| 408 | /* Erase the checkpoint data */ | ||
| 409 | |||
| 410 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 411 | "checkpoint invalidate of %d blocks", | ||
| 412 | dev->blocks_in_checkpt); | ||
| 413 | |||
| 414 | return yaffs_checkpt_erase(dev); | ||
| 415 | } | ||
diff --git a/fs/yaffs2/yaffs_checkptrw.h b/fs/yaffs2/yaffs_checkptrw.h new file mode 100644 index 00000000000..361c6067717 --- /dev/null +++ b/fs/yaffs2/yaffs_checkptrw.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_CHECKPTRW_H__ | ||
| 17 | #define __YAFFS_CHECKPTRW_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing); | ||
| 22 | |||
| 23 | int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes); | ||
| 24 | |||
| 25 | int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes); | ||
| 26 | |||
| 27 | int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum); | ||
| 28 | |||
| 29 | int yaffs_checkpt_close(struct yaffs_dev *dev); | ||
| 30 | |||
| 31 | int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev); | ||
| 32 | |||
| 33 | #endif | ||
diff --git a/fs/yaffs2/yaffs_ecc.c b/fs/yaffs2/yaffs_ecc.c new file mode 100644 index 00000000000..e95a8069a8c --- /dev/null +++ b/fs/yaffs2/yaffs_ecc.c | |||
| @@ -0,0 +1,298 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | /* | ||
| 15 | * This code implements the ECC algorithm used in SmartMedia. | ||
| 16 | * | ||
| 17 | * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. | ||
| 18 | * The two unused bit are set to 1. | ||
| 19 | * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC | ||
| 20 | * blocks are used on a 512-byte NAND page. | ||
| 21 | * | ||
| 22 | */ | ||
| 23 | |||
| 24 | /* Table generated by gen-ecc.c | ||
| 25 | * Using a table means we do not have to calculate p1..p4 and p1'..p4' | ||
| 26 | * for each byte of data. These are instead provided in a table in bits7..2. | ||
| 27 | * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore | ||
| 28 | * this bytes influence on the line parity. | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include "yportenv.h" | ||
| 32 | |||
| 33 | #include "yaffs_ecc.h" | ||
| 34 | |||
| 35 | static const unsigned char column_parity_table[] = { | ||
| 36 | 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, | ||
| 37 | 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, | ||
| 38 | 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, | ||
| 39 | 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, | ||
| 40 | 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, | ||
| 41 | 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, | ||
| 42 | 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, | ||
| 43 | 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, | ||
| 44 | 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, | ||
| 45 | 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, | ||
| 46 | 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, | ||
| 47 | 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, | ||
| 48 | 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, | ||
| 49 | 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, | ||
| 50 | 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, | ||
| 51 | 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, | ||
| 52 | 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, | ||
| 53 | 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, | ||
| 54 | 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, | ||
| 55 | 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, | ||
| 56 | 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, | ||
| 57 | 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, | ||
| 58 | 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, | ||
| 59 | 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, | ||
| 60 | 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, | ||
| 61 | 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, | ||
| 62 | 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, | ||
| 63 | 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, | ||
| 64 | 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, | ||
| 65 | 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, | ||
| 66 | 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, | ||
| 67 | 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, | ||
| 68 | }; | ||
| 69 | |||
| 70 | |||
| 71 | /* Calculate the ECC for a 256-byte block of data */ | ||
| 72 | void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc) | ||
| 73 | { | ||
| 74 | unsigned int i; | ||
| 75 | |||
| 76 | unsigned char col_parity = 0; | ||
| 77 | unsigned char line_parity = 0; | ||
| 78 | unsigned char line_parity_prime = 0; | ||
| 79 | unsigned char t; | ||
| 80 | unsigned char b; | ||
| 81 | |||
| 82 | for (i = 0; i < 256; i++) { | ||
| 83 | b = column_parity_table[*data++]; | ||
| 84 | col_parity ^= b; | ||
| 85 | |||
| 86 | if (b & 0x01) { /* odd number of bits in the byte */ | ||
| 87 | line_parity ^= i; | ||
| 88 | line_parity_prime ^= ~i; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | ecc[2] = (~col_parity) | 0x03; | ||
| 93 | |||
| 94 | t = 0; | ||
| 95 | if (line_parity & 0x80) | ||
| 96 | t |= 0x80; | ||
| 97 | if (line_parity_prime & 0x80) | ||
| 98 | t |= 0x40; | ||
| 99 | if (line_parity & 0x40) | ||
| 100 | t |= 0x20; | ||
| 101 | if (line_parity_prime & 0x40) | ||
| 102 | t |= 0x10; | ||
| 103 | if (line_parity & 0x20) | ||
| 104 | t |= 0x08; | ||
| 105 | if (line_parity_prime & 0x20) | ||
| 106 | t |= 0x04; | ||
| 107 | if (line_parity & 0x10) | ||
| 108 | t |= 0x02; | ||
| 109 | if (line_parity_prime & 0x10) | ||
| 110 | t |= 0x01; | ||
| 111 | ecc[1] = ~t; | ||
| 112 | |||
| 113 | t = 0; | ||
| 114 | if (line_parity & 0x08) | ||
| 115 | t |= 0x80; | ||
| 116 | if (line_parity_prime & 0x08) | ||
| 117 | t |= 0x40; | ||
| 118 | if (line_parity & 0x04) | ||
| 119 | t |= 0x20; | ||
| 120 | if (line_parity_prime & 0x04) | ||
| 121 | t |= 0x10; | ||
| 122 | if (line_parity & 0x02) | ||
| 123 | t |= 0x08; | ||
| 124 | if (line_parity_prime & 0x02) | ||
| 125 | t |= 0x04; | ||
| 126 | if (line_parity & 0x01) | ||
| 127 | t |= 0x02; | ||
| 128 | if (line_parity_prime & 0x01) | ||
| 129 | t |= 0x01; | ||
| 130 | ecc[0] = ~t; | ||
| 131 | |||
| 132 | #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER | ||
| 133 | /* Swap the bytes into the wrong order */ | ||
| 134 | t = ecc[0]; | ||
| 135 | ecc[0] = ecc[1]; | ||
| 136 | ecc[1] = t; | ||
| 137 | #endif | ||
| 138 | } | ||
| 139 | |||
| 140 | /* Correct the ECC on a 256 byte block of data */ | ||
| 141 | |||
| 142 | int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc, | ||
| 143 | const unsigned char *test_ecc) | ||
| 144 | { | ||
| 145 | unsigned char d0, d1, d2; /* deltas */ | ||
| 146 | |||
| 147 | d0 = read_ecc[0] ^ test_ecc[0]; | ||
| 148 | d1 = read_ecc[1] ^ test_ecc[1]; | ||
| 149 | d2 = read_ecc[2] ^ test_ecc[2]; | ||
| 150 | |||
| 151 | if ((d0 | d1 | d2) == 0) | ||
| 152 | return 0; /* no error */ | ||
| 153 | |||
| 154 | if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 && | ||
| 155 | ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 && | ||
| 156 | ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) { | ||
| 157 | /* Single bit (recoverable) error in data */ | ||
| 158 | |||
| 159 | unsigned byte; | ||
| 160 | unsigned bit; | ||
| 161 | |||
| 162 | #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER | ||
| 163 | /* swap the bytes to correct for the wrong order */ | ||
| 164 | unsigned char t; | ||
| 165 | |||
| 166 | t = d0; | ||
| 167 | d0 = d1; | ||
| 168 | d1 = t; | ||
| 169 | #endif | ||
| 170 | |||
| 171 | bit = byte = 0; | ||
| 172 | |||
| 173 | if (d1 & 0x80) | ||
| 174 | byte |= 0x80; | ||
| 175 | if (d1 & 0x20) | ||
| 176 | byte |= 0x40; | ||
| 177 | if (d1 & 0x08) | ||
| 178 | byte |= 0x20; | ||
| 179 | if (d1 & 0x02) | ||
| 180 | byte |= 0x10; | ||
| 181 | if (d0 & 0x80) | ||
| 182 | byte |= 0x08; | ||
| 183 | if (d0 & 0x20) | ||
| 184 | byte |= 0x04; | ||
| 185 | if (d0 & 0x08) | ||
| 186 | byte |= 0x02; | ||
| 187 | if (d0 & 0x02) | ||
| 188 | byte |= 0x01; | ||
| 189 | |||
| 190 | if (d2 & 0x80) | ||
| 191 | bit |= 0x04; | ||
| 192 | if (d2 & 0x20) | ||
| 193 | bit |= 0x02; | ||
| 194 | if (d2 & 0x08) | ||
| 195 | bit |= 0x01; | ||
| 196 | |||
| 197 | data[byte] ^= (1 << bit); | ||
| 198 | |||
| 199 | return 1; /* Corrected the error */ | ||
| 200 | } | ||
| 201 | |||
| 202 | if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) { | ||
| 203 | /* Reccoverable error in ecc */ | ||
| 204 | |||
| 205 | read_ecc[0] = test_ecc[0]; | ||
| 206 | read_ecc[1] = test_ecc[1]; | ||
| 207 | read_ecc[2] = test_ecc[2]; | ||
| 208 | |||
| 209 | return 1; /* Corrected the error */ | ||
| 210 | } | ||
| 211 | |||
| 212 | /* Unrecoverable error */ | ||
| 213 | |||
| 214 | return -1; | ||
| 215 | |||
| 216 | } | ||
| 217 | |||
| 218 | /* | ||
| 219 | * ECCxxxOther does ECC calcs on arbitrary n bytes of data | ||
| 220 | */ | ||
| 221 | void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes, | ||
| 222 | struct yaffs_ecc_other *ecc_other) | ||
| 223 | { | ||
| 224 | unsigned int i; | ||
| 225 | |||
| 226 | unsigned char col_parity = 0; | ||
| 227 | unsigned line_parity = 0; | ||
| 228 | unsigned line_parity_prime = 0; | ||
| 229 | unsigned char b; | ||
| 230 | |||
| 231 | for (i = 0; i < n_bytes; i++) { | ||
| 232 | b = column_parity_table[*data++]; | ||
| 233 | col_parity ^= b; | ||
| 234 | |||
| 235 | if (b & 0x01) { | ||
| 236 | /* odd number of bits in the byte */ | ||
| 237 | line_parity ^= i; | ||
| 238 | line_parity_prime ^= ~i; | ||
| 239 | } | ||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | ecc_other->col_parity = (col_parity >> 2) & 0x3f; | ||
| 244 | ecc_other->line_parity = line_parity; | ||
| 245 | ecc_other->line_parity_prime = line_parity_prime; | ||
| 246 | } | ||
| 247 | |||
| 248 | int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes, | ||
| 249 | struct yaffs_ecc_other *read_ecc, | ||
| 250 | const struct yaffs_ecc_other *test_ecc) | ||
| 251 | { | ||
| 252 | unsigned char delta_col; /* column parity delta */ | ||
| 253 | unsigned delta_line; /* line parity delta */ | ||
| 254 | unsigned delta_line_prime; /* line parity delta */ | ||
| 255 | unsigned bit; | ||
| 256 | |||
| 257 | delta_col = read_ecc->col_parity ^ test_ecc->col_parity; | ||
| 258 | delta_line = read_ecc->line_parity ^ test_ecc->line_parity; | ||
| 259 | delta_line_prime = | ||
| 260 | read_ecc->line_parity_prime ^ test_ecc->line_parity_prime; | ||
| 261 | |||
| 262 | if ((delta_col | delta_line | delta_line_prime) == 0) | ||
| 263 | return 0; /* no error */ | ||
| 264 | |||
| 265 | if (delta_line == ~delta_line_prime && | ||
| 266 | (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) { | ||
| 267 | /* Single bit (recoverable) error in data */ | ||
| 268 | |||
| 269 | bit = 0; | ||
| 270 | |||
| 271 | if (delta_col & 0x20) | ||
| 272 | bit |= 0x04; | ||
| 273 | if (delta_col & 0x08) | ||
| 274 | bit |= 0x02; | ||
| 275 | if (delta_col & 0x02) | ||
| 276 | bit |= 0x01; | ||
| 277 | |||
| 278 | if (delta_line >= n_bytes) | ||
| 279 | return -1; | ||
| 280 | |||
| 281 | data[delta_line] ^= (1 << bit); | ||
| 282 | |||
| 283 | return 1; /* corrected */ | ||
| 284 | } | ||
| 285 | |||
| 286 | if ((hweight32(delta_line) + | ||
| 287 | hweight32(delta_line_prime) + | ||
| 288 | hweight8(delta_col)) == 1) { | ||
| 289 | /* Reccoverable error in ecc */ | ||
| 290 | |||
| 291 | *read_ecc = *test_ecc; | ||
| 292 | return 1; /* corrected */ | ||
| 293 | } | ||
| 294 | |||
| 295 | /* Unrecoverable error */ | ||
| 296 | |||
| 297 | return -1; | ||
| 298 | } | ||
diff --git a/fs/yaffs2/yaffs_ecc.h b/fs/yaffs2/yaffs_ecc.h new file mode 100644 index 00000000000..b0c461d699e --- /dev/null +++ b/fs/yaffs2/yaffs_ecc.h | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | /* | ||
| 17 | * This code implements the ECC algorithm used in SmartMedia. | ||
| 18 | * | ||
| 19 | * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. | ||
| 20 | * The two unused bit are set to 1. | ||
| 21 | * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC | ||
| 22 | * blocks are used on a 512-byte NAND page. | ||
| 23 | * | ||
| 24 | */ | ||
| 25 | |||
| 26 | #ifndef __YAFFS_ECC_H__ | ||
| 27 | #define __YAFFS_ECC_H__ | ||
| 28 | |||
| 29 | struct yaffs_ecc_other { | ||
| 30 | unsigned char col_parity; | ||
| 31 | unsigned line_parity; | ||
| 32 | unsigned line_parity_prime; | ||
| 33 | }; | ||
| 34 | |||
| 35 | void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc); | ||
| 36 | int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc, | ||
| 37 | const unsigned char *test_ecc); | ||
| 38 | |||
| 39 | void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes, | ||
| 40 | struct yaffs_ecc_other *ecc); | ||
| 41 | int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes, | ||
| 42 | struct yaffs_ecc_other *read_ecc, | ||
| 43 | const struct yaffs_ecc_other *test_ecc); | ||
| 44 | #endif | ||
diff --git a/fs/yaffs2/yaffs_getblockinfo.h b/fs/yaffs2/yaffs_getblockinfo.h new file mode 100644 index 00000000000..d87acbde997 --- /dev/null +++ b/fs/yaffs2/yaffs_getblockinfo.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_GETBLOCKINFO_H__ | ||
| 17 | #define __YAFFS_GETBLOCKINFO_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | #include "yaffs_trace.h" | ||
| 21 | |||
| 22 | /* Function to manipulate block info */ | ||
| 23 | static inline struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev | ||
| 24 | *dev, int blk) | ||
| 25 | { | ||
| 26 | if (blk < dev->internal_start_block || blk > dev->internal_end_block) { | ||
| 27 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 28 | "**>> yaffs: get_block_info block %d is not valid", | ||
| 29 | blk); | ||
| 30 | YBUG(); | ||
| 31 | } | ||
| 32 | return &dev->block_info[blk - dev->internal_start_block]; | ||
| 33 | } | ||
| 34 | |||
| 35 | #endif | ||
diff --git a/fs/yaffs2/yaffs_guts.c b/fs/yaffs2/yaffs_guts.c new file mode 100644 index 00000000000..f4ae9deed72 --- /dev/null +++ b/fs/yaffs2/yaffs_guts.c | |||
| @@ -0,0 +1,5164 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yportenv.h" | ||
| 15 | #include "yaffs_trace.h" | ||
| 16 | |||
| 17 | #include "yaffs_guts.h" | ||
| 18 | #include "yaffs_tagsvalidity.h" | ||
| 19 | #include "yaffs_getblockinfo.h" | ||
| 20 | |||
| 21 | #include "yaffs_tagscompat.h" | ||
| 22 | |||
| 23 | #include "yaffs_nand.h" | ||
| 24 | |||
| 25 | #include "yaffs_yaffs1.h" | ||
| 26 | #include "yaffs_yaffs2.h" | ||
| 27 | #include "yaffs_bitmap.h" | ||
| 28 | #include "yaffs_verify.h" | ||
| 29 | |||
| 30 | #include "yaffs_nand.h" | ||
| 31 | #include "yaffs_packedtags2.h" | ||
| 32 | |||
| 33 | #include "yaffs_nameval.h" | ||
| 34 | #include "yaffs_allocator.h" | ||
| 35 | |||
| 36 | #include "yaffs_attribs.h" | ||
| 37 | |||
| 38 | /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */ | ||
| 39 | #define YAFFS_GC_GOOD_ENOUGH 2 | ||
| 40 | #define YAFFS_GC_PASSIVE_THRESHOLD 4 | ||
| 41 | |||
| 42 | #include "yaffs_ecc.h" | ||
| 43 | |||
| 44 | /* Forward declarations */ | ||
| 45 | |||
| 46 | static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, | ||
| 47 | const u8 * buffer, int n_bytes, int use_reserve); | ||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /* Function to calculate chunk and offset */ | ||
| 52 | |||
| 53 | static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr, | ||
| 54 | int *chunk_out, u32 * offset_out) | ||
| 55 | { | ||
| 56 | int chunk; | ||
| 57 | u32 offset; | ||
| 58 | |||
| 59 | chunk = (u32) (addr >> dev->chunk_shift); | ||
| 60 | |||
| 61 | if (dev->chunk_div == 1) { | ||
| 62 | /* easy power of 2 case */ | ||
| 63 | offset = (u32) (addr & dev->chunk_mask); | ||
| 64 | } else { | ||
| 65 | /* Non power-of-2 case */ | ||
| 66 | |||
| 67 | loff_t chunk_base; | ||
| 68 | |||
| 69 | chunk /= dev->chunk_div; | ||
| 70 | |||
| 71 | chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk; | ||
| 72 | offset = (u32) (addr - chunk_base); | ||
| 73 | } | ||
| 74 | |||
| 75 | *chunk_out = chunk; | ||
| 76 | *offset_out = offset; | ||
| 77 | } | ||
| 78 | |||
| 79 | /* Function to return the number of shifts for a power of 2 greater than or | ||
| 80 | * equal to the given number | ||
| 81 | * Note we don't try to cater for all possible numbers and this does not have to | ||
| 82 | * be hellishly efficient. | ||
| 83 | */ | ||
| 84 | |||
| 85 | static u32 calc_shifts_ceiling(u32 x) | ||
| 86 | { | ||
| 87 | int extra_bits; | ||
| 88 | int shifts; | ||
| 89 | |||
| 90 | shifts = extra_bits = 0; | ||
| 91 | |||
| 92 | while (x > 1) { | ||
| 93 | if (x & 1) | ||
| 94 | extra_bits++; | ||
| 95 | x >>= 1; | ||
| 96 | shifts++; | ||
| 97 | } | ||
| 98 | |||
| 99 | if (extra_bits) | ||
| 100 | shifts++; | ||
| 101 | |||
| 102 | return shifts; | ||
| 103 | } | ||
| 104 | |||
| 105 | /* Function to return the number of shifts to get a 1 in bit 0 | ||
| 106 | */ | ||
| 107 | |||
| 108 | static u32 calc_shifts(u32 x) | ||
| 109 | { | ||
| 110 | u32 shifts; | ||
| 111 | |||
| 112 | shifts = 0; | ||
| 113 | |||
| 114 | if (!x) | ||
| 115 | return 0; | ||
| 116 | |||
| 117 | while (!(x & 1)) { | ||
| 118 | x >>= 1; | ||
| 119 | shifts++; | ||
| 120 | } | ||
| 121 | |||
| 122 | return shifts; | ||
| 123 | } | ||
| 124 | |||
| 125 | /* | ||
| 126 | * Temporary buffer manipulations. | ||
| 127 | */ | ||
| 128 | |||
| 129 | static int yaffs_init_tmp_buffers(struct yaffs_dev *dev) | ||
| 130 | { | ||
| 131 | int i; | ||
| 132 | u8 *buf = (u8 *) 1; | ||
| 133 | |||
| 134 | memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer)); | ||
| 135 | |||
| 136 | for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) { | ||
| 137 | dev->temp_buffer[i].line = 0; /* not in use */ | ||
| 138 | dev->temp_buffer[i].buffer = buf = | ||
| 139 | kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); | ||
| 140 | } | ||
| 141 | |||
| 142 | return buf ? YAFFS_OK : YAFFS_FAIL; | ||
| 143 | } | ||
| 144 | |||
| 145 | u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no) | ||
| 146 | { | ||
| 147 | int i, j; | ||
| 148 | |||
| 149 | dev->temp_in_use++; | ||
| 150 | if (dev->temp_in_use > dev->max_temp) | ||
| 151 | dev->max_temp = dev->temp_in_use; | ||
| 152 | |||
| 153 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { | ||
| 154 | if (dev->temp_buffer[i].line == 0) { | ||
| 155 | dev->temp_buffer[i].line = line_no; | ||
| 156 | if ((i + 1) > dev->max_temp) { | ||
| 157 | dev->max_temp = i + 1; | ||
| 158 | for (j = 0; j <= i; j++) | ||
| 159 | dev->temp_buffer[j].max_line = | ||
| 160 | dev->temp_buffer[j].line; | ||
| 161 | } | ||
| 162 | |||
| 163 | return dev->temp_buffer[i].buffer; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | yaffs_trace(YAFFS_TRACE_BUFFERS, | ||
| 168 | "Out of temp buffers at line %d, other held by lines:", | ||
| 169 | line_no); | ||
| 170 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) | ||
| 171 | yaffs_trace(YAFFS_TRACE_BUFFERS," %d", dev->temp_buffer[i].line); | ||
| 172 | |||
| 173 | /* | ||
| 174 | * If we got here then we have to allocate an unmanaged one | ||
| 175 | * This is not good. | ||
| 176 | */ | ||
| 177 | |||
| 178 | dev->unmanaged_buffer_allocs++; | ||
| 179 | return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS); | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no) | ||
| 184 | { | ||
| 185 | int i; | ||
| 186 | |||
| 187 | dev->temp_in_use--; | ||
| 188 | |||
| 189 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { | ||
| 190 | if (dev->temp_buffer[i].buffer == buffer) { | ||
| 191 | dev->temp_buffer[i].line = 0; | ||
| 192 | return; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | if (buffer) { | ||
| 197 | /* assume it is an unmanaged one. */ | ||
| 198 | yaffs_trace(YAFFS_TRACE_BUFFERS, | ||
| 199 | "Releasing unmanaged temp buffer in line %d", | ||
| 200 | line_no); | ||
| 201 | kfree(buffer); | ||
| 202 | dev->unmanaged_buffer_deallocs++; | ||
| 203 | } | ||
| 204 | |||
| 205 | } | ||
| 206 | |||
| 207 | /* | ||
| 208 | * Determine if we have a managed buffer. | ||
| 209 | */ | ||
| 210 | int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer) | ||
| 211 | { | ||
| 212 | int i; | ||
| 213 | |||
| 214 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { | ||
| 215 | if (dev->temp_buffer[i].buffer == buffer) | ||
| 216 | return 1; | ||
| 217 | } | ||
| 218 | |||
| 219 | for (i = 0; i < dev->param.n_caches; i++) { | ||
| 220 | if (dev->cache[i].data == buffer) | ||
| 221 | return 1; | ||
| 222 | } | ||
| 223 | |||
| 224 | if (buffer == dev->checkpt_buffer) | ||
| 225 | return 1; | ||
| 226 | |||
| 227 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 228 | "yaffs: unmaged buffer detected."); | ||
| 229 | return 0; | ||
| 230 | } | ||
| 231 | |||
| 232 | /* | ||
| 233 | * Functions for robustisizing TODO | ||
| 234 | * | ||
| 235 | */ | ||
| 236 | |||
| 237 | static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk, | ||
| 238 | const u8 * data, | ||
| 239 | const struct yaffs_ext_tags *tags) | ||
| 240 | { | ||
| 241 | dev = dev; | ||
| 242 | nand_chunk = nand_chunk; | ||
| 243 | data = data; | ||
| 244 | tags = tags; | ||
| 245 | } | ||
| 246 | |||
| 247 | static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk, | ||
| 248 | const struct yaffs_ext_tags *tags) | ||
| 249 | { | ||
| 250 | dev = dev; | ||
| 251 | nand_chunk = nand_chunk; | ||
| 252 | tags = tags; | ||
| 253 | } | ||
| 254 | |||
| 255 | void yaffs_handle_chunk_error(struct yaffs_dev *dev, | ||
| 256 | struct yaffs_block_info *bi) | ||
| 257 | { | ||
| 258 | if (!bi->gc_prioritise) { | ||
| 259 | bi->gc_prioritise = 1; | ||
| 260 | dev->has_pending_prioritised_gc = 1; | ||
| 261 | bi->chunk_error_strikes++; | ||
| 262 | |||
| 263 | if (bi->chunk_error_strikes > 3) { | ||
| 264 | bi->needs_retiring = 1; /* Too many stikes, so retire this */ | ||
| 265 | yaffs_trace(YAFFS_TRACE_ALWAYS, "yaffs: Block struck out"); | ||
| 266 | |||
| 267 | } | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk, | ||
| 272 | int erased_ok) | ||
| 273 | { | ||
| 274 | int flash_block = nand_chunk / dev->param.chunks_per_block; | ||
| 275 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block); | ||
| 276 | |||
| 277 | yaffs_handle_chunk_error(dev, bi); | ||
| 278 | |||
| 279 | if (erased_ok) { | ||
| 280 | /* Was an actual write failure, so mark the block for retirement */ | ||
| 281 | bi->needs_retiring = 1; | ||
| 282 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, | ||
| 283 | "**>> Block %d needs retiring", flash_block); | ||
| 284 | } | ||
| 285 | |||
| 286 | /* Delete the chunk */ | ||
| 287 | yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); | ||
| 288 | yaffs_skip_rest_of_block(dev); | ||
| 289 | } | ||
| 290 | |||
| 291 | /* | ||
| 292 | * Verification code | ||
| 293 | */ | ||
| 294 | |||
| 295 | /* | ||
| 296 | * Simple hash function. Needs to have a reasonable spread | ||
| 297 | */ | ||
| 298 | |||
| 299 | static inline int yaffs_hash_fn(int n) | ||
| 300 | { | ||
| 301 | n = abs(n); | ||
| 302 | return n % YAFFS_NOBJECT_BUCKETS; | ||
| 303 | } | ||
| 304 | |||
| 305 | /* | ||
| 306 | * Access functions to useful fake objects. | ||
| 307 | * Note that root might have a presence in NAND if permissions are set. | ||
| 308 | */ | ||
| 309 | |||
| 310 | struct yaffs_obj *yaffs_root(struct yaffs_dev *dev) | ||
| 311 | { | ||
| 312 | return dev->root_dir; | ||
| 313 | } | ||
| 314 | |||
| 315 | struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev) | ||
| 316 | { | ||
| 317 | return dev->lost_n_found; | ||
| 318 | } | ||
| 319 | |||
| 320 | /* | ||
| 321 | * Erased NAND checking functions | ||
| 322 | */ | ||
| 323 | |||
| 324 | int yaffs_check_ff(u8 * buffer, int n_bytes) | ||
| 325 | { | ||
| 326 | /* Horrible, slow implementation */ | ||
| 327 | while (n_bytes--) { | ||
| 328 | if (*buffer != 0xFF) | ||
| 329 | return 0; | ||
| 330 | buffer++; | ||
| 331 | } | ||
| 332 | return 1; | ||
| 333 | } | ||
| 334 | |||
| 335 | static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk) | ||
| 336 | { | ||
| 337 | int retval = YAFFS_OK; | ||
| 338 | u8 *data = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 339 | struct yaffs_ext_tags tags; | ||
| 340 | int result; | ||
| 341 | |||
| 342 | result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags); | ||
| 343 | |||
| 344 | if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR) | ||
| 345 | retval = YAFFS_FAIL; | ||
| 346 | |||
| 347 | if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || | ||
| 348 | tags.chunk_used) { | ||
| 349 | yaffs_trace(YAFFS_TRACE_NANDACCESS, "Chunk %d not erased", nand_chunk); | ||
| 350 | retval = YAFFS_FAIL; | ||
| 351 | } | ||
| 352 | |||
| 353 | yaffs_release_temp_buffer(dev, data, __LINE__); | ||
| 354 | |||
| 355 | return retval; | ||
| 356 | |||
| 357 | } | ||
| 358 | |||
| 359 | static int yaffs_verify_chunk_written(struct yaffs_dev *dev, | ||
| 360 | int nand_chunk, | ||
| 361 | const u8 * data, | ||
| 362 | struct yaffs_ext_tags *tags) | ||
| 363 | { | ||
| 364 | int retval = YAFFS_OK; | ||
| 365 | struct yaffs_ext_tags temp_tags; | ||
| 366 | u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 367 | int result; | ||
| 368 | |||
| 369 | result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags); | ||
| 370 | if (memcmp(buffer, data, dev->data_bytes_per_chunk) || | ||
| 371 | temp_tags.obj_id != tags->obj_id || | ||
| 372 | temp_tags.chunk_id != tags->chunk_id || | ||
| 373 | temp_tags.n_bytes != tags->n_bytes) | ||
| 374 | retval = YAFFS_FAIL; | ||
| 375 | |||
| 376 | yaffs_release_temp_buffer(dev, buffer, __LINE__); | ||
| 377 | |||
| 378 | return retval; | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks) | ||
| 383 | { | ||
| 384 | int reserved_chunks; | ||
| 385 | int reserved_blocks = dev->param.n_reserved_blocks; | ||
| 386 | int checkpt_blocks; | ||
| 387 | |||
| 388 | checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev); | ||
| 389 | |||
| 390 | reserved_chunks = | ||
| 391 | ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block); | ||
| 392 | |||
| 393 | return (dev->n_free_chunks > (reserved_chunks + n_chunks)); | ||
| 394 | } | ||
| 395 | |||
| 396 | static int yaffs_find_alloc_block(struct yaffs_dev *dev) | ||
| 397 | { | ||
| 398 | int i; | ||
| 399 | |||
| 400 | struct yaffs_block_info *bi; | ||
| 401 | |||
| 402 | if (dev->n_erased_blocks < 1) { | ||
| 403 | /* Hoosterman we've got a problem. | ||
| 404 | * Can't get space to gc | ||
| 405 | */ | ||
| 406 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 407 | "yaffs tragedy: no more erased blocks" ); | ||
| 408 | |||
| 409 | return -1; | ||
| 410 | } | ||
| 411 | |||
| 412 | /* Find an empty block. */ | ||
| 413 | |||
| 414 | for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { | ||
| 415 | dev->alloc_block_finder++; | ||
| 416 | if (dev->alloc_block_finder < dev->internal_start_block | ||
| 417 | || dev->alloc_block_finder > dev->internal_end_block) { | ||
| 418 | dev->alloc_block_finder = dev->internal_start_block; | ||
| 419 | } | ||
| 420 | |||
| 421 | bi = yaffs_get_block_info(dev, dev->alloc_block_finder); | ||
| 422 | |||
| 423 | if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { | ||
| 424 | bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING; | ||
| 425 | dev->seq_number++; | ||
| 426 | bi->seq_number = dev->seq_number; | ||
| 427 | dev->n_erased_blocks--; | ||
| 428 | yaffs_trace(YAFFS_TRACE_ALLOCATE, | ||
| 429 | "Allocated block %d, seq %d, %d left" , | ||
| 430 | dev->alloc_block_finder, dev->seq_number, | ||
| 431 | dev->n_erased_blocks); | ||
| 432 | return dev->alloc_block_finder; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 437 | "yaffs tragedy: no more erased blocks, but there should have been %d", | ||
| 438 | dev->n_erased_blocks); | ||
| 439 | |||
| 440 | return -1; | ||
| 441 | } | ||
| 442 | |||
| 443 | static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver, | ||
| 444 | struct yaffs_block_info **block_ptr) | ||
| 445 | { | ||
| 446 | int ret_val; | ||
| 447 | struct yaffs_block_info *bi; | ||
| 448 | |||
| 449 | if (dev->alloc_block < 0) { | ||
| 450 | /* Get next block to allocate off */ | ||
| 451 | dev->alloc_block = yaffs_find_alloc_block(dev); | ||
| 452 | dev->alloc_page = 0; | ||
| 453 | } | ||
| 454 | |||
| 455 | if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) { | ||
| 456 | /* Not enough space to allocate unless we're allowed to use the reserve. */ | ||
| 457 | return -1; | ||
| 458 | } | ||
| 459 | |||
| 460 | if (dev->n_erased_blocks < dev->param.n_reserved_blocks | ||
| 461 | && dev->alloc_page == 0) | ||
| 462 | yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve"); | ||
| 463 | |||
| 464 | /* Next page please.... */ | ||
| 465 | if (dev->alloc_block >= 0) { | ||
| 466 | bi = yaffs_get_block_info(dev, dev->alloc_block); | ||
| 467 | |||
| 468 | ret_val = (dev->alloc_block * dev->param.chunks_per_block) + | ||
| 469 | dev->alloc_page; | ||
| 470 | bi->pages_in_use++; | ||
| 471 | yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page); | ||
| 472 | |||
| 473 | dev->alloc_page++; | ||
| 474 | |||
| 475 | dev->n_free_chunks--; | ||
| 476 | |||
| 477 | /* If the block is full set the state to full */ | ||
| 478 | if (dev->alloc_page >= dev->param.chunks_per_block) { | ||
| 479 | bi->block_state = YAFFS_BLOCK_STATE_FULL; | ||
| 480 | dev->alloc_block = -1; | ||
| 481 | } | ||
| 482 | |||
| 483 | if (block_ptr) | ||
| 484 | *block_ptr = bi; | ||
| 485 | |||
| 486 | return ret_val; | ||
| 487 | } | ||
| 488 | |||
| 489 | yaffs_trace(YAFFS_TRACE_ERROR, "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" ); | ||
| 490 | |||
| 491 | return -1; | ||
| 492 | } | ||
| 493 | |||
| 494 | static int yaffs_get_erased_chunks(struct yaffs_dev *dev) | ||
| 495 | { | ||
| 496 | int n; | ||
| 497 | |||
| 498 | n = dev->n_erased_blocks * dev->param.chunks_per_block; | ||
| 499 | |||
| 500 | if (dev->alloc_block > 0) | ||
| 501 | n += (dev->param.chunks_per_block - dev->alloc_page); | ||
| 502 | |||
| 503 | return n; | ||
| 504 | |||
| 505 | } | ||
| 506 | |||
| 507 | /* | ||
| 508 | * yaffs_skip_rest_of_block() skips over the rest of the allocation block | ||
| 509 | * if we don't want to write to it. | ||
| 510 | */ | ||
| 511 | void yaffs_skip_rest_of_block(struct yaffs_dev *dev) | ||
| 512 | { | ||
| 513 | if (dev->alloc_block > 0) { | ||
| 514 | struct yaffs_block_info *bi = | ||
| 515 | yaffs_get_block_info(dev, dev->alloc_block); | ||
| 516 | if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) { | ||
| 517 | bi->block_state = YAFFS_BLOCK_STATE_FULL; | ||
| 518 | dev->alloc_block = -1; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | } | ||
| 522 | |||
| 523 | static int yaffs_write_new_chunk(struct yaffs_dev *dev, | ||
| 524 | const u8 * data, | ||
| 525 | struct yaffs_ext_tags *tags, int use_reserver) | ||
| 526 | { | ||
| 527 | int attempts = 0; | ||
| 528 | int write_ok = 0; | ||
| 529 | int chunk; | ||
| 530 | |||
| 531 | yaffs2_checkpt_invalidate(dev); | ||
| 532 | |||
| 533 | do { | ||
| 534 | struct yaffs_block_info *bi = 0; | ||
| 535 | int erased_ok = 0; | ||
| 536 | |||
| 537 | chunk = yaffs_alloc_chunk(dev, use_reserver, &bi); | ||
| 538 | if (chunk < 0) { | ||
| 539 | /* no space */ | ||
| 540 | break; | ||
| 541 | } | ||
| 542 | |||
| 543 | /* First check this chunk is erased, if it needs | ||
| 544 | * checking. The checking policy (unless forced | ||
| 545 | * always on) is as follows: | ||
| 546 | * | ||
| 547 | * Check the first page we try to write in a block. | ||
| 548 | * If the check passes then we don't need to check any | ||
| 549 | * more. If the check fails, we check again... | ||
| 550 | * If the block has been erased, we don't need to check. | ||
| 551 | * | ||
| 552 | * However, if the block has been prioritised for gc, | ||
| 553 | * then we think there might be something odd about | ||
| 554 | * this block and stop using it. | ||
| 555 | * | ||
| 556 | * Rationale: We should only ever see chunks that have | ||
| 557 | * not been erased if there was a partially written | ||
| 558 | * chunk due to power loss. This checking policy should | ||
| 559 | * catch that case with very few checks and thus save a | ||
| 560 | * lot of checks that are most likely not needed. | ||
| 561 | * | ||
| 562 | * Mods to the above | ||
| 563 | * If an erase check fails or the write fails we skip the | ||
| 564 | * rest of the block. | ||
| 565 | */ | ||
| 566 | |||
| 567 | /* let's give it a try */ | ||
| 568 | attempts++; | ||
| 569 | |||
| 570 | if (dev->param.always_check_erased) | ||
| 571 | bi->skip_erased_check = 0; | ||
| 572 | |||
| 573 | if (!bi->skip_erased_check) { | ||
| 574 | erased_ok = yaffs_check_chunk_erased(dev, chunk); | ||
| 575 | if (erased_ok != YAFFS_OK) { | ||
| 576 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 577 | "**>> yaffs chunk %d was not erased", | ||
| 578 | chunk); | ||
| 579 | |||
| 580 | /* If not erased, delete this one, | ||
| 581 | * skip rest of block and | ||
| 582 | * try another chunk */ | ||
| 583 | yaffs_chunk_del(dev, chunk, 1, __LINE__); | ||
| 584 | yaffs_skip_rest_of_block(dev); | ||
| 585 | continue; | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags); | ||
| 590 | |||
| 591 | if (!bi->skip_erased_check) | ||
| 592 | write_ok = | ||
| 593 | yaffs_verify_chunk_written(dev, chunk, data, tags); | ||
| 594 | |||
| 595 | if (write_ok != YAFFS_OK) { | ||
| 596 | /* Clean up aborted write, skip to next block and | ||
| 597 | * try another chunk */ | ||
| 598 | yaffs_handle_chunk_wr_error(dev, chunk, erased_ok); | ||
| 599 | continue; | ||
| 600 | } | ||
| 601 | |||
| 602 | bi->skip_erased_check = 1; | ||
| 603 | |||
| 604 | /* Copy the data into the robustification buffer */ | ||
| 605 | yaffs_handle_chunk_wr_ok(dev, chunk, data, tags); | ||
| 606 | |||
| 607 | } while (write_ok != YAFFS_OK && | ||
| 608 | (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts)); | ||
| 609 | |||
| 610 | if (!write_ok) | ||
| 611 | chunk = -1; | ||
| 612 | |||
| 613 | if (attempts > 1) { | ||
| 614 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 615 | "**>> yaffs write required %d attempts", | ||
| 616 | attempts); | ||
| 617 | dev->n_retired_writes += (attempts - 1); | ||
| 618 | } | ||
| 619 | |||
| 620 | return chunk; | ||
| 621 | } | ||
| 622 | |||
| 623 | /* | ||
| 624 | * Block retiring for handling a broken block. | ||
| 625 | */ | ||
| 626 | |||
| 627 | static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block) | ||
| 628 | { | ||
| 629 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block); | ||
| 630 | |||
| 631 | yaffs2_checkpt_invalidate(dev); | ||
| 632 | |||
| 633 | yaffs2_clear_oldest_dirty_seq(dev, bi); | ||
| 634 | |||
| 635 | if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) { | ||
| 636 | if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) { | ||
| 637 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 638 | "yaffs: Failed to mark bad and erase block %d", | ||
| 639 | flash_block); | ||
| 640 | } else { | ||
| 641 | struct yaffs_ext_tags tags; | ||
| 642 | int chunk_id = | ||
| 643 | flash_block * dev->param.chunks_per_block; | ||
| 644 | |||
| 645 | u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 646 | |||
| 647 | memset(buffer, 0xff, dev->data_bytes_per_chunk); | ||
| 648 | yaffs_init_tags(&tags); | ||
| 649 | tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK; | ||
| 650 | if (dev->param.write_chunk_tags_fn(dev, chunk_id - | ||
| 651 | dev->chunk_offset, | ||
| 652 | buffer, | ||
| 653 | &tags) != YAFFS_OK) | ||
| 654 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 655 | "yaffs: Failed to write bad block marker to block %d", | ||
| 656 | flash_block); | ||
| 657 | |||
| 658 | yaffs_release_temp_buffer(dev, buffer, __LINE__); | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | bi->block_state = YAFFS_BLOCK_STATE_DEAD; | ||
| 663 | bi->gc_prioritise = 0; | ||
| 664 | bi->needs_retiring = 0; | ||
| 665 | |||
| 666 | dev->n_retired_blocks++; | ||
| 667 | } | ||
| 668 | |||
| 669 | /*---------------- Name handling functions ------------*/ | ||
| 670 | |||
| 671 | static u16 yaffs_calc_name_sum(const YCHAR * name) | ||
| 672 | { | ||
| 673 | u16 sum = 0; | ||
| 674 | u16 i = 1; | ||
| 675 | |||
| 676 | const YUCHAR *bname = (const YUCHAR *)name; | ||
| 677 | if (bname) { | ||
| 678 | while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) { | ||
| 679 | |||
| 680 | /* 0x1f mask is case insensitive */ | ||
| 681 | sum += ((*bname) & 0x1f) * i; | ||
| 682 | i++; | ||
| 683 | bname++; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | return sum; | ||
| 687 | } | ||
| 688 | |||
| 689 | void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name) | ||
| 690 | { | ||
| 691 | #ifndef CONFIG_YAFFS_NO_SHORT_NAMES | ||
| 692 | memset(obj->short_name, 0, sizeof(obj->short_name)); | ||
| 693 | if (name && | ||
| 694 | strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <= | ||
| 695 | YAFFS_SHORT_NAME_LENGTH) | ||
| 696 | strcpy(obj->short_name, name); | ||
| 697 | else | ||
| 698 | obj->short_name[0] = _Y('\0'); | ||
| 699 | #endif | ||
| 700 | obj->sum = yaffs_calc_name_sum(name); | ||
| 701 | } | ||
| 702 | |||
| 703 | void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj, | ||
| 704 | const struct yaffs_obj_hdr *oh) | ||
| 705 | { | ||
| 706 | #ifdef CONFIG_YAFFS_AUTO_UNICODE | ||
| 707 | YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1]; | ||
| 708 | memset(tmp_name, 0, sizeof(tmp_name)); | ||
| 709 | yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name, | ||
| 710 | YAFFS_MAX_NAME_LENGTH + 1); | ||
| 711 | yaffs_set_obj_name(obj, tmp_name); | ||
| 712 | #else | ||
| 713 | yaffs_set_obj_name(obj, oh->name); | ||
| 714 | #endif | ||
| 715 | } | ||
| 716 | |||
| 717 | /*-------------------- TNODES ------------------- | ||
| 718 | |||
| 719 | * List of spare tnodes | ||
| 720 | * The list is hooked together using the first pointer | ||
| 721 | * in the tnode. | ||
| 722 | */ | ||
| 723 | |||
| 724 | struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev) | ||
| 725 | { | ||
| 726 | struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev); | ||
| 727 | if (tn) { | ||
| 728 | memset(tn, 0, dev->tnode_size); | ||
| 729 | dev->n_tnodes++; | ||
| 730 | } | ||
| 731 | |||
| 732 | dev->checkpoint_blocks_required = 0; /* force recalculation */ | ||
| 733 | |||
| 734 | return tn; | ||
| 735 | } | ||
| 736 | |||
| 737 | /* FreeTnode frees up a tnode and puts it back on the free list */ | ||
| 738 | static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn) | ||
| 739 | { | ||
| 740 | yaffs_free_raw_tnode(dev, tn); | ||
| 741 | dev->n_tnodes--; | ||
| 742 | dev->checkpoint_blocks_required = 0; /* force recalculation */ | ||
| 743 | } | ||
| 744 | |||
| 745 | static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev) | ||
| 746 | { | ||
| 747 | yaffs_deinit_raw_tnodes_and_objs(dev); | ||
| 748 | dev->n_obj = 0; | ||
| 749 | dev->n_tnodes = 0; | ||
| 750 | } | ||
| 751 | |||
| 752 | void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn, | ||
| 753 | unsigned pos, unsigned val) | ||
| 754 | { | ||
| 755 | u32 *map = (u32 *) tn; | ||
| 756 | u32 bit_in_map; | ||
| 757 | u32 bit_in_word; | ||
| 758 | u32 word_in_map; | ||
| 759 | u32 mask; | ||
| 760 | |||
| 761 | pos &= YAFFS_TNODES_LEVEL0_MASK; | ||
| 762 | val >>= dev->chunk_grp_bits; | ||
| 763 | |||
| 764 | bit_in_map = pos * dev->tnode_width; | ||
| 765 | word_in_map = bit_in_map / 32; | ||
| 766 | bit_in_word = bit_in_map & (32 - 1); | ||
| 767 | |||
| 768 | mask = dev->tnode_mask << bit_in_word; | ||
| 769 | |||
| 770 | map[word_in_map] &= ~mask; | ||
| 771 | map[word_in_map] |= (mask & (val << bit_in_word)); | ||
| 772 | |||
| 773 | if (dev->tnode_width > (32 - bit_in_word)) { | ||
| 774 | bit_in_word = (32 - bit_in_word); | ||
| 775 | word_in_map++;; | ||
| 776 | mask = | ||
| 777 | dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word); | ||
| 778 | map[word_in_map] &= ~mask; | ||
| 779 | map[word_in_map] |= (mask & (val >> bit_in_word)); | ||
| 780 | } | ||
| 781 | } | ||
| 782 | |||
| 783 | u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn, | ||
| 784 | unsigned pos) | ||
| 785 | { | ||
| 786 | u32 *map = (u32 *) tn; | ||
| 787 | u32 bit_in_map; | ||
| 788 | u32 bit_in_word; | ||
| 789 | u32 word_in_map; | ||
| 790 | u32 val; | ||
| 791 | |||
| 792 | pos &= YAFFS_TNODES_LEVEL0_MASK; | ||
| 793 | |||
| 794 | bit_in_map = pos * dev->tnode_width; | ||
| 795 | word_in_map = bit_in_map / 32; | ||
| 796 | bit_in_word = bit_in_map & (32 - 1); | ||
| 797 | |||
| 798 | val = map[word_in_map] >> bit_in_word; | ||
| 799 | |||
| 800 | if (dev->tnode_width > (32 - bit_in_word)) { | ||
| 801 | bit_in_word = (32 - bit_in_word); | ||
| 802 | word_in_map++;; | ||
| 803 | val |= (map[word_in_map] << bit_in_word); | ||
| 804 | } | ||
| 805 | |||
| 806 | val &= dev->tnode_mask; | ||
| 807 | val <<= dev->chunk_grp_bits; | ||
| 808 | |||
| 809 | return val; | ||
| 810 | } | ||
| 811 | |||
| 812 | /* ------------------- End of individual tnode manipulation -----------------*/ | ||
| 813 | |||
| 814 | /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------ | ||
| 815 | * The look up tree is represented by the top tnode and the number of top_level | ||
| 816 | * in the tree. 0 means only the level 0 tnode is in the tree. | ||
| 817 | */ | ||
| 818 | |||
| 819 | /* FindLevel0Tnode finds the level 0 tnode, if one exists. */ | ||
| 820 | struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev, | ||
| 821 | struct yaffs_file_var *file_struct, | ||
| 822 | u32 chunk_id) | ||
| 823 | { | ||
| 824 | struct yaffs_tnode *tn = file_struct->top; | ||
| 825 | u32 i; | ||
| 826 | int required_depth; | ||
| 827 | int level = file_struct->top_level; | ||
| 828 | |||
| 829 | dev = dev; | ||
| 830 | |||
| 831 | /* Check sane level and chunk Id */ | ||
| 832 | if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) | ||
| 833 | return NULL; | ||
| 834 | |||
| 835 | if (chunk_id > YAFFS_MAX_CHUNK_ID) | ||
| 836 | return NULL; | ||
| 837 | |||
| 838 | /* First check we're tall enough (ie enough top_level) */ | ||
| 839 | |||
| 840 | i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS; | ||
| 841 | required_depth = 0; | ||
| 842 | while (i) { | ||
| 843 | i >>= YAFFS_TNODES_INTERNAL_BITS; | ||
| 844 | required_depth++; | ||
| 845 | } | ||
| 846 | |||
| 847 | if (required_depth > file_struct->top_level) | ||
| 848 | return NULL; /* Not tall enough, so we can't find it */ | ||
| 849 | |||
| 850 | /* Traverse down to level 0 */ | ||
| 851 | while (level > 0 && tn) { | ||
| 852 | tn = tn->internal[(chunk_id >> | ||
| 853 | (YAFFS_TNODES_LEVEL0_BITS + | ||
| 854 | (level - 1) * | ||
| 855 | YAFFS_TNODES_INTERNAL_BITS)) & | ||
| 856 | YAFFS_TNODES_INTERNAL_MASK]; | ||
| 857 | level--; | ||
| 858 | } | ||
| 859 | |||
| 860 | return tn; | ||
| 861 | } | ||
| 862 | |||
| 863 | /* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree. | ||
| 864 | * This happens in two steps: | ||
| 865 | * 1. If the tree isn't tall enough, then make it taller. | ||
| 866 | * 2. Scan down the tree towards the level 0 tnode adding tnodes if required. | ||
| 867 | * | ||
| 868 | * Used when modifying the tree. | ||
| 869 | * | ||
| 870 | * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will | ||
| 871 | * be plugged into the ttree. | ||
| 872 | */ | ||
| 873 | |||
| 874 | struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, | ||
| 875 | struct yaffs_file_var *file_struct, | ||
| 876 | u32 chunk_id, | ||
| 877 | struct yaffs_tnode *passed_tn) | ||
| 878 | { | ||
| 879 | int required_depth; | ||
| 880 | int i; | ||
| 881 | int l; | ||
| 882 | struct yaffs_tnode *tn; | ||
| 883 | |||
| 884 | u32 x; | ||
| 885 | |||
| 886 | /* Check sane level and page Id */ | ||
| 887 | if (file_struct->top_level < 0 | ||
| 888 | || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL) | ||
| 889 | return NULL; | ||
| 890 | |||
| 891 | if (chunk_id > YAFFS_MAX_CHUNK_ID) | ||
| 892 | return NULL; | ||
| 893 | |||
| 894 | /* First check we're tall enough (ie enough top_level) */ | ||
| 895 | |||
| 896 | x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS; | ||
| 897 | required_depth = 0; | ||
| 898 | while (x) { | ||
| 899 | x >>= YAFFS_TNODES_INTERNAL_BITS; | ||
| 900 | required_depth++; | ||
| 901 | } | ||
| 902 | |||
| 903 | if (required_depth > file_struct->top_level) { | ||
| 904 | /* Not tall enough, gotta make the tree taller */ | ||
| 905 | for (i = file_struct->top_level; i < required_depth; i++) { | ||
| 906 | |||
| 907 | tn = yaffs_get_tnode(dev); | ||
| 908 | |||
| 909 | if (tn) { | ||
| 910 | tn->internal[0] = file_struct->top; | ||
| 911 | file_struct->top = tn; | ||
| 912 | file_struct->top_level++; | ||
| 913 | } else { | ||
| 914 | yaffs_trace(YAFFS_TRACE_ERROR, "yaffs: no more tnodes"); | ||
| 915 | return NULL; | ||
| 916 | } | ||
| 917 | } | ||
| 918 | } | ||
| 919 | |||
| 920 | /* Traverse down to level 0, adding anything we need */ | ||
| 921 | |||
| 922 | l = file_struct->top_level; | ||
| 923 | tn = file_struct->top; | ||
| 924 | |||
| 925 | if (l > 0) { | ||
| 926 | while (l > 0 && tn) { | ||
| 927 | x = (chunk_id >> | ||
| 928 | (YAFFS_TNODES_LEVEL0_BITS + | ||
| 929 | (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) & | ||
| 930 | YAFFS_TNODES_INTERNAL_MASK; | ||
| 931 | |||
| 932 | if ((l > 1) && !tn->internal[x]) { | ||
| 933 | /* Add missing non-level-zero tnode */ | ||
| 934 | tn->internal[x] = yaffs_get_tnode(dev); | ||
| 935 | if (!tn->internal[x]) | ||
| 936 | return NULL; | ||
| 937 | } else if (l == 1) { | ||
| 938 | /* Looking from level 1 at level 0 */ | ||
| 939 | if (passed_tn) { | ||
| 940 | /* If we already have one, then release it. */ | ||
| 941 | if (tn->internal[x]) | ||
| 942 | yaffs_free_tnode(dev, | ||
| 943 | tn-> | ||
| 944 | internal[x]); | ||
| 945 | tn->internal[x] = passed_tn; | ||
| 946 | |||
| 947 | } else if (!tn->internal[x]) { | ||
| 948 | /* Don't have one, none passed in */ | ||
| 949 | tn->internal[x] = yaffs_get_tnode(dev); | ||
| 950 | if (!tn->internal[x]) | ||
| 951 | return NULL; | ||
| 952 | } | ||
| 953 | } | ||
| 954 | |||
| 955 | tn = tn->internal[x]; | ||
| 956 | l--; | ||
| 957 | } | ||
| 958 | } else { | ||
| 959 | /* top is level 0 */ | ||
| 960 | if (passed_tn) { | ||
| 961 | memcpy(tn, passed_tn, | ||
| 962 | (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8); | ||
| 963 | yaffs_free_tnode(dev, passed_tn); | ||
| 964 | } | ||
| 965 | } | ||
| 966 | |||
| 967 | return tn; | ||
| 968 | } | ||
| 969 | |||
| 970 | static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id, | ||
| 971 | int chunk_obj) | ||
| 972 | { | ||
| 973 | return (tags->chunk_id == chunk_obj && | ||
| 974 | tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0; | ||
| 975 | |||
| 976 | } | ||
| 977 | |||
| 978 | static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk, | ||
| 979 | struct yaffs_ext_tags *tags, int obj_id, | ||
| 980 | int inode_chunk) | ||
| 981 | { | ||
| 982 | int j; | ||
| 983 | |||
| 984 | for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) { | ||
| 985 | if (yaffs_check_chunk_bit | ||
| 986 | (dev, the_chunk / dev->param.chunks_per_block, | ||
| 987 | the_chunk % dev->param.chunks_per_block)) { | ||
| 988 | |||
| 989 | if (dev->chunk_grp_size == 1) | ||
| 990 | return the_chunk; | ||
| 991 | else { | ||
| 992 | yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL, | ||
| 993 | tags); | ||
| 994 | if (yaffs_tags_match(tags, obj_id, inode_chunk)) { | ||
| 995 | /* found it; */ | ||
| 996 | return the_chunk; | ||
| 997 | } | ||
| 998 | } | ||
| 999 | } | ||
| 1000 | the_chunk++; | ||
| 1001 | } | ||
| 1002 | return -1; | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk, | ||
| 1006 | struct yaffs_ext_tags *tags) | ||
| 1007 | { | ||
| 1008 | /*Get the Tnode, then get the level 0 offset chunk offset */ | ||
| 1009 | struct yaffs_tnode *tn; | ||
| 1010 | int the_chunk = -1; | ||
| 1011 | struct yaffs_ext_tags local_tags; | ||
| 1012 | int ret_val = -1; | ||
| 1013 | |||
| 1014 | struct yaffs_dev *dev = in->my_dev; | ||
| 1015 | |||
| 1016 | if (!tags) { | ||
| 1017 | /* Passed a NULL, so use our own tags space */ | ||
| 1018 | tags = &local_tags; | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk); | ||
| 1022 | |||
| 1023 | if (tn) { | ||
| 1024 | the_chunk = yaffs_get_group_base(dev, tn, inode_chunk); | ||
| 1025 | |||
| 1026 | ret_val = | ||
| 1027 | yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id, | ||
| 1028 | inode_chunk); | ||
| 1029 | } | ||
| 1030 | return ret_val; | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk, | ||
| 1034 | struct yaffs_ext_tags *tags) | ||
| 1035 | { | ||
| 1036 | /* Get the Tnode, then get the level 0 offset chunk offset */ | ||
| 1037 | struct yaffs_tnode *tn; | ||
| 1038 | int the_chunk = -1; | ||
| 1039 | struct yaffs_ext_tags local_tags; | ||
| 1040 | |||
| 1041 | struct yaffs_dev *dev = in->my_dev; | ||
| 1042 | int ret_val = -1; | ||
| 1043 | |||
| 1044 | if (!tags) { | ||
| 1045 | /* Passed a NULL, so use our own tags space */ | ||
| 1046 | tags = &local_tags; | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk); | ||
| 1050 | |||
| 1051 | if (tn) { | ||
| 1052 | |||
| 1053 | the_chunk = yaffs_get_group_base(dev, tn, inode_chunk); | ||
| 1054 | |||
| 1055 | ret_val = | ||
| 1056 | yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id, | ||
| 1057 | inode_chunk); | ||
| 1058 | |||
| 1059 | /* Delete the entry in the filestructure (if found) */ | ||
| 1060 | if (ret_val != -1) | ||
| 1061 | yaffs_load_tnode_0(dev, tn, inode_chunk, 0); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | return ret_val; | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, | ||
| 1068 | int nand_chunk, int in_scan) | ||
| 1069 | { | ||
| 1070 | /* NB in_scan is zero unless scanning. | ||
| 1071 | * For forward scanning, in_scan is > 0; | ||
| 1072 | * for backward scanning in_scan is < 0 | ||
| 1073 | * | ||
| 1074 | * nand_chunk = 0 is a dummy insert to make sure the tnodes are there. | ||
| 1075 | */ | ||
| 1076 | |||
| 1077 | struct yaffs_tnode *tn; | ||
| 1078 | struct yaffs_dev *dev = in->my_dev; | ||
| 1079 | int existing_cunk; | ||
| 1080 | struct yaffs_ext_tags existing_tags; | ||
| 1081 | struct yaffs_ext_tags new_tags; | ||
| 1082 | unsigned existing_serial, new_serial; | ||
| 1083 | |||
| 1084 | if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) { | ||
| 1085 | /* Just ignore an attempt at putting a chunk into a non-file during scanning | ||
| 1086 | * If it is not during Scanning then something went wrong! | ||
| 1087 | */ | ||
| 1088 | if (!in_scan) { | ||
| 1089 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 1090 | "yaffs tragedy:attempt to put data chunk into a non-file" | ||
| 1091 | ); | ||
| 1092 | YBUG(); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); | ||
| 1096 | return YAFFS_OK; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | tn = yaffs_add_find_tnode_0(dev, | ||
| 1100 | &in->variant.file_variant, | ||
| 1101 | inode_chunk, NULL); | ||
| 1102 | if (!tn) | ||
| 1103 | return YAFFS_FAIL; | ||
| 1104 | |||
| 1105 | if (!nand_chunk) | ||
| 1106 | /* Dummy insert, bail now */ | ||
| 1107 | return YAFFS_OK; | ||
| 1108 | |||
| 1109 | existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk); | ||
| 1110 | |||
| 1111 | if (in_scan != 0) { | ||
| 1112 | /* If we're scanning then we need to test for duplicates | ||
| 1113 | * NB This does not need to be efficient since it should only ever | ||
| 1114 | * happen when the power fails during a write, then only one | ||
| 1115 | * chunk should ever be affected. | ||
| 1116 | * | ||
| 1117 | * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO | ||
| 1118 | * Update: For backward scanning we don't need to re-read tags so this is quite cheap. | ||
| 1119 | */ | ||
| 1120 | |||
| 1121 | if (existing_cunk > 0) { | ||
| 1122 | /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1 | ||
| 1123 | * thus we have to do a FindChunkInFile to get the real chunk id. | ||
| 1124 | * | ||
| 1125 | * We have a duplicate now we need to decide which one to use: | ||
| 1126 | * | ||
| 1127 | * Backwards scanning YAFFS2: The old one is what we use, dump the new one. | ||
| 1128 | * Forward scanning YAFFS2: The new one is what we use, dump the old one. | ||
| 1129 | * YAFFS1: Get both sets of tags and compare serial numbers. | ||
| 1130 | */ | ||
| 1131 | |||
| 1132 | if (in_scan > 0) { | ||
| 1133 | /* Only do this for forward scanning */ | ||
| 1134 | yaffs_rd_chunk_tags_nand(dev, | ||
| 1135 | nand_chunk, | ||
| 1136 | NULL, &new_tags); | ||
| 1137 | |||
| 1138 | /* Do a proper find */ | ||
| 1139 | existing_cunk = | ||
| 1140 | yaffs_find_chunk_in_file(in, inode_chunk, | ||
| 1141 | &existing_tags); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | if (existing_cunk <= 0) { | ||
| 1145 | /*Hoosterman - how did this happen? */ | ||
| 1146 | |||
| 1147 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 1148 | "yaffs tragedy: existing chunk < 0 in scan" | ||
| 1149 | ); | ||
| 1150 | |||
| 1151 | } | ||
| 1152 | |||
| 1153 | /* NB The deleted flags should be false, otherwise the chunks will | ||
| 1154 | * not be loaded during a scan | ||
| 1155 | */ | ||
| 1156 | |||
| 1157 | if (in_scan > 0) { | ||
| 1158 | new_serial = new_tags.serial_number; | ||
| 1159 | existing_serial = existing_tags.serial_number; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | if ((in_scan > 0) && | ||
| 1163 | (existing_cunk <= 0 || | ||
| 1164 | ((existing_serial + 1) & 3) == new_serial)) { | ||
| 1165 | /* Forward scanning. | ||
| 1166 | * Use new | ||
| 1167 | * Delete the old one and drop through to update the tnode | ||
| 1168 | */ | ||
| 1169 | yaffs_chunk_del(dev, existing_cunk, 1, | ||
| 1170 | __LINE__); | ||
| 1171 | } else { | ||
| 1172 | /* Backward scanning or we want to use the existing one | ||
| 1173 | * Use existing. | ||
| 1174 | * Delete the new one and return early so that the tnode isn't changed | ||
| 1175 | */ | ||
| 1176 | yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); | ||
| 1177 | return YAFFS_OK; | ||
| 1178 | } | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | } | ||
| 1182 | |||
| 1183 | if (existing_cunk == 0) | ||
| 1184 | in->n_data_chunks++; | ||
| 1185 | |||
| 1186 | yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk); | ||
| 1187 | |||
| 1188 | return YAFFS_OK; | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk) | ||
| 1192 | { | ||
| 1193 | struct yaffs_block_info *the_block; | ||
| 1194 | unsigned block_no; | ||
| 1195 | |||
| 1196 | yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk); | ||
| 1197 | |||
| 1198 | block_no = chunk / dev->param.chunks_per_block; | ||
| 1199 | the_block = yaffs_get_block_info(dev, block_no); | ||
| 1200 | if (the_block) { | ||
| 1201 | the_block->soft_del_pages++; | ||
| 1202 | dev->n_free_chunks++; | ||
| 1203 | yaffs2_update_oldest_dirty_seq(dev, block_no, the_block); | ||
| 1204 | } | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file. | ||
| 1208 | * All soft deleting does is increment the block's softdelete count and pulls the chunk out | ||
| 1209 | * of the tnode. | ||
| 1210 | * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted. | ||
| 1211 | */ | ||
| 1212 | |||
| 1213 | static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn, | ||
| 1214 | u32 level, int chunk_offset) | ||
| 1215 | { | ||
| 1216 | int i; | ||
| 1217 | int the_chunk; | ||
| 1218 | int all_done = 1; | ||
| 1219 | struct yaffs_dev *dev = in->my_dev; | ||
| 1220 | |||
| 1221 | if (tn) { | ||
| 1222 | if (level > 0) { | ||
| 1223 | |||
| 1224 | for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0; | ||
| 1225 | i--) { | ||
| 1226 | if (tn->internal[i]) { | ||
| 1227 | all_done = | ||
| 1228 | yaffs_soft_del_worker(in, | ||
| 1229 | tn->internal | ||
| 1230 | [i], | ||
| 1231 | level - 1, | ||
| 1232 | (chunk_offset | ||
| 1233 | << | ||
| 1234 | YAFFS_TNODES_INTERNAL_BITS) | ||
| 1235 | + i); | ||
| 1236 | if (all_done) { | ||
| 1237 | yaffs_free_tnode(dev, | ||
| 1238 | tn->internal | ||
| 1239 | [i]); | ||
| 1240 | tn->internal[i] = NULL; | ||
| 1241 | } else { | ||
| 1242 | /* Hoosterman... how could this happen? */ | ||
| 1243 | } | ||
| 1244 | } | ||
| 1245 | } | ||
| 1246 | return (all_done) ? 1 : 0; | ||
| 1247 | } else if (level == 0) { | ||
| 1248 | |||
| 1249 | for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) { | ||
| 1250 | the_chunk = yaffs_get_group_base(dev, tn, i); | ||
| 1251 | if (the_chunk) { | ||
| 1252 | /* Note this does not find the real chunk, only the chunk group. | ||
| 1253 | * We make an assumption that a chunk group is not larger than | ||
| 1254 | * a block. | ||
| 1255 | */ | ||
| 1256 | yaffs_soft_del_chunk(dev, the_chunk); | ||
| 1257 | yaffs_load_tnode_0(dev, tn, i, 0); | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | } | ||
| 1261 | return 1; | ||
| 1262 | |||
| 1263 | } | ||
| 1264 | |||
| 1265 | } | ||
| 1266 | |||
| 1267 | return 1; | ||
| 1268 | |||
| 1269 | } | ||
| 1270 | |||
| 1271 | static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj) | ||
| 1272 | { | ||
| 1273 | struct yaffs_dev *dev = obj->my_dev; | ||
| 1274 | struct yaffs_obj *parent; | ||
| 1275 | |||
| 1276 | yaffs_verify_obj_in_dir(obj); | ||
| 1277 | parent = obj->parent; | ||
| 1278 | |||
| 1279 | yaffs_verify_dir(parent); | ||
| 1280 | |||
| 1281 | if (dev && dev->param.remove_obj_fn) | ||
| 1282 | dev->param.remove_obj_fn(obj); | ||
| 1283 | |||
| 1284 | list_del_init(&obj->siblings); | ||
| 1285 | obj->parent = NULL; | ||
| 1286 | |||
| 1287 | yaffs_verify_dir(parent); | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj) | ||
| 1291 | { | ||
| 1292 | if (!directory) { | ||
| 1293 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 1294 | "tragedy: Trying to add an object to a null pointer directory" | ||
| 1295 | ); | ||
| 1296 | YBUG(); | ||
| 1297 | return; | ||
| 1298 | } | ||
| 1299 | if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 1300 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 1301 | "tragedy: Trying to add an object to a non-directory" | ||
| 1302 | ); | ||
| 1303 | YBUG(); | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | if (obj->siblings.prev == NULL) { | ||
| 1307 | /* Not initialised */ | ||
| 1308 | YBUG(); | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | yaffs_verify_dir(directory); | ||
| 1312 | |||
| 1313 | yaffs_remove_obj_from_dir(obj); | ||
| 1314 | |||
| 1315 | /* Now add it */ | ||
| 1316 | list_add(&obj->siblings, &directory->variant.dir_variant.children); | ||
| 1317 | obj->parent = directory; | ||
| 1318 | |||
| 1319 | if (directory == obj->my_dev->unlinked_dir | ||
| 1320 | || directory == obj->my_dev->del_dir) { | ||
| 1321 | obj->unlinked = 1; | ||
| 1322 | obj->my_dev->n_unlinked_files++; | ||
| 1323 | obj->rename_allowed = 0; | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | yaffs_verify_dir(directory); | ||
| 1327 | yaffs_verify_obj_in_dir(obj); | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | static int yaffs_change_obj_name(struct yaffs_obj *obj, | ||
| 1331 | struct yaffs_obj *new_dir, | ||
| 1332 | const YCHAR * new_name, int force, int shadows) | ||
| 1333 | { | ||
| 1334 | int unlink_op; | ||
| 1335 | int del_op; | ||
| 1336 | |||
| 1337 | struct yaffs_obj *existing_target; | ||
| 1338 | |||
| 1339 | if (new_dir == NULL) | ||
| 1340 | new_dir = obj->parent; /* use the old directory */ | ||
| 1341 | |||
| 1342 | if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 1343 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 1344 | "tragedy: yaffs_change_obj_name: new_dir is not a directory" | ||
| 1345 | ); | ||
| 1346 | YBUG(); | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */ | ||
| 1350 | if (obj->my_dev->param.is_yaffs2) | ||
| 1351 | unlink_op = (new_dir == obj->my_dev->unlinked_dir); | ||
| 1352 | else | ||
| 1353 | unlink_op = (new_dir == obj->my_dev->unlinked_dir | ||
| 1354 | && obj->variant_type == YAFFS_OBJECT_TYPE_FILE); | ||
| 1355 | |||
| 1356 | del_op = (new_dir == obj->my_dev->del_dir); | ||
| 1357 | |||
| 1358 | existing_target = yaffs_find_by_name(new_dir, new_name); | ||
| 1359 | |||
| 1360 | /* If the object is a file going into the unlinked directory, | ||
| 1361 | * then it is OK to just stuff it in since duplicate names are allowed. | ||
| 1362 | * else only proceed if the new name does not exist and if we're putting | ||
| 1363 | * it into a directory. | ||
| 1364 | */ | ||
| 1365 | if ((unlink_op || | ||
| 1366 | del_op || | ||
| 1367 | force || | ||
| 1368 | (shadows > 0) || | ||
| 1369 | !existing_target) && | ||
| 1370 | new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 1371 | yaffs_set_obj_name(obj, new_name); | ||
| 1372 | obj->dirty = 1; | ||
| 1373 | |||
| 1374 | yaffs_add_obj_to_dir(new_dir, obj); | ||
| 1375 | |||
| 1376 | if (unlink_op) | ||
| 1377 | obj->unlinked = 1; | ||
| 1378 | |||
| 1379 | /* If it is a deletion then we mark it as a shrink for gc purposes. */ | ||
| 1380 | if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= | ||
| 1381 | 0) | ||
| 1382 | return YAFFS_OK; | ||
| 1383 | } | ||
| 1384 | |||
| 1385 | return YAFFS_FAIL; | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | /*------------------------ Short Operations Cache ---------------------------------------- | ||
| 1389 | * In many situations where there is no high level buffering a lot of | ||
| 1390 | * reads might be short sequential reads, and a lot of writes may be short | ||
| 1391 | * sequential writes. eg. scanning/writing a jpeg file. | ||
| 1392 | * In these cases, a short read/write cache can provide a huge perfomance | ||
| 1393 | * benefit with dumb-as-a-rock code. | ||
| 1394 | * In Linux, the page cache provides read buffering and the short op cache | ||
| 1395 | * provides write buffering. | ||
| 1396 | * | ||
| 1397 | * There are a limited number (~10) of cache chunks per device so that we don't | ||
| 1398 | * need a very intelligent search. | ||
| 1399 | */ | ||
| 1400 | |||
| 1401 | static int yaffs_obj_cache_dirty(struct yaffs_obj *obj) | ||
| 1402 | { | ||
| 1403 | struct yaffs_dev *dev = obj->my_dev; | ||
| 1404 | int i; | ||
| 1405 | struct yaffs_cache *cache; | ||
| 1406 | int n_caches = obj->my_dev->param.n_caches; | ||
| 1407 | |||
| 1408 | for (i = 0; i < n_caches; i++) { | ||
| 1409 | cache = &dev->cache[i]; | ||
| 1410 | if (cache->object == obj && cache->dirty) | ||
| 1411 | return 1; | ||
| 1412 | } | ||
| 1413 | |||
| 1414 | return 0; | ||
| 1415 | } | ||
| 1416 | |||
| 1417 | static void yaffs_flush_file_cache(struct yaffs_obj *obj) | ||
| 1418 | { | ||
| 1419 | struct yaffs_dev *dev = obj->my_dev; | ||
| 1420 | int lowest = -99; /* Stop compiler whining. */ | ||
| 1421 | int i; | ||
| 1422 | struct yaffs_cache *cache; | ||
| 1423 | int chunk_written = 0; | ||
| 1424 | int n_caches = obj->my_dev->param.n_caches; | ||
| 1425 | |||
| 1426 | if (n_caches > 0) { | ||
| 1427 | do { | ||
| 1428 | cache = NULL; | ||
| 1429 | |||
| 1430 | /* Find the dirty cache for this object with the lowest chunk id. */ | ||
| 1431 | for (i = 0; i < n_caches; i++) { | ||
| 1432 | if (dev->cache[i].object == obj && | ||
| 1433 | dev->cache[i].dirty) { | ||
| 1434 | if (!cache | ||
| 1435 | || dev->cache[i].chunk_id < | ||
| 1436 | lowest) { | ||
| 1437 | cache = &dev->cache[i]; | ||
| 1438 | lowest = cache->chunk_id; | ||
| 1439 | } | ||
| 1440 | } | ||
| 1441 | } | ||
| 1442 | |||
| 1443 | if (cache && !cache->locked) { | ||
| 1444 | /* Write it out and free it up */ | ||
| 1445 | |||
| 1446 | chunk_written = | ||
| 1447 | yaffs_wr_data_obj(cache->object, | ||
| 1448 | cache->chunk_id, | ||
| 1449 | cache->data, | ||
| 1450 | cache->n_bytes, 1); | ||
| 1451 | cache->dirty = 0; | ||
| 1452 | cache->object = NULL; | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | } while (cache && chunk_written > 0); | ||
| 1456 | |||
| 1457 | if (cache) | ||
| 1458 | /* Hoosterman, disk full while writing cache out. */ | ||
| 1459 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 1460 | "yaffs tragedy: no space during cache write"); | ||
| 1461 | |||
| 1462 | } | ||
| 1463 | |||
| 1464 | } | ||
| 1465 | |||
| 1466 | /*yaffs_flush_whole_cache(dev) | ||
| 1467 | * | ||
| 1468 | * | ||
| 1469 | */ | ||
| 1470 | |||
| 1471 | void yaffs_flush_whole_cache(struct yaffs_dev *dev) | ||
| 1472 | { | ||
| 1473 | struct yaffs_obj *obj; | ||
| 1474 | int n_caches = dev->param.n_caches; | ||
| 1475 | int i; | ||
| 1476 | |||
| 1477 | /* Find a dirty object in the cache and flush it... | ||
| 1478 | * until there are no further dirty objects. | ||
| 1479 | */ | ||
| 1480 | do { | ||
| 1481 | obj = NULL; | ||
| 1482 | for (i = 0; i < n_caches && !obj; i++) { | ||
| 1483 | if (dev->cache[i].object && dev->cache[i].dirty) | ||
| 1484 | obj = dev->cache[i].object; | ||
| 1485 | |||
| 1486 | } | ||
| 1487 | if (obj) | ||
| 1488 | yaffs_flush_file_cache(obj); | ||
| 1489 | |||
| 1490 | } while (obj); | ||
| 1491 | |||
| 1492 | } | ||
| 1493 | |||
| 1494 | /* Grab us a cache chunk for use. | ||
| 1495 | * First look for an empty one. | ||
| 1496 | * Then look for the least recently used non-dirty one. | ||
| 1497 | * Then look for the least recently used dirty one...., flush and look again. | ||
| 1498 | */ | ||
| 1499 | static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev) | ||
| 1500 | { | ||
| 1501 | int i; | ||
| 1502 | |||
| 1503 | if (dev->param.n_caches > 0) { | ||
| 1504 | for (i = 0; i < dev->param.n_caches; i++) { | ||
| 1505 | if (!dev->cache[i].object) | ||
| 1506 | return &dev->cache[i]; | ||
| 1507 | } | ||
| 1508 | } | ||
| 1509 | |||
| 1510 | return NULL; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) | ||
| 1514 | { | ||
| 1515 | struct yaffs_cache *cache; | ||
| 1516 | struct yaffs_obj *the_obj; | ||
| 1517 | int usage; | ||
| 1518 | int i; | ||
| 1519 | int pushout; | ||
| 1520 | |||
| 1521 | if (dev->param.n_caches > 0) { | ||
| 1522 | /* Try find a non-dirty one... */ | ||
| 1523 | |||
| 1524 | cache = yaffs_grab_chunk_worker(dev); | ||
| 1525 | |||
| 1526 | if (!cache) { | ||
| 1527 | /* They were all dirty, find the last recently used object and flush | ||
| 1528 | * its cache, then find again. | ||
| 1529 | * NB what's here is not very accurate, we actually flush the object | ||
| 1530 | * the last recently used page. | ||
| 1531 | */ | ||
| 1532 | |||
| 1533 | /* With locking we can't assume we can use entry zero */ | ||
| 1534 | |||
| 1535 | the_obj = NULL; | ||
| 1536 | usage = -1; | ||
| 1537 | cache = NULL; | ||
| 1538 | pushout = -1; | ||
| 1539 | |||
| 1540 | for (i = 0; i < dev->param.n_caches; i++) { | ||
| 1541 | if (dev->cache[i].object && | ||
| 1542 | !dev->cache[i].locked && | ||
| 1543 | (dev->cache[i].last_use < usage | ||
| 1544 | || !cache)) { | ||
| 1545 | usage = dev->cache[i].last_use; | ||
| 1546 | the_obj = dev->cache[i].object; | ||
| 1547 | cache = &dev->cache[i]; | ||
| 1548 | pushout = i; | ||
| 1549 | } | ||
| 1550 | } | ||
| 1551 | |||
| 1552 | if (!cache || cache->dirty) { | ||
| 1553 | /* Flush and try again */ | ||
| 1554 | yaffs_flush_file_cache(the_obj); | ||
| 1555 | cache = yaffs_grab_chunk_worker(dev); | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | } | ||
| 1559 | return cache; | ||
| 1560 | } else { | ||
| 1561 | return NULL; | ||
| 1562 | } | ||
| 1563 | } | ||
| 1564 | |||
| 1565 | /* Find a cached chunk */ | ||
| 1566 | static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj, | ||
| 1567 | int chunk_id) | ||
| 1568 | { | ||
| 1569 | struct yaffs_dev *dev = obj->my_dev; | ||
| 1570 | int i; | ||
| 1571 | if (dev->param.n_caches > 0) { | ||
| 1572 | for (i = 0; i < dev->param.n_caches; i++) { | ||
| 1573 | if (dev->cache[i].object == obj && | ||
| 1574 | dev->cache[i].chunk_id == chunk_id) { | ||
| 1575 | dev->cache_hits++; | ||
| 1576 | |||
| 1577 | return &dev->cache[i]; | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | } | ||
| 1581 | return NULL; | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | /* Mark the chunk for the least recently used algorithym */ | ||
| 1585 | static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache, | ||
| 1586 | int is_write) | ||
| 1587 | { | ||
| 1588 | |||
| 1589 | if (dev->param.n_caches > 0) { | ||
| 1590 | if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) { | ||
| 1591 | /* Reset the cache usages */ | ||
| 1592 | int i; | ||
| 1593 | for (i = 1; i < dev->param.n_caches; i++) | ||
| 1594 | dev->cache[i].last_use = 0; | ||
| 1595 | |||
| 1596 | dev->cache_last_use = 0; | ||
| 1597 | } | ||
| 1598 | |||
| 1599 | dev->cache_last_use++; | ||
| 1600 | |||
| 1601 | cache->last_use = dev->cache_last_use; | ||
| 1602 | |||
| 1603 | if (is_write) | ||
| 1604 | cache->dirty = 1; | ||
| 1605 | } | ||
| 1606 | } | ||
| 1607 | |||
| 1608 | /* Invalidate a single cache page. | ||
| 1609 | * Do this when a whole page gets written, | ||
| 1610 | * ie the short cache for this page is no longer valid. | ||
| 1611 | */ | ||
| 1612 | static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id) | ||
| 1613 | { | ||
| 1614 | if (object->my_dev->param.n_caches > 0) { | ||
| 1615 | struct yaffs_cache *cache = | ||
| 1616 | yaffs_find_chunk_cache(object, chunk_id); | ||
| 1617 | |||
| 1618 | if (cache) | ||
| 1619 | cache->object = NULL; | ||
| 1620 | } | ||
| 1621 | } | ||
| 1622 | |||
| 1623 | /* Invalidate all the cache pages associated with this object | ||
| 1624 | * Do this whenever ther file is deleted or resized. | ||
| 1625 | */ | ||
| 1626 | static void yaffs_invalidate_whole_cache(struct yaffs_obj *in) | ||
| 1627 | { | ||
| 1628 | int i; | ||
| 1629 | struct yaffs_dev *dev = in->my_dev; | ||
| 1630 | |||
| 1631 | if (dev->param.n_caches > 0) { | ||
| 1632 | /* Invalidate it. */ | ||
| 1633 | for (i = 0; i < dev->param.n_caches; i++) { | ||
| 1634 | if (dev->cache[i].object == in) | ||
| 1635 | dev->cache[i].object = NULL; | ||
| 1636 | } | ||
| 1637 | } | ||
| 1638 | } | ||
| 1639 | |||
| 1640 | static void yaffs_unhash_obj(struct yaffs_obj *obj) | ||
| 1641 | { | ||
| 1642 | int bucket; | ||
| 1643 | struct yaffs_dev *dev = obj->my_dev; | ||
| 1644 | |||
| 1645 | /* If it is still linked into the bucket list, free from the list */ | ||
| 1646 | if (!list_empty(&obj->hash_link)) { | ||
| 1647 | list_del_init(&obj->hash_link); | ||
| 1648 | bucket = yaffs_hash_fn(obj->obj_id); | ||
| 1649 | dev->obj_bucket[bucket].count--; | ||
| 1650 | } | ||
| 1651 | } | ||
| 1652 | |||
| 1653 | /* FreeObject frees up a Object and puts it back on the free list */ | ||
| 1654 | static void yaffs_free_obj(struct yaffs_obj *obj) | ||
| 1655 | { | ||
| 1656 | struct yaffs_dev *dev = obj->my_dev; | ||
| 1657 | |||
| 1658 | yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p", | ||
| 1659 | obj, obj->my_inode); | ||
| 1660 | |||
| 1661 | if (!obj) | ||
| 1662 | YBUG(); | ||
| 1663 | if (obj->parent) | ||
| 1664 | YBUG(); | ||
| 1665 | if (!list_empty(&obj->siblings)) | ||
| 1666 | YBUG(); | ||
| 1667 | |||
| 1668 | if (obj->my_inode) { | ||
| 1669 | /* We're still hooked up to a cached inode. | ||
| 1670 | * Don't delete now, but mark for later deletion | ||
| 1671 | */ | ||
| 1672 | obj->defered_free = 1; | ||
| 1673 | return; | ||
| 1674 | } | ||
| 1675 | |||
| 1676 | yaffs_unhash_obj(obj); | ||
| 1677 | |||
| 1678 | yaffs_free_raw_obj(dev, obj); | ||
| 1679 | dev->n_obj--; | ||
| 1680 | dev->checkpoint_blocks_required = 0; /* force recalculation */ | ||
| 1681 | } | ||
| 1682 | |||
| 1683 | void yaffs_handle_defered_free(struct yaffs_obj *obj) | ||
| 1684 | { | ||
| 1685 | if (obj->defered_free) | ||
| 1686 | yaffs_free_obj(obj); | ||
| 1687 | } | ||
| 1688 | |||
| 1689 | static int yaffs_generic_obj_del(struct yaffs_obj *in) | ||
| 1690 | { | ||
| 1691 | |||
| 1692 | /* First off, invalidate the file's data in the cache, without flushing. */ | ||
| 1693 | yaffs_invalidate_whole_cache(in); | ||
| 1694 | |||
| 1695 | if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) { | ||
| 1696 | /* Move to the unlinked directory so we have a record that it was deleted. */ | ||
| 1697 | yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0, | ||
| 1698 | 0); | ||
| 1699 | |||
| 1700 | } | ||
| 1701 | |||
| 1702 | yaffs_remove_obj_from_dir(in); | ||
| 1703 | yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__); | ||
| 1704 | in->hdr_chunk = 0; | ||
| 1705 | |||
| 1706 | yaffs_free_obj(in); | ||
| 1707 | return YAFFS_OK; | ||
| 1708 | |||
| 1709 | } | ||
| 1710 | |||
| 1711 | static void yaffs_soft_del_file(struct yaffs_obj *obj) | ||
| 1712 | { | ||
| 1713 | if (obj->deleted && | ||
| 1714 | obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) { | ||
| 1715 | if (obj->n_data_chunks <= 0) { | ||
| 1716 | /* Empty file with no duplicate object headers, | ||
| 1717 | * just delete it immediately */ | ||
| 1718 | yaffs_free_tnode(obj->my_dev, | ||
| 1719 | obj->variant.file_variant.top); | ||
| 1720 | obj->variant.file_variant.top = NULL; | ||
| 1721 | yaffs_trace(YAFFS_TRACE_TRACING, | ||
| 1722 | "yaffs: Deleting empty file %d", | ||
| 1723 | obj->obj_id); | ||
| 1724 | yaffs_generic_obj_del(obj); | ||
| 1725 | } else { | ||
| 1726 | yaffs_soft_del_worker(obj, | ||
| 1727 | obj->variant.file_variant.top, | ||
| 1728 | obj->variant. | ||
| 1729 | file_variant.top_level, 0); | ||
| 1730 | obj->soft_del = 1; | ||
| 1731 | } | ||
| 1732 | } | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | /* Pruning removes any part of the file structure tree that is beyond the | ||
| 1736 | * bounds of the file (ie that does not point to chunks). | ||
| 1737 | * | ||
| 1738 | * A file should only get pruned when its size is reduced. | ||
| 1739 | * | ||
| 1740 | * Before pruning, the chunks must be pulled from the tree and the | ||
| 1741 | * level 0 tnode entries must be zeroed out. | ||
| 1742 | * Could also use this for file deletion, but that's probably better handled | ||
| 1743 | * by a special case. | ||
| 1744 | * | ||
| 1745 | * This function is recursive. For levels > 0 the function is called again on | ||
| 1746 | * any sub-tree. For level == 0 we just check if the sub-tree has data. | ||
| 1747 | * If there is no data in a subtree then it is pruned. | ||
| 1748 | */ | ||
| 1749 | |||
| 1750 | static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev, | ||
| 1751 | struct yaffs_tnode *tn, u32 level, | ||
| 1752 | int del0) | ||
| 1753 | { | ||
| 1754 | int i; | ||
| 1755 | int has_data; | ||
| 1756 | |||
| 1757 | if (tn) { | ||
| 1758 | has_data = 0; | ||
| 1759 | |||
| 1760 | if (level > 0) { | ||
| 1761 | for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) { | ||
| 1762 | if (tn->internal[i]) { | ||
| 1763 | tn->internal[i] = | ||
| 1764 | yaffs_prune_worker(dev, | ||
| 1765 | tn->internal[i], | ||
| 1766 | level - 1, | ||
| 1767 | (i == | ||
| 1768 | 0) ? del0 : 1); | ||
| 1769 | } | ||
| 1770 | |||
| 1771 | if (tn->internal[i]) | ||
| 1772 | has_data++; | ||
| 1773 | } | ||
| 1774 | } else { | ||
| 1775 | int tnode_size_u32 = dev->tnode_size / sizeof(u32); | ||
| 1776 | u32 *map = (u32 *) tn; | ||
| 1777 | |||
| 1778 | for (i = 0; !has_data && i < tnode_size_u32; i++) { | ||
| 1779 | if (map[i]) | ||
| 1780 | has_data++; | ||
| 1781 | } | ||
| 1782 | } | ||
| 1783 | |||
| 1784 | if (has_data == 0 && del0) { | ||
| 1785 | /* Free and return NULL */ | ||
| 1786 | |||
| 1787 | yaffs_free_tnode(dev, tn); | ||
| 1788 | tn = NULL; | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | } | ||
| 1792 | |||
| 1793 | return tn; | ||
| 1794 | |||
| 1795 | } | ||
| 1796 | |||
| 1797 | static int yaffs_prune_tree(struct yaffs_dev *dev, | ||
| 1798 | struct yaffs_file_var *file_struct) | ||
| 1799 | { | ||
| 1800 | int i; | ||
| 1801 | int has_data; | ||
| 1802 | int done = 0; | ||
| 1803 | struct yaffs_tnode *tn; | ||
| 1804 | |||
| 1805 | if (file_struct->top_level > 0) { | ||
| 1806 | file_struct->top = | ||
| 1807 | yaffs_prune_worker(dev, file_struct->top, | ||
| 1808 | file_struct->top_level, 0); | ||
| 1809 | |||
| 1810 | /* Now we have a tree with all the non-zero branches NULL but the height | ||
| 1811 | * is the same as it was. | ||
| 1812 | * Let's see if we can trim internal tnodes to shorten the tree. | ||
| 1813 | * We can do this if only the 0th element in the tnode is in use | ||
| 1814 | * (ie all the non-zero are NULL) | ||
| 1815 | */ | ||
| 1816 | |||
| 1817 | while (file_struct->top_level && !done) { | ||
| 1818 | tn = file_struct->top; | ||
| 1819 | |||
| 1820 | has_data = 0; | ||
| 1821 | for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) { | ||
| 1822 | if (tn->internal[i]) | ||
| 1823 | has_data++; | ||
| 1824 | } | ||
| 1825 | |||
| 1826 | if (!has_data) { | ||
| 1827 | file_struct->top = tn->internal[0]; | ||
| 1828 | file_struct->top_level--; | ||
| 1829 | yaffs_free_tnode(dev, tn); | ||
| 1830 | } else { | ||
| 1831 | done = 1; | ||
| 1832 | } | ||
| 1833 | } | ||
| 1834 | } | ||
| 1835 | |||
| 1836 | return YAFFS_OK; | ||
| 1837 | } | ||
| 1838 | |||
| 1839 | /*-------------------- End of File Structure functions.-------------------*/ | ||
| 1840 | |||
| 1841 | /* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */ | ||
| 1842 | static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev) | ||
| 1843 | { | ||
| 1844 | struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev); | ||
| 1845 | |||
| 1846 | if (obj) { | ||
| 1847 | dev->n_obj++; | ||
| 1848 | |||
| 1849 | /* Now sweeten it up... */ | ||
| 1850 | |||
| 1851 | memset(obj, 0, sizeof(struct yaffs_obj)); | ||
| 1852 | obj->being_created = 1; | ||
| 1853 | |||
| 1854 | obj->my_dev = dev; | ||
| 1855 | obj->hdr_chunk = 0; | ||
| 1856 | obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN; | ||
| 1857 | INIT_LIST_HEAD(&(obj->hard_links)); | ||
| 1858 | INIT_LIST_HEAD(&(obj->hash_link)); | ||
| 1859 | INIT_LIST_HEAD(&obj->siblings); | ||
| 1860 | |||
| 1861 | /* Now make the directory sane */ | ||
| 1862 | if (dev->root_dir) { | ||
| 1863 | obj->parent = dev->root_dir; | ||
| 1864 | list_add(&(obj->siblings), | ||
| 1865 | &dev->root_dir->variant.dir_variant.children); | ||
| 1866 | } | ||
| 1867 | |||
| 1868 | /* Add it to the lost and found directory. | ||
| 1869 | * NB Can't put root or lost-n-found in lost-n-found so | ||
| 1870 | * check if lost-n-found exists first | ||
| 1871 | */ | ||
| 1872 | if (dev->lost_n_found) | ||
| 1873 | yaffs_add_obj_to_dir(dev->lost_n_found, obj); | ||
| 1874 | |||
| 1875 | obj->being_created = 0; | ||
| 1876 | } | ||
| 1877 | |||
| 1878 | dev->checkpoint_blocks_required = 0; /* force recalculation */ | ||
| 1879 | |||
| 1880 | return obj; | ||
| 1881 | } | ||
| 1882 | |||
| 1883 | static int yaffs_find_nice_bucket(struct yaffs_dev *dev) | ||
| 1884 | { | ||
| 1885 | int i; | ||
| 1886 | int l = 999; | ||
| 1887 | int lowest = 999999; | ||
| 1888 | |||
| 1889 | /* Search for the shortest list or one that | ||
| 1890 | * isn't too long. | ||
| 1891 | */ | ||
| 1892 | |||
| 1893 | for (i = 0; i < 10 && lowest > 4; i++) { | ||
| 1894 | dev->bucket_finder++; | ||
| 1895 | dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS; | ||
| 1896 | if (dev->obj_bucket[dev->bucket_finder].count < lowest) { | ||
| 1897 | lowest = dev->obj_bucket[dev->bucket_finder].count; | ||
| 1898 | l = dev->bucket_finder; | ||
| 1899 | } | ||
| 1900 | |||
| 1901 | } | ||
| 1902 | |||
| 1903 | return l; | ||
| 1904 | } | ||
| 1905 | |||
| 1906 | static int yaffs_new_obj_id(struct yaffs_dev *dev) | ||
| 1907 | { | ||
| 1908 | int bucket = yaffs_find_nice_bucket(dev); | ||
| 1909 | |||
| 1910 | /* Now find an object value that has not already been taken | ||
| 1911 | * by scanning the list. | ||
| 1912 | */ | ||
| 1913 | |||
| 1914 | int found = 0; | ||
| 1915 | struct list_head *i; | ||
| 1916 | |||
| 1917 | u32 n = (u32) bucket; | ||
| 1918 | |||
| 1919 | /* yaffs_check_obj_hash_sane(); */ | ||
| 1920 | |||
| 1921 | while (!found) { | ||
| 1922 | found = 1; | ||
| 1923 | n += YAFFS_NOBJECT_BUCKETS; | ||
| 1924 | if (1 || dev->obj_bucket[bucket].count > 0) { | ||
| 1925 | list_for_each(i, &dev->obj_bucket[bucket].list) { | ||
| 1926 | /* If there is already one in the list */ | ||
| 1927 | if (i && list_entry(i, struct yaffs_obj, | ||
| 1928 | hash_link)->obj_id == n) { | ||
| 1929 | found = 0; | ||
| 1930 | } | ||
| 1931 | } | ||
| 1932 | } | ||
| 1933 | } | ||
| 1934 | |||
| 1935 | return n; | ||
| 1936 | } | ||
| 1937 | |||
| 1938 | static void yaffs_hash_obj(struct yaffs_obj *in) | ||
| 1939 | { | ||
| 1940 | int bucket = yaffs_hash_fn(in->obj_id); | ||
| 1941 | struct yaffs_dev *dev = in->my_dev; | ||
| 1942 | |||
| 1943 | list_add(&in->hash_link, &dev->obj_bucket[bucket].list); | ||
| 1944 | dev->obj_bucket[bucket].count++; | ||
| 1945 | } | ||
| 1946 | |||
| 1947 | struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number) | ||
| 1948 | { | ||
| 1949 | int bucket = yaffs_hash_fn(number); | ||
| 1950 | struct list_head *i; | ||
| 1951 | struct yaffs_obj *in; | ||
| 1952 | |||
| 1953 | list_for_each(i, &dev->obj_bucket[bucket].list) { | ||
| 1954 | /* Look if it is in the list */ | ||
| 1955 | if (i) { | ||
| 1956 | in = list_entry(i, struct yaffs_obj, hash_link); | ||
| 1957 | if (in->obj_id == number) { | ||
| 1958 | |||
| 1959 | /* Don't tell the VFS about this one if it is defered free */ | ||
| 1960 | if (in->defered_free) | ||
| 1961 | return NULL; | ||
| 1962 | |||
| 1963 | return in; | ||
| 1964 | } | ||
| 1965 | } | ||
| 1966 | } | ||
| 1967 | |||
| 1968 | return NULL; | ||
| 1969 | } | ||
| 1970 | |||
| 1971 | struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number, | ||
| 1972 | enum yaffs_obj_type type) | ||
| 1973 | { | ||
| 1974 | struct yaffs_obj *the_obj = NULL; | ||
| 1975 | struct yaffs_tnode *tn = NULL; | ||
| 1976 | |||
| 1977 | if (number < 0) | ||
| 1978 | number = yaffs_new_obj_id(dev); | ||
| 1979 | |||
| 1980 | if (type == YAFFS_OBJECT_TYPE_FILE) { | ||
| 1981 | tn = yaffs_get_tnode(dev); | ||
| 1982 | if (!tn) | ||
| 1983 | return NULL; | ||
| 1984 | } | ||
| 1985 | |||
| 1986 | the_obj = yaffs_alloc_empty_obj(dev); | ||
| 1987 | if (!the_obj) { | ||
| 1988 | if (tn) | ||
| 1989 | yaffs_free_tnode(dev, tn); | ||
| 1990 | return NULL; | ||
| 1991 | } | ||
| 1992 | |||
| 1993 | if (the_obj) { | ||
| 1994 | the_obj->fake = 0; | ||
| 1995 | the_obj->rename_allowed = 1; | ||
| 1996 | the_obj->unlink_allowed = 1; | ||
| 1997 | the_obj->obj_id = number; | ||
| 1998 | yaffs_hash_obj(the_obj); | ||
| 1999 | the_obj->variant_type = type; | ||
| 2000 | yaffs_load_current_time(the_obj, 1, 1); | ||
| 2001 | |||
| 2002 | switch (type) { | ||
| 2003 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 2004 | the_obj->variant.file_variant.file_size = 0; | ||
| 2005 | the_obj->variant.file_variant.scanned_size = 0; | ||
| 2006 | the_obj->variant.file_variant.shrink_size = ~0; /* max */ | ||
| 2007 | the_obj->variant.file_variant.top_level = 0; | ||
| 2008 | the_obj->variant.file_variant.top = tn; | ||
| 2009 | break; | ||
| 2010 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 2011 | INIT_LIST_HEAD(&the_obj->variant.dir_variant.children); | ||
| 2012 | INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty); | ||
| 2013 | break; | ||
| 2014 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 2015 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 2016 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 2017 | /* No action required */ | ||
| 2018 | break; | ||
| 2019 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 2020 | /* todo this should not happen */ | ||
| 2021 | break; | ||
| 2022 | } | ||
| 2023 | } | ||
| 2024 | |||
| 2025 | return the_obj; | ||
| 2026 | } | ||
| 2027 | |||
| 2028 | static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev, | ||
| 2029 | int number, u32 mode) | ||
| 2030 | { | ||
| 2031 | |||
| 2032 | struct yaffs_obj *obj = | ||
| 2033 | yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY); | ||
| 2034 | if (obj) { | ||
| 2035 | obj->fake = 1; /* it is fake so it might have no NAND presence... */ | ||
| 2036 | obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */ | ||
| 2037 | obj->unlink_allowed = 0; /* ... or unlink it */ | ||
| 2038 | obj->deleted = 0; | ||
| 2039 | obj->unlinked = 0; | ||
| 2040 | obj->yst_mode = mode; | ||
| 2041 | obj->my_dev = dev; | ||
| 2042 | obj->hdr_chunk = 0; /* Not a valid chunk. */ | ||
| 2043 | } | ||
| 2044 | |||
| 2045 | return obj; | ||
| 2046 | |||
| 2047 | } | ||
| 2048 | |||
| 2049 | |||
| 2050 | static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev) | ||
| 2051 | { | ||
| 2052 | int i; | ||
| 2053 | |||
| 2054 | dev->n_obj = 0; | ||
| 2055 | dev->n_tnodes = 0; | ||
| 2056 | |||
| 2057 | yaffs_init_raw_tnodes_and_objs(dev); | ||
| 2058 | |||
| 2059 | for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { | ||
| 2060 | INIT_LIST_HEAD(&dev->obj_bucket[i].list); | ||
| 2061 | dev->obj_bucket[i].count = 0; | ||
| 2062 | } | ||
| 2063 | } | ||
| 2064 | |||
| 2065 | struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev, | ||
| 2066 | int number, | ||
| 2067 | enum yaffs_obj_type type) | ||
| 2068 | { | ||
| 2069 | struct yaffs_obj *the_obj = NULL; | ||
| 2070 | |||
| 2071 | if (number > 0) | ||
| 2072 | the_obj = yaffs_find_by_number(dev, number); | ||
| 2073 | |||
| 2074 | if (!the_obj) | ||
| 2075 | the_obj = yaffs_new_obj(dev, number, type); | ||
| 2076 | |||
| 2077 | return the_obj; | ||
| 2078 | |||
| 2079 | } | ||
| 2080 | |||
| 2081 | YCHAR *yaffs_clone_str(const YCHAR * str) | ||
| 2082 | { | ||
| 2083 | YCHAR *new_str = NULL; | ||
| 2084 | int len; | ||
| 2085 | |||
| 2086 | if (!str) | ||
| 2087 | str = _Y(""); | ||
| 2088 | |||
| 2089 | len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH); | ||
| 2090 | new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS); | ||
| 2091 | if (new_str) { | ||
| 2092 | strncpy(new_str, str, len); | ||
| 2093 | new_str[len] = 0; | ||
| 2094 | } | ||
| 2095 | return new_str; | ||
| 2096 | |||
| 2097 | } | ||
| 2098 | /* | ||
| 2099 | *yaffs_update_parent() handles fixing a directories mtime and ctime when a new | ||
| 2100 | * link (ie. name) is created or deleted in the directory. | ||
| 2101 | * | ||
| 2102 | * ie. | ||
| 2103 | * create dir/a : update dir's mtime/ctime | ||
| 2104 | * rm dir/a: update dir's mtime/ctime | ||
| 2105 | * modify dir/a: don't update dir's mtimme/ctime | ||
| 2106 | * | ||
| 2107 | * This can be handled immediately or defered. Defering helps reduce the number | ||
| 2108 | * of updates when many files in a directory are changed within a brief period. | ||
| 2109 | * | ||
| 2110 | * If the directory updating is defered then yaffs_update_dirty_dirs must be | ||
| 2111 | * called periodically. | ||
| 2112 | */ | ||
| 2113 | |||
| 2114 | static void yaffs_update_parent(struct yaffs_obj *obj) | ||
| 2115 | { | ||
| 2116 | struct yaffs_dev *dev; | ||
| 2117 | if (!obj) | ||
| 2118 | return; | ||
| 2119 | dev = obj->my_dev; | ||
| 2120 | obj->dirty = 1; | ||
| 2121 | yaffs_load_current_time(obj, 0, 1); | ||
| 2122 | if (dev->param.defered_dir_update) { | ||
| 2123 | struct list_head *link = &obj->variant.dir_variant.dirty; | ||
| 2124 | |||
| 2125 | if (list_empty(link)) { | ||
| 2126 | list_add(link, &dev->dirty_dirs); | ||
| 2127 | yaffs_trace(YAFFS_TRACE_BACKGROUND, | ||
| 2128 | "Added object %d to dirty directories", | ||
| 2129 | obj->obj_id); | ||
| 2130 | } | ||
| 2131 | |||
| 2132 | } else { | ||
| 2133 | yaffs_update_oh(obj, NULL, 0, 0, 0, NULL); | ||
| 2134 | } | ||
| 2135 | } | ||
| 2136 | |||
| 2137 | void yaffs_update_dirty_dirs(struct yaffs_dev *dev) | ||
| 2138 | { | ||
| 2139 | struct list_head *link; | ||
| 2140 | struct yaffs_obj *obj; | ||
| 2141 | struct yaffs_dir_var *d_s; | ||
| 2142 | union yaffs_obj_var *o_v; | ||
| 2143 | |||
| 2144 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories"); | ||
| 2145 | |||
| 2146 | while (!list_empty(&dev->dirty_dirs)) { | ||
| 2147 | link = dev->dirty_dirs.next; | ||
| 2148 | list_del_init(link); | ||
| 2149 | |||
| 2150 | d_s = list_entry(link, struct yaffs_dir_var, dirty); | ||
| 2151 | o_v = list_entry(d_s, union yaffs_obj_var, dir_variant); | ||
| 2152 | obj = list_entry(o_v, struct yaffs_obj, variant); | ||
| 2153 | |||
| 2154 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d", | ||
| 2155 | obj->obj_id); | ||
| 2156 | |||
| 2157 | if (obj->dirty) | ||
| 2158 | yaffs_update_oh(obj, NULL, 0, 0, 0, NULL); | ||
| 2159 | } | ||
| 2160 | } | ||
| 2161 | |||
| 2162 | /* | ||
| 2163 | * Mknod (create) a new object. | ||
| 2164 | * equiv_obj only has meaning for a hard link; | ||
| 2165 | * alias_str only has meaning for a symlink. | ||
| 2166 | * rdev only has meaning for devices (a subset of special objects) | ||
| 2167 | */ | ||
| 2168 | |||
| 2169 | static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type, | ||
| 2170 | struct yaffs_obj *parent, | ||
| 2171 | const YCHAR * name, | ||
| 2172 | u32 mode, | ||
| 2173 | u32 uid, | ||
| 2174 | u32 gid, | ||
| 2175 | struct yaffs_obj *equiv_obj, | ||
| 2176 | const YCHAR * alias_str, u32 rdev) | ||
| 2177 | { | ||
| 2178 | struct yaffs_obj *in; | ||
| 2179 | YCHAR *str = NULL; | ||
| 2180 | |||
| 2181 | struct yaffs_dev *dev = parent->my_dev; | ||
| 2182 | |||
| 2183 | /* Check if the entry exists. If it does then fail the call since we don't want a dup. */ | ||
| 2184 | if (yaffs_find_by_name(parent, name)) | ||
| 2185 | return NULL; | ||
| 2186 | |||
| 2187 | if (type == YAFFS_OBJECT_TYPE_SYMLINK) { | ||
| 2188 | str = yaffs_clone_str(alias_str); | ||
| 2189 | if (!str) | ||
| 2190 | return NULL; | ||
| 2191 | } | ||
| 2192 | |||
| 2193 | in = yaffs_new_obj(dev, -1, type); | ||
| 2194 | |||
| 2195 | if (!in) { | ||
| 2196 | if (str) | ||
| 2197 | kfree(str); | ||
| 2198 | return NULL; | ||
| 2199 | } | ||
| 2200 | |||
| 2201 | if (in) { | ||
| 2202 | in->hdr_chunk = 0; | ||
| 2203 | in->valid = 1; | ||
| 2204 | in->variant_type = type; | ||
| 2205 | |||
| 2206 | in->yst_mode = mode; | ||
| 2207 | |||
| 2208 | yaffs_attribs_init(in, gid, uid, rdev); | ||
| 2209 | |||
| 2210 | in->n_data_chunks = 0; | ||
| 2211 | |||
| 2212 | yaffs_set_obj_name(in, name); | ||
| 2213 | in->dirty = 1; | ||
| 2214 | |||
| 2215 | yaffs_add_obj_to_dir(parent, in); | ||
| 2216 | |||
| 2217 | in->my_dev = parent->my_dev; | ||
| 2218 | |||
| 2219 | switch (type) { | ||
| 2220 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 2221 | in->variant.symlink_variant.alias = str; | ||
| 2222 | break; | ||
| 2223 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 2224 | in->variant.hardlink_variant.equiv_obj = equiv_obj; | ||
| 2225 | in->variant.hardlink_variant.equiv_id = | ||
| 2226 | equiv_obj->obj_id; | ||
| 2227 | list_add(&in->hard_links, &equiv_obj->hard_links); | ||
| 2228 | break; | ||
| 2229 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 2230 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 2231 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 2232 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 2233 | /* do nothing */ | ||
| 2234 | break; | ||
| 2235 | } | ||
| 2236 | |||
| 2237 | if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) { | ||
| 2238 | /* Could not create the object header, fail the creation */ | ||
| 2239 | yaffs_del_obj(in); | ||
| 2240 | in = NULL; | ||
| 2241 | } | ||
| 2242 | |||
| 2243 | yaffs_update_parent(parent); | ||
| 2244 | } | ||
| 2245 | |||
| 2246 | return in; | ||
| 2247 | } | ||
| 2248 | |||
| 2249 | struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, | ||
| 2250 | const YCHAR * name, u32 mode, u32 uid, | ||
| 2251 | u32 gid) | ||
| 2252 | { | ||
| 2253 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode, | ||
| 2254 | uid, gid, NULL, NULL, 0); | ||
| 2255 | } | ||
| 2256 | |||
| 2257 | struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name, | ||
| 2258 | u32 mode, u32 uid, u32 gid) | ||
| 2259 | { | ||
| 2260 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name, | ||
| 2261 | mode, uid, gid, NULL, NULL, 0); | ||
| 2262 | } | ||
| 2263 | |||
| 2264 | struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, | ||
| 2265 | const YCHAR * name, u32 mode, u32 uid, | ||
| 2266 | u32 gid, u32 rdev) | ||
| 2267 | { | ||
| 2268 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode, | ||
| 2269 | uid, gid, NULL, NULL, rdev); | ||
| 2270 | } | ||
| 2271 | |||
| 2272 | struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, | ||
| 2273 | const YCHAR * name, u32 mode, u32 uid, | ||
| 2274 | u32 gid, const YCHAR * alias) | ||
| 2275 | { | ||
| 2276 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode, | ||
| 2277 | uid, gid, NULL, alias, 0); | ||
| 2278 | } | ||
| 2279 | |||
| 2280 | /* yaffs_link_obj returns the object id of the equivalent object.*/ | ||
| 2281 | struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name, | ||
| 2282 | struct yaffs_obj *equiv_obj) | ||
| 2283 | { | ||
| 2284 | /* Get the real object in case we were fed a hard link as an equivalent object */ | ||
| 2285 | equiv_obj = yaffs_get_equivalent_obj(equiv_obj); | ||
| 2286 | |||
| 2287 | if (yaffs_create_obj | ||
| 2288 | (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0, | ||
| 2289 | equiv_obj, NULL, 0)) { | ||
| 2290 | return equiv_obj; | ||
| 2291 | } else { | ||
| 2292 | return NULL; | ||
| 2293 | } | ||
| 2294 | |||
| 2295 | } | ||
| 2296 | |||
| 2297 | |||
| 2298 | |||
| 2299 | /*------------------------- Block Management and Page Allocation ----------------*/ | ||
| 2300 | |||
| 2301 | static int yaffs_init_blocks(struct yaffs_dev *dev) | ||
| 2302 | { | ||
| 2303 | int n_blocks = dev->internal_end_block - dev->internal_start_block + 1; | ||
| 2304 | |||
| 2305 | dev->block_info = NULL; | ||
| 2306 | dev->chunk_bits = NULL; | ||
| 2307 | |||
| 2308 | dev->alloc_block = -1; /* force it to get a new one */ | ||
| 2309 | |||
| 2310 | /* If the first allocation strategy fails, thry the alternate one */ | ||
| 2311 | dev->block_info = | ||
| 2312 | kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS); | ||
| 2313 | if (!dev->block_info) { | ||
| 2314 | dev->block_info = | ||
| 2315 | vmalloc(n_blocks * sizeof(struct yaffs_block_info)); | ||
| 2316 | dev->block_info_alt = 1; | ||
| 2317 | } else { | ||
| 2318 | dev->block_info_alt = 0; | ||
| 2319 | } | ||
| 2320 | |||
| 2321 | if (dev->block_info) { | ||
| 2322 | /* Set up dynamic blockinfo stuff. Round up bytes. */ | ||
| 2323 | dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; | ||
| 2324 | dev->chunk_bits = | ||
| 2325 | kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS); | ||
| 2326 | if (!dev->chunk_bits) { | ||
| 2327 | dev->chunk_bits = | ||
| 2328 | vmalloc(dev->chunk_bit_stride * n_blocks); | ||
| 2329 | dev->chunk_bits_alt = 1; | ||
| 2330 | } else { | ||
| 2331 | dev->chunk_bits_alt = 0; | ||
| 2332 | } | ||
| 2333 | } | ||
| 2334 | |||
| 2335 | if (dev->block_info && dev->chunk_bits) { | ||
| 2336 | memset(dev->block_info, 0, | ||
| 2337 | n_blocks * sizeof(struct yaffs_block_info)); | ||
| 2338 | memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks); | ||
| 2339 | return YAFFS_OK; | ||
| 2340 | } | ||
| 2341 | |||
| 2342 | return YAFFS_FAIL; | ||
| 2343 | } | ||
| 2344 | |||
| 2345 | static void yaffs_deinit_blocks(struct yaffs_dev *dev) | ||
| 2346 | { | ||
| 2347 | if (dev->block_info_alt && dev->block_info) | ||
| 2348 | vfree(dev->block_info); | ||
| 2349 | else if (dev->block_info) | ||
| 2350 | kfree(dev->block_info); | ||
| 2351 | |||
| 2352 | dev->block_info_alt = 0; | ||
| 2353 | |||
| 2354 | dev->block_info = NULL; | ||
| 2355 | |||
| 2356 | if (dev->chunk_bits_alt && dev->chunk_bits) | ||
| 2357 | vfree(dev->chunk_bits); | ||
| 2358 | else if (dev->chunk_bits) | ||
| 2359 | kfree(dev->chunk_bits); | ||
| 2360 | dev->chunk_bits_alt = 0; | ||
| 2361 | dev->chunk_bits = NULL; | ||
| 2362 | } | ||
| 2363 | |||
| 2364 | void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no) | ||
| 2365 | { | ||
| 2366 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no); | ||
| 2367 | |||
| 2368 | int erased_ok = 0; | ||
| 2369 | |||
| 2370 | /* If the block is still healthy erase it and mark as clean. | ||
| 2371 | * If the block has had a data failure, then retire it. | ||
| 2372 | */ | ||
| 2373 | |||
| 2374 | yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE, | ||
| 2375 | "yaffs_block_became_dirty block %d state %d %s", | ||
| 2376 | block_no, bi->block_state, | ||
| 2377 | (bi->needs_retiring) ? "needs retiring" : ""); | ||
| 2378 | |||
| 2379 | yaffs2_clear_oldest_dirty_seq(dev, bi); | ||
| 2380 | |||
| 2381 | bi->block_state = YAFFS_BLOCK_STATE_DIRTY; | ||
| 2382 | |||
| 2383 | /* If this is the block being garbage collected then stop gc'ing this block */ | ||
| 2384 | if (block_no == dev->gc_block) | ||
| 2385 | dev->gc_block = 0; | ||
| 2386 | |||
| 2387 | /* If this block is currently the best candidate for gc then drop as a candidate */ | ||
| 2388 | if (block_no == dev->gc_dirtiest) { | ||
| 2389 | dev->gc_dirtiest = 0; | ||
| 2390 | dev->gc_pages_in_use = 0; | ||
| 2391 | } | ||
| 2392 | |||
| 2393 | if (!bi->needs_retiring) { | ||
| 2394 | yaffs2_checkpt_invalidate(dev); | ||
| 2395 | erased_ok = yaffs_erase_block(dev, block_no); | ||
| 2396 | if (!erased_ok) { | ||
| 2397 | dev->n_erase_failures++; | ||
| 2398 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, | ||
| 2399 | "**>> Erasure failed %d", block_no); | ||
| 2400 | } | ||
| 2401 | } | ||
| 2402 | |||
| 2403 | if (erased_ok && | ||
| 2404 | ((yaffs_trace_mask & YAFFS_TRACE_ERASE) | ||
| 2405 | || !yaffs_skip_verification(dev))) { | ||
| 2406 | int i; | ||
| 2407 | for (i = 0; i < dev->param.chunks_per_block; i++) { | ||
| 2408 | if (!yaffs_check_chunk_erased | ||
| 2409 | (dev, block_no * dev->param.chunks_per_block + i)) { | ||
| 2410 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 2411 | ">>Block %d erasure supposedly OK, but chunk %d not erased", | ||
| 2412 | block_no, i); | ||
| 2413 | } | ||
| 2414 | } | ||
| 2415 | } | ||
| 2416 | |||
| 2417 | if (erased_ok) { | ||
| 2418 | /* Clean it up... */ | ||
| 2419 | bi->block_state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 2420 | bi->seq_number = 0; | ||
| 2421 | dev->n_erased_blocks++; | ||
| 2422 | bi->pages_in_use = 0; | ||
| 2423 | bi->soft_del_pages = 0; | ||
| 2424 | bi->has_shrink_hdr = 0; | ||
| 2425 | bi->skip_erased_check = 1; /* Clean, so no need to check */ | ||
| 2426 | bi->gc_prioritise = 0; | ||
| 2427 | yaffs_clear_chunk_bits(dev, block_no); | ||
| 2428 | |||
| 2429 | yaffs_trace(YAFFS_TRACE_ERASE, | ||
| 2430 | "Erased block %d", block_no); | ||
| 2431 | } else { | ||
| 2432 | /* We lost a block of free space */ | ||
| 2433 | dev->n_free_chunks -= dev->param.chunks_per_block; | ||
| 2434 | yaffs_retire_block(dev, block_no); | ||
| 2435 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, | ||
| 2436 | "**>> Block %d retired", block_no); | ||
| 2437 | } | ||
| 2438 | } | ||
| 2439 | |||
| 2440 | |||
| 2441 | |||
| 2442 | static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block) | ||
| 2443 | { | ||
| 2444 | int old_chunk; | ||
| 2445 | int new_chunk; | ||
| 2446 | int mark_flash; | ||
| 2447 | int ret_val = YAFFS_OK; | ||
| 2448 | int i; | ||
| 2449 | int is_checkpt_block; | ||
| 2450 | int matching_chunk; | ||
| 2451 | int max_copies; | ||
| 2452 | |||
| 2453 | int chunks_before = yaffs_get_erased_chunks(dev); | ||
| 2454 | int chunks_after; | ||
| 2455 | |||
| 2456 | struct yaffs_ext_tags tags; | ||
| 2457 | |||
| 2458 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, block); | ||
| 2459 | |||
| 2460 | struct yaffs_obj *object; | ||
| 2461 | |||
| 2462 | is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT); | ||
| 2463 | |||
| 2464 | yaffs_trace(YAFFS_TRACE_TRACING, | ||
| 2465 | "Collecting block %d, in use %d, shrink %d, whole_block %d", | ||
| 2466 | block, bi->pages_in_use, bi->has_shrink_hdr, | ||
| 2467 | whole_block); | ||
| 2468 | |||
| 2469 | /*yaffs_verify_free_chunks(dev); */ | ||
| 2470 | |||
| 2471 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL) | ||
| 2472 | bi->block_state = YAFFS_BLOCK_STATE_COLLECTING; | ||
| 2473 | |||
| 2474 | bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */ | ||
| 2475 | |||
| 2476 | dev->gc_disable = 1; | ||
| 2477 | |||
| 2478 | if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) { | ||
| 2479 | yaffs_trace(YAFFS_TRACE_TRACING, | ||
| 2480 | "Collecting block %d that has no chunks in use", | ||
| 2481 | block); | ||
| 2482 | yaffs_block_became_dirty(dev, block); | ||
| 2483 | } else { | ||
| 2484 | |||
| 2485 | u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 2486 | |||
| 2487 | yaffs_verify_blk(dev, bi, block); | ||
| 2488 | |||
| 2489 | max_copies = (whole_block) ? dev->param.chunks_per_block : 5; | ||
| 2490 | old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk; | ||
| 2491 | |||
| 2492 | for ( /* init already done */ ; | ||
| 2493 | ret_val == YAFFS_OK && | ||
| 2494 | dev->gc_chunk < dev->param.chunks_per_block && | ||
| 2495 | (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) && | ||
| 2496 | max_copies > 0; dev->gc_chunk++, old_chunk++) { | ||
| 2497 | if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) { | ||
| 2498 | |||
| 2499 | /* This page is in use and might need to be copied off */ | ||
| 2500 | |||
| 2501 | max_copies--; | ||
| 2502 | |||
| 2503 | mark_flash = 1; | ||
| 2504 | |||
| 2505 | yaffs_init_tags(&tags); | ||
| 2506 | |||
| 2507 | yaffs_rd_chunk_tags_nand(dev, old_chunk, | ||
| 2508 | buffer, &tags); | ||
| 2509 | |||
| 2510 | object = yaffs_find_by_number(dev, tags.obj_id); | ||
| 2511 | |||
| 2512 | yaffs_trace(YAFFS_TRACE_GC_DETAIL, | ||
| 2513 | "Collecting chunk in block %d, %d %d %d ", | ||
| 2514 | dev->gc_chunk, tags.obj_id, | ||
| 2515 | tags.chunk_id, tags.n_bytes); | ||
| 2516 | |||
| 2517 | if (object && !yaffs_skip_verification(dev)) { | ||
| 2518 | if (tags.chunk_id == 0) | ||
| 2519 | matching_chunk = | ||
| 2520 | object->hdr_chunk; | ||
| 2521 | else if (object->soft_del) | ||
| 2522 | matching_chunk = old_chunk; /* Defeat the test */ | ||
| 2523 | else | ||
| 2524 | matching_chunk = | ||
| 2525 | yaffs_find_chunk_in_file | ||
| 2526 | (object, tags.chunk_id, | ||
| 2527 | NULL); | ||
| 2528 | |||
| 2529 | if (old_chunk != matching_chunk) | ||
| 2530 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 2531 | "gc: page in gc mismatch: %d %d %d %d", | ||
| 2532 | old_chunk, | ||
| 2533 | matching_chunk, | ||
| 2534 | tags.obj_id, | ||
| 2535 | tags.chunk_id); | ||
| 2536 | |||
| 2537 | } | ||
| 2538 | |||
| 2539 | if (!object) { | ||
| 2540 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 2541 | "page %d in gc has no object: %d %d %d ", | ||
| 2542 | old_chunk, | ||
| 2543 | tags.obj_id, tags.chunk_id, | ||
| 2544 | tags.n_bytes); | ||
| 2545 | } | ||
| 2546 | |||
| 2547 | if (object && | ||
| 2548 | object->deleted && | ||
| 2549 | object->soft_del && tags.chunk_id != 0) { | ||
| 2550 | /* Data chunk in a soft deleted file, throw it away | ||
| 2551 | * It's a soft deleted data chunk, | ||
| 2552 | * No need to copy this, just forget about it and | ||
| 2553 | * fix up the object. | ||
| 2554 | */ | ||
| 2555 | |||
| 2556 | /* Free chunks already includes softdeleted chunks. | ||
| 2557 | * How ever this chunk is going to soon be really deleted | ||
| 2558 | * which will increment free chunks. | ||
| 2559 | * We have to decrement free chunks so this works out properly. | ||
| 2560 | */ | ||
| 2561 | dev->n_free_chunks--; | ||
| 2562 | bi->soft_del_pages--; | ||
| 2563 | |||
| 2564 | object->n_data_chunks--; | ||
| 2565 | |||
| 2566 | if (object->n_data_chunks <= 0) { | ||
| 2567 | /* remeber to clean up the object */ | ||
| 2568 | dev->gc_cleanup_list[dev-> | ||
| 2569 | n_clean_ups] | ||
| 2570 | = tags.obj_id; | ||
| 2571 | dev->n_clean_ups++; | ||
| 2572 | } | ||
| 2573 | mark_flash = 0; | ||
| 2574 | } else if (0) { | ||
| 2575 | /* Todo object && object->deleted && object->n_data_chunks == 0 */ | ||
| 2576 | /* Deleted object header with no data chunks. | ||
| 2577 | * Can be discarded and the file deleted. | ||
| 2578 | */ | ||
| 2579 | object->hdr_chunk = 0; | ||
| 2580 | yaffs_free_tnode(object->my_dev, | ||
| 2581 | object-> | ||
| 2582 | variant.file_variant. | ||
| 2583 | top); | ||
| 2584 | object->variant.file_variant.top = NULL; | ||
| 2585 | yaffs_generic_obj_del(object); | ||
| 2586 | |||
| 2587 | } else if (object) { | ||
| 2588 | /* It's either a data chunk in a live file or | ||
| 2589 | * an ObjectHeader, so we're interested in it. | ||
| 2590 | * NB Need to keep the ObjectHeaders of deleted files | ||
| 2591 | * until the whole file has been deleted off | ||
| 2592 | */ | ||
| 2593 | tags.serial_number++; | ||
| 2594 | |||
| 2595 | dev->n_gc_copies++; | ||
| 2596 | |||
| 2597 | if (tags.chunk_id == 0) { | ||
| 2598 | /* It is an object Id, | ||
| 2599 | * We need to nuke the shrinkheader flags first | ||
| 2600 | * Also need to clean up shadowing. | ||
| 2601 | * We no longer want the shrink_header flag since its work is done | ||
| 2602 | * and if it is left in place it will mess up scanning. | ||
| 2603 | */ | ||
| 2604 | |||
| 2605 | struct yaffs_obj_hdr *oh; | ||
| 2606 | oh = (struct yaffs_obj_hdr *) | ||
| 2607 | buffer; | ||
| 2608 | |||
| 2609 | oh->is_shrink = 0; | ||
| 2610 | tags.extra_is_shrink = 0; | ||
| 2611 | |||
| 2612 | oh->shadows_obj = 0; | ||
| 2613 | oh->inband_shadowed_obj_id = 0; | ||
| 2614 | tags.extra_shadows = 0; | ||
| 2615 | |||
| 2616 | /* Update file size */ | ||
| 2617 | if (object->variant_type == | ||
| 2618 | YAFFS_OBJECT_TYPE_FILE) { | ||
| 2619 | oh->file_size = | ||
| 2620 | object->variant. | ||
| 2621 | file_variant. | ||
| 2622 | file_size; | ||
| 2623 | tags.extra_length = | ||
| 2624 | oh->file_size; | ||
| 2625 | } | ||
| 2626 | |||
| 2627 | yaffs_verify_oh(object, oh, | ||
| 2628 | &tags, 1); | ||
| 2629 | new_chunk = | ||
| 2630 | yaffs_write_new_chunk(dev, | ||
| 2631 | (u8 *) | ||
| 2632 | oh, | ||
| 2633 | &tags, | ||
| 2634 | 1); | ||
| 2635 | } else { | ||
| 2636 | new_chunk = | ||
| 2637 | yaffs_write_new_chunk(dev, | ||
| 2638 | buffer, | ||
| 2639 | &tags, | ||
| 2640 | 1); | ||
| 2641 | } | ||
| 2642 | |||
| 2643 | if (new_chunk < 0) { | ||
| 2644 | ret_val = YAFFS_FAIL; | ||
| 2645 | } else { | ||
| 2646 | |||
| 2647 | /* Ok, now fix up the Tnodes etc. */ | ||
| 2648 | |||
| 2649 | if (tags.chunk_id == 0) { | ||
| 2650 | /* It's a header */ | ||
| 2651 | object->hdr_chunk = | ||
| 2652 | new_chunk; | ||
| 2653 | object->serial = | ||
| 2654 | tags.serial_number; | ||
| 2655 | } else { | ||
| 2656 | /* It's a data chunk */ | ||
| 2657 | int ok; | ||
| 2658 | ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0); | ||
| 2659 | } | ||
| 2660 | } | ||
| 2661 | } | ||
| 2662 | |||
| 2663 | if (ret_val == YAFFS_OK) | ||
| 2664 | yaffs_chunk_del(dev, old_chunk, | ||
| 2665 | mark_flash, __LINE__); | ||
| 2666 | |||
| 2667 | } | ||
| 2668 | } | ||
| 2669 | |||
| 2670 | yaffs_release_temp_buffer(dev, buffer, __LINE__); | ||
| 2671 | |||
| 2672 | } | ||
| 2673 | |||
| 2674 | yaffs_verify_collected_blk(dev, bi, block); | ||
| 2675 | |||
| 2676 | if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { | ||
| 2677 | /* | ||
| 2678 | * The gc did not complete. Set block state back to FULL | ||
| 2679 | * because checkpointing does not restore gc. | ||
| 2680 | */ | ||
| 2681 | bi->block_state = YAFFS_BLOCK_STATE_FULL; | ||
| 2682 | } else { | ||
| 2683 | /* The gc completed. */ | ||
| 2684 | /* Do any required cleanups */ | ||
| 2685 | for (i = 0; i < dev->n_clean_ups; i++) { | ||
| 2686 | /* Time to delete the file too */ | ||
| 2687 | object = | ||
| 2688 | yaffs_find_by_number(dev, dev->gc_cleanup_list[i]); | ||
| 2689 | if (object) { | ||
| 2690 | yaffs_free_tnode(dev, | ||
| 2691 | object->variant. | ||
| 2692 | file_variant.top); | ||
| 2693 | object->variant.file_variant.top = NULL; | ||
| 2694 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 2695 | "yaffs: About to finally delete object %d", | ||
| 2696 | object->obj_id); | ||
| 2697 | yaffs_generic_obj_del(object); | ||
| 2698 | object->my_dev->n_deleted_files--; | ||
| 2699 | } | ||
| 2700 | |||
| 2701 | } | ||
| 2702 | |||
| 2703 | chunks_after = yaffs_get_erased_chunks(dev); | ||
| 2704 | if (chunks_before >= chunks_after) | ||
| 2705 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 2706 | "gc did not increase free chunks before %d after %d", | ||
| 2707 | chunks_before, chunks_after); | ||
| 2708 | dev->gc_block = 0; | ||
| 2709 | dev->gc_chunk = 0; | ||
| 2710 | dev->n_clean_ups = 0; | ||
| 2711 | } | ||
| 2712 | |||
| 2713 | dev->gc_disable = 0; | ||
| 2714 | |||
| 2715 | return ret_val; | ||
| 2716 | } | ||
| 2717 | |||
| 2718 | /* | ||
| 2719 | * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough) | ||
| 2720 | * for garbage collection. | ||
| 2721 | */ | ||
| 2722 | |||
| 2723 | static unsigned yaffs_find_gc_block(struct yaffs_dev *dev, | ||
| 2724 | int aggressive, int background) | ||
| 2725 | { | ||
| 2726 | int i; | ||
| 2727 | int iterations; | ||
| 2728 | unsigned selected = 0; | ||
| 2729 | int prioritised = 0; | ||
| 2730 | int prioritised_exist = 0; | ||
| 2731 | struct yaffs_block_info *bi; | ||
| 2732 | int threshold; | ||
| 2733 | |||
| 2734 | /* First let's see if we need to grab a prioritised block */ | ||
| 2735 | if (dev->has_pending_prioritised_gc && !aggressive) { | ||
| 2736 | dev->gc_dirtiest = 0; | ||
| 2737 | bi = dev->block_info; | ||
| 2738 | for (i = dev->internal_start_block; | ||
| 2739 | i <= dev->internal_end_block && !selected; i++) { | ||
| 2740 | |||
| 2741 | if (bi->gc_prioritise) { | ||
| 2742 | prioritised_exist = 1; | ||
| 2743 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL && | ||
| 2744 | yaffs_block_ok_for_gc(dev, bi)) { | ||
| 2745 | selected = i; | ||
| 2746 | prioritised = 1; | ||
| 2747 | } | ||
| 2748 | } | ||
| 2749 | bi++; | ||
| 2750 | } | ||
| 2751 | |||
| 2752 | /* | ||
| 2753 | * If there is a prioritised block and none was selected then | ||
| 2754 | * this happened because there is at least one old dirty block gumming | ||
| 2755 | * up the works. Let's gc the oldest dirty block. | ||
| 2756 | */ | ||
| 2757 | |||
| 2758 | if (prioritised_exist && | ||
| 2759 | !selected && dev->oldest_dirty_block > 0) | ||
| 2760 | selected = dev->oldest_dirty_block; | ||
| 2761 | |||
| 2762 | if (!prioritised_exist) /* None found, so we can clear this */ | ||
| 2763 | dev->has_pending_prioritised_gc = 0; | ||
| 2764 | } | ||
| 2765 | |||
| 2766 | /* If we're doing aggressive GC then we are happy to take a less-dirty block, and | ||
| 2767 | * search harder. | ||
| 2768 | * else (we're doing a leasurely gc), then we only bother to do this if the | ||
| 2769 | * block has only a few pages in use. | ||
| 2770 | */ | ||
| 2771 | |||
| 2772 | if (!selected) { | ||
| 2773 | int pages_used; | ||
| 2774 | int n_blocks = | ||
| 2775 | dev->internal_end_block - dev->internal_start_block + 1; | ||
| 2776 | if (aggressive) { | ||
| 2777 | threshold = dev->param.chunks_per_block; | ||
| 2778 | iterations = n_blocks; | ||
| 2779 | } else { | ||
| 2780 | int max_threshold; | ||
| 2781 | |||
| 2782 | if (background) | ||
| 2783 | max_threshold = dev->param.chunks_per_block / 2; | ||
| 2784 | else | ||
| 2785 | max_threshold = dev->param.chunks_per_block / 8; | ||
| 2786 | |||
| 2787 | if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD) | ||
| 2788 | max_threshold = YAFFS_GC_PASSIVE_THRESHOLD; | ||
| 2789 | |||
| 2790 | threshold = background ? (dev->gc_not_done + 2) * 2 : 0; | ||
| 2791 | if (threshold < YAFFS_GC_PASSIVE_THRESHOLD) | ||
| 2792 | threshold = YAFFS_GC_PASSIVE_THRESHOLD; | ||
| 2793 | if (threshold > max_threshold) | ||
| 2794 | threshold = max_threshold; | ||
| 2795 | |||
| 2796 | iterations = n_blocks / 16 + 1; | ||
| 2797 | if (iterations > 100) | ||
| 2798 | iterations = 100; | ||
| 2799 | } | ||
| 2800 | |||
| 2801 | for (i = 0; | ||
| 2802 | i < iterations && | ||
| 2803 | (dev->gc_dirtiest < 1 || | ||
| 2804 | dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) { | ||
| 2805 | dev->gc_block_finder++; | ||
| 2806 | if (dev->gc_block_finder < dev->internal_start_block || | ||
| 2807 | dev->gc_block_finder > dev->internal_end_block) | ||
| 2808 | dev->gc_block_finder = | ||
| 2809 | dev->internal_start_block; | ||
| 2810 | |||
| 2811 | bi = yaffs_get_block_info(dev, dev->gc_block_finder); | ||
| 2812 | |||
| 2813 | pages_used = bi->pages_in_use - bi->soft_del_pages; | ||
| 2814 | |||
| 2815 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL && | ||
| 2816 | pages_used < dev->param.chunks_per_block && | ||
| 2817 | (dev->gc_dirtiest < 1 | ||
| 2818 | || pages_used < dev->gc_pages_in_use) | ||
| 2819 | && yaffs_block_ok_for_gc(dev, bi)) { | ||
| 2820 | dev->gc_dirtiest = dev->gc_block_finder; | ||
| 2821 | dev->gc_pages_in_use = pages_used; | ||
| 2822 | } | ||
| 2823 | } | ||
| 2824 | |||
| 2825 | if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold) | ||
| 2826 | selected = dev->gc_dirtiest; | ||
| 2827 | } | ||
| 2828 | |||
| 2829 | /* | ||
| 2830 | * If nothing has been selected for a while, try selecting the oldest dirty | ||
| 2831 | * because that's gumming up the works. | ||
| 2832 | */ | ||
| 2833 | |||
| 2834 | if (!selected && dev->param.is_yaffs2 && | ||
| 2835 | dev->gc_not_done >= (background ? 10 : 20)) { | ||
| 2836 | yaffs2_find_oldest_dirty_seq(dev); | ||
| 2837 | if (dev->oldest_dirty_block > 0) { | ||
| 2838 | selected = dev->oldest_dirty_block; | ||
| 2839 | dev->gc_dirtiest = selected; | ||
| 2840 | dev->oldest_dirty_gc_count++; | ||
| 2841 | bi = yaffs_get_block_info(dev, selected); | ||
| 2842 | dev->gc_pages_in_use = | ||
| 2843 | bi->pages_in_use - bi->soft_del_pages; | ||
| 2844 | } else { | ||
| 2845 | dev->gc_not_done = 0; | ||
| 2846 | } | ||
| 2847 | } | ||
| 2848 | |||
| 2849 | if (selected) { | ||
| 2850 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 2851 | "GC Selected block %d with %d free, prioritised:%d", | ||
| 2852 | selected, | ||
| 2853 | dev->param.chunks_per_block - dev->gc_pages_in_use, | ||
| 2854 | prioritised); | ||
| 2855 | |||
| 2856 | dev->n_gc_blocks++; | ||
| 2857 | if (background) | ||
| 2858 | dev->bg_gcs++; | ||
| 2859 | |||
| 2860 | dev->gc_dirtiest = 0; | ||
| 2861 | dev->gc_pages_in_use = 0; | ||
| 2862 | dev->gc_not_done = 0; | ||
| 2863 | if (dev->refresh_skip > 0) | ||
| 2864 | dev->refresh_skip--; | ||
| 2865 | } else { | ||
| 2866 | dev->gc_not_done++; | ||
| 2867 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 2868 | "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s", | ||
| 2869 | dev->gc_block_finder, dev->gc_not_done, threshold, | ||
| 2870 | dev->gc_dirtiest, dev->gc_pages_in_use, | ||
| 2871 | dev->oldest_dirty_block, background ? " bg" : ""); | ||
| 2872 | } | ||
| 2873 | |||
| 2874 | return selected; | ||
| 2875 | } | ||
| 2876 | |||
| 2877 | /* New garbage collector | ||
| 2878 | * If we're very low on erased blocks then we do aggressive garbage collection | ||
| 2879 | * otherwise we do "leasurely" garbage collection. | ||
| 2880 | * Aggressive gc looks further (whole array) and will accept less dirty blocks. | ||
| 2881 | * Passive gc only inspects smaller areas and will only accept more dirty blocks. | ||
| 2882 | * | ||
| 2883 | * The idea is to help clear out space in a more spread-out manner. | ||
| 2884 | * Dunno if it really does anything useful. | ||
| 2885 | */ | ||
| 2886 | static int yaffs_check_gc(struct yaffs_dev *dev, int background) | ||
| 2887 | { | ||
| 2888 | int aggressive = 0; | ||
| 2889 | int gc_ok = YAFFS_OK; | ||
| 2890 | int max_tries = 0; | ||
| 2891 | int min_erased; | ||
| 2892 | int erased_chunks; | ||
| 2893 | int checkpt_block_adjust; | ||
| 2894 | |||
| 2895 | if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0) | ||
| 2896 | return YAFFS_OK; | ||
| 2897 | |||
| 2898 | if (dev->gc_disable) { | ||
| 2899 | /* Bail out so we don't get recursive gc */ | ||
| 2900 | return YAFFS_OK; | ||
| 2901 | } | ||
| 2902 | |||
| 2903 | /* This loop should pass the first time. | ||
| 2904 | * We'll only see looping here if the collection does not increase space. | ||
| 2905 | */ | ||
| 2906 | |||
| 2907 | do { | ||
| 2908 | max_tries++; | ||
| 2909 | |||
| 2910 | checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev); | ||
| 2911 | |||
| 2912 | min_erased = | ||
| 2913 | dev->param.n_reserved_blocks + checkpt_block_adjust + 1; | ||
| 2914 | erased_chunks = | ||
| 2915 | dev->n_erased_blocks * dev->param.chunks_per_block; | ||
| 2916 | |||
| 2917 | /* If we need a block soon then do aggressive gc. */ | ||
| 2918 | if (dev->n_erased_blocks < min_erased) | ||
| 2919 | aggressive = 1; | ||
| 2920 | else { | ||
| 2921 | if (!background | ||
| 2922 | && erased_chunks > (dev->n_free_chunks / 4)) | ||
| 2923 | break; | ||
| 2924 | |||
| 2925 | if (dev->gc_skip > 20) | ||
| 2926 | dev->gc_skip = 20; | ||
| 2927 | if (erased_chunks < dev->n_free_chunks / 2 || | ||
| 2928 | dev->gc_skip < 1 || background) | ||
| 2929 | aggressive = 0; | ||
| 2930 | else { | ||
| 2931 | dev->gc_skip--; | ||
| 2932 | break; | ||
| 2933 | } | ||
| 2934 | } | ||
| 2935 | |||
| 2936 | dev->gc_skip = 5; | ||
| 2937 | |||
| 2938 | /* If we don't already have a block being gc'd then see if we should start another */ | ||
| 2939 | |||
| 2940 | if (dev->gc_block < 1 && !aggressive) { | ||
| 2941 | dev->gc_block = yaffs2_find_refresh_block(dev); | ||
| 2942 | dev->gc_chunk = 0; | ||
| 2943 | dev->n_clean_ups = 0; | ||
| 2944 | } | ||
| 2945 | if (dev->gc_block < 1) { | ||
| 2946 | dev->gc_block = | ||
| 2947 | yaffs_find_gc_block(dev, aggressive, background); | ||
| 2948 | dev->gc_chunk = 0; | ||
| 2949 | dev->n_clean_ups = 0; | ||
| 2950 | } | ||
| 2951 | |||
| 2952 | if (dev->gc_block > 0) { | ||
| 2953 | dev->all_gcs++; | ||
| 2954 | if (!aggressive) | ||
| 2955 | dev->passive_gc_count++; | ||
| 2956 | |||
| 2957 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 2958 | "yaffs: GC n_erased_blocks %d aggressive %d", | ||
| 2959 | dev->n_erased_blocks, aggressive); | ||
| 2960 | |||
| 2961 | gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive); | ||
| 2962 | } | ||
| 2963 | |||
| 2964 | if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) | ||
| 2965 | && dev->gc_block > 0) { | ||
| 2966 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 2967 | "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d", | ||
| 2968 | dev->n_erased_blocks, max_tries, | ||
| 2969 | dev->gc_block); | ||
| 2970 | } | ||
| 2971 | } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) && | ||
| 2972 | (dev->gc_block > 0) && (max_tries < 2)); | ||
| 2973 | |||
| 2974 | return aggressive ? gc_ok : YAFFS_OK; | ||
| 2975 | } | ||
| 2976 | |||
| 2977 | /* | ||
| 2978 | * yaffs_bg_gc() | ||
| 2979 | * Garbage collects. Intended to be called from a background thread. | ||
| 2980 | * Returns non-zero if at least half the free chunks are erased. | ||
| 2981 | */ | ||
| 2982 | int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency) | ||
| 2983 | { | ||
| 2984 | int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block; | ||
| 2985 | |||
| 2986 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency); | ||
| 2987 | |||
| 2988 | yaffs_check_gc(dev, 1); | ||
| 2989 | return erased_chunks > dev->n_free_chunks / 2; | ||
| 2990 | } | ||
| 2991 | |||
| 2992 | /*-------------------- Data file manipulation -----------------*/ | ||
| 2993 | |||
| 2994 | static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer) | ||
| 2995 | { | ||
| 2996 | int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL); | ||
| 2997 | |||
| 2998 | if (nand_chunk >= 0) | ||
| 2999 | return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk, | ||
| 3000 | buffer, NULL); | ||
| 3001 | else { | ||
| 3002 | yaffs_trace(YAFFS_TRACE_NANDACCESS, | ||
| 3003 | "Chunk %d not found zero instead", | ||
| 3004 | nand_chunk); | ||
| 3005 | /* get sane (zero) data if you read a hole */ | ||
| 3006 | memset(buffer, 0, in->my_dev->data_bytes_per_chunk); | ||
| 3007 | return 0; | ||
| 3008 | } | ||
| 3009 | |||
| 3010 | } | ||
| 3011 | |||
| 3012 | void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, | ||
| 3013 | int lyn) | ||
| 3014 | { | ||
| 3015 | int block; | ||
| 3016 | int page; | ||
| 3017 | struct yaffs_ext_tags tags; | ||
| 3018 | struct yaffs_block_info *bi; | ||
| 3019 | |||
| 3020 | if (chunk_id <= 0) | ||
| 3021 | return; | ||
| 3022 | |||
| 3023 | dev->n_deletions++; | ||
| 3024 | block = chunk_id / dev->param.chunks_per_block; | ||
| 3025 | page = chunk_id % dev->param.chunks_per_block; | ||
| 3026 | |||
| 3027 | if (!yaffs_check_chunk_bit(dev, block, page)) | ||
| 3028 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 3029 | "Deleting invalid chunk %d", chunk_id); | ||
| 3030 | |||
| 3031 | bi = yaffs_get_block_info(dev, block); | ||
| 3032 | |||
| 3033 | yaffs2_update_oldest_dirty_seq(dev, block, bi); | ||
| 3034 | |||
| 3035 | yaffs_trace(YAFFS_TRACE_DELETION, | ||
| 3036 | "line %d delete of chunk %d", | ||
| 3037 | lyn, chunk_id); | ||
| 3038 | |||
| 3039 | if (!dev->param.is_yaffs2 && mark_flash && | ||
| 3040 | bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) { | ||
| 3041 | |||
| 3042 | yaffs_init_tags(&tags); | ||
| 3043 | |||
| 3044 | tags.is_deleted = 1; | ||
| 3045 | |||
| 3046 | yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags); | ||
| 3047 | yaffs_handle_chunk_update(dev, chunk_id, &tags); | ||
| 3048 | } else { | ||
| 3049 | dev->n_unmarked_deletions++; | ||
| 3050 | } | ||
| 3051 | |||
| 3052 | /* Pull out of the management area. | ||
| 3053 | * If the whole block became dirty, this will kick off an erasure. | ||
| 3054 | */ | ||
| 3055 | if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING || | ||
| 3056 | bi->block_state == YAFFS_BLOCK_STATE_FULL || | ||
| 3057 | bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || | ||
| 3058 | bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { | ||
| 3059 | dev->n_free_chunks++; | ||
| 3060 | |||
| 3061 | yaffs_clear_chunk_bit(dev, block, page); | ||
| 3062 | |||
| 3063 | bi->pages_in_use--; | ||
| 3064 | |||
| 3065 | if (bi->pages_in_use == 0 && | ||
| 3066 | !bi->has_shrink_hdr && | ||
| 3067 | bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING && | ||
| 3068 | bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) { | ||
| 3069 | yaffs_block_became_dirty(dev, block); | ||
| 3070 | } | ||
| 3071 | |||
| 3072 | } | ||
| 3073 | |||
| 3074 | } | ||
| 3075 | |||
| 3076 | static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, | ||
| 3077 | const u8 * buffer, int n_bytes, int use_reserve) | ||
| 3078 | { | ||
| 3079 | /* Find old chunk Need to do this to get serial number | ||
| 3080 | * Write new one and patch into tree. | ||
| 3081 | * Invalidate old tags. | ||
| 3082 | */ | ||
| 3083 | |||
| 3084 | int prev_chunk_id; | ||
| 3085 | struct yaffs_ext_tags prev_tags; | ||
| 3086 | |||
| 3087 | int new_chunk_id; | ||
| 3088 | struct yaffs_ext_tags new_tags; | ||
| 3089 | |||
| 3090 | struct yaffs_dev *dev = in->my_dev; | ||
| 3091 | |||
| 3092 | yaffs_check_gc(dev, 0); | ||
| 3093 | |||
| 3094 | /* Get the previous chunk at this location in the file if it exists. | ||
| 3095 | * If it does not exist then put a zero into the tree. This creates | ||
| 3096 | * the tnode now, rather than later when it is harder to clean up. | ||
| 3097 | */ | ||
| 3098 | prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags); | ||
| 3099 | if (prev_chunk_id < 1 && | ||
| 3100 | !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0)) | ||
| 3101 | return 0; | ||
| 3102 | |||
| 3103 | /* Set up new tags */ | ||
| 3104 | yaffs_init_tags(&new_tags); | ||
| 3105 | |||
| 3106 | new_tags.chunk_id = inode_chunk; | ||
| 3107 | new_tags.obj_id = in->obj_id; | ||
| 3108 | new_tags.serial_number = | ||
| 3109 | (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1; | ||
| 3110 | new_tags.n_bytes = n_bytes; | ||
| 3111 | |||
| 3112 | if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) { | ||
| 3113 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 3114 | "Writing %d bytes to chunk!!!!!!!!!", | ||
| 3115 | n_bytes); | ||
| 3116 | YBUG(); | ||
| 3117 | } | ||
| 3118 | |||
| 3119 | new_chunk_id = | ||
| 3120 | yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve); | ||
| 3121 | |||
| 3122 | if (new_chunk_id > 0) { | ||
| 3123 | yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0); | ||
| 3124 | |||
| 3125 | if (prev_chunk_id > 0) | ||
| 3126 | yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__); | ||
| 3127 | |||
| 3128 | yaffs_verify_file_sane(in); | ||
| 3129 | } | ||
| 3130 | return new_chunk_id; | ||
| 3131 | |||
| 3132 | } | ||
| 3133 | |||
| 3134 | |||
| 3135 | |||
| 3136 | static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set, | ||
| 3137 | const YCHAR * name, const void *value, int size, | ||
| 3138 | int flags) | ||
| 3139 | { | ||
| 3140 | struct yaffs_xattr_mod xmod; | ||
| 3141 | |||
| 3142 | int result; | ||
| 3143 | |||
| 3144 | xmod.set = set; | ||
| 3145 | xmod.name = name; | ||
| 3146 | xmod.data = value; | ||
| 3147 | xmod.size = size; | ||
| 3148 | xmod.flags = flags; | ||
| 3149 | xmod.result = -ENOSPC; | ||
| 3150 | |||
| 3151 | result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod); | ||
| 3152 | |||
| 3153 | if (result > 0) | ||
| 3154 | return xmod.result; | ||
| 3155 | else | ||
| 3156 | return -ENOSPC; | ||
| 3157 | } | ||
| 3158 | |||
| 3159 | static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, | ||
| 3160 | struct yaffs_xattr_mod *xmod) | ||
| 3161 | { | ||
| 3162 | int retval = 0; | ||
| 3163 | int x_offs = sizeof(struct yaffs_obj_hdr); | ||
| 3164 | struct yaffs_dev *dev = obj->my_dev; | ||
| 3165 | int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr); | ||
| 3166 | |||
| 3167 | char *x_buffer = buffer + x_offs; | ||
| 3168 | |||
| 3169 | if (xmod->set) | ||
| 3170 | retval = | ||
| 3171 | nval_set(x_buffer, x_size, xmod->name, xmod->data, | ||
| 3172 | xmod->size, xmod->flags); | ||
| 3173 | else | ||
| 3174 | retval = nval_del(x_buffer, x_size, xmod->name); | ||
| 3175 | |||
| 3176 | obj->has_xattr = nval_hasvalues(x_buffer, x_size); | ||
| 3177 | obj->xattr_known = 1; | ||
| 3178 | |||
| 3179 | xmod->result = retval; | ||
| 3180 | |||
| 3181 | return retval; | ||
| 3182 | } | ||
| 3183 | |||
| 3184 | static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name, | ||
| 3185 | void *value, int size) | ||
| 3186 | { | ||
| 3187 | char *buffer = NULL; | ||
| 3188 | int result; | ||
| 3189 | struct yaffs_ext_tags tags; | ||
| 3190 | struct yaffs_dev *dev = obj->my_dev; | ||
| 3191 | int x_offs = sizeof(struct yaffs_obj_hdr); | ||
| 3192 | int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr); | ||
| 3193 | |||
| 3194 | char *x_buffer; | ||
| 3195 | |||
| 3196 | int retval = 0; | ||
| 3197 | |||
| 3198 | if (obj->hdr_chunk < 1) | ||
| 3199 | return -ENODATA; | ||
| 3200 | |||
| 3201 | /* If we know that the object has no xattribs then don't do all the | ||
| 3202 | * reading and parsing. | ||
| 3203 | */ | ||
| 3204 | if (obj->xattr_known && !obj->has_xattr) { | ||
| 3205 | if (name) | ||
| 3206 | return -ENODATA; | ||
| 3207 | else | ||
| 3208 | return 0; | ||
| 3209 | } | ||
| 3210 | |||
| 3211 | buffer = (char *)yaffs_get_temp_buffer(dev, __LINE__); | ||
| 3212 | if (!buffer) | ||
| 3213 | return -ENOMEM; | ||
| 3214 | |||
| 3215 | result = | ||
| 3216 | yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags); | ||
| 3217 | |||
| 3218 | if (result != YAFFS_OK) | ||
| 3219 | retval = -ENOENT; | ||
| 3220 | else { | ||
| 3221 | x_buffer = buffer + x_offs; | ||
| 3222 | |||
| 3223 | if (!obj->xattr_known) { | ||
| 3224 | obj->has_xattr = nval_hasvalues(x_buffer, x_size); | ||
| 3225 | obj->xattr_known = 1; | ||
| 3226 | } | ||
| 3227 | |||
| 3228 | if (name) | ||
| 3229 | retval = nval_get(x_buffer, x_size, name, value, size); | ||
| 3230 | else | ||
| 3231 | retval = nval_list(x_buffer, x_size, value, size); | ||
| 3232 | } | ||
| 3233 | yaffs_release_temp_buffer(dev, (u8 *) buffer, __LINE__); | ||
| 3234 | return retval; | ||
| 3235 | } | ||
| 3236 | |||
| 3237 | int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name, | ||
| 3238 | const void *value, int size, int flags) | ||
| 3239 | { | ||
| 3240 | return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags); | ||
| 3241 | } | ||
| 3242 | |||
| 3243 | int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name) | ||
| 3244 | { | ||
| 3245 | return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0); | ||
| 3246 | } | ||
| 3247 | |||
| 3248 | int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value, | ||
| 3249 | int size) | ||
| 3250 | { | ||
| 3251 | return yaffs_do_xattrib_fetch(obj, name, value, size); | ||
| 3252 | } | ||
| 3253 | |||
| 3254 | int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size) | ||
| 3255 | { | ||
| 3256 | return yaffs_do_xattrib_fetch(obj, NULL, buffer, size); | ||
| 3257 | } | ||
| 3258 | |||
| 3259 | static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) | ||
| 3260 | { | ||
| 3261 | u8 *chunk_data; | ||
| 3262 | struct yaffs_obj_hdr *oh; | ||
| 3263 | struct yaffs_dev *dev; | ||
| 3264 | struct yaffs_ext_tags tags; | ||
| 3265 | int result; | ||
| 3266 | int alloc_failed = 0; | ||
| 3267 | |||
| 3268 | if (!in) | ||
| 3269 | return; | ||
| 3270 | |||
| 3271 | dev = in->my_dev; | ||
| 3272 | |||
| 3273 | if (in->lazy_loaded && in->hdr_chunk > 0) { | ||
| 3274 | in->lazy_loaded = 0; | ||
| 3275 | chunk_data = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 3276 | |||
| 3277 | result = | ||
| 3278 | yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data, | ||
| 3279 | &tags); | ||
| 3280 | oh = (struct yaffs_obj_hdr *)chunk_data; | ||
| 3281 | |||
| 3282 | in->yst_mode = oh->yst_mode; | ||
| 3283 | yaffs_load_attribs(in, oh); | ||
| 3284 | yaffs_set_obj_name_from_oh(in, oh); | ||
| 3285 | |||
| 3286 | if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { | ||
| 3287 | in->variant.symlink_variant.alias = | ||
| 3288 | yaffs_clone_str(oh->alias); | ||
| 3289 | if (!in->variant.symlink_variant.alias) | ||
| 3290 | alloc_failed = 1; /* Not returned to caller */ | ||
| 3291 | } | ||
| 3292 | |||
| 3293 | yaffs_release_temp_buffer(dev, chunk_data, __LINE__); | ||
| 3294 | } | ||
| 3295 | } | ||
| 3296 | |||
| 3297 | static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name, | ||
| 3298 | const YCHAR * oh_name, int buff_size) | ||
| 3299 | { | ||
| 3300 | #ifdef CONFIG_YAFFS_AUTO_UNICODE | ||
| 3301 | if (dev->param.auto_unicode) { | ||
| 3302 | if (*oh_name) { | ||
| 3303 | /* It is an ASCII name, do an ASCII to | ||
| 3304 | * unicode conversion */ | ||
| 3305 | const char *ascii_oh_name = (const char *)oh_name; | ||
| 3306 | int n = buff_size - 1; | ||
| 3307 | while (n > 0 && *ascii_oh_name) { | ||
| 3308 | *name = *ascii_oh_name; | ||
| 3309 | name++; | ||
| 3310 | ascii_oh_name++; | ||
| 3311 | n--; | ||
| 3312 | } | ||
| 3313 | } else { | ||
| 3314 | strncpy(name, oh_name + 1, buff_size - 1); | ||
| 3315 | } | ||
| 3316 | } else { | ||
| 3317 | #else | ||
| 3318 | { | ||
| 3319 | #endif | ||
| 3320 | strncpy(name, oh_name, buff_size - 1); | ||
| 3321 | } | ||
| 3322 | } | ||
| 3323 | |||
| 3324 | static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name, | ||
| 3325 | const YCHAR * name) | ||
| 3326 | { | ||
| 3327 | #ifdef CONFIG_YAFFS_AUTO_UNICODE | ||
| 3328 | |||
| 3329 | int is_ascii; | ||
| 3330 | YCHAR *w; | ||
| 3331 | |||
| 3332 | if (dev->param.auto_unicode) { | ||
| 3333 | |||
| 3334 | is_ascii = 1; | ||
| 3335 | w = name; | ||
| 3336 | |||
| 3337 | /* Figure out if the name will fit in ascii character set */ | ||
| 3338 | while (is_ascii && *w) { | ||
| 3339 | if ((*w) & 0xff00) | ||
| 3340 | is_ascii = 0; | ||
| 3341 | w++; | ||
| 3342 | } | ||
| 3343 | |||
| 3344 | if (is_ascii) { | ||
| 3345 | /* It is an ASCII name, so do a unicode to ascii conversion */ | ||
| 3346 | char *ascii_oh_name = (char *)oh_name; | ||
| 3347 | int n = YAFFS_MAX_NAME_LENGTH - 1; | ||
| 3348 | while (n > 0 && *name) { | ||
| 3349 | *ascii_oh_name = *name; | ||
| 3350 | name++; | ||
| 3351 | ascii_oh_name++; | ||
| 3352 | n--; | ||
| 3353 | } | ||
| 3354 | } else { | ||
| 3355 | /* It is a unicode name, so save starting at the second YCHAR */ | ||
| 3356 | *oh_name = 0; | ||
| 3357 | strncpy(oh_name + 1, name, | ||
| 3358 | YAFFS_MAX_NAME_LENGTH - 2); | ||
| 3359 | } | ||
| 3360 | } else { | ||
| 3361 | #else | ||
| 3362 | { | ||
| 3363 | #endif | ||
| 3364 | strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1); | ||
| 3365 | } | ||
| 3366 | |||
| 3367 | } | ||
| 3368 | |||
| 3369 | /* UpdateObjectHeader updates the header on NAND for an object. | ||
| 3370 | * If name is not NULL, then that new name is used. | ||
| 3371 | */ | ||
| 3372 | int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force, | ||
| 3373 | int is_shrink, int shadows, struct yaffs_xattr_mod *xmod) | ||
| 3374 | { | ||
| 3375 | |||
| 3376 | struct yaffs_block_info *bi; | ||
| 3377 | |||
| 3378 | struct yaffs_dev *dev = in->my_dev; | ||
| 3379 | |||
| 3380 | int prev_chunk_id; | ||
| 3381 | int ret_val = 0; | ||
| 3382 | int result = 0; | ||
| 3383 | |||
| 3384 | int new_chunk_id; | ||
| 3385 | struct yaffs_ext_tags new_tags; | ||
| 3386 | struct yaffs_ext_tags old_tags; | ||
| 3387 | const YCHAR *alias = NULL; | ||
| 3388 | |||
| 3389 | u8 *buffer = NULL; | ||
| 3390 | YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1]; | ||
| 3391 | |||
| 3392 | struct yaffs_obj_hdr *oh = NULL; | ||
| 3393 | |||
| 3394 | strcpy(old_name, _Y("silly old name")); | ||
| 3395 | |||
| 3396 | if (!in->fake || in == dev->root_dir || | ||
| 3397 | force || xmod) { | ||
| 3398 | |||
| 3399 | yaffs_check_gc(dev, 0); | ||
| 3400 | yaffs_check_obj_details_loaded(in); | ||
| 3401 | |||
| 3402 | buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__); | ||
| 3403 | oh = (struct yaffs_obj_hdr *)buffer; | ||
| 3404 | |||
| 3405 | prev_chunk_id = in->hdr_chunk; | ||
| 3406 | |||
| 3407 | if (prev_chunk_id > 0) { | ||
| 3408 | result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id, | ||
| 3409 | buffer, &old_tags); | ||
| 3410 | |||
| 3411 | yaffs_verify_oh(in, oh, &old_tags, 0); | ||
| 3412 | |||
| 3413 | memcpy(old_name, oh->name, sizeof(oh->name)); | ||
| 3414 | memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr)); | ||
| 3415 | } else { | ||
| 3416 | memset(buffer, 0xFF, dev->data_bytes_per_chunk); | ||
| 3417 | } | ||
| 3418 | |||
| 3419 | oh->type = in->variant_type; | ||
| 3420 | oh->yst_mode = in->yst_mode; | ||
| 3421 | oh->shadows_obj = oh->inband_shadowed_obj_id = shadows; | ||
| 3422 | |||
| 3423 | yaffs_load_attribs_oh(oh, in); | ||
| 3424 | |||
| 3425 | if (in->parent) | ||
| 3426 | oh->parent_obj_id = in->parent->obj_id; | ||
| 3427 | else | ||
| 3428 | oh->parent_obj_id = 0; | ||
| 3429 | |||
| 3430 | if (name && *name) { | ||
| 3431 | memset(oh->name, 0, sizeof(oh->name)); | ||
| 3432 | yaffs_load_oh_from_name(dev, oh->name, name); | ||
| 3433 | } else if (prev_chunk_id > 0) { | ||
| 3434 | memcpy(oh->name, old_name, sizeof(oh->name)); | ||
| 3435 | } else { | ||
| 3436 | memset(oh->name, 0, sizeof(oh->name)); | ||
| 3437 | } | ||
| 3438 | |||
| 3439 | oh->is_shrink = is_shrink; | ||
| 3440 | |||
| 3441 | switch (in->variant_type) { | ||
| 3442 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 3443 | /* Should not happen */ | ||
| 3444 | break; | ||
| 3445 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 3446 | oh->file_size = | ||
| 3447 | (oh->parent_obj_id == YAFFS_OBJECTID_DELETED | ||
| 3448 | || oh->parent_obj_id == | ||
| 3449 | YAFFS_OBJECTID_UNLINKED) ? 0 : in-> | ||
| 3450 | variant.file_variant.file_size; | ||
| 3451 | break; | ||
| 3452 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 3453 | oh->equiv_id = in->variant.hardlink_variant.equiv_id; | ||
| 3454 | break; | ||
| 3455 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 3456 | /* Do nothing */ | ||
| 3457 | break; | ||
| 3458 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 3459 | /* Do nothing */ | ||
| 3460 | break; | ||
| 3461 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 3462 | alias = in->variant.symlink_variant.alias; | ||
| 3463 | if (!alias) | ||
| 3464 | alias = _Y("no alias"); | ||
| 3465 | strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH); | ||
| 3466 | oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0; | ||
| 3467 | break; | ||
| 3468 | } | ||
| 3469 | |||
| 3470 | /* process any xattrib modifications */ | ||
| 3471 | if (xmod) | ||
| 3472 | yaffs_apply_xattrib_mod(in, (char *)buffer, xmod); | ||
| 3473 | |||
| 3474 | /* Tags */ | ||
| 3475 | yaffs_init_tags(&new_tags); | ||
| 3476 | in->serial++; | ||
| 3477 | new_tags.chunk_id = 0; | ||
| 3478 | new_tags.obj_id = in->obj_id; | ||
| 3479 | new_tags.serial_number = in->serial; | ||
| 3480 | |||
| 3481 | /* Add extra info for file header */ | ||
| 3482 | |||
| 3483 | new_tags.extra_available = 1; | ||
| 3484 | new_tags.extra_parent_id = oh->parent_obj_id; | ||
| 3485 | new_tags.extra_length = oh->file_size; | ||
| 3486 | new_tags.extra_is_shrink = oh->is_shrink; | ||
| 3487 | new_tags.extra_equiv_id = oh->equiv_id; | ||
| 3488 | new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0; | ||
| 3489 | new_tags.extra_obj_type = in->variant_type; | ||
| 3490 | |||
| 3491 | yaffs_verify_oh(in, oh, &new_tags, 1); | ||
| 3492 | |||
| 3493 | /* Create new chunk in NAND */ | ||
| 3494 | new_chunk_id = | ||
| 3495 | yaffs_write_new_chunk(dev, buffer, &new_tags, | ||
| 3496 | (prev_chunk_id > 0) ? 1 : 0); | ||
| 3497 | |||
| 3498 | if (new_chunk_id >= 0) { | ||
| 3499 | |||
| 3500 | in->hdr_chunk = new_chunk_id; | ||
| 3501 | |||
| 3502 | if (prev_chunk_id > 0) { | ||
| 3503 | yaffs_chunk_del(dev, prev_chunk_id, 1, | ||
| 3504 | __LINE__); | ||
| 3505 | } | ||
| 3506 | |||
| 3507 | if (!yaffs_obj_cache_dirty(in)) | ||
| 3508 | in->dirty = 0; | ||
| 3509 | |||
| 3510 | /* If this was a shrink, then mark the block that the chunk lives on */ | ||
| 3511 | if (is_shrink) { | ||
| 3512 | bi = yaffs_get_block_info(in->my_dev, | ||
| 3513 | new_chunk_id / | ||
| 3514 | in->my_dev->param. | ||
| 3515 | chunks_per_block); | ||
| 3516 | bi->has_shrink_hdr = 1; | ||
| 3517 | } | ||
| 3518 | |||
| 3519 | } | ||
| 3520 | |||
| 3521 | ret_val = new_chunk_id; | ||
| 3522 | |||
| 3523 | } | ||
| 3524 | |||
| 3525 | if (buffer) | ||
| 3526 | yaffs_release_temp_buffer(dev, buffer, __LINE__); | ||
| 3527 | |||
| 3528 | return ret_val; | ||
| 3529 | } | ||
| 3530 | |||
| 3531 | /*--------------------- File read/write ------------------------ | ||
| 3532 | * Read and write have very similar structures. | ||
| 3533 | * In general the read/write has three parts to it | ||
| 3534 | * An incomplete chunk to start with (if the read/write is not chunk-aligned) | ||
| 3535 | * Some complete chunks | ||
| 3536 | * An incomplete chunk to end off with | ||
| 3537 | * | ||
| 3538 | * Curve-balls: the first chunk might also be the last chunk. | ||
| 3539 | */ | ||
| 3540 | |||
| 3541 | int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes) | ||
| 3542 | { | ||
| 3543 | |||
| 3544 | int chunk; | ||
| 3545 | u32 start; | ||
| 3546 | int n_copy; | ||
| 3547 | int n = n_bytes; | ||
| 3548 | int n_done = 0; | ||
| 3549 | struct yaffs_cache *cache; | ||
| 3550 | |||
| 3551 | struct yaffs_dev *dev; | ||
| 3552 | |||
| 3553 | dev = in->my_dev; | ||
| 3554 | |||
| 3555 | while (n > 0) { | ||
| 3556 | /* chunk = offset / dev->data_bytes_per_chunk + 1; */ | ||
| 3557 | /* start = offset % dev->data_bytes_per_chunk; */ | ||
| 3558 | yaffs_addr_to_chunk(dev, offset, &chunk, &start); | ||
| 3559 | chunk++; | ||
| 3560 | |||
| 3561 | /* OK now check for the curveball where the start and end are in | ||
| 3562 | * the same chunk. | ||
| 3563 | */ | ||
| 3564 | if ((start + n) < dev->data_bytes_per_chunk) | ||
| 3565 | n_copy = n; | ||
| 3566 | else | ||
| 3567 | n_copy = dev->data_bytes_per_chunk - start; | ||
| 3568 | |||
| 3569 | cache = yaffs_find_chunk_cache(in, chunk); | ||
| 3570 | |||
| 3571 | /* If the chunk is already in the cache or it is less than a whole chunk | ||
| 3572 | * or we're using inband tags then use the cache (if there is caching) | ||
| 3573 | * else bypass the cache. | ||
| 3574 | */ | ||
| 3575 | if (cache || n_copy != dev->data_bytes_per_chunk | ||
| 3576 | || dev->param.inband_tags) { | ||
| 3577 | if (dev->param.n_caches > 0) { | ||
| 3578 | |||
| 3579 | /* If we can't find the data in the cache, then load it up. */ | ||
| 3580 | |||
| 3581 | if (!cache) { | ||
| 3582 | cache = | ||
| 3583 | yaffs_grab_chunk_cache(in->my_dev); | ||
| 3584 | cache->object = in; | ||
| 3585 | cache->chunk_id = chunk; | ||
| 3586 | cache->dirty = 0; | ||
| 3587 | cache->locked = 0; | ||
| 3588 | yaffs_rd_data_obj(in, chunk, | ||
| 3589 | cache->data); | ||
| 3590 | cache->n_bytes = 0; | ||
| 3591 | } | ||
| 3592 | |||
| 3593 | yaffs_use_cache(dev, cache, 0); | ||
| 3594 | |||
| 3595 | cache->locked = 1; | ||
| 3596 | |||
| 3597 | memcpy(buffer, &cache->data[start], n_copy); | ||
| 3598 | |||
| 3599 | cache->locked = 0; | ||
| 3600 | } else { | ||
| 3601 | /* Read into the local buffer then copy.. */ | ||
| 3602 | |||
| 3603 | u8 *local_buffer = | ||
| 3604 | yaffs_get_temp_buffer(dev, __LINE__); | ||
| 3605 | yaffs_rd_data_obj(in, chunk, local_buffer); | ||
| 3606 | |||
| 3607 | memcpy(buffer, &local_buffer[start], n_copy); | ||
| 3608 | |||
| 3609 | yaffs_release_temp_buffer(dev, local_buffer, | ||
| 3610 | __LINE__); | ||
| 3611 | } | ||
| 3612 | |||
| 3613 | } else { | ||
| 3614 | |||
| 3615 | /* A full chunk. Read directly into the supplied buffer. */ | ||
| 3616 | yaffs_rd_data_obj(in, chunk, buffer); | ||
| 3617 | |||
| 3618 | } | ||
| 3619 | |||
| 3620 | n -= n_copy; | ||
| 3621 | offset += n_copy; | ||
| 3622 | buffer += n_copy; | ||
| 3623 | n_done += n_copy; | ||
| 3624 | |||
| 3625 | } | ||
| 3626 | |||
| 3627 | return n_done; | ||
| 3628 | } | ||
| 3629 | |||
| 3630 | int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset, | ||
| 3631 | int n_bytes, int write_trhrough) | ||
| 3632 | { | ||
| 3633 | |||
| 3634 | int chunk; | ||
| 3635 | u32 start; | ||
| 3636 | int n_copy; | ||
| 3637 | int n = n_bytes; | ||
| 3638 | int n_done = 0; | ||
| 3639 | int n_writeback; | ||
| 3640 | int start_write = offset; | ||
| 3641 | int chunk_written = 0; | ||
| 3642 | u32 n_bytes_read; | ||
| 3643 | u32 chunk_start; | ||
| 3644 | |||
| 3645 | struct yaffs_dev *dev; | ||
| 3646 | |||
| 3647 | dev = in->my_dev; | ||
| 3648 | |||
| 3649 | while (n > 0 && chunk_written >= 0) { | ||
| 3650 | yaffs_addr_to_chunk(dev, offset, &chunk, &start); | ||
| 3651 | |||
| 3652 | if (chunk * dev->data_bytes_per_chunk + start != offset || | ||
| 3653 | start >= dev->data_bytes_per_chunk) { | ||
| 3654 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 3655 | "AddrToChunk of offset %d gives chunk %d start %d", | ||
| 3656 | (int)offset, chunk, start); | ||
| 3657 | } | ||
| 3658 | chunk++; /* File pos to chunk in file offset */ | ||
| 3659 | |||
| 3660 | /* OK now check for the curveball where the start and end are in | ||
| 3661 | * the same chunk. | ||
| 3662 | */ | ||
| 3663 | |||
| 3664 | if ((start + n) < dev->data_bytes_per_chunk) { | ||
| 3665 | n_copy = n; | ||
| 3666 | |||
| 3667 | /* Now folks, to calculate how many bytes to write back.... | ||
| 3668 | * If we're overwriting and not writing to then end of file then | ||
| 3669 | * we need to write back as much as was there before. | ||
| 3670 | */ | ||
| 3671 | |||
| 3672 | chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk); | ||
| 3673 | |||
| 3674 | if (chunk_start > in->variant.file_variant.file_size) | ||
| 3675 | n_bytes_read = 0; /* Past end of file */ | ||
| 3676 | else | ||
| 3677 | n_bytes_read = | ||
| 3678 | in->variant.file_variant.file_size - | ||
| 3679 | chunk_start; | ||
| 3680 | |||
| 3681 | if (n_bytes_read > dev->data_bytes_per_chunk) | ||
| 3682 | n_bytes_read = dev->data_bytes_per_chunk; | ||
| 3683 | |||
| 3684 | n_writeback = | ||
| 3685 | (n_bytes_read > | ||
| 3686 | (start + n)) ? n_bytes_read : (start + n); | ||
| 3687 | |||
| 3688 | if (n_writeback < 0 | ||
| 3689 | || n_writeback > dev->data_bytes_per_chunk) | ||
| 3690 | YBUG(); | ||
| 3691 | |||
| 3692 | } else { | ||
| 3693 | n_copy = dev->data_bytes_per_chunk - start; | ||
| 3694 | n_writeback = dev->data_bytes_per_chunk; | ||
| 3695 | } | ||
| 3696 | |||
| 3697 | if (n_copy != dev->data_bytes_per_chunk | ||
| 3698 | || dev->param.inband_tags) { | ||
| 3699 | /* An incomplete start or end chunk (or maybe both start and end chunk), | ||
| 3700 | * or we're using inband tags, so we want to use the cache buffers. | ||
| 3701 | */ | ||
| 3702 | if (dev->param.n_caches > 0) { | ||
| 3703 | struct yaffs_cache *cache; | ||
| 3704 | /* If we can't find the data in the cache, then load the cache */ | ||
| 3705 | cache = yaffs_find_chunk_cache(in, chunk); | ||
| 3706 | |||
| 3707 | if (!cache | ||
| 3708 | && yaffs_check_alloc_available(dev, 1)) { | ||
| 3709 | cache = yaffs_grab_chunk_cache(dev); | ||
| 3710 | cache->object = in; | ||
| 3711 | cache->chunk_id = chunk; | ||
| 3712 | cache->dirty = 0; | ||
| 3713 | cache->locked = 0; | ||
| 3714 | yaffs_rd_data_obj(in, chunk, | ||
| 3715 | cache->data); | ||
| 3716 | } else if (cache && | ||
| 3717 | !cache->dirty && | ||
| 3718 | !yaffs_check_alloc_available(dev, | ||
| 3719 | 1)) { | ||
| 3720 | /* Drop the cache if it was a read cache item and | ||
| 3721 | * no space check has been made for it. | ||
| 3722 | */ | ||
| 3723 | cache = NULL; | ||
| 3724 | } | ||
| 3725 | |||
| 3726 | if (cache) { | ||
| 3727 | yaffs_use_cache(dev, cache, 1); | ||
| 3728 | cache->locked = 1; | ||
| 3729 | |||
| 3730 | memcpy(&cache->data[start], buffer, | ||
| 3731 | n_copy); | ||
| 3732 | |||
| 3733 | cache->locked = 0; | ||
| 3734 | cache->n_bytes = n_writeback; | ||
| 3735 | |||
| 3736 | if (write_trhrough) { | ||
| 3737 | chunk_written = | ||
| 3738 | yaffs_wr_data_obj | ||
| 3739 | (cache->object, | ||
| 3740 | cache->chunk_id, | ||
| 3741 | cache->data, | ||
| 3742 | cache->n_bytes, 1); | ||
| 3743 | cache->dirty = 0; | ||
| 3744 | } | ||
| 3745 | |||
| 3746 | } else { | ||
| 3747 | chunk_written = -1; /* fail the write */ | ||
| 3748 | } | ||
| 3749 | } else { | ||
| 3750 | /* An incomplete start or end chunk (or maybe both start and end chunk) | ||
| 3751 | * Read into the local buffer then copy, then copy over and write back. | ||
| 3752 | */ | ||
| 3753 | |||
| 3754 | u8 *local_buffer = | ||
| 3755 | yaffs_get_temp_buffer(dev, __LINE__); | ||
| 3756 | |||
| 3757 | yaffs_rd_data_obj(in, chunk, local_buffer); | ||
| 3758 | |||
| 3759 | memcpy(&local_buffer[start], buffer, n_copy); | ||
| 3760 | |||
| 3761 | chunk_written = | ||
| 3762 | yaffs_wr_data_obj(in, chunk, | ||
| 3763 | local_buffer, | ||
| 3764 | n_writeback, 0); | ||
| 3765 | |||
| 3766 | yaffs_release_temp_buffer(dev, local_buffer, | ||
| 3767 | __LINE__); | ||
| 3768 | |||
| 3769 | } | ||
| 3770 | |||
| 3771 | } else { | ||
| 3772 | /* A full chunk. Write directly from the supplied buffer. */ | ||
| 3773 | |||
| 3774 | chunk_written = | ||
| 3775 | yaffs_wr_data_obj(in, chunk, buffer, | ||
| 3776 | dev->data_bytes_per_chunk, 0); | ||
| 3777 | |||
| 3778 | /* Since we've overwritten the cached data, we better invalidate it. */ | ||
| 3779 | yaffs_invalidate_chunk_cache(in, chunk); | ||
| 3780 | } | ||
| 3781 | |||
| 3782 | if (chunk_written >= 0) { | ||
| 3783 | n -= n_copy; | ||
| 3784 | offset += n_copy; | ||
| 3785 | buffer += n_copy; | ||
| 3786 | n_done += n_copy; | ||
| 3787 | } | ||
| 3788 | |||
| 3789 | } | ||
| 3790 | |||
| 3791 | /* Update file object */ | ||
| 3792 | |||
| 3793 | if ((start_write + n_done) > in->variant.file_variant.file_size) | ||
| 3794 | in->variant.file_variant.file_size = (start_write + n_done); | ||
| 3795 | |||
| 3796 | in->dirty = 1; | ||
| 3797 | |||
| 3798 | return n_done; | ||
| 3799 | } | ||
| 3800 | |||
| 3801 | int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset, | ||
| 3802 | int n_bytes, int write_trhrough) | ||
| 3803 | { | ||
| 3804 | yaffs2_handle_hole(in, offset); | ||
| 3805 | return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough); | ||
| 3806 | } | ||
| 3807 | |||
| 3808 | /* ---------------------- File resizing stuff ------------------ */ | ||
| 3809 | |||
| 3810 | static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size) | ||
| 3811 | { | ||
| 3812 | |||
| 3813 | struct yaffs_dev *dev = in->my_dev; | ||
| 3814 | int old_size = in->variant.file_variant.file_size; | ||
| 3815 | |||
| 3816 | int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk; | ||
| 3817 | |||
| 3818 | int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) / | ||
| 3819 | dev->data_bytes_per_chunk; | ||
| 3820 | int i; | ||
| 3821 | int chunk_id; | ||
| 3822 | |||
| 3823 | /* Delete backwards so that we don't end up with holes if | ||
| 3824 | * power is lost part-way through the operation. | ||
| 3825 | */ | ||
| 3826 | for (i = last_del; i >= start_del; i--) { | ||
| 3827 | /* NB this could be optimised somewhat, | ||
| 3828 | * eg. could retrieve the tags and write them without | ||
| 3829 | * using yaffs_chunk_del | ||
| 3830 | */ | ||
| 3831 | |||
| 3832 | chunk_id = yaffs_find_del_file_chunk(in, i, NULL); | ||
| 3833 | if (chunk_id > 0) { | ||
| 3834 | if (chunk_id < | ||
| 3835 | (dev->internal_start_block * | ||
| 3836 | dev->param.chunks_per_block) | ||
| 3837 | || chunk_id >= | ||
| 3838 | ((dev->internal_end_block + | ||
| 3839 | 1) * dev->param.chunks_per_block)) { | ||
| 3840 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 3841 | "Found daft chunk_id %d for %d", | ||
| 3842 | chunk_id, i); | ||
| 3843 | } else { | ||
| 3844 | in->n_data_chunks--; | ||
| 3845 | yaffs_chunk_del(dev, chunk_id, 1, __LINE__); | ||
| 3846 | } | ||
| 3847 | } | ||
| 3848 | } | ||
| 3849 | |||
| 3850 | } | ||
| 3851 | |||
| 3852 | void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size) | ||
| 3853 | { | ||
| 3854 | int new_full; | ||
| 3855 | u32 new_partial; | ||
| 3856 | struct yaffs_dev *dev = obj->my_dev; | ||
| 3857 | |||
| 3858 | yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial); | ||
| 3859 | |||
| 3860 | yaffs_prune_chunks(obj, new_size); | ||
| 3861 | |||
| 3862 | if (new_partial != 0) { | ||
| 3863 | int last_chunk = 1 + new_full; | ||
| 3864 | u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 3865 | |||
| 3866 | /* Rewrite the last chunk with its new size and zero pad */ | ||
| 3867 | yaffs_rd_data_obj(obj, last_chunk, local_buffer); | ||
| 3868 | memset(local_buffer + new_partial, 0, | ||
| 3869 | dev->data_bytes_per_chunk - new_partial); | ||
| 3870 | |||
| 3871 | yaffs_wr_data_obj(obj, last_chunk, local_buffer, | ||
| 3872 | new_partial, 1); | ||
| 3873 | |||
| 3874 | yaffs_release_temp_buffer(dev, local_buffer, __LINE__); | ||
| 3875 | } | ||
| 3876 | |||
| 3877 | obj->variant.file_variant.file_size = new_size; | ||
| 3878 | |||
| 3879 | yaffs_prune_tree(dev, &obj->variant.file_variant); | ||
| 3880 | } | ||
| 3881 | |||
| 3882 | int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size) | ||
| 3883 | { | ||
| 3884 | struct yaffs_dev *dev = in->my_dev; | ||
| 3885 | int old_size = in->variant.file_variant.file_size; | ||
| 3886 | |||
| 3887 | yaffs_flush_file_cache(in); | ||
| 3888 | yaffs_invalidate_whole_cache(in); | ||
| 3889 | |||
| 3890 | yaffs_check_gc(dev, 0); | ||
| 3891 | |||
| 3892 | if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) | ||
| 3893 | return YAFFS_FAIL; | ||
| 3894 | |||
| 3895 | if (new_size == old_size) | ||
| 3896 | return YAFFS_OK; | ||
| 3897 | |||
| 3898 | if (new_size > old_size) { | ||
| 3899 | yaffs2_handle_hole(in, new_size); | ||
| 3900 | in->variant.file_variant.file_size = new_size; | ||
| 3901 | } else { | ||
| 3902 | /* new_size < old_size */ | ||
| 3903 | yaffs_resize_file_down(in, new_size); | ||
| 3904 | } | ||
| 3905 | |||
| 3906 | /* Write a new object header to reflect the resize. | ||
| 3907 | * show we've shrunk the file, if need be | ||
| 3908 | * Do this only if the file is not in the deleted directories | ||
| 3909 | * and is not shadowed. | ||
| 3910 | */ | ||
| 3911 | if (in->parent && | ||
| 3912 | !in->is_shadowed && | ||
| 3913 | in->parent->obj_id != YAFFS_OBJECTID_UNLINKED && | ||
| 3914 | in->parent->obj_id != YAFFS_OBJECTID_DELETED) | ||
| 3915 | yaffs_update_oh(in, NULL, 0, 0, 0, NULL); | ||
| 3916 | |||
| 3917 | return YAFFS_OK; | ||
| 3918 | } | ||
| 3919 | |||
| 3920 | int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync) | ||
| 3921 | { | ||
| 3922 | int ret_val; | ||
| 3923 | if (in->dirty) { | ||
| 3924 | yaffs_flush_file_cache(in); | ||
| 3925 | if (data_sync) /* Only sync data */ | ||
| 3926 | ret_val = YAFFS_OK; | ||
| 3927 | else { | ||
| 3928 | if (update_time) | ||
| 3929 | yaffs_load_current_time(in, 0, 0); | ||
| 3930 | |||
| 3931 | ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= | ||
| 3932 | 0) ? YAFFS_OK : YAFFS_FAIL; | ||
| 3933 | } | ||
| 3934 | } else { | ||
| 3935 | ret_val = YAFFS_OK; | ||
| 3936 | } | ||
| 3937 | |||
| 3938 | return ret_val; | ||
| 3939 | |||
| 3940 | } | ||
| 3941 | |||
| 3942 | |||
| 3943 | /* yaffs_del_file deletes the whole file data | ||
| 3944 | * and the inode associated with the file. | ||
| 3945 | * It does not delete the links associated with the file. | ||
| 3946 | */ | ||
| 3947 | static int yaffs_unlink_file_if_needed(struct yaffs_obj *in) | ||
| 3948 | { | ||
| 3949 | |||
| 3950 | int ret_val; | ||
| 3951 | int del_now = 0; | ||
| 3952 | struct yaffs_dev *dev = in->my_dev; | ||
| 3953 | |||
| 3954 | if (!in->my_inode) | ||
| 3955 | del_now = 1; | ||
| 3956 | |||
| 3957 | if (del_now) { | ||
| 3958 | ret_val = | ||
| 3959 | yaffs_change_obj_name(in, in->my_dev->del_dir, | ||
| 3960 | _Y("deleted"), 0, 0); | ||
| 3961 | yaffs_trace(YAFFS_TRACE_TRACING, | ||
| 3962 | "yaffs: immediate deletion of file %d", | ||
| 3963 | in->obj_id); | ||
| 3964 | in->deleted = 1; | ||
| 3965 | in->my_dev->n_deleted_files++; | ||
| 3966 | if (dev->param.disable_soft_del || dev->param.is_yaffs2) | ||
| 3967 | yaffs_resize_file(in, 0); | ||
| 3968 | yaffs_soft_del_file(in); | ||
| 3969 | } else { | ||
| 3970 | ret_val = | ||
| 3971 | yaffs_change_obj_name(in, in->my_dev->unlinked_dir, | ||
| 3972 | _Y("unlinked"), 0, 0); | ||
| 3973 | } | ||
| 3974 | |||
| 3975 | return ret_val; | ||
| 3976 | } | ||
| 3977 | |||
| 3978 | int yaffs_del_file(struct yaffs_obj *in) | ||
| 3979 | { | ||
| 3980 | int ret_val = YAFFS_OK; | ||
| 3981 | int deleted; /* Need to cache value on stack if in is freed */ | ||
| 3982 | struct yaffs_dev *dev = in->my_dev; | ||
| 3983 | |||
| 3984 | if (dev->param.disable_soft_del || dev->param.is_yaffs2) | ||
| 3985 | yaffs_resize_file(in, 0); | ||
| 3986 | |||
| 3987 | if (in->n_data_chunks > 0) { | ||
| 3988 | /* Use soft deletion if there is data in the file. | ||
| 3989 | * That won't be the case if it has been resized to zero. | ||
| 3990 | */ | ||
| 3991 | if (!in->unlinked) | ||
| 3992 | ret_val = yaffs_unlink_file_if_needed(in); | ||
| 3993 | |||
| 3994 | deleted = in->deleted; | ||
| 3995 | |||
| 3996 | if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) { | ||
| 3997 | in->deleted = 1; | ||
| 3998 | deleted = 1; | ||
| 3999 | in->my_dev->n_deleted_files++; | ||
| 4000 | yaffs_soft_del_file(in); | ||
| 4001 | } | ||
| 4002 | return deleted ? YAFFS_OK : YAFFS_FAIL; | ||
| 4003 | } else { | ||
| 4004 | /* The file has no data chunks so we toss it immediately */ | ||
| 4005 | yaffs_free_tnode(in->my_dev, in->variant.file_variant.top); | ||
| 4006 | in->variant.file_variant.top = NULL; | ||
| 4007 | yaffs_generic_obj_del(in); | ||
| 4008 | |||
| 4009 | return YAFFS_OK; | ||
| 4010 | } | ||
| 4011 | } | ||
| 4012 | |||
| 4013 | int yaffs_is_non_empty_dir(struct yaffs_obj *obj) | ||
| 4014 | { | ||
| 4015 | return (obj && | ||
| 4016 | obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) && | ||
| 4017 | !(list_empty(&obj->variant.dir_variant.children)); | ||
| 4018 | } | ||
| 4019 | |||
| 4020 | static int yaffs_del_dir(struct yaffs_obj *obj) | ||
| 4021 | { | ||
| 4022 | /* First check that the directory is empty. */ | ||
| 4023 | if (yaffs_is_non_empty_dir(obj)) | ||
| 4024 | return YAFFS_FAIL; | ||
| 4025 | |||
| 4026 | return yaffs_generic_obj_del(obj); | ||
| 4027 | } | ||
| 4028 | |||
| 4029 | static int yaffs_del_symlink(struct yaffs_obj *in) | ||
| 4030 | { | ||
| 4031 | if (in->variant.symlink_variant.alias) | ||
| 4032 | kfree(in->variant.symlink_variant.alias); | ||
| 4033 | in->variant.symlink_variant.alias = NULL; | ||
| 4034 | |||
| 4035 | return yaffs_generic_obj_del(in); | ||
| 4036 | } | ||
| 4037 | |||
| 4038 | static int yaffs_del_link(struct yaffs_obj *in) | ||
| 4039 | { | ||
| 4040 | /* remove this hardlink from the list assocaited with the equivalent | ||
| 4041 | * object | ||
| 4042 | */ | ||
| 4043 | list_del_init(&in->hard_links); | ||
| 4044 | return yaffs_generic_obj_del(in); | ||
| 4045 | } | ||
| 4046 | |||
| 4047 | int yaffs_del_obj(struct yaffs_obj *obj) | ||
| 4048 | { | ||
| 4049 | int ret_val = -1; | ||
| 4050 | switch (obj->variant_type) { | ||
| 4051 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 4052 | ret_val = yaffs_del_file(obj); | ||
| 4053 | break; | ||
| 4054 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 4055 | if (!list_empty(&obj->variant.dir_variant.dirty)) { | ||
| 4056 | yaffs_trace(YAFFS_TRACE_BACKGROUND, | ||
| 4057 | "Remove object %d from dirty directories", | ||
| 4058 | obj->obj_id); | ||
| 4059 | list_del_init(&obj->variant.dir_variant.dirty); | ||
| 4060 | } | ||
| 4061 | return yaffs_del_dir(obj); | ||
| 4062 | break; | ||
| 4063 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 4064 | ret_val = yaffs_del_symlink(obj); | ||
| 4065 | break; | ||
| 4066 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 4067 | ret_val = yaffs_del_link(obj); | ||
| 4068 | break; | ||
| 4069 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 4070 | ret_val = yaffs_generic_obj_del(obj); | ||
| 4071 | break; | ||
| 4072 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 4073 | ret_val = 0; | ||
| 4074 | break; /* should not happen. */ | ||
| 4075 | } | ||
| 4076 | |||
| 4077 | return ret_val; | ||
| 4078 | } | ||
| 4079 | |||
| 4080 | static int yaffs_unlink_worker(struct yaffs_obj *obj) | ||
| 4081 | { | ||
| 4082 | |||
| 4083 | int del_now = 0; | ||
| 4084 | |||
| 4085 | if (!obj->my_inode) | ||
| 4086 | del_now = 1; | ||
| 4087 | |||
| 4088 | if (obj) | ||
| 4089 | yaffs_update_parent(obj->parent); | ||
| 4090 | |||
| 4091 | if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { | ||
| 4092 | return yaffs_del_link(obj); | ||
| 4093 | } else if (!list_empty(&obj->hard_links)) { | ||
| 4094 | /* Curve ball: We're unlinking an object that has a hardlink. | ||
| 4095 | * | ||
| 4096 | * This problem arises because we are not strictly following | ||
| 4097 | * The Linux link/inode model. | ||
| 4098 | * | ||
| 4099 | * We can't really delete the object. | ||
| 4100 | * Instead, we do the following: | ||
| 4101 | * - Select a hardlink. | ||
| 4102 | * - Unhook it from the hard links | ||
| 4103 | * - Move it from its parent directory (so that the rename can work) | ||
| 4104 | * - Rename the object to the hardlink's name. | ||
| 4105 | * - Delete the hardlink | ||
| 4106 | */ | ||
| 4107 | |||
| 4108 | struct yaffs_obj *hl; | ||
| 4109 | struct yaffs_obj *parent; | ||
| 4110 | int ret_val; | ||
| 4111 | YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; | ||
| 4112 | |||
| 4113 | hl = list_entry(obj->hard_links.next, struct yaffs_obj, | ||
| 4114 | hard_links); | ||
| 4115 | |||
| 4116 | yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1); | ||
| 4117 | parent = hl->parent; | ||
| 4118 | |||
| 4119 | list_del_init(&hl->hard_links); | ||
| 4120 | |||
| 4121 | yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl); | ||
| 4122 | |||
| 4123 | ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0); | ||
| 4124 | |||
| 4125 | if (ret_val == YAFFS_OK) | ||
| 4126 | ret_val = yaffs_generic_obj_del(hl); | ||
| 4127 | |||
| 4128 | return ret_val; | ||
| 4129 | |||
| 4130 | } else if (del_now) { | ||
| 4131 | switch (obj->variant_type) { | ||
| 4132 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 4133 | return yaffs_del_file(obj); | ||
| 4134 | break; | ||
| 4135 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 4136 | list_del_init(&obj->variant.dir_variant.dirty); | ||
| 4137 | return yaffs_del_dir(obj); | ||
| 4138 | break; | ||
| 4139 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 4140 | return yaffs_del_symlink(obj); | ||
| 4141 | break; | ||
| 4142 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 4143 | return yaffs_generic_obj_del(obj); | ||
| 4144 | break; | ||
| 4145 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 4146 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 4147 | default: | ||
| 4148 | return YAFFS_FAIL; | ||
| 4149 | } | ||
| 4150 | } else if (yaffs_is_non_empty_dir(obj)) { | ||
| 4151 | return YAFFS_FAIL; | ||
| 4152 | } else { | ||
| 4153 | return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir, | ||
| 4154 | _Y("unlinked"), 0, 0); | ||
| 4155 | } | ||
| 4156 | } | ||
| 4157 | |||
| 4158 | static int yaffs_unlink_obj(struct yaffs_obj *obj) | ||
| 4159 | { | ||
| 4160 | |||
| 4161 | if (obj && obj->unlink_allowed) | ||
| 4162 | return yaffs_unlink_worker(obj); | ||
| 4163 | |||
| 4164 | return YAFFS_FAIL; | ||
| 4165 | |||
| 4166 | } | ||
| 4167 | |||
| 4168 | int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name) | ||
| 4169 | { | ||
| 4170 | struct yaffs_obj *obj; | ||
| 4171 | |||
| 4172 | obj = yaffs_find_by_name(dir, name); | ||
| 4173 | return yaffs_unlink_obj(obj); | ||
| 4174 | } | ||
| 4175 | |||
| 4176 | /* Note: | ||
| 4177 | * If old_name is NULL then we take old_dir as the object to be renamed. | ||
| 4178 | */ | ||
| 4179 | int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name, | ||
| 4180 | struct yaffs_obj *new_dir, const YCHAR * new_name) | ||
| 4181 | { | ||
| 4182 | struct yaffs_obj *obj = NULL; | ||
| 4183 | struct yaffs_obj *existing_target = NULL; | ||
| 4184 | int force = 0; | ||
| 4185 | int result; | ||
| 4186 | struct yaffs_dev *dev; | ||
| 4187 | |||
| 4188 | if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) | ||
| 4189 | YBUG(); | ||
| 4190 | if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) | ||
| 4191 | YBUG(); | ||
| 4192 | |||
| 4193 | dev = old_dir->my_dev; | ||
| 4194 | |||
| 4195 | #ifdef CONFIG_YAFFS_CASE_INSENSITIVE | ||
| 4196 | /* Special case for case insemsitive systems. | ||
| 4197 | * While look-up is case insensitive, the name isn't. | ||
| 4198 | * Therefore we might want to change x.txt to X.txt | ||
| 4199 | */ | ||
| 4200 | if (old_dir == new_dir && | ||
| 4201 | old_name && new_name && | ||
| 4202 | strcmp(old_name, new_name) == 0) | ||
| 4203 | force = 1; | ||
| 4204 | #endif | ||
| 4205 | |||
| 4206 | if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) > | ||
| 4207 | YAFFS_MAX_NAME_LENGTH) | ||
| 4208 | /* ENAMETOOLONG */ | ||
| 4209 | return YAFFS_FAIL; | ||
| 4210 | |||
| 4211 | if(old_name) | ||
| 4212 | obj = yaffs_find_by_name(old_dir, old_name); | ||
| 4213 | else{ | ||
| 4214 | obj = old_dir; | ||
| 4215 | old_dir = obj->parent; | ||
| 4216 | } | ||
| 4217 | |||
| 4218 | |||
| 4219 | if (obj && obj->rename_allowed) { | ||
| 4220 | |||
| 4221 | /* Now do the handling for an existing target, if there is one */ | ||
| 4222 | |||
| 4223 | existing_target = yaffs_find_by_name(new_dir, new_name); | ||
| 4224 | if (yaffs_is_non_empty_dir(existing_target)){ | ||
| 4225 | return YAFFS_FAIL; /* ENOTEMPTY */ | ||
| 4226 | } else if (existing_target && existing_target != obj) { | ||
| 4227 | /* Nuke the target first, using shadowing, | ||
| 4228 | * but only if it isn't the same object. | ||
| 4229 | * | ||
| 4230 | * Note we must disable gc otherwise it can mess up the shadowing. | ||
| 4231 | * | ||
| 4232 | */ | ||
| 4233 | dev->gc_disable = 1; | ||
| 4234 | yaffs_change_obj_name(obj, new_dir, new_name, force, | ||
| 4235 | existing_target->obj_id); | ||
| 4236 | existing_target->is_shadowed = 1; | ||
| 4237 | yaffs_unlink_obj(existing_target); | ||
| 4238 | dev->gc_disable = 0; | ||
| 4239 | } | ||
| 4240 | |||
| 4241 | result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0); | ||
| 4242 | |||
| 4243 | yaffs_update_parent(old_dir); | ||
| 4244 | if (new_dir != old_dir) | ||
| 4245 | yaffs_update_parent(new_dir); | ||
| 4246 | |||
| 4247 | return result; | ||
| 4248 | } | ||
| 4249 | return YAFFS_FAIL; | ||
| 4250 | } | ||
| 4251 | |||
| 4252 | /*----------------------- Initialisation Scanning ---------------------- */ | ||
| 4253 | |||
| 4254 | void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id, | ||
| 4255 | int backward_scanning) | ||
| 4256 | { | ||
| 4257 | struct yaffs_obj *obj; | ||
| 4258 | |||
| 4259 | if (!backward_scanning) { | ||
| 4260 | /* Handle YAFFS1 forward scanning case | ||
| 4261 | * For YAFFS1 we always do the deletion | ||
| 4262 | */ | ||
| 4263 | |||
| 4264 | } else { | ||
| 4265 | /* Handle YAFFS2 case (backward scanning) | ||
| 4266 | * If the shadowed object exists then ignore. | ||
| 4267 | */ | ||
| 4268 | obj = yaffs_find_by_number(dev, obj_id); | ||
| 4269 | if (obj) | ||
| 4270 | return; | ||
| 4271 | } | ||
| 4272 | |||
| 4273 | /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc. | ||
| 4274 | * We put it in unlinked dir to be cleaned up after the scanning | ||
| 4275 | */ | ||
| 4276 | obj = | ||
| 4277 | yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE); | ||
| 4278 | if (!obj) | ||
| 4279 | return; | ||
| 4280 | obj->is_shadowed = 1; | ||
| 4281 | yaffs_add_obj_to_dir(dev->unlinked_dir, obj); | ||
| 4282 | obj->variant.file_variant.shrink_size = 0; | ||
| 4283 | obj->valid = 1; /* So that we don't read any other info for this file */ | ||
| 4284 | |||
| 4285 | } | ||
| 4286 | |||
| 4287 | void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list) | ||
| 4288 | { | ||
| 4289 | struct yaffs_obj *hl; | ||
| 4290 | struct yaffs_obj *in; | ||
| 4291 | |||
| 4292 | while (hard_list) { | ||
| 4293 | hl = hard_list; | ||
| 4294 | hard_list = (struct yaffs_obj *)(hard_list->hard_links.next); | ||
| 4295 | |||
| 4296 | in = yaffs_find_by_number(dev, | ||
| 4297 | hl->variant. | ||
| 4298 | hardlink_variant.equiv_id); | ||
| 4299 | |||
| 4300 | if (in) { | ||
| 4301 | /* Add the hardlink pointers */ | ||
| 4302 | hl->variant.hardlink_variant.equiv_obj = in; | ||
| 4303 | list_add(&hl->hard_links, &in->hard_links); | ||
| 4304 | } else { | ||
| 4305 | /* Todo Need to report/handle this better. | ||
| 4306 | * Got a problem... hardlink to a non-existant object | ||
| 4307 | */ | ||
| 4308 | hl->variant.hardlink_variant.equiv_obj = NULL; | ||
| 4309 | INIT_LIST_HEAD(&hl->hard_links); | ||
| 4310 | |||
| 4311 | } | ||
| 4312 | } | ||
| 4313 | } | ||
| 4314 | |||
| 4315 | static void yaffs_strip_deleted_objs(struct yaffs_dev *dev) | ||
| 4316 | { | ||
| 4317 | /* | ||
| 4318 | * Sort out state of unlinked and deleted objects after scanning. | ||
| 4319 | */ | ||
| 4320 | struct list_head *i; | ||
| 4321 | struct list_head *n; | ||
| 4322 | struct yaffs_obj *l; | ||
| 4323 | |||
| 4324 | if (dev->read_only) | ||
| 4325 | return; | ||
| 4326 | |||
| 4327 | /* Soft delete all the unlinked files */ | ||
| 4328 | list_for_each_safe(i, n, | ||
| 4329 | &dev->unlinked_dir->variant.dir_variant.children) { | ||
| 4330 | if (i) { | ||
| 4331 | l = list_entry(i, struct yaffs_obj, siblings); | ||
| 4332 | yaffs_del_obj(l); | ||
| 4333 | } | ||
| 4334 | } | ||
| 4335 | |||
| 4336 | list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) { | ||
| 4337 | if (i) { | ||
| 4338 | l = list_entry(i, struct yaffs_obj, siblings); | ||
| 4339 | yaffs_del_obj(l); | ||
| 4340 | } | ||
| 4341 | } | ||
| 4342 | |||
| 4343 | } | ||
| 4344 | |||
| 4345 | /* | ||
| 4346 | * This code iterates through all the objects making sure that they are rooted. | ||
| 4347 | * Any unrooted objects are re-rooted in lost+found. | ||
| 4348 | * An object needs to be in one of: | ||
| 4349 | * - Directly under deleted, unlinked | ||
| 4350 | * - Directly or indirectly under root. | ||
| 4351 | * | ||
| 4352 | * Note: | ||
| 4353 | * This code assumes that we don't ever change the current relationships between | ||
| 4354 | * directories: | ||
| 4355 | * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL | ||
| 4356 | * lost-n-found->parent == root_dir | ||
| 4357 | * | ||
| 4358 | * This fixes the problem where directories might have inadvertently been deleted | ||
| 4359 | * leaving the object "hanging" without being rooted in the directory tree. | ||
| 4360 | */ | ||
| 4361 | |||
| 4362 | static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj) | ||
| 4363 | { | ||
| 4364 | return (obj == dev->del_dir || | ||
| 4365 | obj == dev->unlinked_dir || obj == dev->root_dir); | ||
| 4366 | } | ||
| 4367 | |||
| 4368 | static void yaffs_fix_hanging_objs(struct yaffs_dev *dev) | ||
| 4369 | { | ||
| 4370 | struct yaffs_obj *obj; | ||
| 4371 | struct yaffs_obj *parent; | ||
| 4372 | int i; | ||
| 4373 | struct list_head *lh; | ||
| 4374 | struct list_head *n; | ||
| 4375 | int depth_limit; | ||
| 4376 | int hanging; | ||
| 4377 | |||
| 4378 | if (dev->read_only) | ||
| 4379 | return; | ||
| 4380 | |||
| 4381 | /* Iterate through the objects in each hash entry, | ||
| 4382 | * looking at each object. | ||
| 4383 | * Make sure it is rooted. | ||
| 4384 | */ | ||
| 4385 | |||
| 4386 | for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { | ||
| 4387 | list_for_each_safe(lh, n, &dev->obj_bucket[i].list) { | ||
| 4388 | if (lh) { | ||
| 4389 | obj = | ||
| 4390 | list_entry(lh, struct yaffs_obj, hash_link); | ||
| 4391 | parent = obj->parent; | ||
| 4392 | |||
| 4393 | if (yaffs_has_null_parent(dev, obj)) { | ||
| 4394 | /* These directories are not hanging */ | ||
| 4395 | hanging = 0; | ||
| 4396 | } else if (!parent | ||
| 4397 | || parent->variant_type != | ||
| 4398 | YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 4399 | hanging = 1; | ||
| 4400 | } else if (yaffs_has_null_parent(dev, parent)) { | ||
| 4401 | hanging = 0; | ||
| 4402 | } else { | ||
| 4403 | /* | ||
| 4404 | * Need to follow the parent chain to see if it is hanging. | ||
| 4405 | */ | ||
| 4406 | hanging = 0; | ||
| 4407 | depth_limit = 100; | ||
| 4408 | |||
| 4409 | while (parent != dev->root_dir && | ||
| 4410 | parent->parent && | ||
| 4411 | parent->parent->variant_type == | ||
| 4412 | YAFFS_OBJECT_TYPE_DIRECTORY | ||
| 4413 | && depth_limit > 0) { | ||
| 4414 | parent = parent->parent; | ||
| 4415 | depth_limit--; | ||
| 4416 | } | ||
| 4417 | if (parent != dev->root_dir) | ||
| 4418 | hanging = 1; | ||
| 4419 | } | ||
| 4420 | if (hanging) { | ||
| 4421 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 4422 | "Hanging object %d moved to lost and found", | ||
| 4423 | obj->obj_id); | ||
| 4424 | yaffs_add_obj_to_dir(dev->lost_n_found, | ||
| 4425 | obj); | ||
| 4426 | } | ||
| 4427 | } | ||
| 4428 | } | ||
| 4429 | } | ||
| 4430 | } | ||
| 4431 | |||
| 4432 | /* | ||
| 4433 | * Delete directory contents for cleaning up lost and found. | ||
| 4434 | */ | ||
| 4435 | static void yaffs_del_dir_contents(struct yaffs_obj *dir) | ||
| 4436 | { | ||
| 4437 | struct yaffs_obj *obj; | ||
| 4438 | struct list_head *lh; | ||
| 4439 | struct list_head *n; | ||
| 4440 | |||
| 4441 | if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) | ||
| 4442 | YBUG(); | ||
| 4443 | |||
| 4444 | list_for_each_safe(lh, n, &dir->variant.dir_variant.children) { | ||
| 4445 | if (lh) { | ||
| 4446 | obj = list_entry(lh, struct yaffs_obj, siblings); | ||
| 4447 | if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) | ||
| 4448 | yaffs_del_dir_contents(obj); | ||
| 4449 | |||
| 4450 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 4451 | "Deleting lost_found object %d", | ||
| 4452 | obj->obj_id); | ||
| 4453 | |||
| 4454 | /* Need to use UnlinkObject since Delete would not handle | ||
| 4455 | * hardlinked objects correctly. | ||
| 4456 | */ | ||
| 4457 | yaffs_unlink_obj(obj); | ||
| 4458 | } | ||
| 4459 | } | ||
| 4460 | |||
| 4461 | } | ||
| 4462 | |||
| 4463 | static void yaffs_empty_l_n_f(struct yaffs_dev *dev) | ||
| 4464 | { | ||
| 4465 | yaffs_del_dir_contents(dev->lost_n_found); | ||
| 4466 | } | ||
| 4467 | |||
| 4468 | |||
| 4469 | struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory, | ||
| 4470 | const YCHAR * name) | ||
| 4471 | { | ||
| 4472 | int sum; | ||
| 4473 | |||
| 4474 | struct list_head *i; | ||
| 4475 | YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1]; | ||
| 4476 | |||
| 4477 | struct yaffs_obj *l; | ||
| 4478 | |||
| 4479 | if (!name) | ||
| 4480 | return NULL; | ||
| 4481 | |||
| 4482 | if (!directory) { | ||
| 4483 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 4484 | "tragedy: yaffs_find_by_name: null pointer directory" | ||
| 4485 | ); | ||
| 4486 | YBUG(); | ||
| 4487 | return NULL; | ||
| 4488 | } | ||
| 4489 | if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 4490 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 4491 | "tragedy: yaffs_find_by_name: non-directory" | ||
| 4492 | ); | ||
| 4493 | YBUG(); | ||
| 4494 | } | ||
| 4495 | |||
| 4496 | sum = yaffs_calc_name_sum(name); | ||
| 4497 | |||
| 4498 | list_for_each(i, &directory->variant.dir_variant.children) { | ||
| 4499 | if (i) { | ||
| 4500 | l = list_entry(i, struct yaffs_obj, siblings); | ||
| 4501 | |||
| 4502 | if (l->parent != directory) | ||
| 4503 | YBUG(); | ||
| 4504 | |||
| 4505 | yaffs_check_obj_details_loaded(l); | ||
| 4506 | |||
| 4507 | /* Special case for lost-n-found */ | ||
| 4508 | if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { | ||
| 4509 | if (!strcmp(name, YAFFS_LOSTNFOUND_NAME)) | ||
| 4510 | return l; | ||
| 4511 | } else if (l->sum == sum | ||
| 4512 | || l->hdr_chunk <= 0) { | ||
| 4513 | /* LostnFound chunk called Objxxx | ||
| 4514 | * Do a real check | ||
| 4515 | */ | ||
| 4516 | yaffs_get_obj_name(l, buffer, | ||
| 4517 | YAFFS_MAX_NAME_LENGTH + 1); | ||
| 4518 | if (strncmp | ||
| 4519 | (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0) | ||
| 4520 | return l; | ||
| 4521 | } | ||
| 4522 | } | ||
| 4523 | } | ||
| 4524 | |||
| 4525 | return NULL; | ||
| 4526 | } | ||
| 4527 | |||
| 4528 | /* GetEquivalentObject dereferences any hard links to get to the | ||
| 4529 | * actual object. | ||
| 4530 | */ | ||
| 4531 | |||
| 4532 | struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj) | ||
| 4533 | { | ||
| 4534 | if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { | ||
| 4535 | /* We want the object id of the equivalent object, not this one */ | ||
| 4536 | obj = obj->variant.hardlink_variant.equiv_obj; | ||
| 4537 | yaffs_check_obj_details_loaded(obj); | ||
| 4538 | } | ||
| 4539 | return obj; | ||
| 4540 | } | ||
| 4541 | |||
| 4542 | /* | ||
| 4543 | * A note or two on object names. | ||
| 4544 | * * If the object name is missing, we then make one up in the form objnnn | ||
| 4545 | * | ||
| 4546 | * * ASCII names are stored in the object header's name field from byte zero | ||
| 4547 | * * Unicode names are historically stored starting from byte zero. | ||
| 4548 | * | ||
| 4549 | * Then there are automatic Unicode names... | ||
| 4550 | * The purpose of these is to save names in a way that can be read as | ||
| 4551 | * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII | ||
| 4552 | * system to share files. | ||
| 4553 | * | ||
| 4554 | * These automatic unicode are stored slightly differently... | ||
| 4555 | * - If the name can fit in the ASCII character space then they are saved as | ||
| 4556 | * ascii names as per above. | ||
| 4557 | * - If the name needs Unicode then the name is saved in Unicode | ||
| 4558 | * starting at oh->name[1]. | ||
| 4559 | |||
| 4560 | */ | ||
| 4561 | static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name, | ||
| 4562 | int buffer_size) | ||
| 4563 | { | ||
| 4564 | /* Create an object name if we could not find one. */ | ||
| 4565 | if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) { | ||
| 4566 | YCHAR local_name[20]; | ||
| 4567 | YCHAR num_string[20]; | ||
| 4568 | YCHAR *x = &num_string[19]; | ||
| 4569 | unsigned v = obj->obj_id; | ||
| 4570 | num_string[19] = 0; | ||
| 4571 | while (v > 0) { | ||
| 4572 | x--; | ||
| 4573 | *x = '0' + (v % 10); | ||
| 4574 | v /= 10; | ||
| 4575 | } | ||
| 4576 | /* make up a name */ | ||
| 4577 | strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX); | ||
| 4578 | strcat(local_name, x); | ||
| 4579 | strncpy(name, local_name, buffer_size - 1); | ||
| 4580 | } | ||
| 4581 | } | ||
| 4582 | |||
| 4583 | int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size) | ||
| 4584 | { | ||
| 4585 | memset(name, 0, buffer_size * sizeof(YCHAR)); | ||
| 4586 | |||
| 4587 | yaffs_check_obj_details_loaded(obj); | ||
| 4588 | |||
| 4589 | if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { | ||
| 4590 | strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1); | ||
| 4591 | } | ||
| 4592 | #ifndef CONFIG_YAFFS_NO_SHORT_NAMES | ||
| 4593 | else if (obj->short_name[0]) { | ||
| 4594 | strcpy(name, obj->short_name); | ||
| 4595 | } | ||
| 4596 | #endif | ||
| 4597 | else if (obj->hdr_chunk > 0) { | ||
| 4598 | int result; | ||
| 4599 | u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__); | ||
| 4600 | |||
| 4601 | struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer; | ||
| 4602 | |||
| 4603 | memset(buffer, 0, obj->my_dev->data_bytes_per_chunk); | ||
| 4604 | |||
| 4605 | if (obj->hdr_chunk > 0) { | ||
| 4606 | result = yaffs_rd_chunk_tags_nand(obj->my_dev, | ||
| 4607 | obj->hdr_chunk, | ||
| 4608 | buffer, NULL); | ||
| 4609 | } | ||
| 4610 | yaffs_load_name_from_oh(obj->my_dev, name, oh->name, | ||
| 4611 | buffer_size); | ||
| 4612 | |||
| 4613 | yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__); | ||
| 4614 | } | ||
| 4615 | |||
| 4616 | yaffs_fix_null_name(obj, name, buffer_size); | ||
| 4617 | |||
| 4618 | return strnlen(name, YAFFS_MAX_NAME_LENGTH); | ||
| 4619 | } | ||
| 4620 | |||
| 4621 | int yaffs_get_obj_length(struct yaffs_obj *obj) | ||
| 4622 | { | ||
| 4623 | /* Dereference any hard linking */ | ||
| 4624 | obj = yaffs_get_equivalent_obj(obj); | ||
| 4625 | |||
| 4626 | if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) | ||
| 4627 | return obj->variant.file_variant.file_size; | ||
| 4628 | if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { | ||
| 4629 | if (!obj->variant.symlink_variant.alias) | ||
| 4630 | return 0; | ||
| 4631 | return strnlen(obj->variant.symlink_variant.alias, | ||
| 4632 | YAFFS_MAX_ALIAS_LENGTH); | ||
| 4633 | } else { | ||
| 4634 | /* Only a directory should drop through to here */ | ||
| 4635 | return obj->my_dev->data_bytes_per_chunk; | ||
| 4636 | } | ||
| 4637 | } | ||
| 4638 | |||
| 4639 | int yaffs_get_obj_link_count(struct yaffs_obj *obj) | ||
| 4640 | { | ||
| 4641 | int count = 0; | ||
| 4642 | struct list_head *i; | ||
| 4643 | |||
| 4644 | if (!obj->unlinked) | ||
| 4645 | count++; /* the object itself */ | ||
| 4646 | |||
| 4647 | list_for_each(i, &obj->hard_links) | ||
| 4648 | count++; /* add the hard links; */ | ||
| 4649 | |||
| 4650 | return count; | ||
| 4651 | } | ||
| 4652 | |||
| 4653 | int yaffs_get_obj_inode(struct yaffs_obj *obj) | ||
| 4654 | { | ||
| 4655 | obj = yaffs_get_equivalent_obj(obj); | ||
| 4656 | |||
| 4657 | return obj->obj_id; | ||
| 4658 | } | ||
| 4659 | |||
| 4660 | unsigned yaffs_get_obj_type(struct yaffs_obj *obj) | ||
| 4661 | { | ||
| 4662 | obj = yaffs_get_equivalent_obj(obj); | ||
| 4663 | |||
| 4664 | switch (obj->variant_type) { | ||
| 4665 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 4666 | return DT_REG; | ||
| 4667 | break; | ||
| 4668 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 4669 | return DT_DIR; | ||
| 4670 | break; | ||
| 4671 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 4672 | return DT_LNK; | ||
| 4673 | break; | ||
| 4674 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 4675 | return DT_REG; | ||
| 4676 | break; | ||
| 4677 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 4678 | if (S_ISFIFO(obj->yst_mode)) | ||
| 4679 | return DT_FIFO; | ||
| 4680 | if (S_ISCHR(obj->yst_mode)) | ||
| 4681 | return DT_CHR; | ||
| 4682 | if (S_ISBLK(obj->yst_mode)) | ||
| 4683 | return DT_BLK; | ||
| 4684 | if (S_ISSOCK(obj->yst_mode)) | ||
| 4685 | return DT_SOCK; | ||
| 4686 | default: | ||
| 4687 | return DT_REG; | ||
| 4688 | break; | ||
| 4689 | } | ||
| 4690 | } | ||
| 4691 | |||
| 4692 | YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj) | ||
| 4693 | { | ||
| 4694 | obj = yaffs_get_equivalent_obj(obj); | ||
| 4695 | if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) | ||
| 4696 | return yaffs_clone_str(obj->variant.symlink_variant.alias); | ||
| 4697 | else | ||
| 4698 | return yaffs_clone_str(_Y("")); | ||
| 4699 | } | ||
| 4700 | |||
| 4701 | /*--------------------------- Initialisation code -------------------------- */ | ||
| 4702 | |||
| 4703 | static int yaffs_check_dev_fns(const struct yaffs_dev *dev) | ||
| 4704 | { | ||
| 4705 | |||
| 4706 | /* Common functions, gotta have */ | ||
| 4707 | if (!dev->param.erase_fn || !dev->param.initialise_flash_fn) | ||
| 4708 | return 0; | ||
| 4709 | |||
| 4710 | #ifdef CONFIG_YAFFS_YAFFS2 | ||
| 4711 | |||
| 4712 | /* Can use the "with tags" style interface for yaffs1 or yaffs2 */ | ||
| 4713 | if (dev->param.write_chunk_tags_fn && | ||
| 4714 | dev->param.read_chunk_tags_fn && | ||
| 4715 | !dev->param.write_chunk_fn && | ||
| 4716 | !dev->param.read_chunk_fn && | ||
| 4717 | dev->param.bad_block_fn && dev->param.query_block_fn) | ||
| 4718 | return 1; | ||
| 4719 | #endif | ||
| 4720 | |||
| 4721 | /* Can use the "spare" style interface for yaffs1 */ | ||
| 4722 | if (!dev->param.is_yaffs2 && | ||
| 4723 | !dev->param.write_chunk_tags_fn && | ||
| 4724 | !dev->param.read_chunk_tags_fn && | ||
| 4725 | dev->param.write_chunk_fn && | ||
| 4726 | dev->param.read_chunk_fn && | ||
| 4727 | !dev->param.bad_block_fn && !dev->param.query_block_fn) | ||
| 4728 | return 1; | ||
| 4729 | |||
| 4730 | return 0; /* bad */ | ||
| 4731 | } | ||
| 4732 | |||
| 4733 | static int yaffs_create_initial_dir(struct yaffs_dev *dev) | ||
| 4734 | { | ||
| 4735 | /* Initialise the unlinked, deleted, root and lost and found directories */ | ||
| 4736 | |||
| 4737 | dev->lost_n_found = dev->root_dir = NULL; | ||
| 4738 | dev->unlinked_dir = dev->del_dir = NULL; | ||
| 4739 | |||
| 4740 | dev->unlinked_dir = | ||
| 4741 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR); | ||
| 4742 | |||
| 4743 | dev->del_dir = | ||
| 4744 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR); | ||
| 4745 | |||
| 4746 | dev->root_dir = | ||
| 4747 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT, | ||
| 4748 | YAFFS_ROOT_MODE | S_IFDIR); | ||
| 4749 | dev->lost_n_found = | ||
| 4750 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND, | ||
| 4751 | YAFFS_LOSTNFOUND_MODE | S_IFDIR); | ||
| 4752 | |||
| 4753 | if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir | ||
| 4754 | && dev->del_dir) { | ||
| 4755 | yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found); | ||
| 4756 | return YAFFS_OK; | ||
| 4757 | } | ||
| 4758 | |||
| 4759 | return YAFFS_FAIL; | ||
| 4760 | } | ||
| 4761 | |||
| 4762 | int yaffs_guts_initialise(struct yaffs_dev *dev) | ||
| 4763 | { | ||
| 4764 | int init_failed = 0; | ||
| 4765 | unsigned x; | ||
| 4766 | int bits; | ||
| 4767 | |||
| 4768 | yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()" ); | ||
| 4769 | |||
| 4770 | /* Check stuff that must be set */ | ||
| 4771 | |||
| 4772 | if (!dev) { | ||
| 4773 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 4774 | "yaffs: Need a device" | ||
| 4775 | ); | ||
| 4776 | return YAFFS_FAIL; | ||
| 4777 | } | ||
| 4778 | |||
| 4779 | dev->internal_start_block = dev->param.start_block; | ||
| 4780 | dev->internal_end_block = dev->param.end_block; | ||
| 4781 | dev->block_offset = 0; | ||
| 4782 | dev->chunk_offset = 0; | ||
| 4783 | dev->n_free_chunks = 0; | ||
| 4784 | |||
| 4785 | dev->gc_block = 0; | ||
| 4786 | |||
| 4787 | if (dev->param.start_block == 0) { | ||
| 4788 | dev->internal_start_block = dev->param.start_block + 1; | ||
| 4789 | dev->internal_end_block = dev->param.end_block + 1; | ||
| 4790 | dev->block_offset = 1; | ||
| 4791 | dev->chunk_offset = dev->param.chunks_per_block; | ||
| 4792 | } | ||
| 4793 | |||
| 4794 | /* Check geometry parameters. */ | ||
| 4795 | |||
| 4796 | if ((!dev->param.inband_tags && dev->param.is_yaffs2 && | ||
| 4797 | dev->param.total_bytes_per_chunk < 1024) || | ||
| 4798 | (!dev->param.is_yaffs2 && | ||
| 4799 | dev->param.total_bytes_per_chunk < 512) || | ||
| 4800 | (dev->param.inband_tags && !dev->param.is_yaffs2) || | ||
| 4801 | dev->param.chunks_per_block < 2 || | ||
| 4802 | dev->param.n_reserved_blocks < 2 || | ||
| 4803 | dev->internal_start_block <= 0 || | ||
| 4804 | dev->internal_end_block <= 0 || | ||
| 4805 | dev->internal_end_block <= | ||
| 4806 | (dev->internal_start_block + dev->param.n_reserved_blocks + 2) | ||
| 4807 | ) { | ||
| 4808 | /* otherwise it is too small */ | ||
| 4809 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 4810 | "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ", | ||
| 4811 | dev->param.total_bytes_per_chunk, | ||
| 4812 | dev->param.is_yaffs2 ? "2" : "", | ||
| 4813 | dev->param.inband_tags); | ||
| 4814 | return YAFFS_FAIL; | ||
| 4815 | } | ||
| 4816 | |||
| 4817 | if (yaffs_init_nand(dev) != YAFFS_OK) { | ||
| 4818 | yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed"); | ||
| 4819 | return YAFFS_FAIL; | ||
| 4820 | } | ||
| 4821 | |||
| 4822 | /* Sort out space for inband tags, if required */ | ||
| 4823 | if (dev->param.inband_tags) | ||
| 4824 | dev->data_bytes_per_chunk = | ||
| 4825 | dev->param.total_bytes_per_chunk - | ||
| 4826 | sizeof(struct yaffs_packed_tags2_tags_only); | ||
| 4827 | else | ||
| 4828 | dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk; | ||
| 4829 | |||
| 4830 | /* Got the right mix of functions? */ | ||
| 4831 | if (!yaffs_check_dev_fns(dev)) { | ||
| 4832 | /* Function missing */ | ||
| 4833 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 4834 | "device function(s) missing or wrong"); | ||
| 4835 | |||
| 4836 | return YAFFS_FAIL; | ||
| 4837 | } | ||
| 4838 | |||
| 4839 | if (dev->is_mounted) { | ||
| 4840 | yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted"); | ||
| 4841 | return YAFFS_FAIL; | ||
| 4842 | } | ||
| 4843 | |||
| 4844 | /* Finished with most checks. One or two more checks happen later on too. */ | ||
| 4845 | |||
| 4846 | dev->is_mounted = 1; | ||
| 4847 | |||
| 4848 | /* OK now calculate a few things for the device */ | ||
| 4849 | |||
| 4850 | /* | ||
| 4851 | * Calculate all the chunk size manipulation numbers: | ||
| 4852 | */ | ||
| 4853 | x = dev->data_bytes_per_chunk; | ||
| 4854 | /* We always use dev->chunk_shift and dev->chunk_div */ | ||
| 4855 | dev->chunk_shift = calc_shifts(x); | ||
| 4856 | x >>= dev->chunk_shift; | ||
| 4857 | dev->chunk_div = x; | ||
| 4858 | /* We only use chunk mask if chunk_div is 1 */ | ||
| 4859 | dev->chunk_mask = (1 << dev->chunk_shift) - 1; | ||
| 4860 | |||
| 4861 | /* | ||
| 4862 | * Calculate chunk_grp_bits. | ||
| 4863 | * We need to find the next power of 2 > than internal_end_block | ||
| 4864 | */ | ||
| 4865 | |||
| 4866 | x = dev->param.chunks_per_block * (dev->internal_end_block + 1); | ||
| 4867 | |||
| 4868 | bits = calc_shifts_ceiling(x); | ||
| 4869 | |||
| 4870 | /* Set up tnode width if wide tnodes are enabled. */ | ||
| 4871 | if (!dev->param.wide_tnodes_disabled) { | ||
| 4872 | /* bits must be even so that we end up with 32-bit words */ | ||
| 4873 | if (bits & 1) | ||
| 4874 | bits++; | ||
| 4875 | if (bits < 16) | ||
| 4876 | dev->tnode_width = 16; | ||
| 4877 | else | ||
| 4878 | dev->tnode_width = bits; | ||
| 4879 | } else { | ||
| 4880 | dev->tnode_width = 16; | ||
| 4881 | } | ||
| 4882 | |||
| 4883 | dev->tnode_mask = (1 << dev->tnode_width) - 1; | ||
| 4884 | |||
| 4885 | /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled), | ||
| 4886 | * so if the bitwidth of the | ||
| 4887 | * chunk range we're using is greater than 16 we need | ||
| 4888 | * to figure out chunk shift and chunk_grp_size | ||
| 4889 | */ | ||
| 4890 | |||
| 4891 | if (bits <= dev->tnode_width) | ||
| 4892 | dev->chunk_grp_bits = 0; | ||
| 4893 | else | ||
| 4894 | dev->chunk_grp_bits = bits - dev->tnode_width; | ||
| 4895 | |||
| 4896 | dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8; | ||
| 4897 | if (dev->tnode_size < sizeof(struct yaffs_tnode)) | ||
| 4898 | dev->tnode_size = sizeof(struct yaffs_tnode); | ||
| 4899 | |||
| 4900 | dev->chunk_grp_size = 1 << dev->chunk_grp_bits; | ||
| 4901 | |||
| 4902 | if (dev->param.chunks_per_block < dev->chunk_grp_size) { | ||
| 4903 | /* We have a problem because the soft delete won't work if | ||
| 4904 | * the chunk group size > chunks per block. | ||
| 4905 | * This can be remedied by using larger "virtual blocks". | ||
| 4906 | */ | ||
| 4907 | yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large"); | ||
| 4908 | |||
| 4909 | return YAFFS_FAIL; | ||
| 4910 | } | ||
| 4911 | |||
| 4912 | /* OK, we've finished verifying the device, lets continue with initialisation */ | ||
| 4913 | |||
| 4914 | /* More device initialisation */ | ||
| 4915 | dev->all_gcs = 0; | ||
| 4916 | dev->passive_gc_count = 0; | ||
| 4917 | dev->oldest_dirty_gc_count = 0; | ||
| 4918 | dev->bg_gcs = 0; | ||
| 4919 | dev->gc_block_finder = 0; | ||
| 4920 | dev->buffered_block = -1; | ||
| 4921 | dev->doing_buffered_block_rewrite = 0; | ||
| 4922 | dev->n_deleted_files = 0; | ||
| 4923 | dev->n_bg_deletions = 0; | ||
| 4924 | dev->n_unlinked_files = 0; | ||
| 4925 | dev->n_ecc_fixed = 0; | ||
| 4926 | dev->n_ecc_unfixed = 0; | ||
| 4927 | dev->n_tags_ecc_fixed = 0; | ||
| 4928 | dev->n_tags_ecc_unfixed = 0; | ||
| 4929 | dev->n_erase_failures = 0; | ||
| 4930 | dev->n_erased_blocks = 0; | ||
| 4931 | dev->gc_disable = 0; | ||
| 4932 | dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */ | ||
| 4933 | INIT_LIST_HEAD(&dev->dirty_dirs); | ||
| 4934 | dev->oldest_dirty_seq = 0; | ||
| 4935 | dev->oldest_dirty_block = 0; | ||
| 4936 | |||
| 4937 | /* Initialise temporary buffers and caches. */ | ||
| 4938 | if (!yaffs_init_tmp_buffers(dev)) | ||
| 4939 | init_failed = 1; | ||
| 4940 | |||
| 4941 | dev->cache = NULL; | ||
| 4942 | dev->gc_cleanup_list = NULL; | ||
| 4943 | |||
| 4944 | if (!init_failed && dev->param.n_caches > 0) { | ||
| 4945 | int i; | ||
| 4946 | void *buf; | ||
| 4947 | int cache_bytes = | ||
| 4948 | dev->param.n_caches * sizeof(struct yaffs_cache); | ||
| 4949 | |||
| 4950 | if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES) | ||
| 4951 | dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES; | ||
| 4952 | |||
| 4953 | dev->cache = kmalloc(cache_bytes, GFP_NOFS); | ||
| 4954 | |||
| 4955 | buf = (u8 *) dev->cache; | ||
| 4956 | |||
| 4957 | if (dev->cache) | ||
| 4958 | memset(dev->cache, 0, cache_bytes); | ||
| 4959 | |||
| 4960 | for (i = 0; i < dev->param.n_caches && buf; i++) { | ||
| 4961 | dev->cache[i].object = NULL; | ||
| 4962 | dev->cache[i].last_use = 0; | ||
| 4963 | dev->cache[i].dirty = 0; | ||
| 4964 | dev->cache[i].data = buf = | ||
| 4965 | kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); | ||
| 4966 | } | ||
| 4967 | if (!buf) | ||
| 4968 | init_failed = 1; | ||
| 4969 | |||
| 4970 | dev->cache_last_use = 0; | ||
| 4971 | } | ||
| 4972 | |||
| 4973 | dev->cache_hits = 0; | ||
| 4974 | |||
| 4975 | if (!init_failed) { | ||
| 4976 | dev->gc_cleanup_list = | ||
| 4977 | kmalloc(dev->param.chunks_per_block * sizeof(u32), | ||
| 4978 | GFP_NOFS); | ||
| 4979 | if (!dev->gc_cleanup_list) | ||
| 4980 | init_failed = 1; | ||
| 4981 | } | ||
| 4982 | |||
| 4983 | if (dev->param.is_yaffs2) | ||
| 4984 | dev->param.use_header_file_size = 1; | ||
| 4985 | |||
| 4986 | if (!init_failed && !yaffs_init_blocks(dev)) | ||
| 4987 | init_failed = 1; | ||
| 4988 | |||
| 4989 | yaffs_init_tnodes_and_objs(dev); | ||
| 4990 | |||
| 4991 | if (!init_failed && !yaffs_create_initial_dir(dev)) | ||
| 4992 | init_failed = 1; | ||
| 4993 | |||
| 4994 | if (!init_failed) { | ||
| 4995 | /* Now scan the flash. */ | ||
| 4996 | if (dev->param.is_yaffs2) { | ||
| 4997 | if (yaffs2_checkpt_restore(dev)) { | ||
| 4998 | yaffs_check_obj_details_loaded(dev->root_dir); | ||
| 4999 | yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT, | ||
| 5000 | "yaffs: restored from checkpoint" | ||
| 5001 | ); | ||
| 5002 | } else { | ||
| 5003 | |||
| 5004 | /* Clean up the mess caused by an aborted checkpoint load | ||
| 5005 | * and scan backwards. | ||
| 5006 | */ | ||
| 5007 | yaffs_deinit_blocks(dev); | ||
| 5008 | |||
| 5009 | yaffs_deinit_tnodes_and_objs(dev); | ||
| 5010 | |||
| 5011 | dev->n_erased_blocks = 0; | ||
| 5012 | dev->n_free_chunks = 0; | ||
| 5013 | dev->alloc_block = -1; | ||
| 5014 | dev->alloc_page = -1; | ||
| 5015 | dev->n_deleted_files = 0; | ||
| 5016 | dev->n_unlinked_files = 0; | ||
| 5017 | dev->n_bg_deletions = 0; | ||
| 5018 | |||
| 5019 | if (!init_failed && !yaffs_init_blocks(dev)) | ||
| 5020 | init_failed = 1; | ||
| 5021 | |||
| 5022 | yaffs_init_tnodes_and_objs(dev); | ||
| 5023 | |||
| 5024 | if (!init_failed | ||
| 5025 | && !yaffs_create_initial_dir(dev)) | ||
| 5026 | init_failed = 1; | ||
| 5027 | |||
| 5028 | if (!init_failed && !yaffs2_scan_backwards(dev)) | ||
| 5029 | init_failed = 1; | ||
| 5030 | } | ||
| 5031 | } else if (!yaffs1_scan(dev)) { | ||
| 5032 | init_failed = 1; | ||
| 5033 | } | ||
| 5034 | |||
| 5035 | yaffs_strip_deleted_objs(dev); | ||
| 5036 | yaffs_fix_hanging_objs(dev); | ||
| 5037 | if (dev->param.empty_lost_n_found) | ||
| 5038 | yaffs_empty_l_n_f(dev); | ||
| 5039 | } | ||
| 5040 | |||
| 5041 | if (init_failed) { | ||
| 5042 | /* Clean up the mess */ | ||
| 5043 | yaffs_trace(YAFFS_TRACE_TRACING, | ||
| 5044 | "yaffs: yaffs_guts_initialise() aborted."); | ||
| 5045 | |||
| 5046 | yaffs_deinitialise(dev); | ||
| 5047 | return YAFFS_FAIL; | ||
| 5048 | } | ||
| 5049 | |||
| 5050 | /* Zero out stats */ | ||
| 5051 | dev->n_page_reads = 0; | ||
| 5052 | dev->n_page_writes = 0; | ||
| 5053 | dev->n_erasures = 0; | ||
| 5054 | dev->n_gc_copies = 0; | ||
| 5055 | dev->n_retired_writes = 0; | ||
| 5056 | |||
| 5057 | dev->n_retired_blocks = 0; | ||
| 5058 | |||
| 5059 | yaffs_verify_free_chunks(dev); | ||
| 5060 | yaffs_verify_blocks(dev); | ||
| 5061 | |||
| 5062 | /* Clean up any aborted checkpoint data */ | ||
| 5063 | if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0) | ||
| 5064 | yaffs2_checkpt_invalidate(dev); | ||
| 5065 | |||
| 5066 | yaffs_trace(YAFFS_TRACE_TRACING, | ||
| 5067 | "yaffs: yaffs_guts_initialise() done."); | ||
| 5068 | return YAFFS_OK; | ||
| 5069 | |||
| 5070 | } | ||
| 5071 | |||
| 5072 | void yaffs_deinitialise(struct yaffs_dev *dev) | ||
| 5073 | { | ||
| 5074 | if (dev->is_mounted) { | ||
| 5075 | int i; | ||
| 5076 | |||
| 5077 | yaffs_deinit_blocks(dev); | ||
| 5078 | yaffs_deinit_tnodes_and_objs(dev); | ||
| 5079 | if (dev->param.n_caches > 0 && dev->cache) { | ||
| 5080 | |||
| 5081 | for (i = 0; i < dev->param.n_caches; i++) { | ||
| 5082 | if (dev->cache[i].data) | ||
| 5083 | kfree(dev->cache[i].data); | ||
| 5084 | dev->cache[i].data = NULL; | ||
| 5085 | } | ||
| 5086 | |||
| 5087 | kfree(dev->cache); | ||
| 5088 | dev->cache = NULL; | ||
| 5089 | } | ||
| 5090 | |||
| 5091 | kfree(dev->gc_cleanup_list); | ||
| 5092 | |||
| 5093 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) | ||
| 5094 | kfree(dev->temp_buffer[i].buffer); | ||
| 5095 | |||
| 5096 | dev->is_mounted = 0; | ||
| 5097 | |||
| 5098 | if (dev->param.deinitialise_flash_fn) | ||
| 5099 | dev->param.deinitialise_flash_fn(dev); | ||
| 5100 | } | ||
| 5101 | } | ||
| 5102 | |||
| 5103 | int yaffs_count_free_chunks(struct yaffs_dev *dev) | ||
| 5104 | { | ||
| 5105 | int n_free = 0; | ||
| 5106 | int b; | ||
| 5107 | |||
| 5108 | struct yaffs_block_info *blk; | ||
| 5109 | |||
| 5110 | blk = dev->block_info; | ||
| 5111 | for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) { | ||
| 5112 | switch (blk->block_state) { | ||
| 5113 | case YAFFS_BLOCK_STATE_EMPTY: | ||
| 5114 | case YAFFS_BLOCK_STATE_ALLOCATING: | ||
| 5115 | case YAFFS_BLOCK_STATE_COLLECTING: | ||
| 5116 | case YAFFS_BLOCK_STATE_FULL: | ||
| 5117 | n_free += | ||
| 5118 | (dev->param.chunks_per_block - blk->pages_in_use + | ||
| 5119 | blk->soft_del_pages); | ||
| 5120 | break; | ||
| 5121 | default: | ||
| 5122 | break; | ||
| 5123 | } | ||
| 5124 | blk++; | ||
| 5125 | } | ||
| 5126 | |||
| 5127 | return n_free; | ||
| 5128 | } | ||
| 5129 | |||
| 5130 | int yaffs_get_n_free_chunks(struct yaffs_dev *dev) | ||
| 5131 | { | ||
| 5132 | /* This is what we report to the outside world */ | ||
| 5133 | |||
| 5134 | int n_free; | ||
| 5135 | int n_dirty_caches; | ||
| 5136 | int blocks_for_checkpt; | ||
| 5137 | int i; | ||
| 5138 | |||
| 5139 | n_free = dev->n_free_chunks; | ||
| 5140 | n_free += dev->n_deleted_files; | ||
| 5141 | |||
| 5142 | /* Now count the number of dirty chunks in the cache and subtract those */ | ||
| 5143 | |||
| 5144 | for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) { | ||
| 5145 | if (dev->cache[i].dirty) | ||
| 5146 | n_dirty_caches++; | ||
| 5147 | } | ||
| 5148 | |||
| 5149 | n_free -= n_dirty_caches; | ||
| 5150 | |||
| 5151 | n_free -= | ||
| 5152 | ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block); | ||
| 5153 | |||
| 5154 | /* Now we figure out how much to reserve for the checkpoint and report that... */ | ||
| 5155 | blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev); | ||
| 5156 | |||
| 5157 | n_free -= (blocks_for_checkpt * dev->param.chunks_per_block); | ||
| 5158 | |||
| 5159 | if (n_free < 0) | ||
| 5160 | n_free = 0; | ||
| 5161 | |||
| 5162 | return n_free; | ||
| 5163 | |||
| 5164 | } | ||
diff --git a/fs/yaffs2/yaffs_guts.h b/fs/yaffs2/yaffs_guts.h new file mode 100644 index 00000000000..307eba28676 --- /dev/null +++ b/fs/yaffs2/yaffs_guts.h | |||
| @@ -0,0 +1,915 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_GUTS_H__ | ||
| 17 | #define __YAFFS_GUTS_H__ | ||
| 18 | |||
| 19 | #include "yportenv.h" | ||
| 20 | |||
| 21 | #define YAFFS_OK 1 | ||
| 22 | #define YAFFS_FAIL 0 | ||
| 23 | |||
| 24 | /* Give us a Y=0x59, | ||
| 25 | * Give us an A=0x41, | ||
| 26 | * Give us an FF=0xFF | ||
| 27 | * Give us an S=0x53 | ||
| 28 | * And what have we got... | ||
| 29 | */ | ||
| 30 | #define YAFFS_MAGIC 0x5941FF53 | ||
| 31 | |||
| 32 | #define YAFFS_NTNODES_LEVEL0 16 | ||
| 33 | #define YAFFS_TNODES_LEVEL0_BITS 4 | ||
| 34 | #define YAFFS_TNODES_LEVEL0_MASK 0xf | ||
| 35 | |||
| 36 | #define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2) | ||
| 37 | #define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1) | ||
| 38 | #define YAFFS_TNODES_INTERNAL_MASK 0x7 | ||
| 39 | #define YAFFS_TNODES_MAX_LEVEL 6 | ||
| 40 | |||
| 41 | #ifndef CONFIG_YAFFS_NO_YAFFS1 | ||
| 42 | #define YAFFS_BYTES_PER_SPARE 16 | ||
| 43 | #define YAFFS_BYTES_PER_CHUNK 512 | ||
| 44 | #define YAFFS_CHUNK_SIZE_SHIFT 9 | ||
| 45 | #define YAFFS_CHUNKS_PER_BLOCK 32 | ||
| 46 | #define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK) | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024 | ||
| 50 | #define YAFFS_MIN_YAFFS2_SPARE_SIZE 32 | ||
| 51 | |||
| 52 | #define YAFFS_MAX_CHUNK_ID 0x000FFFFF | ||
| 53 | |||
| 54 | #define YAFFS_ALLOCATION_NOBJECTS 100 | ||
| 55 | #define YAFFS_ALLOCATION_NTNODES 100 | ||
| 56 | #define YAFFS_ALLOCATION_NLINKS 100 | ||
| 57 | |||
| 58 | #define YAFFS_NOBJECT_BUCKETS 256 | ||
| 59 | |||
| 60 | #define YAFFS_OBJECT_SPACE 0x40000 | ||
| 61 | #define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE -1) | ||
| 62 | |||
| 63 | #define YAFFS_CHECKPOINT_VERSION 4 | ||
| 64 | |||
| 65 | #ifdef CONFIG_YAFFS_UNICODE | ||
| 66 | #define YAFFS_MAX_NAME_LENGTH 127 | ||
| 67 | #define YAFFS_MAX_ALIAS_LENGTH 79 | ||
| 68 | #else | ||
| 69 | #define YAFFS_MAX_NAME_LENGTH 255 | ||
| 70 | #define YAFFS_MAX_ALIAS_LENGTH 159 | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #define YAFFS_SHORT_NAME_LENGTH 15 | ||
| 74 | |||
| 75 | /* Some special object ids for pseudo objects */ | ||
| 76 | #define YAFFS_OBJECTID_ROOT 1 | ||
| 77 | #define YAFFS_OBJECTID_LOSTNFOUND 2 | ||
| 78 | #define YAFFS_OBJECTID_UNLINKED 3 | ||
| 79 | #define YAFFS_OBJECTID_DELETED 4 | ||
| 80 | |||
| 81 | /* Pseudo object ids for checkpointing */ | ||
| 82 | #define YAFFS_OBJECTID_SB_HEADER 0x10 | ||
| 83 | #define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20 | ||
| 84 | #define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21 | ||
| 85 | |||
| 86 | #define YAFFS_MAX_SHORT_OP_CACHES 20 | ||
| 87 | |||
| 88 | #define YAFFS_N_TEMP_BUFFERS 6 | ||
| 89 | |||
| 90 | /* We limit the number attempts at sucessfully saving a chunk of data. | ||
| 91 | * Small-page devices have 32 pages per block; large-page devices have 64. | ||
| 92 | * Default to something in the order of 5 to 10 blocks worth of chunks. | ||
| 93 | */ | ||
| 94 | #define YAFFS_WR_ATTEMPTS (5*64) | ||
| 95 | |||
| 96 | /* Sequence numbers are used in YAFFS2 to determine block allocation order. | ||
| 97 | * The range is limited slightly to help distinguish bad numbers from good. | ||
| 98 | * This also allows us to perhaps in the future use special numbers for | ||
| 99 | * special purposes. | ||
| 100 | * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years, | ||
| 101 | * and is a larger number than the lifetime of a 2GB device. | ||
| 102 | */ | ||
| 103 | #define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000 | ||
| 104 | #define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00 | ||
| 105 | |||
| 106 | /* Special sequence number for bad block that failed to be marked bad */ | ||
| 107 | #define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000 | ||
| 108 | |||
| 109 | /* ChunkCache is used for short read/write operations.*/ | ||
| 110 | struct yaffs_cache { | ||
| 111 | struct yaffs_obj *object; | ||
| 112 | int chunk_id; | ||
| 113 | int last_use; | ||
| 114 | int dirty; | ||
| 115 | int n_bytes; /* Only valid if the cache is dirty */ | ||
| 116 | int locked; /* Can't push out or flush while locked. */ | ||
| 117 | u8 *data; | ||
| 118 | }; | ||
| 119 | |||
| 120 | /* Tags structures in RAM | ||
| 121 | * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise | ||
| 122 | * the structure size will get blown out. | ||
| 123 | */ | ||
| 124 | |||
| 125 | #ifndef CONFIG_YAFFS_NO_YAFFS1 | ||
| 126 | struct yaffs_tags { | ||
| 127 | unsigned chunk_id:20; | ||
| 128 | unsigned serial_number:2; | ||
| 129 | unsigned n_bytes_lsb:10; | ||
| 130 | unsigned obj_id:18; | ||
| 131 | unsigned ecc:12; | ||
| 132 | unsigned n_bytes_msb:2; | ||
| 133 | }; | ||
| 134 | |||
| 135 | union yaffs_tags_union { | ||
| 136 | struct yaffs_tags as_tags; | ||
| 137 | u8 as_bytes[8]; | ||
| 138 | }; | ||
| 139 | |||
| 140 | #endif | ||
| 141 | |||
| 142 | /* Stuff used for extended tags in YAFFS2 */ | ||
| 143 | |||
| 144 | enum yaffs_ecc_result { | ||
| 145 | YAFFS_ECC_RESULT_UNKNOWN, | ||
| 146 | YAFFS_ECC_RESULT_NO_ERROR, | ||
| 147 | YAFFS_ECC_RESULT_FIXED, | ||
| 148 | YAFFS_ECC_RESULT_UNFIXED | ||
| 149 | }; | ||
| 150 | |||
| 151 | enum yaffs_obj_type { | ||
| 152 | YAFFS_OBJECT_TYPE_UNKNOWN, | ||
| 153 | YAFFS_OBJECT_TYPE_FILE, | ||
| 154 | YAFFS_OBJECT_TYPE_SYMLINK, | ||
| 155 | YAFFS_OBJECT_TYPE_DIRECTORY, | ||
| 156 | YAFFS_OBJECT_TYPE_HARDLINK, | ||
| 157 | YAFFS_OBJECT_TYPE_SPECIAL | ||
| 158 | }; | ||
| 159 | |||
| 160 | #define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL | ||
| 161 | |||
| 162 | struct yaffs_ext_tags { | ||
| 163 | |||
| 164 | unsigned validity0; | ||
| 165 | unsigned chunk_used; /* Status of the chunk: used or unused */ | ||
| 166 | unsigned obj_id; /* If 0 then this is not part of an object (unused) */ | ||
| 167 | unsigned chunk_id; /* If 0 then this is a header, else a data chunk */ | ||
| 168 | unsigned n_bytes; /* Only valid for data chunks */ | ||
| 169 | |||
| 170 | /* The following stuff only has meaning when we read */ | ||
| 171 | enum yaffs_ecc_result ecc_result; | ||
| 172 | unsigned block_bad; | ||
| 173 | |||
| 174 | /* YAFFS 1 stuff */ | ||
| 175 | unsigned is_deleted; /* The chunk is marked deleted */ | ||
| 176 | unsigned serial_number; /* Yaffs1 2-bit serial number */ | ||
| 177 | |||
| 178 | /* YAFFS2 stuff */ | ||
| 179 | unsigned seq_number; /* The sequence number of this block */ | ||
| 180 | |||
| 181 | /* Extra info if this is an object header (YAFFS2 only) */ | ||
| 182 | |||
| 183 | unsigned extra_available; /* There is extra info available if this is not zero */ | ||
| 184 | unsigned extra_parent_id; /* The parent object */ | ||
| 185 | unsigned extra_is_shrink; /* Is it a shrink header? */ | ||
| 186 | unsigned extra_shadows; /* Does this shadow another object? */ | ||
| 187 | |||
| 188 | enum yaffs_obj_type extra_obj_type; /* What object type? */ | ||
| 189 | |||
| 190 | unsigned extra_length; /* Length if it is a file */ | ||
| 191 | unsigned extra_equiv_id; /* Equivalent object Id if it is a hard link */ | ||
| 192 | |||
| 193 | unsigned validity1; | ||
| 194 | |||
| 195 | }; | ||
| 196 | |||
| 197 | /* Spare structure for YAFFS1 */ | ||
| 198 | struct yaffs_spare { | ||
| 199 | u8 tb0; | ||
| 200 | u8 tb1; | ||
| 201 | u8 tb2; | ||
| 202 | u8 tb3; | ||
| 203 | u8 page_status; /* set to 0 to delete the chunk */ | ||
| 204 | u8 block_status; | ||
| 205 | u8 tb4; | ||
| 206 | u8 tb5; | ||
| 207 | u8 ecc1[3]; | ||
| 208 | u8 tb6; | ||
| 209 | u8 tb7; | ||
| 210 | u8 ecc2[3]; | ||
| 211 | }; | ||
| 212 | |||
| 213 | /*Special structure for passing through to mtd */ | ||
| 214 | struct yaffs_nand_spare { | ||
| 215 | struct yaffs_spare spare; | ||
| 216 | int eccres1; | ||
| 217 | int eccres2; | ||
| 218 | }; | ||
| 219 | |||
| 220 | /* Block data in RAM */ | ||
| 221 | |||
| 222 | enum yaffs_block_state { | ||
| 223 | YAFFS_BLOCK_STATE_UNKNOWN = 0, | ||
| 224 | |||
| 225 | YAFFS_BLOCK_STATE_SCANNING, | ||
| 226 | /* Being scanned */ | ||
| 227 | |||
| 228 | YAFFS_BLOCK_STATE_NEEDS_SCANNING, | ||
| 229 | /* The block might have something on it (ie it is allocating or full, perhaps empty) | ||
| 230 | * but it needs to be scanned to determine its true state. | ||
| 231 | * This state is only valid during scanning. | ||
| 232 | * NB We tolerate empty because the pre-scanner might be incapable of deciding | ||
| 233 | * However, if this state is returned on a YAFFS2 device, then we expect a sequence number | ||
| 234 | */ | ||
| 235 | |||
| 236 | YAFFS_BLOCK_STATE_EMPTY, | ||
| 237 | /* This block is empty */ | ||
| 238 | |||
| 239 | YAFFS_BLOCK_STATE_ALLOCATING, | ||
| 240 | /* This block is partially allocated. | ||
| 241 | * At least one page holds valid data. | ||
| 242 | * This is the one currently being used for page | ||
| 243 | * allocation. Should never be more than one of these. | ||
| 244 | * If a block is only partially allocated at mount it is treated as full. | ||
| 245 | */ | ||
| 246 | |||
| 247 | YAFFS_BLOCK_STATE_FULL, | ||
| 248 | /* All the pages in this block have been allocated. | ||
| 249 | * If a block was only partially allocated when mounted we treat | ||
| 250 | * it as fully allocated. | ||
| 251 | */ | ||
| 252 | |||
| 253 | YAFFS_BLOCK_STATE_DIRTY, | ||
| 254 | /* The block was full and now all chunks have been deleted. | ||
| 255 | * Erase me, reuse me. | ||
| 256 | */ | ||
| 257 | |||
| 258 | YAFFS_BLOCK_STATE_CHECKPOINT, | ||
| 259 | /* This block is assigned to holding checkpoint data. */ | ||
| 260 | |||
| 261 | YAFFS_BLOCK_STATE_COLLECTING, | ||
| 262 | /* This block is being garbage collected */ | ||
| 263 | |||
| 264 | YAFFS_BLOCK_STATE_DEAD | ||
| 265 | /* This block has failed and is not in use */ | ||
| 266 | }; | ||
| 267 | |||
| 268 | #define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1) | ||
| 269 | |||
| 270 | struct yaffs_block_info { | ||
| 271 | |||
| 272 | int soft_del_pages:10; /* number of soft deleted pages */ | ||
| 273 | int pages_in_use:10; /* number of pages in use */ | ||
| 274 | unsigned block_state:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */ | ||
| 275 | u32 needs_retiring:1; /* Data has failed on this block, need to get valid data off */ | ||
| 276 | /* and retire the block. */ | ||
| 277 | u32 skip_erased_check:1; /* If this is set we can skip the erased check on this block */ | ||
| 278 | u32 gc_prioritise:1; /* An ECC check or blank check has failed on this block. | ||
| 279 | It should be prioritised for GC */ | ||
| 280 | u32 chunk_error_strikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */ | ||
| 281 | |||
| 282 | #ifdef CONFIG_YAFFS_YAFFS2 | ||
| 283 | u32 has_shrink_hdr:1; /* This block has at least one shrink object header */ | ||
| 284 | u32 seq_number; /* block sequence number for yaffs2 */ | ||
| 285 | #endif | ||
| 286 | |||
| 287 | }; | ||
| 288 | |||
| 289 | /* -------------------------- Object structure -------------------------------*/ | ||
| 290 | /* This is the object structure as stored on NAND */ | ||
| 291 | |||
| 292 | struct yaffs_obj_hdr { | ||
| 293 | enum yaffs_obj_type type; | ||
| 294 | |||
| 295 | /* Apply to everything */ | ||
| 296 | int parent_obj_id; | ||
| 297 | u16 sum_no_longer_used; /* checksum of name. No longer used */ | ||
| 298 | YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; | ||
| 299 | |||
| 300 | /* The following apply to directories, files, symlinks - not hard links */ | ||
| 301 | u32 yst_mode; /* protection */ | ||
| 302 | |||
| 303 | u32 yst_uid; | ||
| 304 | u32 yst_gid; | ||
| 305 | u32 yst_atime; | ||
| 306 | u32 yst_mtime; | ||
| 307 | u32 yst_ctime; | ||
| 308 | |||
| 309 | /* File size applies to files only */ | ||
| 310 | int file_size; | ||
| 311 | |||
| 312 | /* Equivalent object id applies to hard links only. */ | ||
| 313 | int equiv_id; | ||
| 314 | |||
| 315 | /* Alias is for symlinks only. */ | ||
| 316 | YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1]; | ||
| 317 | |||
| 318 | u32 yst_rdev; /* device stuff for block and char devices (major/min) */ | ||
| 319 | |||
| 320 | u32 win_ctime[2]; | ||
| 321 | u32 win_atime[2]; | ||
| 322 | u32 win_mtime[2]; | ||
| 323 | |||
| 324 | u32 inband_shadowed_obj_id; | ||
| 325 | u32 inband_is_shrink; | ||
| 326 | |||
| 327 | u32 reserved[2]; | ||
| 328 | int shadows_obj; /* This object header shadows the specified object if > 0 */ | ||
| 329 | |||
| 330 | /* is_shrink applies to object headers written when we shrink the file (ie resize) */ | ||
| 331 | u32 is_shrink; | ||
| 332 | |||
| 333 | }; | ||
| 334 | |||
| 335 | /*--------------------------- Tnode -------------------------- */ | ||
| 336 | |||
| 337 | struct yaffs_tnode { | ||
| 338 | struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL]; | ||
| 339 | }; | ||
| 340 | |||
| 341 | /*------------------------ Object -----------------------------*/ | ||
| 342 | /* An object can be one of: | ||
| 343 | * - a directory (no data, has children links | ||
| 344 | * - a regular file (data.... not prunes :->). | ||
| 345 | * - a symlink [symbolic link] (the alias). | ||
| 346 | * - a hard link | ||
| 347 | */ | ||
| 348 | |||
| 349 | struct yaffs_file_var { | ||
| 350 | u32 file_size; | ||
| 351 | u32 scanned_size; | ||
| 352 | u32 shrink_size; | ||
| 353 | int top_level; | ||
| 354 | struct yaffs_tnode *top; | ||
| 355 | }; | ||
| 356 | |||
| 357 | struct yaffs_dir_var { | ||
| 358 | struct list_head children; /* list of child links */ | ||
| 359 | struct list_head dirty; /* Entry for list of dirty directories */ | ||
| 360 | }; | ||
| 361 | |||
| 362 | struct yaffs_symlink_var { | ||
| 363 | YCHAR *alias; | ||
| 364 | }; | ||
| 365 | |||
| 366 | struct yaffs_hardlink_var { | ||
| 367 | struct yaffs_obj *equiv_obj; | ||
| 368 | u32 equiv_id; | ||
| 369 | }; | ||
| 370 | |||
| 371 | union yaffs_obj_var { | ||
| 372 | struct yaffs_file_var file_variant; | ||
| 373 | struct yaffs_dir_var dir_variant; | ||
| 374 | struct yaffs_symlink_var symlink_variant; | ||
| 375 | struct yaffs_hardlink_var hardlink_variant; | ||
| 376 | }; | ||
| 377 | |||
| 378 | struct yaffs_obj { | ||
| 379 | u8 deleted:1; /* This should only apply to unlinked files. */ | ||
| 380 | u8 soft_del:1; /* it has also been soft deleted */ | ||
| 381 | u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory. */ | ||
| 382 | u8 fake:1; /* A fake object has no presence on NAND. */ | ||
| 383 | u8 rename_allowed:1; /* Some objects are not allowed to be renamed. */ | ||
| 384 | u8 unlink_allowed:1; | ||
| 385 | u8 dirty:1; /* the object needs to be written to flash */ | ||
| 386 | u8 valid:1; /* When the file system is being loaded up, this | ||
| 387 | * object might be created before the data | ||
| 388 | * is available (ie. file data records appear before the header). | ||
| 389 | */ | ||
| 390 | u8 lazy_loaded:1; /* This object has been lazy loaded and is missing some detail */ | ||
| 391 | |||
| 392 | u8 defered_free:1; /* For Linux kernel. Object is removed from NAND, but is | ||
| 393 | * still in the inode cache. Free of object is defered. | ||
| 394 | * until the inode is released. | ||
| 395 | */ | ||
| 396 | u8 being_created:1; /* This object is still being created so skip some checks. */ | ||
| 397 | u8 is_shadowed:1; /* This object is shadowed on the way to being renamed. */ | ||
| 398 | |||
| 399 | u8 xattr_known:1; /* We know if this has object has xattribs or not. */ | ||
| 400 | u8 has_xattr:1; /* This object has xattribs. Valid if xattr_known. */ | ||
| 401 | |||
| 402 | u8 serial; /* serial number of chunk in NAND. Cached here */ | ||
| 403 | u16 sum; /* sum of the name to speed searching */ | ||
| 404 | |||
| 405 | struct yaffs_dev *my_dev; /* The device I'm on */ | ||
| 406 | |||
| 407 | struct list_head hash_link; /* list of objects in this hash bucket */ | ||
| 408 | |||
| 409 | struct list_head hard_links; /* all the equivalent hard linked objects */ | ||
| 410 | |||
| 411 | /* directory structure stuff */ | ||
| 412 | /* also used for linking up the free list */ | ||
| 413 | struct yaffs_obj *parent; | ||
| 414 | struct list_head siblings; | ||
| 415 | |||
| 416 | /* Where's my object header in NAND? */ | ||
| 417 | int hdr_chunk; | ||
| 418 | |||
| 419 | int n_data_chunks; /* Number of data chunks attached to the file. */ | ||
| 420 | |||
| 421 | u32 obj_id; /* the object id value */ | ||
| 422 | |||
| 423 | u32 yst_mode; | ||
| 424 | |||
| 425 | #ifndef CONFIG_YAFFS_NO_SHORT_NAMES | ||
| 426 | YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1]; | ||
| 427 | #endif | ||
| 428 | |||
| 429 | #ifdef CONFIG_YAFFS_WINCE | ||
| 430 | u32 win_ctime[2]; | ||
| 431 | u32 win_mtime[2]; | ||
| 432 | u32 win_atime[2]; | ||
| 433 | #else | ||
| 434 | u32 yst_uid; | ||
| 435 | u32 yst_gid; | ||
| 436 | u32 yst_atime; | ||
| 437 | u32 yst_mtime; | ||
| 438 | u32 yst_ctime; | ||
| 439 | #endif | ||
| 440 | |||
| 441 | u32 yst_rdev; | ||
| 442 | |||
| 443 | void *my_inode; | ||
| 444 | |||
| 445 | enum yaffs_obj_type variant_type; | ||
| 446 | |||
| 447 | union yaffs_obj_var variant; | ||
| 448 | |||
| 449 | }; | ||
| 450 | |||
| 451 | struct yaffs_obj_bucket { | ||
| 452 | struct list_head list; | ||
| 453 | int count; | ||
| 454 | }; | ||
| 455 | |||
| 456 | /* yaffs_checkpt_obj holds the definition of an object as dumped | ||
| 457 | * by checkpointing. | ||
| 458 | */ | ||
| 459 | |||
| 460 | struct yaffs_checkpt_obj { | ||
| 461 | int struct_type; | ||
| 462 | u32 obj_id; | ||
| 463 | u32 parent_id; | ||
| 464 | int hdr_chunk; | ||
| 465 | enum yaffs_obj_type variant_type:3; | ||
| 466 | u8 deleted:1; | ||
| 467 | u8 soft_del:1; | ||
| 468 | u8 unlinked:1; | ||
| 469 | u8 fake:1; | ||
| 470 | u8 rename_allowed:1; | ||
| 471 | u8 unlink_allowed:1; | ||
| 472 | u8 serial; | ||
| 473 | int n_data_chunks; | ||
| 474 | u32 size_or_equiv_obj; | ||
| 475 | }; | ||
| 476 | |||
| 477 | /*--------------------- Temporary buffers ---------------- | ||
| 478 | * | ||
| 479 | * These are chunk-sized working buffers. Each device has a few | ||
| 480 | */ | ||
| 481 | |||
| 482 | struct yaffs_buffer { | ||
| 483 | u8 *buffer; | ||
| 484 | int line; /* track from whence this buffer was allocated */ | ||
| 485 | int max_line; | ||
| 486 | }; | ||
| 487 | |||
| 488 | /*----------------- Device ---------------------------------*/ | ||
| 489 | |||
| 490 | struct yaffs_param { | ||
| 491 | const YCHAR *name; | ||
| 492 | |||
| 493 | /* | ||
| 494 | * Entry parameters set up way early. Yaffs sets up the rest. | ||
| 495 | * The structure should be zeroed out before use so that unused | ||
| 496 | * and defualt values are zero. | ||
| 497 | */ | ||
| 498 | |||
| 499 | int inband_tags; /* Use unband tags */ | ||
| 500 | u32 total_bytes_per_chunk; /* Should be >= 512, does not need to be a power of 2 */ | ||
| 501 | int chunks_per_block; /* does not need to be a power of 2 */ | ||
| 502 | int spare_bytes_per_chunk; /* spare area size */ | ||
| 503 | int start_block; /* Start block we're allowed to use */ | ||
| 504 | int end_block; /* End block we're allowed to use */ | ||
| 505 | int n_reserved_blocks; /* We want this tuneable so that we can reduce */ | ||
| 506 | /* reserved blocks on NOR and RAM. */ | ||
| 507 | |||
| 508 | int n_caches; /* If <= 0, then short op caching is disabled, else | ||
| 509 | * the number of short op caches (don't use too many). | ||
| 510 | * 10 to 20 is a good bet. | ||
| 511 | */ | ||
| 512 | int use_nand_ecc; /* Flag to decide whether or not to use NANDECC on data (yaffs1) */ | ||
| 513 | int no_tags_ecc; /* Flag to decide whether or not to do ECC on packed tags (yaffs2) */ | ||
| 514 | |||
| 515 | int is_yaffs2; /* Use yaffs2 mode on this device */ | ||
| 516 | |||
| 517 | int empty_lost_n_found; /* Auto-empty lost+found directory on mount */ | ||
| 518 | |||
| 519 | int refresh_period; /* How often we should check to do a block refresh */ | ||
| 520 | |||
| 521 | /* Checkpoint control. Can be set before or after initialisation */ | ||
| 522 | u8 skip_checkpt_rd; | ||
| 523 | u8 skip_checkpt_wr; | ||
| 524 | |||
| 525 | int enable_xattr; /* Enable xattribs */ | ||
| 526 | |||
| 527 | /* NAND access functions (Must be set before calling YAFFS) */ | ||
| 528 | |||
| 529 | int (*write_chunk_fn) (struct yaffs_dev * dev, | ||
| 530 | int nand_chunk, const u8 * data, | ||
| 531 | const struct yaffs_spare * spare); | ||
| 532 | int (*read_chunk_fn) (struct yaffs_dev * dev, | ||
| 533 | int nand_chunk, u8 * data, | ||
| 534 | struct yaffs_spare * spare); | ||
| 535 | int (*erase_fn) (struct yaffs_dev * dev, int flash_block); | ||
| 536 | int (*initialise_flash_fn) (struct yaffs_dev * dev); | ||
| 537 | int (*deinitialise_flash_fn) (struct yaffs_dev * dev); | ||
| 538 | |||
| 539 | #ifdef CONFIG_YAFFS_YAFFS2 | ||
| 540 | int (*write_chunk_tags_fn) (struct yaffs_dev * dev, | ||
| 541 | int nand_chunk, const u8 * data, | ||
| 542 | const struct yaffs_ext_tags * tags); | ||
| 543 | int (*read_chunk_tags_fn) (struct yaffs_dev * dev, | ||
| 544 | int nand_chunk, u8 * data, | ||
| 545 | struct yaffs_ext_tags * tags); | ||
| 546 | int (*bad_block_fn) (struct yaffs_dev * dev, int block_no); | ||
| 547 | int (*query_block_fn) (struct yaffs_dev * dev, int block_no, | ||
| 548 | enum yaffs_block_state * state, | ||
| 549 | u32 * seq_number); | ||
| 550 | #endif | ||
| 551 | |||
| 552 | /* The remove_obj_fn function must be supplied by OS flavours that | ||
| 553 | * need it. | ||
| 554 | * yaffs direct uses it to implement the faster readdir. | ||
| 555 | * Linux uses it to protect the directory during unlocking. | ||
| 556 | */ | ||
| 557 | void (*remove_obj_fn) (struct yaffs_obj * obj); | ||
| 558 | |||
| 559 | /* Callback to mark the superblock dirty */ | ||
| 560 | void (*sb_dirty_fn) (struct yaffs_dev * dev); | ||
| 561 | |||
| 562 | /* Callback to control garbage collection. */ | ||
| 563 | unsigned (*gc_control) (struct yaffs_dev * dev); | ||
| 564 | |||
| 565 | /* Debug control flags. Don't use unless you know what you're doing */ | ||
| 566 | int use_header_file_size; /* Flag to determine if we should use file sizes from the header */ | ||
| 567 | int disable_lazy_load; /* Disable lazy loading on this device */ | ||
| 568 | int wide_tnodes_disabled; /* Set to disable wide tnodes */ | ||
| 569 | int disable_soft_del; /* yaffs 1 only: Set to disable the use of softdeletion. */ | ||
| 570 | |||
| 571 | int defered_dir_update; /* Set to defer directory updates */ | ||
| 572 | |||
| 573 | #ifdef CONFIG_YAFFS_AUTO_UNICODE | ||
| 574 | int auto_unicode; | ||
| 575 | #endif | ||
| 576 | int always_check_erased; /* Force chunk erased check always on */ | ||
| 577 | }; | ||
| 578 | |||
| 579 | struct yaffs_dev { | ||
| 580 | struct yaffs_param param; | ||
| 581 | |||
| 582 | /* Context storage. Holds extra OS specific data for this device */ | ||
| 583 | |||
| 584 | void *os_context; | ||
| 585 | void *driver_context; | ||
| 586 | |||
| 587 | struct list_head dev_list; | ||
| 588 | |||
| 589 | /* Runtime parameters. Set up by YAFFS. */ | ||
| 590 | int data_bytes_per_chunk; | ||
| 591 | |||
| 592 | /* Non-wide tnode stuff */ | ||
| 593 | u16 chunk_grp_bits; /* Number of bits that need to be resolved if | ||
| 594 | * the tnodes are not wide enough. | ||
| 595 | */ | ||
| 596 | u16 chunk_grp_size; /* == 2^^chunk_grp_bits */ | ||
| 597 | |||
| 598 | /* Stuff to support wide tnodes */ | ||
| 599 | u32 tnode_width; | ||
| 600 | u32 tnode_mask; | ||
| 601 | u32 tnode_size; | ||
| 602 | |||
| 603 | /* Stuff for figuring out file offset to chunk conversions */ | ||
| 604 | u32 chunk_shift; /* Shift value */ | ||
| 605 | u32 chunk_div; /* Divisor after shifting: 1 for power-of-2 sizes */ | ||
| 606 | u32 chunk_mask; /* Mask to use for power-of-2 case */ | ||
| 607 | |||
| 608 | int is_mounted; | ||
| 609 | int read_only; | ||
| 610 | int is_checkpointed; | ||
| 611 | |||
| 612 | /* Stuff to support block offsetting to support start block zero */ | ||
| 613 | int internal_start_block; | ||
| 614 | int internal_end_block; | ||
| 615 | int block_offset; | ||
| 616 | int chunk_offset; | ||
| 617 | |||
| 618 | /* Runtime checkpointing stuff */ | ||
| 619 | int checkpt_page_seq; /* running sequence number of checkpoint pages */ | ||
| 620 | int checkpt_byte_count; | ||
| 621 | int checkpt_byte_offs; | ||
| 622 | u8 *checkpt_buffer; | ||
| 623 | int checkpt_open_write; | ||
| 624 | int blocks_in_checkpt; | ||
| 625 | int checkpt_cur_chunk; | ||
| 626 | int checkpt_cur_block; | ||
| 627 | int checkpt_next_block; | ||
| 628 | int *checkpt_block_list; | ||
| 629 | int checkpt_max_blocks; | ||
| 630 | u32 checkpt_sum; | ||
| 631 | u32 checkpt_xor; | ||
| 632 | |||
| 633 | int checkpoint_blocks_required; /* Number of blocks needed to store current checkpoint set */ | ||
| 634 | |||
| 635 | /* Block Info */ | ||
| 636 | struct yaffs_block_info *block_info; | ||
| 637 | u8 *chunk_bits; /* bitmap of chunks in use */ | ||
| 638 | unsigned block_info_alt:1; /* was allocated using alternative strategy */ | ||
| 639 | unsigned chunk_bits_alt:1; /* was allocated using alternative strategy */ | ||
| 640 | int chunk_bit_stride; /* Number of bytes of chunk_bits per block. | ||
| 641 | * Must be consistent with chunks_per_block. | ||
| 642 | */ | ||
| 643 | |||
| 644 | int n_erased_blocks; | ||
| 645 | int alloc_block; /* Current block being allocated off */ | ||
| 646 | u32 alloc_page; | ||
| 647 | int alloc_block_finder; /* Used to search for next allocation block */ | ||
| 648 | |||
| 649 | /* Object and Tnode memory management */ | ||
| 650 | void *allocator; | ||
| 651 | int n_obj; | ||
| 652 | int n_tnodes; | ||
| 653 | |||
| 654 | int n_hardlinks; | ||
| 655 | |||
| 656 | struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS]; | ||
| 657 | u32 bucket_finder; | ||
| 658 | |||
| 659 | int n_free_chunks; | ||
| 660 | |||
| 661 | /* Garbage collection control */ | ||
| 662 | u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */ | ||
| 663 | u32 n_clean_ups; | ||
| 664 | |||
| 665 | unsigned has_pending_prioritised_gc; /* We think this device might have pending prioritised gcs */ | ||
| 666 | unsigned gc_disable; | ||
| 667 | unsigned gc_block_finder; | ||
| 668 | unsigned gc_dirtiest; | ||
| 669 | unsigned gc_pages_in_use; | ||
| 670 | unsigned gc_not_done; | ||
| 671 | unsigned gc_block; | ||
| 672 | unsigned gc_chunk; | ||
| 673 | unsigned gc_skip; | ||
| 674 | |||
| 675 | /* Special directories */ | ||
| 676 | struct yaffs_obj *root_dir; | ||
| 677 | struct yaffs_obj *lost_n_found; | ||
| 678 | |||
| 679 | /* Buffer areas for storing data to recover from write failures TODO | ||
| 680 | * u8 buffered_data[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK]; | ||
| 681 | * struct yaffs_spare buffered_spare[YAFFS_CHUNKS_PER_BLOCK]; | ||
| 682 | */ | ||
| 683 | |||
| 684 | int buffered_block; /* Which block is buffered here? */ | ||
| 685 | int doing_buffered_block_rewrite; | ||
| 686 | |||
| 687 | struct yaffs_cache *cache; | ||
| 688 | int cache_last_use; | ||
| 689 | |||
| 690 | /* Stuff for background deletion and unlinked files. */ | ||
| 691 | struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted files live. */ | ||
| 692 | struct yaffs_obj *del_dir; /* Directory where deleted objects are sent to disappear. */ | ||
| 693 | struct yaffs_obj *unlinked_deletion; /* Current file being background deleted. */ | ||
| 694 | int n_deleted_files; /* Count of files awaiting deletion; */ | ||
| 695 | int n_unlinked_files; /* Count of unlinked files. */ | ||
| 696 | int n_bg_deletions; /* Count of background deletions. */ | ||
| 697 | |||
| 698 | /* Temporary buffer management */ | ||
| 699 | struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS]; | ||
| 700 | int max_temp; | ||
| 701 | int temp_in_use; | ||
| 702 | int unmanaged_buffer_allocs; | ||
| 703 | int unmanaged_buffer_deallocs; | ||
| 704 | |||
| 705 | /* yaffs2 runtime stuff */ | ||
| 706 | unsigned seq_number; /* Sequence number of currently allocating block */ | ||
| 707 | unsigned oldest_dirty_seq; | ||
| 708 | unsigned oldest_dirty_block; | ||
| 709 | |||
| 710 | /* Block refreshing */ | ||
| 711 | int refresh_skip; /* A skip down counter. Refresh happens when this gets to zero. */ | ||
| 712 | |||
| 713 | /* Dirty directory handling */ | ||
| 714 | struct list_head dirty_dirs; /* List of dirty directories */ | ||
| 715 | |||
| 716 | /* Statistcs */ | ||
| 717 | u32 n_page_writes; | ||
| 718 | u32 n_page_reads; | ||
| 719 | u32 n_erasures; | ||
| 720 | u32 n_erase_failures; | ||
| 721 | u32 n_gc_copies; | ||
| 722 | u32 all_gcs; | ||
| 723 | u32 passive_gc_count; | ||
| 724 | u32 oldest_dirty_gc_count; | ||
| 725 | u32 n_gc_blocks; | ||
| 726 | u32 bg_gcs; | ||
| 727 | u32 n_retired_writes; | ||
| 728 | u32 n_retired_blocks; | ||
| 729 | u32 n_ecc_fixed; | ||
| 730 | u32 n_ecc_unfixed; | ||
| 731 | u32 n_tags_ecc_fixed; | ||
| 732 | u32 n_tags_ecc_unfixed; | ||
| 733 | u32 n_deletions; | ||
| 734 | u32 n_unmarked_deletions; | ||
| 735 | u32 refresh_count; | ||
| 736 | u32 cache_hits; | ||
| 737 | |||
| 738 | }; | ||
| 739 | |||
| 740 | /* The CheckpointDevice structure holds the device information that changes at runtime and | ||
| 741 | * must be preserved over unmount/mount cycles. | ||
| 742 | */ | ||
| 743 | struct yaffs_checkpt_dev { | ||
| 744 | int struct_type; | ||
| 745 | int n_erased_blocks; | ||
| 746 | int alloc_block; /* Current block being allocated off */ | ||
| 747 | u32 alloc_page; | ||
| 748 | int n_free_chunks; | ||
| 749 | |||
| 750 | int n_deleted_files; /* Count of files awaiting deletion; */ | ||
| 751 | int n_unlinked_files; /* Count of unlinked files. */ | ||
| 752 | int n_bg_deletions; /* Count of background deletions. */ | ||
| 753 | |||
| 754 | /* yaffs2 runtime stuff */ | ||
| 755 | unsigned seq_number; /* Sequence number of currently allocating block */ | ||
| 756 | |||
| 757 | }; | ||
| 758 | |||
| 759 | struct yaffs_checkpt_validity { | ||
| 760 | int struct_type; | ||
| 761 | u32 magic; | ||
| 762 | u32 version; | ||
| 763 | u32 head; | ||
| 764 | }; | ||
| 765 | |||
| 766 | struct yaffs_shadow_fixer { | ||
| 767 | int obj_id; | ||
| 768 | int shadowed_id; | ||
| 769 | struct yaffs_shadow_fixer *next; | ||
| 770 | }; | ||
| 771 | |||
| 772 | /* Structure for doing xattr modifications */ | ||
| 773 | struct yaffs_xattr_mod { | ||
| 774 | int set; /* If 0 then this is a deletion */ | ||
| 775 | const YCHAR *name; | ||
| 776 | const void *data; | ||
| 777 | int size; | ||
| 778 | int flags; | ||
| 779 | int result; | ||
| 780 | }; | ||
| 781 | |||
| 782 | /*----------------------- YAFFS Functions -----------------------*/ | ||
| 783 | |||
| 784 | int yaffs_guts_initialise(struct yaffs_dev *dev); | ||
| 785 | void yaffs_deinitialise(struct yaffs_dev *dev); | ||
| 786 | |||
| 787 | int yaffs_get_n_free_chunks(struct yaffs_dev *dev); | ||
| 788 | |||
| 789 | int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name, | ||
| 790 | struct yaffs_obj *new_dir, const YCHAR * new_name); | ||
| 791 | |||
| 792 | int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name); | ||
| 793 | int yaffs_del_obj(struct yaffs_obj *obj); | ||
| 794 | |||
| 795 | int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size); | ||
| 796 | int yaffs_get_obj_length(struct yaffs_obj *obj); | ||
| 797 | int yaffs_get_obj_inode(struct yaffs_obj *obj); | ||
| 798 | unsigned yaffs_get_obj_type(struct yaffs_obj *obj); | ||
| 799 | int yaffs_get_obj_link_count(struct yaffs_obj *obj); | ||
| 800 | |||
| 801 | /* File operations */ | ||
| 802 | int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset, | ||
| 803 | int n_bytes); | ||
| 804 | int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset, | ||
| 805 | int n_bytes, int write_trhrough); | ||
| 806 | int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size); | ||
| 807 | |||
| 808 | struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, | ||
| 809 | const YCHAR * name, u32 mode, u32 uid, | ||
| 810 | u32 gid); | ||
| 811 | |||
| 812 | int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync); | ||
| 813 | |||
| 814 | /* Flushing and checkpointing */ | ||
| 815 | void yaffs_flush_whole_cache(struct yaffs_dev *dev); | ||
| 816 | |||
| 817 | int yaffs_checkpoint_save(struct yaffs_dev *dev); | ||
| 818 | int yaffs_checkpoint_restore(struct yaffs_dev *dev); | ||
| 819 | |||
| 820 | /* Directory operations */ | ||
| 821 | struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name, | ||
| 822 | u32 mode, u32 uid, u32 gid); | ||
| 823 | struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir, | ||
| 824 | const YCHAR * name); | ||
| 825 | struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number); | ||
| 826 | |||
| 827 | /* Link operations */ | ||
| 828 | struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name, | ||
| 829 | struct yaffs_obj *equiv_obj); | ||
| 830 | |||
| 831 | struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj); | ||
| 832 | |||
| 833 | /* Symlink operations */ | ||
| 834 | struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, | ||
| 835 | const YCHAR * name, u32 mode, u32 uid, | ||
| 836 | u32 gid, const YCHAR * alias); | ||
| 837 | YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj); | ||
| 838 | |||
| 839 | /* Special inodes (fifos, sockets and devices) */ | ||
| 840 | struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, | ||
| 841 | const YCHAR * name, u32 mode, u32 uid, | ||
| 842 | u32 gid, u32 rdev); | ||
| 843 | |||
| 844 | int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name, | ||
| 845 | const void *value, int size, int flags); | ||
| 846 | int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value, | ||
| 847 | int size); | ||
| 848 | int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size); | ||
| 849 | int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name); | ||
| 850 | |||
| 851 | /* Special directories */ | ||
| 852 | struct yaffs_obj *yaffs_root(struct yaffs_dev *dev); | ||
| 853 | struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev); | ||
| 854 | |||
| 855 | void yaffs_handle_defered_free(struct yaffs_obj *obj); | ||
| 856 | |||
| 857 | void yaffs_update_dirty_dirs(struct yaffs_dev *dev); | ||
| 858 | |||
| 859 | int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency); | ||
| 860 | |||
| 861 | /* Debug dump */ | ||
| 862 | int yaffs_dump_obj(struct yaffs_obj *obj); | ||
| 863 | |||
| 864 | void yaffs_guts_test(struct yaffs_dev *dev); | ||
| 865 | |||
| 866 | /* A few useful functions to be used within the core files*/ | ||
| 867 | void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, | ||
| 868 | int lyn); | ||
| 869 | int yaffs_check_ff(u8 * buffer, int n_bytes); | ||
| 870 | void yaffs_handle_chunk_error(struct yaffs_dev *dev, | ||
| 871 | struct yaffs_block_info *bi); | ||
| 872 | |||
| 873 | u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev, int line_no); | ||
| 874 | void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no); | ||
| 875 | |||
| 876 | struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev, | ||
| 877 | int number, | ||
| 878 | enum yaffs_obj_type type); | ||
| 879 | int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, | ||
| 880 | int nand_chunk, int in_scan); | ||
| 881 | void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name); | ||
| 882 | void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj, | ||
| 883 | const struct yaffs_obj_hdr *oh); | ||
| 884 | void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj); | ||
| 885 | YCHAR *yaffs_clone_str(const YCHAR * str); | ||
| 886 | void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list); | ||
| 887 | void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no); | ||
| 888 | int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, | ||
| 889 | int force, int is_shrink, int shadows, | ||
| 890 | struct yaffs_xattr_mod *xop); | ||
| 891 | void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id, | ||
| 892 | int backward_scanning); | ||
| 893 | int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks); | ||
| 894 | struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev); | ||
| 895 | struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, | ||
| 896 | struct yaffs_file_var *file_struct, | ||
| 897 | u32 chunk_id, | ||
| 898 | struct yaffs_tnode *passed_tn); | ||
| 899 | |||
| 900 | int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset, | ||
| 901 | int n_bytes, int write_trhrough); | ||
| 902 | void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size); | ||
| 903 | void yaffs_skip_rest_of_block(struct yaffs_dev *dev); | ||
| 904 | |||
| 905 | int yaffs_count_free_chunks(struct yaffs_dev *dev); | ||
| 906 | |||
| 907 | struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev, | ||
| 908 | struct yaffs_file_var *file_struct, | ||
| 909 | u32 chunk_id); | ||
| 910 | |||
| 911 | u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn, | ||
| 912 | unsigned pos); | ||
| 913 | |||
| 914 | int yaffs_is_non_empty_dir(struct yaffs_obj *obj); | ||
| 915 | #endif | ||
diff --git a/fs/yaffs2/yaffs_linux.h b/fs/yaffs2/yaffs_linux.h new file mode 100644 index 00000000000..3b508cbc4e8 --- /dev/null +++ b/fs/yaffs2/yaffs_linux.h | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_LINUX_H__ | ||
| 17 | #define __YAFFS_LINUX_H__ | ||
| 18 | |||
| 19 | #include "yportenv.h" | ||
| 20 | |||
| 21 | struct yaffs_linux_context { | ||
| 22 | struct list_head context_list; /* List of these we have mounted */ | ||
| 23 | struct yaffs_dev *dev; | ||
| 24 | struct super_block *super; | ||
| 25 | struct task_struct *bg_thread; /* Background thread for this device */ | ||
| 26 | int bg_running; | ||
| 27 | struct mutex gross_lock; /* Gross locking mutex*/ | ||
| 28 | u8 *spare_buffer; /* For mtdif2 use. Don't know the size of the buffer | ||
| 29 | * at compile time so we have to allocate it. | ||
| 30 | */ | ||
| 31 | struct list_head search_contexts; | ||
| 32 | void (*put_super_fn) (struct super_block * sb); | ||
| 33 | |||
| 34 | struct task_struct *readdir_process; | ||
| 35 | unsigned mount_id; | ||
| 36 | }; | ||
| 37 | |||
| 38 | #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context)) | ||
| 39 | #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context)) | ||
| 40 | |||
| 41 | #endif | ||
diff --git a/fs/yaffs2/yaffs_mtdif.c b/fs/yaffs2/yaffs_mtdif.c new file mode 100644 index 00000000000..7cf53b3d91b --- /dev/null +++ b/fs/yaffs2/yaffs_mtdif.c | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yportenv.h" | ||
| 15 | |||
| 16 | #include "yaffs_mtdif.h" | ||
| 17 | |||
| 18 | #include "linux/mtd/mtd.h" | ||
| 19 | #include "linux/types.h" | ||
| 20 | #include "linux/time.h" | ||
| 21 | #include "linux/mtd/nand.h" | ||
| 22 | |||
| 23 | #include "yaffs_linux.h" | ||
| 24 | |||
| 25 | int nandmtd_erase_block(struct yaffs_dev *dev, int block_no) | ||
| 26 | { | ||
| 27 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 28 | u32 addr = | ||
| 29 | ((loff_t) block_no) * dev->param.total_bytes_per_chunk | ||
| 30 | * dev->param.chunks_per_block; | ||
| 31 | struct erase_info ei; | ||
| 32 | |||
| 33 | int retval = 0; | ||
| 34 | |||
| 35 | ei.mtd = mtd; | ||
| 36 | ei.addr = addr; | ||
| 37 | ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block; | ||
| 38 | ei.time = 1000; | ||
| 39 | ei.retries = 2; | ||
| 40 | ei.callback = NULL; | ||
| 41 | ei.priv = (u_long) dev; | ||
| 42 | |||
| 43 | retval = mtd->erase(mtd, &ei); | ||
| 44 | |||
| 45 | if (retval == 0) | ||
| 46 | return YAFFS_OK; | ||
| 47 | else | ||
| 48 | return YAFFS_FAIL; | ||
| 49 | } | ||
| 50 | |||
| 51 | int nandmtd_initialise(struct yaffs_dev *dev) | ||
| 52 | { | ||
| 53 | return YAFFS_OK; | ||
| 54 | } | ||
diff --git a/fs/yaffs2/yaffs_mtdif.h b/fs/yaffs2/yaffs_mtdif.h new file mode 100644 index 00000000000..666507417fe --- /dev/null +++ b/fs/yaffs2/yaffs_mtdif.h | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_MTDIF_H__ | ||
| 17 | #define __YAFFS_MTDIF_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | int nandmtd_erase_block(struct yaffs_dev *dev, int block_no); | ||
| 22 | int nandmtd_initialise(struct yaffs_dev *dev); | ||
| 23 | #endif | ||
diff --git a/fs/yaffs2/yaffs_mtdif1.c b/fs/yaffs2/yaffs_mtdif1.c new file mode 100644 index 00000000000..51083695eb3 --- /dev/null +++ b/fs/yaffs2/yaffs_mtdif1.c | |||
| @@ -0,0 +1,330 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another FFS. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | /* | ||
| 15 | * This module provides the interface between yaffs_nand.c and the | ||
| 16 | * MTD API. This version is used when the MTD interface supports the | ||
| 17 | * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17, | ||
| 18 | * and we have small-page NAND device. | ||
| 19 | * | ||
| 20 | * These functions are invoked via function pointers in yaffs_nand.c. | ||
| 21 | * This replaces functionality provided by functions in yaffs_mtdif.c | ||
| 22 | * and the yaffs_tags compatability functions in yaffs_tagscompat.c that are | ||
| 23 | * called in yaffs_mtdif.c when the function pointers are NULL. | ||
| 24 | * We assume the MTD layer is performing ECC (use_nand_ecc is true). | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "yportenv.h" | ||
| 28 | #include "yaffs_trace.h" | ||
| 29 | #include "yaffs_guts.h" | ||
| 30 | #include "yaffs_packedtags1.h" | ||
| 31 | #include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */ | ||
| 32 | #include "yaffs_linux.h" | ||
| 33 | |||
| 34 | #include "linux/kernel.h" | ||
| 35 | #include "linux/version.h" | ||
| 36 | #include "linux/types.h" | ||
| 37 | #include "linux/mtd/mtd.h" | ||
| 38 | |||
| 39 | #ifndef CONFIG_YAFFS_9BYTE_TAGS | ||
| 40 | # define YTAG1_SIZE 8 | ||
| 41 | #else | ||
| 42 | # define YTAG1_SIZE 9 | ||
| 43 | #endif | ||
| 44 | |||
| 45 | /* Write a chunk (page) of data to NAND. | ||
| 46 | * | ||
| 47 | * Caller always provides ExtendedTags data which are converted to a more | ||
| 48 | * compact (packed) form for storage in NAND. A mini-ECC runs over the | ||
| 49 | * contents of the tags meta-data; used to valid the tags when read. | ||
| 50 | * | ||
| 51 | * - Pack ExtendedTags to packed_tags1 form | ||
| 52 | * - Compute mini-ECC for packed_tags1 | ||
| 53 | * - Write data and packed tags to NAND. | ||
| 54 | * | ||
| 55 | * Note: Due to the use of the packed_tags1 meta-data which does not include | ||
| 56 | * a full sequence number (as found in the larger packed_tags2 form) it is | ||
| 57 | * necessary for Yaffs to re-write a chunk/page (just once) to mark it as | ||
| 58 | * discarded and dirty. This is not ideal: newer NAND parts are supposed | ||
| 59 | * to be written just once. When Yaffs performs this operation, this | ||
| 60 | * function is called with a NULL data pointer -- calling MTD write_oob | ||
| 61 | * without data is valid usage (2.6.17). | ||
| 62 | * | ||
| 63 | * Any underlying MTD error results in YAFFS_FAIL. | ||
| 64 | * Returns YAFFS_OK or YAFFS_FAIL. | ||
| 65 | */ | ||
| 66 | int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, | ||
| 67 | int nand_chunk, const u8 * data, | ||
| 68 | const struct yaffs_ext_tags *etags) | ||
| 69 | { | ||
| 70 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 71 | int chunk_bytes = dev->data_bytes_per_chunk; | ||
| 72 | loff_t addr = ((loff_t) nand_chunk) * chunk_bytes; | ||
| 73 | struct mtd_oob_ops ops; | ||
| 74 | struct yaffs_packed_tags1 pt1; | ||
| 75 | int retval; | ||
| 76 | |||
| 77 | /* we assume that packed_tags1 and struct yaffs_tags are compatible */ | ||
| 78 | compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12); | ||
| 79 | compile_time_assertion(sizeof(struct yaffs_tags) == 8); | ||
| 80 | |||
| 81 | yaffs_pack_tags1(&pt1, etags); | ||
| 82 | yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1); | ||
| 83 | |||
| 84 | /* When deleting a chunk, the upper layer provides only skeletal | ||
| 85 | * etags, one with is_deleted set. However, we need to update the | ||
| 86 | * tags, not erase them completely. So we use the NAND write property | ||
| 87 | * that only zeroed-bits stick and set tag bytes to all-ones and | ||
| 88 | * zero just the (not) deleted bit. | ||
| 89 | */ | ||
| 90 | #ifndef CONFIG_YAFFS_9BYTE_TAGS | ||
| 91 | if (etags->is_deleted) { | ||
| 92 | memset(&pt1, 0xff, 8); | ||
| 93 | /* clear delete status bit to indicate deleted */ | ||
| 94 | pt1.deleted = 0; | ||
| 95 | } | ||
| 96 | #else | ||
| 97 | ((u8 *) & pt1)[8] = 0xff; | ||
| 98 | if (etags->is_deleted) { | ||
| 99 | memset(&pt1, 0xff, 8); | ||
| 100 | /* zero page_status byte to indicate deleted */ | ||
| 101 | ((u8 *) & pt1)[8] = 0; | ||
| 102 | } | ||
| 103 | #endif | ||
| 104 | |||
| 105 | memset(&ops, 0, sizeof(ops)); | ||
| 106 | ops.mode = MTD_OOB_AUTO; | ||
| 107 | ops.len = (data) ? chunk_bytes : 0; | ||
| 108 | ops.ooblen = YTAG1_SIZE; | ||
| 109 | ops.datbuf = (u8 *) data; | ||
| 110 | ops.oobbuf = (u8 *) & pt1; | ||
| 111 | |||
| 112 | retval = mtd->write_oob(mtd, addr, &ops); | ||
| 113 | if (retval) { | ||
| 114 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 115 | "write_oob failed, chunk %d, mtd error %d", | ||
| 116 | nand_chunk, retval); | ||
| 117 | } | ||
| 118 | return retval ? YAFFS_FAIL : YAFFS_OK; | ||
| 119 | } | ||
| 120 | |||
| 121 | /* Return with empty ExtendedTags but add ecc_result. | ||
| 122 | */ | ||
| 123 | static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval) | ||
| 124 | { | ||
| 125 | if (etags) { | ||
| 126 | memset(etags, 0, sizeof(*etags)); | ||
| 127 | etags->ecc_result = ecc_result; | ||
| 128 | } | ||
| 129 | return retval; | ||
| 130 | } | ||
| 131 | |||
| 132 | /* Read a chunk (page) from NAND. | ||
| 133 | * | ||
| 134 | * Caller expects ExtendedTags data to be usable even on error; that is, | ||
| 135 | * all members except ecc_result and block_bad are zeroed. | ||
| 136 | * | ||
| 137 | * - Check ECC results for data (if applicable) | ||
| 138 | * - Check for blank/erased block (return empty ExtendedTags if blank) | ||
| 139 | * - Check the packed_tags1 mini-ECC (correct if necessary/possible) | ||
| 140 | * - Convert packed_tags1 to ExtendedTags | ||
| 141 | * - Update ecc_result and block_bad members to refect state. | ||
| 142 | * | ||
| 143 | * Returns YAFFS_OK or YAFFS_FAIL. | ||
| 144 | */ | ||
| 145 | int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, | ||
| 146 | int nand_chunk, u8 * data, | ||
| 147 | struct yaffs_ext_tags *etags) | ||
| 148 | { | ||
| 149 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 150 | int chunk_bytes = dev->data_bytes_per_chunk; | ||
| 151 | loff_t addr = ((loff_t) nand_chunk) * chunk_bytes; | ||
| 152 | int eccres = YAFFS_ECC_RESULT_NO_ERROR; | ||
| 153 | struct mtd_oob_ops ops; | ||
| 154 | struct yaffs_packed_tags1 pt1; | ||
| 155 | int retval; | ||
| 156 | int deleted; | ||
| 157 | |||
| 158 | memset(&ops, 0, sizeof(ops)); | ||
| 159 | ops.mode = MTD_OOB_AUTO; | ||
| 160 | ops.len = (data) ? chunk_bytes : 0; | ||
| 161 | ops.ooblen = YTAG1_SIZE; | ||
| 162 | ops.datbuf = data; | ||
| 163 | ops.oobbuf = (u8 *) & pt1; | ||
| 164 | |||
| 165 | /* Read page and oob using MTD. | ||
| 166 | * Check status and determine ECC result. | ||
| 167 | */ | ||
| 168 | retval = mtd->read_oob(mtd, addr, &ops); | ||
| 169 | if (retval) { | ||
| 170 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 171 | "read_oob failed, chunk %d, mtd error %d", | ||
| 172 | nand_chunk, retval); | ||
| 173 | } | ||
| 174 | |||
| 175 | switch (retval) { | ||
| 176 | case 0: | ||
| 177 | /* no error */ | ||
| 178 | break; | ||
| 179 | |||
| 180 | case -EUCLEAN: | ||
| 181 | /* MTD's ECC fixed the data */ | ||
| 182 | eccres = YAFFS_ECC_RESULT_FIXED; | ||
| 183 | dev->n_ecc_fixed++; | ||
| 184 | break; | ||
| 185 | |||
| 186 | case -EBADMSG: | ||
| 187 | /* MTD's ECC could not fix the data */ | ||
| 188 | dev->n_ecc_unfixed++; | ||
| 189 | /* fall into... */ | ||
| 190 | default: | ||
| 191 | rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0); | ||
| 192 | etags->block_bad = (mtd->block_isbad) (mtd, addr); | ||
| 193 | return YAFFS_FAIL; | ||
| 194 | } | ||
| 195 | |||
| 196 | /* Check for a blank/erased chunk. | ||
| 197 | */ | ||
| 198 | if (yaffs_check_ff((u8 *) & pt1, 8)) { | ||
| 199 | /* when blank, upper layers want ecc_result to be <= NO_ERROR */ | ||
| 200 | return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK); | ||
| 201 | } | ||
| 202 | #ifndef CONFIG_YAFFS_9BYTE_TAGS | ||
| 203 | /* Read deleted status (bit) then return it to it's non-deleted | ||
| 204 | * state before performing tags mini-ECC check. pt1.deleted is | ||
| 205 | * inverted. | ||
| 206 | */ | ||
| 207 | deleted = !pt1.deleted; | ||
| 208 | pt1.deleted = 1; | ||
| 209 | #else | ||
| 210 | deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7); | ||
| 211 | #endif | ||
| 212 | |||
| 213 | /* Check the packed tags mini-ECC and correct if necessary/possible. | ||
| 214 | */ | ||
| 215 | retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1); | ||
| 216 | switch (retval) { | ||
| 217 | case 0: | ||
| 218 | /* no tags error, use MTD result */ | ||
| 219 | break; | ||
| 220 | case 1: | ||
| 221 | /* recovered tags-ECC error */ | ||
| 222 | dev->n_tags_ecc_fixed++; | ||
| 223 | if (eccres == YAFFS_ECC_RESULT_NO_ERROR) | ||
| 224 | eccres = YAFFS_ECC_RESULT_FIXED; | ||
| 225 | break; | ||
| 226 | default: | ||
| 227 | /* unrecovered tags-ECC error */ | ||
| 228 | dev->n_tags_ecc_unfixed++; | ||
| 229 | return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL); | ||
| 230 | } | ||
| 231 | |||
| 232 | /* Unpack the tags to extended form and set ECC result. | ||
| 233 | * [set should_be_ff just to keep yaffs_unpack_tags1 happy] | ||
| 234 | */ | ||
| 235 | pt1.should_be_ff = 0xFFFFFFFF; | ||
| 236 | yaffs_unpack_tags1(etags, &pt1); | ||
| 237 | etags->ecc_result = eccres; | ||
| 238 | |||
| 239 | /* Set deleted state */ | ||
| 240 | etags->is_deleted = deleted; | ||
| 241 | return YAFFS_OK; | ||
| 242 | } | ||
| 243 | |||
| 244 | /* Mark a block bad. | ||
| 245 | * | ||
| 246 | * This is a persistant state. | ||
| 247 | * Use of this function should be rare. | ||
| 248 | * | ||
| 249 | * Returns YAFFS_OK or YAFFS_FAIL. | ||
| 250 | */ | ||
| 251 | int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no) | ||
| 252 | { | ||
| 253 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 254 | int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk; | ||
| 255 | int retval; | ||
| 256 | |||
| 257 | yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, | ||
| 258 | "marking block %d bad", block_no); | ||
| 259 | |||
| 260 | retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no); | ||
| 261 | return (retval) ? YAFFS_FAIL : YAFFS_OK; | ||
| 262 | } | ||
| 263 | |||
| 264 | /* Check any MTD prerequists. | ||
| 265 | * | ||
| 266 | * Returns YAFFS_OK or YAFFS_FAIL. | ||
| 267 | */ | ||
| 268 | static int nandmtd1_test_prerequists(struct mtd_info *mtd) | ||
| 269 | { | ||
| 270 | /* 2.6.18 has mtd->ecclayout->oobavail */ | ||
| 271 | /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */ | ||
| 272 | int oobavail = mtd->ecclayout->oobavail; | ||
| 273 | |||
| 274 | if (oobavail < YTAG1_SIZE) { | ||
| 275 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 276 | "mtd device has only %d bytes for tags, need %d", | ||
| 277 | oobavail, YTAG1_SIZE); | ||
| 278 | return YAFFS_FAIL; | ||
| 279 | } | ||
| 280 | return YAFFS_OK; | ||
| 281 | } | ||
| 282 | |||
| 283 | /* Query for the current state of a specific block. | ||
| 284 | * | ||
| 285 | * Examine the tags of the first chunk of the block and return the state: | ||
| 286 | * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad | ||
| 287 | * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use | ||
| 288 | * - YAFFS_BLOCK_STATE_EMPTY, the block is clean | ||
| 289 | * | ||
| 290 | * Always returns YAFFS_OK. | ||
| 291 | */ | ||
| 292 | int nandmtd1_query_block(struct yaffs_dev *dev, int block_no, | ||
| 293 | enum yaffs_block_state *state_ptr, u32 * seq_ptr) | ||
| 294 | { | ||
| 295 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 296 | int chunk_num = block_no * dev->param.chunks_per_block; | ||
| 297 | loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk; | ||
| 298 | struct yaffs_ext_tags etags; | ||
| 299 | int state = YAFFS_BLOCK_STATE_DEAD; | ||
| 300 | int seqnum = 0; | ||
| 301 | int retval; | ||
| 302 | |||
| 303 | /* We don't yet have a good place to test for MTD config prerequists. | ||
| 304 | * Do it here as we are called during the initial scan. | ||
| 305 | */ | ||
| 306 | if (nandmtd1_test_prerequists(mtd) != YAFFS_OK) | ||
| 307 | return YAFFS_FAIL; | ||
| 308 | |||
| 309 | retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags); | ||
| 310 | etags.block_bad = (mtd->block_isbad) (mtd, addr); | ||
| 311 | if (etags.block_bad) { | ||
| 312 | yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, | ||
| 313 | "block %d is marked bad", block_no); | ||
| 314 | state = YAFFS_BLOCK_STATE_DEAD; | ||
| 315 | } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) { | ||
| 316 | /* bad tags, need to look more closely */ | ||
| 317 | state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; | ||
| 318 | } else if (etags.chunk_used) { | ||
| 319 | state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; | ||
| 320 | seqnum = etags.seq_number; | ||
| 321 | } else { | ||
| 322 | state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 323 | } | ||
| 324 | |||
| 325 | *state_ptr = state; | ||
| 326 | *seq_ptr = seqnum; | ||
| 327 | |||
| 328 | /* query always succeeds */ | ||
| 329 | return YAFFS_OK; | ||
| 330 | } | ||
diff --git a/fs/yaffs2/yaffs_mtdif1.h b/fs/yaffs2/yaffs_mtdif1.h new file mode 100644 index 00000000000..07ce4524f0f --- /dev/null +++ b/fs/yaffs2/yaffs_mtdif1.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | * | ||
| 11 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #ifndef __YAFFS_MTDIF1_H__ | ||
| 15 | #define __YAFFS_MTDIF1_H__ | ||
| 16 | |||
| 17 | int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, | ||
| 18 | const u8 * data, | ||
| 19 | const struct yaffs_ext_tags *tags); | ||
| 20 | |||
| 21 | int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, | ||
| 22 | u8 * data, struct yaffs_ext_tags *tags); | ||
| 23 | |||
| 24 | int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no); | ||
| 25 | |||
| 26 | int nandmtd1_query_block(struct yaffs_dev *dev, int block_no, | ||
| 27 | enum yaffs_block_state *state, u32 * seq_number); | ||
| 28 | |||
| 29 | #endif | ||
diff --git a/fs/yaffs2/yaffs_mtdif2.c b/fs/yaffs2/yaffs_mtdif2.c new file mode 100644 index 00000000000..d1643df2c38 --- /dev/null +++ b/fs/yaffs2/yaffs_mtdif2.c | |||
| @@ -0,0 +1,225 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | /* mtd interface for YAFFS2 */ | ||
| 15 | |||
| 16 | #include "yportenv.h" | ||
| 17 | #include "yaffs_trace.h" | ||
| 18 | |||
| 19 | #include "yaffs_mtdif2.h" | ||
| 20 | |||
| 21 | #include "linux/mtd/mtd.h" | ||
| 22 | #include "linux/types.h" | ||
| 23 | #include "linux/time.h" | ||
| 24 | |||
| 25 | #include "yaffs_packedtags2.h" | ||
| 26 | |||
| 27 | #include "yaffs_linux.h" | ||
| 28 | |||
| 29 | /* NB For use with inband tags.... | ||
| 30 | * We assume that the data buffer is of size total_bytes_per_chunk so that we can also | ||
| 31 | * use it to load the tags. | ||
| 32 | */ | ||
| 33 | int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, | ||
| 34 | const u8 * data, | ||
| 35 | const struct yaffs_ext_tags *tags) | ||
| 36 | { | ||
| 37 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 38 | struct mtd_oob_ops ops; | ||
| 39 | int retval = 0; | ||
| 40 | |||
| 41 | loff_t addr; | ||
| 42 | |||
| 43 | struct yaffs_packed_tags2 pt; | ||
| 44 | |||
| 45 | int packed_tags_size = | ||
| 46 | dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt); | ||
| 47 | void *packed_tags_ptr = | ||
| 48 | dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; | ||
| 49 | |||
| 50 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 51 | "nandmtd2_write_chunk_tags chunk %d data %p tags %p", | ||
| 52 | nand_chunk, data, tags); | ||
| 53 | |||
| 54 | addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; | ||
| 55 | |||
| 56 | /* For yaffs2 writing there must be both data and tags. | ||
| 57 | * If we're using inband tags, then the tags are stuffed into | ||
| 58 | * the end of the data buffer. | ||
| 59 | */ | ||
| 60 | if (!data || !tags) | ||
| 61 | BUG(); | ||
| 62 | else if (dev->param.inband_tags) { | ||
| 63 | struct yaffs_packed_tags2_tags_only *pt2tp; | ||
| 64 | pt2tp = | ||
| 65 | (struct yaffs_packed_tags2_tags_only *)(data + | ||
| 66 | dev-> | ||
| 67 | data_bytes_per_chunk); | ||
| 68 | yaffs_pack_tags2_tags_only(pt2tp, tags); | ||
| 69 | } else { | ||
| 70 | yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); | ||
| 71 | } | ||
| 72 | |||
| 73 | ops.mode = MTD_OOB_AUTO; | ||
| 74 | ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size; | ||
| 75 | ops.len = dev->param.total_bytes_per_chunk; | ||
| 76 | ops.ooboffs = 0; | ||
| 77 | ops.datbuf = (u8 *) data; | ||
| 78 | ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr; | ||
| 79 | retval = mtd->write_oob(mtd, addr, &ops); | ||
| 80 | |||
| 81 | if (retval == 0) | ||
| 82 | return YAFFS_OK; | ||
| 83 | else | ||
| 84 | return YAFFS_FAIL; | ||
| 85 | } | ||
| 86 | |||
| 87 | int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, | ||
| 88 | u8 * data, struct yaffs_ext_tags *tags) | ||
| 89 | { | ||
| 90 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 91 | struct mtd_oob_ops ops; | ||
| 92 | |||
| 93 | size_t dummy; | ||
| 94 | int retval = 0; | ||
| 95 | int local_data = 0; | ||
| 96 | |||
| 97 | loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; | ||
| 98 | |||
| 99 | struct yaffs_packed_tags2 pt; | ||
| 100 | |||
| 101 | int packed_tags_size = | ||
| 102 | dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt); | ||
| 103 | void *packed_tags_ptr = | ||
| 104 | dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; | ||
| 105 | |||
| 106 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 107 | "nandmtd2_read_chunk_tags chunk %d data %p tags %p", | ||
| 108 | nand_chunk, data, tags); | ||
| 109 | |||
| 110 | if (dev->param.inband_tags) { | ||
| 111 | |||
| 112 | if (!data) { | ||
| 113 | local_data = 1; | ||
| 114 | data = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | if (dev->param.inband_tags || (data && !tags)) | ||
| 120 | retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk, | ||
| 121 | &dummy, data); | ||
| 122 | else if (tags) { | ||
| 123 | ops.mode = MTD_OOB_AUTO; | ||
| 124 | ops.ooblen = packed_tags_size; | ||
| 125 | ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size; | ||
| 126 | ops.ooboffs = 0; | ||
| 127 | ops.datbuf = data; | ||
| 128 | ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer; | ||
| 129 | retval = mtd->read_oob(mtd, addr, &ops); | ||
| 130 | } | ||
| 131 | |||
| 132 | if (dev->param.inband_tags) { | ||
| 133 | if (tags) { | ||
| 134 | struct yaffs_packed_tags2_tags_only *pt2tp; | ||
| 135 | pt2tp = | ||
| 136 | (struct yaffs_packed_tags2_tags_only *)&data[dev-> | ||
| 137 | data_bytes_per_chunk]; | ||
| 138 | yaffs_unpack_tags2_tags_only(tags, pt2tp); | ||
| 139 | } | ||
| 140 | } else { | ||
| 141 | if (tags) { | ||
| 142 | memcpy(packed_tags_ptr, | ||
| 143 | yaffs_dev_to_lc(dev)->spare_buffer, | ||
| 144 | packed_tags_size); | ||
| 145 | yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | if (local_data) | ||
| 150 | yaffs_release_temp_buffer(dev, data, __LINE__); | ||
| 151 | |||
| 152 | if (tags && retval == -EBADMSG | ||
| 153 | && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) { | ||
| 154 | tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED; | ||
| 155 | dev->n_ecc_unfixed++; | ||
| 156 | } | ||
| 157 | if (tags && retval == -EUCLEAN | ||
| 158 | && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) { | ||
| 159 | tags->ecc_result = YAFFS_ECC_RESULT_FIXED; | ||
| 160 | dev->n_ecc_fixed++; | ||
| 161 | } | ||
| 162 | if (retval == 0) | ||
| 163 | return YAFFS_OK; | ||
| 164 | else | ||
| 165 | return YAFFS_FAIL; | ||
| 166 | } | ||
| 167 | |||
| 168 | int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no) | ||
| 169 | { | ||
| 170 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 171 | int retval; | ||
| 172 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 173 | "nandmtd2_mark_block_bad %d", block_no); | ||
| 174 | |||
| 175 | retval = | ||
| 176 | mtd->block_markbad(mtd, | ||
| 177 | block_no * dev->param.chunks_per_block * | ||
| 178 | dev->param.total_bytes_per_chunk); | ||
| 179 | |||
| 180 | if (retval == 0) | ||
| 181 | return YAFFS_OK; | ||
| 182 | else | ||
| 183 | return YAFFS_FAIL; | ||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, | ||
| 188 | enum yaffs_block_state *state, u32 * seq_number) | ||
| 189 | { | ||
| 190 | struct mtd_info *mtd = yaffs_dev_to_mtd(dev); | ||
| 191 | int retval; | ||
| 192 | |||
| 193 | yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no); | ||
| 194 | retval = | ||
| 195 | mtd->block_isbad(mtd, | ||
| 196 | block_no * dev->param.chunks_per_block * | ||
| 197 | dev->param.total_bytes_per_chunk); | ||
| 198 | |||
| 199 | if (retval) { | ||
| 200 | yaffs_trace(YAFFS_TRACE_MTD, "block is bad"); | ||
| 201 | |||
| 202 | *state = YAFFS_BLOCK_STATE_DEAD; | ||
| 203 | *seq_number = 0; | ||
| 204 | } else { | ||
| 205 | struct yaffs_ext_tags t; | ||
| 206 | nandmtd2_read_chunk_tags(dev, block_no * | ||
| 207 | dev->param.chunks_per_block, NULL, &t); | ||
| 208 | |||
| 209 | if (t.chunk_used) { | ||
| 210 | *seq_number = t.seq_number; | ||
| 211 | *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; | ||
| 212 | } else { | ||
| 213 | *seq_number = 0; | ||
| 214 | *state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 218 | "block is bad seq %d state %d", *seq_number, *state); | ||
| 219 | |||
| 220 | if (retval == 0) | ||
| 221 | return YAFFS_OK; | ||
| 222 | else | ||
| 223 | return YAFFS_FAIL; | ||
| 224 | } | ||
| 225 | |||
diff --git a/fs/yaffs2/yaffs_mtdif2.h b/fs/yaffs2/yaffs_mtdif2.h new file mode 100644 index 00000000000..d82112610d0 --- /dev/null +++ b/fs/yaffs2/yaffs_mtdif2.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_MTDIF2_H__ | ||
| 17 | #define __YAFFS_MTDIF2_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, | ||
| 21 | const u8 * data, | ||
| 22 | const struct yaffs_ext_tags *tags); | ||
| 23 | int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, | ||
| 24 | u8 * data, struct yaffs_ext_tags *tags); | ||
| 25 | int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no); | ||
| 26 | int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, | ||
| 27 | enum yaffs_block_state *state, u32 * seq_number); | ||
| 28 | |||
| 29 | #endif | ||
diff --git a/fs/yaffs2/yaffs_nameval.c b/fs/yaffs2/yaffs_nameval.c new file mode 100644 index 00000000000..daa36f989d3 --- /dev/null +++ b/fs/yaffs2/yaffs_nameval.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | /* | ||
| 15 | * This simple implementation of a name-value store assumes a small number of values and fits | ||
| 16 | * into a small finite buffer. | ||
| 17 | * | ||
| 18 | * Each attribute is stored as a record: | ||
| 19 | * sizeof(int) bytes record size. | ||
| 20 | * strnlen+1 bytes name null terminated. | ||
| 21 | * nbytes value. | ||
| 22 | * ---------- | ||
| 23 | * total size stored in record size | ||
| 24 | * | ||
| 25 | * This code has not been tested with unicode yet. | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "yaffs_nameval.h" | ||
| 29 | |||
| 30 | #include "yportenv.h" | ||
| 31 | |||
| 32 | static int nval_find(const char *xb, int xb_size, const YCHAR * name, | ||
| 33 | int *exist_size) | ||
| 34 | { | ||
| 35 | int pos = 0; | ||
| 36 | int size; | ||
| 37 | |||
| 38 | memcpy(&size, xb, sizeof(int)); | ||
| 39 | while (size > 0 && (size < xb_size) && (pos + size < xb_size)) { | ||
| 40 | if (strncmp | ||
| 41 | ((YCHAR *) (xb + pos + sizeof(int)), name, size) == 0) { | ||
| 42 | if (exist_size) | ||
| 43 | *exist_size = size; | ||
| 44 | return pos; | ||
| 45 | } | ||
| 46 | pos += size; | ||
| 47 | if (pos < xb_size - sizeof(int)) | ||
| 48 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 49 | else | ||
| 50 | size = 0; | ||
| 51 | } | ||
| 52 | if (exist_size) | ||
| 53 | *exist_size = 0; | ||
| 54 | return -1; | ||
| 55 | } | ||
| 56 | |||
| 57 | static int nval_used(const char *xb, int xb_size) | ||
| 58 | { | ||
| 59 | int pos = 0; | ||
| 60 | int size; | ||
| 61 | |||
| 62 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 63 | while (size > 0 && (size < xb_size) && (pos + size < xb_size)) { | ||
| 64 | pos += size; | ||
| 65 | if (pos < xb_size - sizeof(int)) | ||
| 66 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 67 | else | ||
| 68 | size = 0; | ||
| 69 | } | ||
| 70 | return pos; | ||
| 71 | } | ||
| 72 | |||
| 73 | int nval_del(char *xb, int xb_size, const YCHAR * name) | ||
| 74 | { | ||
| 75 | int pos = nval_find(xb, xb_size, name, NULL); | ||
| 76 | int size; | ||
| 77 | |||
| 78 | if (pos >= 0 && pos < xb_size) { | ||
| 79 | /* Find size, shift rest over this record, then zero out the rest of buffer */ | ||
| 80 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 81 | memcpy(xb + pos, xb + pos + size, xb_size - (pos + size)); | ||
| 82 | memset(xb + (xb_size - size), 0, size); | ||
| 83 | return 0; | ||
| 84 | } else { | ||
| 85 | return -ENODATA; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf, | ||
| 90 | int bsize, int flags) | ||
| 91 | { | ||
| 92 | int pos; | ||
| 93 | int namelen = strnlen(name, xb_size); | ||
| 94 | int reclen; | ||
| 95 | int size_exist = 0; | ||
| 96 | int space; | ||
| 97 | int start; | ||
| 98 | |||
| 99 | pos = nval_find(xb, xb_size, name, &size_exist); | ||
| 100 | |||
| 101 | if (flags & XATTR_CREATE && pos >= 0) | ||
| 102 | return -EEXIST; | ||
| 103 | if (flags & XATTR_REPLACE && pos < 0) | ||
| 104 | return -ENODATA; | ||
| 105 | |||
| 106 | start = nval_used(xb, xb_size); | ||
| 107 | space = xb_size - start + size_exist; | ||
| 108 | |||
| 109 | reclen = (sizeof(int) + namelen + 1 + bsize); | ||
| 110 | |||
| 111 | if (reclen > space) | ||
| 112 | return -ENOSPC; | ||
| 113 | |||
| 114 | if (pos >= 0) { | ||
| 115 | nval_del(xb, xb_size, name); | ||
| 116 | start = nval_used(xb, xb_size); | ||
| 117 | } | ||
| 118 | |||
| 119 | pos = start; | ||
| 120 | |||
| 121 | memcpy(xb + pos, &reclen, sizeof(int)); | ||
| 122 | pos += sizeof(int); | ||
| 123 | strncpy((YCHAR *) (xb + pos), name, reclen); | ||
| 124 | pos += (namelen + 1); | ||
| 125 | memcpy(xb + pos, buf, bsize); | ||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf, | ||
| 130 | int bsize) | ||
| 131 | { | ||
| 132 | int pos = nval_find(xb, xb_size, name, NULL); | ||
| 133 | int size; | ||
| 134 | |||
| 135 | if (pos >= 0 && pos < xb_size) { | ||
| 136 | |||
| 137 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 138 | pos += sizeof(int); /* advance past record length */ | ||
| 139 | size -= sizeof(int); | ||
| 140 | |||
| 141 | /* Advance over name string */ | ||
| 142 | while (xb[pos] && size > 0 && pos < xb_size) { | ||
| 143 | pos++; | ||
| 144 | size--; | ||
| 145 | } | ||
| 146 | /*Advance over NUL */ | ||
| 147 | pos++; | ||
| 148 | size--; | ||
| 149 | |||
| 150 | if (size <= bsize) { | ||
| 151 | memcpy(buf, xb + pos, size); | ||
| 152 | return size; | ||
| 153 | } | ||
| 154 | |||
| 155 | } | ||
| 156 | if (pos >= 0) | ||
| 157 | return -ERANGE; | ||
| 158 | else | ||
| 159 | return -ENODATA; | ||
| 160 | } | ||
| 161 | |||
| 162 | int nval_list(const char *xb, int xb_size, char *buf, int bsize) | ||
| 163 | { | ||
| 164 | int pos = 0; | ||
| 165 | int size; | ||
| 166 | int name_len; | ||
| 167 | int ncopied = 0; | ||
| 168 | int filled = 0; | ||
| 169 | |||
| 170 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 171 | while (size > sizeof(int) && size <= xb_size && (pos + size) < xb_size | ||
| 172 | && !filled) { | ||
| 173 | pos += sizeof(int); | ||
| 174 | size -= sizeof(int); | ||
| 175 | name_len = strnlen((YCHAR *) (xb + pos), size); | ||
| 176 | if (ncopied + name_len + 1 < bsize) { | ||
| 177 | memcpy(buf, xb + pos, name_len * sizeof(YCHAR)); | ||
| 178 | buf += name_len; | ||
| 179 | *buf = '\0'; | ||
| 180 | buf++; | ||
| 181 | if (sizeof(YCHAR) > 1) { | ||
| 182 | *buf = '\0'; | ||
| 183 | buf++; | ||
| 184 | } | ||
| 185 | ncopied += (name_len + 1); | ||
| 186 | } else { | ||
| 187 | filled = 1; | ||
| 188 | } | ||
| 189 | pos += size; | ||
| 190 | if (pos < xb_size - sizeof(int)) | ||
| 191 | memcpy(&size, xb + pos, sizeof(int)); | ||
| 192 | else | ||
| 193 | size = 0; | ||
| 194 | } | ||
| 195 | return ncopied; | ||
| 196 | } | ||
| 197 | |||
| 198 | int nval_hasvalues(const char *xb, int xb_size) | ||
| 199 | { | ||
| 200 | return nval_used(xb, xb_size) > 0; | ||
| 201 | } | ||
diff --git a/fs/yaffs2/yaffs_nameval.h b/fs/yaffs2/yaffs_nameval.h new file mode 100644 index 00000000000..2bb02b62762 --- /dev/null +++ b/fs/yaffs2/yaffs_nameval.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __NAMEVAL_H__ | ||
| 17 | #define __NAMEVAL_H__ | ||
| 18 | |||
| 19 | #include "yportenv.h" | ||
| 20 | |||
| 21 | int nval_del(char *xb, int xb_size, const YCHAR * name); | ||
| 22 | int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf, | ||
| 23 | int bsize, int flags); | ||
| 24 | int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf, | ||
| 25 | int bsize); | ||
| 26 | int nval_list(const char *xb, int xb_size, char *buf, int bsize); | ||
| 27 | int nval_hasvalues(const char *xb, int xb_size); | ||
| 28 | #endif | ||
diff --git a/fs/yaffs2/yaffs_nand.c b/fs/yaffs2/yaffs_nand.c new file mode 100644 index 00000000000..e816cabf43f --- /dev/null +++ b/fs/yaffs2/yaffs_nand.c | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_nand.h" | ||
| 15 | #include "yaffs_tagscompat.h" | ||
| 16 | #include "yaffs_tagsvalidity.h" | ||
| 17 | |||
| 18 | #include "yaffs_getblockinfo.h" | ||
| 19 | |||
| 20 | int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, | ||
| 21 | u8 * buffer, struct yaffs_ext_tags *tags) | ||
| 22 | { | ||
| 23 | int result; | ||
| 24 | struct yaffs_ext_tags local_tags; | ||
| 25 | |||
| 26 | int realigned_chunk = nand_chunk - dev->chunk_offset; | ||
| 27 | |||
| 28 | dev->n_page_reads++; | ||
| 29 | |||
| 30 | /* If there are no tags provided, use local tags to get prioritised gc working */ | ||
| 31 | if (!tags) | ||
| 32 | tags = &local_tags; | ||
| 33 | |||
| 34 | if (dev->param.read_chunk_tags_fn) | ||
| 35 | result = | ||
| 36 | dev->param.read_chunk_tags_fn(dev, realigned_chunk, buffer, | ||
| 37 | tags); | ||
| 38 | else | ||
| 39 | result = yaffs_tags_compat_rd(dev, | ||
| 40 | realigned_chunk, buffer, tags); | ||
| 41 | if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) { | ||
| 42 | |||
| 43 | struct yaffs_block_info *bi; | ||
| 44 | bi = yaffs_get_block_info(dev, | ||
| 45 | nand_chunk / | ||
| 46 | dev->param.chunks_per_block); | ||
| 47 | yaffs_handle_chunk_error(dev, bi); | ||
| 48 | } | ||
| 49 | |||
| 50 | return result; | ||
| 51 | } | ||
| 52 | |||
| 53 | int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev, | ||
| 54 | int nand_chunk, | ||
| 55 | const u8 * buffer, struct yaffs_ext_tags *tags) | ||
| 56 | { | ||
| 57 | |||
| 58 | dev->n_page_writes++; | ||
| 59 | |||
| 60 | nand_chunk -= dev->chunk_offset; | ||
| 61 | |||
| 62 | if (tags) { | ||
| 63 | tags->seq_number = dev->seq_number; | ||
| 64 | tags->chunk_used = 1; | ||
| 65 | if (!yaffs_validate_tags(tags)) { | ||
| 66 | yaffs_trace(YAFFS_TRACE_ERROR, "Writing uninitialised tags"); | ||
| 67 | YBUG(); | ||
| 68 | } | ||
| 69 | yaffs_trace(YAFFS_TRACE_WRITE, | ||
| 70 | "Writing chunk %d tags %d %d", | ||
| 71 | nand_chunk, tags->obj_id, tags->chunk_id); | ||
| 72 | } else { | ||
| 73 | yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags"); | ||
| 74 | YBUG(); | ||
| 75 | } | ||
| 76 | |||
| 77 | if (dev->param.write_chunk_tags_fn) | ||
| 78 | return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer, | ||
| 79 | tags); | ||
| 80 | else | ||
| 81 | return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags); | ||
| 82 | } | ||
| 83 | |||
| 84 | int yaffs_mark_bad(struct yaffs_dev *dev, int block_no) | ||
| 85 | { | ||
| 86 | block_no -= dev->block_offset; | ||
| 87 | |||
| 88 | if (dev->param.bad_block_fn) | ||
| 89 | return dev->param.bad_block_fn(dev, block_no); | ||
| 90 | else | ||
| 91 | return yaffs_tags_compat_mark_bad(dev, block_no); | ||
| 92 | } | ||
| 93 | |||
| 94 | int yaffs_query_init_block_state(struct yaffs_dev *dev, | ||
| 95 | int block_no, | ||
| 96 | enum yaffs_block_state *state, | ||
| 97 | u32 * seq_number) | ||
| 98 | { | ||
| 99 | block_no -= dev->block_offset; | ||
| 100 | |||
| 101 | if (dev->param.query_block_fn) | ||
| 102 | return dev->param.query_block_fn(dev, block_no, state, | ||
| 103 | seq_number); | ||
| 104 | else | ||
| 105 | return yaffs_tags_compat_query_block(dev, block_no, | ||
| 106 | state, seq_number); | ||
| 107 | } | ||
| 108 | |||
| 109 | int yaffs_erase_block(struct yaffs_dev *dev, int flash_block) | ||
| 110 | { | ||
| 111 | int result; | ||
| 112 | |||
| 113 | flash_block -= dev->block_offset; | ||
| 114 | |||
| 115 | dev->n_erasures++; | ||
| 116 | |||
| 117 | result = dev->param.erase_fn(dev, flash_block); | ||
| 118 | |||
| 119 | return result; | ||
| 120 | } | ||
| 121 | |||
| 122 | int yaffs_init_nand(struct yaffs_dev *dev) | ||
| 123 | { | ||
| 124 | if (dev->param.initialise_flash_fn) | ||
| 125 | return dev->param.initialise_flash_fn(dev); | ||
| 126 | return YAFFS_OK; | ||
| 127 | } | ||
diff --git a/fs/yaffs2/yaffs_nand.h b/fs/yaffs2/yaffs_nand.h new file mode 100644 index 00000000000..543f1987124 --- /dev/null +++ b/fs/yaffs2/yaffs_nand.h | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_NAND_H__ | ||
| 17 | #define __YAFFS_NAND_H__ | ||
| 18 | #include "yaffs_guts.h" | ||
| 19 | |||
| 20 | int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, | ||
| 21 | u8 * buffer, struct yaffs_ext_tags *tags); | ||
| 22 | |||
| 23 | int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev, | ||
| 24 | int nand_chunk, | ||
| 25 | const u8 * buffer, struct yaffs_ext_tags *tags); | ||
| 26 | |||
| 27 | int yaffs_mark_bad(struct yaffs_dev *dev, int block_no); | ||
| 28 | |||
| 29 | int yaffs_query_init_block_state(struct yaffs_dev *dev, | ||
| 30 | int block_no, | ||
| 31 | enum yaffs_block_state *state, | ||
| 32 | unsigned *seq_number); | ||
| 33 | |||
| 34 | int yaffs_erase_block(struct yaffs_dev *dev, int flash_block); | ||
| 35 | |||
| 36 | int yaffs_init_nand(struct yaffs_dev *dev); | ||
| 37 | |||
| 38 | #endif | ||
diff --git a/fs/yaffs2/yaffs_packedtags1.c b/fs/yaffs2/yaffs_packedtags1.c new file mode 100644 index 00000000000..a77f0954fc1 --- /dev/null +++ b/fs/yaffs2/yaffs_packedtags1.c | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_packedtags1.h" | ||
| 15 | #include "yportenv.h" | ||
| 16 | |||
| 17 | void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt, | ||
| 18 | const struct yaffs_ext_tags *t) | ||
| 19 | { | ||
| 20 | pt->chunk_id = t->chunk_id; | ||
| 21 | pt->serial_number = t->serial_number; | ||
| 22 | pt->n_bytes = t->n_bytes; | ||
| 23 | pt->obj_id = t->obj_id; | ||
| 24 | pt->ecc = 0; | ||
| 25 | pt->deleted = (t->is_deleted) ? 0 : 1; | ||
| 26 | pt->unused_stuff = 0; | ||
| 27 | pt->should_be_ff = 0xFFFFFFFF; | ||
| 28 | |||
| 29 | } | ||
| 30 | |||
| 31 | void yaffs_unpack_tags1(struct yaffs_ext_tags *t, | ||
| 32 | const struct yaffs_packed_tags1 *pt) | ||
| 33 | { | ||
| 34 | static const u8 all_ff[] = | ||
| 35 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | ||
| 36 | 0xff | ||
| 37 | }; | ||
| 38 | |||
| 39 | if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) { | ||
| 40 | t->block_bad = 0; | ||
| 41 | if (pt->should_be_ff != 0xFFFFFFFF) | ||
| 42 | t->block_bad = 1; | ||
| 43 | t->chunk_used = 1; | ||
| 44 | t->obj_id = pt->obj_id; | ||
| 45 | t->chunk_id = pt->chunk_id; | ||
| 46 | t->n_bytes = pt->n_bytes; | ||
| 47 | t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR; | ||
| 48 | t->is_deleted = (pt->deleted) ? 0 : 1; | ||
| 49 | t->serial_number = pt->serial_number; | ||
| 50 | } else { | ||
| 51 | memset(t, 0, sizeof(struct yaffs_ext_tags)); | ||
| 52 | } | ||
| 53 | } | ||
diff --git a/fs/yaffs2/yaffs_packedtags1.h b/fs/yaffs2/yaffs_packedtags1.h new file mode 100644 index 00000000000..d6861ff505e --- /dev/null +++ b/fs/yaffs2/yaffs_packedtags1.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | /* This is used to pack YAFFS1 tags, not YAFFS2 tags. */ | ||
| 17 | |||
| 18 | #ifndef __YAFFS_PACKEDTAGS1_H__ | ||
| 19 | #define __YAFFS_PACKEDTAGS1_H__ | ||
| 20 | |||
| 21 | #include "yaffs_guts.h" | ||
| 22 | |||
| 23 | struct yaffs_packed_tags1 { | ||
| 24 | unsigned chunk_id:20; | ||
| 25 | unsigned serial_number:2; | ||
| 26 | unsigned n_bytes:10; | ||
| 27 | unsigned obj_id:18; | ||
| 28 | unsigned ecc:12; | ||
| 29 | unsigned deleted:1; | ||
| 30 | unsigned unused_stuff:1; | ||
| 31 | unsigned should_be_ff; | ||
| 32 | |||
| 33 | }; | ||
| 34 | |||
| 35 | void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt, | ||
| 36 | const struct yaffs_ext_tags *t); | ||
| 37 | void yaffs_unpack_tags1(struct yaffs_ext_tags *t, | ||
| 38 | const struct yaffs_packed_tags1 *pt); | ||
| 39 | #endif | ||
diff --git a/fs/yaffs2/yaffs_packedtags2.c b/fs/yaffs2/yaffs_packedtags2.c new file mode 100644 index 00000000000..8e7fea3d286 --- /dev/null +++ b/fs/yaffs2/yaffs_packedtags2.c | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_packedtags2.h" | ||
| 15 | #include "yportenv.h" | ||
| 16 | #include "yaffs_trace.h" | ||
| 17 | #include "yaffs_tagsvalidity.h" | ||
| 18 | |||
| 19 | /* This code packs a set of extended tags into a binary structure for | ||
| 20 | * NAND storage | ||
| 21 | */ | ||
| 22 | |||
| 23 | /* Some of the information is "extra" struff which can be packed in to | ||
| 24 | * speed scanning | ||
| 25 | * This is defined by having the EXTRA_HEADER_INFO_FLAG set. | ||
| 26 | */ | ||
| 27 | |||
| 28 | /* Extra flags applied to chunk_id */ | ||
| 29 | |||
| 30 | #define EXTRA_HEADER_INFO_FLAG 0x80000000 | ||
| 31 | #define EXTRA_SHRINK_FLAG 0x40000000 | ||
| 32 | #define EXTRA_SHADOWS_FLAG 0x20000000 | ||
| 33 | #define EXTRA_SPARE_FLAGS 0x10000000 | ||
| 34 | |||
| 35 | #define ALL_EXTRA_FLAGS 0xF0000000 | ||
| 36 | |||
| 37 | /* Also, the top 4 bits of the object Id are set to the object type. */ | ||
| 38 | #define EXTRA_OBJECT_TYPE_SHIFT (28) | ||
| 39 | #define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT) | ||
| 40 | |||
| 41 | static void yaffs_dump_packed_tags2_tags_only(const struct | ||
| 42 | yaffs_packed_tags2_tags_only *ptt) | ||
| 43 | { | ||
| 44 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 45 | "packed tags obj %d chunk %d byte %d seq %d", | ||
| 46 | ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number); | ||
| 47 | } | ||
| 48 | |||
| 49 | static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt) | ||
| 50 | { | ||
| 51 | yaffs_dump_packed_tags2_tags_only(&pt->t); | ||
| 52 | } | ||
| 53 | |||
| 54 | static void yaffs_dump_tags2(const struct yaffs_ext_tags *t) | ||
| 55 | { | ||
| 56 | yaffs_trace(YAFFS_TRACE_MTD, | ||
| 57 | "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d", | ||
| 58 | t->ecc_result, t->block_bad, t->chunk_used, t->obj_id, | ||
| 59 | t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number, | ||
| 60 | t->seq_number); | ||
| 61 | |||
| 62 | } | ||
| 63 | |||
| 64 | void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt, | ||
| 65 | const struct yaffs_ext_tags *t) | ||
| 66 | { | ||
| 67 | ptt->chunk_id = t->chunk_id; | ||
| 68 | ptt->seq_number = t->seq_number; | ||
| 69 | ptt->n_bytes = t->n_bytes; | ||
| 70 | ptt->obj_id = t->obj_id; | ||
| 71 | |||
| 72 | if (t->chunk_id == 0 && t->extra_available) { | ||
| 73 | /* Store the extra header info instead */ | ||
| 74 | /* We save the parent object in the chunk_id */ | ||
| 75 | ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id; | ||
| 76 | if (t->extra_is_shrink) | ||
| 77 | ptt->chunk_id |= EXTRA_SHRINK_FLAG; | ||
| 78 | if (t->extra_shadows) | ||
| 79 | ptt->chunk_id |= EXTRA_SHADOWS_FLAG; | ||
| 80 | |||
| 81 | ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK; | ||
| 82 | ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT); | ||
| 83 | |||
| 84 | if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK) | ||
| 85 | ptt->n_bytes = t->extra_equiv_id; | ||
| 86 | else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE) | ||
| 87 | ptt->n_bytes = t->extra_length; | ||
| 88 | else | ||
| 89 | ptt->n_bytes = 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | yaffs_dump_packed_tags2_tags_only(ptt); | ||
| 93 | yaffs_dump_tags2(t); | ||
| 94 | } | ||
| 95 | |||
| 96 | void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt, | ||
| 97 | const struct yaffs_ext_tags *t, int tags_ecc) | ||
| 98 | { | ||
| 99 | yaffs_pack_tags2_tags_only(&pt->t, t); | ||
| 100 | |||
| 101 | if (tags_ecc) | ||
| 102 | yaffs_ecc_calc_other((unsigned char *)&pt->t, | ||
| 103 | sizeof(struct | ||
| 104 | yaffs_packed_tags2_tags_only), | ||
| 105 | &pt->ecc); | ||
| 106 | } | ||
| 107 | |||
| 108 | void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t, | ||
| 109 | struct yaffs_packed_tags2_tags_only *ptt) | ||
| 110 | { | ||
| 111 | |||
| 112 | memset(t, 0, sizeof(struct yaffs_ext_tags)); | ||
| 113 | |||
| 114 | yaffs_init_tags(t); | ||
| 115 | |||
| 116 | if (ptt->seq_number != 0xFFFFFFFF) { | ||
| 117 | t->block_bad = 0; | ||
| 118 | t->chunk_used = 1; | ||
| 119 | t->obj_id = ptt->obj_id; | ||
| 120 | t->chunk_id = ptt->chunk_id; | ||
| 121 | t->n_bytes = ptt->n_bytes; | ||
| 122 | t->is_deleted = 0; | ||
| 123 | t->serial_number = 0; | ||
| 124 | t->seq_number = ptt->seq_number; | ||
| 125 | |||
| 126 | /* Do extra header info stuff */ | ||
| 127 | |||
| 128 | if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) { | ||
| 129 | t->chunk_id = 0; | ||
| 130 | t->n_bytes = 0; | ||
| 131 | |||
| 132 | t->extra_available = 1; | ||
| 133 | t->extra_parent_id = | ||
| 134 | ptt->chunk_id & (~(ALL_EXTRA_FLAGS)); | ||
| 135 | t->extra_is_shrink = | ||
| 136 | (ptt->chunk_id & EXTRA_SHRINK_FLAG) ? 1 : 0; | ||
| 137 | t->extra_shadows = | ||
| 138 | (ptt->chunk_id & EXTRA_SHADOWS_FLAG) ? 1 : 0; | ||
| 139 | t->extra_obj_type = | ||
| 140 | ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT; | ||
| 141 | t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK; | ||
| 142 | |||
| 143 | if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK) | ||
| 144 | t->extra_equiv_id = ptt->n_bytes; | ||
| 145 | else | ||
| 146 | t->extra_length = ptt->n_bytes; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | yaffs_dump_packed_tags2_tags_only(ptt); | ||
| 151 | yaffs_dump_tags2(t); | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt, | ||
| 156 | int tags_ecc) | ||
| 157 | { | ||
| 158 | |||
| 159 | enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR; | ||
| 160 | |||
| 161 | if (pt->t.seq_number != 0xFFFFFFFF && tags_ecc) { | ||
| 162 | /* Chunk is in use and we need to do ECC */ | ||
| 163 | |||
| 164 | struct yaffs_ecc_other ecc; | ||
| 165 | int result; | ||
| 166 | yaffs_ecc_calc_other((unsigned char *)&pt->t, | ||
| 167 | sizeof(struct | ||
| 168 | yaffs_packed_tags2_tags_only), | ||
| 169 | &ecc); | ||
| 170 | result = | ||
| 171 | yaffs_ecc_correct_other((unsigned char *)&pt->t, | ||
| 172 | sizeof(struct | ||
| 173 | yaffs_packed_tags2_tags_only), | ||
| 174 | &pt->ecc, &ecc); | ||
| 175 | switch (result) { | ||
| 176 | case 0: | ||
| 177 | ecc_result = YAFFS_ECC_RESULT_NO_ERROR; | ||
| 178 | break; | ||
| 179 | case 1: | ||
| 180 | ecc_result = YAFFS_ECC_RESULT_FIXED; | ||
| 181 | break; | ||
| 182 | case -1: | ||
| 183 | ecc_result = YAFFS_ECC_RESULT_UNFIXED; | ||
| 184 | break; | ||
| 185 | default: | ||
| 186 | ecc_result = YAFFS_ECC_RESULT_UNKNOWN; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | yaffs_unpack_tags2_tags_only(t, &pt->t); | ||
| 191 | |||
| 192 | t->ecc_result = ecc_result; | ||
| 193 | |||
| 194 | yaffs_dump_packed_tags2(pt); | ||
| 195 | yaffs_dump_tags2(t); | ||
| 196 | } | ||
diff --git a/fs/yaffs2/yaffs_packedtags2.h b/fs/yaffs2/yaffs_packedtags2.h new file mode 100644 index 00000000000..f3296697bc0 --- /dev/null +++ b/fs/yaffs2/yaffs_packedtags2.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | /* This is used to pack YAFFS2 tags, not YAFFS1tags. */ | ||
| 17 | |||
| 18 | #ifndef __YAFFS_PACKEDTAGS2_H__ | ||
| 19 | #define __YAFFS_PACKEDTAGS2_H__ | ||
| 20 | |||
| 21 | #include "yaffs_guts.h" | ||
| 22 | #include "yaffs_ecc.h" | ||
| 23 | |||
| 24 | struct yaffs_packed_tags2_tags_only { | ||
| 25 | unsigned seq_number; | ||
| 26 | unsigned obj_id; | ||
| 27 | unsigned chunk_id; | ||
| 28 | unsigned n_bytes; | ||
| 29 | }; | ||
| 30 | |||
| 31 | struct yaffs_packed_tags2 { | ||
| 32 | struct yaffs_packed_tags2_tags_only t; | ||
| 33 | struct yaffs_ecc_other ecc; | ||
| 34 | }; | ||
| 35 | |||
| 36 | /* Full packed tags with ECC, used for oob tags */ | ||
| 37 | void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt, | ||
| 38 | const struct yaffs_ext_tags *t, int tags_ecc); | ||
| 39 | void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt, | ||
| 40 | int tags_ecc); | ||
| 41 | |||
| 42 | /* Only the tags part (no ECC for use with inband tags */ | ||
| 43 | void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *pt, | ||
| 44 | const struct yaffs_ext_tags *t); | ||
| 45 | void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t, | ||
| 46 | struct yaffs_packed_tags2_tags_only *pt); | ||
| 47 | #endif | ||
diff --git a/fs/yaffs2/yaffs_tagscompat.c b/fs/yaffs2/yaffs_tagscompat.c new file mode 100644 index 00000000000..7578075d9ac --- /dev/null +++ b/fs/yaffs2/yaffs_tagscompat.c | |||
| @@ -0,0 +1,422 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_guts.h" | ||
| 15 | #include "yaffs_tagscompat.h" | ||
| 16 | #include "yaffs_ecc.h" | ||
| 17 | #include "yaffs_getblockinfo.h" | ||
| 18 | #include "yaffs_trace.h" | ||
| 19 | |||
| 20 | static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk); | ||
| 21 | |||
| 22 | |||
| 23 | /********** Tags ECC calculations *********/ | ||
| 24 | |||
| 25 | void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare) | ||
| 26 | { | ||
| 27 | yaffs_ecc_cacl(data, spare->ecc1); | ||
| 28 | yaffs_ecc_cacl(&data[256], spare->ecc2); | ||
| 29 | } | ||
| 30 | |||
| 31 | void yaffs_calc_tags_ecc(struct yaffs_tags *tags) | ||
| 32 | { | ||
| 33 | /* Calculate an ecc */ | ||
| 34 | |||
| 35 | unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes; | ||
| 36 | unsigned i, j; | ||
| 37 | unsigned ecc = 0; | ||
| 38 | unsigned bit = 0; | ||
| 39 | |||
| 40 | tags->ecc = 0; | ||
| 41 | |||
| 42 | for (i = 0; i < 8; i++) { | ||
| 43 | for (j = 1; j & 0xff; j <<= 1) { | ||
| 44 | bit++; | ||
| 45 | if (b[i] & j) | ||
| 46 | ecc ^= bit; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | tags->ecc = ecc; | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | int yaffs_check_tags_ecc(struct yaffs_tags *tags) | ||
| 55 | { | ||
| 56 | unsigned ecc = tags->ecc; | ||
| 57 | |||
| 58 | yaffs_calc_tags_ecc(tags); | ||
| 59 | |||
| 60 | ecc ^= tags->ecc; | ||
| 61 | |||
| 62 | if (ecc && ecc <= 64) { | ||
| 63 | /* TODO: Handle the failure better. Retire? */ | ||
| 64 | unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes; | ||
| 65 | |||
| 66 | ecc--; | ||
| 67 | |||
| 68 | b[ecc / 8] ^= (1 << (ecc & 7)); | ||
| 69 | |||
| 70 | /* Now recvalc the ecc */ | ||
| 71 | yaffs_calc_tags_ecc(tags); | ||
| 72 | |||
| 73 | return 1; /* recovered error */ | ||
| 74 | } else if (ecc) { | ||
| 75 | /* Wierd ecc failure value */ | ||
| 76 | /* TODO Need to do somethiong here */ | ||
| 77 | return -1; /* unrecovered error */ | ||
| 78 | } | ||
| 79 | |||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | /********** Tags **********/ | ||
| 84 | |||
| 85 | static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr, | ||
| 86 | struct yaffs_tags *tags_ptr) | ||
| 87 | { | ||
| 88 | union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr; | ||
| 89 | |||
| 90 | yaffs_calc_tags_ecc(tags_ptr); | ||
| 91 | |||
| 92 | spare_ptr->tb0 = tu->as_bytes[0]; | ||
| 93 | spare_ptr->tb1 = tu->as_bytes[1]; | ||
| 94 | spare_ptr->tb2 = tu->as_bytes[2]; | ||
| 95 | spare_ptr->tb3 = tu->as_bytes[3]; | ||
| 96 | spare_ptr->tb4 = tu->as_bytes[4]; | ||
| 97 | spare_ptr->tb5 = tu->as_bytes[5]; | ||
| 98 | spare_ptr->tb6 = tu->as_bytes[6]; | ||
| 99 | spare_ptr->tb7 = tu->as_bytes[7]; | ||
| 100 | } | ||
| 101 | |||
| 102 | static void yaffs_get_tags_from_spare(struct yaffs_dev *dev, | ||
| 103 | struct yaffs_spare *spare_ptr, | ||
| 104 | struct yaffs_tags *tags_ptr) | ||
| 105 | { | ||
| 106 | union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr; | ||
| 107 | int result; | ||
| 108 | |||
| 109 | tu->as_bytes[0] = spare_ptr->tb0; | ||
| 110 | tu->as_bytes[1] = spare_ptr->tb1; | ||
| 111 | tu->as_bytes[2] = spare_ptr->tb2; | ||
| 112 | tu->as_bytes[3] = spare_ptr->tb3; | ||
| 113 | tu->as_bytes[4] = spare_ptr->tb4; | ||
| 114 | tu->as_bytes[5] = spare_ptr->tb5; | ||
| 115 | tu->as_bytes[6] = spare_ptr->tb6; | ||
| 116 | tu->as_bytes[7] = spare_ptr->tb7; | ||
| 117 | |||
| 118 | result = yaffs_check_tags_ecc(tags_ptr); | ||
| 119 | if (result > 0) | ||
| 120 | dev->n_tags_ecc_fixed++; | ||
| 121 | else if (result < 0) | ||
| 122 | dev->n_tags_ecc_unfixed++; | ||
| 123 | } | ||
| 124 | |||
| 125 | static void yaffs_spare_init(struct yaffs_spare *spare) | ||
| 126 | { | ||
| 127 | memset(spare, 0xFF, sizeof(struct yaffs_spare)); | ||
| 128 | } | ||
| 129 | |||
| 130 | static int yaffs_wr_nand(struct yaffs_dev *dev, | ||
| 131 | int nand_chunk, const u8 * data, | ||
| 132 | struct yaffs_spare *spare) | ||
| 133 | { | ||
| 134 | if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) { | ||
| 135 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 136 | "**>> yaffs chunk %d is not valid", | ||
| 137 | nand_chunk); | ||
| 138 | return YAFFS_FAIL; | ||
| 139 | } | ||
| 140 | |||
| 141 | return dev->param.write_chunk_fn(dev, nand_chunk, data, spare); | ||
| 142 | } | ||
| 143 | |||
| 144 | static int yaffs_rd_chunk_nand(struct yaffs_dev *dev, | ||
| 145 | int nand_chunk, | ||
| 146 | u8 * data, | ||
| 147 | struct yaffs_spare *spare, | ||
| 148 | enum yaffs_ecc_result *ecc_result, | ||
| 149 | int correct_errors) | ||
| 150 | { | ||
| 151 | int ret_val; | ||
| 152 | struct yaffs_spare local_spare; | ||
| 153 | |||
| 154 | if (!spare && data) { | ||
| 155 | /* If we don't have a real spare, then we use a local one. */ | ||
| 156 | /* Need this for the calculation of the ecc */ | ||
| 157 | spare = &local_spare; | ||
| 158 | } | ||
| 159 | |||
| 160 | if (!dev->param.use_nand_ecc) { | ||
| 161 | ret_val = | ||
| 162 | dev->param.read_chunk_fn(dev, nand_chunk, data, spare); | ||
| 163 | if (data && correct_errors) { | ||
| 164 | /* Do ECC correction */ | ||
| 165 | /* Todo handle any errors */ | ||
| 166 | int ecc_result1, ecc_result2; | ||
| 167 | u8 calc_ecc[3]; | ||
| 168 | |||
| 169 | yaffs_ecc_cacl(data, calc_ecc); | ||
| 170 | ecc_result1 = | ||
| 171 | yaffs_ecc_correct(data, spare->ecc1, calc_ecc); | ||
| 172 | yaffs_ecc_cacl(&data[256], calc_ecc); | ||
| 173 | ecc_result2 = | ||
| 174 | yaffs_ecc_correct(&data[256], spare->ecc2, | ||
| 175 | calc_ecc); | ||
| 176 | |||
| 177 | if (ecc_result1 > 0) { | ||
| 178 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 179 | "**>>yaffs ecc error fix performed on chunk %d:0", | ||
| 180 | nand_chunk); | ||
| 181 | dev->n_ecc_fixed++; | ||
| 182 | } else if (ecc_result1 < 0) { | ||
| 183 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 184 | "**>>yaffs ecc error unfixed on chunk %d:0", | ||
| 185 | nand_chunk); | ||
| 186 | dev->n_ecc_unfixed++; | ||
| 187 | } | ||
| 188 | |||
| 189 | if (ecc_result2 > 0) { | ||
| 190 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 191 | "**>>yaffs ecc error fix performed on chunk %d:1", | ||
| 192 | nand_chunk); | ||
| 193 | dev->n_ecc_fixed++; | ||
| 194 | } else if (ecc_result2 < 0) { | ||
| 195 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 196 | "**>>yaffs ecc error unfixed on chunk %d:1", | ||
| 197 | nand_chunk); | ||
| 198 | dev->n_ecc_unfixed++; | ||
| 199 | } | ||
| 200 | |||
| 201 | if (ecc_result1 || ecc_result2) { | ||
| 202 | /* We had a data problem on this page */ | ||
| 203 | yaffs_handle_rd_data_error(dev, nand_chunk); | ||
| 204 | } | ||
| 205 | |||
| 206 | if (ecc_result1 < 0 || ecc_result2 < 0) | ||
| 207 | *ecc_result = YAFFS_ECC_RESULT_UNFIXED; | ||
| 208 | else if (ecc_result1 > 0 || ecc_result2 > 0) | ||
| 209 | *ecc_result = YAFFS_ECC_RESULT_FIXED; | ||
| 210 | else | ||
| 211 | *ecc_result = YAFFS_ECC_RESULT_NO_ERROR; | ||
| 212 | } | ||
| 213 | } else { | ||
| 214 | /* Must allocate enough memory for spare+2*sizeof(int) */ | ||
| 215 | /* for ecc results from device. */ | ||
| 216 | struct yaffs_nand_spare nspare; | ||
| 217 | |||
| 218 | memset(&nspare, 0, sizeof(nspare)); | ||
| 219 | |||
| 220 | ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data, | ||
| 221 | (struct yaffs_spare *) | ||
| 222 | &nspare); | ||
| 223 | memcpy(spare, &nspare, sizeof(struct yaffs_spare)); | ||
| 224 | if (data && correct_errors) { | ||
| 225 | if (nspare.eccres1 > 0) { | ||
| 226 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 227 | "**>>mtd ecc error fix performed on chunk %d:0", | ||
| 228 | nand_chunk); | ||
| 229 | } else if (nspare.eccres1 < 0) { | ||
| 230 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 231 | "**>>mtd ecc error unfixed on chunk %d:0", | ||
| 232 | nand_chunk); | ||
| 233 | } | ||
| 234 | |||
| 235 | if (nspare.eccres2 > 0) { | ||
| 236 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 237 | "**>>mtd ecc error fix performed on chunk %d:1", | ||
| 238 | nand_chunk); | ||
| 239 | } else if (nspare.eccres2 < 0) { | ||
| 240 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 241 | "**>>mtd ecc error unfixed on chunk %d:1", | ||
| 242 | nand_chunk); | ||
| 243 | } | ||
| 244 | |||
| 245 | if (nspare.eccres1 || nspare.eccres2) { | ||
| 246 | /* We had a data problem on this page */ | ||
| 247 | yaffs_handle_rd_data_error(dev, nand_chunk); | ||
| 248 | } | ||
| 249 | |||
| 250 | if (nspare.eccres1 < 0 || nspare.eccres2 < 0) | ||
| 251 | *ecc_result = YAFFS_ECC_RESULT_UNFIXED; | ||
| 252 | else if (nspare.eccres1 > 0 || nspare.eccres2 > 0) | ||
| 253 | *ecc_result = YAFFS_ECC_RESULT_FIXED; | ||
| 254 | else | ||
| 255 | *ecc_result = YAFFS_ECC_RESULT_NO_ERROR; | ||
| 256 | |||
| 257 | } | ||
| 258 | } | ||
| 259 | return ret_val; | ||
| 260 | } | ||
| 261 | |||
| 262 | /* | ||
| 263 | * Functions for robustisizing | ||
| 264 | */ | ||
| 265 | |||
| 266 | static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk) | ||
| 267 | { | ||
| 268 | int flash_block = nand_chunk / dev->param.chunks_per_block; | ||
| 269 | |||
| 270 | /* Mark the block for retirement */ | ||
| 271 | yaffs_get_block_info(dev, | ||
| 272 | flash_block + dev->block_offset)->needs_retiring = | ||
| 273 | 1; | ||
| 274 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, | ||
| 275 | "**>>Block %d marked for retirement", | ||
| 276 | flash_block); | ||
| 277 | |||
| 278 | /* TODO: | ||
| 279 | * Just do a garbage collection on the affected block | ||
| 280 | * then retire the block | ||
| 281 | * NB recursion | ||
| 282 | */ | ||
| 283 | } | ||
| 284 | |||
| 285 | int yaffs_tags_compat_wr(struct yaffs_dev *dev, | ||
| 286 | int nand_chunk, | ||
| 287 | const u8 * data, const struct yaffs_ext_tags *ext_tags) | ||
| 288 | { | ||
| 289 | struct yaffs_spare spare; | ||
| 290 | struct yaffs_tags tags; | ||
| 291 | |||
| 292 | yaffs_spare_init(&spare); | ||
| 293 | |||
| 294 | if (ext_tags->is_deleted) | ||
| 295 | spare.page_status = 0; | ||
| 296 | else { | ||
| 297 | tags.obj_id = ext_tags->obj_id; | ||
| 298 | tags.chunk_id = ext_tags->chunk_id; | ||
| 299 | |||
| 300 | tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff; | ||
| 301 | |||
| 302 | if (dev->data_bytes_per_chunk >= 1024) | ||
| 303 | tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3; | ||
| 304 | else | ||
| 305 | tags.n_bytes_msb = 3; | ||
| 306 | |||
| 307 | tags.serial_number = ext_tags->serial_number; | ||
| 308 | |||
| 309 | if (!dev->param.use_nand_ecc && data) | ||
| 310 | yaffs_calc_ecc(data, &spare); | ||
| 311 | |||
| 312 | yaffs_load_tags_to_spare(&spare, &tags); | ||
| 313 | |||
| 314 | } | ||
| 315 | |||
| 316 | return yaffs_wr_nand(dev, nand_chunk, data, &spare); | ||
| 317 | } | ||
| 318 | |||
| 319 | int yaffs_tags_compat_rd(struct yaffs_dev *dev, | ||
| 320 | int nand_chunk, | ||
| 321 | u8 * data, struct yaffs_ext_tags *ext_tags) | ||
| 322 | { | ||
| 323 | |||
| 324 | struct yaffs_spare spare; | ||
| 325 | struct yaffs_tags tags; | ||
| 326 | enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN; | ||
| 327 | |||
| 328 | static struct yaffs_spare spare_ff; | ||
| 329 | static int init; | ||
| 330 | |||
| 331 | if (!init) { | ||
| 332 | memset(&spare_ff, 0xFF, sizeof(spare_ff)); | ||
| 333 | init = 1; | ||
| 334 | } | ||
| 335 | |||
| 336 | if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) { | ||
| 337 | /* ext_tags may be NULL */ | ||
| 338 | if (ext_tags) { | ||
| 339 | |||
| 340 | int deleted = | ||
| 341 | (hweight8(spare.page_status) < 7) ? 1 : 0; | ||
| 342 | |||
| 343 | ext_tags->is_deleted = deleted; | ||
| 344 | ext_tags->ecc_result = ecc_result; | ||
| 345 | ext_tags->block_bad = 0; /* We're reading it */ | ||
| 346 | /* therefore it is not a bad block */ | ||
| 347 | ext_tags->chunk_used = | ||
| 348 | (memcmp(&spare_ff, &spare, sizeof(spare_ff)) != | ||
| 349 | 0) ? 1 : 0; | ||
| 350 | |||
| 351 | if (ext_tags->chunk_used) { | ||
| 352 | yaffs_get_tags_from_spare(dev, &spare, &tags); | ||
| 353 | |||
| 354 | ext_tags->obj_id = tags.obj_id; | ||
| 355 | ext_tags->chunk_id = tags.chunk_id; | ||
| 356 | ext_tags->n_bytes = tags.n_bytes_lsb; | ||
| 357 | |||
| 358 | if (dev->data_bytes_per_chunk >= 1024) | ||
| 359 | ext_tags->n_bytes |= | ||
| 360 | (((unsigned)tags. | ||
| 361 | n_bytes_msb) << 10); | ||
| 362 | |||
| 363 | ext_tags->serial_number = tags.serial_number; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | return YAFFS_OK; | ||
| 368 | } else { | ||
| 369 | return YAFFS_FAIL; | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block) | ||
| 374 | { | ||
| 375 | |||
| 376 | struct yaffs_spare spare; | ||
| 377 | |||
| 378 | memset(&spare, 0xff, sizeof(struct yaffs_spare)); | ||
| 379 | |||
| 380 | spare.block_status = 'Y'; | ||
| 381 | |||
| 382 | yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL, | ||
| 383 | &spare); | ||
| 384 | yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1, | ||
| 385 | NULL, &spare); | ||
| 386 | |||
| 387 | return YAFFS_OK; | ||
| 388 | |||
| 389 | } | ||
| 390 | |||
| 391 | int yaffs_tags_compat_query_block(struct yaffs_dev *dev, | ||
| 392 | int block_no, | ||
| 393 | enum yaffs_block_state *state, | ||
| 394 | u32 * seq_number) | ||
| 395 | { | ||
| 396 | |||
| 397 | struct yaffs_spare spare0, spare1; | ||
| 398 | static struct yaffs_spare spare_ff; | ||
| 399 | static int init; | ||
| 400 | enum yaffs_ecc_result dummy; | ||
| 401 | |||
| 402 | if (!init) { | ||
| 403 | memset(&spare_ff, 0xFF, sizeof(spare_ff)); | ||
| 404 | init = 1; | ||
| 405 | } | ||
| 406 | |||
| 407 | *seq_number = 0; | ||
| 408 | |||
| 409 | yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL, | ||
| 410 | &spare0, &dummy, 1); | ||
| 411 | yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1, | ||
| 412 | NULL, &spare1, &dummy, 1); | ||
| 413 | |||
| 414 | if (hweight8(spare0.block_status & spare1.block_status) < 7) | ||
| 415 | *state = YAFFS_BLOCK_STATE_DEAD; | ||
| 416 | else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0) | ||
| 417 | *state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 418 | else | ||
| 419 | *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; | ||
| 420 | |||
| 421 | return YAFFS_OK; | ||
| 422 | } | ||
diff --git a/fs/yaffs2/yaffs_tagscompat.h b/fs/yaffs2/yaffs_tagscompat.h new file mode 100644 index 00000000000..8cd35dcd3ca --- /dev/null +++ b/fs/yaffs2/yaffs_tagscompat.h | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_TAGSCOMPAT_H__ | ||
| 17 | #define __YAFFS_TAGSCOMPAT_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | int yaffs_tags_compat_wr(struct yaffs_dev *dev, | ||
| 21 | int nand_chunk, | ||
| 22 | const u8 * data, const struct yaffs_ext_tags *tags); | ||
| 23 | int yaffs_tags_compat_rd(struct yaffs_dev *dev, | ||
| 24 | int nand_chunk, | ||
| 25 | u8 * data, struct yaffs_ext_tags *tags); | ||
| 26 | int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no); | ||
| 27 | int yaffs_tags_compat_query_block(struct yaffs_dev *dev, | ||
| 28 | int block_no, | ||
| 29 | enum yaffs_block_state *state, | ||
| 30 | u32 * seq_number); | ||
| 31 | |||
| 32 | void yaffs_calc_tags_ecc(struct yaffs_tags *tags); | ||
| 33 | int yaffs_check_tags_ecc(struct yaffs_tags *tags); | ||
| 34 | int yaffs_count_bits(u8 byte); | ||
| 35 | |||
| 36 | #endif | ||
diff --git a/fs/yaffs2/yaffs_tagsvalidity.c b/fs/yaffs2/yaffs_tagsvalidity.c new file mode 100644 index 00000000000..4358d79d4be --- /dev/null +++ b/fs/yaffs2/yaffs_tagsvalidity.c | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_tagsvalidity.h" | ||
| 15 | |||
| 16 | void yaffs_init_tags(struct yaffs_ext_tags *tags) | ||
| 17 | { | ||
| 18 | memset(tags, 0, sizeof(struct yaffs_ext_tags)); | ||
| 19 | tags->validity0 = 0xAAAAAAAA; | ||
| 20 | tags->validity1 = 0x55555555; | ||
| 21 | } | ||
| 22 | |||
| 23 | int yaffs_validate_tags(struct yaffs_ext_tags *tags) | ||
| 24 | { | ||
| 25 | return (tags->validity0 == 0xAAAAAAAA && tags->validity1 == 0x55555555); | ||
| 26 | |||
| 27 | } | ||
diff --git a/fs/yaffs2/yaffs_tagsvalidity.h b/fs/yaffs2/yaffs_tagsvalidity.h new file mode 100644 index 00000000000..36a021fc8fa --- /dev/null +++ b/fs/yaffs2/yaffs_tagsvalidity.h | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_TAGS_VALIDITY_H__ | ||
| 17 | #define __YAFFS_TAGS_VALIDITY_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | void yaffs_init_tags(struct yaffs_ext_tags *tags); | ||
| 22 | int yaffs_validate_tags(struct yaffs_ext_tags *tags); | ||
| 23 | #endif | ||
diff --git a/fs/yaffs2/yaffs_trace.h b/fs/yaffs2/yaffs_trace.h new file mode 100644 index 00000000000..6273dbf9f63 --- /dev/null +++ b/fs/yaffs2/yaffs_trace.h | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YTRACE_H__ | ||
| 17 | #define __YTRACE_H__ | ||
| 18 | |||
| 19 | extern unsigned int yaffs_trace_mask; | ||
| 20 | extern unsigned int yaffs_wr_attempts; | ||
| 21 | |||
| 22 | /* | ||
| 23 | * Tracing flags. | ||
| 24 | * The flags masked in YAFFS_TRACE_ALWAYS are always traced. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #define YAFFS_TRACE_OS 0x00000002 | ||
| 28 | #define YAFFS_TRACE_ALLOCATE 0x00000004 | ||
| 29 | #define YAFFS_TRACE_SCAN 0x00000008 | ||
| 30 | #define YAFFS_TRACE_BAD_BLOCKS 0x00000010 | ||
| 31 | #define YAFFS_TRACE_ERASE 0x00000020 | ||
| 32 | #define YAFFS_TRACE_GC 0x00000040 | ||
| 33 | #define YAFFS_TRACE_WRITE 0x00000080 | ||
| 34 | #define YAFFS_TRACE_TRACING 0x00000100 | ||
| 35 | #define YAFFS_TRACE_DELETION 0x00000200 | ||
| 36 | #define YAFFS_TRACE_BUFFERS 0x00000400 | ||
| 37 | #define YAFFS_TRACE_NANDACCESS 0x00000800 | ||
| 38 | #define YAFFS_TRACE_GC_DETAIL 0x00001000 | ||
| 39 | #define YAFFS_TRACE_SCAN_DEBUG 0x00002000 | ||
| 40 | #define YAFFS_TRACE_MTD 0x00004000 | ||
| 41 | #define YAFFS_TRACE_CHECKPOINT 0x00008000 | ||
| 42 | |||
| 43 | #define YAFFS_TRACE_VERIFY 0x00010000 | ||
| 44 | #define YAFFS_TRACE_VERIFY_NAND 0x00020000 | ||
| 45 | #define YAFFS_TRACE_VERIFY_FULL 0x00040000 | ||
| 46 | #define YAFFS_TRACE_VERIFY_ALL 0x000F0000 | ||
| 47 | |||
| 48 | #define YAFFS_TRACE_SYNC 0x00100000 | ||
| 49 | #define YAFFS_TRACE_BACKGROUND 0x00200000 | ||
| 50 | #define YAFFS_TRACE_LOCK 0x00400000 | ||
| 51 | #define YAFFS_TRACE_MOUNT 0x00800000 | ||
| 52 | |||
| 53 | #define YAFFS_TRACE_ERROR 0x40000000 | ||
| 54 | #define YAFFS_TRACE_BUG 0x80000000 | ||
| 55 | #define YAFFS_TRACE_ALWAYS 0xF0000000 | ||
| 56 | |||
| 57 | #endif | ||
diff --git a/fs/yaffs2/yaffs_verify.c b/fs/yaffs2/yaffs_verify.c new file mode 100644 index 00000000000..738c7f69a5e --- /dev/null +++ b/fs/yaffs2/yaffs_verify.c | |||
| @@ -0,0 +1,535 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_verify.h" | ||
| 15 | #include "yaffs_trace.h" | ||
| 16 | #include "yaffs_bitmap.h" | ||
| 17 | #include "yaffs_getblockinfo.h" | ||
| 18 | #include "yaffs_nand.h" | ||
| 19 | |||
| 20 | int yaffs_skip_verification(struct yaffs_dev *dev) | ||
| 21 | { | ||
| 22 | dev = dev; | ||
| 23 | return !(yaffs_trace_mask & | ||
| 24 | (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL)); | ||
| 25 | } | ||
| 26 | |||
| 27 | static int yaffs_skip_full_verification(struct yaffs_dev *dev) | ||
| 28 | { | ||
| 29 | dev = dev; | ||
| 30 | return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL)); | ||
| 31 | } | ||
| 32 | |||
| 33 | static int yaffs_skip_nand_verification(struct yaffs_dev *dev) | ||
| 34 | { | ||
| 35 | dev = dev; | ||
| 36 | return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND)); | ||
| 37 | } | ||
| 38 | |||
| 39 | static const char *block_state_name[] = { | ||
| 40 | "Unknown", | ||
| 41 | "Needs scanning", | ||
| 42 | "Scanning", | ||
| 43 | "Empty", | ||
| 44 | "Allocating", | ||
| 45 | "Full", | ||
| 46 | "Dirty", | ||
| 47 | "Checkpoint", | ||
| 48 | "Collecting", | ||
| 49 | "Dead" | ||
| 50 | }; | ||
| 51 | |||
| 52 | void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n) | ||
| 53 | { | ||
| 54 | int actually_used; | ||
| 55 | int in_use; | ||
| 56 | |||
| 57 | if (yaffs_skip_verification(dev)) | ||
| 58 | return; | ||
| 59 | |||
| 60 | /* Report illegal runtime states */ | ||
| 61 | if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES) | ||
| 62 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 63 | "Block %d has undefined state %d", | ||
| 64 | n, bi->block_state); | ||
| 65 | |||
| 66 | switch (bi->block_state) { | ||
| 67 | case YAFFS_BLOCK_STATE_UNKNOWN: | ||
| 68 | case YAFFS_BLOCK_STATE_SCANNING: | ||
| 69 | case YAFFS_BLOCK_STATE_NEEDS_SCANNING: | ||
| 70 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 71 | "Block %d has bad run-state %s", | ||
| 72 | n, block_state_name[bi->block_state]); | ||
| 73 | } | ||
| 74 | |||
| 75 | /* Check pages in use and soft deletions are legal */ | ||
| 76 | |||
| 77 | actually_used = bi->pages_in_use - bi->soft_del_pages; | ||
| 78 | |||
| 79 | if (bi->pages_in_use < 0 | ||
| 80 | || bi->pages_in_use > dev->param.chunks_per_block | ||
| 81 | || bi->soft_del_pages < 0 | ||
| 82 | || bi->soft_del_pages > dev->param.chunks_per_block | ||
| 83 | || actually_used < 0 || actually_used > dev->param.chunks_per_block) | ||
| 84 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 85 | "Block %d has illegal values pages_in_used %d soft_del_pages %d", | ||
| 86 | n, bi->pages_in_use, bi->soft_del_pages); | ||
| 87 | |||
| 88 | /* Check chunk bitmap legal */ | ||
| 89 | in_use = yaffs_count_chunk_bits(dev, n); | ||
| 90 | if (in_use != bi->pages_in_use) | ||
| 91 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 92 | "Block %d has inconsistent values pages_in_use %d counted chunk bits %d", | ||
| 93 | n, bi->pages_in_use, in_use); | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | void yaffs_verify_collected_blk(struct yaffs_dev *dev, | ||
| 98 | struct yaffs_block_info *bi, int n) | ||
| 99 | { | ||
| 100 | yaffs_verify_blk(dev, bi, n); | ||
| 101 | |||
| 102 | /* After collection the block should be in the erased state */ | ||
| 103 | |||
| 104 | if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING && | ||
| 105 | bi->block_state != YAFFS_BLOCK_STATE_EMPTY) { | ||
| 106 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 107 | "Block %d is in state %d after gc, should be erased", | ||
| 108 | n, bi->block_state); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | void yaffs_verify_blocks(struct yaffs_dev *dev) | ||
| 113 | { | ||
| 114 | int i; | ||
| 115 | int state_count[YAFFS_NUMBER_OF_BLOCK_STATES]; | ||
| 116 | int illegal_states = 0; | ||
| 117 | |||
| 118 | if (yaffs_skip_verification(dev)) | ||
| 119 | return; | ||
| 120 | |||
| 121 | memset(state_count, 0, sizeof(state_count)); | ||
| 122 | |||
| 123 | for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { | ||
| 124 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, i); | ||
| 125 | yaffs_verify_blk(dev, bi, i); | ||
| 126 | |||
| 127 | if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES) | ||
| 128 | state_count[bi->block_state]++; | ||
| 129 | else | ||
| 130 | illegal_states++; | ||
| 131 | } | ||
| 132 | |||
| 133 | yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary"); | ||
| 134 | |||
| 135 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 136 | "%d blocks have illegal states", | ||
| 137 | illegal_states); | ||
| 138 | if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1) | ||
| 139 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 140 | "Too many allocating blocks"); | ||
| 141 | |||
| 142 | for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++) | ||
| 143 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 144 | "%s %d blocks", | ||
| 145 | block_state_name[i], state_count[i]); | ||
| 146 | |||
| 147 | if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT]) | ||
| 148 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 149 | "Checkpoint block count wrong dev %d count %d", | ||
| 150 | dev->blocks_in_checkpt, | ||
| 151 | state_count[YAFFS_BLOCK_STATE_CHECKPOINT]); | ||
| 152 | |||
| 153 | if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY]) | ||
| 154 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 155 | "Erased block count wrong dev %d count %d", | ||
| 156 | dev->n_erased_blocks, | ||
| 157 | state_count[YAFFS_BLOCK_STATE_EMPTY]); | ||
| 158 | |||
| 159 | if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1) | ||
| 160 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 161 | "Too many collecting blocks %d (max is 1)", | ||
| 162 | state_count[YAFFS_BLOCK_STATE_COLLECTING]); | ||
| 163 | } | ||
| 164 | |||
| 165 | /* | ||
| 166 | * Verify the object header. oh must be valid, but obj and tags may be NULL in which | ||
| 167 | * case those tests will not be performed. | ||
| 168 | */ | ||
| 169 | void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh, | ||
| 170 | struct yaffs_ext_tags *tags, int parent_check) | ||
| 171 | { | ||
| 172 | if (obj && yaffs_skip_verification(obj->my_dev)) | ||
| 173 | return; | ||
| 174 | |||
| 175 | if (!(tags && obj && oh)) { | ||
| 176 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 177 | "Verifying object header tags %p obj %p oh %p", | ||
| 178 | tags, obj, oh); | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | |||
| 182 | if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN || | ||
| 183 | oh->type > YAFFS_OBJECT_TYPE_MAX) | ||
| 184 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 185 | "Obj %d header type is illegal value 0x%x", | ||
| 186 | tags->obj_id, oh->type); | ||
| 187 | |||
| 188 | if (tags->obj_id != obj->obj_id) | ||
| 189 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 190 | "Obj %d header mismatch obj_id %d", | ||
| 191 | tags->obj_id, obj->obj_id); | ||
| 192 | |||
| 193 | /* | ||
| 194 | * Check that the object's parent ids match if parent_check requested. | ||
| 195 | * | ||
| 196 | * Tests do not apply to the root object. | ||
| 197 | */ | ||
| 198 | |||
| 199 | if (parent_check && tags->obj_id > 1 && !obj->parent) | ||
| 200 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 201 | "Obj %d header mismatch parent_id %d obj->parent is NULL", | ||
| 202 | tags->obj_id, oh->parent_obj_id); | ||
| 203 | |||
| 204 | if (parent_check && obj->parent && | ||
| 205 | oh->parent_obj_id != obj->parent->obj_id && | ||
| 206 | (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED || | ||
| 207 | obj->parent->obj_id != YAFFS_OBJECTID_DELETED)) | ||
| 208 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 209 | "Obj %d header mismatch parent_id %d parent_obj_id %d", | ||
| 210 | tags->obj_id, oh->parent_obj_id, | ||
| 211 | obj->parent->obj_id); | ||
| 212 | |||
| 213 | if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */ | ||
| 214 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 215 | "Obj %d header name is NULL", | ||
| 216 | obj->obj_id); | ||
| 217 | |||
| 218 | if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Trashed name */ | ||
| 219 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 220 | "Obj %d header name is 0xFF", | ||
| 221 | obj->obj_id); | ||
| 222 | } | ||
| 223 | |||
| 224 | void yaffs_verify_file(struct yaffs_obj *obj) | ||
| 225 | { | ||
| 226 | int required_depth; | ||
| 227 | int actual_depth; | ||
| 228 | u32 last_chunk; | ||
| 229 | u32 x; | ||
| 230 | u32 i; | ||
| 231 | struct yaffs_dev *dev; | ||
| 232 | struct yaffs_ext_tags tags; | ||
| 233 | struct yaffs_tnode *tn; | ||
| 234 | u32 obj_id; | ||
| 235 | |||
| 236 | if (!obj) | ||
| 237 | return; | ||
| 238 | |||
| 239 | if (yaffs_skip_verification(obj->my_dev)) | ||
| 240 | return; | ||
| 241 | |||
| 242 | dev = obj->my_dev; | ||
| 243 | obj_id = obj->obj_id; | ||
| 244 | |||
| 245 | /* Check file size is consistent with tnode depth */ | ||
| 246 | last_chunk = | ||
| 247 | obj->variant.file_variant.file_size / dev->data_bytes_per_chunk + 1; | ||
| 248 | x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS; | ||
| 249 | required_depth = 0; | ||
| 250 | while (x > 0) { | ||
| 251 | x >>= YAFFS_TNODES_INTERNAL_BITS; | ||
| 252 | required_depth++; | ||
| 253 | } | ||
| 254 | |||
| 255 | actual_depth = obj->variant.file_variant.top_level; | ||
| 256 | |||
| 257 | /* Check that the chunks in the tnode tree are all correct. | ||
| 258 | * We do this by scanning through the tnode tree and | ||
| 259 | * checking the tags for every chunk match. | ||
| 260 | */ | ||
| 261 | |||
| 262 | if (yaffs_skip_nand_verification(dev)) | ||
| 263 | return; | ||
| 264 | |||
| 265 | for (i = 1; i <= last_chunk; i++) { | ||
| 266 | tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i); | ||
| 267 | |||
| 268 | if (tn) { | ||
| 269 | u32 the_chunk = yaffs_get_group_base(dev, tn, i); | ||
| 270 | if (the_chunk > 0) { | ||
| 271 | yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL, | ||
| 272 | &tags); | ||
| 273 | if (tags.obj_id != obj_id || tags.chunk_id != i) | ||
| 274 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 275 | "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)", | ||
| 276 | obj_id, i, the_chunk, | ||
| 277 | tags.obj_id, tags.chunk_id); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | void yaffs_verify_link(struct yaffs_obj *obj) | ||
| 284 | { | ||
| 285 | if (obj && yaffs_skip_verification(obj->my_dev)) | ||
| 286 | return; | ||
| 287 | |||
| 288 | /* Verify sane equivalent object */ | ||
| 289 | } | ||
| 290 | |||
| 291 | void yaffs_verify_symlink(struct yaffs_obj *obj) | ||
| 292 | { | ||
| 293 | if (obj && yaffs_skip_verification(obj->my_dev)) | ||
| 294 | return; | ||
| 295 | |||
| 296 | /* Verify symlink string */ | ||
| 297 | } | ||
| 298 | |||
| 299 | void yaffs_verify_special(struct yaffs_obj *obj) | ||
| 300 | { | ||
| 301 | if (obj && yaffs_skip_verification(obj->my_dev)) | ||
| 302 | return; | ||
| 303 | } | ||
| 304 | |||
| 305 | void yaffs_verify_obj(struct yaffs_obj *obj) | ||
| 306 | { | ||
| 307 | struct yaffs_dev *dev; | ||
| 308 | |||
| 309 | u32 chunk_min; | ||
| 310 | u32 chunk_max; | ||
| 311 | |||
| 312 | u32 chunk_id_ok; | ||
| 313 | u32 chunk_in_range; | ||
| 314 | u32 chunk_wrongly_deleted; | ||
| 315 | u32 chunk_valid; | ||
| 316 | |||
| 317 | if (!obj) | ||
| 318 | return; | ||
| 319 | |||
| 320 | if (obj->being_created) | ||
| 321 | return; | ||
| 322 | |||
| 323 | dev = obj->my_dev; | ||
| 324 | |||
| 325 | if (yaffs_skip_verification(dev)) | ||
| 326 | return; | ||
| 327 | |||
| 328 | /* Check sane object header chunk */ | ||
| 329 | |||
| 330 | chunk_min = dev->internal_start_block * dev->param.chunks_per_block; | ||
| 331 | chunk_max = | ||
| 332 | (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1; | ||
| 333 | |||
| 334 | chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min && | ||
| 335 | ((unsigned)(obj->hdr_chunk)) <= chunk_max); | ||
| 336 | chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0); | ||
| 337 | chunk_valid = chunk_in_range && | ||
| 338 | yaffs_check_chunk_bit(dev, | ||
| 339 | obj->hdr_chunk / dev->param.chunks_per_block, | ||
| 340 | obj->hdr_chunk % dev->param.chunks_per_block); | ||
| 341 | chunk_wrongly_deleted = chunk_in_range && !chunk_valid; | ||
| 342 | |||
| 343 | if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted)) | ||
| 344 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 345 | "Obj %d has chunk_id %d %s %s", | ||
| 346 | obj->obj_id, obj->hdr_chunk, | ||
| 347 | chunk_id_ok ? "" : ",out of range", | ||
| 348 | chunk_wrongly_deleted ? ",marked as deleted" : ""); | ||
| 349 | |||
| 350 | if (chunk_valid && !yaffs_skip_nand_verification(dev)) { | ||
| 351 | struct yaffs_ext_tags tags; | ||
| 352 | struct yaffs_obj_hdr *oh; | ||
| 353 | u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 354 | |||
| 355 | oh = (struct yaffs_obj_hdr *)buffer; | ||
| 356 | |||
| 357 | yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags); | ||
| 358 | |||
| 359 | yaffs_verify_oh(obj, oh, &tags, 1); | ||
| 360 | |||
| 361 | yaffs_release_temp_buffer(dev, buffer, __LINE__); | ||
| 362 | } | ||
| 363 | |||
| 364 | /* Verify it has a parent */ | ||
| 365 | if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) { | ||
| 366 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 367 | "Obj %d has parent pointer %p which does not look like an object", | ||
| 368 | obj->obj_id, obj->parent); | ||
| 369 | } | ||
| 370 | |||
| 371 | /* Verify parent is a directory */ | ||
| 372 | if (obj->parent | ||
| 373 | && obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 374 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 375 | "Obj %d's parent is not a directory (type %d)", | ||
| 376 | obj->obj_id, obj->parent->variant_type); | ||
| 377 | } | ||
| 378 | |||
| 379 | switch (obj->variant_type) { | ||
| 380 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 381 | yaffs_verify_file(obj); | ||
| 382 | break; | ||
| 383 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 384 | yaffs_verify_symlink(obj); | ||
| 385 | break; | ||
| 386 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 387 | yaffs_verify_dir(obj); | ||
| 388 | break; | ||
| 389 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 390 | yaffs_verify_link(obj); | ||
| 391 | break; | ||
| 392 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 393 | yaffs_verify_special(obj); | ||
| 394 | break; | ||
| 395 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 396 | default: | ||
| 397 | yaffs_trace(YAFFS_TRACE_VERIFY, | ||
| 398 | "Obj %d has illegaltype %d", | ||
| 399 | obj->obj_id, obj->variant_type); | ||
| 400 | break; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | |||
| 404 | void yaffs_verify_objects(struct yaffs_dev *dev) | ||
| 405 | { | ||
| 406 | struct yaffs_obj *obj; | ||
| 407 | int i; | ||
| 408 | struct list_head *lh; | ||
| 409 | |||
| 410 | if (yaffs_skip_verification(dev)) | ||
| 411 | return; | ||
| 412 | |||
| 413 | /* Iterate through the objects in each hash entry */ | ||
| 414 | |||
| 415 | for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { | ||
| 416 | list_for_each(lh, &dev->obj_bucket[i].list) { | ||
| 417 | if (lh) { | ||
| 418 | obj = | ||
| 419 | list_entry(lh, struct yaffs_obj, hash_link); | ||
| 420 | yaffs_verify_obj(obj); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | void yaffs_verify_obj_in_dir(struct yaffs_obj *obj) | ||
| 427 | { | ||
| 428 | struct list_head *lh; | ||
| 429 | struct yaffs_obj *list_obj; | ||
| 430 | |||
| 431 | int count = 0; | ||
| 432 | |||
| 433 | if (!obj) { | ||
| 434 | yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify"); | ||
| 435 | YBUG(); | ||
| 436 | return; | ||
| 437 | } | ||
| 438 | |||
| 439 | if (yaffs_skip_verification(obj->my_dev)) | ||
| 440 | return; | ||
| 441 | |||
| 442 | if (!obj->parent) { | ||
| 443 | yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent" ); | ||
| 444 | YBUG(); | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | |||
| 448 | if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 449 | yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory"); | ||
| 450 | YBUG(); | ||
| 451 | } | ||
| 452 | |||
| 453 | /* Iterate through the objects in each hash entry */ | ||
| 454 | |||
| 455 | list_for_each(lh, &obj->parent->variant.dir_variant.children) { | ||
| 456 | if (lh) { | ||
| 457 | list_obj = list_entry(lh, struct yaffs_obj, siblings); | ||
| 458 | yaffs_verify_obj(list_obj); | ||
| 459 | if (obj == list_obj) | ||
| 460 | count++; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | if (count != 1) { | ||
| 465 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 466 | "Object in directory %d times", | ||
| 467 | count); | ||
| 468 | YBUG(); | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | void yaffs_verify_dir(struct yaffs_obj *directory) | ||
| 473 | { | ||
| 474 | struct list_head *lh; | ||
| 475 | struct yaffs_obj *list_obj; | ||
| 476 | |||
| 477 | if (!directory) { | ||
| 478 | YBUG(); | ||
| 479 | return; | ||
| 480 | } | ||
| 481 | |||
| 482 | if (yaffs_skip_full_verification(directory->my_dev)) | ||
| 483 | return; | ||
| 484 | |||
| 485 | if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 486 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 487 | "Directory has wrong type: %d", | ||
| 488 | directory->variant_type); | ||
| 489 | YBUG(); | ||
| 490 | } | ||
| 491 | |||
| 492 | /* Iterate through the objects in each hash entry */ | ||
| 493 | |||
| 494 | list_for_each(lh, &directory->variant.dir_variant.children) { | ||
| 495 | if (lh) { | ||
| 496 | list_obj = list_entry(lh, struct yaffs_obj, siblings); | ||
| 497 | if (list_obj->parent != directory) { | ||
| 498 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 499 | "Object in directory list has wrong parent %p", | ||
| 500 | list_obj->parent); | ||
| 501 | YBUG(); | ||
| 502 | } | ||
| 503 | yaffs_verify_obj_in_dir(list_obj); | ||
| 504 | } | ||
| 505 | } | ||
| 506 | } | ||
| 507 | |||
| 508 | static int yaffs_free_verification_failures; | ||
| 509 | |||
| 510 | void yaffs_verify_free_chunks(struct yaffs_dev *dev) | ||
| 511 | { | ||
| 512 | int counted; | ||
| 513 | int difference; | ||
| 514 | |||
| 515 | if (yaffs_skip_verification(dev)) | ||
| 516 | return; | ||
| 517 | |||
| 518 | counted = yaffs_count_free_chunks(dev); | ||
| 519 | |||
| 520 | difference = dev->n_free_chunks - counted; | ||
| 521 | |||
| 522 | if (difference) { | ||
| 523 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 524 | "Freechunks verification failure %d %d %d", | ||
| 525 | dev->n_free_chunks, counted, difference); | ||
| 526 | yaffs_free_verification_failures++; | ||
| 527 | } | ||
| 528 | } | ||
| 529 | |||
| 530 | int yaffs_verify_file_sane(struct yaffs_obj *in) | ||
| 531 | { | ||
| 532 | in = in; | ||
| 533 | return YAFFS_OK; | ||
| 534 | } | ||
| 535 | |||
diff --git a/fs/yaffs2/yaffs_verify.h b/fs/yaffs2/yaffs_verify.h new file mode 100644 index 00000000000..cc6f8899930 --- /dev/null +++ b/fs/yaffs2/yaffs_verify.h | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_VERIFY_H__ | ||
| 17 | #define __YAFFS_VERIFY_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, | ||
| 22 | int n); | ||
| 23 | void yaffs_verify_collected_blk(struct yaffs_dev *dev, | ||
| 24 | struct yaffs_block_info *bi, int n); | ||
| 25 | void yaffs_verify_blocks(struct yaffs_dev *dev); | ||
| 26 | |||
| 27 | void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh, | ||
| 28 | struct yaffs_ext_tags *tags, int parent_check); | ||
| 29 | void yaffs_verify_file(struct yaffs_obj *obj); | ||
| 30 | void yaffs_verify_link(struct yaffs_obj *obj); | ||
| 31 | void yaffs_verify_symlink(struct yaffs_obj *obj); | ||
| 32 | void yaffs_verify_special(struct yaffs_obj *obj); | ||
| 33 | void yaffs_verify_obj(struct yaffs_obj *obj); | ||
| 34 | void yaffs_verify_objects(struct yaffs_dev *dev); | ||
| 35 | void yaffs_verify_obj_in_dir(struct yaffs_obj *obj); | ||
| 36 | void yaffs_verify_dir(struct yaffs_obj *directory); | ||
| 37 | void yaffs_verify_free_chunks(struct yaffs_dev *dev); | ||
| 38 | |||
| 39 | int yaffs_verify_file_sane(struct yaffs_obj *obj); | ||
| 40 | |||
| 41 | int yaffs_skip_verification(struct yaffs_dev *dev); | ||
| 42 | |||
| 43 | #endif | ||
diff --git a/fs/yaffs2/yaffs_vfs.c b/fs/yaffs2/yaffs_vfs.c new file mode 100644 index 00000000000..d95875fe1e5 --- /dev/null +++ b/fs/yaffs2/yaffs_vfs.c | |||
| @@ -0,0 +1,2790 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * Acknowledgements: | ||
| 9 | * Luc van OostenRyck for numerous patches. | ||
| 10 | * Nick Bane for numerous patches. | ||
| 11 | * Nick Bane for 2.5/2.6 integration. | ||
| 12 | * Andras Toth for mknod rdev issue. | ||
| 13 | * Michael Fischer for finding the problem with inode inconsistency. | ||
| 14 | * Some code bodily lifted from JFFS | ||
| 15 | * | ||
| 16 | * This program is free software; you can redistribute it and/or modify | ||
| 17 | * it under the terms of the GNU General Public License version 2 as | ||
| 18 | * published by the Free Software Foundation. | ||
| 19 | */ | ||
| 20 | |||
| 21 | /* | ||
| 22 | * | ||
| 23 | * This is the file system front-end to YAFFS that hooks it up to | ||
| 24 | * the VFS. | ||
| 25 | * | ||
| 26 | * Special notes: | ||
| 27 | * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with | ||
| 28 | * this superblock | ||
| 29 | * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this | ||
| 30 | * superblock | ||
| 31 | * >> inode->u.generic_ip points to the associated struct yaffs_obj. | ||
| 32 | */ | ||
| 33 | |||
| 34 | /* | ||
| 35 | * NB There are two variants of Linux VFS glue code. This variant supports | ||
| 36 | * a single version and should not include any multi-version code. | ||
| 37 | */ | ||
| 38 | #include <linux/version.h> | ||
| 39 | |||
| 40 | #include <linux/kernel.h> | ||
| 41 | #include <linux/module.h> | ||
| 42 | #include <linux/slab.h> | ||
| 43 | #include <linux/init.h> | ||
| 44 | #include <linux/fs.h> | ||
| 45 | #include <linux/proc_fs.h> | ||
| 46 | #include <linux/pagemap.h> | ||
| 47 | #include <linux/mtd/mtd.h> | ||
| 48 | #include <linux/interrupt.h> | ||
| 49 | #include <linux/string.h> | ||
| 50 | #include <linux/ctype.h> | ||
| 51 | #include <linux/namei.h> | ||
| 52 | #include <linux/exportfs.h> | ||
| 53 | #include <linux/kthread.h> | ||
| 54 | #include <linux/delay.h> | ||
| 55 | #include <linux/freezer.h> | ||
| 56 | |||
| 57 | #include <asm/div64.h> | ||
| 58 | |||
| 59 | #include <linux/statfs.h> | ||
| 60 | |||
| 61 | #define UnlockPage(p) unlock_page(p) | ||
| 62 | #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags) | ||
| 63 | |||
| 64 | #define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf) | ||
| 65 | |||
| 66 | #define YPROC_ROOT NULL | ||
| 67 | |||
| 68 | #define Y_INIT_TIMER(a) init_timer_on_stack(a) | ||
| 69 | |||
| 70 | #define WRITE_SIZE_STR "writesize" | ||
| 71 | #define WRITE_SIZE(mtd) ((mtd)->writesize) | ||
| 72 | |||
| 73 | static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size) | ||
| 74 | { | ||
| 75 | uint64_t result = partition_size; | ||
| 76 | do_div(result, block_size); | ||
| 77 | return (uint32_t) result; | ||
| 78 | } | ||
| 79 | |||
| 80 | #include <linux/uaccess.h> | ||
| 81 | #include <linux/mtd/mtd.h> | ||
| 82 | |||
| 83 | #include "yportenv.h" | ||
| 84 | #include "yaffs_trace.h" | ||
| 85 | #include "yaffs_guts.h" | ||
| 86 | #include "yaffs_attribs.h" | ||
| 87 | |||
| 88 | #include "yaffs_linux.h" | ||
| 89 | |||
| 90 | #include "yaffs_mtdif.h" | ||
| 91 | #include "yaffs_mtdif1.h" | ||
| 92 | #include "yaffs_mtdif2.h" | ||
| 93 | |||
| 94 | unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS; | ||
| 95 | unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS; | ||
| 96 | unsigned int yaffs_auto_checkpoint = 1; | ||
| 97 | unsigned int yaffs_gc_control = 1; | ||
| 98 | unsigned int yaffs_bg_enable = 1; | ||
| 99 | |||
| 100 | /* Module Parameters */ | ||
| 101 | module_param(yaffs_trace_mask, uint, 0644); | ||
| 102 | module_param(yaffs_wr_attempts, uint, 0644); | ||
| 103 | module_param(yaffs_auto_checkpoint, uint, 0644); | ||
| 104 | module_param(yaffs_gc_control, uint, 0644); | ||
| 105 | module_param(yaffs_bg_enable, uint, 0644); | ||
| 106 | |||
| 107 | |||
| 108 | #define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private) | ||
| 109 | #define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr))) | ||
| 110 | #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode) | ||
| 111 | #define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info) | ||
| 112 | |||
| 113 | #define update_dir_time(dir) do {\ | ||
| 114 | (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \ | ||
| 115 | } while(0) | ||
| 116 | |||
| 117 | |||
| 118 | static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev) | ||
| 119 | { | ||
| 120 | return yaffs_gc_control; | ||
| 121 | } | ||
| 122 | |||
| 123 | static void yaffs_gross_lock(struct yaffs_dev *dev) | ||
| 124 | { | ||
| 125 | yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current); | ||
| 126 | mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock)); | ||
| 127 | yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current); | ||
| 128 | } | ||
| 129 | |||
| 130 | static void yaffs_gross_unlock(struct yaffs_dev *dev) | ||
| 131 | { | ||
| 132 | yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current); | ||
| 133 | mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock)); | ||
| 134 | } | ||
| 135 | |||
| 136 | static void yaffs_fill_inode_from_obj(struct inode *inode, | ||
| 137 | struct yaffs_obj *obj); | ||
| 138 | |||
| 139 | static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino) | ||
| 140 | { | ||
| 141 | struct inode *inode; | ||
| 142 | struct yaffs_obj *obj; | ||
| 143 | struct yaffs_dev *dev = yaffs_super_to_dev(sb); | ||
| 144 | |||
| 145 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino); | ||
| 146 | |||
| 147 | inode = iget_locked(sb, ino); | ||
| 148 | if (!inode) | ||
| 149 | return ERR_PTR(-ENOMEM); | ||
| 150 | if (!(inode->i_state & I_NEW)) | ||
| 151 | return inode; | ||
| 152 | |||
| 153 | /* NB This is called as a side effect of other functions, but | ||
| 154 | * we had to release the lock to prevent deadlocks, so | ||
| 155 | * need to lock again. | ||
| 156 | */ | ||
| 157 | |||
| 158 | yaffs_gross_lock(dev); | ||
| 159 | |||
| 160 | obj = yaffs_find_by_number(dev, inode->i_ino); | ||
| 161 | |||
| 162 | yaffs_fill_inode_from_obj(inode, obj); | ||
| 163 | |||
| 164 | yaffs_gross_unlock(dev); | ||
| 165 | |||
| 166 | unlock_new_inode(inode); | ||
| 167 | return inode; | ||
| 168 | } | ||
| 169 | |||
| 170 | struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev, | ||
| 171 | struct yaffs_obj *obj) | ||
| 172 | { | ||
| 173 | struct inode *inode; | ||
| 174 | |||
| 175 | if (!sb) { | ||
| 176 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 177 | "yaffs_get_inode for NULL super_block!!"); | ||
| 178 | return NULL; | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | if (!obj) { | ||
| 183 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 184 | "yaffs_get_inode for NULL object!!"); | ||
| 185 | return NULL; | ||
| 186 | |||
| 187 | } | ||
| 188 | |||
| 189 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 190 | "yaffs_get_inode for object %d", | ||
| 191 | obj->obj_id); | ||
| 192 | |||
| 193 | inode = yaffs_iget(sb, obj->obj_id); | ||
| 194 | if (IS_ERR(inode)) | ||
| 195 | return NULL; | ||
| 196 | |||
| 197 | /* NB Side effect: iget calls back to yaffs_read_inode(). */ | ||
| 198 | /* iget also increments the inode's i_count */ | ||
| 199 | /* NB You can't be holding gross_lock or deadlock will happen! */ | ||
| 200 | |||
| 201 | return inode; | ||
| 202 | } | ||
| 203 | |||
| 204 | static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, | ||
| 205 | dev_t rdev) | ||
| 206 | { | ||
| 207 | struct inode *inode; | ||
| 208 | |||
| 209 | struct yaffs_obj *obj = NULL; | ||
| 210 | struct yaffs_dev *dev; | ||
| 211 | |||
| 212 | struct yaffs_obj *parent = yaffs_inode_to_obj(dir); | ||
| 213 | |||
| 214 | int error = -ENOSPC; | ||
| 215 | uid_t uid = current->cred->fsuid; | ||
| 216 | gid_t gid = | ||
| 217 | (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid; | ||
| 218 | |||
| 219 | if ((dir->i_mode & S_ISGID) && S_ISDIR(mode)) | ||
| 220 | mode |= S_ISGID; | ||
| 221 | |||
| 222 | if (parent) { | ||
| 223 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 224 | "yaffs_mknod: parent object %d type %d", | ||
| 225 | parent->obj_id, parent->variant_type); | ||
| 226 | } else { | ||
| 227 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 228 | "yaffs_mknod: could not get parent object"); | ||
| 229 | return -EPERM; | ||
| 230 | } | ||
| 231 | |||
| 232 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 233 | "yaffs_mknod: making oject for %s, mode %x dev %x", | ||
| 234 | dentry->d_name.name, mode, rdev); | ||
| 235 | |||
| 236 | dev = parent->my_dev; | ||
| 237 | |||
| 238 | yaffs_gross_lock(dev); | ||
| 239 | |||
| 240 | switch (mode & S_IFMT) { | ||
| 241 | default: | ||
| 242 | /* Special (socket, fifo, device...) */ | ||
| 243 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special"); | ||
| 244 | obj = | ||
| 245 | yaffs_create_special(parent, dentry->d_name.name, mode, uid, | ||
| 246 | gid, old_encode_dev(rdev)); | ||
| 247 | break; | ||
| 248 | case S_IFREG: /* file */ | ||
| 249 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file"); | ||
| 250 | obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid, | ||
| 251 | gid); | ||
| 252 | break; | ||
| 253 | case S_IFDIR: /* directory */ | ||
| 254 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory"); | ||
| 255 | obj = yaffs_create_dir(parent, dentry->d_name.name, mode, | ||
| 256 | uid, gid); | ||
| 257 | break; | ||
| 258 | case S_IFLNK: /* symlink */ | ||
| 259 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink"); | ||
| 260 | obj = NULL; /* Do we ever get here? */ | ||
| 261 | break; | ||
| 262 | } | ||
| 263 | |||
| 264 | /* Can not call yaffs_get_inode() with gross lock held */ | ||
| 265 | yaffs_gross_unlock(dev); | ||
| 266 | |||
| 267 | if (obj) { | ||
| 268 | inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj); | ||
| 269 | d_instantiate(dentry, inode); | ||
| 270 | update_dir_time(dir); | ||
| 271 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 272 | "yaffs_mknod created object %d count = %d", | ||
| 273 | obj->obj_id, atomic_read(&inode->i_count)); | ||
| 274 | error = 0; | ||
| 275 | yaffs_fill_inode_from_obj(dir, parent); | ||
| 276 | } else { | ||
| 277 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object"); | ||
| 278 | error = -ENOMEM; | ||
| 279 | } | ||
| 280 | |||
| 281 | return error; | ||
| 282 | } | ||
| 283 | |||
| 284 | static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | ||
| 285 | { | ||
| 286 | return yaffs_mknod(dir, dentry, mode | S_IFDIR, 0); | ||
| 287 | } | ||
| 288 | |||
| 289 | static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, | ||
| 290 | struct nameidata *n) | ||
| 291 | { | ||
| 292 | return yaffs_mknod(dir, dentry, mode | S_IFREG, 0); | ||
| 293 | } | ||
| 294 | |||
| 295 | static int yaffs_link(struct dentry *old_dentry, struct inode *dir, | ||
| 296 | struct dentry *dentry) | ||
| 297 | { | ||
| 298 | struct inode *inode = old_dentry->d_inode; | ||
| 299 | struct yaffs_obj *obj = NULL; | ||
| 300 | struct yaffs_obj *link = NULL; | ||
| 301 | struct yaffs_dev *dev; | ||
| 302 | |||
| 303 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_link"); | ||
| 304 | |||
| 305 | obj = yaffs_inode_to_obj(inode); | ||
| 306 | dev = obj->my_dev; | ||
| 307 | |||
| 308 | yaffs_gross_lock(dev); | ||
| 309 | |||
| 310 | if (!S_ISDIR(inode->i_mode)) /* Don't link directories */ | ||
| 311 | link = | ||
| 312 | yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name, | ||
| 313 | obj); | ||
| 314 | |||
| 315 | if (link) { | ||
| 316 | old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj); | ||
| 317 | d_instantiate(dentry, old_dentry->d_inode); | ||
| 318 | atomic_inc(&old_dentry->d_inode->i_count); | ||
| 319 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 320 | "yaffs_link link count %d i_count %d", | ||
| 321 | old_dentry->d_inode->i_nlink, | ||
| 322 | atomic_read(&old_dentry->d_inode->i_count)); | ||
| 323 | } | ||
| 324 | |||
| 325 | yaffs_gross_unlock(dev); | ||
| 326 | |||
| 327 | if (link) { | ||
| 328 | update_dir_time(dir); | ||
| 329 | return 0; | ||
| 330 | } | ||
| 331 | |||
| 332 | return -EPERM; | ||
| 333 | } | ||
| 334 | |||
| 335 | static int yaffs_symlink(struct inode *dir, struct dentry *dentry, | ||
| 336 | const char *symname) | ||
| 337 | { | ||
| 338 | struct yaffs_obj *obj; | ||
| 339 | struct yaffs_dev *dev; | ||
| 340 | uid_t uid = current->cred->fsuid; | ||
| 341 | gid_t gid = | ||
| 342 | (dir->i_mode & S_ISGID) ? dir->i_gid : current->cred->fsgid; | ||
| 343 | |||
| 344 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink"); | ||
| 345 | |||
| 346 | dev = yaffs_inode_to_obj(dir)->my_dev; | ||
| 347 | yaffs_gross_lock(dev); | ||
| 348 | obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name, | ||
| 349 | S_IFLNK | S_IRWXUGO, uid, gid, symname); | ||
| 350 | yaffs_gross_unlock(dev); | ||
| 351 | |||
| 352 | if (obj) { | ||
| 353 | struct inode *inode; | ||
| 354 | |||
| 355 | inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); | ||
| 356 | d_instantiate(dentry, inode); | ||
| 357 | update_dir_time(dir); | ||
| 358 | yaffs_trace(YAFFS_TRACE_OS, "symlink created OK"); | ||
| 359 | return 0; | ||
| 360 | } else { | ||
| 361 | yaffs_trace(YAFFS_TRACE_OS, "symlink not created"); | ||
| 362 | } | ||
| 363 | |||
| 364 | return -ENOMEM; | ||
| 365 | } | ||
| 366 | |||
| 367 | static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry, | ||
| 368 | struct nameidata *n) | ||
| 369 | { | ||
| 370 | struct yaffs_obj *obj; | ||
| 371 | struct inode *inode = NULL; | ||
| 372 | |||
| 373 | struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev; | ||
| 374 | |||
| 375 | if (current != yaffs_dev_to_lc(dev)->readdir_process) | ||
| 376 | yaffs_gross_lock(dev); | ||
| 377 | |||
| 378 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 379 | "yaffs_lookup for %d:%s", | ||
| 380 | yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name); | ||
| 381 | |||
| 382 | obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name); | ||
| 383 | |||
| 384 | obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */ | ||
| 385 | |||
| 386 | /* Can't hold gross lock when calling yaffs_get_inode() */ | ||
| 387 | if (current != yaffs_dev_to_lc(dev)->readdir_process) | ||
| 388 | yaffs_gross_unlock(dev); | ||
| 389 | |||
| 390 | if (obj) { | ||
| 391 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 392 | "yaffs_lookup found %d", obj->obj_id); | ||
| 393 | |||
| 394 | inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); | ||
| 395 | |||
| 396 | if (inode) { | ||
| 397 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_loookup dentry"); | ||
| 398 | d_add(dentry, inode); | ||
| 399 | /* return dentry; */ | ||
| 400 | return NULL; | ||
| 401 | } | ||
| 402 | |||
| 403 | } else { | ||
| 404 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found"); | ||
| 405 | |||
| 406 | } | ||
| 407 | |||
| 408 | d_add(dentry, inode); | ||
| 409 | |||
| 410 | return NULL; | ||
| 411 | } | ||
| 412 | |||
| 413 | static int yaffs_unlink(struct inode *dir, struct dentry *dentry) | ||
| 414 | { | ||
| 415 | int ret_val; | ||
| 416 | |||
| 417 | struct yaffs_dev *dev; | ||
| 418 | struct yaffs_obj *obj; | ||
| 419 | |||
| 420 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 421 | "yaffs_unlink %d:%s", | ||
| 422 | (int)(dir->i_ino), dentry->d_name.name); | ||
| 423 | obj = yaffs_inode_to_obj(dir); | ||
| 424 | dev = obj->my_dev; | ||
| 425 | |||
| 426 | yaffs_gross_lock(dev); | ||
| 427 | |||
| 428 | ret_val = yaffs_unlinker(obj, dentry->d_name.name); | ||
| 429 | |||
| 430 | if (ret_val == YAFFS_OK) { | ||
| 431 | dentry->d_inode->i_nlink--; | ||
| 432 | dir->i_version++; | ||
| 433 | yaffs_gross_unlock(dev); | ||
| 434 | mark_inode_dirty(dentry->d_inode); | ||
| 435 | update_dir_time(dir); | ||
| 436 | return 0; | ||
| 437 | } | ||
| 438 | yaffs_gross_unlock(dev); | ||
| 439 | return -ENOTEMPTY; | ||
| 440 | } | ||
| 441 | |||
| 442 | static int yaffs_sync_object(struct file *file, int datasync) | ||
| 443 | { | ||
| 444 | |||
| 445 | struct yaffs_obj *obj; | ||
| 446 | struct yaffs_dev *dev; | ||
| 447 | struct dentry *dentry = file->f_path.dentry; | ||
| 448 | |||
| 449 | obj = yaffs_dentry_to_obj(dentry); | ||
| 450 | |||
| 451 | dev = obj->my_dev; | ||
| 452 | |||
| 453 | yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, "yaffs_sync_object"); | ||
| 454 | yaffs_gross_lock(dev); | ||
| 455 | yaffs_flush_file(obj, 1, datasync); | ||
| 456 | yaffs_gross_unlock(dev); | ||
| 457 | return 0; | ||
| 458 | } | ||
| 459 | /* | ||
| 460 | * The VFS layer already does all the dentry stuff for rename. | ||
| 461 | * | ||
| 462 | * NB: POSIX says you can rename an object over an old object of the same name | ||
| 463 | */ | ||
| 464 | static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry, | ||
| 465 | struct inode *new_dir, struct dentry *new_dentry) | ||
| 466 | { | ||
| 467 | struct yaffs_dev *dev; | ||
| 468 | int ret_val = YAFFS_FAIL; | ||
| 469 | struct yaffs_obj *target; | ||
| 470 | |||
| 471 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename"); | ||
| 472 | dev = yaffs_inode_to_obj(old_dir)->my_dev; | ||
| 473 | |||
| 474 | yaffs_gross_lock(dev); | ||
| 475 | |||
| 476 | /* Check if the target is an existing directory that is not empty. */ | ||
| 477 | target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir), | ||
| 478 | new_dentry->d_name.name); | ||
| 479 | |||
| 480 | if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY && | ||
| 481 | !list_empty(&target->variant.dir_variant.children)) { | ||
| 482 | |||
| 483 | yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir"); | ||
| 484 | |||
| 485 | ret_val = YAFFS_FAIL; | ||
| 486 | } else { | ||
| 487 | /* Now does unlinking internally using shadowing mechanism */ | ||
| 488 | yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj"); | ||
| 489 | |||
| 490 | ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir), | ||
| 491 | old_dentry->d_name.name, | ||
| 492 | yaffs_inode_to_obj(new_dir), | ||
| 493 | new_dentry->d_name.name); | ||
| 494 | } | ||
| 495 | yaffs_gross_unlock(dev); | ||
| 496 | |||
| 497 | if (ret_val == YAFFS_OK) { | ||
| 498 | if (target) { | ||
| 499 | new_dentry->d_inode->i_nlink--; | ||
| 500 | mark_inode_dirty(new_dentry->d_inode); | ||
| 501 | } | ||
| 502 | |||
| 503 | update_dir_time(old_dir); | ||
| 504 | if (old_dir != new_dir) | ||
| 505 | update_dir_time(new_dir); | ||
| 506 | return 0; | ||
| 507 | } else { | ||
| 508 | return -ENOTEMPTY; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) | ||
| 513 | { | ||
| 514 | struct inode *inode = dentry->d_inode; | ||
| 515 | int error = 0; | ||
| 516 | struct yaffs_dev *dev; | ||
| 517 | |||
| 518 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 519 | "yaffs_setattr of object %d", | ||
| 520 | yaffs_inode_to_obj(inode)->obj_id); | ||
| 521 | |||
| 522 | /* Fail if a requested resize >= 2GB */ | ||
| 523 | if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31)) | ||
| 524 | error = -EINVAL; | ||
| 525 | |||
| 526 | if (error == 0) | ||
| 527 | error = inode_change_ok(inode, attr); | ||
| 528 | if (error == 0) { | ||
| 529 | int result; | ||
| 530 | if (!error) { | ||
| 531 | setattr_copy(inode, attr); | ||
| 532 | yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called"); | ||
| 533 | if (attr->ia_valid & ATTR_SIZE) { | ||
| 534 | truncate_setsize(inode, attr->ia_size); | ||
| 535 | inode->i_blocks = (inode->i_size + 511) >> 9; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | dev = yaffs_inode_to_obj(inode)->my_dev; | ||
| 539 | if (attr->ia_valid & ATTR_SIZE) { | ||
| 540 | yaffs_trace(YAFFS_TRACE_OS, "resize to %d(%x)", | ||
| 541 | (int)(attr->ia_size), | ||
| 542 | (int)(attr->ia_size)); | ||
| 543 | } | ||
| 544 | yaffs_gross_lock(dev); | ||
| 545 | result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr); | ||
| 546 | if (result == YAFFS_OK) { | ||
| 547 | error = 0; | ||
| 548 | } else { | ||
| 549 | error = -EPERM; | ||
| 550 | } | ||
| 551 | yaffs_gross_unlock(dev); | ||
| 552 | |||
| 553 | } | ||
| 554 | |||
| 555 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error); | ||
| 556 | |||
| 557 | return error; | ||
| 558 | } | ||
| 559 | |||
| 560 | #ifdef CONFIG_YAFFS_XATTR | ||
| 561 | static int yaffs_setxattr(struct dentry *dentry, const char *name, | ||
| 562 | const void *value, size_t size, int flags) | ||
| 563 | { | ||
| 564 | struct inode *inode = dentry->d_inode; | ||
| 565 | int error = 0; | ||
| 566 | struct yaffs_dev *dev; | ||
| 567 | struct yaffs_obj *obj = yaffs_inode_to_obj(inode); | ||
| 568 | |||
| 569 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id); | ||
| 570 | |||
| 571 | if (error == 0) { | ||
| 572 | int result; | ||
| 573 | dev = obj->my_dev; | ||
| 574 | yaffs_gross_lock(dev); | ||
| 575 | result = yaffs_set_xattrib(obj, name, value, size, flags); | ||
| 576 | if (result == YAFFS_OK) | ||
| 577 | error = 0; | ||
| 578 | else if (result < 0) | ||
| 579 | error = result; | ||
| 580 | yaffs_gross_unlock(dev); | ||
| 581 | |||
| 582 | } | ||
| 583 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error); | ||
| 584 | |||
| 585 | return error; | ||
| 586 | } | ||
| 587 | |||
| 588 | static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff, | ||
| 589 | size_t size) | ||
| 590 | { | ||
| 591 | struct inode *inode = dentry->d_inode; | ||
| 592 | int error = 0; | ||
| 593 | struct yaffs_dev *dev; | ||
| 594 | struct yaffs_obj *obj = yaffs_inode_to_obj(inode); | ||
| 595 | |||
| 596 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 597 | "yaffs_getxattr \"%s\" from object %d", | ||
| 598 | name, obj->obj_id); | ||
| 599 | |||
| 600 | if (error == 0) { | ||
| 601 | dev = obj->my_dev; | ||
| 602 | yaffs_gross_lock(dev); | ||
| 603 | error = yaffs_get_xattrib(obj, name, buff, size); | ||
| 604 | yaffs_gross_unlock(dev); | ||
| 605 | |||
| 606 | } | ||
| 607 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error); | ||
| 608 | |||
| 609 | return error; | ||
| 610 | } | ||
| 611 | |||
| 612 | static int yaffs_removexattr(struct dentry *dentry, const char *name) | ||
| 613 | { | ||
| 614 | struct inode *inode = dentry->d_inode; | ||
| 615 | int error = 0; | ||
| 616 | struct yaffs_dev *dev; | ||
| 617 | struct yaffs_obj *obj = yaffs_inode_to_obj(inode); | ||
| 618 | |||
| 619 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 620 | "yaffs_removexattr of object %d", obj->obj_id); | ||
| 621 | |||
| 622 | if (error == 0) { | ||
| 623 | int result; | ||
| 624 | dev = obj->my_dev; | ||
| 625 | yaffs_gross_lock(dev); | ||
| 626 | result = yaffs_remove_xattrib(obj, name); | ||
| 627 | if (result == YAFFS_OK) | ||
| 628 | error = 0; | ||
| 629 | else if (result < 0) | ||
| 630 | error = result; | ||
| 631 | yaffs_gross_unlock(dev); | ||
| 632 | |||
| 633 | } | ||
| 634 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 635 | "yaffs_removexattr done returning %d", error); | ||
| 636 | |||
| 637 | return error; | ||
| 638 | } | ||
| 639 | |||
| 640 | static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size) | ||
| 641 | { | ||
| 642 | struct inode *inode = dentry->d_inode; | ||
| 643 | int error = 0; | ||
| 644 | struct yaffs_dev *dev; | ||
| 645 | struct yaffs_obj *obj = yaffs_inode_to_obj(inode); | ||
| 646 | |||
| 647 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 648 | "yaffs_listxattr of object %d", obj->obj_id); | ||
| 649 | |||
| 650 | if (error == 0) { | ||
| 651 | dev = obj->my_dev; | ||
| 652 | yaffs_gross_lock(dev); | ||
| 653 | error = yaffs_list_xattrib(obj, buff, size); | ||
| 654 | yaffs_gross_unlock(dev); | ||
| 655 | |||
| 656 | } | ||
| 657 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 658 | "yaffs_listxattr done returning %d", error); | ||
| 659 | |||
| 660 | return error; | ||
| 661 | } | ||
| 662 | |||
| 663 | #endif | ||
| 664 | |||
| 665 | static const struct inode_operations yaffs_dir_inode_operations = { | ||
| 666 | .create = yaffs_create, | ||
| 667 | .lookup = yaffs_lookup, | ||
| 668 | .link = yaffs_link, | ||
| 669 | .unlink = yaffs_unlink, | ||
| 670 | .symlink = yaffs_symlink, | ||
| 671 | .mkdir = yaffs_mkdir, | ||
| 672 | .rmdir = yaffs_unlink, | ||
| 673 | .mknod = yaffs_mknod, | ||
| 674 | .rename = yaffs_rename, | ||
| 675 | .setattr = yaffs_setattr, | ||
| 676 | #ifdef CONFIG_YAFFS_XATTR | ||
| 677 | .setxattr = yaffs_setxattr, | ||
| 678 | .getxattr = yaffs_getxattr, | ||
| 679 | .listxattr = yaffs_listxattr, | ||
| 680 | .removexattr = yaffs_removexattr, | ||
| 681 | #endif | ||
| 682 | }; | ||
| 683 | /*-----------------------------------------------------------------*/ | ||
| 684 | /* Directory search context allows us to unlock access to yaffs during | ||
| 685 | * filldir without causing problems with the directory being modified. | ||
| 686 | * This is similar to the tried and tested mechanism used in yaffs direct. | ||
| 687 | * | ||
| 688 | * A search context iterates along a doubly linked list of siblings in the | ||
| 689 | * directory. If the iterating object is deleted then this would corrupt | ||
| 690 | * the list iteration, likely causing a crash. The search context avoids | ||
| 691 | * this by using the remove_obj_fn to move the search context to the | ||
| 692 | * next object before the object is deleted. | ||
| 693 | * | ||
| 694 | * Many readdirs (and thus seach conexts) may be alive simulateously so | ||
| 695 | * each struct yaffs_dev has a list of these. | ||
| 696 | * | ||
| 697 | * A seach context lives for the duration of a readdir. | ||
| 698 | * | ||
| 699 | * All these functions must be called while yaffs is locked. | ||
| 700 | */ | ||
| 701 | |||
| 702 | struct yaffs_search_context { | ||
| 703 | struct yaffs_dev *dev; | ||
| 704 | struct yaffs_obj *dir_obj; | ||
| 705 | struct yaffs_obj *next_return; | ||
| 706 | struct list_head others; | ||
| 707 | }; | ||
| 708 | |||
| 709 | /* | ||
| 710 | * yaffs_new_search() creates a new search context, initialises it and | ||
| 711 | * adds it to the device's search context list. | ||
| 712 | * | ||
| 713 | * Called at start of readdir. | ||
| 714 | */ | ||
| 715 | static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir) | ||
| 716 | { | ||
| 717 | struct yaffs_dev *dev = dir->my_dev; | ||
| 718 | struct yaffs_search_context *sc = | ||
| 719 | kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS); | ||
| 720 | if (sc) { | ||
| 721 | sc->dir_obj = dir; | ||
| 722 | sc->dev = dev; | ||
| 723 | if (list_empty(&sc->dir_obj->variant.dir_variant.children)) | ||
| 724 | sc->next_return = NULL; | ||
| 725 | else | ||
| 726 | sc->next_return = | ||
| 727 | list_entry(dir->variant.dir_variant.children.next, | ||
| 728 | struct yaffs_obj, siblings); | ||
| 729 | INIT_LIST_HEAD(&sc->others); | ||
| 730 | list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts)); | ||
| 731 | } | ||
| 732 | return sc; | ||
| 733 | } | ||
| 734 | |||
| 735 | /* | ||
| 736 | * yaffs_search_end() disposes of a search context and cleans up. | ||
| 737 | */ | ||
| 738 | static void yaffs_search_end(struct yaffs_search_context *sc) | ||
| 739 | { | ||
| 740 | if (sc) { | ||
| 741 | list_del(&sc->others); | ||
| 742 | kfree(sc); | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | /* | ||
| 747 | * yaffs_search_advance() moves a search context to the next object. | ||
| 748 | * Called when the search iterates or when an object removal causes | ||
| 749 | * the search context to be moved to the next object. | ||
| 750 | */ | ||
| 751 | static void yaffs_search_advance(struct yaffs_search_context *sc) | ||
| 752 | { | ||
| 753 | if (!sc) | ||
| 754 | return; | ||
| 755 | |||
| 756 | if (sc->next_return == NULL || | ||
| 757 | list_empty(&sc->dir_obj->variant.dir_variant.children)) | ||
| 758 | sc->next_return = NULL; | ||
| 759 | else { | ||
| 760 | struct list_head *next = sc->next_return->siblings.next; | ||
| 761 | |||
| 762 | if (next == &sc->dir_obj->variant.dir_variant.children) | ||
| 763 | sc->next_return = NULL; /* end of list */ | ||
| 764 | else | ||
| 765 | sc->next_return = | ||
| 766 | list_entry(next, struct yaffs_obj, siblings); | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | /* | ||
| 771 | * yaffs_remove_obj_callback() is called when an object is unlinked. | ||
| 772 | * We check open search contexts and advance any which are currently | ||
| 773 | * on the object being iterated. | ||
| 774 | */ | ||
| 775 | static void yaffs_remove_obj_callback(struct yaffs_obj *obj) | ||
| 776 | { | ||
| 777 | |||
| 778 | struct list_head *i; | ||
| 779 | struct yaffs_search_context *sc; | ||
| 780 | struct list_head *search_contexts = | ||
| 781 | &(yaffs_dev_to_lc(obj->my_dev)->search_contexts); | ||
| 782 | |||
| 783 | /* Iterate through the directory search contexts. | ||
| 784 | * If any are currently on the object being removed, then advance | ||
| 785 | * the search context to the next object to prevent a hanging pointer. | ||
| 786 | */ | ||
| 787 | list_for_each(i, search_contexts) { | ||
| 788 | if (i) { | ||
| 789 | sc = list_entry(i, struct yaffs_search_context, others); | ||
| 790 | if (sc->next_return == obj) | ||
| 791 | yaffs_search_advance(sc); | ||
| 792 | } | ||
| 793 | } | ||
| 794 | |||
| 795 | } | ||
| 796 | |||
| 797 | static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) | ||
| 798 | { | ||
| 799 | struct yaffs_obj *obj; | ||
| 800 | struct yaffs_dev *dev; | ||
| 801 | struct yaffs_search_context *sc; | ||
| 802 | struct inode *inode = f->f_dentry->d_inode; | ||
| 803 | unsigned long offset, curoffs; | ||
| 804 | struct yaffs_obj *l; | ||
| 805 | int ret_val = 0; | ||
| 806 | |||
| 807 | char name[YAFFS_MAX_NAME_LENGTH + 1]; | ||
| 808 | |||
| 809 | obj = yaffs_dentry_to_obj(f->f_dentry); | ||
| 810 | dev = obj->my_dev; | ||
| 811 | |||
| 812 | yaffs_gross_lock(dev); | ||
| 813 | |||
| 814 | yaffs_dev_to_lc(dev)->readdir_process = current; | ||
| 815 | |||
| 816 | offset = f->f_pos; | ||
| 817 | |||
| 818 | sc = yaffs_new_search(obj); | ||
| 819 | if (!sc) { | ||
| 820 | ret_val = -ENOMEM; | ||
| 821 | goto out; | ||
| 822 | } | ||
| 823 | |||
| 824 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 825 | "yaffs_readdir: starting at %d", (int)offset); | ||
| 826 | |||
| 827 | if (offset == 0) { | ||
| 828 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 829 | "yaffs_readdir: entry . ino %d", | ||
| 830 | (int)inode->i_ino); | ||
| 831 | yaffs_gross_unlock(dev); | ||
| 832 | if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) { | ||
| 833 | yaffs_gross_lock(dev); | ||
| 834 | goto out; | ||
| 835 | } | ||
| 836 | yaffs_gross_lock(dev); | ||
| 837 | offset++; | ||
| 838 | f->f_pos++; | ||
| 839 | } | ||
| 840 | if (offset == 1) { | ||
| 841 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 842 | "yaffs_readdir: entry .. ino %d", | ||
| 843 | (int)f->f_dentry->d_parent->d_inode->i_ino); | ||
| 844 | yaffs_gross_unlock(dev); | ||
| 845 | if (filldir(dirent, "..", 2, offset, | ||
| 846 | f->f_dentry->d_parent->d_inode->i_ino, | ||
| 847 | DT_DIR) < 0) { | ||
| 848 | yaffs_gross_lock(dev); | ||
| 849 | goto out; | ||
| 850 | } | ||
| 851 | yaffs_gross_lock(dev); | ||
| 852 | offset++; | ||
| 853 | f->f_pos++; | ||
| 854 | } | ||
| 855 | |||
| 856 | curoffs = 1; | ||
| 857 | |||
| 858 | /* If the directory has changed since the open or last call to | ||
| 859 | readdir, rewind to after the 2 canned entries. */ | ||
| 860 | if (f->f_version != inode->i_version) { | ||
| 861 | offset = 2; | ||
| 862 | f->f_pos = offset; | ||
| 863 | f->f_version = inode->i_version; | ||
| 864 | } | ||
| 865 | |||
| 866 | while (sc->next_return) { | ||
| 867 | curoffs++; | ||
| 868 | l = sc->next_return; | ||
| 869 | if (curoffs >= offset) { | ||
| 870 | int this_inode = yaffs_get_obj_inode(l); | ||
| 871 | int this_type = yaffs_get_obj_type(l); | ||
| 872 | |||
| 873 | yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1); | ||
| 874 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 875 | "yaffs_readdir: %s inode %d", | ||
| 876 | name, yaffs_get_obj_inode(l)); | ||
| 877 | |||
| 878 | yaffs_gross_unlock(dev); | ||
| 879 | |||
| 880 | if (filldir(dirent, | ||
| 881 | name, | ||
| 882 | strlen(name), | ||
| 883 | offset, this_inode, this_type) < 0) { | ||
| 884 | yaffs_gross_lock(dev); | ||
| 885 | goto out; | ||
| 886 | } | ||
| 887 | |||
| 888 | yaffs_gross_lock(dev); | ||
| 889 | |||
| 890 | offset++; | ||
| 891 | f->f_pos++; | ||
| 892 | } | ||
| 893 | yaffs_search_advance(sc); | ||
| 894 | } | ||
| 895 | |||
| 896 | out: | ||
| 897 | yaffs_search_end(sc); | ||
| 898 | yaffs_dev_to_lc(dev)->readdir_process = NULL; | ||
| 899 | yaffs_gross_unlock(dev); | ||
| 900 | |||
| 901 | return ret_val; | ||
| 902 | } | ||
| 903 | |||
| 904 | static const struct file_operations yaffs_dir_operations = { | ||
| 905 | .read = generic_read_dir, | ||
| 906 | .readdir = yaffs_readdir, | ||
| 907 | .fsync = yaffs_sync_object, | ||
| 908 | .llseek = generic_file_llseek, | ||
| 909 | }; | ||
| 910 | |||
| 911 | |||
| 912 | |||
| 913 | static int yaffs_file_flush(struct file *file, fl_owner_t id) | ||
| 914 | { | ||
| 915 | struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry); | ||
| 916 | |||
| 917 | struct yaffs_dev *dev = obj->my_dev; | ||
| 918 | |||
| 919 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 920 | "yaffs_file_flush object %d (%s)", | ||
| 921 | obj->obj_id, obj->dirty ? "dirty" : "clean"); | ||
| 922 | |||
| 923 | yaffs_gross_lock(dev); | ||
| 924 | |||
| 925 | yaffs_flush_file(obj, 1, 0); | ||
| 926 | |||
| 927 | yaffs_gross_unlock(dev); | ||
| 928 | |||
| 929 | return 0; | ||
| 930 | } | ||
| 931 | |||
| 932 | static const struct file_operations yaffs_file_operations = { | ||
| 933 | .read = do_sync_read, | ||
| 934 | .write = do_sync_write, | ||
| 935 | .aio_read = generic_file_aio_read, | ||
| 936 | .aio_write = generic_file_aio_write, | ||
| 937 | .mmap = generic_file_mmap, | ||
| 938 | .flush = yaffs_file_flush, | ||
| 939 | .fsync = yaffs_sync_object, | ||
| 940 | .splice_read = generic_file_splice_read, | ||
| 941 | .splice_write = generic_file_splice_write, | ||
| 942 | .llseek = generic_file_llseek, | ||
| 943 | }; | ||
| 944 | |||
| 945 | |||
| 946 | /* ExportFS support */ | ||
| 947 | static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, | ||
| 948 | uint32_t generation) | ||
| 949 | { | ||
| 950 | return yaffs_iget(sb, ino); | ||
| 951 | } | ||
| 952 | |||
| 953 | static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb, | ||
| 954 | struct fid *fid, int fh_len, | ||
| 955 | int fh_type) | ||
| 956 | { | ||
| 957 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, | ||
| 958 | yaffs2_nfs_get_inode); | ||
| 959 | } | ||
| 960 | |||
| 961 | static struct dentry *yaffs2_fh_to_parent(struct super_block *sb, | ||
| 962 | struct fid *fid, int fh_len, | ||
| 963 | int fh_type) | ||
| 964 | { | ||
| 965 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, | ||
| 966 | yaffs2_nfs_get_inode); | ||
| 967 | } | ||
| 968 | |||
| 969 | struct dentry *yaffs2_get_parent(struct dentry *dentry) | ||
| 970 | { | ||
| 971 | |||
| 972 | struct super_block *sb = dentry->d_inode->i_sb; | ||
| 973 | struct dentry *parent = ERR_PTR(-ENOENT); | ||
| 974 | struct inode *inode; | ||
| 975 | unsigned long parent_ino; | ||
| 976 | struct yaffs_obj *d_obj; | ||
| 977 | struct yaffs_obj *parent_obj; | ||
| 978 | |||
| 979 | d_obj = yaffs_inode_to_obj(dentry->d_inode); | ||
| 980 | |||
| 981 | if (d_obj) { | ||
| 982 | parent_obj = d_obj->parent; | ||
| 983 | if (parent_obj) { | ||
| 984 | parent_ino = yaffs_get_obj_inode(parent_obj); | ||
| 985 | inode = yaffs_iget(sb, parent_ino); | ||
| 986 | |||
| 987 | if (IS_ERR(inode)) { | ||
| 988 | parent = ERR_CAST(inode); | ||
| 989 | } else { | ||
| 990 | parent = d_obtain_alias(inode); | ||
| 991 | if (!IS_ERR(parent)) { | ||
| 992 | parent = ERR_PTR(-ENOMEM); | ||
| 993 | iput(inode); | ||
| 994 | } | ||
| 995 | } | ||
| 996 | } | ||
| 997 | } | ||
| 998 | |||
| 999 | return parent; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | /* Just declare a zero structure as a NULL value implies | ||
| 1003 | * using the default functions of exportfs. | ||
| 1004 | */ | ||
| 1005 | |||
| 1006 | static struct export_operations yaffs_export_ops = { | ||
| 1007 | .fh_to_dentry = yaffs2_fh_to_dentry, | ||
| 1008 | .fh_to_parent = yaffs2_fh_to_parent, | ||
| 1009 | .get_parent = yaffs2_get_parent, | ||
| 1010 | }; | ||
| 1011 | |||
| 1012 | |||
| 1013 | /*-----------------------------------------------------------------*/ | ||
| 1014 | |||
| 1015 | static int yaffs_readlink(struct dentry *dentry, char __user * buffer, | ||
| 1016 | int buflen) | ||
| 1017 | { | ||
| 1018 | unsigned char *alias; | ||
| 1019 | int ret; | ||
| 1020 | |||
| 1021 | struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev; | ||
| 1022 | |||
| 1023 | yaffs_gross_lock(dev); | ||
| 1024 | |||
| 1025 | alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry)); | ||
| 1026 | |||
| 1027 | yaffs_gross_unlock(dev); | ||
| 1028 | |||
| 1029 | if (!alias) | ||
| 1030 | return -ENOMEM; | ||
| 1031 | |||
| 1032 | ret = vfs_readlink(dentry, buffer, buflen, alias); | ||
| 1033 | kfree(alias); | ||
| 1034 | return ret; | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd) | ||
| 1038 | { | ||
| 1039 | unsigned char *alias; | ||
| 1040 | void *ret; | ||
| 1041 | struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev; | ||
| 1042 | |||
| 1043 | yaffs_gross_lock(dev); | ||
| 1044 | |||
| 1045 | alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry)); | ||
| 1046 | yaffs_gross_unlock(dev); | ||
| 1047 | |||
| 1048 | if (!alias) { | ||
| 1049 | ret = ERR_PTR(-ENOMEM); | ||
| 1050 | goto out; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | nd_set_link(nd, alias); | ||
| 1054 | ret = (void *)alias; | ||
| 1055 | out: | ||
| 1056 | return ret; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) | ||
| 1060 | { | ||
| 1061 | kfree(alias); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | |||
| 1065 | static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj) | ||
| 1066 | { | ||
| 1067 | /* Clear the association between the inode and | ||
| 1068 | * the struct yaffs_obj. | ||
| 1069 | */ | ||
| 1070 | obj->my_inode = NULL; | ||
| 1071 | yaffs_inode_to_obj_lv(inode) = NULL; | ||
| 1072 | |||
| 1073 | /* If the object freeing was deferred, then the real | ||
| 1074 | * free happens now. | ||
| 1075 | * This should fix the inode inconsistency problem. | ||
| 1076 | */ | ||
| 1077 | yaffs_handle_defered_free(obj); | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | /* yaffs_evict_inode combines into one operation what was previously done in | ||
| 1081 | * yaffs_clear_inode() and yaffs_delete_inode() | ||
| 1082 | * | ||
| 1083 | */ | ||
| 1084 | static void yaffs_evict_inode(struct inode *inode) | ||
| 1085 | { | ||
| 1086 | struct yaffs_obj *obj; | ||
| 1087 | struct yaffs_dev *dev; | ||
| 1088 | int deleteme = 0; | ||
| 1089 | |||
| 1090 | obj = yaffs_inode_to_obj(inode); | ||
| 1091 | |||
| 1092 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1093 | "yaffs_evict_inode: ino %d, count %d %s", | ||
| 1094 | (int)inode->i_ino, | ||
| 1095 | atomic_read(&inode->i_count), | ||
| 1096 | obj ? "object exists" : "null object"); | ||
| 1097 | |||
| 1098 | if (!inode->i_nlink && !is_bad_inode(inode)) | ||
| 1099 | deleteme = 1; | ||
| 1100 | truncate_inode_pages(&inode->i_data, 0); | ||
| 1101 | end_writeback(inode); | ||
| 1102 | |||
| 1103 | if (deleteme && obj) { | ||
| 1104 | dev = obj->my_dev; | ||
| 1105 | yaffs_gross_lock(dev); | ||
| 1106 | yaffs_del_obj(obj); | ||
| 1107 | yaffs_gross_unlock(dev); | ||
| 1108 | } | ||
| 1109 | if (obj) { | ||
| 1110 | dev = obj->my_dev; | ||
| 1111 | yaffs_gross_lock(dev); | ||
| 1112 | yaffs_unstitch_obj(inode, obj); | ||
| 1113 | yaffs_gross_unlock(dev); | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | } | ||
| 1117 | |||
| 1118 | static void yaffs_touch_super(struct yaffs_dev *dev) | ||
| 1119 | { | ||
| 1120 | struct super_block *sb = yaffs_dev_to_lc(dev)->super; | ||
| 1121 | |||
| 1122 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb); | ||
| 1123 | if (sb) | ||
| 1124 | sb->s_dirt = 1; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | static int yaffs_readpage_nolock(struct file *f, struct page *pg) | ||
| 1128 | { | ||
| 1129 | /* Lifted from jffs2 */ | ||
| 1130 | |||
| 1131 | struct yaffs_obj *obj; | ||
| 1132 | unsigned char *pg_buf; | ||
| 1133 | int ret; | ||
| 1134 | |||
| 1135 | struct yaffs_dev *dev; | ||
| 1136 | |||
| 1137 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1138 | "yaffs_readpage_nolock at %08x, size %08x", | ||
| 1139 | (unsigned)(pg->index << PAGE_CACHE_SHIFT), | ||
| 1140 | (unsigned)PAGE_CACHE_SIZE); | ||
| 1141 | |||
| 1142 | obj = yaffs_dentry_to_obj(f->f_dentry); | ||
| 1143 | |||
| 1144 | dev = obj->my_dev; | ||
| 1145 | |||
| 1146 | BUG_ON(!PageLocked(pg)); | ||
| 1147 | |||
| 1148 | pg_buf = kmap(pg); | ||
| 1149 | /* FIXME: Can kmap fail? */ | ||
| 1150 | |||
| 1151 | yaffs_gross_lock(dev); | ||
| 1152 | |||
| 1153 | ret = yaffs_file_rd(obj, pg_buf, | ||
| 1154 | pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE); | ||
| 1155 | |||
| 1156 | yaffs_gross_unlock(dev); | ||
| 1157 | |||
| 1158 | if (ret >= 0) | ||
| 1159 | ret = 0; | ||
| 1160 | |||
| 1161 | if (ret) { | ||
| 1162 | ClearPageUptodate(pg); | ||
| 1163 | SetPageError(pg); | ||
| 1164 | } else { | ||
| 1165 | SetPageUptodate(pg); | ||
| 1166 | ClearPageError(pg); | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | flush_dcache_page(pg); | ||
| 1170 | kunmap(pg); | ||
| 1171 | |||
| 1172 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done"); | ||
| 1173 | return ret; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | static int yaffs_readpage_unlock(struct file *f, struct page *pg) | ||
| 1177 | { | ||
| 1178 | int ret = yaffs_readpage_nolock(f, pg); | ||
| 1179 | UnlockPage(pg); | ||
| 1180 | return ret; | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | static int yaffs_readpage(struct file *f, struct page *pg) | ||
| 1184 | { | ||
| 1185 | int ret; | ||
| 1186 | |||
| 1187 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage"); | ||
| 1188 | ret = yaffs_readpage_unlock(f, pg); | ||
| 1189 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done"); | ||
| 1190 | return ret; | ||
| 1191 | } | ||
| 1192 | |||
| 1193 | /* writepage inspired by/stolen from smbfs */ | ||
| 1194 | |||
| 1195 | static int yaffs_writepage(struct page *page, struct writeback_control *wbc) | ||
| 1196 | { | ||
| 1197 | struct yaffs_dev *dev; | ||
| 1198 | struct address_space *mapping = page->mapping; | ||
| 1199 | struct inode *inode; | ||
| 1200 | unsigned long end_index; | ||
| 1201 | char *buffer; | ||
| 1202 | struct yaffs_obj *obj; | ||
| 1203 | int n_written = 0; | ||
| 1204 | unsigned n_bytes; | ||
| 1205 | loff_t i_size; | ||
| 1206 | |||
| 1207 | if (!mapping) | ||
| 1208 | BUG(); | ||
| 1209 | inode = mapping->host; | ||
| 1210 | if (!inode) | ||
| 1211 | BUG(); | ||
| 1212 | i_size = i_size_read(inode); | ||
| 1213 | |||
| 1214 | end_index = i_size >> PAGE_CACHE_SHIFT; | ||
| 1215 | |||
| 1216 | if (page->index < end_index) | ||
| 1217 | n_bytes = PAGE_CACHE_SIZE; | ||
| 1218 | else { | ||
| 1219 | n_bytes = i_size & (PAGE_CACHE_SIZE - 1); | ||
| 1220 | |||
| 1221 | if (page->index > end_index || !n_bytes) { | ||
| 1222 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1223 | "yaffs_writepage at %08x, inode size = %08x!!!", | ||
| 1224 | (unsigned)(page->index << PAGE_CACHE_SHIFT), | ||
| 1225 | (unsigned)inode->i_size); | ||
| 1226 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1227 | " -> don't care!!"); | ||
| 1228 | |||
| 1229 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); | ||
| 1230 | set_page_writeback(page); | ||
| 1231 | unlock_page(page); | ||
| 1232 | end_page_writeback(page); | ||
| 1233 | return 0; | ||
| 1234 | } | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | if (n_bytes != PAGE_CACHE_SIZE) | ||
| 1238 | zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE); | ||
| 1239 | |||
| 1240 | get_page(page); | ||
| 1241 | |||
| 1242 | buffer = kmap(page); | ||
| 1243 | |||
| 1244 | obj = yaffs_inode_to_obj(inode); | ||
| 1245 | dev = obj->my_dev; | ||
| 1246 | yaffs_gross_lock(dev); | ||
| 1247 | |||
| 1248 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1249 | "yaffs_writepage at %08x, size %08x", | ||
| 1250 | (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes); | ||
| 1251 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1252 | "writepag0: obj = %05x, ino = %05x", | ||
| 1253 | (int)obj->variant.file_variant.file_size, (int)inode->i_size); | ||
| 1254 | |||
| 1255 | n_written = yaffs_wr_file(obj, buffer, | ||
| 1256 | page->index << PAGE_CACHE_SHIFT, n_bytes, 0); | ||
| 1257 | |||
| 1258 | yaffs_touch_super(dev); | ||
| 1259 | |||
| 1260 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1261 | "writepag1: obj = %05x, ino = %05x", | ||
| 1262 | (int)obj->variant.file_variant.file_size, (int)inode->i_size); | ||
| 1263 | |||
| 1264 | yaffs_gross_unlock(dev); | ||
| 1265 | |||
| 1266 | kunmap(page); | ||
| 1267 | set_page_writeback(page); | ||
| 1268 | unlock_page(page); | ||
| 1269 | end_page_writeback(page); | ||
| 1270 | put_page(page); | ||
| 1271 | |||
| 1272 | return (n_written == n_bytes) ? 0 : -ENOSPC; | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | /* Space holding and freeing is done to ensure we have space available for | ||
| 1276 | * write_begin/end. | ||
| 1277 | * For now we just assume few parallel writes and check against a small | ||
| 1278 | * number. | ||
| 1279 | * Todo: need to do this with a counter to handle parallel reads better. | ||
| 1280 | */ | ||
| 1281 | |||
| 1282 | static ssize_t yaffs_hold_space(struct file *f) | ||
| 1283 | { | ||
| 1284 | struct yaffs_obj *obj; | ||
| 1285 | struct yaffs_dev *dev; | ||
| 1286 | |||
| 1287 | int n_free_chunks; | ||
| 1288 | |||
| 1289 | obj = yaffs_dentry_to_obj(f->f_dentry); | ||
| 1290 | |||
| 1291 | dev = obj->my_dev; | ||
| 1292 | |||
| 1293 | yaffs_gross_lock(dev); | ||
| 1294 | |||
| 1295 | n_free_chunks = yaffs_get_n_free_chunks(dev); | ||
| 1296 | |||
| 1297 | yaffs_gross_unlock(dev); | ||
| 1298 | |||
| 1299 | return (n_free_chunks > 20) ? 1 : 0; | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | static void yaffs_release_space(struct file *f) | ||
| 1303 | { | ||
| 1304 | struct yaffs_obj *obj; | ||
| 1305 | struct yaffs_dev *dev; | ||
| 1306 | |||
| 1307 | obj = yaffs_dentry_to_obj(f->f_dentry); | ||
| 1308 | |||
| 1309 | dev = obj->my_dev; | ||
| 1310 | |||
| 1311 | yaffs_gross_lock(dev); | ||
| 1312 | |||
| 1313 | yaffs_gross_unlock(dev); | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | static int yaffs_write_begin(struct file *filp, struct address_space *mapping, | ||
| 1317 | loff_t pos, unsigned len, unsigned flags, | ||
| 1318 | struct page **pagep, void **fsdata) | ||
| 1319 | { | ||
| 1320 | struct page *pg = NULL; | ||
| 1321 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | ||
| 1322 | |||
| 1323 | int ret = 0; | ||
| 1324 | int space_held = 0; | ||
| 1325 | |||
| 1326 | /* Get a page */ | ||
| 1327 | pg = grab_cache_page_write_begin(mapping, index, flags); | ||
| 1328 | |||
| 1329 | *pagep = pg; | ||
| 1330 | if (!pg) { | ||
| 1331 | ret = -ENOMEM; | ||
| 1332 | goto out; | ||
| 1333 | } | ||
| 1334 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1335 | "start yaffs_write_begin index %d(%x) uptodate %d", | ||
| 1336 | (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0); | ||
| 1337 | |||
| 1338 | /* Get fs space */ | ||
| 1339 | space_held = yaffs_hold_space(filp); | ||
| 1340 | |||
| 1341 | if (!space_held) { | ||
| 1342 | ret = -ENOSPC; | ||
| 1343 | goto out; | ||
| 1344 | } | ||
| 1345 | |||
| 1346 | /* Update page if required */ | ||
| 1347 | |||
| 1348 | if (!Page_Uptodate(pg)) | ||
| 1349 | ret = yaffs_readpage_nolock(filp, pg); | ||
| 1350 | |||
| 1351 | if (ret) | ||
| 1352 | goto out; | ||
| 1353 | |||
| 1354 | /* Happy path return */ | ||
| 1355 | yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok"); | ||
| 1356 | |||
| 1357 | return 0; | ||
| 1358 | |||
| 1359 | out: | ||
| 1360 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1361 | "end yaffs_write_begin fail returning %d", ret); | ||
| 1362 | if (space_held) | ||
| 1363 | yaffs_release_space(filp); | ||
| 1364 | if (pg) { | ||
| 1365 | unlock_page(pg); | ||
| 1366 | page_cache_release(pg); | ||
| 1367 | } | ||
| 1368 | return ret; | ||
| 1369 | } | ||
| 1370 | |||
| 1371 | static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, | ||
| 1372 | loff_t * pos) | ||
| 1373 | { | ||
| 1374 | struct yaffs_obj *obj; | ||
| 1375 | int n_written, ipos; | ||
| 1376 | struct inode *inode; | ||
| 1377 | struct yaffs_dev *dev; | ||
| 1378 | |||
| 1379 | obj = yaffs_dentry_to_obj(f->f_dentry); | ||
| 1380 | |||
| 1381 | dev = obj->my_dev; | ||
| 1382 | |||
| 1383 | yaffs_gross_lock(dev); | ||
| 1384 | |||
| 1385 | inode = f->f_dentry->d_inode; | ||
| 1386 | |||
| 1387 | if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) | ||
| 1388 | ipos = inode->i_size; | ||
| 1389 | else | ||
| 1390 | ipos = *pos; | ||
| 1391 | |||
| 1392 | if (!obj) | ||
| 1393 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1394 | "yaffs_file_write: hey obj is null!"); | ||
| 1395 | else | ||
| 1396 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1397 | "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)", | ||
| 1398 | (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos); | ||
| 1399 | |||
| 1400 | n_written = yaffs_wr_file(obj, buf, ipos, n, 0); | ||
| 1401 | |||
| 1402 | yaffs_touch_super(dev); | ||
| 1403 | |||
| 1404 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1405 | "yaffs_file_write: %d(%x) bytes written", | ||
| 1406 | (unsigned)n, (unsigned)n); | ||
| 1407 | |||
| 1408 | if (n_written > 0) { | ||
| 1409 | ipos += n_written; | ||
| 1410 | *pos = ipos; | ||
| 1411 | if (ipos > inode->i_size) { | ||
| 1412 | inode->i_size = ipos; | ||
| 1413 | inode->i_blocks = (ipos + 511) >> 9; | ||
| 1414 | |||
| 1415 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1416 | "yaffs_file_write size updated to %d bytes, %d blocks", | ||
| 1417 | ipos, (int)(inode->i_blocks)); | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | } | ||
| 1421 | yaffs_gross_unlock(dev); | ||
| 1422 | return (n_written == 0) && (n > 0) ? -ENOSPC : n_written; | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | static int yaffs_write_end(struct file *filp, struct address_space *mapping, | ||
| 1426 | loff_t pos, unsigned len, unsigned copied, | ||
| 1427 | struct page *pg, void *fsdadata) | ||
| 1428 | { | ||
| 1429 | int ret = 0; | ||
| 1430 | void *addr, *kva; | ||
| 1431 | uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1); | ||
| 1432 | |||
| 1433 | kva = kmap(pg); | ||
| 1434 | addr = kva + offset_into_page; | ||
| 1435 | |||
| 1436 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1437 | "yaffs_write_end addr %p pos %x n_bytes %d", | ||
| 1438 | addr, (unsigned)pos, copied); | ||
| 1439 | |||
| 1440 | ret = yaffs_file_write(filp, addr, copied, &pos); | ||
| 1441 | |||
| 1442 | if (ret != copied) { | ||
| 1443 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1444 | "yaffs_write_end not same size ret %d copied %d", | ||
| 1445 | ret, copied); | ||
| 1446 | SetPageError(pg); | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | kunmap(pg); | ||
| 1450 | |||
| 1451 | yaffs_release_space(filp); | ||
| 1452 | unlock_page(pg); | ||
| 1453 | page_cache_release(pg); | ||
| 1454 | return ret; | ||
| 1455 | } | ||
| 1456 | |||
| 1457 | static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf) | ||
| 1458 | { | ||
| 1459 | struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev; | ||
| 1460 | struct super_block *sb = dentry->d_sb; | ||
| 1461 | |||
| 1462 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs"); | ||
| 1463 | |||
| 1464 | yaffs_gross_lock(dev); | ||
| 1465 | |||
| 1466 | buf->f_type = YAFFS_MAGIC; | ||
| 1467 | buf->f_bsize = sb->s_blocksize; | ||
| 1468 | buf->f_namelen = 255; | ||
| 1469 | |||
| 1470 | if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) { | ||
| 1471 | /* Do this if chunk size is not a power of 2 */ | ||
| 1472 | |||
| 1473 | uint64_t bytes_in_dev; | ||
| 1474 | uint64_t bytes_free; | ||
| 1475 | |||
| 1476 | bytes_in_dev = | ||
| 1477 | ((uint64_t) | ||
| 1478 | ((dev->param.end_block - dev->param.start_block + | ||
| 1479 | 1))) * ((uint64_t) (dev->param.chunks_per_block * | ||
| 1480 | dev->data_bytes_per_chunk)); | ||
| 1481 | |||
| 1482 | do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */ | ||
| 1483 | buf->f_blocks = bytes_in_dev; | ||
| 1484 | |||
| 1485 | bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) * | ||
| 1486 | ((uint64_t) (dev->data_bytes_per_chunk)); | ||
| 1487 | |||
| 1488 | do_div(bytes_free, sb->s_blocksize); | ||
| 1489 | |||
| 1490 | buf->f_bfree = bytes_free; | ||
| 1491 | |||
| 1492 | } else if (sb->s_blocksize > dev->data_bytes_per_chunk) { | ||
| 1493 | |||
| 1494 | buf->f_blocks = | ||
| 1495 | (dev->param.end_block - dev->param.start_block + 1) * | ||
| 1496 | dev->param.chunks_per_block / | ||
| 1497 | (sb->s_blocksize / dev->data_bytes_per_chunk); | ||
| 1498 | buf->f_bfree = | ||
| 1499 | yaffs_get_n_free_chunks(dev) / | ||
| 1500 | (sb->s_blocksize / dev->data_bytes_per_chunk); | ||
| 1501 | } else { | ||
| 1502 | buf->f_blocks = | ||
| 1503 | (dev->param.end_block - dev->param.start_block + 1) * | ||
| 1504 | dev->param.chunks_per_block * | ||
| 1505 | (dev->data_bytes_per_chunk / sb->s_blocksize); | ||
| 1506 | |||
| 1507 | buf->f_bfree = | ||
| 1508 | yaffs_get_n_free_chunks(dev) * | ||
| 1509 | (dev->data_bytes_per_chunk / sb->s_blocksize); | ||
| 1510 | } | ||
| 1511 | |||
| 1512 | buf->f_files = 0; | ||
| 1513 | buf->f_ffree = 0; | ||
| 1514 | buf->f_bavail = buf->f_bfree; | ||
| 1515 | |||
| 1516 | yaffs_gross_unlock(dev); | ||
| 1517 | return 0; | ||
| 1518 | } | ||
| 1519 | |||
| 1520 | static void yaffs_flush_inodes(struct super_block *sb) | ||
| 1521 | { | ||
| 1522 | struct inode *iptr; | ||
| 1523 | struct yaffs_obj *obj; | ||
| 1524 | |||
| 1525 | list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) { | ||
| 1526 | obj = yaffs_inode_to_obj(iptr); | ||
| 1527 | if (obj) { | ||
| 1528 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1529 | "flushing obj %d", obj->obj_id); | ||
| 1530 | yaffs_flush_file(obj, 1, 0); | ||
| 1531 | } | ||
| 1532 | } | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | static void yaffs_flush_super(struct super_block *sb, int do_checkpoint) | ||
| 1536 | { | ||
| 1537 | struct yaffs_dev *dev = yaffs_super_to_dev(sb); | ||
| 1538 | if (!dev) | ||
| 1539 | return; | ||
| 1540 | |||
| 1541 | yaffs_flush_inodes(sb); | ||
| 1542 | yaffs_update_dirty_dirs(dev); | ||
| 1543 | yaffs_flush_whole_cache(dev); | ||
| 1544 | if (do_checkpoint) | ||
| 1545 | yaffs_checkpoint_save(dev); | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev) | ||
| 1549 | { | ||
| 1550 | unsigned erased_chunks = | ||
| 1551 | dev->n_erased_blocks * dev->param.chunks_per_block; | ||
| 1552 | struct yaffs_linux_context *context = yaffs_dev_to_lc(dev); | ||
| 1553 | unsigned scattered = 0; /* Free chunks not in an erased block */ | ||
| 1554 | |||
| 1555 | if (erased_chunks < dev->n_free_chunks) | ||
| 1556 | scattered = (dev->n_free_chunks - erased_chunks); | ||
| 1557 | |||
| 1558 | if (!context->bg_running) | ||
| 1559 | return 0; | ||
| 1560 | else if (scattered < (dev->param.chunks_per_block * 2)) | ||
| 1561 | return 0; | ||
| 1562 | else if (erased_chunks > dev->n_free_chunks / 2) | ||
| 1563 | return 0; | ||
| 1564 | else if (erased_chunks > dev->n_free_chunks / 4) | ||
| 1565 | return 1; | ||
| 1566 | else | ||
| 1567 | return 2; | ||
| 1568 | } | ||
| 1569 | |||
| 1570 | static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint) | ||
| 1571 | { | ||
| 1572 | |||
| 1573 | struct yaffs_dev *dev = yaffs_super_to_dev(sb); | ||
| 1574 | unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4); | ||
| 1575 | unsigned gc_urgent = yaffs_bg_gc_urgency(dev); | ||
| 1576 | int do_checkpoint; | ||
| 1577 | |||
| 1578 | yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND, | ||
| 1579 | "yaffs_do_sync_fs: gc-urgency %d %s %s%s", | ||
| 1580 | gc_urgent, | ||
| 1581 | sb->s_dirt ? "dirty" : "clean", | ||
| 1582 | request_checkpoint ? "checkpoint requested" : "no checkpoint", | ||
| 1583 | oneshot_checkpoint ? " one-shot" : ""); | ||
| 1584 | |||
| 1585 | yaffs_gross_lock(dev); | ||
| 1586 | do_checkpoint = ((request_checkpoint && !gc_urgent) || | ||
| 1587 | oneshot_checkpoint) && !dev->is_checkpointed; | ||
| 1588 | |||
| 1589 | if (sb->s_dirt || do_checkpoint) { | ||
| 1590 | yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint); | ||
| 1591 | sb->s_dirt = 0; | ||
| 1592 | if (oneshot_checkpoint) | ||
| 1593 | yaffs_auto_checkpoint &= ~4; | ||
| 1594 | } | ||
| 1595 | yaffs_gross_unlock(dev); | ||
| 1596 | |||
| 1597 | return 0; | ||
| 1598 | } | ||
| 1599 | |||
| 1600 | /* | ||
| 1601 | * yaffs background thread functions . | ||
| 1602 | * yaffs_bg_thread_fn() the thread function | ||
| 1603 | * yaffs_bg_start() launches the background thread. | ||
| 1604 | * yaffs_bg_stop() cleans up the background thread. | ||
| 1605 | * | ||
| 1606 | * NB: | ||
| 1607 | * The thread should only run after the yaffs is initialised | ||
| 1608 | * The thread should be stopped before yaffs is unmounted. | ||
| 1609 | * The thread should not do any writing while the fs is in read only. | ||
| 1610 | */ | ||
| 1611 | |||
| 1612 | void yaffs_background_waker(unsigned long data) | ||
| 1613 | { | ||
| 1614 | wake_up_process((struct task_struct *)data); | ||
| 1615 | } | ||
| 1616 | |||
| 1617 | static int yaffs_bg_thread_fn(void *data) | ||
| 1618 | { | ||
| 1619 | struct yaffs_dev *dev = (struct yaffs_dev *)data; | ||
| 1620 | struct yaffs_linux_context *context = yaffs_dev_to_lc(dev); | ||
| 1621 | unsigned long now = jiffies; | ||
| 1622 | unsigned long next_dir_update = now; | ||
| 1623 | unsigned long next_gc = now; | ||
| 1624 | unsigned long expires; | ||
| 1625 | unsigned int urgency; | ||
| 1626 | |||
| 1627 | int gc_result; | ||
| 1628 | struct timer_list timer; | ||
| 1629 | |||
| 1630 | yaffs_trace(YAFFS_TRACE_BACKGROUND, | ||
| 1631 | "yaffs_background starting for dev %p", (void *)dev); | ||
| 1632 | |||
| 1633 | set_freezable(); | ||
| 1634 | while (context->bg_running) { | ||
| 1635 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background"); | ||
| 1636 | |||
| 1637 | if (kthread_should_stop()) | ||
| 1638 | break; | ||
| 1639 | |||
| 1640 | if (try_to_freeze()) | ||
| 1641 | continue; | ||
| 1642 | |||
| 1643 | yaffs_gross_lock(dev); | ||
| 1644 | |||
| 1645 | now = jiffies; | ||
| 1646 | |||
| 1647 | if (time_after(now, next_dir_update) && yaffs_bg_enable) { | ||
| 1648 | yaffs_update_dirty_dirs(dev); | ||
| 1649 | next_dir_update = now + HZ; | ||
| 1650 | } | ||
| 1651 | |||
| 1652 | if (time_after(now, next_gc) && yaffs_bg_enable) { | ||
| 1653 | if (!dev->is_checkpointed) { | ||
| 1654 | urgency = yaffs_bg_gc_urgency(dev); | ||
| 1655 | gc_result = yaffs_bg_gc(dev, urgency); | ||
| 1656 | if (urgency > 1) | ||
| 1657 | next_gc = now + HZ / 20 + 1; | ||
| 1658 | else if (urgency > 0) | ||
| 1659 | next_gc = now + HZ / 10 + 1; | ||
| 1660 | else | ||
| 1661 | next_gc = now + HZ * 2; | ||
| 1662 | } else { | ||
| 1663 | /* | ||
| 1664 | * gc not running so set to next_dir_update | ||
| 1665 | * to cut down on wake ups | ||
| 1666 | */ | ||
| 1667 | next_gc = next_dir_update; | ||
| 1668 | } | ||
| 1669 | } | ||
| 1670 | yaffs_gross_unlock(dev); | ||
| 1671 | expires = next_dir_update; | ||
| 1672 | if (time_before(next_gc, expires)) | ||
| 1673 | expires = next_gc; | ||
| 1674 | if (time_before(expires, now)) | ||
| 1675 | expires = now + HZ; | ||
| 1676 | |||
| 1677 | Y_INIT_TIMER(&timer); | ||
| 1678 | timer.expires = expires + 1; | ||
| 1679 | timer.data = (unsigned long)current; | ||
| 1680 | timer.function = yaffs_background_waker; | ||
| 1681 | |||
| 1682 | set_current_state(TASK_INTERRUPTIBLE); | ||
| 1683 | add_timer(&timer); | ||
| 1684 | schedule(); | ||
| 1685 | del_timer_sync(&timer); | ||
| 1686 | } | ||
| 1687 | |||
| 1688 | return 0; | ||
| 1689 | } | ||
| 1690 | |||
| 1691 | static int yaffs_bg_start(struct yaffs_dev *dev) | ||
| 1692 | { | ||
| 1693 | int retval = 0; | ||
| 1694 | struct yaffs_linux_context *context = yaffs_dev_to_lc(dev); | ||
| 1695 | |||
| 1696 | if (dev->read_only) | ||
| 1697 | return -1; | ||
| 1698 | |||
| 1699 | context->bg_running = 1; | ||
| 1700 | |||
| 1701 | context->bg_thread = kthread_run(yaffs_bg_thread_fn, | ||
| 1702 | (void *)dev, "yaffs-bg-%d", | ||
| 1703 | context->mount_id); | ||
| 1704 | |||
| 1705 | if (IS_ERR(context->bg_thread)) { | ||
| 1706 | retval = PTR_ERR(context->bg_thread); | ||
| 1707 | context->bg_thread = NULL; | ||
| 1708 | context->bg_running = 0; | ||
| 1709 | } | ||
| 1710 | return retval; | ||
| 1711 | } | ||
| 1712 | |||
| 1713 | static void yaffs_bg_stop(struct yaffs_dev *dev) | ||
| 1714 | { | ||
| 1715 | struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev); | ||
| 1716 | |||
| 1717 | ctxt->bg_running = 0; | ||
| 1718 | |||
| 1719 | if (ctxt->bg_thread) { | ||
| 1720 | kthread_stop(ctxt->bg_thread); | ||
| 1721 | ctxt->bg_thread = NULL; | ||
| 1722 | } | ||
| 1723 | } | ||
| 1724 | |||
| 1725 | static void yaffs_write_super(struct super_block *sb) | ||
| 1726 | { | ||
| 1727 | unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2); | ||
| 1728 | |||
| 1729 | yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND, | ||
| 1730 | "yaffs_write_super%s", | ||
| 1731 | request_checkpoint ? " checkpt" : ""); | ||
| 1732 | |||
| 1733 | yaffs_do_sync_fs(sb, request_checkpoint); | ||
| 1734 | |||
| 1735 | } | ||
| 1736 | |||
| 1737 | static int yaffs_sync_fs(struct super_block *sb, int wait) | ||
| 1738 | { | ||
| 1739 | unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1); | ||
| 1740 | |||
| 1741 | yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, | ||
| 1742 | "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : ""); | ||
| 1743 | |||
| 1744 | yaffs_do_sync_fs(sb, request_checkpoint); | ||
| 1745 | |||
| 1746 | return 0; | ||
| 1747 | } | ||
| 1748 | |||
| 1749 | |||
| 1750 | static LIST_HEAD(yaffs_context_list); | ||
| 1751 | struct mutex yaffs_context_lock; | ||
| 1752 | |||
| 1753 | |||
| 1754 | |||
| 1755 | struct yaffs_options { | ||
| 1756 | int inband_tags; | ||
| 1757 | int skip_checkpoint_read; | ||
| 1758 | int skip_checkpoint_write; | ||
| 1759 | int no_cache; | ||
| 1760 | int tags_ecc_on; | ||
| 1761 | int tags_ecc_overridden; | ||
| 1762 | int lazy_loading_enabled; | ||
| 1763 | int lazy_loading_overridden; | ||
| 1764 | int empty_lost_and_found; | ||
| 1765 | int empty_lost_and_found_overridden; | ||
| 1766 | }; | ||
| 1767 | |||
| 1768 | #define MAX_OPT_LEN 30 | ||
| 1769 | static int yaffs_parse_options(struct yaffs_options *options, | ||
| 1770 | const char *options_str) | ||
| 1771 | { | ||
| 1772 | char cur_opt[MAX_OPT_LEN + 1]; | ||
| 1773 | int p; | ||
| 1774 | int error = 0; | ||
| 1775 | |||
| 1776 | /* Parse through the options which is a comma seperated list */ | ||
| 1777 | |||
| 1778 | while (options_str && *options_str && !error) { | ||
| 1779 | memset(cur_opt, 0, MAX_OPT_LEN + 1); | ||
| 1780 | p = 0; | ||
| 1781 | |||
| 1782 | while (*options_str == ',') | ||
| 1783 | options_str++; | ||
| 1784 | |||
| 1785 | while (*options_str && *options_str != ',') { | ||
| 1786 | if (p < MAX_OPT_LEN) { | ||
| 1787 | cur_opt[p] = *options_str; | ||
| 1788 | p++; | ||
| 1789 | } | ||
| 1790 | options_str++; | ||
| 1791 | } | ||
| 1792 | |||
| 1793 | if (!strcmp(cur_opt, "inband-tags")) { | ||
| 1794 | options->inband_tags = 1; | ||
| 1795 | } else if (!strcmp(cur_opt, "tags-ecc-off")) { | ||
| 1796 | options->tags_ecc_on = 0; | ||
| 1797 | options->tags_ecc_overridden = 1; | ||
| 1798 | } else if (!strcmp(cur_opt, "tags-ecc-on")) { | ||
| 1799 | options->tags_ecc_on = 1; | ||
| 1800 | options->tags_ecc_overridden = 1; | ||
| 1801 | } else if (!strcmp(cur_opt, "lazy-loading-off")) { | ||
| 1802 | options->lazy_loading_enabled = 0; | ||
| 1803 | options->lazy_loading_overridden = 1; | ||
| 1804 | } else if (!strcmp(cur_opt, "lazy-loading-on")) { | ||
| 1805 | options->lazy_loading_enabled = 1; | ||
| 1806 | options->lazy_loading_overridden = 1; | ||
| 1807 | } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) { | ||
| 1808 | options->empty_lost_and_found = 0; | ||
| 1809 | options->empty_lost_and_found_overridden = 1; | ||
| 1810 | } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) { | ||
| 1811 | options->empty_lost_and_found = 1; | ||
| 1812 | options->empty_lost_and_found_overridden = 1; | ||
| 1813 | } else if (!strcmp(cur_opt, "no-cache")) { | ||
| 1814 | options->no_cache = 1; | ||
| 1815 | } else if (!strcmp(cur_opt, "no-checkpoint-read")) { | ||
| 1816 | options->skip_checkpoint_read = 1; | ||
| 1817 | } else if (!strcmp(cur_opt, "no-checkpoint-write")) { | ||
| 1818 | options->skip_checkpoint_write = 1; | ||
| 1819 | } else if (!strcmp(cur_opt, "no-checkpoint")) { | ||
| 1820 | options->skip_checkpoint_read = 1; | ||
| 1821 | options->skip_checkpoint_write = 1; | ||
| 1822 | } else { | ||
| 1823 | printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n", | ||
| 1824 | cur_opt); | ||
| 1825 | error = 1; | ||
| 1826 | } | ||
| 1827 | } | ||
| 1828 | |||
| 1829 | return error; | ||
| 1830 | } | ||
| 1831 | |||
| 1832 | static struct address_space_operations yaffs_file_address_operations = { | ||
| 1833 | .readpage = yaffs_readpage, | ||
| 1834 | .writepage = yaffs_writepage, | ||
| 1835 | .write_begin = yaffs_write_begin, | ||
| 1836 | .write_end = yaffs_write_end, | ||
| 1837 | }; | ||
| 1838 | |||
| 1839 | |||
| 1840 | |||
| 1841 | static const struct inode_operations yaffs_file_inode_operations = { | ||
| 1842 | .setattr = yaffs_setattr, | ||
| 1843 | #ifdef CONFIG_YAFFS_XATTR | ||
| 1844 | .setxattr = yaffs_setxattr, | ||
| 1845 | .getxattr = yaffs_getxattr, | ||
| 1846 | .listxattr = yaffs_listxattr, | ||
| 1847 | .removexattr = yaffs_removexattr, | ||
| 1848 | #endif | ||
| 1849 | }; | ||
| 1850 | |||
| 1851 | static const struct inode_operations yaffs_symlink_inode_operations = { | ||
| 1852 | .readlink = yaffs_readlink, | ||
| 1853 | .follow_link = yaffs_follow_link, | ||
| 1854 | .put_link = yaffs_put_link, | ||
| 1855 | .setattr = yaffs_setattr, | ||
| 1856 | #ifdef CONFIG_YAFFS_XATTR | ||
| 1857 | .setxattr = yaffs_setxattr, | ||
| 1858 | .getxattr = yaffs_getxattr, | ||
| 1859 | .listxattr = yaffs_listxattr, | ||
| 1860 | .removexattr = yaffs_removexattr, | ||
| 1861 | #endif | ||
| 1862 | }; | ||
| 1863 | |||
| 1864 | static void yaffs_fill_inode_from_obj(struct inode *inode, | ||
| 1865 | struct yaffs_obj *obj) | ||
| 1866 | { | ||
| 1867 | if (inode && obj) { | ||
| 1868 | |||
| 1869 | /* Check mode against the variant type and attempt to repair if broken. */ | ||
| 1870 | u32 mode = obj->yst_mode; | ||
| 1871 | switch (obj->variant_type) { | ||
| 1872 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 1873 | if (!S_ISREG(mode)) { | ||
| 1874 | obj->yst_mode &= ~S_IFMT; | ||
| 1875 | obj->yst_mode |= S_IFREG; | ||
| 1876 | } | ||
| 1877 | |||
| 1878 | break; | ||
| 1879 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 1880 | if (!S_ISLNK(mode)) { | ||
| 1881 | obj->yst_mode &= ~S_IFMT; | ||
| 1882 | obj->yst_mode |= S_IFLNK; | ||
| 1883 | } | ||
| 1884 | |||
| 1885 | break; | ||
| 1886 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 1887 | if (!S_ISDIR(mode)) { | ||
| 1888 | obj->yst_mode &= ~S_IFMT; | ||
| 1889 | obj->yst_mode |= S_IFDIR; | ||
| 1890 | } | ||
| 1891 | |||
| 1892 | break; | ||
| 1893 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 1894 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 1895 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 1896 | default: | ||
| 1897 | /* TODO? */ | ||
| 1898 | break; | ||
| 1899 | } | ||
| 1900 | |||
| 1901 | inode->i_flags |= S_NOATIME; | ||
| 1902 | |||
| 1903 | inode->i_ino = obj->obj_id; | ||
| 1904 | inode->i_mode = obj->yst_mode; | ||
| 1905 | inode->i_uid = obj->yst_uid; | ||
| 1906 | inode->i_gid = obj->yst_gid; | ||
| 1907 | |||
| 1908 | inode->i_rdev = old_decode_dev(obj->yst_rdev); | ||
| 1909 | |||
| 1910 | inode->i_atime.tv_sec = (time_t) (obj->yst_atime); | ||
| 1911 | inode->i_atime.tv_nsec = 0; | ||
| 1912 | inode->i_mtime.tv_sec = (time_t) obj->yst_mtime; | ||
| 1913 | inode->i_mtime.tv_nsec = 0; | ||
| 1914 | inode->i_ctime.tv_sec = (time_t) obj->yst_ctime; | ||
| 1915 | inode->i_ctime.tv_nsec = 0; | ||
| 1916 | inode->i_size = yaffs_get_obj_length(obj); | ||
| 1917 | inode->i_blocks = (inode->i_size + 511) >> 9; | ||
| 1918 | |||
| 1919 | inode->i_nlink = yaffs_get_obj_link_count(obj); | ||
| 1920 | |||
| 1921 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1922 | "yaffs_fill_inode mode %x uid %d gid %d size %d count %d", | ||
| 1923 | inode->i_mode, inode->i_uid, inode->i_gid, | ||
| 1924 | (int)inode->i_size, atomic_read(&inode->i_count)); | ||
| 1925 | |||
| 1926 | switch (obj->yst_mode & S_IFMT) { | ||
| 1927 | default: /* fifo, device or socket */ | ||
| 1928 | init_special_inode(inode, obj->yst_mode, | ||
| 1929 | old_decode_dev(obj->yst_rdev)); | ||
| 1930 | break; | ||
| 1931 | case S_IFREG: /* file */ | ||
| 1932 | inode->i_op = &yaffs_file_inode_operations; | ||
| 1933 | inode->i_fop = &yaffs_file_operations; | ||
| 1934 | inode->i_mapping->a_ops = | ||
| 1935 | &yaffs_file_address_operations; | ||
| 1936 | break; | ||
| 1937 | case S_IFDIR: /* directory */ | ||
| 1938 | inode->i_op = &yaffs_dir_inode_operations; | ||
| 1939 | inode->i_fop = &yaffs_dir_operations; | ||
| 1940 | break; | ||
| 1941 | case S_IFLNK: /* symlink */ | ||
| 1942 | inode->i_op = &yaffs_symlink_inode_operations; | ||
| 1943 | break; | ||
| 1944 | } | ||
| 1945 | |||
| 1946 | yaffs_inode_to_obj_lv(inode) = obj; | ||
| 1947 | |||
| 1948 | obj->my_inode = inode; | ||
| 1949 | |||
| 1950 | } else { | ||
| 1951 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 1952 | "yaffs_fill_inode invalid parameters"); | ||
| 1953 | } | ||
| 1954 | } | ||
| 1955 | |||
| 1956 | static void yaffs_put_super(struct super_block *sb) | ||
| 1957 | { | ||
| 1958 | struct yaffs_dev *dev = yaffs_super_to_dev(sb); | ||
| 1959 | |||
| 1960 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super"); | ||
| 1961 | |||
| 1962 | yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, | ||
| 1963 | "Shutting down yaffs background thread"); | ||
| 1964 | yaffs_bg_stop(dev); | ||
| 1965 | yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, | ||
| 1966 | "yaffs background thread shut down"); | ||
| 1967 | |||
| 1968 | yaffs_gross_lock(dev); | ||
| 1969 | |||
| 1970 | yaffs_flush_super(sb, 1); | ||
| 1971 | |||
| 1972 | if (yaffs_dev_to_lc(dev)->put_super_fn) | ||
| 1973 | yaffs_dev_to_lc(dev)->put_super_fn(sb); | ||
| 1974 | |||
| 1975 | yaffs_deinitialise(dev); | ||
| 1976 | |||
| 1977 | yaffs_gross_unlock(dev); | ||
| 1978 | mutex_lock(&yaffs_context_lock); | ||
| 1979 | list_del_init(&(yaffs_dev_to_lc(dev)->context_list)); | ||
| 1980 | mutex_unlock(&yaffs_context_lock); | ||
| 1981 | |||
| 1982 | if (yaffs_dev_to_lc(dev)->spare_buffer) { | ||
| 1983 | kfree(yaffs_dev_to_lc(dev)->spare_buffer); | ||
| 1984 | yaffs_dev_to_lc(dev)->spare_buffer = NULL; | ||
| 1985 | } | ||
| 1986 | |||
| 1987 | kfree(dev); | ||
| 1988 | } | ||
| 1989 | |||
| 1990 | static void yaffs_mtd_put_super(struct super_block *sb) | ||
| 1991 | { | ||
| 1992 | struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_super_to_dev(sb)); | ||
| 1993 | |||
| 1994 | if (mtd->sync) | ||
| 1995 | mtd->sync(mtd); | ||
| 1996 | |||
| 1997 | put_mtd_device(mtd); | ||
| 1998 | } | ||
| 1999 | |||
| 2000 | static const struct super_operations yaffs_super_ops = { | ||
| 2001 | .statfs = yaffs_statfs, | ||
| 2002 | .put_super = yaffs_put_super, | ||
| 2003 | .evict_inode = yaffs_evict_inode, | ||
| 2004 | .sync_fs = yaffs_sync_fs, | ||
| 2005 | .write_super = yaffs_write_super, | ||
| 2006 | }; | ||
| 2007 | |||
| 2008 | static struct super_block *yaffs_internal_read_super(int yaffs_version, | ||
| 2009 | struct super_block *sb, | ||
| 2010 | void *data, int silent) | ||
| 2011 | { | ||
| 2012 | int n_blocks; | ||
| 2013 | struct inode *inode = NULL; | ||
| 2014 | struct dentry *root; | ||
| 2015 | struct yaffs_dev *dev = 0; | ||
| 2016 | char devname_buf[BDEVNAME_SIZE + 1]; | ||
| 2017 | struct mtd_info *mtd; | ||
| 2018 | int err; | ||
| 2019 | char *data_str = (char *)data; | ||
| 2020 | struct yaffs_linux_context *context = NULL; | ||
| 2021 | struct yaffs_param *param; | ||
| 2022 | |||
| 2023 | int read_only = 0; | ||
| 2024 | |||
| 2025 | struct yaffs_options options; | ||
| 2026 | |||
| 2027 | unsigned mount_id; | ||
| 2028 | int found; | ||
| 2029 | struct yaffs_linux_context *context_iterator; | ||
| 2030 | struct list_head *l; | ||
| 2031 | |||
| 2032 | sb->s_magic = YAFFS_MAGIC; | ||
| 2033 | sb->s_op = &yaffs_super_ops; | ||
| 2034 | sb->s_flags |= MS_NOATIME; | ||
| 2035 | |||
| 2036 | read_only = ((sb->s_flags & MS_RDONLY) != 0); | ||
| 2037 | |||
| 2038 | sb->s_export_op = &yaffs_export_ops; | ||
| 2039 | |||
| 2040 | if (!sb) | ||
| 2041 | printk(KERN_INFO "yaffs: sb is NULL\n"); | ||
| 2042 | else if (!sb->s_dev) | ||
| 2043 | printk(KERN_INFO "yaffs: sb->s_dev is NULL\n"); | ||
| 2044 | else if (!yaffs_devname(sb, devname_buf)) | ||
| 2045 | printk(KERN_INFO "yaffs: devname is NULL\n"); | ||
| 2046 | else | ||
| 2047 | printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n", | ||
| 2048 | sb->s_dev, | ||
| 2049 | yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw"); | ||
| 2050 | |||
| 2051 | if (!data_str) | ||
| 2052 | data_str = ""; | ||
| 2053 | |||
| 2054 | printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str); | ||
| 2055 | |||
| 2056 | memset(&options, 0, sizeof(options)); | ||
| 2057 | |||
| 2058 | if (yaffs_parse_options(&options, data_str)) { | ||
| 2059 | /* Option parsing failed */ | ||
| 2060 | return NULL; | ||
| 2061 | } | ||
| 2062 | |||
| 2063 | sb->s_blocksize = PAGE_CACHE_SIZE; | ||
| 2064 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | ||
| 2065 | |||
| 2066 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 2067 | "yaffs_read_super: Using yaffs%d", yaffs_version); | ||
| 2068 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 2069 | "yaffs_read_super: block size %d", (int)(sb->s_blocksize)); | ||
| 2070 | |||
| 2071 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2072 | "Attempting MTD mount of %u.%u,\"%s\"", | ||
| 2073 | MAJOR(sb->s_dev), MINOR(sb->s_dev), | ||
| 2074 | yaffs_devname(sb, devname_buf)); | ||
| 2075 | |||
| 2076 | /* Check it's an mtd device..... */ | ||
| 2077 | if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) | ||
| 2078 | return NULL; /* This isn't an mtd device */ | ||
| 2079 | |||
| 2080 | /* Get the device */ | ||
| 2081 | mtd = get_mtd_device(NULL, MINOR(sb->s_dev)); | ||
| 2082 | if (!mtd) { | ||
| 2083 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2084 | "MTD device #%u doesn't appear to exist", | ||
| 2085 | MINOR(sb->s_dev)); | ||
| 2086 | return NULL; | ||
| 2087 | } | ||
| 2088 | /* Check it's NAND */ | ||
| 2089 | if (mtd->type != MTD_NANDFLASH) { | ||
| 2090 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2091 | "MTD device is not NAND it's type %d", | ||
| 2092 | mtd->type); | ||
| 2093 | return NULL; | ||
| 2094 | } | ||
| 2095 | |||
| 2096 | yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase); | ||
| 2097 | yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read); | ||
| 2098 | yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write); | ||
| 2099 | yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob); | ||
| 2100 | yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob); | ||
| 2101 | yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad); | ||
| 2102 | yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad); | ||
| 2103 | yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd)); | ||
| 2104 | yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize); | ||
| 2105 | yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize); | ||
| 2106 | yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size); | ||
| 2107 | |||
| 2108 | #ifdef CONFIG_YAFFS_AUTO_YAFFS2 | ||
| 2109 | |||
| 2110 | if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) { | ||
| 2111 | yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2"); | ||
| 2112 | yaffs_version = 2; | ||
| 2113 | } | ||
| 2114 | |||
| 2115 | /* Added NCB 26/5/2006 for completeness */ | ||
| 2116 | if (yaffs_version == 2 && !options.inband_tags | ||
| 2117 | && WRITE_SIZE(mtd) == 512) { | ||
| 2118 | yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1"); | ||
| 2119 | yaffs_version = 1; | ||
| 2120 | } | ||
| 2121 | #endif | ||
| 2122 | |||
| 2123 | if (yaffs_version == 2) { | ||
| 2124 | /* Check for version 2 style functions */ | ||
| 2125 | if (!mtd->erase || | ||
| 2126 | !mtd->block_isbad || | ||
| 2127 | !mtd->block_markbad || | ||
| 2128 | !mtd->read || | ||
| 2129 | !mtd->write || !mtd->read_oob || !mtd->write_oob) { | ||
| 2130 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2131 | "MTD device does not support required functions"); | ||
| 2132 | return NULL; | ||
| 2133 | } | ||
| 2134 | |||
| 2135 | if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE || | ||
| 2136 | mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) && | ||
| 2137 | !options.inband_tags) { | ||
| 2138 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2139 | "MTD device does not have the right page sizes"); | ||
| 2140 | return NULL; | ||
| 2141 | } | ||
| 2142 | } else { | ||
| 2143 | /* Check for V1 style functions */ | ||
| 2144 | if (!mtd->erase || | ||
| 2145 | !mtd->read || | ||
| 2146 | !mtd->write || !mtd->read_oob || !mtd->write_oob) { | ||
| 2147 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2148 | "MTD device does not support required functions"); | ||
| 2149 | return NULL; | ||
| 2150 | } | ||
| 2151 | |||
| 2152 | if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK || | ||
| 2153 | mtd->oobsize != YAFFS_BYTES_PER_SPARE) { | ||
| 2154 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2155 | "MTD device does not support have the right page sizes"); | ||
| 2156 | return NULL; | ||
| 2157 | } | ||
| 2158 | } | ||
| 2159 | |||
| 2160 | /* OK, so if we got here, we have an MTD that's NAND and looks | ||
| 2161 | * like it has the right capabilities | ||
| 2162 | * Set the struct yaffs_dev up for mtd | ||
| 2163 | */ | ||
| 2164 | |||
| 2165 | if (!read_only && !(mtd->flags & MTD_WRITEABLE)) { | ||
| 2166 | read_only = 1; | ||
| 2167 | printk(KERN_INFO | ||
| 2168 | "yaffs: mtd is read only, setting superblock read only"); | ||
| 2169 | sb->s_flags |= MS_RDONLY; | ||
| 2170 | } | ||
| 2171 | |||
| 2172 | dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL); | ||
| 2173 | context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL); | ||
| 2174 | |||
| 2175 | if (!dev || !context) { | ||
| 2176 | if (dev) | ||
| 2177 | kfree(dev); | ||
| 2178 | if (context) | ||
| 2179 | kfree(context); | ||
| 2180 | dev = NULL; | ||
| 2181 | context = NULL; | ||
| 2182 | } | ||
| 2183 | |||
| 2184 | if (!dev) { | ||
| 2185 | /* Deep shit could not allocate device structure */ | ||
| 2186 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2187 | "yaffs_read_super failed trying to allocate yaffs_dev"); | ||
| 2188 | return NULL; | ||
| 2189 | } | ||
| 2190 | memset(dev, 0, sizeof(struct yaffs_dev)); | ||
| 2191 | param = &(dev->param); | ||
| 2192 | |||
| 2193 | memset(context, 0, sizeof(struct yaffs_linux_context)); | ||
| 2194 | dev->os_context = context; | ||
| 2195 | INIT_LIST_HEAD(&(context->context_list)); | ||
| 2196 | context->dev = dev; | ||
| 2197 | context->super = sb; | ||
| 2198 | |||
| 2199 | dev->read_only = read_only; | ||
| 2200 | |||
| 2201 | sb->s_fs_info = dev; | ||
| 2202 | |||
| 2203 | dev->driver_context = mtd; | ||
| 2204 | param->name = mtd->name; | ||
| 2205 | |||
| 2206 | /* Set up the memory size parameters.... */ | ||
| 2207 | |||
| 2208 | n_blocks = | ||
| 2209 | YCALCBLOCKS(mtd->size, | ||
| 2210 | (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK)); | ||
| 2211 | |||
| 2212 | param->start_block = 0; | ||
| 2213 | param->end_block = n_blocks - 1; | ||
| 2214 | param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK; | ||
| 2215 | param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK; | ||
| 2216 | param->n_reserved_blocks = 5; | ||
| 2217 | param->n_caches = (options.no_cache) ? 0 : 10; | ||
| 2218 | param->inband_tags = options.inband_tags; | ||
| 2219 | |||
| 2220 | #ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD | ||
| 2221 | param->disable_lazy_load = 1; | ||
| 2222 | #endif | ||
| 2223 | #ifdef CONFIG_YAFFS_XATTR | ||
| 2224 | param->enable_xattr = 1; | ||
| 2225 | #endif | ||
| 2226 | if (options.lazy_loading_overridden) | ||
| 2227 | param->disable_lazy_load = !options.lazy_loading_enabled; | ||
| 2228 | |||
| 2229 | #ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC | ||
| 2230 | param->no_tags_ecc = 1; | ||
| 2231 | #endif | ||
| 2232 | |||
| 2233 | #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND | ||
| 2234 | #else | ||
| 2235 | param->defered_dir_update = 1; | ||
| 2236 | #endif | ||
| 2237 | |||
| 2238 | if (options.tags_ecc_overridden) | ||
| 2239 | param->no_tags_ecc = !options.tags_ecc_on; | ||
| 2240 | |||
| 2241 | #ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND | ||
| 2242 | param->empty_lost_n_found = 1; | ||
| 2243 | #endif | ||
| 2244 | |||
| 2245 | #ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING | ||
| 2246 | param->refresh_period = 0; | ||
| 2247 | #else | ||
| 2248 | param->refresh_period = 500; | ||
| 2249 | #endif | ||
| 2250 | |||
| 2251 | #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED | ||
| 2252 | param->always_check_erased = 1; | ||
| 2253 | #endif | ||
| 2254 | |||
| 2255 | if (options.empty_lost_and_found_overridden) | ||
| 2256 | param->empty_lost_n_found = options.empty_lost_and_found; | ||
| 2257 | |||
| 2258 | /* ... and the functions. */ | ||
| 2259 | if (yaffs_version == 2) { | ||
| 2260 | param->write_chunk_tags_fn = nandmtd2_write_chunk_tags; | ||
| 2261 | param->read_chunk_tags_fn = nandmtd2_read_chunk_tags; | ||
| 2262 | param->bad_block_fn = nandmtd2_mark_block_bad; | ||
| 2263 | param->query_block_fn = nandmtd2_query_block; | ||
| 2264 | yaffs_dev_to_lc(dev)->spare_buffer = | ||
| 2265 | kmalloc(mtd->oobsize, GFP_NOFS); | ||
| 2266 | param->is_yaffs2 = 1; | ||
| 2267 | param->total_bytes_per_chunk = mtd->writesize; | ||
| 2268 | param->chunks_per_block = mtd->erasesize / mtd->writesize; | ||
| 2269 | n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize); | ||
| 2270 | |||
| 2271 | param->start_block = 0; | ||
| 2272 | param->end_block = n_blocks - 1; | ||
| 2273 | } else { | ||
| 2274 | /* use the MTD interface in yaffs_mtdif1.c */ | ||
| 2275 | param->write_chunk_tags_fn = nandmtd1_write_chunk_tags; | ||
| 2276 | param->read_chunk_tags_fn = nandmtd1_read_chunk_tags; | ||
| 2277 | param->bad_block_fn = nandmtd1_mark_block_bad; | ||
| 2278 | param->query_block_fn = nandmtd1_query_block; | ||
| 2279 | param->is_yaffs2 = 0; | ||
| 2280 | } | ||
| 2281 | /* ... and common functions */ | ||
| 2282 | param->erase_fn = nandmtd_erase_block; | ||
| 2283 | param->initialise_flash_fn = nandmtd_initialise; | ||
| 2284 | |||
| 2285 | yaffs_dev_to_lc(dev)->put_super_fn = yaffs_mtd_put_super; | ||
| 2286 | |||
| 2287 | param->sb_dirty_fn = yaffs_touch_super; | ||
| 2288 | param->gc_control = yaffs_gc_control_callback; | ||
| 2289 | |||
| 2290 | yaffs_dev_to_lc(dev)->super = sb; | ||
| 2291 | |||
| 2292 | #ifndef CONFIG_YAFFS_DOES_ECC | ||
| 2293 | param->use_nand_ecc = 1; | ||
| 2294 | #endif | ||
| 2295 | |||
| 2296 | param->skip_checkpt_rd = options.skip_checkpoint_read; | ||
| 2297 | param->skip_checkpt_wr = options.skip_checkpoint_write; | ||
| 2298 | |||
| 2299 | mutex_lock(&yaffs_context_lock); | ||
| 2300 | /* Get a mount id */ | ||
| 2301 | found = 0; | ||
| 2302 | for (mount_id = 0; !found; mount_id++) { | ||
| 2303 | found = 1; | ||
| 2304 | list_for_each(l, &yaffs_context_list) { | ||
| 2305 | context_iterator = | ||
| 2306 | list_entry(l, struct yaffs_linux_context, | ||
| 2307 | context_list); | ||
| 2308 | if (context_iterator->mount_id == mount_id) | ||
| 2309 | found = 0; | ||
| 2310 | } | ||
| 2311 | } | ||
| 2312 | context->mount_id = mount_id; | ||
| 2313 | |||
| 2314 | list_add_tail(&(yaffs_dev_to_lc(dev)->context_list), | ||
| 2315 | &yaffs_context_list); | ||
| 2316 | mutex_unlock(&yaffs_context_lock); | ||
| 2317 | |||
| 2318 | /* Directory search handling... */ | ||
| 2319 | INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts)); | ||
| 2320 | param->remove_obj_fn = yaffs_remove_obj_callback; | ||
| 2321 | |||
| 2322 | mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock)); | ||
| 2323 | |||
| 2324 | yaffs_gross_lock(dev); | ||
| 2325 | |||
| 2326 | err = yaffs_guts_initialise(dev); | ||
| 2327 | |||
| 2328 | yaffs_trace(YAFFS_TRACE_OS, | ||
| 2329 | "yaffs_read_super: guts initialised %s", | ||
| 2330 | (err == YAFFS_OK) ? "OK" : "FAILED"); | ||
| 2331 | |||
| 2332 | if (err == YAFFS_OK) | ||
| 2333 | yaffs_bg_start(dev); | ||
| 2334 | |||
| 2335 | if (!context->bg_thread) | ||
| 2336 | param->defered_dir_update = 0; | ||
| 2337 | |||
| 2338 | /* Release lock before yaffs_get_inode() */ | ||
| 2339 | yaffs_gross_unlock(dev); | ||
| 2340 | |||
| 2341 | /* Create root inode */ | ||
| 2342 | if (err == YAFFS_OK) | ||
| 2343 | inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev)); | ||
| 2344 | |||
| 2345 | if (!inode) | ||
| 2346 | return NULL; | ||
| 2347 | |||
| 2348 | inode->i_op = &yaffs_dir_inode_operations; | ||
| 2349 | inode->i_fop = &yaffs_dir_operations; | ||
| 2350 | |||
| 2351 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode"); | ||
| 2352 | |||
| 2353 | root = d_alloc_root(inode); | ||
| 2354 | |||
| 2355 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done"); | ||
| 2356 | |||
| 2357 | if (!root) { | ||
| 2358 | iput(inode); | ||
| 2359 | return NULL; | ||
| 2360 | } | ||
| 2361 | sb->s_root = root; | ||
| 2362 | sb->s_dirt = !dev->is_checkpointed; | ||
| 2363 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2364 | "yaffs_read_super: is_checkpointed %d", | ||
| 2365 | dev->is_checkpointed); | ||
| 2366 | |||
| 2367 | yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done"); | ||
| 2368 | return sb; | ||
| 2369 | } | ||
| 2370 | |||
| 2371 | static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data, | ||
| 2372 | int silent) | ||
| 2373 | { | ||
| 2374 | return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL; | ||
| 2375 | } | ||
| 2376 | |||
| 2377 | static struct dentry *yaffs_read_super(struct file_system_type *fs, | ||
| 2378 | int flags, const char *dev_name, | ||
| 2379 | void *data) | ||
| 2380 | { | ||
| 2381 | |||
| 2382 | return mount_bdev(fs, flags, dev_name, data, | ||
| 2383 | yaffs_internal_read_super_mtd); | ||
| 2384 | } | ||
| 2385 | |||
| 2386 | static struct file_system_type yaffs_fs_type = { | ||
| 2387 | .owner = THIS_MODULE, | ||
| 2388 | .name = "yaffs", | ||
| 2389 | .mount = yaffs_read_super, | ||
| 2390 | .kill_sb = kill_block_super, | ||
| 2391 | .fs_flags = FS_REQUIRES_DEV, | ||
| 2392 | }; | ||
| 2393 | |||
| 2394 | #ifdef CONFIG_YAFFS_YAFFS2 | ||
| 2395 | |||
| 2396 | static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data, | ||
| 2397 | int silent) | ||
| 2398 | { | ||
| 2399 | return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL; | ||
| 2400 | } | ||
| 2401 | |||
| 2402 | static struct dentry *yaffs2_read_super(struct file_system_type *fs, | ||
| 2403 | int flags, const char *dev_name, void *data) | ||
| 2404 | { | ||
| 2405 | return mount_bdev(fs, flags, dev_name, data, | ||
| 2406 | yaffs2_internal_read_super_mtd); | ||
| 2407 | } | ||
| 2408 | |||
| 2409 | static struct file_system_type yaffs2_fs_type = { | ||
| 2410 | .owner = THIS_MODULE, | ||
| 2411 | .name = "yaffs2", | ||
| 2412 | .mount = yaffs2_read_super, | ||
| 2413 | .kill_sb = kill_block_super, | ||
| 2414 | .fs_flags = FS_REQUIRES_DEV, | ||
| 2415 | }; | ||
| 2416 | #endif /* CONFIG_YAFFS_YAFFS2 */ | ||
| 2417 | |||
| 2418 | static struct proc_dir_entry *my_proc_entry; | ||
| 2419 | |||
| 2420 | static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev) | ||
| 2421 | { | ||
| 2422 | struct yaffs_param *param = &dev->param; | ||
| 2423 | buf += sprintf(buf, "start_block........... %d\n", param->start_block); | ||
| 2424 | buf += sprintf(buf, "end_block............. %d\n", param->end_block); | ||
| 2425 | buf += sprintf(buf, "total_bytes_per_chunk. %d\n", | ||
| 2426 | param->total_bytes_per_chunk); | ||
| 2427 | buf += sprintf(buf, "use_nand_ecc.......... %d\n", | ||
| 2428 | param->use_nand_ecc); | ||
| 2429 | buf += sprintf(buf, "no_tags_ecc........... %d\n", param->no_tags_ecc); | ||
| 2430 | buf += sprintf(buf, "is_yaffs2............. %d\n", param->is_yaffs2); | ||
| 2431 | buf += sprintf(buf, "inband_tags........... %d\n", param->inband_tags); | ||
| 2432 | buf += sprintf(buf, "empty_lost_n_found.... %d\n", | ||
| 2433 | param->empty_lost_n_found); | ||
| 2434 | buf += sprintf(buf, "disable_lazy_load..... %d\n", | ||
| 2435 | param->disable_lazy_load); | ||
| 2436 | buf += sprintf(buf, "refresh_period........ %d\n", | ||
| 2437 | param->refresh_period); | ||
| 2438 | buf += sprintf(buf, "n_caches.............. %d\n", param->n_caches); | ||
| 2439 | buf += sprintf(buf, "n_reserved_blocks..... %d\n", | ||
| 2440 | param->n_reserved_blocks); | ||
| 2441 | buf += sprintf(buf, "always_check_erased... %d\n", | ||
| 2442 | param->always_check_erased); | ||
| 2443 | |||
| 2444 | return buf; | ||
| 2445 | } | ||
| 2446 | |||
| 2447 | static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev) | ||
| 2448 | { | ||
| 2449 | buf += | ||
| 2450 | sprintf(buf, "data_bytes_per_chunk.. %d\n", | ||
| 2451 | dev->data_bytes_per_chunk); | ||
| 2452 | buf += sprintf(buf, "chunk_grp_bits........ %d\n", dev->chunk_grp_bits); | ||
| 2453 | buf += sprintf(buf, "chunk_grp_size........ %d\n", dev->chunk_grp_size); | ||
| 2454 | buf += | ||
| 2455 | sprintf(buf, "n_erased_blocks....... %d\n", dev->n_erased_blocks); | ||
| 2456 | buf += | ||
| 2457 | sprintf(buf, "blocks_in_checkpt..... %d\n", dev->blocks_in_checkpt); | ||
| 2458 | buf += sprintf(buf, "\n"); | ||
| 2459 | buf += sprintf(buf, "n_tnodes.............. %d\n", dev->n_tnodes); | ||
| 2460 | buf += sprintf(buf, "n_obj................. %d\n", dev->n_obj); | ||
| 2461 | buf += sprintf(buf, "n_free_chunks......... %d\n", dev->n_free_chunks); | ||
| 2462 | buf += sprintf(buf, "\n"); | ||
| 2463 | buf += sprintf(buf, "n_page_writes......... %u\n", dev->n_page_writes); | ||
| 2464 | buf += sprintf(buf, "n_page_reads.......... %u\n", dev->n_page_reads); | ||
| 2465 | buf += sprintf(buf, "n_erasures............ %u\n", dev->n_erasures); | ||
| 2466 | buf += sprintf(buf, "n_gc_copies........... %u\n", dev->n_gc_copies); | ||
| 2467 | buf += sprintf(buf, "all_gcs............... %u\n", dev->all_gcs); | ||
| 2468 | buf += | ||
| 2469 | sprintf(buf, "passive_gc_count...... %u\n", dev->passive_gc_count); | ||
| 2470 | buf += | ||
| 2471 | sprintf(buf, "oldest_dirty_gc_count. %u\n", | ||
| 2472 | dev->oldest_dirty_gc_count); | ||
| 2473 | buf += sprintf(buf, "n_gc_blocks........... %u\n", dev->n_gc_blocks); | ||
| 2474 | buf += sprintf(buf, "bg_gcs................ %u\n", dev->bg_gcs); | ||
| 2475 | buf += | ||
| 2476 | sprintf(buf, "n_retired_writes...... %u\n", dev->n_retired_writes); | ||
| 2477 | buf += | ||
| 2478 | sprintf(buf, "n_retired_blocks...... %u\n", dev->n_retired_blocks); | ||
| 2479 | buf += sprintf(buf, "n_ecc_fixed........... %u\n", dev->n_ecc_fixed); | ||
| 2480 | buf += sprintf(buf, "n_ecc_unfixed......... %u\n", dev->n_ecc_unfixed); | ||
| 2481 | buf += | ||
| 2482 | sprintf(buf, "n_tags_ecc_fixed...... %u\n", dev->n_tags_ecc_fixed); | ||
| 2483 | buf += | ||
| 2484 | sprintf(buf, "n_tags_ecc_unfixed.... %u\n", | ||
| 2485 | dev->n_tags_ecc_unfixed); | ||
| 2486 | buf += sprintf(buf, "cache_hits............ %u\n", dev->cache_hits); | ||
| 2487 | buf += | ||
| 2488 | sprintf(buf, "n_deleted_files....... %u\n", dev->n_deleted_files); | ||
| 2489 | buf += | ||
| 2490 | sprintf(buf, "n_unlinked_files...... %u\n", dev->n_unlinked_files); | ||
| 2491 | buf += sprintf(buf, "refresh_count......... %u\n", dev->refresh_count); | ||
| 2492 | buf += sprintf(buf, "n_bg_deletions........ %u\n", dev->n_bg_deletions); | ||
| 2493 | |||
| 2494 | return buf; | ||
| 2495 | } | ||
| 2496 | |||
| 2497 | static int yaffs_proc_read(char *page, | ||
| 2498 | char **start, | ||
| 2499 | off_t offset, int count, int *eof, void *data) | ||
| 2500 | { | ||
| 2501 | struct list_head *item; | ||
| 2502 | char *buf = page; | ||
| 2503 | int step = offset; | ||
| 2504 | int n = 0; | ||
| 2505 | |||
| 2506 | /* Get proc_file_read() to step 'offset' by one on each sucessive call. | ||
| 2507 | * We use 'offset' (*ppos) to indicate where we are in dev_list. | ||
| 2508 | * This also assumes the user has posted a read buffer large | ||
| 2509 | * enough to hold the complete output; but that's life in /proc. | ||
| 2510 | */ | ||
| 2511 | |||
| 2512 | *(int *)start = 1; | ||
| 2513 | |||
| 2514 | /* Print header first */ | ||
| 2515 | if (step == 0) | ||
| 2516 | buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__ "\n"); | ||
| 2517 | else if (step == 1) | ||
| 2518 | buf += sprintf(buf, "\n"); | ||
| 2519 | else { | ||
| 2520 | step -= 2; | ||
| 2521 | |||
| 2522 | mutex_lock(&yaffs_context_lock); | ||
| 2523 | |||
| 2524 | /* Locate and print the Nth entry. Order N-squared but N is small. */ | ||
| 2525 | list_for_each(item, &yaffs_context_list) { | ||
| 2526 | struct yaffs_linux_context *dc = | ||
| 2527 | list_entry(item, struct yaffs_linux_context, | ||
| 2528 | context_list); | ||
| 2529 | struct yaffs_dev *dev = dc->dev; | ||
| 2530 | |||
| 2531 | if (n < (step & ~1)) { | ||
| 2532 | n += 2; | ||
| 2533 | continue; | ||
| 2534 | } | ||
| 2535 | if ((step & 1) == 0) { | ||
| 2536 | buf += | ||
| 2537 | sprintf(buf, "\nDevice %d \"%s\"\n", n, | ||
| 2538 | dev->param.name); | ||
| 2539 | buf = yaffs_dump_dev_part0(buf, dev); | ||
| 2540 | } else { | ||
| 2541 | buf = yaffs_dump_dev_part1(buf, dev); | ||
| 2542 | } | ||
| 2543 | |||
| 2544 | break; | ||
| 2545 | } | ||
| 2546 | mutex_unlock(&yaffs_context_lock); | ||
| 2547 | } | ||
| 2548 | |||
| 2549 | return buf - page < count ? buf - page : count; | ||
| 2550 | } | ||
| 2551 | |||
| 2552 | |||
| 2553 | /** | ||
| 2554 | * Set the verbosity of the warnings and error messages. | ||
| 2555 | * | ||
| 2556 | * Note that the names can only be a..z or _ with the current code. | ||
| 2557 | */ | ||
| 2558 | |||
| 2559 | static struct { | ||
| 2560 | char *mask_name; | ||
| 2561 | unsigned mask_bitfield; | ||
| 2562 | } mask_flags[] = { | ||
| 2563 | {"allocate", YAFFS_TRACE_ALLOCATE}, | ||
| 2564 | {"always", YAFFS_TRACE_ALWAYS}, | ||
| 2565 | {"background", YAFFS_TRACE_BACKGROUND}, | ||
| 2566 | {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS}, | ||
| 2567 | {"buffers", YAFFS_TRACE_BUFFERS}, | ||
| 2568 | {"bug", YAFFS_TRACE_BUG}, | ||
| 2569 | {"checkpt", YAFFS_TRACE_CHECKPOINT}, | ||
| 2570 | {"deletion", YAFFS_TRACE_DELETION}, | ||
| 2571 | {"erase", YAFFS_TRACE_ERASE}, | ||
| 2572 | {"error", YAFFS_TRACE_ERROR}, | ||
| 2573 | {"gc_detail", YAFFS_TRACE_GC_DETAIL}, | ||
| 2574 | {"gc", YAFFS_TRACE_GC}, | ||
| 2575 | {"lock", YAFFS_TRACE_LOCK}, | ||
| 2576 | {"mtd", YAFFS_TRACE_MTD}, | ||
| 2577 | {"nandaccess", YAFFS_TRACE_NANDACCESS}, | ||
| 2578 | {"os", YAFFS_TRACE_OS}, | ||
| 2579 | {"scan_debug", YAFFS_TRACE_SCAN_DEBUG}, | ||
| 2580 | {"scan", YAFFS_TRACE_SCAN}, | ||
| 2581 | {"mount", YAFFS_TRACE_MOUNT}, | ||
| 2582 | {"tracing", YAFFS_TRACE_TRACING}, | ||
| 2583 | {"sync", YAFFS_TRACE_SYNC}, | ||
| 2584 | {"write", YAFFS_TRACE_WRITE}, | ||
| 2585 | {"verify", YAFFS_TRACE_VERIFY}, | ||
| 2586 | {"verify_nand", YAFFS_TRACE_VERIFY_NAND}, | ||
| 2587 | {"verify_full", YAFFS_TRACE_VERIFY_FULL}, | ||
| 2588 | {"verify_all", YAFFS_TRACE_VERIFY_ALL}, | ||
| 2589 | {"all", 0xffffffff}, | ||
| 2590 | {"none", 0}, | ||
| 2591 | {NULL, 0}, | ||
| 2592 | }; | ||
| 2593 | |||
| 2594 | #define MAX_MASK_NAME_LENGTH 40 | ||
| 2595 | static int yaffs_proc_write_trace_options(struct file *file, const char *buf, | ||
| 2596 | unsigned long count, void *data) | ||
| 2597 | { | ||
| 2598 | unsigned rg = 0, mask_bitfield; | ||
| 2599 | char *end; | ||
| 2600 | char *mask_name; | ||
| 2601 | const char *x; | ||
| 2602 | char substring[MAX_MASK_NAME_LENGTH + 1]; | ||
| 2603 | int i; | ||
| 2604 | int done = 0; | ||
| 2605 | int add, len = 0; | ||
| 2606 | int pos = 0; | ||
| 2607 | |||
| 2608 | rg = yaffs_trace_mask; | ||
| 2609 | |||
| 2610 | while (!done && (pos < count)) { | ||
| 2611 | done = 1; | ||
| 2612 | while ((pos < count) && isspace(buf[pos])) | ||
| 2613 | pos++; | ||
| 2614 | |||
| 2615 | switch (buf[pos]) { | ||
| 2616 | case '+': | ||
| 2617 | case '-': | ||
| 2618 | case '=': | ||
| 2619 | add = buf[pos]; | ||
| 2620 | pos++; | ||
| 2621 | break; | ||
| 2622 | |||
| 2623 | default: | ||
| 2624 | add = ' '; | ||
| 2625 | break; | ||
| 2626 | } | ||
| 2627 | mask_name = NULL; | ||
| 2628 | |||
| 2629 | mask_bitfield = simple_strtoul(buf + pos, &end, 0); | ||
| 2630 | |||
| 2631 | if (end > buf + pos) { | ||
| 2632 | mask_name = "numeral"; | ||
| 2633 | len = end - (buf + pos); | ||
| 2634 | pos += len; | ||
| 2635 | done = 0; | ||
| 2636 | } else { | ||
| 2637 | for (x = buf + pos, i = 0; | ||
| 2638 | (*x == '_' || (*x >= 'a' && *x <= 'z')) && | ||
| 2639 | i < MAX_MASK_NAME_LENGTH; x++, i++, pos++) | ||
| 2640 | substring[i] = *x; | ||
| 2641 | substring[i] = '\0'; | ||
| 2642 | |||
| 2643 | for (i = 0; mask_flags[i].mask_name != NULL; i++) { | ||
| 2644 | if (strcmp(substring, mask_flags[i].mask_name) | ||
| 2645 | == 0) { | ||
| 2646 | mask_name = mask_flags[i].mask_name; | ||
| 2647 | mask_bitfield = | ||
| 2648 | mask_flags[i].mask_bitfield; | ||
| 2649 | done = 0; | ||
| 2650 | break; | ||
| 2651 | } | ||
| 2652 | } | ||
| 2653 | } | ||
| 2654 | |||
| 2655 | if (mask_name != NULL) { | ||
| 2656 | done = 0; | ||
| 2657 | switch (add) { | ||
| 2658 | case '-': | ||
| 2659 | rg &= ~mask_bitfield; | ||
| 2660 | break; | ||
| 2661 | case '+': | ||
| 2662 | rg |= mask_bitfield; | ||
| 2663 | break; | ||
| 2664 | case '=': | ||
| 2665 | rg = mask_bitfield; | ||
| 2666 | break; | ||
| 2667 | default: | ||
| 2668 | rg |= mask_bitfield; | ||
| 2669 | break; | ||
| 2670 | } | ||
| 2671 | } | ||
| 2672 | } | ||
| 2673 | |||
| 2674 | yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS; | ||
| 2675 | |||
| 2676 | printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask); | ||
| 2677 | |||
| 2678 | if (rg & YAFFS_TRACE_ALWAYS) { | ||
| 2679 | for (i = 0; mask_flags[i].mask_name != NULL; i++) { | ||
| 2680 | char flag; | ||
| 2681 | flag = ((rg & mask_flags[i].mask_bitfield) == | ||
| 2682 | mask_flags[i].mask_bitfield) ? '+' : '-'; | ||
| 2683 | printk(KERN_DEBUG "%c%s\n", flag, | ||
| 2684 | mask_flags[i].mask_name); | ||
| 2685 | } | ||
| 2686 | } | ||
| 2687 | |||
| 2688 | return count; | ||
| 2689 | } | ||
| 2690 | |||
| 2691 | static int yaffs_proc_write(struct file *file, const char *buf, | ||
| 2692 | unsigned long count, void *data) | ||
| 2693 | { | ||
| 2694 | return yaffs_proc_write_trace_options(file, buf, count, data); | ||
| 2695 | } | ||
| 2696 | |||
| 2697 | /* Stuff to handle installation of file systems */ | ||
| 2698 | struct file_system_to_install { | ||
| 2699 | struct file_system_type *fst; | ||
| 2700 | int installed; | ||
| 2701 | }; | ||
| 2702 | |||
| 2703 | static struct file_system_to_install fs_to_install[] = { | ||
| 2704 | {&yaffs_fs_type, 0}, | ||
| 2705 | {&yaffs2_fs_type, 0}, | ||
| 2706 | {NULL, 0} | ||
| 2707 | }; | ||
| 2708 | |||
| 2709 | static int __init init_yaffs_fs(void) | ||
| 2710 | { | ||
| 2711 | int error = 0; | ||
| 2712 | struct file_system_to_install *fsinst; | ||
| 2713 | |||
| 2714 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2715 | "yaffs built " __DATE__ " " __TIME__ " Installing."); | ||
| 2716 | |||
| 2717 | #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED | ||
| 2718 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2719 | "\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n"); | ||
| 2720 | #endif | ||
| 2721 | |||
| 2722 | mutex_init(&yaffs_context_lock); | ||
| 2723 | |||
| 2724 | /* Install the proc_fs entries */ | ||
| 2725 | my_proc_entry = create_proc_entry("yaffs", | ||
| 2726 | S_IRUGO | S_IFREG, YPROC_ROOT); | ||
| 2727 | |||
| 2728 | if (my_proc_entry) { | ||
| 2729 | my_proc_entry->write_proc = yaffs_proc_write; | ||
| 2730 | my_proc_entry->read_proc = yaffs_proc_read; | ||
| 2731 | my_proc_entry->data = NULL; | ||
| 2732 | } else { | ||
| 2733 | return -ENOMEM; | ||
| 2734 | } | ||
| 2735 | |||
| 2736 | |||
| 2737 | /* Now add the file system entries */ | ||
| 2738 | |||
| 2739 | fsinst = fs_to_install; | ||
| 2740 | |||
| 2741 | while (fsinst->fst && !error) { | ||
| 2742 | error = register_filesystem(fsinst->fst); | ||
| 2743 | if (!error) | ||
| 2744 | fsinst->installed = 1; | ||
| 2745 | fsinst++; | ||
| 2746 | } | ||
| 2747 | |||
| 2748 | /* Any errors? uninstall */ | ||
| 2749 | if (error) { | ||
| 2750 | fsinst = fs_to_install; | ||
| 2751 | |||
| 2752 | while (fsinst->fst) { | ||
| 2753 | if (fsinst->installed) { | ||
| 2754 | unregister_filesystem(fsinst->fst); | ||
| 2755 | fsinst->installed = 0; | ||
| 2756 | } | ||
| 2757 | fsinst++; | ||
| 2758 | } | ||
| 2759 | } | ||
| 2760 | |||
| 2761 | return error; | ||
| 2762 | } | ||
| 2763 | |||
| 2764 | static void __exit exit_yaffs_fs(void) | ||
| 2765 | { | ||
| 2766 | |||
| 2767 | struct file_system_to_install *fsinst; | ||
| 2768 | |||
| 2769 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 2770 | "yaffs built " __DATE__ " " __TIME__ " removing."); | ||
| 2771 | |||
| 2772 | remove_proc_entry("yaffs", YPROC_ROOT); | ||
| 2773 | |||
| 2774 | fsinst = fs_to_install; | ||
| 2775 | |||
| 2776 | while (fsinst->fst) { | ||
| 2777 | if (fsinst->installed) { | ||
| 2778 | unregister_filesystem(fsinst->fst); | ||
| 2779 | fsinst->installed = 0; | ||
| 2780 | } | ||
| 2781 | fsinst++; | ||
| 2782 | } | ||
| 2783 | } | ||
| 2784 | |||
| 2785 | module_init(init_yaffs_fs) | ||
| 2786 | module_exit(exit_yaffs_fs) | ||
| 2787 | |||
| 2788 | MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system"); | ||
| 2789 | MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010"); | ||
| 2790 | MODULE_LICENSE("GPL"); | ||
diff --git a/fs/yaffs2/yaffs_yaffs1.c b/fs/yaffs2/yaffs_yaffs1.c new file mode 100644 index 00000000000..9eb60308254 --- /dev/null +++ b/fs/yaffs2/yaffs_yaffs1.c | |||
| @@ -0,0 +1,433 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_yaffs1.h" | ||
| 15 | #include "yportenv.h" | ||
| 16 | #include "yaffs_trace.h" | ||
| 17 | #include "yaffs_bitmap.h" | ||
| 18 | #include "yaffs_getblockinfo.h" | ||
| 19 | #include "yaffs_nand.h" | ||
| 20 | #include "yaffs_attribs.h" | ||
| 21 | |||
| 22 | int yaffs1_scan(struct yaffs_dev *dev) | ||
| 23 | { | ||
| 24 | struct yaffs_ext_tags tags; | ||
| 25 | int blk; | ||
| 26 | int result; | ||
| 27 | |||
| 28 | int chunk; | ||
| 29 | int c; | ||
| 30 | int deleted; | ||
| 31 | enum yaffs_block_state state; | ||
| 32 | struct yaffs_obj *hard_list = NULL; | ||
| 33 | struct yaffs_block_info *bi; | ||
| 34 | u32 seq_number; | ||
| 35 | struct yaffs_obj_hdr *oh; | ||
| 36 | struct yaffs_obj *in; | ||
| 37 | struct yaffs_obj *parent; | ||
| 38 | |||
| 39 | int alloc_failed = 0; | ||
| 40 | |||
| 41 | struct yaffs_shadow_fixer *shadow_fixers = NULL; | ||
| 42 | |||
| 43 | u8 *chunk_data; | ||
| 44 | |||
| 45 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 46 | "yaffs1_scan starts intstartblk %d intendblk %d...", | ||
| 47 | dev->internal_start_block, dev->internal_end_block); | ||
| 48 | |||
| 49 | chunk_data = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 50 | |||
| 51 | dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER; | ||
| 52 | |||
| 53 | /* Scan all the blocks to determine their state */ | ||
| 54 | bi = dev->block_info; | ||
| 55 | for (blk = dev->internal_start_block; blk <= dev->internal_end_block; | ||
| 56 | blk++) { | ||
| 57 | yaffs_clear_chunk_bits(dev, blk); | ||
| 58 | bi->pages_in_use = 0; | ||
| 59 | bi->soft_del_pages = 0; | ||
| 60 | |||
| 61 | yaffs_query_init_block_state(dev, blk, &state, &seq_number); | ||
| 62 | |||
| 63 | bi->block_state = state; | ||
| 64 | bi->seq_number = seq_number; | ||
| 65 | |||
| 66 | if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK) | ||
| 67 | bi->block_state = state = YAFFS_BLOCK_STATE_DEAD; | ||
| 68 | |||
| 69 | yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, | ||
| 70 | "Block scanning block %d state %d seq %d", | ||
| 71 | blk, state, seq_number); | ||
| 72 | |||
| 73 | if (state == YAFFS_BLOCK_STATE_DEAD) { | ||
| 74 | yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, | ||
| 75 | "block %d is bad", blk); | ||
| 76 | } else if (state == YAFFS_BLOCK_STATE_EMPTY) { | ||
| 77 | yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty "); | ||
| 78 | dev->n_erased_blocks++; | ||
| 79 | dev->n_free_chunks += dev->param.chunks_per_block; | ||
| 80 | } | ||
| 81 | bi++; | ||
| 82 | } | ||
| 83 | |||
| 84 | /* For each block.... */ | ||
| 85 | for (blk = dev->internal_start_block; | ||
| 86 | !alloc_failed && blk <= dev->internal_end_block; blk++) { | ||
| 87 | |||
| 88 | cond_resched(); | ||
| 89 | |||
| 90 | bi = yaffs_get_block_info(dev, blk); | ||
| 91 | state = bi->block_state; | ||
| 92 | |||
| 93 | deleted = 0; | ||
| 94 | |||
| 95 | /* For each chunk in each block that needs scanning.... */ | ||
| 96 | for (c = 0; !alloc_failed && c < dev->param.chunks_per_block && | ||
| 97 | state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) { | ||
| 98 | /* Read the tags and decide what to do */ | ||
| 99 | chunk = blk * dev->param.chunks_per_block + c; | ||
| 100 | |||
| 101 | result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, | ||
| 102 | &tags); | ||
| 103 | |||
| 104 | /* Let's have a good look at this chunk... */ | ||
| 105 | |||
| 106 | if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED | ||
| 107 | || tags.is_deleted) { | ||
| 108 | /* YAFFS1 only... | ||
| 109 | * A deleted chunk | ||
| 110 | */ | ||
| 111 | deleted++; | ||
| 112 | dev->n_free_chunks++; | ||
| 113 | /*T((" %d %d deleted\n",blk,c)); */ | ||
| 114 | } else if (!tags.chunk_used) { | ||
| 115 | /* An unassigned chunk in the block | ||
| 116 | * This means that either the block is empty or | ||
| 117 | * this is the one being allocated from | ||
| 118 | */ | ||
| 119 | |||
| 120 | if (c == 0) { | ||
| 121 | /* We're looking at the first chunk in the block so the block is unused */ | ||
| 122 | state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 123 | dev->n_erased_blocks++; | ||
| 124 | } else { | ||
| 125 | /* this is the block being allocated from */ | ||
| 126 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 127 | " Allocating from %d %d", | ||
| 128 | blk, c); | ||
| 129 | state = YAFFS_BLOCK_STATE_ALLOCATING; | ||
| 130 | dev->alloc_block = blk; | ||
| 131 | dev->alloc_page = c; | ||
| 132 | dev->alloc_block_finder = blk; | ||
| 133 | /* Set block finder here to encourage the allocator to go forth from here. */ | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | dev->n_free_chunks += | ||
| 138 | (dev->param.chunks_per_block - c); | ||
| 139 | } else if (tags.chunk_id > 0) { | ||
| 140 | /* chunk_id > 0 so it is a data chunk... */ | ||
| 141 | unsigned int endpos; | ||
| 142 | |||
| 143 | yaffs_set_chunk_bit(dev, blk, c); | ||
| 144 | bi->pages_in_use++; | ||
| 145 | |||
| 146 | in = yaffs_find_or_create_by_number(dev, | ||
| 147 | tags.obj_id, | ||
| 148 | YAFFS_OBJECT_TYPE_FILE); | ||
| 149 | /* PutChunkIntoFile checks for a clash (two data chunks with | ||
| 150 | * the same chunk_id). | ||
| 151 | */ | ||
| 152 | |||
| 153 | if (!in) | ||
| 154 | alloc_failed = 1; | ||
| 155 | |||
| 156 | if (in) { | ||
| 157 | if (!yaffs_put_chunk_in_file | ||
| 158 | (in, tags.chunk_id, chunk, 1)) | ||
| 159 | alloc_failed = 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | endpos = | ||
| 163 | (tags.chunk_id - | ||
| 164 | 1) * dev->data_bytes_per_chunk + | ||
| 165 | tags.n_bytes; | ||
| 166 | if (in | ||
| 167 | && in->variant_type == | ||
| 168 | YAFFS_OBJECT_TYPE_FILE | ||
| 169 | && in->variant.file_variant.scanned_size < | ||
| 170 | endpos) { | ||
| 171 | in->variant.file_variant.scanned_size = | ||
| 172 | endpos; | ||
| 173 | if (!dev->param.use_header_file_size) { | ||
| 174 | in->variant. | ||
| 175 | file_variant.file_size = | ||
| 176 | in->variant. | ||
| 177 | file_variant.scanned_size; | ||
| 178 | } | ||
| 179 | |||
| 180 | } | ||
| 181 | /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */ | ||
| 182 | } else { | ||
| 183 | /* chunk_id == 0, so it is an ObjectHeader. | ||
| 184 | * Thus, we read in the object header and make the object | ||
| 185 | */ | ||
| 186 | yaffs_set_chunk_bit(dev, blk, c); | ||
| 187 | bi->pages_in_use++; | ||
| 188 | |||
| 189 | result = yaffs_rd_chunk_tags_nand(dev, chunk, | ||
| 190 | chunk_data, | ||
| 191 | NULL); | ||
| 192 | |||
| 193 | oh = (struct yaffs_obj_hdr *)chunk_data; | ||
| 194 | |||
| 195 | in = yaffs_find_by_number(dev, tags.obj_id); | ||
| 196 | if (in && in->variant_type != oh->type) { | ||
| 197 | /* This should not happen, but somehow | ||
| 198 | * Wev'e ended up with an obj_id that has been reused but not yet | ||
| 199 | * deleted, and worse still it has changed type. Delete the old object. | ||
| 200 | */ | ||
| 201 | |||
| 202 | yaffs_del_obj(in); | ||
| 203 | |||
| 204 | in = 0; | ||
| 205 | } | ||
| 206 | |||
| 207 | in = yaffs_find_or_create_by_number(dev, | ||
| 208 | tags.obj_id, | ||
| 209 | oh->type); | ||
| 210 | |||
| 211 | if (!in) | ||
| 212 | alloc_failed = 1; | ||
| 213 | |||
| 214 | if (in && oh->shadows_obj > 0) { | ||
| 215 | |||
| 216 | struct yaffs_shadow_fixer *fixer; | ||
| 217 | fixer = | ||
| 218 | kmalloc(sizeof | ||
| 219 | (struct yaffs_shadow_fixer), | ||
| 220 | GFP_NOFS); | ||
| 221 | if (fixer) { | ||
| 222 | fixer->next = shadow_fixers; | ||
| 223 | shadow_fixers = fixer; | ||
| 224 | fixer->obj_id = tags.obj_id; | ||
| 225 | fixer->shadowed_id = | ||
| 226 | oh->shadows_obj; | ||
| 227 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 228 | " Shadow fixer: %d shadows %d", | ||
| 229 | fixer->obj_id, | ||
| 230 | fixer->shadowed_id); | ||
| 231 | |||
| 232 | } | ||
| 233 | |||
| 234 | } | ||
| 235 | |||
| 236 | if (in && in->valid) { | ||
| 237 | /* We have already filled this one. We have a duplicate and need to resolve it. */ | ||
| 238 | |||
| 239 | unsigned existing_serial = in->serial; | ||
| 240 | unsigned new_serial = | ||
| 241 | tags.serial_number; | ||
| 242 | |||
| 243 | if (((existing_serial + 1) & 3) == | ||
| 244 | new_serial) { | ||
| 245 | /* Use new one - destroy the exisiting one */ | ||
| 246 | yaffs_chunk_del(dev, | ||
| 247 | in->hdr_chunk, | ||
| 248 | 1, __LINE__); | ||
| 249 | in->valid = 0; | ||
| 250 | } else { | ||
| 251 | /* Use existing - destroy this one. */ | ||
| 252 | yaffs_chunk_del(dev, chunk, 1, | ||
| 253 | __LINE__); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | if (in && !in->valid && | ||
| 258 | (tags.obj_id == YAFFS_OBJECTID_ROOT || | ||
| 259 | tags.obj_id == | ||
| 260 | YAFFS_OBJECTID_LOSTNFOUND)) { | ||
| 261 | /* We only load some info, don't fiddle with directory structure */ | ||
| 262 | in->valid = 1; | ||
| 263 | in->variant_type = oh->type; | ||
| 264 | |||
| 265 | in->yst_mode = oh->yst_mode; | ||
| 266 | yaffs_load_attribs(in, oh); | ||
| 267 | in->hdr_chunk = chunk; | ||
| 268 | in->serial = tags.serial_number; | ||
| 269 | |||
| 270 | } else if (in && !in->valid) { | ||
| 271 | /* we need to load this info */ | ||
| 272 | |||
| 273 | in->valid = 1; | ||
| 274 | in->variant_type = oh->type; | ||
| 275 | |||
| 276 | in->yst_mode = oh->yst_mode; | ||
| 277 | yaffs_load_attribs(in, oh); | ||
| 278 | in->hdr_chunk = chunk; | ||
| 279 | in->serial = tags.serial_number; | ||
| 280 | |||
| 281 | yaffs_set_obj_name_from_oh(in, oh); | ||
| 282 | in->dirty = 0; | ||
| 283 | |||
| 284 | /* directory stuff... | ||
| 285 | * hook up to parent | ||
| 286 | */ | ||
| 287 | |||
| 288 | parent = | ||
| 289 | yaffs_find_or_create_by_number | ||
| 290 | (dev, oh->parent_obj_id, | ||
| 291 | YAFFS_OBJECT_TYPE_DIRECTORY); | ||
| 292 | if (!parent) | ||
| 293 | alloc_failed = 1; | ||
| 294 | if (parent && parent->variant_type == | ||
| 295 | YAFFS_OBJECT_TYPE_UNKNOWN) { | ||
| 296 | /* Set up as a directory */ | ||
| 297 | parent->variant_type = | ||
| 298 | YAFFS_OBJECT_TYPE_DIRECTORY; | ||
| 299 | INIT_LIST_HEAD(&parent-> | ||
| 300 | variant.dir_variant.children); | ||
| 301 | } else if (!parent | ||
| 302 | || parent->variant_type != | ||
| 303 | YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 304 | /* Hoosterman, another problem.... | ||
| 305 | * We're trying to use a non-directory as a directory | ||
| 306 | */ | ||
| 307 | |||
| 308 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 309 | "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." | ||
| 310 | ); | ||
| 311 | parent = dev->lost_n_found; | ||
| 312 | } | ||
| 313 | |||
| 314 | yaffs_add_obj_to_dir(parent, in); | ||
| 315 | |||
| 316 | if (0 && (parent == dev->del_dir || | ||
| 317 | parent == | ||
| 318 | dev->unlinked_dir)) { | ||
| 319 | in->deleted = 1; /* If it is unlinked at start up then it wants deleting */ | ||
| 320 | dev->n_deleted_files++; | ||
| 321 | } | ||
| 322 | /* Note re hardlinks. | ||
| 323 | * Since we might scan a hardlink before its equivalent object is scanned | ||
| 324 | * we put them all in a list. | ||
| 325 | * After scanning is complete, we should have all the objects, so we run through this | ||
| 326 | * list and fix up all the chains. | ||
| 327 | */ | ||
| 328 | |||
| 329 | switch (in->variant_type) { | ||
| 330 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 331 | /* Todo got a problem */ | ||
| 332 | break; | ||
| 333 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 334 | if (dev->param. | ||
| 335 | use_header_file_size) | ||
| 336 | |||
| 337 | in->variant. | ||
| 338 | file_variant.file_size | ||
| 339 | = oh->file_size; | ||
| 340 | |||
| 341 | break; | ||
| 342 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 343 | in->variant. | ||
| 344 | hardlink_variant.equiv_id = | ||
| 345 | oh->equiv_id; | ||
| 346 | in->hard_links.next = | ||
| 347 | (struct list_head *) | ||
| 348 | hard_list; | ||
| 349 | hard_list = in; | ||
| 350 | break; | ||
| 351 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 352 | /* Do nothing */ | ||
| 353 | break; | ||
| 354 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 355 | /* Do nothing */ | ||
| 356 | break; | ||
| 357 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 358 | in->variant.symlink_variant. | ||
| 359 | alias = | ||
| 360 | yaffs_clone_str(oh->alias); | ||
| 361 | if (!in->variant. | ||
| 362 | symlink_variant.alias) | ||
| 363 | alloc_failed = 1; | ||
| 364 | break; | ||
| 365 | } | ||
| 366 | |||
| 367 | } | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { | ||
| 372 | /* If we got this far while scanning, then the block is fully allocated. */ | ||
| 373 | state = YAFFS_BLOCK_STATE_FULL; | ||
| 374 | } | ||
| 375 | |||
| 376 | if (state == YAFFS_BLOCK_STATE_ALLOCATING) { | ||
| 377 | /* If the block was partially allocated then treat it as fully allocated. */ | ||
| 378 | state = YAFFS_BLOCK_STATE_FULL; | ||
| 379 | dev->alloc_block = -1; | ||
| 380 | } | ||
| 381 | |||
| 382 | bi->block_state = state; | ||
| 383 | |||
| 384 | /* Now let's see if it was dirty */ | ||
| 385 | if (bi->pages_in_use == 0 && | ||
| 386 | !bi->has_shrink_hdr && | ||
| 387 | bi->block_state == YAFFS_BLOCK_STATE_FULL) { | ||
| 388 | yaffs_block_became_dirty(dev, blk); | ||
| 389 | } | ||
| 390 | |||
| 391 | } | ||
| 392 | |||
| 393 | /* Ok, we've done all the scanning. | ||
| 394 | * Fix up the hard link chains. | ||
| 395 | * We should now have scanned all the objects, now it's time to add these | ||
| 396 | * hardlinks. | ||
| 397 | */ | ||
| 398 | |||
| 399 | yaffs_link_fixup(dev, hard_list); | ||
| 400 | |||
| 401 | /* Fix up any shadowed objects */ | ||
| 402 | { | ||
| 403 | struct yaffs_shadow_fixer *fixer; | ||
| 404 | struct yaffs_obj *obj; | ||
| 405 | |||
| 406 | while (shadow_fixers) { | ||
| 407 | fixer = shadow_fixers; | ||
| 408 | shadow_fixers = fixer->next; | ||
| 409 | /* Complete the rename transaction by deleting the shadowed object | ||
| 410 | * then setting the object header to unshadowed. | ||
| 411 | */ | ||
| 412 | obj = yaffs_find_by_number(dev, fixer->shadowed_id); | ||
| 413 | if (obj) | ||
| 414 | yaffs_del_obj(obj); | ||
| 415 | |||
| 416 | obj = yaffs_find_by_number(dev, fixer->obj_id); | ||
| 417 | |||
| 418 | if (obj) | ||
| 419 | yaffs_update_oh(obj, NULL, 1, 0, 0, NULL); | ||
| 420 | |||
| 421 | kfree(fixer); | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | yaffs_release_temp_buffer(dev, chunk_data, __LINE__); | ||
| 426 | |||
| 427 | if (alloc_failed) | ||
| 428 | return YAFFS_FAIL; | ||
| 429 | |||
| 430 | yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends"); | ||
| 431 | |||
| 432 | return YAFFS_OK; | ||
| 433 | } | ||
diff --git a/fs/yaffs2/yaffs_yaffs1.h b/fs/yaffs2/yaffs_yaffs1.h new file mode 100644 index 00000000000..db23e04973b --- /dev/null +++ b/fs/yaffs2/yaffs_yaffs1.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_YAFFS1_H__ | ||
| 17 | #define __YAFFS_YAFFS1_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | int yaffs1_scan(struct yaffs_dev *dev); | ||
| 21 | |||
| 22 | #endif | ||
diff --git a/fs/yaffs2/yaffs_yaffs2.c b/fs/yaffs2/yaffs_yaffs2.c new file mode 100644 index 00000000000..33397af7003 --- /dev/null +++ b/fs/yaffs2/yaffs_yaffs2.c | |||
| @@ -0,0 +1,1598 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU General Public License version 2 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "yaffs_guts.h" | ||
| 15 | #include "yaffs_trace.h" | ||
| 16 | #include "yaffs_yaffs2.h" | ||
| 17 | #include "yaffs_checkptrw.h" | ||
| 18 | #include "yaffs_bitmap.h" | ||
| 19 | #include "yaffs_nand.h" | ||
| 20 | #include "yaffs_getblockinfo.h" | ||
| 21 | #include "yaffs_verify.h" | ||
| 22 | #include "yaffs_attribs.h" | ||
| 23 | |||
| 24 | /* | ||
| 25 | * Checkpoints are really no benefit on very small partitions. | ||
| 26 | * | ||
| 27 | * To save space on small partitions don't bother with checkpoints unless | ||
| 28 | * the partition is at least this big. | ||
| 29 | */ | ||
| 30 | #define YAFFS_CHECKPOINT_MIN_BLOCKS 60 | ||
| 31 | |||
| 32 | #define YAFFS_SMALL_HOLE_THRESHOLD 4 | ||
| 33 | |||
| 34 | /* | ||
| 35 | * Oldest Dirty Sequence Number handling. | ||
| 36 | */ | ||
| 37 | |||
| 38 | /* yaffs_calc_oldest_dirty_seq() | ||
| 39 | * yaffs2_find_oldest_dirty_seq() | ||
| 40 | * Calculate the oldest dirty sequence number if we don't know it. | ||
| 41 | */ | ||
| 42 | void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev) | ||
| 43 | { | ||
| 44 | int i; | ||
| 45 | unsigned seq; | ||
| 46 | unsigned block_no = 0; | ||
| 47 | struct yaffs_block_info *b; | ||
| 48 | |||
| 49 | if (!dev->param.is_yaffs2) | ||
| 50 | return; | ||
| 51 | |||
| 52 | /* Find the oldest dirty sequence number. */ | ||
| 53 | seq = dev->seq_number + 1; | ||
| 54 | b = dev->block_info; | ||
| 55 | for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { | ||
| 56 | if (b->block_state == YAFFS_BLOCK_STATE_FULL && | ||
| 57 | (b->pages_in_use - b->soft_del_pages) < | ||
| 58 | dev->param.chunks_per_block && b->seq_number < seq) { | ||
| 59 | seq = b->seq_number; | ||
| 60 | block_no = i; | ||
| 61 | } | ||
| 62 | b++; | ||
| 63 | } | ||
| 64 | |||
| 65 | if (block_no) { | ||
| 66 | dev->oldest_dirty_seq = seq; | ||
| 67 | dev->oldest_dirty_block = block_no; | ||
| 68 | } | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev) | ||
| 73 | { | ||
| 74 | if (!dev->param.is_yaffs2) | ||
| 75 | return; | ||
| 76 | |||
| 77 | if (!dev->oldest_dirty_seq) | ||
| 78 | yaffs_calc_oldest_dirty_seq(dev); | ||
| 79 | } | ||
| 80 | |||
| 81 | /* | ||
| 82 | * yaffs_clear_oldest_dirty_seq() | ||
| 83 | * Called when a block is erased or marked bad. (ie. when its seq_number | ||
| 84 | * becomes invalid). If the value matches the oldest then we clear | ||
| 85 | * dev->oldest_dirty_seq to force its recomputation. | ||
| 86 | */ | ||
| 87 | void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev, | ||
| 88 | struct yaffs_block_info *bi) | ||
| 89 | { | ||
| 90 | |||
| 91 | if (!dev->param.is_yaffs2) | ||
| 92 | return; | ||
| 93 | |||
| 94 | if (!bi || bi->seq_number == dev->oldest_dirty_seq) { | ||
| 95 | dev->oldest_dirty_seq = 0; | ||
| 96 | dev->oldest_dirty_block = 0; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | /* | ||
| 101 | * yaffs2_update_oldest_dirty_seq() | ||
| 102 | * Update the oldest dirty sequence number whenever we dirty a block. | ||
| 103 | * Only do this if the oldest_dirty_seq is actually being tracked. | ||
| 104 | */ | ||
| 105 | void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no, | ||
| 106 | struct yaffs_block_info *bi) | ||
| 107 | { | ||
| 108 | if (!dev->param.is_yaffs2) | ||
| 109 | return; | ||
| 110 | |||
| 111 | if (dev->oldest_dirty_seq) { | ||
| 112 | if (dev->oldest_dirty_seq > bi->seq_number) { | ||
| 113 | dev->oldest_dirty_seq = bi->seq_number; | ||
| 114 | dev->oldest_dirty_block = block_no; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi) | ||
| 120 | { | ||
| 121 | |||
| 122 | if (!dev->param.is_yaffs2) | ||
| 123 | return 1; /* disqualification only applies to yaffs2. */ | ||
| 124 | |||
| 125 | if (!bi->has_shrink_hdr) | ||
| 126 | return 1; /* can gc */ | ||
| 127 | |||
| 128 | yaffs2_find_oldest_dirty_seq(dev); | ||
| 129 | |||
| 130 | /* Can't do gc of this block if there are any blocks older than this one that have | ||
| 131 | * discarded pages. | ||
| 132 | */ | ||
| 133 | return (bi->seq_number <= dev->oldest_dirty_seq); | ||
| 134 | } | ||
| 135 | |||
| 136 | /* | ||
| 137 | * yaffs2_find_refresh_block() | ||
| 138 | * periodically finds the oldest full block by sequence number for refreshing. | ||
| 139 | * Only for yaffs2. | ||
| 140 | */ | ||
| 141 | u32 yaffs2_find_refresh_block(struct yaffs_dev * dev) | ||
| 142 | { | ||
| 143 | u32 b; | ||
| 144 | |||
| 145 | u32 oldest = 0; | ||
| 146 | u32 oldest_seq = 0; | ||
| 147 | |||
| 148 | struct yaffs_block_info *bi; | ||
| 149 | |||
| 150 | if (!dev->param.is_yaffs2) | ||
| 151 | return oldest; | ||
| 152 | |||
| 153 | /* | ||
| 154 | * If refresh period < 10 then refreshing is disabled. | ||
| 155 | */ | ||
| 156 | if (dev->param.refresh_period < 10) | ||
| 157 | return oldest; | ||
| 158 | |||
| 159 | /* | ||
| 160 | * Fix broken values. | ||
| 161 | */ | ||
| 162 | if (dev->refresh_skip > dev->param.refresh_period) | ||
| 163 | dev->refresh_skip = dev->param.refresh_period; | ||
| 164 | |||
| 165 | if (dev->refresh_skip > 0) | ||
| 166 | return oldest; | ||
| 167 | |||
| 168 | /* | ||
| 169 | * Refresh skip is now zero. | ||
| 170 | * We'll do a refresh this time around.... | ||
| 171 | * Update the refresh skip and find the oldest block. | ||
| 172 | */ | ||
| 173 | dev->refresh_skip = dev->param.refresh_period; | ||
| 174 | dev->refresh_count++; | ||
| 175 | bi = dev->block_info; | ||
| 176 | for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) { | ||
| 177 | |||
| 178 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL) { | ||
| 179 | |||
| 180 | if (oldest < 1 || bi->seq_number < oldest_seq) { | ||
| 181 | oldest = b; | ||
| 182 | oldest_seq = bi->seq_number; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | bi++; | ||
| 186 | } | ||
| 187 | |||
| 188 | if (oldest > 0) { | ||
| 189 | yaffs_trace(YAFFS_TRACE_GC, | ||
| 190 | "GC refresh count %d selected block %d with seq_number %d", | ||
| 191 | dev->refresh_count, oldest, oldest_seq); | ||
| 192 | } | ||
| 193 | |||
| 194 | return oldest; | ||
| 195 | } | ||
| 196 | |||
| 197 | int yaffs2_checkpt_required(struct yaffs_dev *dev) | ||
| 198 | { | ||
| 199 | int nblocks; | ||
| 200 | |||
| 201 | if (!dev->param.is_yaffs2) | ||
| 202 | return 0; | ||
| 203 | |||
| 204 | nblocks = dev->internal_end_block - dev->internal_start_block + 1; | ||
| 205 | |||
| 206 | return !dev->param.skip_checkpt_wr && | ||
| 207 | !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS); | ||
| 208 | } | ||
| 209 | |||
| 210 | int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev) | ||
| 211 | { | ||
| 212 | int retval; | ||
| 213 | |||
| 214 | if (!dev->param.is_yaffs2) | ||
| 215 | return 0; | ||
| 216 | |||
| 217 | if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) { | ||
| 218 | /* Not a valid value so recalculate */ | ||
| 219 | int n_bytes = 0; | ||
| 220 | int n_blocks; | ||
| 221 | int dev_blocks = | ||
| 222 | (dev->param.end_block - dev->param.start_block + 1); | ||
| 223 | |||
| 224 | n_bytes += sizeof(struct yaffs_checkpt_validity); | ||
| 225 | n_bytes += sizeof(struct yaffs_checkpt_dev); | ||
| 226 | n_bytes += dev_blocks * sizeof(struct yaffs_block_info); | ||
| 227 | n_bytes += dev_blocks * dev->chunk_bit_stride; | ||
| 228 | n_bytes += | ||
| 229 | (sizeof(struct yaffs_checkpt_obj) + | ||
| 230 | sizeof(u32)) * (dev->n_obj); | ||
| 231 | n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes); | ||
| 232 | n_bytes += sizeof(struct yaffs_checkpt_validity); | ||
| 233 | n_bytes += sizeof(u32); /* checksum */ | ||
| 234 | |||
| 235 | /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */ | ||
| 236 | |||
| 237 | n_blocks = | ||
| 238 | (n_bytes / | ||
| 239 | (dev->data_bytes_per_chunk * | ||
| 240 | dev->param.chunks_per_block)) + 3; | ||
| 241 | |||
| 242 | dev->checkpoint_blocks_required = n_blocks; | ||
| 243 | } | ||
| 244 | |||
| 245 | retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt; | ||
| 246 | if (retval < 0) | ||
| 247 | retval = 0; | ||
| 248 | return retval; | ||
| 249 | } | ||
| 250 | |||
| 251 | /*--------------------- Checkpointing --------------------*/ | ||
| 252 | |||
| 253 | static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head) | ||
| 254 | { | ||
| 255 | struct yaffs_checkpt_validity cp; | ||
| 256 | |||
| 257 | memset(&cp, 0, sizeof(cp)); | ||
| 258 | |||
| 259 | cp.struct_type = sizeof(cp); | ||
| 260 | cp.magic = YAFFS_MAGIC; | ||
| 261 | cp.version = YAFFS_CHECKPOINT_VERSION; | ||
| 262 | cp.head = (head) ? 1 : 0; | ||
| 263 | |||
| 264 | return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0; | ||
| 265 | } | ||
| 266 | |||
| 267 | static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head) | ||
| 268 | { | ||
| 269 | struct yaffs_checkpt_validity cp; | ||
| 270 | int ok; | ||
| 271 | |||
| 272 | ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp)); | ||
| 273 | |||
| 274 | if (ok) | ||
| 275 | ok = (cp.struct_type == sizeof(cp)) && | ||
| 276 | (cp.magic == YAFFS_MAGIC) && | ||
| 277 | (cp.version == YAFFS_CHECKPOINT_VERSION) && | ||
| 278 | (cp.head == ((head) ? 1 : 0)); | ||
| 279 | return ok ? 1 : 0; | ||
| 280 | } | ||
| 281 | |||
| 282 | static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp, | ||
| 283 | struct yaffs_dev *dev) | ||
| 284 | { | ||
| 285 | cp->n_erased_blocks = dev->n_erased_blocks; | ||
| 286 | cp->alloc_block = dev->alloc_block; | ||
| 287 | cp->alloc_page = dev->alloc_page; | ||
| 288 | cp->n_free_chunks = dev->n_free_chunks; | ||
| 289 | |||
| 290 | cp->n_deleted_files = dev->n_deleted_files; | ||
| 291 | cp->n_unlinked_files = dev->n_unlinked_files; | ||
| 292 | cp->n_bg_deletions = dev->n_bg_deletions; | ||
| 293 | cp->seq_number = dev->seq_number; | ||
| 294 | |||
| 295 | } | ||
| 296 | |||
| 297 | static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev, | ||
| 298 | struct yaffs_checkpt_dev *cp) | ||
| 299 | { | ||
| 300 | dev->n_erased_blocks = cp->n_erased_blocks; | ||
| 301 | dev->alloc_block = cp->alloc_block; | ||
| 302 | dev->alloc_page = cp->alloc_page; | ||
| 303 | dev->n_free_chunks = cp->n_free_chunks; | ||
| 304 | |||
| 305 | dev->n_deleted_files = cp->n_deleted_files; | ||
| 306 | dev->n_unlinked_files = cp->n_unlinked_files; | ||
| 307 | dev->n_bg_deletions = cp->n_bg_deletions; | ||
| 308 | dev->seq_number = cp->seq_number; | ||
| 309 | } | ||
| 310 | |||
| 311 | static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev) | ||
| 312 | { | ||
| 313 | struct yaffs_checkpt_dev cp; | ||
| 314 | u32 n_bytes; | ||
| 315 | u32 n_blocks = | ||
| 316 | (dev->internal_end_block - dev->internal_start_block + 1); | ||
| 317 | |||
| 318 | int ok; | ||
| 319 | |||
| 320 | /* Write device runtime values */ | ||
| 321 | yaffs2_dev_to_checkpt_dev(&cp, dev); | ||
| 322 | cp.struct_type = sizeof(cp); | ||
| 323 | |||
| 324 | ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)); | ||
| 325 | |||
| 326 | /* Write block info */ | ||
| 327 | if (ok) { | ||
| 328 | n_bytes = n_blocks * sizeof(struct yaffs_block_info); | ||
| 329 | ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == | ||
| 330 | n_bytes); | ||
| 331 | } | ||
| 332 | |||
| 333 | /* Write chunk bits */ | ||
| 334 | if (ok) { | ||
| 335 | n_bytes = n_blocks * dev->chunk_bit_stride; | ||
| 336 | ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == | ||
| 337 | n_bytes); | ||
| 338 | } | ||
| 339 | return ok ? 1 : 0; | ||
| 340 | |||
| 341 | } | ||
| 342 | |||
| 343 | static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev) | ||
| 344 | { | ||
| 345 | struct yaffs_checkpt_dev cp; | ||
| 346 | u32 n_bytes; | ||
| 347 | u32 n_blocks = | ||
| 348 | (dev->internal_end_block - dev->internal_start_block + 1); | ||
| 349 | |||
| 350 | int ok; | ||
| 351 | |||
| 352 | ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp)); | ||
| 353 | if (!ok) | ||
| 354 | return 0; | ||
| 355 | |||
| 356 | if (cp.struct_type != sizeof(cp)) | ||
| 357 | return 0; | ||
| 358 | |||
| 359 | yaffs_checkpt_dev_to_dev(dev, &cp); | ||
| 360 | |||
| 361 | n_bytes = n_blocks * sizeof(struct yaffs_block_info); | ||
| 362 | |||
| 363 | ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes); | ||
| 364 | |||
| 365 | if (!ok) | ||
| 366 | return 0; | ||
| 367 | n_bytes = n_blocks * dev->chunk_bit_stride; | ||
| 368 | |||
| 369 | ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes); | ||
| 370 | |||
| 371 | return ok ? 1 : 0; | ||
| 372 | } | ||
| 373 | |||
| 374 | static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp, | ||
| 375 | struct yaffs_obj *obj) | ||
| 376 | { | ||
| 377 | |||
| 378 | cp->obj_id = obj->obj_id; | ||
| 379 | cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0; | ||
| 380 | cp->hdr_chunk = obj->hdr_chunk; | ||
| 381 | cp->variant_type = obj->variant_type; | ||
| 382 | cp->deleted = obj->deleted; | ||
| 383 | cp->soft_del = obj->soft_del; | ||
| 384 | cp->unlinked = obj->unlinked; | ||
| 385 | cp->fake = obj->fake; | ||
| 386 | cp->rename_allowed = obj->rename_allowed; | ||
| 387 | cp->unlink_allowed = obj->unlink_allowed; | ||
| 388 | cp->serial = obj->serial; | ||
| 389 | cp->n_data_chunks = obj->n_data_chunks; | ||
| 390 | |||
| 391 | if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) | ||
| 392 | cp->size_or_equiv_obj = obj->variant.file_variant.file_size; | ||
| 393 | else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) | ||
| 394 | cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id; | ||
| 395 | } | ||
| 396 | |||
| 397 | static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj, | ||
| 398 | struct yaffs_checkpt_obj *cp) | ||
| 399 | { | ||
| 400 | |||
| 401 | struct yaffs_obj *parent; | ||
| 402 | |||
| 403 | if (obj->variant_type != cp->variant_type) { | ||
| 404 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 405 | "Checkpoint read object %d type %d chunk %d does not match existing object type %d", | ||
| 406 | cp->obj_id, cp->variant_type, cp->hdr_chunk, | ||
| 407 | obj->variant_type); | ||
| 408 | return 0; | ||
| 409 | } | ||
| 410 | |||
| 411 | obj->obj_id = cp->obj_id; | ||
| 412 | |||
| 413 | if (cp->parent_id) | ||
| 414 | parent = yaffs_find_or_create_by_number(obj->my_dev, | ||
| 415 | cp->parent_id, | ||
| 416 | YAFFS_OBJECT_TYPE_DIRECTORY); | ||
| 417 | else | ||
| 418 | parent = NULL; | ||
| 419 | |||
| 420 | if (parent) { | ||
| 421 | if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 422 | yaffs_trace(YAFFS_TRACE_ALWAYS, | ||
| 423 | "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory", | ||
| 424 | cp->obj_id, cp->parent_id, | ||
| 425 | cp->variant_type, cp->hdr_chunk, | ||
| 426 | parent->variant_type); | ||
| 427 | return 0; | ||
| 428 | } | ||
| 429 | yaffs_add_obj_to_dir(parent, obj); | ||
| 430 | } | ||
| 431 | |||
| 432 | obj->hdr_chunk = cp->hdr_chunk; | ||
| 433 | obj->variant_type = cp->variant_type; | ||
| 434 | obj->deleted = cp->deleted; | ||
| 435 | obj->soft_del = cp->soft_del; | ||
| 436 | obj->unlinked = cp->unlinked; | ||
| 437 | obj->fake = cp->fake; | ||
| 438 | obj->rename_allowed = cp->rename_allowed; | ||
| 439 | obj->unlink_allowed = cp->unlink_allowed; | ||
| 440 | obj->serial = cp->serial; | ||
| 441 | obj->n_data_chunks = cp->n_data_chunks; | ||
| 442 | |||
| 443 | if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) | ||
| 444 | obj->variant.file_variant.file_size = cp->size_or_equiv_obj; | ||
| 445 | else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) | ||
| 446 | obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj; | ||
| 447 | |||
| 448 | if (obj->hdr_chunk > 0) | ||
| 449 | obj->lazy_loaded = 1; | ||
| 450 | return 1; | ||
| 451 | } | ||
| 452 | |||
| 453 | static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in, | ||
| 454 | struct yaffs_tnode *tn, u32 level, | ||
| 455 | int chunk_offset) | ||
| 456 | { | ||
| 457 | int i; | ||
| 458 | struct yaffs_dev *dev = in->my_dev; | ||
| 459 | int ok = 1; | ||
| 460 | |||
| 461 | if (tn) { | ||
| 462 | if (level > 0) { | ||
| 463 | |||
| 464 | for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) { | ||
| 465 | if (tn->internal[i]) { | ||
| 466 | ok = yaffs2_checkpt_tnode_worker(in, | ||
| 467 | tn-> | ||
| 468 | internal | ||
| 469 | [i], | ||
| 470 | level - | ||
| 471 | 1, | ||
| 472 | (chunk_offset | ||
| 473 | << | ||
| 474 | YAFFS_TNODES_INTERNAL_BITS) | ||
| 475 | + i); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | } else if (level == 0) { | ||
| 479 | u32 base_offset = | ||
| 480 | chunk_offset << YAFFS_TNODES_LEVEL0_BITS; | ||
| 481 | ok = (yaffs2_checkpt_wr | ||
| 482 | (dev, &base_offset, | ||
| 483 | sizeof(base_offset)) == sizeof(base_offset)); | ||
| 484 | if (ok) | ||
| 485 | ok = (yaffs2_checkpt_wr | ||
| 486 | (dev, tn, | ||
| 487 | dev->tnode_size) == dev->tnode_size); | ||
| 488 | } | ||
| 489 | } | ||
| 490 | |||
| 491 | return ok; | ||
| 492 | |||
| 493 | } | ||
| 494 | |||
| 495 | static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj) | ||
| 496 | { | ||
| 497 | u32 end_marker = ~0; | ||
| 498 | int ok = 1; | ||
| 499 | |||
| 500 | if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) { | ||
| 501 | ok = yaffs2_checkpt_tnode_worker(obj, | ||
| 502 | obj->variant.file_variant.top, | ||
| 503 | obj->variant.file_variant. | ||
| 504 | top_level, 0); | ||
| 505 | if (ok) | ||
| 506 | ok = (yaffs2_checkpt_wr | ||
| 507 | (obj->my_dev, &end_marker, | ||
| 508 | sizeof(end_marker)) == sizeof(end_marker)); | ||
| 509 | } | ||
| 510 | |||
| 511 | return ok ? 1 : 0; | ||
| 512 | } | ||
| 513 | |||
| 514 | static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj) | ||
| 515 | { | ||
| 516 | u32 base_chunk; | ||
| 517 | int ok = 1; | ||
| 518 | struct yaffs_dev *dev = obj->my_dev; | ||
| 519 | struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant; | ||
| 520 | struct yaffs_tnode *tn; | ||
| 521 | int nread = 0; | ||
| 522 | |||
| 523 | ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) == | ||
| 524 | sizeof(base_chunk)); | ||
| 525 | |||
| 526 | while (ok && (~base_chunk)) { | ||
| 527 | nread++; | ||
| 528 | /* Read level 0 tnode */ | ||
| 529 | |||
| 530 | tn = yaffs_get_tnode(dev); | ||
| 531 | if (tn) { | ||
| 532 | ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) == | ||
| 533 | dev->tnode_size); | ||
| 534 | } else { | ||
| 535 | ok = 0; | ||
| 536 | } | ||
| 537 | |||
| 538 | if (tn && ok) | ||
| 539 | ok = yaffs_add_find_tnode_0(dev, | ||
| 540 | file_stuct_ptr, | ||
| 541 | base_chunk, tn) ? 1 : 0; | ||
| 542 | |||
| 543 | if (ok) | ||
| 544 | ok = (yaffs2_checkpt_rd | ||
| 545 | (dev, &base_chunk, | ||
| 546 | sizeof(base_chunk)) == sizeof(base_chunk)); | ||
| 547 | |||
| 548 | } | ||
| 549 | |||
| 550 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 551 | "Checkpoint read tnodes %d records, last %d. ok %d", | ||
| 552 | nread, base_chunk, ok); | ||
| 553 | |||
| 554 | return ok ? 1 : 0; | ||
| 555 | } | ||
| 556 | |||
| 557 | static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev) | ||
| 558 | { | ||
| 559 | struct yaffs_obj *obj; | ||
| 560 | struct yaffs_checkpt_obj cp; | ||
| 561 | int i; | ||
| 562 | int ok = 1; | ||
| 563 | struct list_head *lh; | ||
| 564 | |||
| 565 | /* Iterate through the objects in each hash entry, | ||
| 566 | * dumping them to the checkpointing stream. | ||
| 567 | */ | ||
| 568 | |||
| 569 | for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) { | ||
| 570 | list_for_each(lh, &dev->obj_bucket[i].list) { | ||
| 571 | if (lh) { | ||
| 572 | obj = | ||
| 573 | list_entry(lh, struct yaffs_obj, hash_link); | ||
| 574 | if (!obj->defered_free) { | ||
| 575 | yaffs2_obj_checkpt_obj(&cp, obj); | ||
| 576 | cp.struct_type = sizeof(cp); | ||
| 577 | |||
| 578 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 579 | "Checkpoint write object %d parent %d type %d chunk %d obj addr %p", | ||
| 580 | cp.obj_id, cp.parent_id, | ||
| 581 | cp.variant_type, cp.hdr_chunk, obj); | ||
| 582 | |||
| 583 | ok = (yaffs2_checkpt_wr | ||
| 584 | (dev, &cp, | ||
| 585 | sizeof(cp)) == sizeof(cp)); | ||
| 586 | |||
| 587 | if (ok | ||
| 588 | && obj->variant_type == | ||
| 589 | YAFFS_OBJECT_TYPE_FILE) | ||
| 590 | ok = yaffs2_wr_checkpt_tnodes | ||
| 591 | (obj); | ||
| 592 | } | ||
| 593 | } | ||
| 594 | } | ||
| 595 | } | ||
| 596 | |||
| 597 | /* Dump end of list */ | ||
| 598 | memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj)); | ||
| 599 | cp.struct_type = sizeof(cp); | ||
| 600 | |||
| 601 | if (ok) | ||
| 602 | ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)); | ||
| 603 | |||
| 604 | return ok ? 1 : 0; | ||
| 605 | } | ||
| 606 | |||
| 607 | static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev) | ||
| 608 | { | ||
| 609 | struct yaffs_obj *obj; | ||
| 610 | struct yaffs_checkpt_obj cp; | ||
| 611 | int ok = 1; | ||
| 612 | int done = 0; | ||
| 613 | struct yaffs_obj *hard_list = NULL; | ||
| 614 | |||
| 615 | while (ok && !done) { | ||
| 616 | ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp)); | ||
| 617 | if (cp.struct_type != sizeof(cp)) { | ||
| 618 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 619 | "struct size %d instead of %d ok %d", | ||
| 620 | cp.struct_type, (int)sizeof(cp), ok); | ||
| 621 | ok = 0; | ||
| 622 | } | ||
| 623 | |||
| 624 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 625 | "Checkpoint read object %d parent %d type %d chunk %d ", | ||
| 626 | cp.obj_id, cp.parent_id, cp.variant_type, | ||
| 627 | cp.hdr_chunk); | ||
| 628 | |||
| 629 | if (ok && cp.obj_id == ~0) { | ||
| 630 | done = 1; | ||
| 631 | } else if (ok) { | ||
| 632 | obj = | ||
| 633 | yaffs_find_or_create_by_number(dev, cp.obj_id, | ||
| 634 | cp.variant_type); | ||
| 635 | if (obj) { | ||
| 636 | ok = taffs2_checkpt_obj_to_obj(obj, &cp); | ||
| 637 | if (!ok) | ||
| 638 | break; | ||
| 639 | if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) { | ||
| 640 | ok = yaffs2_rd_checkpt_tnodes(obj); | ||
| 641 | } else if (obj->variant_type == | ||
| 642 | YAFFS_OBJECT_TYPE_HARDLINK) { | ||
| 643 | obj->hard_links.next = | ||
| 644 | (struct list_head *)hard_list; | ||
| 645 | hard_list = obj; | ||
| 646 | } | ||
| 647 | } else { | ||
| 648 | ok = 0; | ||
| 649 | } | ||
| 650 | } | ||
| 651 | } | ||
| 652 | |||
| 653 | if (ok) | ||
| 654 | yaffs_link_fixup(dev, hard_list); | ||
| 655 | |||
| 656 | return ok ? 1 : 0; | ||
| 657 | } | ||
| 658 | |||
| 659 | static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev) | ||
| 660 | { | ||
| 661 | u32 checkpt_sum; | ||
| 662 | int ok; | ||
| 663 | |||
| 664 | yaffs2_get_checkpt_sum(dev, &checkpt_sum); | ||
| 665 | |||
| 666 | ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) == | ||
| 667 | sizeof(checkpt_sum)); | ||
| 668 | |||
| 669 | if (!ok) | ||
| 670 | return 0; | ||
| 671 | |||
| 672 | return 1; | ||
| 673 | } | ||
| 674 | |||
| 675 | static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev) | ||
| 676 | { | ||
| 677 | u32 checkpt_sum0; | ||
| 678 | u32 checkpt_sum1; | ||
| 679 | int ok; | ||
| 680 | |||
| 681 | yaffs2_get_checkpt_sum(dev, &checkpt_sum0); | ||
| 682 | |||
| 683 | ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) == | ||
| 684 | sizeof(checkpt_sum1)); | ||
| 685 | |||
| 686 | if (!ok) | ||
| 687 | return 0; | ||
| 688 | |||
| 689 | if (checkpt_sum0 != checkpt_sum1) | ||
| 690 | return 0; | ||
| 691 | |||
| 692 | return 1; | ||
| 693 | } | ||
| 694 | |||
| 695 | static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev) | ||
| 696 | { | ||
| 697 | int ok = 1; | ||
| 698 | |||
| 699 | if (!yaffs2_checkpt_required(dev)) { | ||
| 700 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 701 | "skipping checkpoint write"); | ||
| 702 | ok = 0; | ||
| 703 | } | ||
| 704 | |||
| 705 | if (ok) | ||
| 706 | ok = yaffs2_checkpt_open(dev, 1); | ||
| 707 | |||
| 708 | if (ok) { | ||
| 709 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 710 | "write checkpoint validity"); | ||
| 711 | ok = yaffs2_wr_checkpt_validity_marker(dev, 1); | ||
| 712 | } | ||
| 713 | if (ok) { | ||
| 714 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 715 | "write checkpoint device"); | ||
| 716 | ok = yaffs2_wr_checkpt_dev(dev); | ||
| 717 | } | ||
| 718 | if (ok) { | ||
| 719 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 720 | "write checkpoint objects"); | ||
| 721 | ok = yaffs2_wr_checkpt_objs(dev); | ||
| 722 | } | ||
| 723 | if (ok) { | ||
| 724 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 725 | "write checkpoint validity"); | ||
| 726 | ok = yaffs2_wr_checkpt_validity_marker(dev, 0); | ||
| 727 | } | ||
| 728 | |||
| 729 | if (ok) | ||
| 730 | ok = yaffs2_wr_checkpt_sum(dev); | ||
| 731 | |||
| 732 | if (!yaffs_checkpt_close(dev)) | ||
| 733 | ok = 0; | ||
| 734 | |||
| 735 | if (ok) | ||
| 736 | dev->is_checkpointed = 1; | ||
| 737 | else | ||
| 738 | dev->is_checkpointed = 0; | ||
| 739 | |||
| 740 | return dev->is_checkpointed; | ||
| 741 | } | ||
| 742 | |||
| 743 | static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev) | ||
| 744 | { | ||
| 745 | int ok = 1; | ||
| 746 | |||
| 747 | if (!dev->param.is_yaffs2) | ||
| 748 | ok = 0; | ||
| 749 | |||
| 750 | if (ok && dev->param.skip_checkpt_rd) { | ||
| 751 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 752 | "skipping checkpoint read"); | ||
| 753 | ok = 0; | ||
| 754 | } | ||
| 755 | |||
| 756 | if (ok) | ||
| 757 | ok = yaffs2_checkpt_open(dev, 0); /* open for read */ | ||
| 758 | |||
| 759 | if (ok) { | ||
| 760 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 761 | "read checkpoint validity"); | ||
| 762 | ok = yaffs2_rd_checkpt_validity_marker(dev, 1); | ||
| 763 | } | ||
| 764 | if (ok) { | ||
| 765 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 766 | "read checkpoint device"); | ||
| 767 | ok = yaffs2_rd_checkpt_dev(dev); | ||
| 768 | } | ||
| 769 | if (ok) { | ||
| 770 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 771 | "read checkpoint objects"); | ||
| 772 | ok = yaffs2_rd_checkpt_objs(dev); | ||
| 773 | } | ||
| 774 | if (ok) { | ||
| 775 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 776 | "read checkpoint validity"); | ||
| 777 | ok = yaffs2_rd_checkpt_validity_marker(dev, 0); | ||
| 778 | } | ||
| 779 | |||
| 780 | if (ok) { | ||
| 781 | ok = yaffs2_rd_checkpt_sum(dev); | ||
| 782 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 783 | "read checkpoint checksum %d", ok); | ||
| 784 | } | ||
| 785 | |||
| 786 | if (!yaffs_checkpt_close(dev)) | ||
| 787 | ok = 0; | ||
| 788 | |||
| 789 | if (ok) | ||
| 790 | dev->is_checkpointed = 1; | ||
| 791 | else | ||
| 792 | dev->is_checkpointed = 0; | ||
| 793 | |||
| 794 | return ok ? 1 : 0; | ||
| 795 | |||
| 796 | } | ||
| 797 | |||
| 798 | void yaffs2_checkpt_invalidate(struct yaffs_dev *dev) | ||
| 799 | { | ||
| 800 | if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) { | ||
| 801 | dev->is_checkpointed = 0; | ||
| 802 | yaffs2_checkpt_invalidate_stream(dev); | ||
| 803 | } | ||
| 804 | if (dev->param.sb_dirty_fn) | ||
| 805 | dev->param.sb_dirty_fn(dev); | ||
| 806 | } | ||
| 807 | |||
| 808 | int yaffs_checkpoint_save(struct yaffs_dev *dev) | ||
| 809 | { | ||
| 810 | |||
| 811 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 812 | "save entry: is_checkpointed %d", | ||
| 813 | dev->is_checkpointed); | ||
| 814 | |||
| 815 | yaffs_verify_objects(dev); | ||
| 816 | yaffs_verify_blocks(dev); | ||
| 817 | yaffs_verify_free_chunks(dev); | ||
| 818 | |||
| 819 | if (!dev->is_checkpointed) { | ||
| 820 | yaffs2_checkpt_invalidate(dev); | ||
| 821 | yaffs2_wr_checkpt_data(dev); | ||
| 822 | } | ||
| 823 | |||
| 824 | yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT, | ||
| 825 | "save exit: is_checkpointed %d", | ||
| 826 | dev->is_checkpointed); | ||
| 827 | |||
| 828 | return dev->is_checkpointed; | ||
| 829 | } | ||
| 830 | |||
| 831 | int yaffs2_checkpt_restore(struct yaffs_dev *dev) | ||
| 832 | { | ||
| 833 | int retval; | ||
| 834 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 835 | "restore entry: is_checkpointed %d", | ||
| 836 | dev->is_checkpointed); | ||
| 837 | |||
| 838 | retval = yaffs2_rd_checkpt_data(dev); | ||
| 839 | |||
| 840 | if (dev->is_checkpointed) { | ||
| 841 | yaffs_verify_objects(dev); | ||
| 842 | yaffs_verify_blocks(dev); | ||
| 843 | yaffs_verify_free_chunks(dev); | ||
| 844 | } | ||
| 845 | |||
| 846 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, | ||
| 847 | "restore exit: is_checkpointed %d", | ||
| 848 | dev->is_checkpointed); | ||
| 849 | |||
| 850 | return retval; | ||
| 851 | } | ||
| 852 | |||
| 853 | int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size) | ||
| 854 | { | ||
| 855 | /* if new_size > old_file_size. | ||
| 856 | * We're going to be writing a hole. | ||
| 857 | * If the hole is small then write zeros otherwise write a start of hole marker. | ||
| 858 | */ | ||
| 859 | |||
| 860 | loff_t old_file_size; | ||
| 861 | int increase; | ||
| 862 | int small_hole; | ||
| 863 | int result = YAFFS_OK; | ||
| 864 | struct yaffs_dev *dev = NULL; | ||
| 865 | |||
| 866 | u8 *local_buffer = NULL; | ||
| 867 | |||
| 868 | int small_increase_ok = 0; | ||
| 869 | |||
| 870 | if (!obj) | ||
| 871 | return YAFFS_FAIL; | ||
| 872 | |||
| 873 | if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE) | ||
| 874 | return YAFFS_FAIL; | ||
| 875 | |||
| 876 | dev = obj->my_dev; | ||
| 877 | |||
| 878 | /* Bail out if not yaffs2 mode */ | ||
| 879 | if (!dev->param.is_yaffs2) | ||
| 880 | return YAFFS_OK; | ||
| 881 | |||
| 882 | old_file_size = obj->variant.file_variant.file_size; | ||
| 883 | |||
| 884 | if (new_size <= old_file_size) | ||
| 885 | return YAFFS_OK; | ||
| 886 | |||
| 887 | increase = new_size - old_file_size; | ||
| 888 | |||
| 889 | if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk && | ||
| 890 | yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1)) | ||
| 891 | small_hole = 1; | ||
| 892 | else | ||
| 893 | small_hole = 0; | ||
| 894 | |||
| 895 | if (small_hole) | ||
| 896 | local_buffer = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 897 | |||
| 898 | if (local_buffer) { | ||
| 899 | /* fill hole with zero bytes */ | ||
| 900 | int pos = old_file_size; | ||
| 901 | int this_write; | ||
| 902 | int written; | ||
| 903 | memset(local_buffer, 0, dev->data_bytes_per_chunk); | ||
| 904 | small_increase_ok = 1; | ||
| 905 | |||
| 906 | while (increase > 0 && small_increase_ok) { | ||
| 907 | this_write = increase; | ||
| 908 | if (this_write > dev->data_bytes_per_chunk) | ||
| 909 | this_write = dev->data_bytes_per_chunk; | ||
| 910 | written = | ||
| 911 | yaffs_do_file_wr(obj, local_buffer, pos, this_write, | ||
| 912 | 0); | ||
| 913 | if (written == this_write) { | ||
| 914 | pos += this_write; | ||
| 915 | increase -= this_write; | ||
| 916 | } else { | ||
| 917 | small_increase_ok = 0; | ||
| 918 | } | ||
| 919 | } | ||
| 920 | |||
| 921 | yaffs_release_temp_buffer(dev, local_buffer, __LINE__); | ||
| 922 | |||
| 923 | /* If we were out of space then reverse any chunks we've added */ | ||
| 924 | if (!small_increase_ok) | ||
| 925 | yaffs_resize_file_down(obj, old_file_size); | ||
| 926 | } | ||
| 927 | |||
| 928 | if (!small_increase_ok && | ||
| 929 | obj->parent && | ||
| 930 | obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED && | ||
| 931 | obj->parent->obj_id != YAFFS_OBJECTID_DELETED) { | ||
| 932 | /* Write a hole start header with the old file size */ | ||
| 933 | yaffs_update_oh(obj, NULL, 0, 1, 0, NULL); | ||
| 934 | } | ||
| 935 | |||
| 936 | return result; | ||
| 937 | |||
| 938 | } | ||
| 939 | |||
| 940 | struct yaffs_block_index { | ||
| 941 | int seq; | ||
| 942 | int block; | ||
| 943 | }; | ||
| 944 | |||
| 945 | static int yaffs2_ybicmp(const void *a, const void *b) | ||
| 946 | { | ||
| 947 | int aseq = ((struct yaffs_block_index *)a)->seq; | ||
| 948 | int bseq = ((struct yaffs_block_index *)b)->seq; | ||
| 949 | int ablock = ((struct yaffs_block_index *)a)->block; | ||
| 950 | int bblock = ((struct yaffs_block_index *)b)->block; | ||
| 951 | if (aseq == bseq) | ||
| 952 | return ablock - bblock; | ||
| 953 | else | ||
| 954 | return aseq - bseq; | ||
| 955 | } | ||
| 956 | |||
| 957 | int yaffs2_scan_backwards(struct yaffs_dev *dev) | ||
| 958 | { | ||
| 959 | struct yaffs_ext_tags tags; | ||
| 960 | int blk; | ||
| 961 | int block_iter; | ||
| 962 | int start_iter; | ||
| 963 | int end_iter; | ||
| 964 | int n_to_scan = 0; | ||
| 965 | |||
| 966 | int chunk; | ||
| 967 | int result; | ||
| 968 | int c; | ||
| 969 | int deleted; | ||
| 970 | enum yaffs_block_state state; | ||
| 971 | struct yaffs_obj *hard_list = NULL; | ||
| 972 | struct yaffs_block_info *bi; | ||
| 973 | u32 seq_number; | ||
| 974 | struct yaffs_obj_hdr *oh; | ||
| 975 | struct yaffs_obj *in; | ||
| 976 | struct yaffs_obj *parent; | ||
| 977 | int n_blocks = dev->internal_end_block - dev->internal_start_block + 1; | ||
| 978 | int is_unlinked; | ||
| 979 | u8 *chunk_data; | ||
| 980 | |||
| 981 | int file_size; | ||
| 982 | int is_shrink; | ||
| 983 | int found_chunks; | ||
| 984 | int equiv_id; | ||
| 985 | int alloc_failed = 0; | ||
| 986 | |||
| 987 | struct yaffs_block_index *block_index = NULL; | ||
| 988 | int alt_block_index = 0; | ||
| 989 | |||
| 990 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 991 | "yaffs2_scan_backwards starts intstartblk %d intendblk %d...", | ||
| 992 | dev->internal_start_block, dev->internal_end_block); | ||
| 993 | |||
| 994 | dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER; | ||
| 995 | |||
| 996 | block_index = kmalloc(n_blocks * sizeof(struct yaffs_block_index), | ||
| 997 | GFP_NOFS); | ||
| 998 | |||
| 999 | if (!block_index) { | ||
| 1000 | block_index = | ||
| 1001 | vmalloc(n_blocks * sizeof(struct yaffs_block_index)); | ||
| 1002 | alt_block_index = 1; | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | if (!block_index) { | ||
| 1006 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 1007 | "yaffs2_scan_backwards() could not allocate block index!" | ||
| 1008 | ); | ||
| 1009 | return YAFFS_FAIL; | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | dev->blocks_in_checkpt = 0; | ||
| 1013 | |||
| 1014 | chunk_data = yaffs_get_temp_buffer(dev, __LINE__); | ||
| 1015 | |||
| 1016 | /* Scan all the blocks to determine their state */ | ||
| 1017 | bi = dev->block_info; | ||
| 1018 | for (blk = dev->internal_start_block; blk <= dev->internal_end_block; | ||
| 1019 | blk++) { | ||
| 1020 | yaffs_clear_chunk_bits(dev, blk); | ||
| 1021 | bi->pages_in_use = 0; | ||
| 1022 | bi->soft_del_pages = 0; | ||
| 1023 | |||
| 1024 | yaffs_query_init_block_state(dev, blk, &state, &seq_number); | ||
| 1025 | |||
| 1026 | bi->block_state = state; | ||
| 1027 | bi->seq_number = seq_number; | ||
| 1028 | |||
| 1029 | if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) | ||
| 1030 | bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT; | ||
| 1031 | if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK) | ||
| 1032 | bi->block_state = state = YAFFS_BLOCK_STATE_DEAD; | ||
| 1033 | |||
| 1034 | yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, | ||
| 1035 | "Block scanning block %d state %d seq %d", | ||
| 1036 | blk, state, seq_number); | ||
| 1037 | |||
| 1038 | if (state == YAFFS_BLOCK_STATE_CHECKPOINT) { | ||
| 1039 | dev->blocks_in_checkpt++; | ||
| 1040 | |||
| 1041 | } else if (state == YAFFS_BLOCK_STATE_DEAD) { | ||
| 1042 | yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, | ||
| 1043 | "block %d is bad", blk); | ||
| 1044 | } else if (state == YAFFS_BLOCK_STATE_EMPTY) { | ||
| 1045 | yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty "); | ||
| 1046 | dev->n_erased_blocks++; | ||
| 1047 | dev->n_free_chunks += dev->param.chunks_per_block; | ||
| 1048 | } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { | ||
| 1049 | |||
| 1050 | /* Determine the highest sequence number */ | ||
| 1051 | if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER && | ||
| 1052 | seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) { | ||
| 1053 | |||
| 1054 | block_index[n_to_scan].seq = seq_number; | ||
| 1055 | block_index[n_to_scan].block = blk; | ||
| 1056 | |||
| 1057 | n_to_scan++; | ||
| 1058 | |||
| 1059 | if (seq_number >= dev->seq_number) | ||
| 1060 | dev->seq_number = seq_number; | ||
| 1061 | } else { | ||
| 1062 | /* TODO: Nasty sequence number! */ | ||
| 1063 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 1064 | "Block scanning block %d has bad sequence number %d", | ||
| 1065 | blk, seq_number); | ||
| 1066 | |||
| 1067 | } | ||
| 1068 | } | ||
| 1069 | bi++; | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan); | ||
| 1073 | |||
| 1074 | cond_resched(); | ||
| 1075 | |||
| 1076 | /* Sort the blocks by sequence number */ | ||
| 1077 | sort(block_index, n_to_scan, sizeof(struct yaffs_block_index), | ||
| 1078 | yaffs2_ybicmp, NULL); | ||
| 1079 | |||
| 1080 | cond_resched(); | ||
| 1081 | |||
| 1082 | yaffs_trace(YAFFS_TRACE_SCAN, "...done"); | ||
| 1083 | |||
| 1084 | /* Now scan the blocks looking at the data. */ | ||
| 1085 | start_iter = 0; | ||
| 1086 | end_iter = n_to_scan - 1; | ||
| 1087 | yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan); | ||
| 1088 | |||
| 1089 | /* For each block.... backwards */ | ||
| 1090 | for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter; | ||
| 1091 | block_iter--) { | ||
| 1092 | /* Cooperative multitasking! This loop can run for so | ||
| 1093 | long that watchdog timers expire. */ | ||
| 1094 | cond_resched(); | ||
| 1095 | |||
| 1096 | /* get the block to scan in the correct order */ | ||
| 1097 | blk = block_index[block_iter].block; | ||
| 1098 | |||
| 1099 | bi = yaffs_get_block_info(dev, blk); | ||
| 1100 | |||
| 1101 | state = bi->block_state; | ||
| 1102 | |||
| 1103 | deleted = 0; | ||
| 1104 | |||
| 1105 | /* For each chunk in each block that needs scanning.... */ | ||
| 1106 | found_chunks = 0; | ||
| 1107 | for (c = dev->param.chunks_per_block - 1; | ||
| 1108 | !alloc_failed && c >= 0 && | ||
| 1109 | (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || | ||
| 1110 | state == YAFFS_BLOCK_STATE_ALLOCATING); c--) { | ||
| 1111 | /* Scan backwards... | ||
| 1112 | * Read the tags and decide what to do | ||
| 1113 | */ | ||
| 1114 | |||
| 1115 | chunk = blk * dev->param.chunks_per_block + c; | ||
| 1116 | |||
| 1117 | result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, | ||
| 1118 | &tags); | ||
| 1119 | |||
| 1120 | /* Let's have a good look at this chunk... */ | ||
| 1121 | |||
| 1122 | if (!tags.chunk_used) { | ||
| 1123 | /* An unassigned chunk in the block. | ||
| 1124 | * If there are used chunks after this one, then | ||
| 1125 | * it is a chunk that was skipped due to failing the erased | ||
| 1126 | * check. Just skip it so that it can be deleted. | ||
| 1127 | * But, more typically, We get here when this is an unallocated | ||
| 1128 | * chunk and his means that either the block is empty or | ||
| 1129 | * this is the one being allocated from | ||
| 1130 | */ | ||
| 1131 | |||
| 1132 | if (found_chunks) { | ||
| 1133 | /* This is a chunk that was skipped due to failing the erased check */ | ||
| 1134 | } else if (c == 0) { | ||
| 1135 | /* We're looking at the first chunk in the block so the block is unused */ | ||
| 1136 | state = YAFFS_BLOCK_STATE_EMPTY; | ||
| 1137 | dev->n_erased_blocks++; | ||
| 1138 | } else { | ||
| 1139 | if (state == | ||
| 1140 | YAFFS_BLOCK_STATE_NEEDS_SCANNING | ||
| 1141 | || state == | ||
| 1142 | YAFFS_BLOCK_STATE_ALLOCATING) { | ||
| 1143 | if (dev->seq_number == | ||
| 1144 | bi->seq_number) { | ||
| 1145 | /* this is the block being allocated from */ | ||
| 1146 | |||
| 1147 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 1148 | " Allocating from %d %d", | ||
| 1149 | blk, c); | ||
| 1150 | |||
| 1151 | state = | ||
| 1152 | YAFFS_BLOCK_STATE_ALLOCATING; | ||
| 1153 | dev->alloc_block = blk; | ||
| 1154 | dev->alloc_page = c; | ||
| 1155 | dev-> | ||
| 1156 | alloc_block_finder = | ||
| 1157 | blk; | ||
| 1158 | } else { | ||
| 1159 | /* This is a partially written block that is not | ||
| 1160 | * the current allocation block. | ||
| 1161 | */ | ||
| 1162 | |||
| 1163 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 1164 | "Partially written block %d detected", | ||
| 1165 | blk); | ||
| 1166 | } | ||
| 1167 | } | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | dev->n_free_chunks++; | ||
| 1171 | |||
| 1172 | } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) { | ||
| 1173 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 1174 | " Unfixed ECC in chunk(%d:%d), chunk ignored", | ||
| 1175 | blk, c); | ||
| 1176 | |||
| 1177 | dev->n_free_chunks++; | ||
| 1178 | |||
| 1179 | } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID || | ||
| 1180 | tags.chunk_id > YAFFS_MAX_CHUNK_ID || | ||
| 1181 | (tags.chunk_id > 0 | ||
| 1182 | && tags.n_bytes > dev->data_bytes_per_chunk) | ||
| 1183 | || tags.seq_number != bi->seq_number) { | ||
| 1184 | yaffs_trace(YAFFS_TRACE_SCAN, | ||
| 1185 | "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored", | ||
| 1186 | blk, c, tags.obj_id, | ||
| 1187 | tags.chunk_id, tags.n_bytes); | ||
| 1188 | |||
| 1189 | dev->n_free_chunks++; | ||
| 1190 | |||
| 1191 | } else if (tags.chunk_id > 0) { | ||
| 1192 | /* chunk_id > 0 so it is a data chunk... */ | ||
| 1193 | unsigned int endpos; | ||
| 1194 | u32 chunk_base = | ||
| 1195 | (tags.chunk_id - | ||
| 1196 | 1) * dev->data_bytes_per_chunk; | ||
| 1197 | |||
| 1198 | found_chunks = 1; | ||
| 1199 | |||
| 1200 | yaffs_set_chunk_bit(dev, blk, c); | ||
| 1201 | bi->pages_in_use++; | ||
| 1202 | |||
| 1203 | in = yaffs_find_or_create_by_number(dev, | ||
| 1204 | tags.obj_id, | ||
| 1205 | YAFFS_OBJECT_TYPE_FILE); | ||
| 1206 | if (!in) { | ||
| 1207 | /* Out of memory */ | ||
| 1208 | alloc_failed = 1; | ||
| 1209 | } | ||
| 1210 | |||
| 1211 | if (in && | ||
| 1212 | in->variant_type == YAFFS_OBJECT_TYPE_FILE | ||
| 1213 | && chunk_base < | ||
| 1214 | in->variant.file_variant.shrink_size) { | ||
| 1215 | /* This has not been invalidated by a resize */ | ||
| 1216 | if (!yaffs_put_chunk_in_file | ||
| 1217 | (in, tags.chunk_id, chunk, -1)) { | ||
| 1218 | alloc_failed = 1; | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | /* File size is calculated by looking at the data chunks if we have not | ||
| 1222 | * seen an object header yet. Stop this practice once we find an object header. | ||
| 1223 | */ | ||
| 1224 | endpos = chunk_base + tags.n_bytes; | ||
| 1225 | |||
| 1226 | if (!in->valid && /* have not got an object header yet */ | ||
| 1227 | in->variant.file_variant. | ||
| 1228 | scanned_size < endpos) { | ||
| 1229 | in->variant.file_variant. | ||
| 1230 | scanned_size = endpos; | ||
| 1231 | in->variant.file_variant. | ||
| 1232 | file_size = endpos; | ||
| 1233 | } | ||
| 1234 | |||
| 1235 | } else if (in) { | ||
| 1236 | /* This chunk has been invalidated by a resize, or a past file deletion | ||
| 1237 | * so delete the chunk*/ | ||
| 1238 | yaffs_chunk_del(dev, chunk, 1, | ||
| 1239 | __LINE__); | ||
| 1240 | |||
| 1241 | } | ||
| 1242 | } else { | ||
| 1243 | /* chunk_id == 0, so it is an ObjectHeader. | ||
| 1244 | * Thus, we read in the object header and make the object | ||
| 1245 | */ | ||
| 1246 | found_chunks = 1; | ||
| 1247 | |||
| 1248 | yaffs_set_chunk_bit(dev, blk, c); | ||
| 1249 | bi->pages_in_use++; | ||
| 1250 | |||
| 1251 | oh = NULL; | ||
| 1252 | in = NULL; | ||
| 1253 | |||
| 1254 | if (tags.extra_available) { | ||
| 1255 | in = yaffs_find_or_create_by_number(dev, | ||
| 1256 | tags. | ||
| 1257 | obj_id, | ||
| 1258 | tags. | ||
| 1259 | extra_obj_type); | ||
| 1260 | if (!in) | ||
| 1261 | alloc_failed = 1; | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | if (!in || | ||
| 1265 | (!in->valid && dev->param.disable_lazy_load) | ||
| 1266 | || tags.extra_shadows || (!in->valid | ||
| 1267 | && (tags.obj_id == | ||
| 1268 | YAFFS_OBJECTID_ROOT | ||
| 1269 | || tags. | ||
| 1270 | obj_id == | ||
| 1271 | YAFFS_OBJECTID_LOSTNFOUND))) | ||
| 1272 | { | ||
| 1273 | |||
| 1274 | /* If we don't have valid info then we need to read the chunk | ||
| 1275 | * TODO In future we can probably defer reading the chunk and | ||
| 1276 | * living with invalid data until needed. | ||
| 1277 | */ | ||
| 1278 | |||
| 1279 | result = yaffs_rd_chunk_tags_nand(dev, | ||
| 1280 | chunk, | ||
| 1281 | chunk_data, | ||
| 1282 | NULL); | ||
| 1283 | |||
| 1284 | oh = (struct yaffs_obj_hdr *)chunk_data; | ||
| 1285 | |||
| 1286 | if (dev->param.inband_tags) { | ||
| 1287 | /* Fix up the header if they got corrupted by inband tags */ | ||
| 1288 | oh->shadows_obj = | ||
| 1289 | oh->inband_shadowed_obj_id; | ||
| 1290 | oh->is_shrink = | ||
| 1291 | oh->inband_is_shrink; | ||
| 1292 | } | ||
| 1293 | |||
| 1294 | if (!in) { | ||
| 1295 | in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type); | ||
| 1296 | if (!in) | ||
| 1297 | alloc_failed = 1; | ||
| 1298 | } | ||
| 1299 | |||
| 1300 | } | ||
| 1301 | |||
| 1302 | if (!in) { | ||
| 1303 | /* TODO Hoosterman we have a problem! */ | ||
| 1304 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 1305 | "yaffs tragedy: Could not make object for object %d at chunk %d during scan", | ||
| 1306 | tags.obj_id, chunk); | ||
| 1307 | continue; | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | if (in->valid) { | ||
| 1311 | /* We have already filled this one. | ||
| 1312 | * We have a duplicate that will be discarded, but | ||
| 1313 | * we first have to suck out resize info if it is a file. | ||
| 1314 | */ | ||
| 1315 | |||
| 1316 | if ((in->variant_type == | ||
| 1317 | YAFFS_OBJECT_TYPE_FILE) && ((oh | ||
| 1318 | && | ||
| 1319 | oh-> | ||
| 1320 | type | ||
| 1321 | == | ||
| 1322 | YAFFS_OBJECT_TYPE_FILE) | ||
| 1323 | || | ||
| 1324 | (tags. | ||
| 1325 | extra_available | ||
| 1326 | && | ||
| 1327 | tags. | ||
| 1328 | extra_obj_type | ||
| 1329 | == | ||
| 1330 | YAFFS_OBJECT_TYPE_FILE))) | ||
| 1331 | { | ||
| 1332 | u32 this_size = | ||
| 1333 | (oh) ? oh-> | ||
| 1334 | file_size : | ||
| 1335 | tags.extra_length; | ||
| 1336 | u32 parent_obj_id = | ||
| 1337 | (oh) ? oh->parent_obj_id : | ||
| 1338 | tags.extra_parent_id; | ||
| 1339 | |||
| 1340 | is_shrink = | ||
| 1341 | (oh) ? oh-> | ||
| 1342 | is_shrink : | ||
| 1343 | tags.extra_is_shrink; | ||
| 1344 | |||
| 1345 | /* If it is deleted (unlinked at start also means deleted) | ||
| 1346 | * we treat the file size as being zeroed at this point. | ||
| 1347 | */ | ||
| 1348 | if (parent_obj_id == | ||
| 1349 | YAFFS_OBJECTID_DELETED | ||
| 1350 | || parent_obj_id == | ||
| 1351 | YAFFS_OBJECTID_UNLINKED) { | ||
| 1352 | this_size = 0; | ||
| 1353 | is_shrink = 1; | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | if (is_shrink | ||
| 1357 | && in->variant.file_variant. | ||
| 1358 | shrink_size > this_size) | ||
| 1359 | in->variant. | ||
| 1360 | file_variant. | ||
| 1361 | shrink_size = | ||
| 1362 | this_size; | ||
| 1363 | |||
| 1364 | if (is_shrink) | ||
| 1365 | bi->has_shrink_hdr = 1; | ||
| 1366 | |||
| 1367 | } | ||
| 1368 | /* Use existing - destroy this one. */ | ||
| 1369 | yaffs_chunk_del(dev, chunk, 1, | ||
| 1370 | __LINE__); | ||
| 1371 | |||
| 1372 | } | ||
| 1373 | |||
| 1374 | if (!in->valid && in->variant_type != | ||
| 1375 | (oh ? oh->type : tags.extra_obj_type)) | ||
| 1376 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 1377 | "yaffs tragedy: Bad object type, %d != %d, for object %d at chunk %d during scan", | ||
| 1378 | oh ? | ||
| 1379 | oh->type : tags.extra_obj_type, | ||
| 1380 | in->variant_type, tags.obj_id, | ||
| 1381 | chunk); | ||
| 1382 | |||
| 1383 | if (!in->valid && | ||
| 1384 | (tags.obj_id == YAFFS_OBJECTID_ROOT || | ||
| 1385 | tags.obj_id == | ||
| 1386 | YAFFS_OBJECTID_LOSTNFOUND)) { | ||
| 1387 | /* We only load some info, don't fiddle with directory structure */ | ||
| 1388 | in->valid = 1; | ||
| 1389 | |||
| 1390 | if (oh) { | ||
| 1391 | |||
| 1392 | in->yst_mode = oh->yst_mode; | ||
| 1393 | yaffs_load_attribs(in, oh); | ||
| 1394 | in->lazy_loaded = 0; | ||
| 1395 | } else { | ||
| 1396 | in->lazy_loaded = 1; | ||
| 1397 | } | ||
| 1398 | in->hdr_chunk = chunk; | ||
| 1399 | |||
| 1400 | } else if (!in->valid) { | ||
| 1401 | /* we need to load this info */ | ||
| 1402 | |||
| 1403 | in->valid = 1; | ||
| 1404 | in->hdr_chunk = chunk; | ||
| 1405 | |||
| 1406 | if (oh) { | ||
| 1407 | in->variant_type = oh->type; | ||
| 1408 | |||
| 1409 | in->yst_mode = oh->yst_mode; | ||
| 1410 | yaffs_load_attribs(in, oh); | ||
| 1411 | |||
| 1412 | if (oh->shadows_obj > 0) | ||
| 1413 | yaffs_handle_shadowed_obj | ||
| 1414 | (dev, | ||
| 1415 | oh->shadows_obj, | ||
| 1416 | 1); | ||
| 1417 | |||
| 1418 | yaffs_set_obj_name_from_oh(in, | ||
| 1419 | oh); | ||
| 1420 | parent = | ||
| 1421 | yaffs_find_or_create_by_number | ||
| 1422 | (dev, oh->parent_obj_id, | ||
| 1423 | YAFFS_OBJECT_TYPE_DIRECTORY); | ||
| 1424 | |||
| 1425 | file_size = oh->file_size; | ||
| 1426 | is_shrink = oh->is_shrink; | ||
| 1427 | equiv_id = oh->equiv_id; | ||
| 1428 | |||
| 1429 | } else { | ||
| 1430 | in->variant_type = | ||
| 1431 | tags.extra_obj_type; | ||
| 1432 | parent = | ||
| 1433 | yaffs_find_or_create_by_number | ||
| 1434 | (dev, tags.extra_parent_id, | ||
| 1435 | YAFFS_OBJECT_TYPE_DIRECTORY); | ||
| 1436 | file_size = tags.extra_length; | ||
| 1437 | is_shrink = | ||
| 1438 | tags.extra_is_shrink; | ||
| 1439 | equiv_id = tags.extra_equiv_id; | ||
| 1440 | in->lazy_loaded = 1; | ||
| 1441 | |||
| 1442 | } | ||
| 1443 | in->dirty = 0; | ||
| 1444 | |||
| 1445 | if (!parent) | ||
| 1446 | alloc_failed = 1; | ||
| 1447 | |||
| 1448 | /* directory stuff... | ||
| 1449 | * hook up to parent | ||
| 1450 | */ | ||
| 1451 | |||
| 1452 | if (parent && parent->variant_type == | ||
| 1453 | YAFFS_OBJECT_TYPE_UNKNOWN) { | ||
| 1454 | /* Set up as a directory */ | ||
| 1455 | parent->variant_type = | ||
| 1456 | YAFFS_OBJECT_TYPE_DIRECTORY; | ||
| 1457 | INIT_LIST_HEAD(&parent-> | ||
| 1458 | variant.dir_variant.children); | ||
| 1459 | } else if (!parent | ||
| 1460 | || parent->variant_type != | ||
| 1461 | YAFFS_OBJECT_TYPE_DIRECTORY) { | ||
| 1462 | /* Hoosterman, another problem.... | ||
| 1463 | * We're trying to use a non-directory as a directory | ||
| 1464 | */ | ||
| 1465 | |||
| 1466 | yaffs_trace(YAFFS_TRACE_ERROR, | ||
| 1467 | "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." | ||
| 1468 | ); | ||
| 1469 | parent = dev->lost_n_found; | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | yaffs_add_obj_to_dir(parent, in); | ||
| 1473 | |||
| 1474 | is_unlinked = (parent == dev->del_dir) | ||
| 1475 | || (parent == dev->unlinked_dir); | ||
| 1476 | |||
| 1477 | if (is_shrink) { | ||
| 1478 | /* Mark the block as having a shrink header */ | ||
| 1479 | bi->has_shrink_hdr = 1; | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | /* Note re hardlinks. | ||
| 1483 | * Since we might scan a hardlink before its equivalent object is scanned | ||
| 1484 | * we put them all in a list. | ||
| 1485 | * After scanning is complete, we should have all the objects, so we run | ||
| 1486 | * through this list and fix up all the chains. | ||
| 1487 | */ | ||
| 1488 | |||
| 1489 | switch (in->variant_type) { | ||
| 1490 | case YAFFS_OBJECT_TYPE_UNKNOWN: | ||
| 1491 | /* Todo got a problem */ | ||
| 1492 | break; | ||
| 1493 | case YAFFS_OBJECT_TYPE_FILE: | ||
| 1494 | |||
| 1495 | if (in->variant. | ||
| 1496 | file_variant.scanned_size < | ||
| 1497 | file_size) { | ||
| 1498 | /* This covers the case where the file size is greater | ||
| 1499 | * than where the data is | ||
| 1500 | * This will happen if the file is resized to be larger | ||
| 1501 | * than its current data extents. | ||
| 1502 | */ | ||
| 1503 | in->variant. | ||
| 1504 | file_variant. | ||
| 1505 | file_size = | ||
| 1506 | file_size; | ||
| 1507 | in->variant. | ||
| 1508 | file_variant. | ||
| 1509 | scanned_size = | ||
| 1510 | file_size; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | if (in->variant.file_variant. | ||
| 1514 | shrink_size > file_size) | ||
| 1515 | in->variant. | ||
| 1516 | file_variant. | ||
| 1517 | shrink_size = | ||
| 1518 | file_size; | ||
| 1519 | |||
| 1520 | break; | ||
| 1521 | case YAFFS_OBJECT_TYPE_HARDLINK: | ||
| 1522 | if (!is_unlinked) { | ||
| 1523 | in->variant. | ||
| 1524 | hardlink_variant. | ||
| 1525 | equiv_id = equiv_id; | ||
| 1526 | in->hard_links.next = | ||
| 1527 | (struct list_head *) | ||
| 1528 | hard_list; | ||
| 1529 | hard_list = in; | ||
| 1530 | } | ||
| 1531 | break; | ||
| 1532 | case YAFFS_OBJECT_TYPE_DIRECTORY: | ||
| 1533 | /* Do nothing */ | ||
| 1534 | break; | ||
| 1535 | case YAFFS_OBJECT_TYPE_SPECIAL: | ||
| 1536 | /* Do nothing */ | ||
| 1537 | break; | ||
| 1538 | case YAFFS_OBJECT_TYPE_SYMLINK: | ||
| 1539 | if (oh) { | ||
| 1540 | in->variant. | ||
| 1541 | symlink_variant. | ||
| 1542 | alias = | ||
| 1543 | yaffs_clone_str(oh-> | ||
| 1544 | alias); | ||
| 1545 | if (!in->variant. | ||
| 1546 | symlink_variant. | ||
| 1547 | alias) | ||
| 1548 | alloc_failed = | ||
| 1549 | 1; | ||
| 1550 | } | ||
| 1551 | break; | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | } | ||
| 1555 | |||
| 1556 | } | ||
| 1557 | |||
| 1558 | } /* End of scanning for each chunk */ | ||
| 1559 | |||
| 1560 | if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { | ||
| 1561 | /* If we got this far while scanning, then the block is fully allocated. */ | ||
| 1562 | state = YAFFS_BLOCK_STATE_FULL; | ||
| 1563 | } | ||
| 1564 | |||
| 1565 | bi->block_state = state; | ||
| 1566 | |||
| 1567 | /* Now let's see if it was dirty */ | ||
| 1568 | if (bi->pages_in_use == 0 && | ||
| 1569 | !bi->has_shrink_hdr && | ||
| 1570 | bi->block_state == YAFFS_BLOCK_STATE_FULL) { | ||
| 1571 | yaffs_block_became_dirty(dev, blk); | ||
| 1572 | } | ||
| 1573 | |||
| 1574 | } | ||
| 1575 | |||
| 1576 | yaffs_skip_rest_of_block(dev); | ||
| 1577 | |||
| 1578 | if (alt_block_index) | ||
| 1579 | vfree(block_index); | ||
| 1580 | else | ||
| 1581 | kfree(block_index); | ||
| 1582 | |||
| 1583 | /* Ok, we've done all the scanning. | ||
| 1584 | * Fix up the hard link chains. | ||
| 1585 | * We should now have scanned all the objects, now it's time to add these | ||
| 1586 | * hardlinks. | ||
| 1587 | */ | ||
| 1588 | yaffs_link_fixup(dev, hard_list); | ||
| 1589 | |||
| 1590 | yaffs_release_temp_buffer(dev, chunk_data, __LINE__); | ||
| 1591 | |||
| 1592 | if (alloc_failed) | ||
| 1593 | return YAFFS_FAIL; | ||
| 1594 | |||
| 1595 | yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends"); | ||
| 1596 | |||
| 1597 | return YAFFS_OK; | ||
| 1598 | } | ||
diff --git a/fs/yaffs2/yaffs_yaffs2.h b/fs/yaffs2/yaffs_yaffs2.h new file mode 100644 index 00000000000..e1a9287fc50 --- /dev/null +++ b/fs/yaffs2/yaffs_yaffs2.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YAFFS_YAFFS2_H__ | ||
| 17 | #define __YAFFS_YAFFS2_H__ | ||
| 18 | |||
| 19 | #include "yaffs_guts.h" | ||
| 20 | |||
| 21 | void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev); | ||
| 22 | void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev); | ||
| 23 | void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev, | ||
| 24 | struct yaffs_block_info *bi); | ||
| 25 | void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no, | ||
| 26 | struct yaffs_block_info *bi); | ||
| 27 | int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi); | ||
| 28 | u32 yaffs2_find_refresh_block(struct yaffs_dev *dev); | ||
| 29 | int yaffs2_checkpt_required(struct yaffs_dev *dev); | ||
| 30 | int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev); | ||
| 31 | |||
| 32 | void yaffs2_checkpt_invalidate(struct yaffs_dev *dev); | ||
| 33 | int yaffs2_checkpt_save(struct yaffs_dev *dev); | ||
| 34 | int yaffs2_checkpt_restore(struct yaffs_dev *dev); | ||
| 35 | |||
| 36 | int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size); | ||
| 37 | int yaffs2_scan_backwards(struct yaffs_dev *dev); | ||
| 38 | |||
| 39 | #endif | ||
diff --git a/fs/yaffs2/yportenv.h b/fs/yaffs2/yportenv.h new file mode 100644 index 00000000000..8183425448c --- /dev/null +++ b/fs/yaffs2/yportenv.h | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | /* | ||
| 2 | * YAFFS: Yet another Flash File System . A NAND-flash specific file system. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2002-2010 Aleph One Ltd. | ||
| 5 | * for Toby Churchill Ltd and Brightstar Engineering | ||
| 6 | * | ||
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify | ||
| 10 | * it under the terms of the GNU Lesser General Public License version 2.1 as | ||
| 11 | * published by the Free Software Foundation. | ||
| 12 | * | ||
| 13 | * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #ifndef __YPORTENV_LINUX_H__ | ||
| 17 | #define __YPORTENV_LINUX_H__ | ||
| 18 | |||
| 19 | #include <linux/version.h> | ||
| 20 | #include <linux/kernel.h> | ||
| 21 | #include <linux/mm.h> | ||
| 22 | #include <linux/sched.h> | ||
| 23 | #include <linux/string.h> | ||
| 24 | #include <linux/slab.h> | ||
| 25 | #include <linux/vmalloc.h> | ||
| 26 | #include <linux/xattr.h> | ||
| 27 | #include <linux/list.h> | ||
| 28 | #include <linux/types.h> | ||
| 29 | #include <linux/fs.h> | ||
| 30 | #include <linux/stat.h> | ||
| 31 | #include <linux/sort.h> | ||
| 32 | #include <linux/bitops.h> | ||
| 33 | |||
| 34 | #define YCHAR char | ||
| 35 | #define YUCHAR unsigned char | ||
| 36 | #define _Y(x) x | ||
| 37 | |||
| 38 | #define YAFFS_LOSTNFOUND_NAME "lost+found" | ||
| 39 | #define YAFFS_LOSTNFOUND_PREFIX "obj" | ||
| 40 | |||
| 41 | |||
| 42 | #define YAFFS_ROOT_MODE 0755 | ||
| 43 | #define YAFFS_LOSTNFOUND_MODE 0700 | ||
| 44 | |||
| 45 | #define Y_CURRENT_TIME CURRENT_TIME.tv_sec | ||
| 46 | #define Y_TIME_CONVERT(x) (x).tv_sec | ||
| 47 | |||
| 48 | #define compile_time_assertion(assertion) \ | ||
| 49 | ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; }) | ||
| 50 | |||
| 51 | |||
| 52 | #ifndef Y_DUMP_STACK | ||
| 53 | #define Y_DUMP_STACK() dump_stack() | ||
| 54 | #endif | ||
| 55 | |||
| 56 | #define yaffs_trace(msk, fmt, ...) do { \ | ||
| 57 | if(yaffs_trace_mask & (msk)) \ | ||
| 58 | printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__); \ | ||
| 59 | } while(0) | ||
| 60 | |||
| 61 | #ifndef YBUG | ||
| 62 | #define YBUG() do {\ | ||
| 63 | yaffs_trace(YAFFS_TRACE_BUG,\ | ||
| 64 | "bug " __FILE__ " %d",\ | ||
| 65 | __LINE__);\ | ||
| 66 | Y_DUMP_STACK();\ | ||
| 67 | } while (0) | ||
| 68 | #endif | ||
| 69 | |||
| 70 | #endif | ||
