William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 3 | * |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4 | * Copyright (C) 2002-2011 Aleph One Ltd. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 14 | #include "yportenv.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 15 | #include "yaffs_trace.h" |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <dm/devres.h> |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 17 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 18 | #include "yaffs_guts.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 19 | #include "yaffs_getblockinfo.h" |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 20 | #include "yaffs_tagscompat.h" |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 21 | #include "yaffs_nand.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 22 | #include "yaffs_yaffs1.h" |
| 23 | #include "yaffs_yaffs2.h" |
| 24 | #include "yaffs_bitmap.h" |
| 25 | #include "yaffs_verify.h" |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 26 | #include "yaffs_nand.h" |
| 27 | #include "yaffs_packedtags2.h" |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 28 | #include "yaffs_nameval.h" |
| 29 | #include "yaffs_allocator.h" |
| 30 | #include "yaffs_attribs.h" |
| 31 | #include "yaffs_summary.h" |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 32 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 33 | /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */ |
| 34 | #define YAFFS_GC_GOOD_ENOUGH 2 |
| 35 | #define YAFFS_GC_PASSIVE_THRESHOLD 4 |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 36 | |
| 37 | #include "yaffs_ecc.h" |
| 38 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 39 | /* Forward declarations */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 40 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 41 | static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, |
| 42 | const u8 *buffer, int n_bytes, int use_reserve); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 43 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 44 | /* Function to calculate chunk and offset */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 45 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 46 | void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr, |
| 47 | int *chunk_out, u32 *offset_out) |
| 48 | { |
| 49 | int chunk; |
| 50 | u32 offset; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 51 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 52 | chunk = (u32) (addr >> dev->chunk_shift); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 53 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 54 | if (dev->chunk_div == 1) { |
| 55 | /* easy power of 2 case */ |
| 56 | offset = (u32) (addr & dev->chunk_mask); |
| 57 | } else { |
| 58 | /* Non power-of-2 case */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 59 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 60 | loff_t chunk_base; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 61 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 62 | chunk /= dev->chunk_div; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 63 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 64 | chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk; |
| 65 | offset = (u32) (addr - chunk_base); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 66 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 67 | |
| 68 | *chunk_out = chunk; |
| 69 | *offset_out = offset; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 72 | /* Function to return the number of shifts for a power of 2 greater than or |
| 73 | * equal to the given number |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 74 | * Note we don't try to cater for all possible numbers and this does not have to |
| 75 | * be hellishly efficient. |
| 76 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 77 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 78 | static inline u32 calc_shifts_ceiling(u32 x) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 79 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 80 | int extra_bits; |
| 81 | int shifts; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 82 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 83 | shifts = extra_bits = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 84 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 85 | while (x > 1) { |
| 86 | if (x & 1) |
| 87 | extra_bits++; |
| 88 | x >>= 1; |
| 89 | shifts++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 92 | if (extra_bits) |
| 93 | shifts++; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 94 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 95 | return shifts; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* Function to return the number of shifts to get a 1 in bit 0 |
| 99 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 100 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 101 | static inline u32 calc_shifts(u32 x) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 102 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 103 | u32 shifts; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 104 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 105 | shifts = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 106 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 107 | if (!x) |
| 108 | return 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 109 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 110 | while (!(x & 1)) { |
| 111 | x >>= 1; |
| 112 | shifts++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 113 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 114 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 115 | return shifts; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 118 | /* |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 119 | * Temporary buffer manipulations. |
| 120 | */ |
| 121 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 122 | static int yaffs_init_tmp_buffers(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 123 | { |
| 124 | int i; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 125 | u8 *buf = (u8 *) 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 126 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 127 | memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer)); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 128 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 129 | for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 130 | dev->temp_buffer[i].in_use = 0; |
| 131 | buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); |
| 132 | dev->temp_buffer[i].buffer = buf; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 133 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 134 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 135 | return buf ? YAFFS_OK : YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 138 | u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 139 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 140 | int i; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 141 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 142 | dev->temp_in_use++; |
| 143 | if (dev->temp_in_use > dev->max_temp) |
| 144 | dev->max_temp = dev->temp_in_use; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 145 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 146 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 147 | if (dev->temp_buffer[i].in_use == 0) { |
| 148 | dev->temp_buffer[i].in_use = 1; |
| 149 | return dev->temp_buffer[i].buffer; |
| 150 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 151 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 152 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 153 | yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 154 | /* |
| 155 | * If we got here then we have to allocate an unmanaged one |
| 156 | * This is not good. |
| 157 | */ |
| 158 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 159 | dev->unmanaged_buffer_allocs++; |
| 160 | return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 161 | |
| 162 | } |
| 163 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 164 | void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 165 | { |
| 166 | int i; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 167 | |
| 168 | dev->temp_in_use--; |
| 169 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 170 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 171 | if (dev->temp_buffer[i].buffer == buffer) { |
| 172 | dev->temp_buffer[i].in_use = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (buffer) { |
| 178 | /* assume it is an unmanaged one. */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 179 | yaffs_trace(YAFFS_TRACE_BUFFERS, |
| 180 | "Releasing unmanaged temp buffer"); |
| 181 | kfree(buffer); |
| 182 | dev->unmanaged_buffer_deallocs++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Determine if we have a managed buffer. |
| 189 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 190 | int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 191 | { |
| 192 | int i; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 193 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 194 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 195 | if (dev->temp_buffer[i].buffer == buffer) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 196 | return 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 199 | for (i = 0; i < dev->param.n_caches; i++) { |
| 200 | if (dev->cache[i].data == buffer) |
| 201 | return 1; |
| 202 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 203 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 204 | if (buffer == dev->checkpt_buffer) |
| 205 | return 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 206 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 207 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 208 | "yaffs: unmaged buffer detected."); |
| 209 | return 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 210 | } |
| 211 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 212 | /* |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 213 | * Functions for robustisizing TODO |
| 214 | * |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 215 | */ |
| 216 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 217 | static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk, |
| 218 | const u8 *data, |
| 219 | const struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 220 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 223 | static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk, |
| 224 | const struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 225 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 228 | void yaffs_handle_chunk_error(struct yaffs_dev *dev, |
| 229 | struct yaffs_block_info *bi) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 230 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 231 | if (!bi->gc_prioritise) { |
| 232 | bi->gc_prioritise = 1; |
| 233 | dev->has_pending_prioritised_gc = 1; |
| 234 | bi->chunk_error_strikes++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 235 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 236 | if (bi->chunk_error_strikes > 3) { |
| 237 | bi->needs_retiring = 1; /* Too many stikes, so retire */ |
| 238 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 239 | "yaffs: Block struck out"); |
| 240 | |
| 241 | } |
| 242 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 245 | static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk, |
| 246 | int erased_ok) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 247 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 248 | int flash_block = nand_chunk / dev->param.chunks_per_block; |
| 249 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 250 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 251 | yaffs_handle_chunk_error(dev, bi); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 252 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 253 | if (erased_ok) { |
| 254 | /* Was an actual write failure, |
| 255 | * so mark the block for retirement.*/ |
| 256 | bi->needs_retiring = 1; |
| 257 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, |
| 258 | "**>> Block %d needs retiring", flash_block); |
| 259 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 260 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 261 | /* Delete the chunk */ |
| 262 | yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); |
| 263 | yaffs_skip_rest_of_block(dev); |
| 264 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 265 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 266 | /* |
| 267 | * Verification code |
| 268 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 269 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 270 | /* |
| 271 | * Simple hash function. Needs to have a reasonable spread |
| 272 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 273 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 274 | static inline int yaffs_hash_fn(int n) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 275 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 276 | if (n < 0) |
| 277 | n = -n; |
| 278 | return n % YAFFS_NOBJECT_BUCKETS; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 281 | /* |
| 282 | * Access functions to useful fake objects. |
| 283 | * Note that root might have a presence in NAND if permissions are set. |
| 284 | */ |
| 285 | |
| 286 | struct yaffs_obj *yaffs_root(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 287 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 288 | return dev->root_dir; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 291 | struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 292 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 293 | return dev->lost_n_found; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 296 | /* |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 297 | * Erased NAND checking functions |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 298 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 299 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 300 | int yaffs_check_ff(u8 *buffer, int n_bytes) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 301 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 302 | /* Horrible, slow implementation */ |
| 303 | while (n_bytes--) { |
| 304 | if (*buffer != 0xff) |
| 305 | return 0; |
| 306 | buffer++; |
| 307 | } |
| 308 | return 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 311 | static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 312 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 313 | int retval = YAFFS_OK; |
| 314 | u8 *data = yaffs_get_temp_buffer(dev); |
| 315 | struct yaffs_ext_tags tags; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 316 | |
Anatolij Gustschin | 514c65c | 2012-10-05 23:31:03 +0000 | [diff] [blame] | 317 | yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 318 | |
| 319 | if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR) |
| 320 | retval = YAFFS_FAIL; |
| 321 | |
| 322 | if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || |
| 323 | tags.chunk_used) { |
| 324 | yaffs_trace(YAFFS_TRACE_NANDACCESS, |
| 325 | "Chunk %d not erased", nand_chunk); |
| 326 | retval = YAFFS_FAIL; |
| 327 | } |
| 328 | |
| 329 | yaffs_release_temp_buffer(dev, data); |
| 330 | |
| 331 | return retval; |
| 332 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 335 | static int yaffs_verify_chunk_written(struct yaffs_dev *dev, |
| 336 | int nand_chunk, |
| 337 | const u8 *data, |
| 338 | struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 339 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 340 | int retval = YAFFS_OK; |
| 341 | struct yaffs_ext_tags temp_tags; |
| 342 | u8 *buffer = yaffs_get_temp_buffer(dev); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 343 | |
Anatolij Gustschin | 514c65c | 2012-10-05 23:31:03 +0000 | [diff] [blame] | 344 | yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 345 | if (memcmp(buffer, data, dev->data_bytes_per_chunk) || |
| 346 | temp_tags.obj_id != tags->obj_id || |
| 347 | temp_tags.chunk_id != tags->chunk_id || |
| 348 | temp_tags.n_bytes != tags->n_bytes) |
| 349 | retval = YAFFS_FAIL; |
| 350 | |
| 351 | yaffs_release_temp_buffer(dev, buffer); |
| 352 | |
| 353 | return retval; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 356 | int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 357 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 358 | int reserved_chunks; |
| 359 | int reserved_blocks = dev->param.n_reserved_blocks; |
| 360 | int checkpt_blocks; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 361 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 362 | checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 363 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 364 | reserved_chunks = |
| 365 | (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 366 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 367 | return (dev->n_free_chunks > (reserved_chunks + n_chunks)); |
| 368 | } |
| 369 | |
| 370 | static int yaffs_find_alloc_block(struct yaffs_dev *dev) |
| 371 | { |
| 372 | int i; |
| 373 | struct yaffs_block_info *bi; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 374 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 375 | if (dev->n_erased_blocks < 1) { |
| 376 | /* Hoosterman we've got a problem. |
| 377 | * Can't get space to gc |
| 378 | */ |
| 379 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 380 | "yaffs tragedy: no more erased blocks"); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 381 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 382 | return -1; |
| 383 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 384 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 385 | /* Find an empty block. */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 386 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 387 | for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { |
| 388 | dev->alloc_block_finder++; |
| 389 | if (dev->alloc_block_finder < dev->internal_start_block |
| 390 | || dev->alloc_block_finder > dev->internal_end_block) { |
| 391 | dev->alloc_block_finder = dev->internal_start_block; |
| 392 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 393 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 394 | bi = yaffs_get_block_info(dev, dev->alloc_block_finder); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 395 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 396 | if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { |
| 397 | bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING; |
| 398 | dev->seq_number++; |
| 399 | bi->seq_number = dev->seq_number; |
| 400 | dev->n_erased_blocks--; |
| 401 | yaffs_trace(YAFFS_TRACE_ALLOCATE, |
| 402 | "Allocated block %d, seq %d, %d left" , |
| 403 | dev->alloc_block_finder, dev->seq_number, |
| 404 | dev->n_erased_blocks); |
| 405 | return dev->alloc_block_finder; |
| 406 | } |
| 407 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 408 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 409 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 410 | "yaffs tragedy: no more erased blocks, but there should have been %d", |
| 411 | dev->n_erased_blocks); |
| 412 | |
| 413 | return -1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 416 | static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver, |
| 417 | struct yaffs_block_info **block_ptr) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 418 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 419 | int ret_val; |
| 420 | struct yaffs_block_info *bi; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 421 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 422 | if (dev->alloc_block < 0) { |
| 423 | /* Get next block to allocate off */ |
| 424 | dev->alloc_block = yaffs_find_alloc_block(dev); |
| 425 | dev->alloc_page = 0; |
| 426 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 427 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 428 | if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) { |
| 429 | /* No space unless we're allowed to use the reserve. */ |
| 430 | return -1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 431 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 432 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 433 | if (dev->n_erased_blocks < dev->param.n_reserved_blocks |
| 434 | && dev->alloc_page == 0) |
| 435 | yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve"); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 436 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 437 | /* Next page please.... */ |
| 438 | if (dev->alloc_block >= 0) { |
| 439 | bi = yaffs_get_block_info(dev, dev->alloc_block); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 440 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 441 | ret_val = (dev->alloc_block * dev->param.chunks_per_block) + |
| 442 | dev->alloc_page; |
| 443 | bi->pages_in_use++; |
| 444 | yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 445 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 446 | dev->alloc_page++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 447 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 448 | dev->n_free_chunks--; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 449 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 450 | /* If the block is full set the state to full */ |
| 451 | if (dev->alloc_page >= dev->param.chunks_per_block) { |
| 452 | bi->block_state = YAFFS_BLOCK_STATE_FULL; |
| 453 | dev->alloc_block = -1; |
| 454 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 455 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 456 | if (block_ptr) |
| 457 | *block_ptr = bi; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 458 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 459 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 460 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 461 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 462 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 463 | "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 464 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 465 | return -1; |
| 466 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 467 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 468 | static int yaffs_get_erased_chunks(struct yaffs_dev *dev) |
| 469 | { |
| 470 | int n; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 471 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 472 | n = dev->n_erased_blocks * dev->param.chunks_per_block; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 473 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 474 | if (dev->alloc_block > 0) |
| 475 | n += (dev->param.chunks_per_block - dev->alloc_page); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 476 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 477 | return n; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 478 | |
| 479 | } |
| 480 | |
| 481 | /* |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 482 | * yaffs_skip_rest_of_block() skips over the rest of the allocation block |
| 483 | * if we don't want to write to it. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 484 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 485 | void yaffs_skip_rest_of_block(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 486 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 487 | struct yaffs_block_info *bi; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 488 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 489 | if (dev->alloc_block > 0) { |
| 490 | bi = yaffs_get_block_info(dev, dev->alloc_block); |
| 491 | if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) { |
| 492 | bi->block_state = YAFFS_BLOCK_STATE_FULL; |
| 493 | dev->alloc_block = -1; |
| 494 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 495 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 496 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 497 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 498 | static int yaffs_write_new_chunk(struct yaffs_dev *dev, |
| 499 | const u8 *data, |
| 500 | struct yaffs_ext_tags *tags, int use_reserver) |
| 501 | { |
| 502 | int attempts = 0; |
| 503 | int write_ok = 0; |
| 504 | int chunk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 505 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 506 | yaffs2_checkpt_invalidate(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 507 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 508 | do { |
| 509 | struct yaffs_block_info *bi = 0; |
| 510 | int erased_ok = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 511 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 512 | chunk = yaffs_alloc_chunk(dev, use_reserver, &bi); |
| 513 | if (chunk < 0) { |
| 514 | /* no space */ |
| 515 | break; |
| 516 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 517 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 518 | /* First check this chunk is erased, if it needs |
| 519 | * checking. The checking policy (unless forced |
| 520 | * always on) is as follows: |
| 521 | * |
| 522 | * Check the first page we try to write in a block. |
| 523 | * If the check passes then we don't need to check any |
| 524 | * more. If the check fails, we check again... |
| 525 | * If the block has been erased, we don't need to check. |
| 526 | * |
| 527 | * However, if the block has been prioritised for gc, |
| 528 | * then we think there might be something odd about |
| 529 | * this block and stop using it. |
| 530 | * |
| 531 | * Rationale: We should only ever see chunks that have |
| 532 | * not been erased if there was a partially written |
| 533 | * chunk due to power loss. This checking policy should |
| 534 | * catch that case with very few checks and thus save a |
| 535 | * lot of checks that are most likely not needed. |
| 536 | * |
| 537 | * Mods to the above |
| 538 | * If an erase check fails or the write fails we skip the |
| 539 | * rest of the block. |
| 540 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 541 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 542 | /* let's give it a try */ |
| 543 | attempts++; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 544 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 545 | if (dev->param.always_check_erased) |
| 546 | bi->skip_erased_check = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 547 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 548 | if (!bi->skip_erased_check) { |
| 549 | erased_ok = yaffs_check_chunk_erased(dev, chunk); |
| 550 | if (erased_ok != YAFFS_OK) { |
| 551 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 552 | "**>> yaffs chunk %d was not erased", |
| 553 | chunk); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 554 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 555 | /* If not erased, delete this one, |
| 556 | * skip rest of block and |
| 557 | * try another chunk */ |
| 558 | yaffs_chunk_del(dev, chunk, 1, __LINE__); |
| 559 | yaffs_skip_rest_of_block(dev); |
| 560 | continue; |
| 561 | } |
| 562 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 563 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 564 | write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 565 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 566 | if (!bi->skip_erased_check) |
| 567 | write_ok = |
| 568 | yaffs_verify_chunk_written(dev, chunk, data, tags); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 569 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 570 | if (write_ok != YAFFS_OK) { |
| 571 | /* Clean up aborted write, skip to next block and |
| 572 | * try another chunk */ |
| 573 | yaffs_handle_chunk_wr_error(dev, chunk, erased_ok); |
| 574 | continue; |
| 575 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 576 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 577 | bi->skip_erased_check = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 578 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 579 | /* Copy the data into the robustification buffer */ |
| 580 | yaffs_handle_chunk_wr_ok(dev, chunk, data, tags); |
| 581 | |
| 582 | } while (write_ok != YAFFS_OK && |
| 583 | (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts)); |
| 584 | |
| 585 | if (!write_ok) |
| 586 | chunk = -1; |
| 587 | |
| 588 | if (attempts > 1) { |
| 589 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 590 | "**>> yaffs write required %d attempts", |
| 591 | attempts); |
| 592 | dev->n_retried_writes += (attempts - 1); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 593 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 594 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 595 | return chunk; |
| 596 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 597 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 598 | /* |
| 599 | * Block retiring for handling a broken block. |
| 600 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 601 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 602 | static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block) |
| 603 | { |
| 604 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 605 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 606 | yaffs2_checkpt_invalidate(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 607 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 608 | yaffs2_clear_oldest_dirty_seq(dev, bi); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 609 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 610 | if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) { |
| 611 | if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) { |
| 612 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 613 | "yaffs: Failed to mark bad and erase block %d", |
| 614 | flash_block); |
| 615 | } else { |
| 616 | struct yaffs_ext_tags tags; |
| 617 | int chunk_id = |
| 618 | flash_block * dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 619 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 620 | u8 *buffer = yaffs_get_temp_buffer(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 621 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 622 | memset(buffer, 0xff, dev->data_bytes_per_chunk); |
| 623 | memset(&tags, 0, sizeof(tags)); |
| 624 | tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK; |
| 625 | if (dev->param.write_chunk_tags_fn(dev, chunk_id - |
| 626 | dev->chunk_offset, |
| 627 | buffer, |
| 628 | &tags) != YAFFS_OK) |
| 629 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 630 | "yaffs: Failed to write bad block marker to block %d", |
| 631 | flash_block); |
| 632 | |
| 633 | yaffs_release_temp_buffer(dev, buffer); |
| 634 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 637 | bi->block_state = YAFFS_BLOCK_STATE_DEAD; |
| 638 | bi->gc_prioritise = 0; |
| 639 | bi->needs_retiring = 0; |
| 640 | |
| 641 | dev->n_retired_blocks++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 644 | /*---------------- Name handling functions ------------*/ |
| 645 | |
| 646 | static u16 yaffs_calc_name_sum(const YCHAR *name) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 647 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 648 | u16 sum = 0; |
| 649 | u16 i = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 650 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 651 | if (!name) |
| 652 | return 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 653 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 654 | while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) { |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 655 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 656 | /* 0x1f mask is case insensitive */ |
| 657 | sum += ((*name) & 0x1f) * i; |
| 658 | i++; |
| 659 | name++; |
| 660 | } |
| 661 | return sum; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 664 | void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 665 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 666 | memset(obj->short_name, 0, sizeof(obj->short_name)); |
| 667 | if (name && |
| 668 | yaffs_strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <= |
| 669 | YAFFS_SHORT_NAME_LENGTH) |
| 670 | yaffs_strcpy(obj->short_name, name); |
| 671 | else |
| 672 | obj->short_name[0] = _Y('\0'); |
| 673 | obj->sum = yaffs_calc_name_sum(name); |
| 674 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 675 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 676 | void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj, |
| 677 | const struct yaffs_obj_hdr *oh) |
| 678 | { |
| 679 | #ifdef CONFIG_YAFFS_AUTO_UNICODE |
| 680 | YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1]; |
| 681 | memset(tmp_name, 0, sizeof(tmp_name)); |
| 682 | yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name, |
| 683 | YAFFS_MAX_NAME_LENGTH + 1); |
| 684 | yaffs_set_obj_name(obj, tmp_name); |
| 685 | #else |
| 686 | yaffs_set_obj_name(obj, oh->name); |
| 687 | #endif |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 690 | loff_t yaffs_max_file_size(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 691 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 692 | return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 695 | /*-------------------- TNODES ------------------- |
| 696 | |
| 697 | * List of spare tnodes |
| 698 | * The list is hooked together using the first pointer |
| 699 | * in the tnode. |
| 700 | */ |
| 701 | |
| 702 | struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 703 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 704 | struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 705 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 706 | if (tn) { |
| 707 | memset(tn, 0, dev->tnode_size); |
| 708 | dev->n_tnodes++; |
| 709 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 710 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 711 | dev->checkpoint_blocks_required = 0; /* force recalculation */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 712 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 713 | return tn; |
| 714 | } |
| 715 | |
| 716 | /* FreeTnode frees up a tnode and puts it back on the free list */ |
| 717 | static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn) |
| 718 | { |
| 719 | yaffs_free_raw_tnode(dev, tn); |
| 720 | dev->n_tnodes--; |
| 721 | dev->checkpoint_blocks_required = 0; /* force recalculation */ |
| 722 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 723 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 724 | static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev) |
| 725 | { |
| 726 | yaffs_deinit_raw_tnodes_and_objs(dev); |
| 727 | dev->n_obj = 0; |
| 728 | dev->n_tnodes = 0; |
| 729 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 730 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 731 | void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn, |
| 732 | unsigned pos, unsigned val) |
| 733 | { |
| 734 | u32 *map = (u32 *) tn; |
| 735 | u32 bit_in_map; |
| 736 | u32 bit_in_word; |
| 737 | u32 word_in_map; |
| 738 | u32 mask; |
| 739 | |
| 740 | pos &= YAFFS_TNODES_LEVEL0_MASK; |
| 741 | val >>= dev->chunk_grp_bits; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 742 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 743 | bit_in_map = pos * dev->tnode_width; |
| 744 | word_in_map = bit_in_map / 32; |
| 745 | bit_in_word = bit_in_map & (32 - 1); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 746 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 747 | mask = dev->tnode_mask << bit_in_word; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 748 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 749 | map[word_in_map] &= ~mask; |
| 750 | map[word_in_map] |= (mask & (val << bit_in_word)); |
| 751 | |
| 752 | if (dev->tnode_width > (32 - bit_in_word)) { |
| 753 | bit_in_word = (32 - bit_in_word); |
| 754 | word_in_map++; |
| 755 | mask = |
| 756 | dev->tnode_mask >> bit_in_word; |
| 757 | map[word_in_map] &= ~mask; |
| 758 | map[word_in_map] |= (mask & (val >> bit_in_word)); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 759 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 760 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 761 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 762 | u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn, |
| 763 | unsigned pos) |
| 764 | { |
| 765 | u32 *map = (u32 *) tn; |
| 766 | u32 bit_in_map; |
| 767 | u32 bit_in_word; |
| 768 | u32 word_in_map; |
| 769 | u32 val; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 770 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 771 | pos &= YAFFS_TNODES_LEVEL0_MASK; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 772 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 773 | bit_in_map = pos * dev->tnode_width; |
| 774 | word_in_map = bit_in_map / 32; |
| 775 | bit_in_word = bit_in_map & (32 - 1); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 776 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 777 | val = map[word_in_map] >> bit_in_word; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 778 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 779 | if (dev->tnode_width > (32 - bit_in_word)) { |
| 780 | bit_in_word = (32 - bit_in_word); |
| 781 | word_in_map++; |
| 782 | val |= (map[word_in_map] << bit_in_word); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 783 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 784 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 785 | val &= dev->tnode_mask; |
| 786 | val <<= dev->chunk_grp_bits; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 787 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 788 | return val; |
| 789 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 790 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 791 | /* ------------------- End of individual tnode manipulation -----------------*/ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 792 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 793 | /* ---------Functions to manipulate the look-up tree (made up of tnodes) ------ |
| 794 | * The look up tree is represented by the top tnode and the number of top_level |
| 795 | * in the tree. 0 means only the level 0 tnode is in the tree. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 796 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 797 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 798 | /* FindLevel0Tnode finds the level 0 tnode, if one exists. */ |
| 799 | struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev, |
| 800 | struct yaffs_file_var *file_struct, |
| 801 | u32 chunk_id) |
| 802 | { |
| 803 | struct yaffs_tnode *tn = file_struct->top; |
| 804 | u32 i; |
| 805 | int required_depth; |
| 806 | int level = file_struct->top_level; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 807 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 808 | /* Check sane level and chunk Id */ |
| 809 | if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) |
| 810 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 811 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 812 | if (chunk_id > YAFFS_MAX_CHUNK_ID) |
| 813 | return NULL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 814 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 815 | /* First check we're tall enough (ie enough top_level) */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 816 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 817 | i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS; |
| 818 | required_depth = 0; |
| 819 | while (i) { |
| 820 | i >>= YAFFS_TNODES_INTERNAL_BITS; |
| 821 | required_depth++; |
| 822 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 823 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 824 | if (required_depth > file_struct->top_level) |
| 825 | return NULL; /* Not tall enough, so we can't find it */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 826 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 827 | /* Traverse down to level 0 */ |
| 828 | while (level > 0 && tn) { |
| 829 | tn = tn->internal[(chunk_id >> |
| 830 | (YAFFS_TNODES_LEVEL0_BITS + |
| 831 | (level - 1) * |
| 832 | YAFFS_TNODES_INTERNAL_BITS)) & |
| 833 | YAFFS_TNODES_INTERNAL_MASK]; |
| 834 | level--; |
| 835 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 836 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 837 | return tn; |
| 838 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 839 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 840 | /* add_find_tnode_0 finds the level 0 tnode if it exists, |
| 841 | * otherwise first expands the tree. |
| 842 | * This happens in two steps: |
| 843 | * 1. If the tree isn't tall enough, then make it taller. |
| 844 | * 2. Scan down the tree towards the level 0 tnode adding tnodes if required. |
| 845 | * |
| 846 | * Used when modifying the tree. |
| 847 | * |
| 848 | * If the tn argument is NULL, then a fresh tnode will be added otherwise the |
| 849 | * specified tn will be plugged into the ttree. |
| 850 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 851 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 852 | struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, |
| 853 | struct yaffs_file_var *file_struct, |
| 854 | u32 chunk_id, |
| 855 | struct yaffs_tnode *passed_tn) |
| 856 | { |
| 857 | int required_depth; |
| 858 | int i; |
| 859 | int l; |
| 860 | struct yaffs_tnode *tn; |
| 861 | u32 x; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 862 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 863 | /* Check sane level and page Id */ |
| 864 | if (file_struct->top_level < 0 || |
| 865 | file_struct->top_level > YAFFS_TNODES_MAX_LEVEL) |
| 866 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 867 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 868 | if (chunk_id > YAFFS_MAX_CHUNK_ID) |
| 869 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 870 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 871 | /* First check we're tall enough (ie enough top_level) */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 872 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 873 | x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS; |
| 874 | required_depth = 0; |
| 875 | while (x) { |
| 876 | x >>= YAFFS_TNODES_INTERNAL_BITS; |
| 877 | required_depth++; |
| 878 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 879 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 880 | if (required_depth > file_struct->top_level) { |
| 881 | /* Not tall enough, gotta make the tree taller */ |
| 882 | for (i = file_struct->top_level; i < required_depth; i++) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 883 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 884 | tn = yaffs_get_tnode(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 885 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 886 | if (tn) { |
| 887 | tn->internal[0] = file_struct->top; |
| 888 | file_struct->top = tn; |
| 889 | file_struct->top_level++; |
| 890 | } else { |
| 891 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 892 | "yaffs: no more tnodes"); |
| 893 | return NULL; |
| 894 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 895 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 896 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 897 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 898 | /* Traverse down to level 0, adding anything we need */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 899 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 900 | l = file_struct->top_level; |
| 901 | tn = file_struct->top; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 902 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 903 | if (l > 0) { |
| 904 | while (l > 0 && tn) { |
| 905 | x = (chunk_id >> |
| 906 | (YAFFS_TNODES_LEVEL0_BITS + |
| 907 | (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) & |
| 908 | YAFFS_TNODES_INTERNAL_MASK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 909 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 910 | if ((l > 1) && !tn->internal[x]) { |
| 911 | /* Add missing non-level-zero tnode */ |
| 912 | tn->internal[x] = yaffs_get_tnode(dev); |
| 913 | if (!tn->internal[x]) |
| 914 | return NULL; |
| 915 | } else if (l == 1) { |
| 916 | /* Looking from level 1 at level 0 */ |
| 917 | if (passed_tn) { |
| 918 | /* If we already have one, release it */ |
| 919 | if (tn->internal[x]) |
| 920 | yaffs_free_tnode(dev, |
| 921 | tn->internal[x]); |
| 922 | tn->internal[x] = passed_tn; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 923 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 924 | } else if (!tn->internal[x]) { |
| 925 | /* Don't have one, none passed in */ |
| 926 | tn->internal[x] = yaffs_get_tnode(dev); |
| 927 | if (!tn->internal[x]) |
| 928 | return NULL; |
| 929 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 930 | } |
| 931 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 932 | tn = tn->internal[x]; |
| 933 | l--; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 934 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 935 | } else { |
| 936 | /* top is level 0 */ |
| 937 | if (passed_tn) { |
| 938 | memcpy(tn, passed_tn, |
| 939 | (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8); |
| 940 | yaffs_free_tnode(dev, passed_tn); |
| 941 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 944 | return tn; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 947 | static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id, |
| 948 | int chunk_obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 949 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 950 | return (tags->chunk_id == chunk_obj && |
| 951 | tags->obj_id == obj_id && |
| 952 | !tags->is_deleted) ? 1 : 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 953 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 954 | } |
| 955 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 956 | static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk, |
| 957 | struct yaffs_ext_tags *tags, int obj_id, |
| 958 | int inode_chunk) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 959 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 960 | int j; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 961 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 962 | for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) { |
| 963 | if (yaffs_check_chunk_bit |
| 964 | (dev, the_chunk / dev->param.chunks_per_block, |
| 965 | the_chunk % dev->param.chunks_per_block)) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 966 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 967 | if (dev->chunk_grp_size == 1) |
| 968 | return the_chunk; |
| 969 | else { |
| 970 | yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL, |
| 971 | tags); |
| 972 | if (yaffs_tags_match(tags, |
| 973 | obj_id, inode_chunk)) { |
| 974 | /* found it; */ |
| 975 | return the_chunk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 976 | } |
| 977 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 978 | } |
| 979 | the_chunk++; |
| 980 | } |
| 981 | return -1; |
| 982 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 983 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 984 | static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk, |
| 985 | struct yaffs_ext_tags *tags) |
| 986 | { |
| 987 | /*Get the Tnode, then get the level 0 offset chunk offset */ |
| 988 | struct yaffs_tnode *tn; |
| 989 | int the_chunk = -1; |
| 990 | struct yaffs_ext_tags local_tags; |
| 991 | int ret_val = -1; |
| 992 | struct yaffs_dev *dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 993 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 994 | if (!tags) { |
| 995 | /* Passed a NULL, so use our own tags space */ |
| 996 | tags = &local_tags; |
| 997 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 998 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 999 | tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1000 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1001 | if (!tn) |
| 1002 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1003 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1004 | the_chunk = yaffs_get_group_base(dev, tn, inode_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1005 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1006 | ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id, |
| 1007 | inode_chunk); |
| 1008 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1011 | static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk, |
| 1012 | struct yaffs_ext_tags *tags) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1013 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1014 | /* Get the Tnode, then get the level 0 offset chunk offset */ |
| 1015 | struct yaffs_tnode *tn; |
| 1016 | int the_chunk = -1; |
| 1017 | struct yaffs_ext_tags local_tags; |
| 1018 | struct yaffs_dev *dev = in->my_dev; |
| 1019 | int ret_val = -1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1020 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1021 | if (!tags) { |
| 1022 | /* Passed a NULL, so use our own tags space */ |
| 1023 | tags = &local_tags; |
| 1024 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1025 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1026 | tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1027 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1028 | if (!tn) |
| 1029 | return ret_val; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1030 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1031 | the_chunk = yaffs_get_group_base(dev, tn, inode_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1032 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1033 | ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id, |
| 1034 | inode_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1035 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1036 | /* Delete the entry in the filestructure (if found) */ |
| 1037 | if (ret_val != -1) |
| 1038 | yaffs_load_tnode_0(dev, tn, inode_chunk, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1039 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1040 | return ret_val; |
| 1041 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1042 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1043 | int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, |
| 1044 | int nand_chunk, int in_scan) |
| 1045 | { |
| 1046 | /* NB in_scan is zero unless scanning. |
| 1047 | * For forward scanning, in_scan is > 0; |
| 1048 | * for backward scanning in_scan is < 0 |
| 1049 | * |
| 1050 | * nand_chunk = 0 is a dummy insert to make sure the tnodes are there. |
| 1051 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1052 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1053 | struct yaffs_tnode *tn; |
| 1054 | struct yaffs_dev *dev = in->my_dev; |
| 1055 | int existing_cunk; |
| 1056 | struct yaffs_ext_tags existing_tags; |
| 1057 | struct yaffs_ext_tags new_tags; |
| 1058 | unsigned existing_serial, new_serial; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1059 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1060 | if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) { |
| 1061 | /* Just ignore an attempt at putting a chunk into a non-file |
| 1062 | * during scanning. |
| 1063 | * If it is not during Scanning then something went wrong! |
| 1064 | */ |
| 1065 | if (!in_scan) { |
| 1066 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 1067 | "yaffs tragedy:attempt to put data chunk into a non-file" |
| 1068 | ); |
| 1069 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1072 | yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); |
| 1073 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1076 | tn = yaffs_add_find_tnode_0(dev, |
| 1077 | &in->variant.file_variant, |
| 1078 | inode_chunk, NULL); |
| 1079 | if (!tn) |
| 1080 | return YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1081 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1082 | if (!nand_chunk) |
| 1083 | /* Dummy insert, bail now */ |
| 1084 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1085 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1086 | existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1087 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1088 | if (in_scan != 0) { |
| 1089 | /* If we're scanning then we need to test for duplicates |
| 1090 | * NB This does not need to be efficient since it should only |
| 1091 | * happen when the power fails during a write, then only one |
| 1092 | * chunk should ever be affected. |
| 1093 | * |
| 1094 | * Correction for YAFFS2: This could happen quite a lot and we |
| 1095 | * need to think about efficiency! TODO |
| 1096 | * Update: For backward scanning we don't need to re-read tags |
| 1097 | * so this is quite cheap. |
| 1098 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1099 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1100 | if (existing_cunk > 0) { |
| 1101 | /* NB Right now existing chunk will not be real |
| 1102 | * chunk_id if the chunk group size > 1 |
| 1103 | * thus we have to do a FindChunkInFile to get the |
| 1104 | * real chunk id. |
| 1105 | * |
| 1106 | * We have a duplicate now we need to decide which |
| 1107 | * one to use: |
| 1108 | * |
| 1109 | * Backwards scanning YAFFS2: The old one is what |
| 1110 | * we use, dump the new one. |
| 1111 | * YAFFS1: Get both sets of tags and compare serial |
| 1112 | * numbers. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1113 | */ |
| 1114 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1115 | if (in_scan > 0) { |
| 1116 | /* Only do this for forward scanning */ |
| 1117 | yaffs_rd_chunk_tags_nand(dev, |
| 1118 | nand_chunk, |
| 1119 | NULL, &new_tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1120 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1121 | /* Do a proper find */ |
| 1122 | existing_cunk = |
| 1123 | yaffs_find_chunk_in_file(in, inode_chunk, |
| 1124 | &existing_tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1127 | if (existing_cunk <= 0) { |
| 1128 | /*Hoosterman - how did this happen? */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1129 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1130 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 1131 | "yaffs tragedy: existing chunk < 0 in scan" |
| 1132 | ); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1133 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1134 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1135 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1136 | /* NB The deleted flags should be false, otherwise |
| 1137 | * the chunks will not be loaded during a scan |
| 1138 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1139 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1140 | if (in_scan > 0) { |
| 1141 | new_serial = new_tags.serial_number; |
| 1142 | existing_serial = existing_tags.serial_number; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1143 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1144 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1145 | if ((in_scan > 0) && |
| 1146 | (existing_cunk <= 0 || |
| 1147 | ((existing_serial + 1) & 3) == new_serial)) { |
| 1148 | /* Forward scanning. |
| 1149 | * Use new |
| 1150 | * Delete the old one and drop through to |
| 1151 | * update the tnode |
| 1152 | */ |
| 1153 | yaffs_chunk_del(dev, existing_cunk, 1, |
| 1154 | __LINE__); |
| 1155 | } else { |
| 1156 | /* Backward scanning or we want to use the |
| 1157 | * existing one |
| 1158 | * Delete the new one and return early so that |
| 1159 | * the tnode isn't changed |
| 1160 | */ |
| 1161 | yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); |
| 1162 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1163 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1166 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1167 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1168 | if (existing_cunk == 0) |
| 1169 | in->n_data_chunks++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1170 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1171 | yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk); |
| 1172 | |
| 1173 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1176 | static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1177 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1178 | struct yaffs_block_info *the_block; |
| 1179 | unsigned block_no; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1180 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1181 | yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk); |
| 1182 | |
| 1183 | block_no = chunk / dev->param.chunks_per_block; |
| 1184 | the_block = yaffs_get_block_info(dev, block_no); |
| 1185 | if (the_block) { |
| 1186 | the_block->soft_del_pages++; |
| 1187 | dev->n_free_chunks++; |
| 1188 | yaffs2_update_oldest_dirty_seq(dev, block_no, the_block); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1192 | /* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all |
| 1193 | * the chunks in the file. |
| 1194 | * All soft deleting does is increment the block's softdelete count and pulls |
| 1195 | * the chunk out of the tnode. |
| 1196 | * Thus, essentially this is the same as DeleteWorker except that the chunks |
| 1197 | * are soft deleted. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1198 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1199 | |
| 1200 | static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn, |
| 1201 | u32 level, int chunk_offset) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1202 | { |
| 1203 | int i; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1204 | int the_chunk; |
| 1205 | int all_done = 1; |
| 1206 | struct yaffs_dev *dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1207 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1208 | if (!tn) |
| 1209 | return 1; |
| 1210 | |
| 1211 | if (level > 0) { |
| 1212 | for (i = YAFFS_NTNODES_INTERNAL - 1; |
| 1213 | all_done && i >= 0; |
| 1214 | i--) { |
| 1215 | if (tn->internal[i]) { |
| 1216 | all_done = |
| 1217 | yaffs_soft_del_worker(in, |
| 1218 | tn->internal[i], |
| 1219 | level - 1, |
| 1220 | (chunk_offset << |
| 1221 | YAFFS_TNODES_INTERNAL_BITS) |
| 1222 | + i); |
| 1223 | if (all_done) { |
| 1224 | yaffs_free_tnode(dev, |
| 1225 | tn->internal[i]); |
| 1226 | tn->internal[i] = NULL; |
| 1227 | } else { |
| 1228 | /* Can this happen? */ |
| 1229 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1230 | } |
| 1231 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1232 | return (all_done) ? 1 : 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1233 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1234 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1235 | /* level 0 */ |
| 1236 | for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) { |
| 1237 | the_chunk = yaffs_get_group_base(dev, tn, i); |
| 1238 | if (the_chunk) { |
| 1239 | yaffs_soft_del_chunk(dev, the_chunk); |
| 1240 | yaffs_load_tnode_0(dev, tn, i, 0); |
| 1241 | } |
| 1242 | } |
| 1243 | return 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1246 | static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1247 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1248 | struct yaffs_dev *dev = obj->my_dev; |
| 1249 | struct yaffs_obj *parent; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1250 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1251 | yaffs_verify_obj_in_dir(obj); |
| 1252 | parent = obj->parent; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1253 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1254 | yaffs_verify_dir(parent); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1255 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1256 | if (dev && dev->param.remove_obj_fn) |
| 1257 | dev->param.remove_obj_fn(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1258 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1259 | list_del_init(&obj->siblings); |
| 1260 | obj->parent = NULL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1261 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1262 | yaffs_verify_dir(parent); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1265 | void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1266 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1267 | if (!directory) { |
| 1268 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 1269 | "tragedy: Trying to add an object to a null pointer directory" |
| 1270 | ); |
| 1271 | BUG(); |
| 1272 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1273 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1274 | if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { |
| 1275 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 1276 | "tragedy: Trying to add an object to a non-directory" |
| 1277 | ); |
| 1278 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1279 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1280 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1281 | if (obj->siblings.prev == NULL) { |
| 1282 | /* Not initialised */ |
| 1283 | BUG(); |
| 1284 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1285 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1286 | yaffs_verify_dir(directory); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1287 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1288 | yaffs_remove_obj_from_dir(obj); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1289 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1290 | /* Now add it */ |
| 1291 | list_add(&obj->siblings, &directory->variant.dir_variant.children); |
| 1292 | obj->parent = directory; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1293 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1294 | if (directory == obj->my_dev->unlinked_dir |
| 1295 | || directory == obj->my_dev->del_dir) { |
| 1296 | obj->unlinked = 1; |
| 1297 | obj->my_dev->n_unlinked_files++; |
| 1298 | obj->rename_allowed = 0; |
| 1299 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1300 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1301 | yaffs_verify_dir(directory); |
| 1302 | yaffs_verify_obj_in_dir(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1305 | static int yaffs_change_obj_name(struct yaffs_obj *obj, |
| 1306 | struct yaffs_obj *new_dir, |
| 1307 | const YCHAR *new_name, int force, int shadows) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1308 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1309 | int unlink_op; |
| 1310 | int del_op; |
| 1311 | struct yaffs_obj *existing_target; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1312 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1313 | if (new_dir == NULL) |
| 1314 | new_dir = obj->parent; /* use the old directory */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1315 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1316 | if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { |
| 1317 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 1318 | "tragedy: yaffs_change_obj_name: new_dir is not a directory" |
| 1319 | ); |
| 1320 | BUG(); |
| 1321 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1322 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1323 | unlink_op = (new_dir == obj->my_dev->unlinked_dir); |
| 1324 | del_op = (new_dir == obj->my_dev->del_dir); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1325 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1326 | existing_target = yaffs_find_by_name(new_dir, new_name); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1327 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1328 | /* If the object is a file going into the unlinked directory, |
| 1329 | * then it is OK to just stuff it in since duplicate names are OK. |
| 1330 | * else only proceed if the new name does not exist and we're putting |
| 1331 | * it into a directory. |
| 1332 | */ |
| 1333 | if (!(unlink_op || del_op || force || |
| 1334 | shadows > 0 || !existing_target) || |
| 1335 | new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) |
| 1336 | return YAFFS_FAIL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1337 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1338 | yaffs_set_obj_name(obj, new_name); |
| 1339 | obj->dirty = 1; |
| 1340 | yaffs_add_obj_to_dir(new_dir, obj); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1341 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1342 | if (unlink_op) |
| 1343 | obj->unlinked = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1344 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1345 | /* If it is a deletion then we mark it as a shrink for gc */ |
| 1346 | if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0) |
| 1347 | return YAFFS_OK; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1348 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1349 | return YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1352 | /*------------------------ Short Operations Cache ------------------------------ |
| 1353 | * In many situations where there is no high level buffering a lot of |
| 1354 | * reads might be short sequential reads, and a lot of writes may be short |
| 1355 | * sequential writes. eg. scanning/writing a jpeg file. |
| 1356 | * In these cases, a short read/write cache can provide a huge perfomance |
| 1357 | * benefit with dumb-as-a-rock code. |
| 1358 | * In Linux, the page cache provides read buffering and the short op cache |
| 1359 | * provides write buffering. |
| 1360 | * |
| 1361 | * There are a small number (~10) of cache chunks per device so that we don't |
| 1362 | * need a very intelligent search. |
| 1363 | */ |
| 1364 | |
| 1365 | static int yaffs_obj_cache_dirty(struct yaffs_obj *obj) |
| 1366 | { |
| 1367 | struct yaffs_dev *dev = obj->my_dev; |
| 1368 | int i; |
| 1369 | struct yaffs_cache *cache; |
| 1370 | int n_caches = obj->my_dev->param.n_caches; |
| 1371 | |
| 1372 | for (i = 0; i < n_caches; i++) { |
| 1373 | cache = &dev->cache[i]; |
| 1374 | if (cache->object == obj && cache->dirty) |
| 1375 | return 1; |
| 1376 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1377 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1378 | return 0; |
| 1379 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1380 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1381 | static void yaffs_flush_file_cache(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1382 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1383 | struct yaffs_dev *dev = obj->my_dev; |
| 1384 | int lowest = -99; /* Stop compiler whining. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1385 | int i; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1386 | struct yaffs_cache *cache; |
| 1387 | int chunk_written = 0; |
| 1388 | int n_caches = obj->my_dev->param.n_caches; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1389 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1390 | if (n_caches < 1) |
| 1391 | return; |
| 1392 | do { |
| 1393 | cache = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1394 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1395 | /* Find the lowest dirty chunk for this object */ |
| 1396 | for (i = 0; i < n_caches; i++) { |
| 1397 | if (dev->cache[i].object == obj && |
| 1398 | dev->cache[i].dirty) { |
| 1399 | if (!cache || |
| 1400 | dev->cache[i].chunk_id < lowest) { |
| 1401 | cache = &dev->cache[i]; |
| 1402 | lowest = cache->chunk_id; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1403 | } |
| 1404 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1405 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1406 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1407 | if (cache && !cache->locked) { |
| 1408 | /* Write it out and free it up */ |
| 1409 | chunk_written = |
| 1410 | yaffs_wr_data_obj(cache->object, |
| 1411 | cache->chunk_id, |
| 1412 | cache->data, |
| 1413 | cache->n_bytes, 1); |
| 1414 | cache->dirty = 0; |
| 1415 | cache->object = NULL; |
| 1416 | } |
| 1417 | } while (cache && chunk_written > 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1418 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1419 | if (cache) |
| 1420 | /* Hoosterman, disk full while writing cache out. */ |
| 1421 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 1422 | "yaffs tragedy: no space during cache write"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1425 | /*yaffs_flush_whole_cache(dev) |
| 1426 | * |
| 1427 | * |
| 1428 | */ |
| 1429 | |
| 1430 | void yaffs_flush_whole_cache(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1431 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1432 | struct yaffs_obj *obj; |
| 1433 | int n_caches = dev->param.n_caches; |
| 1434 | int i; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1435 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1436 | /* Find a dirty object in the cache and flush it... |
| 1437 | * until there are no further dirty objects. |
| 1438 | */ |
| 1439 | do { |
| 1440 | obj = NULL; |
| 1441 | for (i = 0; i < n_caches && !obj; i++) { |
| 1442 | if (dev->cache[i].object && dev->cache[i].dirty) |
| 1443 | obj = dev->cache[i].object; |
| 1444 | } |
| 1445 | if (obj) |
| 1446 | yaffs_flush_file_cache(obj); |
| 1447 | } while (obj); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1448 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1451 | /* Grab us a cache chunk for use. |
| 1452 | * First look for an empty one. |
| 1453 | * Then look for the least recently used non-dirty one. |
| 1454 | * Then look for the least recently used dirty one...., flush and look again. |
| 1455 | */ |
| 1456 | static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1457 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1458 | int i; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1459 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1460 | if (dev->param.n_caches > 0) { |
| 1461 | for (i = 0; i < dev->param.n_caches; i++) { |
| 1462 | if (!dev->cache[i].object) |
| 1463 | return &dev->cache[i]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1464 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1465 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1466 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1467 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1468 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1469 | static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1470 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1471 | struct yaffs_cache *cache; |
| 1472 | struct yaffs_obj *the_obj; |
| 1473 | int usage; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1474 | int i; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1475 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1476 | if (dev->param.n_caches < 1) |
| 1477 | return NULL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1478 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1479 | /* Try find a non-dirty one... */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1480 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1481 | cache = yaffs_grab_chunk_worker(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1482 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1483 | if (!cache) { |
| 1484 | /* They were all dirty, find the LRU object and flush |
| 1485 | * its cache, then find again. |
| 1486 | * NB what's here is not very accurate, |
| 1487 | * we actually flush the object with the LRU chunk. |
| 1488 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1489 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1490 | /* With locking we can't assume we can use entry zero, |
| 1491 | * Set the_obj to a valid pointer for Coverity. */ |
| 1492 | the_obj = dev->cache[0].object; |
| 1493 | usage = -1; |
| 1494 | cache = NULL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1495 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1496 | for (i = 0; i < dev->param.n_caches; i++) { |
| 1497 | if (dev->cache[i].object && |
| 1498 | !dev->cache[i].locked && |
| 1499 | (dev->cache[i].last_use < usage || |
| 1500 | !cache)) { |
| 1501 | usage = dev->cache[i].last_use; |
| 1502 | the_obj = dev->cache[i].object; |
| 1503 | cache = &dev->cache[i]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1504 | } |
| 1505 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1506 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1507 | if (!cache || cache->dirty) { |
| 1508 | /* Flush and try again */ |
| 1509 | yaffs_flush_file_cache(the_obj); |
| 1510 | cache = yaffs_grab_chunk_worker(dev); |
| 1511 | } |
| 1512 | } |
| 1513 | return cache; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1514 | } |
| 1515 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1516 | /* Find a cached chunk */ |
| 1517 | static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj, |
| 1518 | int chunk_id) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1519 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1520 | struct yaffs_dev *dev = obj->my_dev; |
| 1521 | int i; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1522 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1523 | if (dev->param.n_caches < 1) |
| 1524 | return NULL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1525 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1526 | for (i = 0; i < dev->param.n_caches; i++) { |
| 1527 | if (dev->cache[i].object == obj && |
| 1528 | dev->cache[i].chunk_id == chunk_id) { |
| 1529 | dev->cache_hits++; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1530 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1531 | return &dev->cache[i]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1532 | } |
| 1533 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1534 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1535 | } |
| 1536 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1537 | /* Mark the chunk for the least recently used algorithym */ |
| 1538 | static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache, |
| 1539 | int is_write) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1540 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1541 | int i; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1542 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1543 | if (dev->param.n_caches < 1) |
| 1544 | return; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1545 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1546 | if (dev->cache_last_use < 0 || |
| 1547 | dev->cache_last_use > 100000000) { |
| 1548 | /* Reset the cache usages */ |
| 1549 | for (i = 1; i < dev->param.n_caches; i++) |
| 1550 | dev->cache[i].last_use = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1551 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1552 | dev->cache_last_use = 0; |
| 1553 | } |
| 1554 | dev->cache_last_use++; |
| 1555 | cache->last_use = dev->cache_last_use; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1556 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1557 | if (is_write) |
| 1558 | cache->dirty = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1559 | } |
| 1560 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1561 | /* Invalidate a single cache page. |
| 1562 | * Do this when a whole page gets written, |
| 1563 | * ie the short cache for this page is no longer valid. |
| 1564 | */ |
| 1565 | static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1566 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1567 | struct yaffs_cache *cache; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1568 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1569 | if (object->my_dev->param.n_caches > 0) { |
| 1570 | cache = yaffs_find_chunk_cache(object, chunk_id); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1571 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1572 | if (cache) |
| 1573 | cache->object = NULL; |
| 1574 | } |
| 1575 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1576 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1577 | /* Invalidate all the cache pages associated with this object |
| 1578 | * Do this whenever ther file is deleted or resized. |
| 1579 | */ |
| 1580 | static void yaffs_invalidate_whole_cache(struct yaffs_obj *in) |
| 1581 | { |
| 1582 | int i; |
| 1583 | struct yaffs_dev *dev = in->my_dev; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1584 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1585 | if (dev->param.n_caches > 0) { |
| 1586 | /* Invalidate it. */ |
| 1587 | for (i = 0; i < dev->param.n_caches; i++) { |
| 1588 | if (dev->cache[i].object == in) |
| 1589 | dev->cache[i].object = NULL; |
| 1590 | } |
| 1591 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1592 | } |
| 1593 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1594 | static void yaffs_unhash_obj(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1595 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1596 | int bucket; |
| 1597 | struct yaffs_dev *dev = obj->my_dev; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1598 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1599 | /* If it is still linked into the bucket list, free from the list */ |
| 1600 | if (!list_empty(&obj->hash_link)) { |
| 1601 | list_del_init(&obj->hash_link); |
| 1602 | bucket = yaffs_hash_fn(obj->obj_id); |
| 1603 | dev->obj_bucket[bucket].count--; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1604 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1605 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1606 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1607 | /* FreeObject frees up a Object and puts it back on the free list */ |
| 1608 | static void yaffs_free_obj(struct yaffs_obj *obj) |
| 1609 | { |
| 1610 | struct yaffs_dev *dev; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1611 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1612 | if (!obj) { |
| 1613 | BUG(); |
| 1614 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1615 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1616 | dev = obj->my_dev; |
| 1617 | yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p", |
| 1618 | obj, obj->my_inode); |
| 1619 | if (obj->parent) |
| 1620 | BUG(); |
| 1621 | if (!list_empty(&obj->siblings)) |
| 1622 | BUG(); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1623 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1624 | if (obj->my_inode) { |
| 1625 | /* We're still hooked up to a cached inode. |
| 1626 | * Don't delete now, but mark for later deletion |
| 1627 | */ |
| 1628 | obj->defered_free = 1; |
| 1629 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1630 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1631 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1632 | yaffs_unhash_obj(obj); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1633 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1634 | yaffs_free_raw_obj(dev, obj); |
| 1635 | dev->n_obj--; |
| 1636 | dev->checkpoint_blocks_required = 0; /* force recalculation */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1637 | } |
| 1638 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1639 | void yaffs_handle_defered_free(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1640 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1641 | if (obj->defered_free) |
| 1642 | yaffs_free_obj(obj); |
| 1643 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1644 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1645 | static int yaffs_generic_obj_del(struct yaffs_obj *in) |
| 1646 | { |
| 1647 | /* Iinvalidate the file's data in the cache, without flushing. */ |
| 1648 | yaffs_invalidate_whole_cache(in); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1649 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1650 | if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) { |
| 1651 | /* Move to unlinked directory so we have a deletion record */ |
| 1652 | yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0, |
| 1653 | 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1654 | } |
| 1655 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1656 | yaffs_remove_obj_from_dir(in); |
| 1657 | yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__); |
| 1658 | in->hdr_chunk = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1659 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1660 | yaffs_free_obj(in); |
| 1661 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1662 | |
| 1663 | } |
| 1664 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1665 | static void yaffs_soft_del_file(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1666 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1667 | if (!obj->deleted || |
| 1668 | obj->variant_type != YAFFS_OBJECT_TYPE_FILE || |
| 1669 | obj->soft_del) |
| 1670 | return; |
| 1671 | |
| 1672 | if (obj->n_data_chunks <= 0) { |
| 1673 | /* Empty file with no duplicate object headers, |
| 1674 | * just delete it immediately */ |
| 1675 | yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top); |
| 1676 | obj->variant.file_variant.top = NULL; |
| 1677 | yaffs_trace(YAFFS_TRACE_TRACING, |
| 1678 | "yaffs: Deleting empty file %d", |
| 1679 | obj->obj_id); |
| 1680 | yaffs_generic_obj_del(obj); |
| 1681 | } else { |
| 1682 | yaffs_soft_del_worker(obj, |
| 1683 | obj->variant.file_variant.top, |
| 1684 | obj->variant. |
| 1685 | file_variant.top_level, 0); |
| 1686 | obj->soft_del = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1690 | /* Pruning removes any part of the file structure tree that is beyond the |
| 1691 | * bounds of the file (ie that does not point to chunks). |
| 1692 | * |
| 1693 | * A file should only get pruned when its size is reduced. |
| 1694 | * |
| 1695 | * Before pruning, the chunks must be pulled from the tree and the |
| 1696 | * level 0 tnode entries must be zeroed out. |
| 1697 | * Could also use this for file deletion, but that's probably better handled |
| 1698 | * by a special case. |
| 1699 | * |
| 1700 | * This function is recursive. For levels > 0 the function is called again on |
| 1701 | * any sub-tree. For level == 0 we just check if the sub-tree has data. |
| 1702 | * If there is no data in a subtree then it is pruned. |
| 1703 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1704 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1705 | static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev, |
| 1706 | struct yaffs_tnode *tn, u32 level, |
| 1707 | int del0) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1708 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1709 | int i; |
| 1710 | int has_data; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1711 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1712 | if (!tn) |
| 1713 | return tn; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1714 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1715 | has_data = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1716 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1717 | if (level > 0) { |
| 1718 | for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) { |
| 1719 | if (tn->internal[i]) { |
| 1720 | tn->internal[i] = |
| 1721 | yaffs_prune_worker(dev, |
| 1722 | tn->internal[i], |
| 1723 | level - 1, |
| 1724 | (i == 0) ? del0 : 1); |
| 1725 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1726 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1727 | if (tn->internal[i]) |
| 1728 | has_data++; |
| 1729 | } |
| 1730 | } else { |
| 1731 | int tnode_size_u32 = dev->tnode_size / sizeof(u32); |
| 1732 | u32 *map = (u32 *) tn; |
| 1733 | |
| 1734 | for (i = 0; !has_data && i < tnode_size_u32; i++) { |
| 1735 | if (map[i]) |
| 1736 | has_data++; |
| 1737 | } |
| 1738 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1739 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1740 | if (has_data == 0 && del0) { |
| 1741 | /* Free and return NULL */ |
| 1742 | yaffs_free_tnode(dev, tn); |
| 1743 | tn = NULL; |
| 1744 | } |
| 1745 | return tn; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1748 | static int yaffs_prune_tree(struct yaffs_dev *dev, |
| 1749 | struct yaffs_file_var *file_struct) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1750 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1751 | int i; |
| 1752 | int has_data; |
| 1753 | int done = 0; |
| 1754 | struct yaffs_tnode *tn; |
| 1755 | |
| 1756 | if (file_struct->top_level < 1) |
| 1757 | return YAFFS_OK; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1758 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1759 | file_struct->top = |
| 1760 | yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1761 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1762 | /* Now we have a tree with all the non-zero branches NULL but |
| 1763 | * the height is the same as it was. |
| 1764 | * Let's see if we can trim internal tnodes to shorten the tree. |
| 1765 | * We can do this if only the 0th element in the tnode is in use |
| 1766 | * (ie all the non-zero are NULL) |
| 1767 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1768 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1769 | while (file_struct->top_level && !done) { |
| 1770 | tn = file_struct->top; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 1771 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1772 | has_data = 0; |
| 1773 | for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) { |
| 1774 | if (tn->internal[i]) |
| 1775 | has_data++; |
| 1776 | } |
| 1777 | |
| 1778 | if (!has_data) { |
| 1779 | file_struct->top = tn->internal[0]; |
| 1780 | file_struct->top_level--; |
| 1781 | yaffs_free_tnode(dev, tn); |
| 1782 | } else { |
| 1783 | done = 1; |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1788 | } |
| 1789 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1790 | /*-------------------- End of File Structure functions.-------------------*/ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1791 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1792 | /* alloc_empty_obj gets us a clean Object.*/ |
| 1793 | static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1794 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1795 | struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1796 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1797 | if (!obj) |
| 1798 | return obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1799 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1800 | dev->n_obj++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1801 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1802 | /* Now sweeten it up... */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1803 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1804 | memset(obj, 0, sizeof(struct yaffs_obj)); |
| 1805 | obj->being_created = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1806 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1807 | obj->my_dev = dev; |
| 1808 | obj->hdr_chunk = 0; |
| 1809 | obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN; |
| 1810 | INIT_LIST_HEAD(&(obj->hard_links)); |
| 1811 | INIT_LIST_HEAD(&(obj->hash_link)); |
| 1812 | INIT_LIST_HEAD(&obj->siblings); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1813 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1814 | /* Now make the directory sane */ |
| 1815 | if (dev->root_dir) { |
| 1816 | obj->parent = dev->root_dir; |
| 1817 | list_add(&(obj->siblings), |
| 1818 | &dev->root_dir->variant.dir_variant.children); |
| 1819 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1820 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1821 | /* Add it to the lost and found directory. |
| 1822 | * NB Can't put root or lost-n-found in lost-n-found so |
| 1823 | * check if lost-n-found exists first |
| 1824 | */ |
| 1825 | if (dev->lost_n_found) |
| 1826 | yaffs_add_obj_to_dir(dev->lost_n_found, obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1827 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1828 | obj->being_created = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1829 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1830 | dev->checkpoint_blocks_required = 0; /* force recalculation */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1831 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1832 | return obj; |
| 1833 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1834 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1835 | static int yaffs_find_nice_bucket(struct yaffs_dev *dev) |
| 1836 | { |
| 1837 | int i; |
| 1838 | int l = 999; |
| 1839 | int lowest = 999999; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1840 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1841 | /* Search for the shortest list or one that |
| 1842 | * isn't too long. |
| 1843 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1844 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1845 | for (i = 0; i < 10 && lowest > 4; i++) { |
| 1846 | dev->bucket_finder++; |
| 1847 | dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS; |
| 1848 | if (dev->obj_bucket[dev->bucket_finder].count < lowest) { |
| 1849 | lowest = dev->obj_bucket[dev->bucket_finder].count; |
| 1850 | l = dev->bucket_finder; |
| 1851 | } |
| 1852 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1853 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1854 | return l; |
| 1855 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1856 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1857 | static int yaffs_new_obj_id(struct yaffs_dev *dev) |
| 1858 | { |
| 1859 | int bucket = yaffs_find_nice_bucket(dev); |
| 1860 | int found = 0; |
| 1861 | struct list_head *i; |
| 1862 | u32 n = (u32) bucket; |
| 1863 | |
| 1864 | /* Now find an object value that has not already been taken |
| 1865 | * by scanning the list. |
| 1866 | */ |
| 1867 | |
| 1868 | while (!found) { |
| 1869 | found = 1; |
| 1870 | n += YAFFS_NOBJECT_BUCKETS; |
Heinrich Schuchardt | 8acf9e8 | 2017-11-09 01:26:43 +0100 | [diff] [blame] | 1871 | list_for_each(i, &dev->obj_bucket[bucket].list) { |
| 1872 | /* If there is already one in the list */ |
Heinrich Schuchardt | dcb6634 | 2018-03-08 22:52:29 +0100 | [diff] [blame] | 1873 | if (list_entry(i, struct yaffs_obj, |
| 1874 | hash_link)->obj_id == n) { |
Heinrich Schuchardt | 8acf9e8 | 2017-11-09 01:26:43 +0100 | [diff] [blame] | 1875 | found = 0; |
| 1876 | break; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1877 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | return n; |
| 1881 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1882 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1883 | static void yaffs_hash_obj(struct yaffs_obj *in) |
| 1884 | { |
| 1885 | int bucket = yaffs_hash_fn(in->obj_id); |
| 1886 | struct yaffs_dev *dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1887 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1888 | list_add(&in->hash_link, &dev->obj_bucket[bucket].list); |
| 1889 | dev->obj_bucket[bucket].count++; |
| 1890 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1891 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1892 | struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number) |
| 1893 | { |
| 1894 | int bucket = yaffs_hash_fn(number); |
| 1895 | struct list_head *i; |
| 1896 | struct yaffs_obj *in; |
| 1897 | |
| 1898 | list_for_each(i, &dev->obj_bucket[bucket].list) { |
| 1899 | /* Look if it is in the list */ |
| 1900 | in = list_entry(i, struct yaffs_obj, hash_link); |
| 1901 | if (in->obj_id == number) { |
| 1902 | /* Don't show if it is defered free */ |
| 1903 | if (in->defered_free) |
| 1904 | return NULL; |
| 1905 | return in; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | return NULL; |
| 1910 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1911 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1912 | struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number, |
| 1913 | enum yaffs_obj_type type) |
| 1914 | { |
| 1915 | struct yaffs_obj *the_obj = NULL; |
| 1916 | struct yaffs_tnode *tn = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1917 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1918 | if (number < 0) |
| 1919 | number = yaffs_new_obj_id(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1920 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1921 | if (type == YAFFS_OBJECT_TYPE_FILE) { |
| 1922 | tn = yaffs_get_tnode(dev); |
| 1923 | if (!tn) |
| 1924 | return NULL; |
| 1925 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1926 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1927 | the_obj = yaffs_alloc_empty_obj(dev); |
| 1928 | if (!the_obj) { |
| 1929 | if (tn) |
| 1930 | yaffs_free_tnode(dev, tn); |
| 1931 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1932 | } |
| 1933 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1934 | the_obj->fake = 0; |
| 1935 | the_obj->rename_allowed = 1; |
| 1936 | the_obj->unlink_allowed = 1; |
| 1937 | the_obj->obj_id = number; |
| 1938 | yaffs_hash_obj(the_obj); |
| 1939 | the_obj->variant_type = type; |
| 1940 | yaffs_load_current_time(the_obj, 1, 1); |
| 1941 | |
| 1942 | switch (type) { |
| 1943 | case YAFFS_OBJECT_TYPE_FILE: |
| 1944 | the_obj->variant.file_variant.file_size = 0; |
| 1945 | the_obj->variant.file_variant.scanned_size = 0; |
| 1946 | the_obj->variant.file_variant.shrink_size = |
| 1947 | yaffs_max_file_size(dev); |
| 1948 | the_obj->variant.file_variant.top_level = 0; |
| 1949 | the_obj->variant.file_variant.top = tn; |
| 1950 | break; |
| 1951 | case YAFFS_OBJECT_TYPE_DIRECTORY: |
| 1952 | INIT_LIST_HEAD(&the_obj->variant.dir_variant.children); |
| 1953 | INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty); |
| 1954 | break; |
| 1955 | case YAFFS_OBJECT_TYPE_SYMLINK: |
| 1956 | case YAFFS_OBJECT_TYPE_HARDLINK: |
| 1957 | case YAFFS_OBJECT_TYPE_SPECIAL: |
| 1958 | /* No action required */ |
| 1959 | break; |
| 1960 | case YAFFS_OBJECT_TYPE_UNKNOWN: |
| 1961 | /* todo this should not happen */ |
| 1962 | break; |
| 1963 | } |
| 1964 | return the_obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1967 | static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev, |
| 1968 | int number, u32 mode) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1969 | { |
| 1970 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1971 | struct yaffs_obj *obj = |
| 1972 | yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1973 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1974 | if (!obj) |
| 1975 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1976 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1977 | obj->fake = 1; /* it is fake so it might not use NAND */ |
| 1978 | obj->rename_allowed = 0; |
| 1979 | obj->unlink_allowed = 0; |
| 1980 | obj->deleted = 0; |
| 1981 | obj->unlinked = 0; |
| 1982 | obj->yst_mode = mode; |
| 1983 | obj->my_dev = dev; |
| 1984 | obj->hdr_chunk = 0; /* Not a valid chunk. */ |
| 1985 | return obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1986 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1987 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1988 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1989 | static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev) |
| 1990 | { |
| 1991 | int i; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1992 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1993 | dev->n_obj = 0; |
| 1994 | dev->n_tnodes = 0; |
| 1995 | yaffs_init_raw_tnodes_and_objs(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1996 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 1997 | for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { |
| 1998 | INIT_LIST_HEAD(&dev->obj_bucket[i].list); |
| 1999 | dev->obj_bucket[i].count = 0; |
| 2000 | } |
| 2001 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2002 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2003 | struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev, |
| 2004 | int number, |
| 2005 | enum yaffs_obj_type type) |
| 2006 | { |
| 2007 | struct yaffs_obj *the_obj = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2008 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2009 | if (number > 0) |
| 2010 | the_obj = yaffs_find_by_number(dev, number); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2011 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2012 | if (!the_obj) |
| 2013 | the_obj = yaffs_new_obj(dev, number, type); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2014 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2015 | return the_obj; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2016 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2017 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2018 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2019 | YCHAR *yaffs_clone_str(const YCHAR *str) |
| 2020 | { |
| 2021 | YCHAR *new_str = NULL; |
| 2022 | int len; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2023 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2024 | if (!str) |
| 2025 | str = _Y(""); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2026 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2027 | len = yaffs_strnlen(str, YAFFS_MAX_ALIAS_LENGTH); |
| 2028 | new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS); |
| 2029 | if (new_str) { |
| 2030 | yaffs_strncpy(new_str, str, len); |
| 2031 | new_str[len] = 0; |
| 2032 | } |
| 2033 | return new_str; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2034 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2035 | } |
| 2036 | /* |
| 2037 | *yaffs_update_parent() handles fixing a directories mtime and ctime when a new |
| 2038 | * link (ie. name) is created or deleted in the directory. |
| 2039 | * |
| 2040 | * ie. |
| 2041 | * create dir/a : update dir's mtime/ctime |
| 2042 | * rm dir/a: update dir's mtime/ctime |
| 2043 | * modify dir/a: don't update dir's mtimme/ctime |
| 2044 | * |
| 2045 | * This can be handled immediately or defered. Defering helps reduce the number |
| 2046 | * of updates when many files in a directory are changed within a brief period. |
| 2047 | * |
| 2048 | * If the directory updating is defered then yaffs_update_dirty_dirs must be |
| 2049 | * called periodically. |
| 2050 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2051 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2052 | static void yaffs_update_parent(struct yaffs_obj *obj) |
| 2053 | { |
| 2054 | struct yaffs_dev *dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2055 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2056 | if (!obj) |
| 2057 | return; |
| 2058 | dev = obj->my_dev; |
| 2059 | obj->dirty = 1; |
| 2060 | yaffs_load_current_time(obj, 0, 1); |
| 2061 | if (dev->param.defered_dir_update) { |
| 2062 | struct list_head *link = &obj->variant.dir_variant.dirty; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2063 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2064 | if (list_empty(link)) { |
| 2065 | list_add(link, &dev->dirty_dirs); |
| 2066 | yaffs_trace(YAFFS_TRACE_BACKGROUND, |
| 2067 | "Added object %d to dirty directories", |
| 2068 | obj->obj_id); |
| 2069 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2070 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2071 | } else { |
| 2072 | yaffs_update_oh(obj, NULL, 0, 0, 0, NULL); |
| 2073 | } |
| 2074 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2075 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2076 | void yaffs_update_dirty_dirs(struct yaffs_dev *dev) |
| 2077 | { |
| 2078 | struct list_head *link; |
| 2079 | struct yaffs_obj *obj; |
| 2080 | struct yaffs_dir_var *d_s; |
| 2081 | union yaffs_obj_var *o_v; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2082 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2083 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2084 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2085 | while (!list_empty(&dev->dirty_dirs)) { |
| 2086 | link = dev->dirty_dirs.next; |
| 2087 | list_del_init(link); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2088 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2089 | d_s = list_entry(link, struct yaffs_dir_var, dirty); |
| 2090 | o_v = list_entry(d_s, union yaffs_obj_var, dir_variant); |
| 2091 | obj = list_entry(o_v, struct yaffs_obj, variant); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2092 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2093 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d", |
| 2094 | obj->obj_id); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2095 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2096 | if (obj->dirty) |
| 2097 | yaffs_update_oh(obj, NULL, 0, 0, 0, NULL); |
| 2098 | } |
| 2099 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2100 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2101 | /* |
| 2102 | * Mknod (create) a new object. |
| 2103 | * equiv_obj only has meaning for a hard link; |
| 2104 | * alias_str only has meaning for a symlink. |
| 2105 | * rdev only has meaning for devices (a subset of special objects) |
| 2106 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2107 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2108 | static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type, |
| 2109 | struct yaffs_obj *parent, |
| 2110 | const YCHAR *name, |
| 2111 | u32 mode, |
| 2112 | u32 uid, |
| 2113 | u32 gid, |
| 2114 | struct yaffs_obj *equiv_obj, |
| 2115 | const YCHAR *alias_str, u32 rdev) |
| 2116 | { |
| 2117 | struct yaffs_obj *in; |
| 2118 | YCHAR *str = NULL; |
| 2119 | struct yaffs_dev *dev = parent->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2120 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2121 | /* Check if the entry exists. |
| 2122 | * If it does then fail the call since we don't want a dup. */ |
| 2123 | if (yaffs_find_by_name(parent, name)) |
| 2124 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2125 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2126 | if (type == YAFFS_OBJECT_TYPE_SYMLINK) { |
| 2127 | str = yaffs_clone_str(alias_str); |
| 2128 | if (!str) |
| 2129 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2132 | in = yaffs_new_obj(dev, -1, type); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2133 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2134 | if (!in) { |
| 2135 | kfree(str); |
| 2136 | return NULL; |
| 2137 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2138 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2139 | in->hdr_chunk = 0; |
| 2140 | in->valid = 1; |
| 2141 | in->variant_type = type; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2142 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2143 | in->yst_mode = mode; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2144 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2145 | yaffs_attribs_init(in, gid, uid, rdev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2146 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2147 | in->n_data_chunks = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2148 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2149 | yaffs_set_obj_name(in, name); |
| 2150 | in->dirty = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2151 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2152 | yaffs_add_obj_to_dir(parent, in); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2153 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2154 | in->my_dev = parent->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2155 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2156 | switch (type) { |
| 2157 | case YAFFS_OBJECT_TYPE_SYMLINK: |
| 2158 | in->variant.symlink_variant.alias = str; |
| 2159 | break; |
| 2160 | case YAFFS_OBJECT_TYPE_HARDLINK: |
| 2161 | in->variant.hardlink_variant.equiv_obj = equiv_obj; |
| 2162 | in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id; |
| 2163 | list_add(&in->hard_links, &equiv_obj->hard_links); |
| 2164 | break; |
| 2165 | case YAFFS_OBJECT_TYPE_FILE: |
| 2166 | case YAFFS_OBJECT_TYPE_DIRECTORY: |
| 2167 | case YAFFS_OBJECT_TYPE_SPECIAL: |
| 2168 | case YAFFS_OBJECT_TYPE_UNKNOWN: |
| 2169 | /* do nothing */ |
| 2170 | break; |
| 2171 | } |
| 2172 | |
| 2173 | if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) { |
| 2174 | /* Could not create the object header, fail */ |
| 2175 | yaffs_del_obj(in); |
| 2176 | in = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2179 | if (in) |
| 2180 | yaffs_update_parent(parent); |
| 2181 | |
| 2182 | return in; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2185 | struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, |
| 2186 | const YCHAR *name, u32 mode, u32 uid, |
| 2187 | u32 gid) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2188 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2189 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode, |
| 2190 | uid, gid, NULL, NULL, 0); |
| 2191 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2192 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2193 | struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name, |
| 2194 | u32 mode, u32 uid, u32 gid) |
| 2195 | { |
| 2196 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name, |
| 2197 | mode, uid, gid, NULL, NULL, 0); |
| 2198 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2199 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2200 | struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, |
| 2201 | const YCHAR *name, u32 mode, u32 uid, |
| 2202 | u32 gid, u32 rdev) |
| 2203 | { |
| 2204 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode, |
| 2205 | uid, gid, NULL, NULL, rdev); |
| 2206 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2207 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2208 | struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, |
| 2209 | const YCHAR *name, u32 mode, u32 uid, |
| 2210 | u32 gid, const YCHAR *alias) |
| 2211 | { |
| 2212 | return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode, |
| 2213 | uid, gid, NULL, alias, 0); |
| 2214 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2215 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2216 | /* yaffs_link_obj returns the object id of the equivalent object.*/ |
| 2217 | struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name, |
| 2218 | struct yaffs_obj *equiv_obj) |
| 2219 | { |
| 2220 | /* Get the real object in case we were fed a hard link obj */ |
| 2221 | equiv_obj = yaffs_get_equivalent_obj(equiv_obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2222 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2223 | if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK, |
| 2224 | parent, name, 0, 0, 0, |
| 2225 | equiv_obj, NULL, 0)) |
| 2226 | return equiv_obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2227 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2228 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2229 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2230 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2231 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2232 | /*---------------------- Block Management and Page Allocation -------------*/ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2233 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2234 | static void yaffs_deinit_blocks(struct yaffs_dev *dev) |
| 2235 | { |
| 2236 | if (dev->block_info_alt && dev->block_info) |
| 2237 | vfree(dev->block_info); |
| 2238 | else |
| 2239 | kfree(dev->block_info); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2240 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2241 | dev->block_info_alt = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2242 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2243 | dev->block_info = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2244 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2245 | if (dev->chunk_bits_alt && dev->chunk_bits) |
| 2246 | vfree(dev->chunk_bits); |
| 2247 | else |
| 2248 | kfree(dev->chunk_bits); |
| 2249 | dev->chunk_bits_alt = 0; |
| 2250 | dev->chunk_bits = NULL; |
| 2251 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2252 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2253 | static int yaffs_init_blocks(struct yaffs_dev *dev) |
| 2254 | { |
| 2255 | int n_blocks = dev->internal_end_block - dev->internal_start_block + 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2256 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2257 | dev->block_info = NULL; |
| 2258 | dev->chunk_bits = NULL; |
| 2259 | dev->alloc_block = -1; /* force it to get a new one */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2260 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2261 | /* If the first allocation strategy fails, thry the alternate one */ |
| 2262 | dev->block_info = |
| 2263 | kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS); |
| 2264 | if (!dev->block_info) { |
| 2265 | dev->block_info = |
| 2266 | vmalloc(n_blocks * sizeof(struct yaffs_block_info)); |
| 2267 | dev->block_info_alt = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2268 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2269 | dev->block_info_alt = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2272 | if (!dev->block_info) |
| 2273 | goto alloc_error; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2274 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2275 | /* Set up dynamic blockinfo stuff. Round up bytes. */ |
| 2276 | dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; |
| 2277 | dev->chunk_bits = |
| 2278 | kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS); |
| 2279 | if (!dev->chunk_bits) { |
| 2280 | dev->chunk_bits = |
| 2281 | vmalloc(dev->chunk_bit_stride * n_blocks); |
| 2282 | dev->chunk_bits_alt = 1; |
| 2283 | } else { |
| 2284 | dev->chunk_bits_alt = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2285 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2286 | if (!dev->chunk_bits) |
| 2287 | goto alloc_error; |
| 2288 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2289 | memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info)); |
| 2290 | memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2291 | return YAFFS_OK; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2292 | |
| 2293 | alloc_error: |
| 2294 | yaffs_deinit_blocks(dev); |
| 2295 | return YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2296 | } |
| 2297 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2298 | void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2299 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2300 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no); |
| 2301 | int erased_ok = 0; |
| 2302 | int i; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2303 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2304 | /* If the block is still healthy erase it and mark as clean. |
| 2305 | * If the block has had a data failure, then retire it. |
| 2306 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2307 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2308 | yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE, |
| 2309 | "yaffs_block_became_dirty block %d state %d %s", |
| 2310 | block_no, bi->block_state, |
| 2311 | (bi->needs_retiring) ? "needs retiring" : ""); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2312 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2313 | yaffs2_clear_oldest_dirty_seq(dev, bi); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2314 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2315 | bi->block_state = YAFFS_BLOCK_STATE_DIRTY; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2316 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2317 | /* If this is the block being garbage collected then stop gc'ing */ |
| 2318 | if (block_no == dev->gc_block) |
| 2319 | dev->gc_block = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2320 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2321 | /* If this block is currently the best candidate for gc |
| 2322 | * then drop as a candidate */ |
| 2323 | if (block_no == dev->gc_dirtiest) { |
| 2324 | dev->gc_dirtiest = 0; |
| 2325 | dev->gc_pages_in_use = 0; |
| 2326 | } |
| 2327 | |
| 2328 | if (!bi->needs_retiring) { |
| 2329 | yaffs2_checkpt_invalidate(dev); |
| 2330 | erased_ok = yaffs_erase_block(dev, block_no); |
| 2331 | if (!erased_ok) { |
| 2332 | dev->n_erase_failures++; |
| 2333 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, |
| 2334 | "**>> Erasure failed %d", block_no); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2335 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2336 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2337 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2338 | /* Verify erasure if needed */ |
| 2339 | if (erased_ok && |
| 2340 | ((yaffs_trace_mask & YAFFS_TRACE_ERASE) || |
| 2341 | !yaffs_skip_verification(dev))) { |
| 2342 | for (i = 0; i < dev->param.chunks_per_block; i++) { |
| 2343 | if (!yaffs_check_chunk_erased(dev, |
| 2344 | block_no * dev->param.chunks_per_block + i)) { |
| 2345 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 2346 | ">>Block %d erasure supposedly OK, but chunk %d not erased", |
| 2347 | block_no, i); |
| 2348 | } |
| 2349 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2352 | if (!erased_ok) { |
| 2353 | /* We lost a block of free space */ |
| 2354 | dev->n_free_chunks -= dev->param.chunks_per_block; |
| 2355 | yaffs_retire_block(dev, block_no); |
| 2356 | yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, |
| 2357 | "**>> Block %d retired", block_no); |
| 2358 | return; |
| 2359 | } |
| 2360 | |
| 2361 | /* Clean it up... */ |
| 2362 | bi->block_state = YAFFS_BLOCK_STATE_EMPTY; |
| 2363 | bi->seq_number = 0; |
| 2364 | dev->n_erased_blocks++; |
| 2365 | bi->pages_in_use = 0; |
| 2366 | bi->soft_del_pages = 0; |
| 2367 | bi->has_shrink_hdr = 0; |
| 2368 | bi->skip_erased_check = 1; /* Clean, so no need to check */ |
| 2369 | bi->gc_prioritise = 0; |
| 2370 | bi->has_summary = 0; |
| 2371 | |
| 2372 | yaffs_clear_chunk_bits(dev, block_no); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2373 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2374 | yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2377 | static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev, |
| 2378 | struct yaffs_block_info *bi, |
| 2379 | int old_chunk, u8 *buffer) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2380 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2381 | int new_chunk; |
| 2382 | int mark_flash = 1; |
| 2383 | struct yaffs_ext_tags tags; |
| 2384 | struct yaffs_obj *object; |
| 2385 | int matching_chunk; |
| 2386 | int ret_val = YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2387 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2388 | memset(&tags, 0, sizeof(tags)); |
| 2389 | yaffs_rd_chunk_tags_nand(dev, old_chunk, |
| 2390 | buffer, &tags); |
| 2391 | object = yaffs_find_by_number(dev, tags.obj_id); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2392 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2393 | yaffs_trace(YAFFS_TRACE_GC_DETAIL, |
| 2394 | "Collecting chunk in block %d, %d %d %d ", |
| 2395 | dev->gc_chunk, tags.obj_id, |
| 2396 | tags.chunk_id, tags.n_bytes); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2397 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2398 | if (object && !yaffs_skip_verification(dev)) { |
| 2399 | if (tags.chunk_id == 0) |
| 2400 | matching_chunk = |
| 2401 | object->hdr_chunk; |
| 2402 | else if (object->soft_del) |
| 2403 | /* Defeat the test */ |
| 2404 | matching_chunk = old_chunk; |
| 2405 | else |
| 2406 | matching_chunk = |
| 2407 | yaffs_find_chunk_in_file |
| 2408 | (object, tags.chunk_id, |
| 2409 | NULL); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2410 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2411 | if (old_chunk != matching_chunk) |
| 2412 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 2413 | "gc: page in gc mismatch: %d %d %d %d", |
| 2414 | old_chunk, |
| 2415 | matching_chunk, |
| 2416 | tags.obj_id, |
| 2417 | tags.chunk_id); |
| 2418 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2419 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2420 | if (!object) { |
| 2421 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 2422 | "page %d in gc has no object: %d %d %d ", |
| 2423 | old_chunk, |
| 2424 | tags.obj_id, tags.chunk_id, |
| 2425 | tags.n_bytes); |
| 2426 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2427 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2428 | if (object && |
| 2429 | object->deleted && |
| 2430 | object->soft_del && tags.chunk_id != 0) { |
| 2431 | /* Data chunk in a soft deleted file, |
| 2432 | * throw it away. |
| 2433 | * It's a soft deleted data chunk, |
| 2434 | * No need to copy this, just forget |
| 2435 | * about it and fix up the object. |
| 2436 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2437 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2438 | /* Free chunks already includes |
| 2439 | * softdeleted chunks, how ever this |
| 2440 | * chunk is going to soon be really |
| 2441 | * deleted which will increment free |
| 2442 | * chunks. We have to decrement free |
| 2443 | * chunks so this works out properly. |
| 2444 | */ |
| 2445 | dev->n_free_chunks--; |
| 2446 | bi->soft_del_pages--; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2447 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2448 | object->n_data_chunks--; |
| 2449 | if (object->n_data_chunks <= 0) { |
| 2450 | /* remeber to clean up obj */ |
| 2451 | dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id; |
| 2452 | dev->n_clean_ups++; |
| 2453 | } |
| 2454 | mark_flash = 0; |
| 2455 | } else if (object) { |
| 2456 | /* It's either a data chunk in a live |
| 2457 | * file or an ObjectHeader, so we're |
| 2458 | * interested in it. |
| 2459 | * NB Need to keep the ObjectHeaders of |
| 2460 | * deleted files until the whole file |
| 2461 | * has been deleted off |
| 2462 | */ |
| 2463 | tags.serial_number++; |
| 2464 | dev->n_gc_copies++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2465 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2466 | if (tags.chunk_id == 0) { |
| 2467 | /* It is an object Id, |
| 2468 | * We need to nuke the |
| 2469 | * shrinkheader flags since its |
| 2470 | * work is done. |
| 2471 | * Also need to clean up |
| 2472 | * shadowing. |
| 2473 | */ |
| 2474 | struct yaffs_obj_hdr *oh; |
| 2475 | oh = (struct yaffs_obj_hdr *) buffer; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2476 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2477 | oh->is_shrink = 0; |
| 2478 | tags.extra_is_shrink = 0; |
| 2479 | oh->shadows_obj = 0; |
| 2480 | oh->inband_shadowed_obj_id = 0; |
| 2481 | tags.extra_shadows = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2482 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2483 | /* Update file size */ |
| 2484 | if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) { |
| 2485 | yaffs_oh_size_load(oh, |
| 2486 | object->variant.file_variant.file_size); |
| 2487 | tags.extra_file_size = |
| 2488 | object->variant.file_variant.file_size; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2489 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2490 | |
| 2491 | yaffs_verify_oh(object, oh, &tags, 1); |
| 2492 | new_chunk = |
| 2493 | yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2494 | } else { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2495 | new_chunk = |
| 2496 | yaffs_write_new_chunk(dev, buffer, &tags, 1); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2497 | } |
| 2498 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2499 | if (new_chunk < 0) { |
| 2500 | ret_val = YAFFS_FAIL; |
| 2501 | } else { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2502 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2503 | /* Now fix up the Tnodes etc. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2504 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2505 | if (tags.chunk_id == 0) { |
| 2506 | /* It's a header */ |
| 2507 | object->hdr_chunk = new_chunk; |
| 2508 | object->serial = tags.serial_number; |
| 2509 | } else { |
| 2510 | /* It's a data chunk */ |
| 2511 | yaffs_put_chunk_in_file(object, tags.chunk_id, |
| 2512 | new_chunk, 0); |
| 2513 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2514 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2515 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2516 | if (ret_val == YAFFS_OK) |
| 2517 | yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__); |
| 2518 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2519 | } |
| 2520 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2521 | static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2522 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2523 | int old_chunk; |
| 2524 | int ret_val = YAFFS_OK; |
| 2525 | int i; |
| 2526 | int is_checkpt_block; |
| 2527 | int max_copies; |
| 2528 | int chunks_before = yaffs_get_erased_chunks(dev); |
| 2529 | int chunks_after; |
| 2530 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, block); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2531 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2532 | is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2533 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2534 | yaffs_trace(YAFFS_TRACE_TRACING, |
| 2535 | "Collecting block %d, in use %d, shrink %d, whole_block %d", |
| 2536 | block, bi->pages_in_use, bi->has_shrink_hdr, |
| 2537 | whole_block); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2538 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2539 | /*yaffs_verify_free_chunks(dev); */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2540 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2541 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL) |
| 2542 | bi->block_state = YAFFS_BLOCK_STATE_COLLECTING; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2543 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2544 | bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2545 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2546 | dev->gc_disable = 1; |
| 2547 | |
| 2548 | yaffs_summary_gc(dev, block); |
| 2549 | |
| 2550 | if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) { |
| 2551 | yaffs_trace(YAFFS_TRACE_TRACING, |
| 2552 | "Collecting block %d that has no chunks in use", |
| 2553 | block); |
| 2554 | yaffs_block_became_dirty(dev, block); |
| 2555 | } else { |
| 2556 | |
| 2557 | u8 *buffer = yaffs_get_temp_buffer(dev); |
| 2558 | |
| 2559 | yaffs_verify_blk(dev, bi, block); |
| 2560 | |
| 2561 | max_copies = (whole_block) ? dev->param.chunks_per_block : 5; |
| 2562 | old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk; |
| 2563 | |
| 2564 | for (/* init already done */ ; |
| 2565 | ret_val == YAFFS_OK && |
| 2566 | dev->gc_chunk < dev->param.chunks_per_block && |
| 2567 | (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) && |
| 2568 | max_copies > 0; |
| 2569 | dev->gc_chunk++, old_chunk++) { |
| 2570 | if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) { |
| 2571 | /* Page is in use and might need to be copied */ |
| 2572 | max_copies--; |
| 2573 | ret_val = yaffs_gc_process_chunk(dev, bi, |
| 2574 | old_chunk, buffer); |
| 2575 | } |
| 2576 | } |
| 2577 | yaffs_release_temp_buffer(dev, buffer); |
| 2578 | } |
| 2579 | |
| 2580 | yaffs_verify_collected_blk(dev, bi, block); |
| 2581 | |
| 2582 | if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { |
| 2583 | /* |
| 2584 | * The gc did not complete. Set block state back to FULL |
| 2585 | * because checkpointing does not restore gc. |
| 2586 | */ |
| 2587 | bi->block_state = YAFFS_BLOCK_STATE_FULL; |
| 2588 | } else { |
| 2589 | /* The gc completed. */ |
| 2590 | /* Do any required cleanups */ |
| 2591 | for (i = 0; i < dev->n_clean_ups; i++) { |
| 2592 | /* Time to delete the file too */ |
| 2593 | struct yaffs_obj *object = |
| 2594 | yaffs_find_by_number(dev, dev->gc_cleanup_list[i]); |
| 2595 | if (object) { |
| 2596 | yaffs_free_tnode(dev, |
| 2597 | object->variant.file_variant.top); |
| 2598 | object->variant.file_variant.top = NULL; |
| 2599 | yaffs_trace(YAFFS_TRACE_GC, |
| 2600 | "yaffs: About to finally delete object %d", |
| 2601 | object->obj_id); |
| 2602 | yaffs_generic_obj_del(object); |
| 2603 | object->my_dev->n_deleted_files--; |
| 2604 | } |
| 2605 | |
| 2606 | } |
| 2607 | chunks_after = yaffs_get_erased_chunks(dev); |
| 2608 | if (chunks_before >= chunks_after) |
| 2609 | yaffs_trace(YAFFS_TRACE_GC, |
| 2610 | "gc did not increase free chunks before %d after %d", |
| 2611 | chunks_before, chunks_after); |
| 2612 | dev->gc_block = 0; |
| 2613 | dev->gc_chunk = 0; |
| 2614 | dev->n_clean_ups = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2615 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2616 | |
| 2617 | dev->gc_disable = 0; |
| 2618 | |
| 2619 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2620 | } |
| 2621 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2622 | /* |
| 2623 | * find_gc_block() selects the dirtiest block (or close enough) |
| 2624 | * for garbage collection. |
| 2625 | */ |
| 2626 | |
| 2627 | static unsigned yaffs_find_gc_block(struct yaffs_dev *dev, |
| 2628 | int aggressive, int background) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2629 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2630 | int i; |
| 2631 | int iterations; |
| 2632 | unsigned selected = 0; |
| 2633 | int prioritised = 0; |
| 2634 | int prioritised_exist = 0; |
| 2635 | struct yaffs_block_info *bi; |
| 2636 | int threshold; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2637 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2638 | /* First let's see if we need to grab a prioritised block */ |
| 2639 | if (dev->has_pending_prioritised_gc && !aggressive) { |
| 2640 | dev->gc_dirtiest = 0; |
| 2641 | bi = dev->block_info; |
| 2642 | for (i = dev->internal_start_block; |
| 2643 | i <= dev->internal_end_block && !selected; i++) { |
| 2644 | |
| 2645 | if (bi->gc_prioritise) { |
| 2646 | prioritised_exist = 1; |
| 2647 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL && |
| 2648 | yaffs_block_ok_for_gc(dev, bi)) { |
| 2649 | selected = i; |
| 2650 | prioritised = 1; |
| 2651 | } |
| 2652 | } |
| 2653 | bi++; |
| 2654 | } |
| 2655 | |
| 2656 | /* |
| 2657 | * If there is a prioritised block and none was selected then |
| 2658 | * this happened because there is at least one old dirty block |
| 2659 | * gumming up the works. Let's gc the oldest dirty block. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2660 | */ |
| 2661 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2662 | if (prioritised_exist && |
| 2663 | !selected && dev->oldest_dirty_block > 0) |
| 2664 | selected = dev->oldest_dirty_block; |
| 2665 | |
| 2666 | if (!prioritised_exist) /* None found, so we can clear this */ |
| 2667 | dev->has_pending_prioritised_gc = 0; |
| 2668 | } |
| 2669 | |
| 2670 | /* If we're doing aggressive GC then we are happy to take a less-dirty |
| 2671 | * block, and search harder. |
| 2672 | * else (leasurely gc), then we only bother to do this if the |
| 2673 | * block has only a few pages in use. |
| 2674 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2675 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2676 | if (!selected) { |
| 2677 | int pages_used; |
| 2678 | int n_blocks = |
| 2679 | dev->internal_end_block - dev->internal_start_block + 1; |
| 2680 | if (aggressive) { |
| 2681 | threshold = dev->param.chunks_per_block; |
| 2682 | iterations = n_blocks; |
| 2683 | } else { |
| 2684 | int max_threshold; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2685 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2686 | if (background) |
| 2687 | max_threshold = dev->param.chunks_per_block / 2; |
| 2688 | else |
| 2689 | max_threshold = dev->param.chunks_per_block / 8; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2690 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2691 | if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD) |
| 2692 | max_threshold = YAFFS_GC_PASSIVE_THRESHOLD; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2693 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2694 | threshold = background ? (dev->gc_not_done + 2) * 2 : 0; |
| 2695 | if (threshold < YAFFS_GC_PASSIVE_THRESHOLD) |
| 2696 | threshold = YAFFS_GC_PASSIVE_THRESHOLD; |
| 2697 | if (threshold > max_threshold) |
| 2698 | threshold = max_threshold; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2699 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2700 | iterations = n_blocks / 16 + 1; |
| 2701 | if (iterations > 100) |
| 2702 | iterations = 100; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2703 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2704 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2705 | for (i = 0; |
| 2706 | i < iterations && |
| 2707 | (dev->gc_dirtiest < 1 || |
| 2708 | dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); |
| 2709 | i++) { |
| 2710 | dev->gc_block_finder++; |
| 2711 | if (dev->gc_block_finder < dev->internal_start_block || |
| 2712 | dev->gc_block_finder > dev->internal_end_block) |
| 2713 | dev->gc_block_finder = |
| 2714 | dev->internal_start_block; |
| 2715 | |
| 2716 | bi = yaffs_get_block_info(dev, dev->gc_block_finder); |
| 2717 | |
| 2718 | pages_used = bi->pages_in_use - bi->soft_del_pages; |
| 2719 | |
| 2720 | if (bi->block_state == YAFFS_BLOCK_STATE_FULL && |
| 2721 | pages_used < dev->param.chunks_per_block && |
| 2722 | (dev->gc_dirtiest < 1 || |
| 2723 | pages_used < dev->gc_pages_in_use) && |
| 2724 | yaffs_block_ok_for_gc(dev, bi)) { |
| 2725 | dev->gc_dirtiest = dev->gc_block_finder; |
| 2726 | dev->gc_pages_in_use = pages_used; |
| 2727 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2728 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2729 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2730 | if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold) |
| 2731 | selected = dev->gc_dirtiest; |
| 2732 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2733 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2734 | /* |
| 2735 | * If nothing has been selected for a while, try the oldest dirty |
| 2736 | * because that's gumming up the works. |
| 2737 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2738 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2739 | if (!selected && dev->param.is_yaffs2 && |
| 2740 | dev->gc_not_done >= (background ? 10 : 20)) { |
| 2741 | yaffs2_find_oldest_dirty_seq(dev); |
| 2742 | if (dev->oldest_dirty_block > 0) { |
| 2743 | selected = dev->oldest_dirty_block; |
| 2744 | dev->gc_dirtiest = selected; |
| 2745 | dev->oldest_dirty_gc_count++; |
| 2746 | bi = yaffs_get_block_info(dev, selected); |
| 2747 | dev->gc_pages_in_use = |
| 2748 | bi->pages_in_use - bi->soft_del_pages; |
| 2749 | } else { |
| 2750 | dev->gc_not_done = 0; |
| 2751 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2752 | } |
| 2753 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2754 | if (selected) { |
| 2755 | yaffs_trace(YAFFS_TRACE_GC, |
| 2756 | "GC Selected block %d with %d free, prioritised:%d", |
| 2757 | selected, |
| 2758 | dev->param.chunks_per_block - dev->gc_pages_in_use, |
| 2759 | prioritised); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2760 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2761 | dev->n_gc_blocks++; |
| 2762 | if (background) |
| 2763 | dev->bg_gcs++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2764 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2765 | dev->gc_dirtiest = 0; |
| 2766 | dev->gc_pages_in_use = 0; |
| 2767 | dev->gc_not_done = 0; |
| 2768 | if (dev->refresh_skip > 0) |
| 2769 | dev->refresh_skip--; |
| 2770 | } else { |
| 2771 | dev->gc_not_done++; |
| 2772 | yaffs_trace(YAFFS_TRACE_GC, |
| 2773 | "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s", |
| 2774 | dev->gc_block_finder, dev->gc_not_done, threshold, |
| 2775 | dev->gc_dirtiest, dev->gc_pages_in_use, |
| 2776 | dev->oldest_dirty_block, background ? " bg" : ""); |
| 2777 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2778 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2779 | return selected; |
| 2780 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2781 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2782 | /* New garbage collector |
| 2783 | * If we're very low on erased blocks then we do aggressive garbage collection |
| 2784 | * otherwise we do "leasurely" garbage collection. |
| 2785 | * Aggressive gc looks further (whole array) and will accept less dirty blocks. |
| 2786 | * Passive gc only inspects smaller areas and only accepts more dirty blocks. |
| 2787 | * |
| 2788 | * The idea is to help clear out space in a more spread-out manner. |
| 2789 | * Dunno if it really does anything useful. |
| 2790 | */ |
| 2791 | static int yaffs_check_gc(struct yaffs_dev *dev, int background) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2792 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2793 | int aggressive = 0; |
| 2794 | int gc_ok = YAFFS_OK; |
| 2795 | int max_tries = 0; |
| 2796 | int min_erased; |
| 2797 | int erased_chunks; |
| 2798 | int checkpt_block_adjust; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2799 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2800 | if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0) |
| 2801 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2802 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2803 | if (dev->gc_disable) |
| 2804 | /* Bail out so we don't get recursive gc */ |
| 2805 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2806 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2807 | /* This loop should pass the first time. |
| 2808 | * Only loops here if the collection does not increase space. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2809 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2810 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2811 | do { |
| 2812 | max_tries++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2813 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2814 | checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2815 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2816 | min_erased = |
| 2817 | dev->param.n_reserved_blocks + checkpt_block_adjust + 1; |
| 2818 | erased_chunks = |
| 2819 | dev->n_erased_blocks * dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2820 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2821 | /* If we need a block soon then do aggressive gc. */ |
| 2822 | if (dev->n_erased_blocks < min_erased) |
| 2823 | aggressive = 1; |
| 2824 | else { |
| 2825 | if (!background |
| 2826 | && erased_chunks > (dev->n_free_chunks / 4)) |
| 2827 | break; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2828 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2829 | if (dev->gc_skip > 20) |
| 2830 | dev->gc_skip = 20; |
| 2831 | if (erased_chunks < dev->n_free_chunks / 2 || |
| 2832 | dev->gc_skip < 1 || background) |
| 2833 | aggressive = 0; |
| 2834 | else { |
| 2835 | dev->gc_skip--; |
| 2836 | break; |
| 2837 | } |
| 2838 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2839 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2840 | dev->gc_skip = 5; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2841 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2842 | /* If we don't already have a block being gc'd then see if we |
| 2843 | * should start another */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2844 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2845 | if (dev->gc_block < 1 && !aggressive) { |
| 2846 | dev->gc_block = yaffs2_find_refresh_block(dev); |
| 2847 | dev->gc_chunk = 0; |
| 2848 | dev->n_clean_ups = 0; |
| 2849 | } |
| 2850 | if (dev->gc_block < 1) { |
| 2851 | dev->gc_block = |
| 2852 | yaffs_find_gc_block(dev, aggressive, background); |
| 2853 | dev->gc_chunk = 0; |
| 2854 | dev->n_clean_ups = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2855 | } |
| 2856 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2857 | if (dev->gc_block > 0) { |
| 2858 | dev->all_gcs++; |
| 2859 | if (!aggressive) |
| 2860 | dev->passive_gc_count++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2861 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2862 | yaffs_trace(YAFFS_TRACE_GC, |
| 2863 | "yaffs: GC n_erased_blocks %d aggressive %d", |
| 2864 | dev->n_erased_blocks, aggressive); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2865 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2866 | gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive); |
| 2867 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2868 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2869 | if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) && |
| 2870 | dev->gc_block > 0) { |
| 2871 | yaffs_trace(YAFFS_TRACE_GC, |
| 2872 | "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d", |
| 2873 | dev->n_erased_blocks, max_tries, |
| 2874 | dev->gc_block); |
| 2875 | } |
| 2876 | } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) && |
| 2877 | (dev->gc_block > 0) && (max_tries < 2)); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2878 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2879 | return aggressive ? gc_ok : YAFFS_OK; |
| 2880 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2881 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2882 | /* |
| 2883 | * yaffs_bg_gc() |
| 2884 | * Garbage collects. Intended to be called from a background thread. |
| 2885 | * Returns non-zero if at least half the free chunks are erased. |
| 2886 | */ |
| 2887 | int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency) |
| 2888 | { |
| 2889 | int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2890 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2891 | yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2892 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2893 | yaffs_check_gc(dev, 1); |
| 2894 | return erased_chunks > dev->n_free_chunks / 2; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2895 | } |
| 2896 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2897 | /*-------------------- Data file manipulation -----------------*/ |
| 2898 | |
| 2899 | static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2900 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2901 | int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2902 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2903 | if (nand_chunk >= 0) |
| 2904 | return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk, |
| 2905 | buffer, NULL); |
| 2906 | else { |
| 2907 | yaffs_trace(YAFFS_TRACE_NANDACCESS, |
| 2908 | "Chunk %d not found zero instead", |
| 2909 | nand_chunk); |
| 2910 | /* get sane (zero) data if you read a hole */ |
| 2911 | memset(buffer, 0, in->my_dev->data_bytes_per_chunk); |
| 2912 | return 0; |
| 2913 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2914 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2915 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2916 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2917 | void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, |
| 2918 | int lyn) |
| 2919 | { |
| 2920 | int block; |
| 2921 | int page; |
| 2922 | struct yaffs_ext_tags tags; |
| 2923 | struct yaffs_block_info *bi; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2924 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2925 | if (chunk_id <= 0) |
| 2926 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2927 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2928 | dev->n_deletions++; |
| 2929 | block = chunk_id / dev->param.chunks_per_block; |
| 2930 | page = chunk_id % dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2931 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2932 | if (!yaffs_check_chunk_bit(dev, block, page)) |
| 2933 | yaffs_trace(YAFFS_TRACE_VERIFY, |
| 2934 | "Deleting invalid chunk %d", chunk_id); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2935 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2936 | bi = yaffs_get_block_info(dev, block); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 2937 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2938 | yaffs2_update_oldest_dirty_seq(dev, block, bi); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2939 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2940 | yaffs_trace(YAFFS_TRACE_DELETION, |
| 2941 | "line %d delete of chunk %d", |
| 2942 | lyn, chunk_id); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2943 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2944 | if (!dev->param.is_yaffs2 && mark_flash && |
| 2945 | bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2946 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2947 | memset(&tags, 0, sizeof(tags)); |
| 2948 | tags.is_deleted = 1; |
| 2949 | yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags); |
| 2950 | yaffs_handle_chunk_update(dev, chunk_id, &tags); |
| 2951 | } else { |
| 2952 | dev->n_unmarked_deletions++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2953 | } |
| 2954 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2955 | /* Pull out of the management area. |
| 2956 | * If the whole block became dirty, this will kick off an erasure. |
| 2957 | */ |
| 2958 | if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING || |
| 2959 | bi->block_state == YAFFS_BLOCK_STATE_FULL || |
| 2960 | bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN || |
| 2961 | bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { |
| 2962 | dev->n_free_chunks++; |
| 2963 | yaffs_clear_chunk_bit(dev, block, page); |
| 2964 | bi->pages_in_use--; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2965 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2966 | if (bi->pages_in_use == 0 && |
| 2967 | !bi->has_shrink_hdr && |
| 2968 | bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING && |
| 2969 | bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) { |
| 2970 | yaffs_block_became_dirty(dev, block); |
| 2971 | } |
| 2972 | } |
| 2973 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2974 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2975 | static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, |
| 2976 | const u8 *buffer, int n_bytes, int use_reserve) |
| 2977 | { |
| 2978 | /* Find old chunk Need to do this to get serial number |
| 2979 | * Write new one and patch into tree. |
| 2980 | * Invalidate old tags. |
| 2981 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2982 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2983 | int prev_chunk_id; |
| 2984 | struct yaffs_ext_tags prev_tags; |
| 2985 | int new_chunk_id; |
| 2986 | struct yaffs_ext_tags new_tags; |
| 2987 | struct yaffs_dev *dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2988 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2989 | yaffs_check_gc(dev, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2990 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 2991 | /* Get the previous chunk at this location in the file if it exists. |
| 2992 | * If it does not exist then put a zero into the tree. This creates |
| 2993 | * the tnode now, rather than later when it is harder to clean up. |
| 2994 | */ |
| 2995 | prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags); |
| 2996 | if (prev_chunk_id < 1 && |
| 2997 | !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0)) |
| 2998 | return 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 2999 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3000 | /* Set up new tags */ |
| 3001 | memset(&new_tags, 0, sizeof(new_tags)); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3002 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3003 | new_tags.chunk_id = inode_chunk; |
| 3004 | new_tags.obj_id = in->obj_id; |
| 3005 | new_tags.serial_number = |
| 3006 | (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1; |
| 3007 | new_tags.n_bytes = n_bytes; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3008 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3009 | if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) { |
| 3010 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 3011 | "Writing %d bytes to chunk!!!!!!!!!", |
| 3012 | n_bytes); |
| 3013 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3014 | } |
| 3015 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3016 | new_chunk_id = |
| 3017 | yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3018 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3019 | if (new_chunk_id > 0) { |
| 3020 | yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3021 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3022 | if (prev_chunk_id > 0) |
| 3023 | yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__); |
| 3024 | |
| 3025 | yaffs_verify_file_sane(in); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3026 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3027 | return new_chunk_id; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3028 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3029 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3030 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3031 | static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set, |
| 3032 | const YCHAR *name, const void *value, int size, |
| 3033 | int flags) |
| 3034 | { |
| 3035 | struct yaffs_xattr_mod xmod; |
| 3036 | int result; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3037 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3038 | xmod.set = set; |
| 3039 | xmod.name = name; |
| 3040 | xmod.data = value; |
| 3041 | xmod.size = size; |
| 3042 | xmod.flags = flags; |
| 3043 | xmod.result = -ENOSPC; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3044 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3045 | result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3046 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3047 | if (result > 0) |
| 3048 | return xmod.result; |
| 3049 | else |
| 3050 | return -ENOSPC; |
| 3051 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3052 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3053 | static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, |
| 3054 | struct yaffs_xattr_mod *xmod) |
| 3055 | { |
| 3056 | int retval = 0; |
| 3057 | int x_offs = sizeof(struct yaffs_obj_hdr); |
| 3058 | struct yaffs_dev *dev = obj->my_dev; |
| 3059 | int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr); |
| 3060 | char *x_buffer = buffer + x_offs; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3061 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3062 | if (xmod->set) |
| 3063 | retval = |
| 3064 | nval_set(x_buffer, x_size, xmod->name, xmod->data, |
| 3065 | xmod->size, xmod->flags); |
| 3066 | else |
| 3067 | retval = nval_del(x_buffer, x_size, xmod->name); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3068 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3069 | obj->has_xattr = nval_hasvalues(x_buffer, x_size); |
| 3070 | obj->xattr_known = 1; |
| 3071 | xmod->result = retval; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3072 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3073 | return retval; |
| 3074 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3075 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3076 | static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name, |
| 3077 | void *value, int size) |
| 3078 | { |
| 3079 | char *buffer = NULL; |
| 3080 | int result; |
| 3081 | struct yaffs_ext_tags tags; |
| 3082 | struct yaffs_dev *dev = obj->my_dev; |
| 3083 | int x_offs = sizeof(struct yaffs_obj_hdr); |
| 3084 | int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr); |
| 3085 | char *x_buffer; |
| 3086 | int retval = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3087 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3088 | if (obj->hdr_chunk < 1) |
| 3089 | return -ENODATA; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3090 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3091 | /* If we know that the object has no xattribs then don't do all the |
| 3092 | * reading and parsing. |
| 3093 | */ |
| 3094 | if (obj->xattr_known && !obj->has_xattr) { |
| 3095 | if (name) |
| 3096 | return -ENODATA; |
| 3097 | else |
| 3098 | return 0; |
| 3099 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3100 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3101 | buffer = (char *)yaffs_get_temp_buffer(dev); |
| 3102 | if (!buffer) |
| 3103 | return -ENOMEM; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3104 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3105 | result = |
| 3106 | yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3107 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3108 | if (result != YAFFS_OK) |
| 3109 | retval = -ENOENT; |
| 3110 | else { |
| 3111 | x_buffer = buffer + x_offs; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3112 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3113 | if (!obj->xattr_known) { |
| 3114 | obj->has_xattr = nval_hasvalues(x_buffer, x_size); |
| 3115 | obj->xattr_known = 1; |
| 3116 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3117 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3118 | if (name) |
| 3119 | retval = nval_get(x_buffer, x_size, name, value, size); |
| 3120 | else |
| 3121 | retval = nval_list(x_buffer, x_size, value, size); |
| 3122 | } |
| 3123 | yaffs_release_temp_buffer(dev, (u8 *) buffer); |
| 3124 | return retval; |
| 3125 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3126 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3127 | int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name, |
| 3128 | const void *value, int size, int flags) |
| 3129 | { |
| 3130 | return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags); |
| 3131 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3132 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3133 | int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name) |
| 3134 | { |
| 3135 | return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0); |
| 3136 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3137 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3138 | int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value, |
| 3139 | int size) |
| 3140 | { |
| 3141 | return yaffs_do_xattrib_fetch(obj, name, value, size); |
| 3142 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3143 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3144 | int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size) |
| 3145 | { |
| 3146 | return yaffs_do_xattrib_fetch(obj, NULL, buffer, size); |
| 3147 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3148 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3149 | static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) |
| 3150 | { |
| 3151 | u8 *buf; |
| 3152 | struct yaffs_obj_hdr *oh; |
| 3153 | struct yaffs_dev *dev; |
| 3154 | struct yaffs_ext_tags tags; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3155 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3156 | if (!in || !in->lazy_loaded || in->hdr_chunk < 1) |
| 3157 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3158 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3159 | dev = in->my_dev; |
| 3160 | in->lazy_loaded = 0; |
| 3161 | buf = yaffs_get_temp_buffer(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3162 | |
Anatolij Gustschin | 514c65c | 2012-10-05 23:31:03 +0000 | [diff] [blame] | 3163 | yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3164 | oh = (struct yaffs_obj_hdr *)buf; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3165 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3166 | in->yst_mode = oh->yst_mode; |
| 3167 | yaffs_load_attribs(in, oh); |
| 3168 | yaffs_set_obj_name_from_oh(in, oh); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3169 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3170 | if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { |
| 3171 | in->variant.symlink_variant.alias = |
| 3172 | yaffs_clone_str(oh->alias); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3173 | } |
| 3174 | yaffs_release_temp_buffer(dev, buf); |
| 3175 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3176 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3177 | static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name, |
| 3178 | const YCHAR *oh_name, int buff_size) |
| 3179 | { |
| 3180 | #ifdef CONFIG_YAFFS_AUTO_UNICODE |
| 3181 | if (dev->param.auto_unicode) { |
| 3182 | if (*oh_name) { |
| 3183 | /* It is an ASCII name, do an ASCII to |
| 3184 | * unicode conversion */ |
| 3185 | const char *ascii_oh_name = (const char *)oh_name; |
| 3186 | int n = buff_size - 1; |
| 3187 | while (n > 0 && *ascii_oh_name) { |
| 3188 | *name = *ascii_oh_name; |
| 3189 | name++; |
| 3190 | ascii_oh_name++; |
| 3191 | n--; |
| 3192 | } |
| 3193 | } else { |
| 3194 | yaffs_strncpy(name, oh_name + 1, buff_size - 1); |
| 3195 | } |
Jeroen Hofstee | 1990e04 | 2014-07-12 15:16:52 +0200 | [diff] [blame] | 3196 | |
| 3197 | return; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3198 | } |
Jeroen Hofstee | 1990e04 | 2014-07-12 15:16:52 +0200 | [diff] [blame] | 3199 | #endif |
| 3200 | |
| 3201 | yaffs_strncpy(name, oh_name, buff_size - 1); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name, |
| 3205 | const YCHAR *name) |
| 3206 | { |
| 3207 | #ifdef CONFIG_YAFFS_AUTO_UNICODE |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3208 | int is_ascii; |
| 3209 | YCHAR *w; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3210 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3211 | if (dev->param.auto_unicode) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3212 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3213 | is_ascii = 1; |
| 3214 | w = name; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3215 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3216 | /* Figure out if the name will fit in ascii character set */ |
| 3217 | while (is_ascii && *w) { |
| 3218 | if ((*w) & 0xff00) |
| 3219 | is_ascii = 0; |
| 3220 | w++; |
| 3221 | } |
| 3222 | |
| 3223 | if (is_ascii) { |
| 3224 | /* It is an ASCII name, so convert unicode to ascii */ |
| 3225 | char *ascii_oh_name = (char *)oh_name; |
| 3226 | int n = YAFFS_MAX_NAME_LENGTH - 1; |
| 3227 | while (n > 0 && *name) { |
| 3228 | *ascii_oh_name = *name; |
| 3229 | name++; |
| 3230 | ascii_oh_name++; |
| 3231 | n--; |
| 3232 | } |
| 3233 | } else { |
| 3234 | /* Unicode name, so save starting at the second YCHAR */ |
| 3235 | *oh_name = 0; |
| 3236 | yaffs_strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2); |
| 3237 | } |
Jeroen Hofstee | 1990e04 | 2014-07-12 15:16:52 +0200 | [diff] [blame] | 3238 | |
| 3239 | return; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3240 | } |
Jeroen Hofstee | 1990e04 | 2014-07-12 15:16:52 +0200 | [diff] [blame] | 3241 | #endif |
| 3242 | |
| 3243 | yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | /* UpdateObjectHeader updates the header on NAND for an object. |
| 3247 | * If name is not NULL, then that new name is used. |
| 3248 | */ |
| 3249 | int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force, |
| 3250 | int is_shrink, int shadows, struct yaffs_xattr_mod *xmod) |
| 3251 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3252 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3253 | struct yaffs_block_info *bi; |
| 3254 | struct yaffs_dev *dev = in->my_dev; |
| 3255 | int prev_chunk_id; |
| 3256 | int ret_val = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3257 | int new_chunk_id; |
| 3258 | struct yaffs_ext_tags new_tags; |
| 3259 | struct yaffs_ext_tags old_tags; |
| 3260 | const YCHAR *alias = NULL; |
| 3261 | u8 *buffer = NULL; |
| 3262 | YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1]; |
| 3263 | struct yaffs_obj_hdr *oh = NULL; |
| 3264 | loff_t file_size = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3265 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3266 | yaffs_strcpy(old_name, _Y("silly old name")); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3267 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3268 | if (in->fake && in != dev->root_dir && !force && !xmod) |
| 3269 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3270 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3271 | yaffs_check_gc(dev, 0); |
| 3272 | yaffs_check_obj_details_loaded(in); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3273 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3274 | buffer = yaffs_get_temp_buffer(in->my_dev); |
| 3275 | oh = (struct yaffs_obj_hdr *)buffer; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3276 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3277 | prev_chunk_id = in->hdr_chunk; |
| 3278 | |
| 3279 | if (prev_chunk_id > 0) { |
Anatolij Gustschin | 514c65c | 2012-10-05 23:31:03 +0000 | [diff] [blame] | 3280 | yaffs_rd_chunk_tags_nand(dev, prev_chunk_id, |
| 3281 | buffer, &old_tags); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3282 | |
| 3283 | yaffs_verify_oh(in, oh, &old_tags, 0); |
| 3284 | memcpy(old_name, oh->name, sizeof(oh->name)); |
| 3285 | memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr)); |
| 3286 | } else { |
| 3287 | memset(buffer, 0xff, dev->data_bytes_per_chunk); |
| 3288 | } |
| 3289 | |
| 3290 | oh->type = in->variant_type; |
| 3291 | oh->yst_mode = in->yst_mode; |
| 3292 | oh->shadows_obj = oh->inband_shadowed_obj_id = shadows; |
| 3293 | |
| 3294 | yaffs_load_attribs_oh(oh, in); |
| 3295 | |
| 3296 | if (in->parent) |
| 3297 | oh->parent_obj_id = in->parent->obj_id; |
| 3298 | else |
| 3299 | oh->parent_obj_id = 0; |
| 3300 | |
| 3301 | if (name && *name) { |
| 3302 | memset(oh->name, 0, sizeof(oh->name)); |
| 3303 | yaffs_load_oh_from_name(dev, oh->name, name); |
| 3304 | } else if (prev_chunk_id > 0) { |
| 3305 | memcpy(oh->name, old_name, sizeof(oh->name)); |
| 3306 | } else { |
| 3307 | memset(oh->name, 0, sizeof(oh->name)); |
| 3308 | } |
| 3309 | |
| 3310 | oh->is_shrink = is_shrink; |
| 3311 | |
| 3312 | switch (in->variant_type) { |
| 3313 | case YAFFS_OBJECT_TYPE_UNKNOWN: |
| 3314 | /* Should not happen */ |
| 3315 | break; |
| 3316 | case YAFFS_OBJECT_TYPE_FILE: |
| 3317 | if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED && |
| 3318 | oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED) |
| 3319 | file_size = in->variant.file_variant.file_size; |
| 3320 | yaffs_oh_size_load(oh, file_size); |
| 3321 | break; |
| 3322 | case YAFFS_OBJECT_TYPE_HARDLINK: |
| 3323 | oh->equiv_id = in->variant.hardlink_variant.equiv_id; |
| 3324 | break; |
| 3325 | case YAFFS_OBJECT_TYPE_SPECIAL: |
| 3326 | /* Do nothing */ |
| 3327 | break; |
| 3328 | case YAFFS_OBJECT_TYPE_DIRECTORY: |
| 3329 | /* Do nothing */ |
| 3330 | break; |
| 3331 | case YAFFS_OBJECT_TYPE_SYMLINK: |
| 3332 | alias = in->variant.symlink_variant.alias; |
| 3333 | if (!alias) |
| 3334 | alias = _Y("no alias"); |
| 3335 | yaffs_strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH); |
| 3336 | oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0; |
| 3337 | break; |
| 3338 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3339 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3340 | /* process any xattrib modifications */ |
| 3341 | if (xmod) |
| 3342 | yaffs_apply_xattrib_mod(in, (char *)buffer, xmod); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3343 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3344 | /* Tags */ |
| 3345 | memset(&new_tags, 0, sizeof(new_tags)); |
| 3346 | in->serial++; |
| 3347 | new_tags.chunk_id = 0; |
| 3348 | new_tags.obj_id = in->obj_id; |
| 3349 | new_tags.serial_number = in->serial; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3350 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3351 | /* Add extra info for file header */ |
| 3352 | new_tags.extra_available = 1; |
| 3353 | new_tags.extra_parent_id = oh->parent_obj_id; |
| 3354 | new_tags.extra_file_size = file_size; |
| 3355 | new_tags.extra_is_shrink = oh->is_shrink; |
| 3356 | new_tags.extra_equiv_id = oh->equiv_id; |
| 3357 | new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0; |
| 3358 | new_tags.extra_obj_type = in->variant_type; |
| 3359 | yaffs_verify_oh(in, oh, &new_tags, 1); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3360 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3361 | /* Create new chunk in NAND */ |
| 3362 | new_chunk_id = |
| 3363 | yaffs_write_new_chunk(dev, buffer, &new_tags, |
| 3364 | (prev_chunk_id > 0) ? 1 : 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3365 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3366 | if (buffer) |
| 3367 | yaffs_release_temp_buffer(dev, buffer); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3368 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3369 | if (new_chunk_id < 0) |
| 3370 | return new_chunk_id; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3371 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3372 | in->hdr_chunk = new_chunk_id; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3373 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3374 | if (prev_chunk_id > 0) |
| 3375 | yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3376 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3377 | if (!yaffs_obj_cache_dirty(in)) |
| 3378 | in->dirty = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3379 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3380 | /* If this was a shrink, then mark the block |
| 3381 | * that the chunk lives on */ |
| 3382 | if (is_shrink) { |
| 3383 | bi = yaffs_get_block_info(in->my_dev, |
| 3384 | new_chunk_id / |
| 3385 | in->my_dev->param.chunks_per_block); |
| 3386 | bi->has_shrink_hdr = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3387 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3388 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3389 | return new_chunk_id; |
| 3390 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3391 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3392 | /*--------------------- File read/write ------------------------ |
| 3393 | * Read and write have very similar structures. |
| 3394 | * In general the read/write has three parts to it |
| 3395 | * An incomplete chunk to start with (if the read/write is not chunk-aligned) |
| 3396 | * Some complete chunks |
| 3397 | * An incomplete chunk to end off with |
| 3398 | * |
| 3399 | * Curve-balls: the first chunk might also be the last chunk. |
| 3400 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3401 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3402 | int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes) |
| 3403 | { |
| 3404 | int chunk; |
| 3405 | u32 start; |
| 3406 | int n_copy; |
| 3407 | int n = n_bytes; |
| 3408 | int n_done = 0; |
| 3409 | struct yaffs_cache *cache; |
| 3410 | struct yaffs_dev *dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3411 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3412 | dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3413 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3414 | while (n > 0) { |
| 3415 | yaffs_addr_to_chunk(dev, offset, &chunk, &start); |
| 3416 | chunk++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3417 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3418 | /* OK now check for the curveball where the start and end are in |
| 3419 | * the same chunk. |
| 3420 | */ |
| 3421 | if ((start + n) < dev->data_bytes_per_chunk) |
| 3422 | n_copy = n; |
| 3423 | else |
| 3424 | n_copy = dev->data_bytes_per_chunk - start; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3425 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3426 | cache = yaffs_find_chunk_cache(in, chunk); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3427 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3428 | /* If the chunk is already in the cache or it is less than |
| 3429 | * a whole chunk or we're using inband tags then use the cache |
| 3430 | * (if there is caching) else bypass the cache. |
| 3431 | */ |
| 3432 | if (cache || n_copy != dev->data_bytes_per_chunk || |
| 3433 | dev->param.inband_tags) { |
| 3434 | if (dev->param.n_caches > 0) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3435 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3436 | /* If we can't find the data in the cache, |
| 3437 | * then load it up. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3438 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3439 | if (!cache) { |
| 3440 | cache = |
| 3441 | yaffs_grab_chunk_cache(in->my_dev); |
| 3442 | cache->object = in; |
| 3443 | cache->chunk_id = chunk; |
| 3444 | cache->dirty = 0; |
| 3445 | cache->locked = 0; |
| 3446 | yaffs_rd_data_obj(in, chunk, |
| 3447 | cache->data); |
| 3448 | cache->n_bytes = 0; |
| 3449 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3450 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3451 | yaffs_use_cache(dev, cache, 0); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3452 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3453 | cache->locked = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3454 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3455 | memcpy(buffer, &cache->data[start], n_copy); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3456 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3457 | cache->locked = 0; |
| 3458 | } else { |
| 3459 | /* Read into the local buffer then copy.. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3460 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3461 | u8 *local_buffer = |
| 3462 | yaffs_get_temp_buffer(dev); |
| 3463 | yaffs_rd_data_obj(in, chunk, local_buffer); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3464 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3465 | memcpy(buffer, &local_buffer[start], n_copy); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3466 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3467 | yaffs_release_temp_buffer(dev, local_buffer); |
| 3468 | } |
| 3469 | } else { |
| 3470 | /* A full chunk. Read directly into the buffer. */ |
| 3471 | yaffs_rd_data_obj(in, chunk, buffer); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3472 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3473 | n -= n_copy; |
| 3474 | offset += n_copy; |
| 3475 | buffer += n_copy; |
| 3476 | n_done += n_copy; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3477 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3478 | return n_done; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3479 | } |
| 3480 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3481 | int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset, |
| 3482 | int n_bytes, int write_through) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3483 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3484 | |
| 3485 | int chunk; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3486 | u32 start; |
| 3487 | int n_copy; |
| 3488 | int n = n_bytes; |
| 3489 | int n_done = 0; |
| 3490 | int n_writeback; |
| 3491 | loff_t start_write = offset; |
| 3492 | int chunk_written = 0; |
| 3493 | u32 n_bytes_read; |
| 3494 | loff_t chunk_start; |
| 3495 | struct yaffs_dev *dev; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3496 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3497 | dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3498 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3499 | while (n > 0 && chunk_written >= 0) { |
| 3500 | yaffs_addr_to_chunk(dev, offset, &chunk, &start); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3501 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3502 | if (((loff_t)chunk) * |
| 3503 | dev->data_bytes_per_chunk + start != offset || |
| 3504 | start >= dev->data_bytes_per_chunk) { |
| 3505 | yaffs_trace(YAFFS_TRACE_ERROR, |
| 3506 | "AddrToChunk of offset %lld gives chunk %d start %d", |
| 3507 | offset, chunk, start); |
| 3508 | } |
| 3509 | chunk++; /* File pos to chunk in file offset */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3510 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3511 | /* OK now check for the curveball where the start and end are in |
| 3512 | * the same chunk. |
| 3513 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3514 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3515 | if ((start + n) < dev->data_bytes_per_chunk) { |
| 3516 | n_copy = n; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3517 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3518 | /* Now calculate how many bytes to write back.... |
| 3519 | * If we're overwriting and not writing to then end of |
| 3520 | * file then we need to write back as much as was there |
| 3521 | * before. |
| 3522 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3523 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3524 | chunk_start = (((loff_t)(chunk - 1)) * |
| 3525 | dev->data_bytes_per_chunk); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3526 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3527 | if (chunk_start > in->variant.file_variant.file_size) |
| 3528 | n_bytes_read = 0; /* Past end of file */ |
| 3529 | else |
| 3530 | n_bytes_read = |
| 3531 | in->variant.file_variant.file_size - |
| 3532 | chunk_start; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3533 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3534 | if (n_bytes_read > dev->data_bytes_per_chunk) |
| 3535 | n_bytes_read = dev->data_bytes_per_chunk; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3536 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3537 | n_writeback = |
| 3538 | (n_bytes_read > |
| 3539 | (start + n)) ? n_bytes_read : (start + n); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3540 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3541 | if (n_writeback < 0 || |
| 3542 | n_writeback > dev->data_bytes_per_chunk) |
| 3543 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3544 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3545 | } else { |
| 3546 | n_copy = dev->data_bytes_per_chunk - start; |
| 3547 | n_writeback = dev->data_bytes_per_chunk; |
| 3548 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3549 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3550 | if (n_copy != dev->data_bytes_per_chunk || |
| 3551 | dev->param.inband_tags) { |
| 3552 | /* An incomplete start or end chunk (or maybe both |
| 3553 | * start and end chunk), or we're using inband tags, |
| 3554 | * so we want to use the cache buffers. |
| 3555 | */ |
| 3556 | if (dev->param.n_caches > 0) { |
| 3557 | struct yaffs_cache *cache; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3558 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3559 | /* If we can't find the data in the cache, then |
| 3560 | * load the cache */ |
| 3561 | cache = yaffs_find_chunk_cache(in, chunk); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3562 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3563 | if (!cache && |
| 3564 | yaffs_check_alloc_available(dev, 1)) { |
| 3565 | cache = yaffs_grab_chunk_cache(dev); |
| 3566 | cache->object = in; |
| 3567 | cache->chunk_id = chunk; |
| 3568 | cache->dirty = 0; |
| 3569 | cache->locked = 0; |
| 3570 | yaffs_rd_data_obj(in, chunk, |
| 3571 | cache->data); |
| 3572 | } else if (cache && |
| 3573 | !cache->dirty && |
| 3574 | !yaffs_check_alloc_available(dev, |
| 3575 | 1)) { |
| 3576 | /* Drop the cache if it was a read cache |
| 3577 | * item and no space check has been made |
| 3578 | * for it. |
| 3579 | */ |
| 3580 | cache = NULL; |
| 3581 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3582 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3583 | if (cache) { |
| 3584 | yaffs_use_cache(dev, cache, 1); |
| 3585 | cache->locked = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3586 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3587 | memcpy(&cache->data[start], buffer, |
| 3588 | n_copy); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3589 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3590 | cache->locked = 0; |
| 3591 | cache->n_bytes = n_writeback; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3592 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3593 | if (write_through) { |
| 3594 | chunk_written = |
| 3595 | yaffs_wr_data_obj |
| 3596 | (cache->object, |
| 3597 | cache->chunk_id, |
| 3598 | cache->data, |
| 3599 | cache->n_bytes, 1); |
| 3600 | cache->dirty = 0; |
| 3601 | } |
| 3602 | } else { |
| 3603 | chunk_written = -1; /* fail write */ |
| 3604 | } |
| 3605 | } else { |
| 3606 | /* An incomplete start or end chunk (or maybe |
| 3607 | * both start and end chunk). Read into the |
| 3608 | * local buffer then copy over and write back. |
| 3609 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3610 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3611 | u8 *local_buffer = yaffs_get_temp_buffer(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3612 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3613 | yaffs_rd_data_obj(in, chunk, local_buffer); |
| 3614 | memcpy(&local_buffer[start], buffer, n_copy); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3615 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3616 | chunk_written = |
| 3617 | yaffs_wr_data_obj(in, chunk, |
| 3618 | local_buffer, |
| 3619 | n_writeback, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3620 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3621 | yaffs_release_temp_buffer(dev, local_buffer); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3622 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3623 | } else { |
| 3624 | /* A full chunk. Write directly from the buffer. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3625 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3626 | chunk_written = |
| 3627 | yaffs_wr_data_obj(in, chunk, buffer, |
| 3628 | dev->data_bytes_per_chunk, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3629 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3630 | /* Since we've overwritten the cached data, |
| 3631 | * we better invalidate it. */ |
| 3632 | yaffs_invalidate_chunk_cache(in, chunk); |
| 3633 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3634 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3635 | if (chunk_written >= 0) { |
| 3636 | n -= n_copy; |
| 3637 | offset += n_copy; |
| 3638 | buffer += n_copy; |
| 3639 | n_done += n_copy; |
| 3640 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3641 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3642 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3643 | /* Update file object */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3644 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3645 | if ((start_write + n_done) > in->variant.file_variant.file_size) |
| 3646 | in->variant.file_variant.file_size = (start_write + n_done); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3647 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3648 | in->dirty = 1; |
| 3649 | return n_done; |
| 3650 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3651 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3652 | int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset, |
| 3653 | int n_bytes, int write_through) |
| 3654 | { |
| 3655 | yaffs2_handle_hole(in, offset); |
| 3656 | return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through); |
| 3657 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3658 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3659 | /* ---------------------- File resizing stuff ------------------ */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3660 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3661 | static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size) |
| 3662 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3663 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3664 | struct yaffs_dev *dev = in->my_dev; |
| 3665 | loff_t old_size = in->variant.file_variant.file_size; |
| 3666 | int i; |
| 3667 | int chunk_id; |
| 3668 | u32 dummy; |
| 3669 | int last_del; |
| 3670 | int start_del; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3671 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3672 | if (old_size > 0) |
| 3673 | yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy); |
| 3674 | else |
| 3675 | last_del = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3676 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3677 | yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1, |
| 3678 | &start_del, &dummy); |
| 3679 | last_del++; |
| 3680 | start_del++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3681 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3682 | /* Delete backwards so that we don't end up with holes if |
| 3683 | * power is lost part-way through the operation. |
| 3684 | */ |
| 3685 | for (i = last_del; i >= start_del; i--) { |
| 3686 | /* NB this could be optimised somewhat, |
| 3687 | * eg. could retrieve the tags and write them without |
| 3688 | * using yaffs_chunk_del |
| 3689 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3690 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3691 | chunk_id = yaffs_find_del_file_chunk(in, i, NULL); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3692 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3693 | if (chunk_id < 1) |
| 3694 | continue; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3695 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3696 | if (chunk_id < |
| 3697 | (dev->internal_start_block * dev->param.chunks_per_block) || |
| 3698 | chunk_id >= |
| 3699 | ((dev->internal_end_block + 1) * |
| 3700 | dev->param.chunks_per_block)) { |
| 3701 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 3702 | "Found daft chunk_id %d for %d", |
| 3703 | chunk_id, i); |
| 3704 | } else { |
| 3705 | in->n_data_chunks--; |
| 3706 | yaffs_chunk_del(dev, chunk_id, 1, __LINE__); |
| 3707 | } |
| 3708 | } |
| 3709 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3710 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3711 | void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size) |
| 3712 | { |
| 3713 | int new_full; |
| 3714 | u32 new_partial; |
| 3715 | struct yaffs_dev *dev = obj->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3716 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3717 | yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3718 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3719 | yaffs_prune_chunks(obj, new_size); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3720 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3721 | if (new_partial != 0) { |
| 3722 | int last_chunk = 1 + new_full; |
| 3723 | u8 *local_buffer = yaffs_get_temp_buffer(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3724 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3725 | /* Rewrite the last chunk with its new size and zero pad */ |
| 3726 | yaffs_rd_data_obj(obj, last_chunk, local_buffer); |
| 3727 | memset(local_buffer + new_partial, 0, |
| 3728 | dev->data_bytes_per_chunk - new_partial); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3729 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3730 | yaffs_wr_data_obj(obj, last_chunk, local_buffer, |
| 3731 | new_partial, 1); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3732 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3733 | yaffs_release_temp_buffer(dev, local_buffer); |
| 3734 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3735 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3736 | obj->variant.file_variant.file_size = new_size; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3737 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3738 | yaffs_prune_tree(dev, &obj->variant.file_variant); |
| 3739 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3740 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3741 | int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size) |
| 3742 | { |
| 3743 | struct yaffs_dev *dev = in->my_dev; |
| 3744 | loff_t old_size = in->variant.file_variant.file_size; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3745 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3746 | yaffs_flush_file_cache(in); |
| 3747 | yaffs_invalidate_whole_cache(in); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3748 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3749 | yaffs_check_gc(dev, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3750 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3751 | if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) |
| 3752 | return YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3753 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3754 | if (new_size == old_size) |
| 3755 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3756 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3757 | if (new_size > old_size) { |
| 3758 | yaffs2_handle_hole(in, new_size); |
| 3759 | in->variant.file_variant.file_size = new_size; |
| 3760 | } else { |
| 3761 | /* new_size < old_size */ |
| 3762 | yaffs_resize_file_down(in, new_size); |
| 3763 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3764 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3765 | /* Write a new object header to reflect the resize. |
| 3766 | * show we've shrunk the file, if need be |
| 3767 | * Do this only if the file is not in the deleted directories |
| 3768 | * and is not shadowed. |
| 3769 | */ |
| 3770 | if (in->parent && |
| 3771 | !in->is_shadowed && |
| 3772 | in->parent->obj_id != YAFFS_OBJECTID_UNLINKED && |
| 3773 | in->parent->obj_id != YAFFS_OBJECTID_DELETED) |
| 3774 | yaffs_update_oh(in, NULL, 0, 0, 0, NULL); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3775 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3776 | return YAFFS_OK; |
| 3777 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3778 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3779 | int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync) |
| 3780 | { |
| 3781 | if (!in->dirty) |
| 3782 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3783 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3784 | yaffs_flush_file_cache(in); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3785 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3786 | if (data_sync) |
| 3787 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3788 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3789 | if (update_time) |
| 3790 | yaffs_load_current_time(in, 0, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3791 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3792 | return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ? |
| 3793 | YAFFS_OK : YAFFS_FAIL; |
| 3794 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3795 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3796 | /* yaffs_del_file deletes the whole file data |
| 3797 | * and the inode associated with the file. |
| 3798 | * It does not delete the links associated with the file. |
| 3799 | */ |
| 3800 | static int yaffs_unlink_file_if_needed(struct yaffs_obj *in) |
| 3801 | { |
| 3802 | int ret_val; |
| 3803 | int del_now = 0; |
| 3804 | struct yaffs_dev *dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3805 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3806 | if (!in->my_inode) |
| 3807 | del_now = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3808 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3809 | if (del_now) { |
| 3810 | ret_val = |
| 3811 | yaffs_change_obj_name(in, in->my_dev->del_dir, |
| 3812 | _Y("deleted"), 0, 0); |
| 3813 | yaffs_trace(YAFFS_TRACE_TRACING, |
| 3814 | "yaffs: immediate deletion of file %d", |
| 3815 | in->obj_id); |
| 3816 | in->deleted = 1; |
| 3817 | in->my_dev->n_deleted_files++; |
| 3818 | if (dev->param.disable_soft_del || dev->param.is_yaffs2) |
| 3819 | yaffs_resize_file(in, 0); |
| 3820 | yaffs_soft_del_file(in); |
| 3821 | } else { |
| 3822 | ret_val = |
| 3823 | yaffs_change_obj_name(in, in->my_dev->unlinked_dir, |
| 3824 | _Y("unlinked"), 0, 0); |
| 3825 | } |
| 3826 | return ret_val; |
| 3827 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3828 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3829 | int yaffs_del_file(struct yaffs_obj *in) |
| 3830 | { |
| 3831 | int ret_val = YAFFS_OK; |
| 3832 | int deleted; /* Need to cache value on stack if in is freed */ |
| 3833 | struct yaffs_dev *dev = in->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3834 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3835 | if (dev->param.disable_soft_del || dev->param.is_yaffs2) |
| 3836 | yaffs_resize_file(in, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3837 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3838 | if (in->n_data_chunks > 0) { |
| 3839 | /* Use soft deletion if there is data in the file. |
| 3840 | * That won't be the case if it has been resized to zero. |
| 3841 | */ |
| 3842 | if (!in->unlinked) |
| 3843 | ret_val = yaffs_unlink_file_if_needed(in); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3844 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3845 | deleted = in->deleted; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3846 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3847 | if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) { |
| 3848 | in->deleted = 1; |
| 3849 | deleted = 1; |
| 3850 | in->my_dev->n_deleted_files++; |
| 3851 | yaffs_soft_del_file(in); |
| 3852 | } |
| 3853 | return deleted ? YAFFS_OK : YAFFS_FAIL; |
| 3854 | } else { |
| 3855 | /* The file has no data chunks so we toss it immediately */ |
| 3856 | yaffs_free_tnode(in->my_dev, in->variant.file_variant.top); |
| 3857 | in->variant.file_variant.top = NULL; |
| 3858 | yaffs_generic_obj_del(in); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3859 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3860 | return YAFFS_OK; |
| 3861 | } |
| 3862 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3863 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3864 | int yaffs_is_non_empty_dir(struct yaffs_obj *obj) |
| 3865 | { |
| 3866 | return (obj && |
| 3867 | obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) && |
| 3868 | !(list_empty(&obj->variant.dir_variant.children)); |
| 3869 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3870 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3871 | static int yaffs_del_dir(struct yaffs_obj *obj) |
| 3872 | { |
| 3873 | /* First check that the directory is empty. */ |
| 3874 | if (yaffs_is_non_empty_dir(obj)) |
| 3875 | return YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3876 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3877 | return yaffs_generic_obj_del(obj); |
| 3878 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3879 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3880 | static int yaffs_del_symlink(struct yaffs_obj *in) |
| 3881 | { |
| 3882 | kfree(in->variant.symlink_variant.alias); |
| 3883 | in->variant.symlink_variant.alias = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3884 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3885 | return yaffs_generic_obj_del(in); |
| 3886 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3887 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3888 | static int yaffs_del_link(struct yaffs_obj *in) |
| 3889 | { |
| 3890 | /* remove this hardlink from the list associated with the equivalent |
| 3891 | * object |
| 3892 | */ |
| 3893 | list_del_init(&in->hard_links); |
| 3894 | return yaffs_generic_obj_del(in); |
| 3895 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3896 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3897 | int yaffs_del_obj(struct yaffs_obj *obj) |
| 3898 | { |
| 3899 | int ret_val = -1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3900 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3901 | switch (obj->variant_type) { |
| 3902 | case YAFFS_OBJECT_TYPE_FILE: |
| 3903 | ret_val = yaffs_del_file(obj); |
| 3904 | break; |
| 3905 | case YAFFS_OBJECT_TYPE_DIRECTORY: |
| 3906 | if (!list_empty(&obj->variant.dir_variant.dirty)) { |
| 3907 | yaffs_trace(YAFFS_TRACE_BACKGROUND, |
| 3908 | "Remove object %d from dirty directories", |
| 3909 | obj->obj_id); |
| 3910 | list_del_init(&obj->variant.dir_variant.dirty); |
| 3911 | } |
| 3912 | return yaffs_del_dir(obj); |
| 3913 | break; |
| 3914 | case YAFFS_OBJECT_TYPE_SYMLINK: |
| 3915 | ret_val = yaffs_del_symlink(obj); |
| 3916 | break; |
| 3917 | case YAFFS_OBJECT_TYPE_HARDLINK: |
| 3918 | ret_val = yaffs_del_link(obj); |
| 3919 | break; |
| 3920 | case YAFFS_OBJECT_TYPE_SPECIAL: |
| 3921 | ret_val = yaffs_generic_obj_del(obj); |
| 3922 | break; |
| 3923 | case YAFFS_OBJECT_TYPE_UNKNOWN: |
| 3924 | ret_val = 0; |
| 3925 | break; /* should not happen. */ |
| 3926 | } |
| 3927 | return ret_val; |
| 3928 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3929 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3930 | static int yaffs_unlink_worker(struct yaffs_obj *obj) |
| 3931 | { |
| 3932 | int del_now = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3933 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3934 | if (!obj) |
| 3935 | return YAFFS_FAIL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3936 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3937 | if (!obj->my_inode) |
| 3938 | del_now = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3939 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3940 | yaffs_update_parent(obj->parent); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3941 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3942 | if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { |
| 3943 | return yaffs_del_link(obj); |
| 3944 | } else if (!list_empty(&obj->hard_links)) { |
| 3945 | /* Curve ball: We're unlinking an object that has a hardlink. |
| 3946 | * |
| 3947 | * This problem arises because we are not strictly following |
| 3948 | * The Linux link/inode model. |
| 3949 | * |
| 3950 | * We can't really delete the object. |
| 3951 | * Instead, we do the following: |
| 3952 | * - Select a hardlink. |
| 3953 | * - Unhook it from the hard links |
| 3954 | * - Move it from its parent directory so that the rename works. |
| 3955 | * - Rename the object to the hardlink's name. |
| 3956 | * - Delete the hardlink |
| 3957 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3958 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3959 | struct yaffs_obj *hl; |
| 3960 | struct yaffs_obj *parent; |
| 3961 | int ret_val; |
| 3962 | YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3963 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3964 | hl = list_entry(obj->hard_links.next, struct yaffs_obj, |
| 3965 | hard_links); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3966 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3967 | yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1); |
| 3968 | parent = hl->parent; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 3969 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3970 | list_del_init(&hl->hard_links); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3971 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3972 | yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3973 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3974 | ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3975 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3976 | if (ret_val == YAFFS_OK) |
| 3977 | ret_val = yaffs_generic_obj_del(hl); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3978 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3979 | return ret_val; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 3980 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 3981 | } else if (del_now) { |
| 3982 | switch (obj->variant_type) { |
| 3983 | case YAFFS_OBJECT_TYPE_FILE: |
| 3984 | return yaffs_del_file(obj); |
| 3985 | break; |
| 3986 | case YAFFS_OBJECT_TYPE_DIRECTORY: |
| 3987 | list_del_init(&obj->variant.dir_variant.dirty); |
| 3988 | return yaffs_del_dir(obj); |
| 3989 | break; |
| 3990 | case YAFFS_OBJECT_TYPE_SYMLINK: |
| 3991 | return yaffs_del_symlink(obj); |
| 3992 | break; |
| 3993 | case YAFFS_OBJECT_TYPE_SPECIAL: |
| 3994 | return yaffs_generic_obj_del(obj); |
| 3995 | break; |
| 3996 | case YAFFS_OBJECT_TYPE_HARDLINK: |
| 3997 | case YAFFS_OBJECT_TYPE_UNKNOWN: |
| 3998 | default: |
| 3999 | return YAFFS_FAIL; |
| 4000 | } |
| 4001 | } else if (yaffs_is_non_empty_dir(obj)) { |
| 4002 | return YAFFS_FAIL; |
| 4003 | } else { |
| 4004 | return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir, |
| 4005 | _Y("unlinked"), 0, 0); |
| 4006 | } |
| 4007 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4008 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4009 | static int yaffs_unlink_obj(struct yaffs_obj *obj) |
| 4010 | { |
| 4011 | if (obj && obj->unlink_allowed) |
| 4012 | return yaffs_unlink_worker(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4013 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4014 | return YAFFS_FAIL; |
| 4015 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4016 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4017 | int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name) |
| 4018 | { |
| 4019 | struct yaffs_obj *obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4020 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4021 | obj = yaffs_find_by_name(dir, name); |
| 4022 | return yaffs_unlink_obj(obj); |
| 4023 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4024 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4025 | /* Note: |
| 4026 | * If old_name is NULL then we take old_dir as the object to be renamed. |
| 4027 | */ |
| 4028 | int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name, |
| 4029 | struct yaffs_obj *new_dir, const YCHAR *new_name) |
| 4030 | { |
| 4031 | struct yaffs_obj *obj = NULL; |
| 4032 | struct yaffs_obj *existing_target = NULL; |
| 4033 | int force = 0; |
| 4034 | int result; |
| 4035 | struct yaffs_dev *dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4036 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4037 | if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { |
| 4038 | BUG(); |
| 4039 | return YAFFS_FAIL; |
| 4040 | } |
| 4041 | if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { |
| 4042 | BUG(); |
| 4043 | return YAFFS_FAIL; |
| 4044 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4045 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4046 | dev = old_dir->my_dev; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4047 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4048 | #ifdef CONFIG_YAFFS_CASE_INSENSITIVE |
| 4049 | /* Special case for case insemsitive systems. |
| 4050 | * While look-up is case insensitive, the name isn't. |
| 4051 | * Therefore we might want to change x.txt to X.txt |
| 4052 | */ |
| 4053 | if (old_dir == new_dir && |
| 4054 | old_name && new_name && |
| 4055 | yaffs_strcmp(old_name, new_name) == 0) |
| 4056 | force = 1; |
| 4057 | #endif |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4058 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4059 | if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) > |
| 4060 | YAFFS_MAX_NAME_LENGTH) |
| 4061 | /* ENAMETOOLONG */ |
| 4062 | return YAFFS_FAIL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4063 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4064 | if (old_name) |
| 4065 | obj = yaffs_find_by_name(old_dir, old_name); |
| 4066 | else{ |
| 4067 | obj = old_dir; |
| 4068 | old_dir = obj->parent; |
| 4069 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4070 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4071 | if (obj && obj->rename_allowed) { |
| 4072 | /* Now handle an existing target, if there is one */ |
| 4073 | existing_target = yaffs_find_by_name(new_dir, new_name); |
| 4074 | if (yaffs_is_non_empty_dir(existing_target)) { |
| 4075 | return YAFFS_FAIL; /* ENOTEMPTY */ |
| 4076 | } else if (existing_target && existing_target != obj) { |
| 4077 | /* Nuke the target first, using shadowing, |
| 4078 | * but only if it isn't the same object. |
| 4079 | * |
| 4080 | * Note we must disable gc here otherwise it can mess |
| 4081 | * up the shadowing. |
| 4082 | * |
| 4083 | */ |
| 4084 | dev->gc_disable = 1; |
| 4085 | yaffs_change_obj_name(obj, new_dir, new_name, force, |
| 4086 | existing_target->obj_id); |
| 4087 | existing_target->is_shadowed = 1; |
| 4088 | yaffs_unlink_obj(existing_target); |
| 4089 | dev->gc_disable = 0; |
| 4090 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4091 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4092 | result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4093 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4094 | yaffs_update_parent(old_dir); |
| 4095 | if (new_dir != old_dir) |
| 4096 | yaffs_update_parent(new_dir); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4097 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4098 | return result; |
| 4099 | } |
| 4100 | return YAFFS_FAIL; |
| 4101 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4102 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4103 | /*----------------------- Initialisation Scanning ---------------------- */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4104 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4105 | void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id, |
| 4106 | int backward_scanning) |
| 4107 | { |
| 4108 | struct yaffs_obj *obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4109 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4110 | if (backward_scanning) { |
| 4111 | /* Handle YAFFS2 case (backward scanning) |
| 4112 | * If the shadowed object exists then ignore. |
| 4113 | */ |
| 4114 | obj = yaffs_find_by_number(dev, obj_id); |
| 4115 | if (obj) |
| 4116 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4117 | } |
| 4118 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4119 | /* Let's create it (if it does not exist) assuming it is a file so that |
| 4120 | * it can do shrinking etc. |
| 4121 | * We put it in unlinked dir to be cleaned up after the scanning |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4122 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4123 | obj = |
| 4124 | yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE); |
| 4125 | if (!obj) |
| 4126 | return; |
| 4127 | obj->is_shadowed = 1; |
| 4128 | yaffs_add_obj_to_dir(dev->unlinked_dir, obj); |
| 4129 | obj->variant.file_variant.shrink_size = 0; |
| 4130 | obj->valid = 1; /* So that we don't read any other info. */ |
| 4131 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4132 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4133 | void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list) |
| 4134 | { |
| 4135 | struct list_head *lh; |
| 4136 | struct list_head *save; |
| 4137 | struct yaffs_obj *hl; |
| 4138 | struct yaffs_obj *in; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4139 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4140 | list_for_each_safe(lh, save, hard_list) { |
| 4141 | hl = list_entry(lh, struct yaffs_obj, hard_links); |
| 4142 | in = yaffs_find_by_number(dev, |
| 4143 | hl->variant.hardlink_variant.equiv_id); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4144 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4145 | if (in) { |
| 4146 | /* Add the hardlink pointers */ |
| 4147 | hl->variant.hardlink_variant.equiv_obj = in; |
| 4148 | list_add(&hl->hard_links, &in->hard_links); |
| 4149 | } else { |
| 4150 | /* Todo Need to report/handle this better. |
| 4151 | * Got a problem... hardlink to a non-existant object |
| 4152 | */ |
| 4153 | hl->variant.hardlink_variant.equiv_obj = NULL; |
| 4154 | INIT_LIST_HEAD(&hl->hard_links); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4155 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4156 | } |
| 4157 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4158 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4159 | static void yaffs_strip_deleted_objs(struct yaffs_dev *dev) |
| 4160 | { |
| 4161 | /* |
| 4162 | * Sort out state of unlinked and deleted objects after scanning. |
| 4163 | */ |
| 4164 | struct list_head *i; |
| 4165 | struct list_head *n; |
| 4166 | struct yaffs_obj *l; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4167 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4168 | if (dev->read_only) |
| 4169 | return; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4170 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4171 | /* Soft delete all the unlinked files */ |
| 4172 | list_for_each_safe(i, n, |
| 4173 | &dev->unlinked_dir->variant.dir_variant.children) { |
| 4174 | l = list_entry(i, struct yaffs_obj, siblings); |
| 4175 | yaffs_del_obj(l); |
| 4176 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4177 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4178 | list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) { |
| 4179 | l = list_entry(i, struct yaffs_obj, siblings); |
| 4180 | yaffs_del_obj(l); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4181 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4182 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4183 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4184 | /* |
| 4185 | * This code iterates through all the objects making sure that they are rooted. |
| 4186 | * Any unrooted objects are re-rooted in lost+found. |
| 4187 | * An object needs to be in one of: |
| 4188 | * - Directly under deleted, unlinked |
| 4189 | * - Directly or indirectly under root. |
| 4190 | * |
| 4191 | * Note: |
| 4192 | * This code assumes that we don't ever change the current relationships |
| 4193 | * between directories: |
| 4194 | * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL |
| 4195 | * lost-n-found->parent == root_dir |
| 4196 | * |
| 4197 | * This fixes the problem where directories might have inadvertently been |
| 4198 | * deleted leaving the object "hanging" without being rooted in the |
| 4199 | * directory tree. |
| 4200 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4201 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4202 | static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj) |
| 4203 | { |
| 4204 | return (obj == dev->del_dir || |
| 4205 | obj == dev->unlinked_dir || obj == dev->root_dir); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4206 | } |
| 4207 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4208 | static void yaffs_fix_hanging_objs(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4209 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4210 | struct yaffs_obj *obj; |
| 4211 | struct yaffs_obj *parent; |
| 4212 | int i; |
| 4213 | struct list_head *lh; |
| 4214 | struct list_head *n; |
| 4215 | int depth_limit; |
| 4216 | int hanging; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4217 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4218 | if (dev->read_only) |
| 4219 | return; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4220 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4221 | /* Iterate through the objects in each hash entry, |
| 4222 | * looking at each object. |
| 4223 | * Make sure it is rooted. |
| 4224 | */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4225 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4226 | for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { |
| 4227 | list_for_each_safe(lh, n, &dev->obj_bucket[i].list) { |
| 4228 | obj = list_entry(lh, struct yaffs_obj, hash_link); |
| 4229 | parent = obj->parent; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4230 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4231 | if (yaffs_has_null_parent(dev, obj)) { |
| 4232 | /* These directories are not hanging */ |
| 4233 | hanging = 0; |
| 4234 | } else if (!parent || |
| 4235 | parent->variant_type != |
| 4236 | YAFFS_OBJECT_TYPE_DIRECTORY) { |
| 4237 | hanging = 1; |
| 4238 | } else if (yaffs_has_null_parent(dev, parent)) { |
| 4239 | hanging = 0; |
| 4240 | } else { |
| 4241 | /* |
| 4242 | * Need to follow the parent chain to |
| 4243 | * see if it is hanging. |
| 4244 | */ |
| 4245 | hanging = 0; |
| 4246 | depth_limit = 100; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4247 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4248 | while (parent != dev->root_dir && |
| 4249 | parent->parent && |
| 4250 | parent->parent->variant_type == |
| 4251 | YAFFS_OBJECT_TYPE_DIRECTORY && |
| 4252 | depth_limit > 0) { |
| 4253 | parent = parent->parent; |
| 4254 | depth_limit--; |
| 4255 | } |
| 4256 | if (parent != dev->root_dir) |
| 4257 | hanging = 1; |
| 4258 | } |
| 4259 | if (hanging) { |
| 4260 | yaffs_trace(YAFFS_TRACE_SCAN, |
| 4261 | "Hanging object %d moved to lost and found", |
| 4262 | obj->obj_id); |
| 4263 | yaffs_add_obj_to_dir(dev->lost_n_found, obj); |
| 4264 | } |
| 4265 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4266 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4267 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4268 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4269 | /* |
| 4270 | * Delete directory contents for cleaning up lost and found. |
| 4271 | */ |
| 4272 | static void yaffs_del_dir_contents(struct yaffs_obj *dir) |
| 4273 | { |
| 4274 | struct yaffs_obj *obj; |
| 4275 | struct list_head *lh; |
| 4276 | struct list_head *n; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4277 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4278 | if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) |
| 4279 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4280 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4281 | list_for_each_safe(lh, n, &dir->variant.dir_variant.children) { |
| 4282 | obj = list_entry(lh, struct yaffs_obj, siblings); |
| 4283 | if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) |
| 4284 | yaffs_del_dir_contents(obj); |
| 4285 | yaffs_trace(YAFFS_TRACE_SCAN, |
| 4286 | "Deleting lost_found object %d", |
| 4287 | obj->obj_id); |
| 4288 | yaffs_unlink_obj(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4292 | static void yaffs_empty_l_n_f(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4293 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4294 | yaffs_del_dir_contents(dev->lost_n_found); |
| 4295 | } |
| 4296 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4297 | struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory, |
| 4298 | const YCHAR *name) |
| 4299 | { |
| 4300 | int sum; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4301 | struct list_head *i; |
| 4302 | YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1]; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4303 | struct yaffs_obj *l; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4304 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4305 | if (!name) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4306 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4307 | |
| 4308 | if (!directory) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4309 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 4310 | "tragedy: yaffs_find_by_name: null pointer directory" |
| 4311 | ); |
| 4312 | BUG(); |
| 4313 | return NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4314 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4315 | if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { |
| 4316 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 4317 | "tragedy: yaffs_find_by_name: non-directory" |
| 4318 | ); |
| 4319 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4320 | } |
| 4321 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4322 | sum = yaffs_calc_name_sum(name); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4323 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4324 | list_for_each(i, &directory->variant.dir_variant.children) { |
| 4325 | l = list_entry(i, struct yaffs_obj, siblings); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4326 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4327 | if (l->parent != directory) |
| 4328 | BUG(); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4329 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4330 | yaffs_check_obj_details_loaded(l); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4331 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4332 | /* Special case for lost-n-found */ |
| 4333 | if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { |
| 4334 | if (!yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME)) |
| 4335 | return l; |
| 4336 | } else if (l->sum == sum || l->hdr_chunk <= 0) { |
| 4337 | /* LostnFound chunk called Objxxx |
| 4338 | * Do a real check |
| 4339 | */ |
| 4340 | yaffs_get_obj_name(l, buffer, |
| 4341 | YAFFS_MAX_NAME_LENGTH + 1); |
| 4342 | if (!yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH)) |
| 4343 | return l; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4344 | } |
| 4345 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4346 | return NULL; |
| 4347 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4348 | |
| 4349 | /* GetEquivalentObject dereferences any hard links to get to the |
| 4350 | * actual object. |
| 4351 | */ |
| 4352 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4353 | struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4354 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4355 | if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { |
| 4356 | obj = obj->variant.hardlink_variant.equiv_obj; |
| 4357 | yaffs_check_obj_details_loaded(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4358 | } |
| 4359 | return obj; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4360 | } |
| 4361 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4362 | /* |
| 4363 | * A note or two on object names. |
| 4364 | * * If the object name is missing, we then make one up in the form objnnn |
| 4365 | * |
| 4366 | * * ASCII names are stored in the object header's name field from byte zero |
| 4367 | * * Unicode names are historically stored starting from byte zero. |
| 4368 | * |
| 4369 | * Then there are automatic Unicode names... |
| 4370 | * The purpose of these is to save names in a way that can be read as |
| 4371 | * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII |
| 4372 | * system to share files. |
| 4373 | * |
| 4374 | * These automatic unicode are stored slightly differently... |
| 4375 | * - If the name can fit in the ASCII character space then they are saved as |
| 4376 | * ascii names as per above. |
| 4377 | * - If the name needs Unicode then the name is saved in Unicode |
| 4378 | * starting at oh->name[1]. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4379 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4380 | */ |
| 4381 | static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name, |
| 4382 | int buffer_size) |
| 4383 | { |
| 4384 | /* Create an object name if we could not find one. */ |
| 4385 | if (yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) { |
| 4386 | YCHAR local_name[20]; |
| 4387 | YCHAR num_string[20]; |
| 4388 | YCHAR *x = &num_string[19]; |
| 4389 | unsigned v = obj->obj_id; |
| 4390 | num_string[19] = 0; |
| 4391 | while (v > 0) { |
| 4392 | x--; |
| 4393 | *x = '0' + (v % 10); |
| 4394 | v /= 10; |
| 4395 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4396 | /* make up a name */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4397 | yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX); |
| 4398 | yaffs_strcat(local_name, x); |
| 4399 | yaffs_strncpy(name, local_name, buffer_size - 1); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4400 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4401 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4402 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4403 | int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size) |
| 4404 | { |
| 4405 | memset(name, 0, buffer_size * sizeof(YCHAR)); |
| 4406 | yaffs_check_obj_details_loaded(obj); |
| 4407 | if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { |
| 4408 | yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1); |
| 4409 | } else if (obj->short_name[0]) { |
| 4410 | yaffs_strcpy(name, obj->short_name); |
| 4411 | } else if (obj->hdr_chunk > 0) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4412 | u8 *buffer = yaffs_get_temp_buffer(obj->my_dev); |
| 4413 | |
| 4414 | struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4415 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4416 | memset(buffer, 0, obj->my_dev->data_bytes_per_chunk); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4417 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4418 | if (obj->hdr_chunk > 0) { |
Anatolij Gustschin | 514c65c | 2012-10-05 23:31:03 +0000 | [diff] [blame] | 4419 | yaffs_rd_chunk_tags_nand(obj->my_dev, |
| 4420 | obj->hdr_chunk, |
| 4421 | buffer, NULL); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4422 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4423 | yaffs_load_name_from_oh(obj->my_dev, name, oh->name, |
| 4424 | buffer_size); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4425 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4426 | yaffs_release_temp_buffer(obj->my_dev, buffer); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4427 | } |
| 4428 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4429 | yaffs_fix_null_name(obj, name, buffer_size); |
| 4430 | |
| 4431 | return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4432 | } |
| 4433 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4434 | loff_t yaffs_get_obj_length(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4435 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4436 | /* Dereference any hard linking */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4437 | obj = yaffs_get_equivalent_obj(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4438 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4439 | if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) |
| 4440 | return obj->variant.file_variant.file_size; |
| 4441 | if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { |
| 4442 | if (!obj->variant.symlink_variant.alias) |
| 4443 | return 0; |
| 4444 | return yaffs_strnlen(obj->variant.symlink_variant.alias, |
| 4445 | YAFFS_MAX_ALIAS_LENGTH); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4446 | } else { |
| 4447 | /* Only a directory should drop through to here */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4448 | return obj->my_dev->data_bytes_per_chunk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4449 | } |
| 4450 | } |
| 4451 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4452 | int yaffs_get_obj_link_count(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4453 | { |
| 4454 | int count = 0; |
| 4455 | struct list_head *i; |
| 4456 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4457 | if (!obj->unlinked) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4458 | count++; /* the object itself */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4459 | |
| 4460 | list_for_each(i, &obj->hard_links) |
| 4461 | count++; /* add the hard links; */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4462 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4463 | return count; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4464 | } |
| 4465 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4466 | int yaffs_get_obj_inode(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4467 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4468 | obj = yaffs_get_equivalent_obj(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4469 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4470 | return obj->obj_id; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4471 | } |
| 4472 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4473 | unsigned yaffs_get_obj_type(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4474 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4475 | obj = yaffs_get_equivalent_obj(obj); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4476 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4477 | switch (obj->variant_type) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4478 | case YAFFS_OBJECT_TYPE_FILE: |
| 4479 | return DT_REG; |
| 4480 | break; |
| 4481 | case YAFFS_OBJECT_TYPE_DIRECTORY: |
| 4482 | return DT_DIR; |
| 4483 | break; |
| 4484 | case YAFFS_OBJECT_TYPE_SYMLINK: |
| 4485 | return DT_LNK; |
| 4486 | break; |
| 4487 | case YAFFS_OBJECT_TYPE_HARDLINK: |
| 4488 | return DT_REG; |
| 4489 | break; |
| 4490 | case YAFFS_OBJECT_TYPE_SPECIAL: |
| 4491 | if (S_ISFIFO(obj->yst_mode)) |
| 4492 | return DT_FIFO; |
| 4493 | if (S_ISCHR(obj->yst_mode)) |
| 4494 | return DT_CHR; |
| 4495 | if (S_ISBLK(obj->yst_mode)) |
| 4496 | return DT_BLK; |
| 4497 | if (S_ISSOCK(obj->yst_mode)) |
| 4498 | return DT_SOCK; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4499 | return DT_REG; |
| 4500 | break; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4501 | default: |
| 4502 | return DT_REG; |
| 4503 | break; |
| 4504 | } |
| 4505 | } |
| 4506 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4507 | YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4508 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4509 | obj = yaffs_get_equivalent_obj(obj); |
| 4510 | if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) |
| 4511 | return yaffs_clone_str(obj->variant.symlink_variant.alias); |
| 4512 | else |
| 4513 | return yaffs_clone_str(_Y("")); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4514 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4515 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4516 | /*--------------------------- Initialisation code -------------------------- */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4517 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4518 | static int yaffs_check_dev_fns(const struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4519 | { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4520 | /* Common functions, gotta have */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4521 | if (!dev->param.erase_fn || !dev->param.initialise_flash_fn) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4522 | return 0; |
| 4523 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4524 | /* Can use the "with tags" style interface for yaffs1 or yaffs2 */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4525 | if (dev->param.write_chunk_tags_fn && |
| 4526 | dev->param.read_chunk_tags_fn && |
| 4527 | !dev->param.write_chunk_fn && |
| 4528 | !dev->param.read_chunk_fn && |
| 4529 | dev->param.bad_block_fn && dev->param.query_block_fn) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4530 | return 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4531 | |
| 4532 | /* Can use the "spare" style interface for yaffs1 */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4533 | if (!dev->param.is_yaffs2 && |
| 4534 | !dev->param.write_chunk_tags_fn && |
| 4535 | !dev->param.read_chunk_tags_fn && |
| 4536 | dev->param.write_chunk_fn && |
| 4537 | dev->param.read_chunk_fn && |
| 4538 | !dev->param.bad_block_fn && !dev->param.query_block_fn) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4539 | return 1; |
| 4540 | |
| 4541 | return 0; /* bad */ |
| 4542 | } |
| 4543 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4544 | static int yaffs_create_initial_dir(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4545 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4546 | /* Initialise the unlinked, deleted, root and lost+found directories */ |
| 4547 | dev->lost_n_found = dev->root_dir = NULL; |
| 4548 | dev->unlinked_dir = dev->del_dir = NULL; |
| 4549 | dev->unlinked_dir = |
| 4550 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR); |
| 4551 | dev->del_dir = |
| 4552 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR); |
| 4553 | dev->root_dir = |
| 4554 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT, |
| 4555 | YAFFS_ROOT_MODE | S_IFDIR); |
| 4556 | dev->lost_n_found = |
| 4557 | yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND, |
| 4558 | YAFFS_LOSTNFOUND_MODE | S_IFDIR); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4559 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4560 | if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir |
| 4561 | && dev->del_dir) { |
| 4562 | yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4563 | return YAFFS_OK; |
| 4564 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4565 | return YAFFS_FAIL; |
| 4566 | } |
| 4567 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4568 | int yaffs_guts_initialise(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4569 | { |
| 4570 | int init_failed = 0; |
| 4571 | unsigned x; |
| 4572 | int bits; |
| 4573 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4574 | yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4575 | |
| 4576 | /* Check stuff that must be set */ |
| 4577 | |
| 4578 | if (!dev) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4579 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 4580 | "yaffs: Need a device" |
| 4581 | ); |
| 4582 | return YAFFS_FAIL; |
| 4583 | } |
| 4584 | |
| 4585 | if (dev->is_mounted) { |
| 4586 | yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4587 | return YAFFS_FAIL; |
| 4588 | } |
| 4589 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4590 | dev->internal_start_block = dev->param.start_block; |
| 4591 | dev->internal_end_block = dev->param.end_block; |
| 4592 | dev->block_offset = 0; |
| 4593 | dev->chunk_offset = 0; |
| 4594 | dev->n_free_chunks = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4595 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4596 | dev->gc_block = 0; |
| 4597 | |
| 4598 | if (dev->param.start_block == 0) { |
| 4599 | dev->internal_start_block = dev->param.start_block + 1; |
| 4600 | dev->internal_end_block = dev->param.end_block + 1; |
| 4601 | dev->block_offset = 1; |
| 4602 | dev->chunk_offset = dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | /* Check geometry parameters. */ |
| 4606 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4607 | if ((!dev->param.inband_tags && dev->param.is_yaffs2 && |
| 4608 | dev->param.total_bytes_per_chunk < 1024) || |
| 4609 | (!dev->param.is_yaffs2 && |
| 4610 | dev->param.total_bytes_per_chunk < 512) || |
| 4611 | (dev->param.inband_tags && !dev->param.is_yaffs2) || |
| 4612 | dev->param.chunks_per_block < 2 || |
| 4613 | dev->param.n_reserved_blocks < 2 || |
| 4614 | dev->internal_start_block <= 0 || |
| 4615 | dev->internal_end_block <= 0 || |
| 4616 | dev->internal_end_block <= |
| 4617 | (dev->internal_start_block + dev->param.n_reserved_blocks + 2) |
| 4618 | ) { |
| 4619 | /* otherwise it is too small */ |
| 4620 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 4621 | "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ", |
| 4622 | dev->param.total_bytes_per_chunk, |
| 4623 | dev->param.is_yaffs2 ? "2" : "", |
| 4624 | dev->param.inband_tags); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4625 | return YAFFS_FAIL; |
| 4626 | } |
| 4627 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4628 | if (yaffs_init_nand(dev) != YAFFS_OK) { |
| 4629 | yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4630 | return YAFFS_FAIL; |
| 4631 | } |
| 4632 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4633 | /* Sort out space for inband tags, if required */ |
| 4634 | if (dev->param.inband_tags) |
| 4635 | dev->data_bytes_per_chunk = |
| 4636 | dev->param.total_bytes_per_chunk - |
| 4637 | sizeof(struct yaffs_packed_tags2_tags_only); |
| 4638 | else |
| 4639 | dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk; |
| 4640 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4641 | /* Got the right mix of functions? */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4642 | if (!yaffs_check_dev_fns(dev)) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4643 | /* Function missing */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4644 | yaffs_trace(YAFFS_TRACE_ALWAYS, |
| 4645 | "device function(s) missing or wrong"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4646 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4647 | return YAFFS_FAIL; |
| 4648 | } |
| 4649 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4650 | /* Finished with most checks. Further checks happen later on too. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4651 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4652 | dev->is_mounted = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4653 | |
| 4654 | /* OK now calculate a few things for the device */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4655 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4656 | /* |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4657 | * Calculate all the chunk size manipulation numbers: |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4658 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4659 | x = dev->data_bytes_per_chunk; |
| 4660 | /* We always use dev->chunk_shift and dev->chunk_div */ |
| 4661 | dev->chunk_shift = calc_shifts(x); |
| 4662 | x >>= dev->chunk_shift; |
| 4663 | dev->chunk_div = x; |
| 4664 | /* We only use chunk mask if chunk_div is 1 */ |
| 4665 | dev->chunk_mask = (1 << dev->chunk_shift) - 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4666 | |
| 4667 | /* |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4668 | * Calculate chunk_grp_bits. |
| 4669 | * We need to find the next power of 2 > than internal_end_block |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4670 | */ |
| 4671 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4672 | x = dev->param.chunks_per_block * (dev->internal_end_block + 1); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4673 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4674 | bits = calc_shifts_ceiling(x); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4675 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4676 | /* Set up tnode width if wide tnodes are enabled. */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4677 | if (!dev->param.wide_tnodes_disabled) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4678 | /* bits must be even so that we end up with 32-bit words */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4679 | if (bits & 1) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4680 | bits++; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4681 | if (bits < 16) |
| 4682 | dev->tnode_width = 16; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4683 | else |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4684 | dev->tnode_width = bits; |
| 4685 | } else { |
| 4686 | dev->tnode_width = 16; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4687 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4688 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4689 | dev->tnode_mask = (1 << dev->tnode_width) - 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4690 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4691 | /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled), |
| 4692 | * so if the bitwidth of the |
| 4693 | * chunk range we're using is greater than 16 we need |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4694 | * to figure out chunk shift and chunk_grp_size |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4695 | */ |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4696 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4697 | if (bits <= dev->tnode_width) |
| 4698 | dev->chunk_grp_bits = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4699 | else |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4700 | dev->chunk_grp_bits = bits - dev->tnode_width; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4701 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4702 | dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8; |
| 4703 | if (dev->tnode_size < sizeof(struct yaffs_tnode)) |
| 4704 | dev->tnode_size = sizeof(struct yaffs_tnode); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4705 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4706 | dev->chunk_grp_size = 1 << dev->chunk_grp_bits; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4707 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4708 | if (dev->param.chunks_per_block < dev->chunk_grp_size) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4709 | /* We have a problem because the soft delete won't work if |
| 4710 | * the chunk group size > chunks per block. |
| 4711 | * This can be remedied by using larger "virtual blocks". |
| 4712 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4713 | yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large"); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4714 | |
| 4715 | return YAFFS_FAIL; |
| 4716 | } |
| 4717 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4718 | /* Finished verifying the device, continue with initialisation */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4719 | |
| 4720 | /* More device initialisation */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4721 | dev->all_gcs = 0; |
| 4722 | dev->passive_gc_count = 0; |
| 4723 | dev->oldest_dirty_gc_count = 0; |
| 4724 | dev->bg_gcs = 0; |
| 4725 | dev->gc_block_finder = 0; |
| 4726 | dev->buffered_block = -1; |
| 4727 | dev->doing_buffered_block_rewrite = 0; |
| 4728 | dev->n_deleted_files = 0; |
| 4729 | dev->n_bg_deletions = 0; |
| 4730 | dev->n_unlinked_files = 0; |
| 4731 | dev->n_ecc_fixed = 0; |
| 4732 | dev->n_ecc_unfixed = 0; |
| 4733 | dev->n_tags_ecc_fixed = 0; |
| 4734 | dev->n_tags_ecc_unfixed = 0; |
| 4735 | dev->n_erase_failures = 0; |
| 4736 | dev->n_erased_blocks = 0; |
| 4737 | dev->gc_disable = 0; |
| 4738 | dev->has_pending_prioritised_gc = 1; |
| 4739 | /* Assume the worst for now, will get fixed on first GC */ |
| 4740 | INIT_LIST_HEAD(&dev->dirty_dirs); |
| 4741 | dev->oldest_dirty_seq = 0; |
| 4742 | dev->oldest_dirty_block = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4743 | |
| 4744 | /* Initialise temporary buffers and caches. */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4745 | if (!yaffs_init_tmp_buffers(dev)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4746 | init_failed = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4747 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4748 | dev->cache = NULL; |
| 4749 | dev->gc_cleanup_list = NULL; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4750 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4751 | if (!init_failed && dev->param.n_caches > 0) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4752 | int i; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4753 | void *buf; |
| 4754 | int cache_bytes = |
| 4755 | dev->param.n_caches * sizeof(struct yaffs_cache); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4756 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4757 | if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES) |
| 4758 | dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4759 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4760 | dev->cache = kmalloc(cache_bytes, GFP_NOFS); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4761 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4762 | buf = (u8 *) dev->cache; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4763 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4764 | if (dev->cache) |
| 4765 | memset(dev->cache, 0, cache_bytes); |
| 4766 | |
| 4767 | for (i = 0; i < dev->param.n_caches && buf; i++) { |
| 4768 | dev->cache[i].object = NULL; |
| 4769 | dev->cache[i].last_use = 0; |
| 4770 | dev->cache[i].dirty = 0; |
| 4771 | dev->cache[i].data = buf = |
| 4772 | kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4773 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4774 | if (!buf) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4775 | init_failed = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4776 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4777 | dev->cache_last_use = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4778 | } |
| 4779 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4780 | dev->cache_hits = 0; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4781 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4782 | if (!init_failed) { |
| 4783 | dev->gc_cleanup_list = |
| 4784 | kmalloc(dev->param.chunks_per_block * sizeof(u32), |
| 4785 | GFP_NOFS); |
| 4786 | if (!dev->gc_cleanup_list) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4787 | init_failed = 1; |
| 4788 | } |
| 4789 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4790 | if (dev->param.is_yaffs2) |
| 4791 | dev->param.use_header_file_size = 1; |
| 4792 | |
| 4793 | if (!init_failed && !yaffs_init_blocks(dev)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4794 | init_failed = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4795 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4796 | yaffs_init_tnodes_and_objs(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4797 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4798 | if (!init_failed && !yaffs_create_initial_dir(dev)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4799 | init_failed = 1; |
| 4800 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4801 | if (!init_failed && dev->param.is_yaffs2 && |
| 4802 | !dev->param.disable_summary && |
| 4803 | !yaffs_summary_init(dev)) |
| 4804 | init_failed = 1; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4805 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4806 | if (!init_failed) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4807 | /* Now scan the flash. */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4808 | if (dev->param.is_yaffs2) { |
| 4809 | if (yaffs2_checkpt_restore(dev)) { |
| 4810 | yaffs_check_obj_details_loaded(dev->root_dir); |
| 4811 | yaffs_trace(YAFFS_TRACE_CHECKPOINT | |
| 4812 | YAFFS_TRACE_MOUNT, |
| 4813 | "yaffs: restored from checkpoint" |
| 4814 | ); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4815 | } else { |
| 4816 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4817 | /* Clean up the mess caused by an aborted |
| 4818 | * checkpoint load then scan backwards. |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4819 | */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4820 | yaffs_deinit_blocks(dev); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4821 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4822 | yaffs_deinit_tnodes_and_objs(dev); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4823 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4824 | dev->n_erased_blocks = 0; |
| 4825 | dev->n_free_chunks = 0; |
| 4826 | dev->alloc_block = -1; |
| 4827 | dev->alloc_page = -1; |
| 4828 | dev->n_deleted_files = 0; |
| 4829 | dev->n_unlinked_files = 0; |
| 4830 | dev->n_bg_deletions = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4831 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4832 | if (!init_failed && !yaffs_init_blocks(dev)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4833 | init_failed = 1; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4834 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4835 | yaffs_init_tnodes_and_objs(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4836 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4837 | if (!init_failed |
| 4838 | && !yaffs_create_initial_dir(dev)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4839 | init_failed = 1; |
| 4840 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4841 | if (!init_failed && !yaffs2_scan_backwards(dev)) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4842 | init_failed = 1; |
| 4843 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4844 | } else if (!yaffs1_scan(dev)) { |
| 4845 | init_failed = 1; |
| 4846 | } |
| 4847 | |
| 4848 | yaffs_strip_deleted_objs(dev); |
| 4849 | yaffs_fix_hanging_objs(dev); |
| 4850 | if (dev->param.empty_lost_n_found) |
| 4851 | yaffs_empty_l_n_f(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4852 | } |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4853 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4854 | if (init_failed) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4855 | /* Clean up the mess */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4856 | yaffs_trace(YAFFS_TRACE_TRACING, |
| 4857 | "yaffs: yaffs_guts_initialise() aborted."); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4858 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4859 | yaffs_deinitialise(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4860 | return YAFFS_FAIL; |
| 4861 | } |
| 4862 | |
| 4863 | /* Zero out stats */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4864 | dev->n_page_reads = 0; |
| 4865 | dev->n_page_writes = 0; |
| 4866 | dev->n_erasures = 0; |
| 4867 | dev->n_gc_copies = 0; |
| 4868 | dev->n_retried_writes = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4869 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4870 | dev->n_retired_blocks = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4871 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4872 | yaffs_verify_free_chunks(dev); |
| 4873 | yaffs_verify_blocks(dev); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4874 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4875 | /* Clean up any aborted checkpoint data */ |
| 4876 | if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0) |
| 4877 | yaffs2_checkpt_invalidate(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4878 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4879 | yaffs_trace(YAFFS_TRACE_TRACING, |
| 4880 | "yaffs: yaffs_guts_initialise() done."); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4881 | return YAFFS_OK; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4882 | } |
| 4883 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4884 | void yaffs_deinitialise(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4885 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4886 | if (dev->is_mounted) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4887 | int i; |
| 4888 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4889 | yaffs_deinit_blocks(dev); |
| 4890 | yaffs_deinit_tnodes_and_objs(dev); |
| 4891 | yaffs_summary_deinit(dev); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4892 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4893 | if (dev->param.n_caches > 0 && dev->cache) { |
| 4894 | |
| 4895 | for (i = 0; i < dev->param.n_caches; i++) { |
| 4896 | kfree(dev->cache[i].data); |
| 4897 | dev->cache[i].data = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4898 | } |
| 4899 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4900 | kfree(dev->cache); |
| 4901 | dev->cache = NULL; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4902 | } |
| 4903 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4904 | kfree(dev->gc_cleanup_list); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4905 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4906 | for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) |
| 4907 | kfree(dev->temp_buffer[i].buffer); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4908 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4909 | dev->is_mounted = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4910 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4911 | if (dev->param.deinitialise_flash_fn) |
| 4912 | dev->param.deinitialise_flash_fn(dev); |
| 4913 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4916 | int yaffs_count_free_chunks(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4917 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4918 | int n_free = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4919 | int b; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4920 | struct yaffs_block_info *blk; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4921 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4922 | blk = dev->block_info; |
| 4923 | for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) { |
| 4924 | switch (blk->block_state) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4925 | case YAFFS_BLOCK_STATE_EMPTY: |
| 4926 | case YAFFS_BLOCK_STATE_ALLOCATING: |
| 4927 | case YAFFS_BLOCK_STATE_COLLECTING: |
| 4928 | case YAFFS_BLOCK_STATE_FULL: |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4929 | n_free += |
| 4930 | (dev->param.chunks_per_block - blk->pages_in_use + |
| 4931 | blk->soft_del_pages); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4932 | break; |
| 4933 | default: |
| 4934 | break; |
| 4935 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4936 | blk++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4937 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4938 | return n_free; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4939 | } |
| 4940 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4941 | int yaffs_get_n_free_chunks(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4942 | { |
| 4943 | /* This is what we report to the outside world */ |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4944 | int n_free; |
| 4945 | int n_dirty_caches; |
| 4946 | int blocks_for_checkpt; |
| 4947 | int i; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4948 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4949 | n_free = dev->n_free_chunks; |
| 4950 | n_free += dev->n_deleted_files; |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4951 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4952 | /* Now count and subtract the number of dirty chunks in the cache. */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4953 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4954 | for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) { |
| 4955 | if (dev->cache[i].dirty) |
| 4956 | n_dirty_caches++; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4957 | } |
| 4958 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4959 | n_free -= n_dirty_caches; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4960 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4961 | n_free -= |
| 4962 | ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4963 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4964 | /* Now figure checkpoint space and report that... */ |
| 4965 | blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev); |
Wolfgang Denk | 74e0dde | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 4966 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4967 | n_free -= (blocks_for_checkpt * dev->param.chunks_per_block); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4968 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4969 | if (n_free < 0) |
| 4970 | n_free = 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4971 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4972 | return n_free; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4973 | } |
| 4974 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4975 | /*\ |
| 4976 | * Marshalling functions to get loff_t file sizes into aand out of |
| 4977 | * object headers. |
| 4978 | */ |
| 4979 | void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4980 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4981 | oh->file_size_low = (fsize & 0xFFFFFFFF); |
| 4982 | oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4985 | loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4986 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4987 | loff_t retval; |
| 4988 | |
| 4989 | if (~(oh->file_size_high)) |
| 4990 | retval = (((loff_t) oh->file_size_high) << 32) | |
| 4991 | (((loff_t) oh->file_size_low) & 0xFFFFFFFF); |
| 4992 | else |
| 4993 | retval = (loff_t) oh->file_size_low; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4994 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4995 | return retval; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 4996 | } |