blob: e89d02513c19d0e459ca7923b1a2e93c15b45d22 [file] [log] [blame]
William Juulc051bbe2007-11-15 11:13:05 +01001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
Charles Manning3796e1f2012-05-09 16:55:17 +00004 * Copyright (C) 2002-2011 Aleph One Ltd.
William Juulc051bbe2007-11-15 11:13:05 +01005 * 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 Juulc051bbe2007-11-15 11:13:05 +010014#include "yportenv.h"
Charles Manning3796e1f2012-05-09 16:55:17 +000015#include "yaffs_trace.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <dm/devres.h>
William Juulc051bbe2007-11-15 11:13:05 +010017
William Juulc051bbe2007-11-15 11:13:05 +010018#include "yaffs_guts.h"
Charles Manning3796e1f2012-05-09 16:55:17 +000019#include "yaffs_getblockinfo.h"
William Juulc051bbe2007-11-15 11:13:05 +010020#include "yaffs_tagscompat.h"
William Juulc051bbe2007-11-15 11:13:05 +010021#include "yaffs_nand.h"
Charles Manning3796e1f2012-05-09 16:55:17 +000022#include "yaffs_yaffs1.h"
23#include "yaffs_yaffs2.h"
24#include "yaffs_bitmap.h"
25#include "yaffs_verify.h"
William Juulc051bbe2007-11-15 11:13:05 +010026#include "yaffs_nand.h"
27#include "yaffs_packedtags2.h"
Charles Manning3796e1f2012-05-09 16:55:17 +000028#include "yaffs_nameval.h"
29#include "yaffs_allocator.h"
30#include "yaffs_attribs.h"
31#include "yaffs_summary.h"
William Juulc051bbe2007-11-15 11:13:05 +010032
Charles Manning3796e1f2012-05-09 16:55:17 +000033/* 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 Juulc051bbe2007-11-15 11:13:05 +010036
37#include "yaffs_ecc.h"
38
Charles Manning3796e1f2012-05-09 16:55:17 +000039/* Forward declarations */
William Juulc051bbe2007-11-15 11:13:05 +010040
Charles Manning3796e1f2012-05-09 16:55:17 +000041static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
42 const u8 *buffer, int n_bytes, int use_reserve);
William Juulc051bbe2007-11-15 11:13:05 +010043
Charles Manning3796e1f2012-05-09 16:55:17 +000044/* Function to calculate chunk and offset */
William Juulc051bbe2007-11-15 11:13:05 +010045
Charles Manning3796e1f2012-05-09 16:55:17 +000046void 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 Juulc051bbe2007-11-15 11:13:05 +010051
Charles Manning3796e1f2012-05-09 16:55:17 +000052 chunk = (u32) (addr >> dev->chunk_shift);
William Juulc051bbe2007-11-15 11:13:05 +010053
Charles Manning3796e1f2012-05-09 16:55:17 +000054 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 Juulc051bbe2007-11-15 11:13:05 +010059
Charles Manning3796e1f2012-05-09 16:55:17 +000060 loff_t chunk_base;
William Juulc051bbe2007-11-15 11:13:05 +010061
Charles Manning3796e1f2012-05-09 16:55:17 +000062 chunk /= dev->chunk_div;
William Juulc051bbe2007-11-15 11:13:05 +010063
Charles Manning3796e1f2012-05-09 16:55:17 +000064 chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
65 offset = (u32) (addr - chunk_base);
William Juulc051bbe2007-11-15 11:13:05 +010066 }
Charles Manning3796e1f2012-05-09 16:55:17 +000067
68 *chunk_out = chunk;
69 *offset_out = offset;
William Juulc051bbe2007-11-15 11:13:05 +010070}
71
Charles Manning3796e1f2012-05-09 16:55:17 +000072/* Function to return the number of shifts for a power of 2 greater than or
73 * equal to the given number
William Juulc051bbe2007-11-15 11:13:05 +010074 * Note we don't try to cater for all possible numbers and this does not have to
75 * be hellishly efficient.
76 */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020077
Charles Manning3796e1f2012-05-09 16:55:17 +000078static inline u32 calc_shifts_ceiling(u32 x)
William Juulc051bbe2007-11-15 11:13:05 +010079{
Charles Manning3796e1f2012-05-09 16:55:17 +000080 int extra_bits;
81 int shifts;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020082
Charles Manning3796e1f2012-05-09 16:55:17 +000083 shifts = extra_bits = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020084
Charles Manning3796e1f2012-05-09 16:55:17 +000085 while (x > 1) {
86 if (x & 1)
87 extra_bits++;
88 x >>= 1;
89 shifts++;
William Juulc051bbe2007-11-15 11:13:05 +010090 }
91
Charles Manning3796e1f2012-05-09 16:55:17 +000092 if (extra_bits)
93 shifts++;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020094
Charles Manning3796e1f2012-05-09 16:55:17 +000095 return shifts;
William Juulc051bbe2007-11-15 11:13:05 +010096}
97
98/* Function to return the number of shifts to get a 1 in bit 0
99 */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200100
Charles Manning3796e1f2012-05-09 16:55:17 +0000101static inline u32 calc_shifts(u32 x)
William Juulc051bbe2007-11-15 11:13:05 +0100102{
Charles Manning3796e1f2012-05-09 16:55:17 +0000103 u32 shifts;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200104
Charles Manning3796e1f2012-05-09 16:55:17 +0000105 shifts = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200106
Charles Manning3796e1f2012-05-09 16:55:17 +0000107 if (!x)
108 return 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200109
Charles Manning3796e1f2012-05-09 16:55:17 +0000110 while (!(x & 1)) {
111 x >>= 1;
112 shifts++;
William Juulc051bbe2007-11-15 11:13:05 +0100113 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200114
Charles Manning3796e1f2012-05-09 16:55:17 +0000115 return shifts;
William Juulc051bbe2007-11-15 11:13:05 +0100116}
117
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200118/*
William Juulc051bbe2007-11-15 11:13:05 +0100119 * Temporary buffer manipulations.
120 */
121
Charles Manning3796e1f2012-05-09 16:55:17 +0000122static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100123{
124 int i;
Charles Manning3796e1f2012-05-09 16:55:17 +0000125 u8 *buf = (u8 *) 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200126
Charles Manning3796e1f2012-05-09 16:55:17 +0000127 memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200128
William Juulc051bbe2007-11-15 11:13:05 +0100129 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
Charles Manning3796e1f2012-05-09 16:55:17 +0000130 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 Juulc051bbe2007-11-15 11:13:05 +0100133 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200134
William Juulc051bbe2007-11-15 11:13:05 +0100135 return buf ? YAFFS_OK : YAFFS_FAIL;
William Juulc051bbe2007-11-15 11:13:05 +0100136}
137
Charles Manning3796e1f2012-05-09 16:55:17 +0000138u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
William Juulc051bbe2007-11-15 11:13:05 +0100139{
Charles Manning3796e1f2012-05-09 16:55:17 +0000140 int i;
William Juulc051bbe2007-11-15 11:13:05 +0100141
Charles Manning3796e1f2012-05-09 16:55:17 +0000142 dev->temp_in_use++;
143 if (dev->temp_in_use > dev->max_temp)
144 dev->max_temp = dev->temp_in_use;
William Juulc051bbe2007-11-15 11:13:05 +0100145
William Juulc051bbe2007-11-15 11:13:05 +0100146 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
Charles Manning3796e1f2012-05-09 16:55:17 +0000147 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 Juulc051bbe2007-11-15 11:13:05 +0100151 }
William Juulc051bbe2007-11-15 11:13:05 +0100152
Charles Manning3796e1f2012-05-09 16:55:17 +0000153 yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
William Juulc051bbe2007-11-15 11:13:05 +0100154 /*
155 * If we got here then we have to allocate an unmanaged one
156 * This is not good.
157 */
158
Charles Manning3796e1f2012-05-09 16:55:17 +0000159 dev->unmanaged_buffer_allocs++;
160 return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
William Juulc051bbe2007-11-15 11:13:05 +0100161
162}
163
Charles Manning3796e1f2012-05-09 16:55:17 +0000164void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
William Juulc051bbe2007-11-15 11:13:05 +0100165{
166 int i;
Charles Manning3796e1f2012-05-09 16:55:17 +0000167
168 dev->temp_in_use--;
169
William Juulc051bbe2007-11-15 11:13:05 +0100170 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
Charles Manning3796e1f2012-05-09 16:55:17 +0000171 if (dev->temp_buffer[i].buffer == buffer) {
172 dev->temp_buffer[i].in_use = 0;
William Juulc051bbe2007-11-15 11:13:05 +0100173 return;
174 }
175 }
176
177 if (buffer) {
178 /* assume it is an unmanaged one. */
Charles Manning3796e1f2012-05-09 16:55:17 +0000179 yaffs_trace(YAFFS_TRACE_BUFFERS,
180 "Releasing unmanaged temp buffer");
181 kfree(buffer);
182 dev->unmanaged_buffer_deallocs++;
William Juulc051bbe2007-11-15 11:13:05 +0100183 }
184
185}
186
187/*
188 * Determine if we have a managed buffer.
189 */
Charles Manning3796e1f2012-05-09 16:55:17 +0000190int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer)
William Juulc051bbe2007-11-15 11:13:05 +0100191{
192 int i;
Charles Manning3796e1f2012-05-09 16:55:17 +0000193
William Juulc051bbe2007-11-15 11:13:05 +0100194 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
Charles Manning3796e1f2012-05-09 16:55:17 +0000195 if (dev->temp_buffer[i].buffer == buffer)
William Juulc051bbe2007-11-15 11:13:05 +0100196 return 1;
William Juulc051bbe2007-11-15 11:13:05 +0100197 }
198
Charles Manning3796e1f2012-05-09 16:55:17 +0000199 for (i = 0; i < dev->param.n_caches; i++) {
200 if (dev->cache[i].data == buffer)
201 return 1;
202 }
William Juulc051bbe2007-11-15 11:13:05 +0100203
Charles Manning3796e1f2012-05-09 16:55:17 +0000204 if (buffer == dev->checkpt_buffer)
205 return 1;
William Juulc051bbe2007-11-15 11:13:05 +0100206
Charles Manning3796e1f2012-05-09 16:55:17 +0000207 yaffs_trace(YAFFS_TRACE_ALWAYS,
208 "yaffs: unmaged buffer detected.");
209 return 0;
William Juulc051bbe2007-11-15 11:13:05 +0100210}
211
William Juulc051bbe2007-11-15 11:13:05 +0100212/*
Charles Manning3796e1f2012-05-09 16:55:17 +0000213 * Functions for robustisizing TODO
214 *
William Juulc051bbe2007-11-15 11:13:05 +0100215 */
216
Charles Manning3796e1f2012-05-09 16:55:17 +0000217static 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 Juulc051bbe2007-11-15 11:13:05 +0100220{
William Juulc051bbe2007-11-15 11:13:05 +0100221}
222
Charles Manning3796e1f2012-05-09 16:55:17 +0000223static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
224 const struct yaffs_ext_tags *tags)
William Juulc051bbe2007-11-15 11:13:05 +0100225{
William Juulc051bbe2007-11-15 11:13:05 +0100226}
227
Charles Manning3796e1f2012-05-09 16:55:17 +0000228void yaffs_handle_chunk_error(struct yaffs_dev *dev,
229 struct yaffs_block_info *bi)
William Juulc051bbe2007-11-15 11:13:05 +0100230{
Charles Manning3796e1f2012-05-09 16:55:17 +0000231 if (!bi->gc_prioritise) {
232 bi->gc_prioritise = 1;
233 dev->has_pending_prioritised_gc = 1;
234 bi->chunk_error_strikes++;
William Juulc051bbe2007-11-15 11:13:05 +0100235
Charles Manning3796e1f2012-05-09 16:55:17 +0000236 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 Juulc051bbe2007-11-15 11:13:05 +0100243}
244
Charles Manning3796e1f2012-05-09 16:55:17 +0000245static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
246 int erased_ok)
William Juulc051bbe2007-11-15 11:13:05 +0100247{
Charles Manning3796e1f2012-05-09 16:55:17 +0000248 int flash_block = nand_chunk / dev->param.chunks_per_block;
249 struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
William Juulc051bbe2007-11-15 11:13:05 +0100250
Charles Manning3796e1f2012-05-09 16:55:17 +0000251 yaffs_handle_chunk_error(dev, bi);
William Juulc051bbe2007-11-15 11:13:05 +0100252
Charles Manning3796e1f2012-05-09 16:55:17 +0000253 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 Juulc051bbe2007-11-15 11:13:05 +0100260
Charles Manning3796e1f2012-05-09 16:55:17 +0000261 /* Delete the chunk */
262 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
263 yaffs_skip_rest_of_block(dev);
264}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200265
Charles Manning3796e1f2012-05-09 16:55:17 +0000266/*
267 * Verification code
268 */
William Juulc051bbe2007-11-15 11:13:05 +0100269
Charles Manning3796e1f2012-05-09 16:55:17 +0000270/*
271 * Simple hash function. Needs to have a reasonable spread
272 */
William Juulc051bbe2007-11-15 11:13:05 +0100273
Charles Manning3796e1f2012-05-09 16:55:17 +0000274static inline int yaffs_hash_fn(int n)
William Juulc051bbe2007-11-15 11:13:05 +0100275{
Charles Manning3796e1f2012-05-09 16:55:17 +0000276 if (n < 0)
277 n = -n;
278 return n % YAFFS_NOBJECT_BUCKETS;
William Juulc051bbe2007-11-15 11:13:05 +0100279}
280
Charles Manning3796e1f2012-05-09 16:55:17 +0000281/*
282 * Access functions to useful fake objects.
283 * Note that root might have a presence in NAND if permissions are set.
284 */
285
286struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100287{
Charles Manning3796e1f2012-05-09 16:55:17 +0000288 return dev->root_dir;
William Juulc051bbe2007-11-15 11:13:05 +0100289}
290
Charles Manning3796e1f2012-05-09 16:55:17 +0000291struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100292{
Charles Manning3796e1f2012-05-09 16:55:17 +0000293 return dev->lost_n_found;
William Juulc051bbe2007-11-15 11:13:05 +0100294}
295
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200296/*
Charles Manning3796e1f2012-05-09 16:55:17 +0000297 * Erased NAND checking functions
William Juulc051bbe2007-11-15 11:13:05 +0100298 */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200299
Charles Manning3796e1f2012-05-09 16:55:17 +0000300int yaffs_check_ff(u8 *buffer, int n_bytes)
William Juulc051bbe2007-11-15 11:13:05 +0100301{
Charles Manning3796e1f2012-05-09 16:55:17 +0000302 /* Horrible, slow implementation */
303 while (n_bytes--) {
304 if (*buffer != 0xff)
305 return 0;
306 buffer++;
307 }
308 return 1;
William Juulc051bbe2007-11-15 11:13:05 +0100309}
310
Charles Manning3796e1f2012-05-09 16:55:17 +0000311static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
William Juulc051bbe2007-11-15 11:13:05 +0100312{
Charles Manning3796e1f2012-05-09 16:55:17 +0000313 int retval = YAFFS_OK;
314 u8 *data = yaffs_get_temp_buffer(dev);
315 struct yaffs_ext_tags tags;
Charles Manning3796e1f2012-05-09 16:55:17 +0000316
Anatolij Gustschin514c65c2012-10-05 23:31:03 +0000317 yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
Charles Manning3796e1f2012-05-09 16:55:17 +0000318
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 Juulc051bbe2007-11-15 11:13:05 +0100333}
334
Charles Manning3796e1f2012-05-09 16:55:17 +0000335static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
336 int nand_chunk,
337 const u8 *data,
338 struct yaffs_ext_tags *tags)
William Juulc051bbe2007-11-15 11:13:05 +0100339{
Charles Manning3796e1f2012-05-09 16:55:17 +0000340 int retval = YAFFS_OK;
341 struct yaffs_ext_tags temp_tags;
342 u8 *buffer = yaffs_get_temp_buffer(dev);
Charles Manning3796e1f2012-05-09 16:55:17 +0000343
Anatolij Gustschin514c65c2012-10-05 23:31:03 +0000344 yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
Charles Manning3796e1f2012-05-09 16:55:17 +0000345 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 Juulc051bbe2007-11-15 11:13:05 +0100354}
355
Charles Manning3796e1f2012-05-09 16:55:17 +0000356int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
William Juulc051bbe2007-11-15 11:13:05 +0100357{
Charles Manning3796e1f2012-05-09 16:55:17 +0000358 int reserved_chunks;
359 int reserved_blocks = dev->param.n_reserved_blocks;
360 int checkpt_blocks;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200361
Charles Manning3796e1f2012-05-09 16:55:17 +0000362 checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200363
Charles Manning3796e1f2012-05-09 16:55:17 +0000364 reserved_chunks =
365 (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200366
Charles Manning3796e1f2012-05-09 16:55:17 +0000367 return (dev->n_free_chunks > (reserved_chunks + n_chunks));
368}
369
370static int yaffs_find_alloc_block(struct yaffs_dev *dev)
371{
372 int i;
373 struct yaffs_block_info *bi;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200374
Charles Manning3796e1f2012-05-09 16:55:17 +0000375 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 Denk74e0dde2008-08-14 14:41:06 +0200381
Charles Manning3796e1f2012-05-09 16:55:17 +0000382 return -1;
383 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200384
Charles Manning3796e1f2012-05-09 16:55:17 +0000385 /* Find an empty block. */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200386
Charles Manning3796e1f2012-05-09 16:55:17 +0000387 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 Denk74e0dde2008-08-14 14:41:06 +0200393
Charles Manning3796e1f2012-05-09 16:55:17 +0000394 bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200395
Charles Manning3796e1f2012-05-09 16:55:17 +0000396 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 Denk74e0dde2008-08-14 14:41:06 +0200408
Charles Manning3796e1f2012-05-09 16:55:17 +0000409 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 Juulc051bbe2007-11-15 11:13:05 +0100414}
415
Charles Manning3796e1f2012-05-09 16:55:17 +0000416static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
417 struct yaffs_block_info **block_ptr)
William Juulc051bbe2007-11-15 11:13:05 +0100418{
Charles Manning3796e1f2012-05-09 16:55:17 +0000419 int ret_val;
420 struct yaffs_block_info *bi;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200421
Charles Manning3796e1f2012-05-09 16:55:17 +0000422 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 Denk74e0dde2008-08-14 14:41:06 +0200427
Charles Manning3796e1f2012-05-09 16:55:17 +0000428 if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
429 /* No space unless we're allowed to use the reserve. */
430 return -1;
William Juulc051bbe2007-11-15 11:13:05 +0100431 }
William Juulc051bbe2007-11-15 11:13:05 +0100432
Charles Manning3796e1f2012-05-09 16:55:17 +0000433 if (dev->n_erased_blocks < dev->param.n_reserved_blocks
434 && dev->alloc_page == 0)
435 yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200436
Charles Manning3796e1f2012-05-09 16:55:17 +0000437 /* Next page please.... */
438 if (dev->alloc_block >= 0) {
439 bi = yaffs_get_block_info(dev, dev->alloc_block);
William Juulc051bbe2007-11-15 11:13:05 +0100440
Charles Manning3796e1f2012-05-09 16:55:17 +0000441 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 Juulc051bbe2007-11-15 11:13:05 +0100445
Charles Manning3796e1f2012-05-09 16:55:17 +0000446 dev->alloc_page++;
William Juulc051bbe2007-11-15 11:13:05 +0100447
Charles Manning3796e1f2012-05-09 16:55:17 +0000448 dev->n_free_chunks--;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200449
Charles Manning3796e1f2012-05-09 16:55:17 +0000450 /* 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 Juulc051bbe2007-11-15 11:13:05 +0100455
Charles Manning3796e1f2012-05-09 16:55:17 +0000456 if (block_ptr)
457 *block_ptr = bi;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200458
Charles Manning3796e1f2012-05-09 16:55:17 +0000459 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +0100460 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200461
Charles Manning3796e1f2012-05-09 16:55:17 +0000462 yaffs_trace(YAFFS_TRACE_ERROR,
463 "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
William Juulc051bbe2007-11-15 11:13:05 +0100464
Charles Manning3796e1f2012-05-09 16:55:17 +0000465 return -1;
466}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200467
Charles Manning3796e1f2012-05-09 16:55:17 +0000468static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
469{
470 int n;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200471
Charles Manning3796e1f2012-05-09 16:55:17 +0000472 n = dev->n_erased_blocks * dev->param.chunks_per_block;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200473
Charles Manning3796e1f2012-05-09 16:55:17 +0000474 if (dev->alloc_block > 0)
475 n += (dev->param.chunks_per_block - dev->alloc_page);
William Juulc051bbe2007-11-15 11:13:05 +0100476
Charles Manning3796e1f2012-05-09 16:55:17 +0000477 return n;
William Juulc051bbe2007-11-15 11:13:05 +0100478
479}
480
481/*
Charles Manning3796e1f2012-05-09 16:55:17 +0000482 * yaffs_skip_rest_of_block() skips over the rest of the allocation block
483 * if we don't want to write to it.
William Juulc051bbe2007-11-15 11:13:05 +0100484 */
Charles Manning3796e1f2012-05-09 16:55:17 +0000485void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100486{
Charles Manning3796e1f2012-05-09 16:55:17 +0000487 struct yaffs_block_info *bi;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200488
Charles Manning3796e1f2012-05-09 16:55:17 +0000489 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 Juulc051bbe2007-11-15 11:13:05 +0100495 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000496}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200497
Charles Manning3796e1f2012-05-09 16:55:17 +0000498static 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 Juulc051bbe2007-11-15 11:13:05 +0100505
Charles Manning3796e1f2012-05-09 16:55:17 +0000506 yaffs2_checkpt_invalidate(dev);
William Juulc051bbe2007-11-15 11:13:05 +0100507
Charles Manning3796e1f2012-05-09 16:55:17 +0000508 do {
509 struct yaffs_block_info *bi = 0;
510 int erased_ok = 0;
William Juulc051bbe2007-11-15 11:13:05 +0100511
Charles Manning3796e1f2012-05-09 16:55:17 +0000512 chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
513 if (chunk < 0) {
514 /* no space */
515 break;
516 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200517
Charles Manning3796e1f2012-05-09 16:55:17 +0000518 /* 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 Denk74e0dde2008-08-14 14:41:06 +0200541
Charles Manning3796e1f2012-05-09 16:55:17 +0000542 /* let's give it a try */
543 attempts++;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200544
Charles Manning3796e1f2012-05-09 16:55:17 +0000545 if (dev->param.always_check_erased)
546 bi->skip_erased_check = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200547
Charles Manning3796e1f2012-05-09 16:55:17 +0000548 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 Denk74e0dde2008-08-14 14:41:06 +0200554
Charles Manning3796e1f2012-05-09 16:55:17 +0000555 /* 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 Juulc051bbe2007-11-15 11:13:05 +0100563
Charles Manning3796e1f2012-05-09 16:55:17 +0000564 write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
William Juulc051bbe2007-11-15 11:13:05 +0100565
Charles Manning3796e1f2012-05-09 16:55:17 +0000566 if (!bi->skip_erased_check)
567 write_ok =
568 yaffs_verify_chunk_written(dev, chunk, data, tags);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200569
Charles Manning3796e1f2012-05-09 16:55:17 +0000570 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 Denk74e0dde2008-08-14 14:41:06 +0200576
Charles Manning3796e1f2012-05-09 16:55:17 +0000577 bi->skip_erased_check = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200578
Charles Manning3796e1f2012-05-09 16:55:17 +0000579 /* 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 Juulc051bbe2007-11-15 11:13:05 +0100593 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200594
Charles Manning3796e1f2012-05-09 16:55:17 +0000595 return chunk;
596}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200597
Charles Manning3796e1f2012-05-09 16:55:17 +0000598/*
599 * Block retiring for handling a broken block.
600 */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200601
Charles Manning3796e1f2012-05-09 16:55:17 +0000602static 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 Denk74e0dde2008-08-14 14:41:06 +0200605
Charles Manning3796e1f2012-05-09 16:55:17 +0000606 yaffs2_checkpt_invalidate(dev);
William Juulc051bbe2007-11-15 11:13:05 +0100607
Charles Manning3796e1f2012-05-09 16:55:17 +0000608 yaffs2_clear_oldest_dirty_seq(dev, bi);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200609
Charles Manning3796e1f2012-05-09 16:55:17 +0000610 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 Juulc051bbe2007-11-15 11:13:05 +0100619
Charles Manning3796e1f2012-05-09 16:55:17 +0000620 u8 *buffer = yaffs_get_temp_buffer(dev);
William Juulc051bbe2007-11-15 11:13:05 +0100621
Charles Manning3796e1f2012-05-09 16:55:17 +0000622 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 Juulc051bbe2007-11-15 11:13:05 +0100635 }
636
Charles Manning3796e1f2012-05-09 16:55:17 +0000637 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
638 bi->gc_prioritise = 0;
639 bi->needs_retiring = 0;
640
641 dev->n_retired_blocks++;
William Juulc051bbe2007-11-15 11:13:05 +0100642}
643
Charles Manning3796e1f2012-05-09 16:55:17 +0000644/*---------------- Name handling functions ------------*/
645
646static u16 yaffs_calc_name_sum(const YCHAR *name)
William Juulc051bbe2007-11-15 11:13:05 +0100647{
Charles Manning3796e1f2012-05-09 16:55:17 +0000648 u16 sum = 0;
649 u16 i = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200650
Charles Manning3796e1f2012-05-09 16:55:17 +0000651 if (!name)
652 return 0;
William Juulc051bbe2007-11-15 11:13:05 +0100653
Charles Manning3796e1f2012-05-09 16:55:17 +0000654 while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200655
Charles Manning3796e1f2012-05-09 16:55:17 +0000656 /* 0x1f mask is case insensitive */
657 sum += ((*name) & 0x1f) * i;
658 i++;
659 name++;
660 }
661 return sum;
William Juulc051bbe2007-11-15 11:13:05 +0100662}
663
Charles Manning3796e1f2012-05-09 16:55:17 +0000664void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
William Juulc051bbe2007-11-15 11:13:05 +0100665{
Charles Manning3796e1f2012-05-09 16:55:17 +0000666 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 Denk74e0dde2008-08-14 14:41:06 +0200675
Charles Manning3796e1f2012-05-09 16:55:17 +0000676void 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 Juulc051bbe2007-11-15 11:13:05 +0100688}
689
Charles Manning3796e1f2012-05-09 16:55:17 +0000690loff_t yaffs_max_file_size(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100691{
Charles Manning3796e1f2012-05-09 16:55:17 +0000692 return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
William Juulc051bbe2007-11-15 11:13:05 +0100693}
694
Charles Manning3796e1f2012-05-09 16:55:17 +0000695/*-------------------- TNODES -------------------
696
697 * List of spare tnodes
698 * The list is hooked together using the first pointer
699 * in the tnode.
700 */
701
702struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100703{
Charles Manning3796e1f2012-05-09 16:55:17 +0000704 struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200705
Charles Manning3796e1f2012-05-09 16:55:17 +0000706 if (tn) {
707 memset(tn, 0, dev->tnode_size);
708 dev->n_tnodes++;
709 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200710
Charles Manning3796e1f2012-05-09 16:55:17 +0000711 dev->checkpoint_blocks_required = 0; /* force recalculation */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200712
Charles Manning3796e1f2012-05-09 16:55:17 +0000713 return tn;
714}
715
716/* FreeTnode frees up a tnode and puts it back on the free list */
717static 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 Denk74e0dde2008-08-14 14:41:06 +0200723
Charles Manning3796e1f2012-05-09 16:55:17 +0000724static 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 Denk74e0dde2008-08-14 14:41:06 +0200730
Charles Manning3796e1f2012-05-09 16:55:17 +0000731void 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 Denk74e0dde2008-08-14 14:41:06 +0200742
Charles Manning3796e1f2012-05-09 16:55:17 +0000743 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 Denk74e0dde2008-08-14 14:41:06 +0200746
Charles Manning3796e1f2012-05-09 16:55:17 +0000747 mask = dev->tnode_mask << bit_in_word;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200748
Charles Manning3796e1f2012-05-09 16:55:17 +0000749 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 Juulc051bbe2007-11-15 11:13:05 +0100759 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000760}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200761
Charles Manning3796e1f2012-05-09 16:55:17 +0000762u32 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 Denk74e0dde2008-08-14 14:41:06 +0200770
Charles Manning3796e1f2012-05-09 16:55:17 +0000771 pos &= YAFFS_TNODES_LEVEL0_MASK;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200772
Charles Manning3796e1f2012-05-09 16:55:17 +0000773 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 Denk74e0dde2008-08-14 14:41:06 +0200776
Charles Manning3796e1f2012-05-09 16:55:17 +0000777 val = map[word_in_map] >> bit_in_word;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200778
Charles Manning3796e1f2012-05-09 16:55:17 +0000779 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 Juulc051bbe2007-11-15 11:13:05 +0100783 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200784
Charles Manning3796e1f2012-05-09 16:55:17 +0000785 val &= dev->tnode_mask;
786 val <<= dev->chunk_grp_bits;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200787
Charles Manning3796e1f2012-05-09 16:55:17 +0000788 return val;
789}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200790
Charles Manning3796e1f2012-05-09 16:55:17 +0000791/* ------------------- End of individual tnode manipulation -----------------*/
William Juulc051bbe2007-11-15 11:13:05 +0100792
Charles Manning3796e1f2012-05-09 16:55:17 +0000793/* ---------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 Juulc051bbe2007-11-15 11:13:05 +0100796 */
William Juulc051bbe2007-11-15 11:13:05 +0100797
Charles Manning3796e1f2012-05-09 16:55:17 +0000798/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
799struct 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 Juulc051bbe2007-11-15 11:13:05 +0100807
Charles Manning3796e1f2012-05-09 16:55:17 +0000808 /* Check sane level and chunk Id */
809 if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
810 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +0100811
Charles Manning3796e1f2012-05-09 16:55:17 +0000812 if (chunk_id > YAFFS_MAX_CHUNK_ID)
813 return NULL;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200814
Charles Manning3796e1f2012-05-09 16:55:17 +0000815 /* First check we're tall enough (ie enough top_level) */
William Juulc051bbe2007-11-15 11:13:05 +0100816
Charles Manning3796e1f2012-05-09 16:55:17 +0000817 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 Juulc051bbe2007-11-15 11:13:05 +0100823
Charles Manning3796e1f2012-05-09 16:55:17 +0000824 if (required_depth > file_struct->top_level)
825 return NULL; /* Not tall enough, so we can't find it */
William Juulc051bbe2007-11-15 11:13:05 +0100826
Charles Manning3796e1f2012-05-09 16:55:17 +0000827 /* 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 Juulc051bbe2007-11-15 11:13:05 +0100836
Charles Manning3796e1f2012-05-09 16:55:17 +0000837 return tn;
838}
William Juulc051bbe2007-11-15 11:13:05 +0100839
Charles Manning3796e1f2012-05-09 16:55:17 +0000840/* 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 Denk74e0dde2008-08-14 14:41:06 +0200851
Charles Manning3796e1f2012-05-09 16:55:17 +0000852struct 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 Denk74e0dde2008-08-14 14:41:06 +0200862
Charles Manning3796e1f2012-05-09 16:55:17 +0000863 /* 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 Juulc051bbe2007-11-15 11:13:05 +0100867
Charles Manning3796e1f2012-05-09 16:55:17 +0000868 if (chunk_id > YAFFS_MAX_CHUNK_ID)
869 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +0100870
Charles Manning3796e1f2012-05-09 16:55:17 +0000871 /* First check we're tall enough (ie enough top_level) */
William Juulc051bbe2007-11-15 11:13:05 +0100872
Charles Manning3796e1f2012-05-09 16:55:17 +0000873 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 Juulc051bbe2007-11-15 11:13:05 +0100879
Charles Manning3796e1f2012-05-09 16:55:17 +0000880 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 Juulc051bbe2007-11-15 11:13:05 +0100883
Charles Manning3796e1f2012-05-09 16:55:17 +0000884 tn = yaffs_get_tnode(dev);
William Juulc051bbe2007-11-15 11:13:05 +0100885
Charles Manning3796e1f2012-05-09 16:55:17 +0000886 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 Juulc051bbe2007-11-15 11:13:05 +0100895 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000896 }
William Juulc051bbe2007-11-15 11:13:05 +0100897
Charles Manning3796e1f2012-05-09 16:55:17 +0000898 /* Traverse down to level 0, adding anything we need */
William Juulc051bbe2007-11-15 11:13:05 +0100899
Charles Manning3796e1f2012-05-09 16:55:17 +0000900 l = file_struct->top_level;
901 tn = file_struct->top;
William Juulc051bbe2007-11-15 11:13:05 +0100902
Charles Manning3796e1f2012-05-09 16:55:17 +0000903 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 Juulc051bbe2007-11-15 11:13:05 +0100909
Charles Manning3796e1f2012-05-09 16:55:17 +0000910 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 Juulc051bbe2007-11-15 11:13:05 +0100923
Charles Manning3796e1f2012-05-09 16:55:17 +0000924 } 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 Juulc051bbe2007-11-15 11:13:05 +0100930 }
931
Charles Manning3796e1f2012-05-09 16:55:17 +0000932 tn = tn->internal[x];
933 l--;
William Juulc051bbe2007-11-15 11:13:05 +0100934 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000935 } 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 Juulc051bbe2007-11-15 11:13:05 +0100942 }
943
Charles Manning3796e1f2012-05-09 16:55:17 +0000944 return tn;
William Juulc051bbe2007-11-15 11:13:05 +0100945}
946
Charles Manning3796e1f2012-05-09 16:55:17 +0000947static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
948 int chunk_obj)
William Juulc051bbe2007-11-15 11:13:05 +0100949{
Charles Manning3796e1f2012-05-09 16:55:17 +0000950 return (tags->chunk_id == chunk_obj &&
951 tags->obj_id == obj_id &&
952 !tags->is_deleted) ? 1 : 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200953
William Juulc051bbe2007-11-15 11:13:05 +0100954}
955
Charles Manning3796e1f2012-05-09 16:55:17 +0000956static 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 Juulc051bbe2007-11-15 11:13:05 +0100959{
Charles Manning3796e1f2012-05-09 16:55:17 +0000960 int j;
William Juulc051bbe2007-11-15 11:13:05 +0100961
Charles Manning3796e1f2012-05-09 16:55:17 +0000962 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 Juulc051bbe2007-11-15 11:13:05 +0100966
Charles Manning3796e1f2012-05-09 16:55:17 +0000967 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 Juulc051bbe2007-11-15 11:13:05 +0100976 }
977 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000978 }
979 the_chunk++;
980 }
981 return -1;
982}
William Juulc051bbe2007-11-15 11:13:05 +0100983
Charles Manning3796e1f2012-05-09 16:55:17 +0000984static 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 Juulc051bbe2007-11-15 11:13:05 +0100993
Charles Manning3796e1f2012-05-09 16:55:17 +0000994 if (!tags) {
995 /* Passed a NULL, so use our own tags space */
996 tags = &local_tags;
997 }
William Juulc051bbe2007-11-15 11:13:05 +0100998
Charles Manning3796e1f2012-05-09 16:55:17 +0000999 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
William Juulc051bbe2007-11-15 11:13:05 +01001000
Charles Manning3796e1f2012-05-09 16:55:17 +00001001 if (!tn)
1002 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +01001003
Charles Manning3796e1f2012-05-09 16:55:17 +00001004 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
William Juulc051bbe2007-11-15 11:13:05 +01001005
Charles Manning3796e1f2012-05-09 16:55:17 +00001006 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1007 inode_chunk);
1008 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +01001009}
1010
Charles Manning3796e1f2012-05-09 16:55:17 +00001011static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
1012 struct yaffs_ext_tags *tags)
William Juulc051bbe2007-11-15 11:13:05 +01001013{
Charles Manning3796e1f2012-05-09 16:55:17 +00001014 /* 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 Denk74e0dde2008-08-14 14:41:06 +02001020
Charles Manning3796e1f2012-05-09 16:55:17 +00001021 if (!tags) {
1022 /* Passed a NULL, so use our own tags space */
1023 tags = &local_tags;
1024 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001025
Charles Manning3796e1f2012-05-09 16:55:17 +00001026 tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001027
Charles Manning3796e1f2012-05-09 16:55:17 +00001028 if (!tn)
1029 return ret_val;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001030
Charles Manning3796e1f2012-05-09 16:55:17 +00001031 the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
William Juulc051bbe2007-11-15 11:13:05 +01001032
Charles Manning3796e1f2012-05-09 16:55:17 +00001033 ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
1034 inode_chunk);
William Juulc051bbe2007-11-15 11:13:05 +01001035
Charles Manning3796e1f2012-05-09 16:55:17 +00001036 /* Delete the entry in the filestructure (if found) */
1037 if (ret_val != -1)
1038 yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
William Juulc051bbe2007-11-15 11:13:05 +01001039
Charles Manning3796e1f2012-05-09 16:55:17 +00001040 return ret_val;
1041}
William Juulc051bbe2007-11-15 11:13:05 +01001042
Charles Manning3796e1f2012-05-09 16:55:17 +00001043int 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 Juulc051bbe2007-11-15 11:13:05 +01001052
Charles Manning3796e1f2012-05-09 16:55:17 +00001053 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 Juulc051bbe2007-11-15 11:13:05 +01001059
Charles Manning3796e1f2012-05-09 16:55:17 +00001060 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 Juulc051bbe2007-11-15 11:13:05 +01001070 }
1071
Charles Manning3796e1f2012-05-09 16:55:17 +00001072 yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
1073 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01001074 }
1075
Charles Manning3796e1f2012-05-09 16:55:17 +00001076 tn = yaffs_add_find_tnode_0(dev,
1077 &in->variant.file_variant,
1078 inode_chunk, NULL);
1079 if (!tn)
1080 return YAFFS_FAIL;
William Juulc051bbe2007-11-15 11:13:05 +01001081
Charles Manning3796e1f2012-05-09 16:55:17 +00001082 if (!nand_chunk)
1083 /* Dummy insert, bail now */
1084 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01001085
Charles Manning3796e1f2012-05-09 16:55:17 +00001086 existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
William Juulc051bbe2007-11-15 11:13:05 +01001087
Charles Manning3796e1f2012-05-09 16:55:17 +00001088 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 Juulc051bbe2007-11-15 11:13:05 +01001099
Charles Manning3796e1f2012-05-09 16:55:17 +00001100 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 Juulc051bbe2007-11-15 11:13:05 +01001113 */
1114
Charles Manning3796e1f2012-05-09 16:55:17 +00001115 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 Juulc051bbe2007-11-15 11:13:05 +01001120
Charles Manning3796e1f2012-05-09 16:55:17 +00001121 /* Do a proper find */
1122 existing_cunk =
1123 yaffs_find_chunk_in_file(in, inode_chunk,
1124 &existing_tags);
William Juulc051bbe2007-11-15 11:13:05 +01001125 }
1126
Charles Manning3796e1f2012-05-09 16:55:17 +00001127 if (existing_cunk <= 0) {
1128 /*Hoosterman - how did this happen? */
William Juulc051bbe2007-11-15 11:13:05 +01001129
Charles Manning3796e1f2012-05-09 16:55:17 +00001130 yaffs_trace(YAFFS_TRACE_ERROR,
1131 "yaffs tragedy: existing chunk < 0 in scan"
1132 );
William Juulc051bbe2007-11-15 11:13:05 +01001133
Charles Manning3796e1f2012-05-09 16:55:17 +00001134 }
William Juulc051bbe2007-11-15 11:13:05 +01001135
Charles Manning3796e1f2012-05-09 16:55:17 +00001136 /* NB The deleted flags should be false, otherwise
1137 * the chunks will not be loaded during a scan
1138 */
William Juulc051bbe2007-11-15 11:13:05 +01001139
Charles Manning3796e1f2012-05-09 16:55:17 +00001140 if (in_scan > 0) {
1141 new_serial = new_tags.serial_number;
1142 existing_serial = existing_tags.serial_number;
William Juulc051bbe2007-11-15 11:13:05 +01001143 }
William Juulc051bbe2007-11-15 11:13:05 +01001144
Charles Manning3796e1f2012-05-09 16:55:17 +00001145 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 Juulc051bbe2007-11-15 11:13:05 +01001163 }
William Juulc051bbe2007-11-15 11:13:05 +01001164 }
1165
Charles Manning3796e1f2012-05-09 16:55:17 +00001166 }
William Juulc051bbe2007-11-15 11:13:05 +01001167
Charles Manning3796e1f2012-05-09 16:55:17 +00001168 if (existing_cunk == 0)
1169 in->n_data_chunks++;
William Juulc051bbe2007-11-15 11:13:05 +01001170
Charles Manning3796e1f2012-05-09 16:55:17 +00001171 yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
1172
1173 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01001174}
1175
Charles Manning3796e1f2012-05-09 16:55:17 +00001176static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
William Juulc051bbe2007-11-15 11:13:05 +01001177{
Charles Manning3796e1f2012-05-09 16:55:17 +00001178 struct yaffs_block_info *the_block;
1179 unsigned block_no;
William Juulc051bbe2007-11-15 11:13:05 +01001180
Charles Manning3796e1f2012-05-09 16:55:17 +00001181 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 Juulc051bbe2007-11-15 11:13:05 +01001189 }
1190}
1191
Charles Manning3796e1f2012-05-09 16:55:17 +00001192/* 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 Juulc051bbe2007-11-15 11:13:05 +01001198 */
Charles Manning3796e1f2012-05-09 16:55:17 +00001199
1200static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
1201 u32 level, int chunk_offset)
William Juulc051bbe2007-11-15 11:13:05 +01001202{
1203 int i;
Charles Manning3796e1f2012-05-09 16:55:17 +00001204 int the_chunk;
1205 int all_done = 1;
1206 struct yaffs_dev *dev = in->my_dev;
William Juulc051bbe2007-11-15 11:13:05 +01001207
Charles Manning3796e1f2012-05-09 16:55:17 +00001208 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 Juulc051bbe2007-11-15 11:13:05 +01001230 }
1231 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001232 return (all_done) ? 1 : 0;
William Juulc051bbe2007-11-15 11:13:05 +01001233 }
William Juulc051bbe2007-11-15 11:13:05 +01001234
Charles Manning3796e1f2012-05-09 16:55:17 +00001235 /* 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 Juulc051bbe2007-11-15 11:13:05 +01001244}
1245
Charles Manning3796e1f2012-05-09 16:55:17 +00001246static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01001247{
Charles Manning3796e1f2012-05-09 16:55:17 +00001248 struct yaffs_dev *dev = obj->my_dev;
1249 struct yaffs_obj *parent;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001250
Charles Manning3796e1f2012-05-09 16:55:17 +00001251 yaffs_verify_obj_in_dir(obj);
1252 parent = obj->parent;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001253
Charles Manning3796e1f2012-05-09 16:55:17 +00001254 yaffs_verify_dir(parent);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001255
Charles Manning3796e1f2012-05-09 16:55:17 +00001256 if (dev && dev->param.remove_obj_fn)
1257 dev->param.remove_obj_fn(obj);
William Juulc051bbe2007-11-15 11:13:05 +01001258
Charles Manning3796e1f2012-05-09 16:55:17 +00001259 list_del_init(&obj->siblings);
1260 obj->parent = NULL;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001261
Charles Manning3796e1f2012-05-09 16:55:17 +00001262 yaffs_verify_dir(parent);
William Juulc051bbe2007-11-15 11:13:05 +01001263}
1264
Charles Manning3796e1f2012-05-09 16:55:17 +00001265void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01001266{
Charles Manning3796e1f2012-05-09 16:55:17 +00001267 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 Juulc051bbe2007-11-15 11:13:05 +01001273 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001274 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 Juulc051bbe2007-11-15 11:13:05 +01001279 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001280
Charles Manning3796e1f2012-05-09 16:55:17 +00001281 if (obj->siblings.prev == NULL) {
1282 /* Not initialised */
1283 BUG();
1284 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001285
Charles Manning3796e1f2012-05-09 16:55:17 +00001286 yaffs_verify_dir(directory);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001287
Charles Manning3796e1f2012-05-09 16:55:17 +00001288 yaffs_remove_obj_from_dir(obj);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001289
Charles Manning3796e1f2012-05-09 16:55:17 +00001290 /* Now add it */
1291 list_add(&obj->siblings, &directory->variant.dir_variant.children);
1292 obj->parent = directory;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001293
Charles Manning3796e1f2012-05-09 16:55:17 +00001294 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 Denk74e0dde2008-08-14 14:41:06 +02001300
Charles Manning3796e1f2012-05-09 16:55:17 +00001301 yaffs_verify_dir(directory);
1302 yaffs_verify_obj_in_dir(obj);
William Juulc051bbe2007-11-15 11:13:05 +01001303}
1304
Charles Manning3796e1f2012-05-09 16:55:17 +00001305static 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 Juulc051bbe2007-11-15 11:13:05 +01001308{
Charles Manning3796e1f2012-05-09 16:55:17 +00001309 int unlink_op;
1310 int del_op;
1311 struct yaffs_obj *existing_target;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001312
Charles Manning3796e1f2012-05-09 16:55:17 +00001313 if (new_dir == NULL)
1314 new_dir = obj->parent; /* use the old directory */
William Juulc051bbe2007-11-15 11:13:05 +01001315
Charles Manning3796e1f2012-05-09 16:55:17 +00001316 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 Juulc051bbe2007-11-15 11:13:05 +01001322
Charles Manning3796e1f2012-05-09 16:55:17 +00001323 unlink_op = (new_dir == obj->my_dev->unlinked_dir);
1324 del_op = (new_dir == obj->my_dev->del_dir);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001325
Charles Manning3796e1f2012-05-09 16:55:17 +00001326 existing_target = yaffs_find_by_name(new_dir, new_name);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001327
Charles Manning3796e1f2012-05-09 16:55:17 +00001328 /* 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 Denk74e0dde2008-08-14 14:41:06 +02001337
Charles Manning3796e1f2012-05-09 16:55:17 +00001338 yaffs_set_obj_name(obj, new_name);
1339 obj->dirty = 1;
1340 yaffs_add_obj_to_dir(new_dir, obj);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001341
Charles Manning3796e1f2012-05-09 16:55:17 +00001342 if (unlink_op)
1343 obj->unlinked = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001344
Charles Manning3796e1f2012-05-09 16:55:17 +00001345 /* 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 Denk74e0dde2008-08-14 14:41:06 +02001348
Charles Manning3796e1f2012-05-09 16:55:17 +00001349 return YAFFS_FAIL;
William Juulc051bbe2007-11-15 11:13:05 +01001350}
1351
Charles Manning3796e1f2012-05-09 16:55:17 +00001352/*------------------------ 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
1365static 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 Juulc051bbe2007-11-15 11:13:05 +01001377
Charles Manning3796e1f2012-05-09 16:55:17 +00001378 return 0;
1379}
William Juulc051bbe2007-11-15 11:13:05 +01001380
Charles Manning3796e1f2012-05-09 16:55:17 +00001381static void yaffs_flush_file_cache(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01001382{
Charles Manning3796e1f2012-05-09 16:55:17 +00001383 struct yaffs_dev *dev = obj->my_dev;
1384 int lowest = -99; /* Stop compiler whining. */
William Juulc051bbe2007-11-15 11:13:05 +01001385 int i;
Charles Manning3796e1f2012-05-09 16:55:17 +00001386 struct yaffs_cache *cache;
1387 int chunk_written = 0;
1388 int n_caches = obj->my_dev->param.n_caches;
William Juulc051bbe2007-11-15 11:13:05 +01001389
Charles Manning3796e1f2012-05-09 16:55:17 +00001390 if (n_caches < 1)
1391 return;
1392 do {
1393 cache = NULL;
William Juulc051bbe2007-11-15 11:13:05 +01001394
Charles Manning3796e1f2012-05-09 16:55:17 +00001395 /* 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 Juulc051bbe2007-11-15 11:13:05 +01001403 }
1404 }
William Juulc051bbe2007-11-15 11:13:05 +01001405 }
William Juulc051bbe2007-11-15 11:13:05 +01001406
Charles Manning3796e1f2012-05-09 16:55:17 +00001407 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 Juulc051bbe2007-11-15 11:13:05 +01001418
Charles Manning3796e1f2012-05-09 16:55:17 +00001419 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 Juulc051bbe2007-11-15 11:13:05 +01001423}
1424
Charles Manning3796e1f2012-05-09 16:55:17 +00001425/*yaffs_flush_whole_cache(dev)
1426 *
1427 *
1428 */
1429
1430void yaffs_flush_whole_cache(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01001431{
Charles Manning3796e1f2012-05-09 16:55:17 +00001432 struct yaffs_obj *obj;
1433 int n_caches = dev->param.n_caches;
1434 int i;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001435
Charles Manning3796e1f2012-05-09 16:55:17 +00001436 /* 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 Denk74e0dde2008-08-14 14:41:06 +02001448
William Juulc051bbe2007-11-15 11:13:05 +01001449}
1450
Charles Manning3796e1f2012-05-09 16:55:17 +00001451/* 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 */
1456static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01001457{
Charles Manning3796e1f2012-05-09 16:55:17 +00001458 int i;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001459
Charles Manning3796e1f2012-05-09 16:55:17 +00001460 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 Juulc051bbe2007-11-15 11:13:05 +01001464 }
William Juulc051bbe2007-11-15 11:13:05 +01001465 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001466 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01001467}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001468
Charles Manning3796e1f2012-05-09 16:55:17 +00001469static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01001470{
Charles Manning3796e1f2012-05-09 16:55:17 +00001471 struct yaffs_cache *cache;
1472 struct yaffs_obj *the_obj;
1473 int usage;
William Juulc051bbe2007-11-15 11:13:05 +01001474 int i;
William Juulc051bbe2007-11-15 11:13:05 +01001475
Charles Manning3796e1f2012-05-09 16:55:17 +00001476 if (dev->param.n_caches < 1)
1477 return NULL;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001478
Charles Manning3796e1f2012-05-09 16:55:17 +00001479 /* Try find a non-dirty one... */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001480
Charles Manning3796e1f2012-05-09 16:55:17 +00001481 cache = yaffs_grab_chunk_worker(dev);
William Juulc051bbe2007-11-15 11:13:05 +01001482
Charles Manning3796e1f2012-05-09 16:55:17 +00001483 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 Denk74e0dde2008-08-14 14:41:06 +02001489
Charles Manning3796e1f2012-05-09 16:55:17 +00001490 /* 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 Denk74e0dde2008-08-14 14:41:06 +02001495
Charles Manning3796e1f2012-05-09 16:55:17 +00001496 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 Juulc051bbe2007-11-15 11:13:05 +01001504 }
1505 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001506
Charles Manning3796e1f2012-05-09 16:55:17 +00001507 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 Juulc051bbe2007-11-15 11:13:05 +01001514}
1515
Charles Manning3796e1f2012-05-09 16:55:17 +00001516/* Find a cached chunk */
1517static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
1518 int chunk_id)
William Juulc051bbe2007-11-15 11:13:05 +01001519{
Charles Manning3796e1f2012-05-09 16:55:17 +00001520 struct yaffs_dev *dev = obj->my_dev;
1521 int i;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001522
Charles Manning3796e1f2012-05-09 16:55:17 +00001523 if (dev->param.n_caches < 1)
1524 return NULL;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001525
Charles Manning3796e1f2012-05-09 16:55:17 +00001526 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 Denk74e0dde2008-08-14 14:41:06 +02001530
Charles Manning3796e1f2012-05-09 16:55:17 +00001531 return &dev->cache[i];
William Juulc051bbe2007-11-15 11:13:05 +01001532 }
1533 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001534 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01001535}
1536
Charles Manning3796e1f2012-05-09 16:55:17 +00001537/* Mark the chunk for the least recently used algorithym */
1538static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
1539 int is_write)
William Juulc051bbe2007-11-15 11:13:05 +01001540{
Charles Manning3796e1f2012-05-09 16:55:17 +00001541 int i;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001542
Charles Manning3796e1f2012-05-09 16:55:17 +00001543 if (dev->param.n_caches < 1)
1544 return;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001545
Charles Manning3796e1f2012-05-09 16:55:17 +00001546 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 Denk74e0dde2008-08-14 14:41:06 +02001551
Charles Manning3796e1f2012-05-09 16:55:17 +00001552 dev->cache_last_use = 0;
1553 }
1554 dev->cache_last_use++;
1555 cache->last_use = dev->cache_last_use;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001556
Charles Manning3796e1f2012-05-09 16:55:17 +00001557 if (is_write)
1558 cache->dirty = 1;
William Juulc051bbe2007-11-15 11:13:05 +01001559}
1560
Charles Manning3796e1f2012-05-09 16:55:17 +00001561/* 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 */
1565static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
William Juulc051bbe2007-11-15 11:13:05 +01001566{
Charles Manning3796e1f2012-05-09 16:55:17 +00001567 struct yaffs_cache *cache;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001568
Charles Manning3796e1f2012-05-09 16:55:17 +00001569 if (object->my_dev->param.n_caches > 0) {
1570 cache = yaffs_find_chunk_cache(object, chunk_id);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001571
Charles Manning3796e1f2012-05-09 16:55:17 +00001572 if (cache)
1573 cache->object = NULL;
1574 }
1575}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001576
Charles Manning3796e1f2012-05-09 16:55:17 +00001577/* Invalidate all the cache pages associated with this object
1578 * Do this whenever ther file is deleted or resized.
1579 */
1580static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
1581{
1582 int i;
1583 struct yaffs_dev *dev = in->my_dev;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001584
Charles Manning3796e1f2012-05-09 16:55:17 +00001585 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 Juulc051bbe2007-11-15 11:13:05 +01001592}
1593
Charles Manning3796e1f2012-05-09 16:55:17 +00001594static void yaffs_unhash_obj(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01001595{
Charles Manning3796e1f2012-05-09 16:55:17 +00001596 int bucket;
1597 struct yaffs_dev *dev = obj->my_dev;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001598
Charles Manning3796e1f2012-05-09 16:55:17 +00001599 /* 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 Juulc051bbe2007-11-15 11:13:05 +01001604 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001605}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001606
Charles Manning3796e1f2012-05-09 16:55:17 +00001607/* FreeObject frees up a Object and puts it back on the free list */
1608static void yaffs_free_obj(struct yaffs_obj *obj)
1609{
1610 struct yaffs_dev *dev;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001611
Charles Manning3796e1f2012-05-09 16:55:17 +00001612 if (!obj) {
1613 BUG();
1614 return;
William Juulc051bbe2007-11-15 11:13:05 +01001615 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001616 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 Denk74e0dde2008-08-14 14:41:06 +02001623
Charles Manning3796e1f2012-05-09 16:55:17 +00001624 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 Juulc051bbe2007-11-15 11:13:05 +01001630 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001631
Charles Manning3796e1f2012-05-09 16:55:17 +00001632 yaffs_unhash_obj(obj);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001633
Charles Manning3796e1f2012-05-09 16:55:17 +00001634 yaffs_free_raw_obj(dev, obj);
1635 dev->n_obj--;
1636 dev->checkpoint_blocks_required = 0; /* force recalculation */
William Juulc051bbe2007-11-15 11:13:05 +01001637}
1638
Charles Manning3796e1f2012-05-09 16:55:17 +00001639void yaffs_handle_defered_free(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01001640{
Charles Manning3796e1f2012-05-09 16:55:17 +00001641 if (obj->defered_free)
1642 yaffs_free_obj(obj);
1643}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001644
Charles Manning3796e1f2012-05-09 16:55:17 +00001645static 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 Denk74e0dde2008-08-14 14:41:06 +02001649
Charles Manning3796e1f2012-05-09 16:55:17 +00001650 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 Juulc051bbe2007-11-15 11:13:05 +01001654 }
1655
Charles Manning3796e1f2012-05-09 16:55:17 +00001656 yaffs_remove_obj_from_dir(in);
1657 yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
1658 in->hdr_chunk = 0;
William Juulc051bbe2007-11-15 11:13:05 +01001659
Charles Manning3796e1f2012-05-09 16:55:17 +00001660 yaffs_free_obj(in);
1661 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01001662
1663}
1664
Charles Manning3796e1f2012-05-09 16:55:17 +00001665static void yaffs_soft_del_file(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01001666{
Charles Manning3796e1f2012-05-09 16:55:17 +00001667 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 Juulc051bbe2007-11-15 11:13:05 +01001687 }
1688}
1689
Charles Manning3796e1f2012-05-09 16:55:17 +00001690/* 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 Juulc051bbe2007-11-15 11:13:05 +01001704
Charles Manning3796e1f2012-05-09 16:55:17 +00001705static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
1706 struct yaffs_tnode *tn, u32 level,
1707 int del0)
William Juulc051bbe2007-11-15 11:13:05 +01001708{
Charles Manning3796e1f2012-05-09 16:55:17 +00001709 int i;
1710 int has_data;
William Juulc051bbe2007-11-15 11:13:05 +01001711
Charles Manning3796e1f2012-05-09 16:55:17 +00001712 if (!tn)
1713 return tn;
William Juulc051bbe2007-11-15 11:13:05 +01001714
Charles Manning3796e1f2012-05-09 16:55:17 +00001715 has_data = 0;
William Juulc051bbe2007-11-15 11:13:05 +01001716
Charles Manning3796e1f2012-05-09 16:55:17 +00001717 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 Denk74e0dde2008-08-14 14:41:06 +02001726
Charles Manning3796e1f2012-05-09 16:55:17 +00001727 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 Juulc051bbe2007-11-15 11:13:05 +01001739
Charles Manning3796e1f2012-05-09 16:55:17 +00001740 if (has_data == 0 && del0) {
1741 /* Free and return NULL */
1742 yaffs_free_tnode(dev, tn);
1743 tn = NULL;
1744 }
1745 return tn;
William Juulc051bbe2007-11-15 11:13:05 +01001746}
1747
Charles Manning3796e1f2012-05-09 16:55:17 +00001748static int yaffs_prune_tree(struct yaffs_dev *dev,
1749 struct yaffs_file_var *file_struct)
William Juulc051bbe2007-11-15 11:13:05 +01001750{
Charles Manning3796e1f2012-05-09 16:55:17 +00001751 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 Denk74e0dde2008-08-14 14:41:06 +02001758
Charles Manning3796e1f2012-05-09 16:55:17 +00001759 file_struct->top =
1760 yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
William Juulc051bbe2007-11-15 11:13:05 +01001761
Charles Manning3796e1f2012-05-09 16:55:17 +00001762 /* 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 Juulc051bbe2007-11-15 11:13:05 +01001768
Charles Manning3796e1f2012-05-09 16:55:17 +00001769 while (file_struct->top_level && !done) {
1770 tn = file_struct->top;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02001771
Charles Manning3796e1f2012-05-09 16:55:17 +00001772 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 Juulc051bbe2007-11-15 11:13:05 +01001788}
1789
Charles Manning3796e1f2012-05-09 16:55:17 +00001790/*-------------------- End of File Structure functions.-------------------*/
William Juulc051bbe2007-11-15 11:13:05 +01001791
Charles Manning3796e1f2012-05-09 16:55:17 +00001792/* alloc_empty_obj gets us a clean Object.*/
1793static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01001794{
Charles Manning3796e1f2012-05-09 16:55:17 +00001795 struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
William Juulc051bbe2007-11-15 11:13:05 +01001796
Charles Manning3796e1f2012-05-09 16:55:17 +00001797 if (!obj)
1798 return obj;
William Juulc051bbe2007-11-15 11:13:05 +01001799
Charles Manning3796e1f2012-05-09 16:55:17 +00001800 dev->n_obj++;
William Juulc051bbe2007-11-15 11:13:05 +01001801
Charles Manning3796e1f2012-05-09 16:55:17 +00001802 /* Now sweeten it up... */
William Juulc051bbe2007-11-15 11:13:05 +01001803
Charles Manning3796e1f2012-05-09 16:55:17 +00001804 memset(obj, 0, sizeof(struct yaffs_obj));
1805 obj->being_created = 1;
William Juulc051bbe2007-11-15 11:13:05 +01001806
Charles Manning3796e1f2012-05-09 16:55:17 +00001807 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 Juulc051bbe2007-11-15 11:13:05 +01001813
Charles Manning3796e1f2012-05-09 16:55:17 +00001814 /* 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 Juulc051bbe2007-11-15 11:13:05 +01001820
Charles Manning3796e1f2012-05-09 16:55:17 +00001821 /* 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 Juulc051bbe2007-11-15 11:13:05 +01001827
Charles Manning3796e1f2012-05-09 16:55:17 +00001828 obj->being_created = 0;
William Juulc051bbe2007-11-15 11:13:05 +01001829
Charles Manning3796e1f2012-05-09 16:55:17 +00001830 dev->checkpoint_blocks_required = 0; /* force recalculation */
William Juulc051bbe2007-11-15 11:13:05 +01001831
Charles Manning3796e1f2012-05-09 16:55:17 +00001832 return obj;
1833}
William Juulc051bbe2007-11-15 11:13:05 +01001834
Charles Manning3796e1f2012-05-09 16:55:17 +00001835static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
1836{
1837 int i;
1838 int l = 999;
1839 int lowest = 999999;
William Juulc051bbe2007-11-15 11:13:05 +01001840
Charles Manning3796e1f2012-05-09 16:55:17 +00001841 /* Search for the shortest list or one that
1842 * isn't too long.
1843 */
William Juulc051bbe2007-11-15 11:13:05 +01001844
Charles Manning3796e1f2012-05-09 16:55:17 +00001845 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 Juulc051bbe2007-11-15 11:13:05 +01001853
Charles Manning3796e1f2012-05-09 16:55:17 +00001854 return l;
1855}
William Juulc051bbe2007-11-15 11:13:05 +01001856
Charles Manning3796e1f2012-05-09 16:55:17 +00001857static 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 Schuchardt8acf9e82017-11-09 01:26:43 +01001871 list_for_each(i, &dev->obj_bucket[bucket].list) {
1872 /* If there is already one in the list */
Heinrich Schuchardtdcb66342018-03-08 22:52:29 +01001873 if (list_entry(i, struct yaffs_obj,
1874 hash_link)->obj_id == n) {
Heinrich Schuchardt8acf9e82017-11-09 01:26:43 +01001875 found = 0;
1876 break;
William Juulc051bbe2007-11-15 11:13:05 +01001877 }
Charles Manning3796e1f2012-05-09 16:55:17 +00001878 }
1879 }
1880 return n;
1881}
William Juulc051bbe2007-11-15 11:13:05 +01001882
Charles Manning3796e1f2012-05-09 16:55:17 +00001883static 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 Juulc051bbe2007-11-15 11:13:05 +01001887
Charles Manning3796e1f2012-05-09 16:55:17 +00001888 list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
1889 dev->obj_bucket[bucket].count++;
1890}
William Juulc051bbe2007-11-15 11:13:05 +01001891
Charles Manning3796e1f2012-05-09 16:55:17 +00001892struct 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 Juulc051bbe2007-11-15 11:13:05 +01001911
Charles Manning3796e1f2012-05-09 16:55:17 +00001912struct 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 Juulc051bbe2007-11-15 11:13:05 +01001917
Charles Manning3796e1f2012-05-09 16:55:17 +00001918 if (number < 0)
1919 number = yaffs_new_obj_id(dev);
William Juulc051bbe2007-11-15 11:13:05 +01001920
Charles Manning3796e1f2012-05-09 16:55:17 +00001921 if (type == YAFFS_OBJECT_TYPE_FILE) {
1922 tn = yaffs_get_tnode(dev);
1923 if (!tn)
1924 return NULL;
1925 }
William Juulc051bbe2007-11-15 11:13:05 +01001926
Charles Manning3796e1f2012-05-09 16:55:17 +00001927 the_obj = yaffs_alloc_empty_obj(dev);
1928 if (!the_obj) {
1929 if (tn)
1930 yaffs_free_tnode(dev, tn);
1931 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01001932 }
1933
Charles Manning3796e1f2012-05-09 16:55:17 +00001934 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 Juulc051bbe2007-11-15 11:13:05 +01001965}
1966
Charles Manning3796e1f2012-05-09 16:55:17 +00001967static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
1968 int number, u32 mode)
William Juulc051bbe2007-11-15 11:13:05 +01001969{
1970
Charles Manning3796e1f2012-05-09 16:55:17 +00001971 struct yaffs_obj *obj =
1972 yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
William Juulc051bbe2007-11-15 11:13:05 +01001973
Charles Manning3796e1f2012-05-09 16:55:17 +00001974 if (!obj)
1975 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01001976
Charles Manning3796e1f2012-05-09 16:55:17 +00001977 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 Juulc051bbe2007-11-15 11:13:05 +01001986
Charles Manning3796e1f2012-05-09 16:55:17 +00001987}
William Juulc051bbe2007-11-15 11:13:05 +01001988
Charles Manning3796e1f2012-05-09 16:55:17 +00001989static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
1990{
1991 int i;
William Juulc051bbe2007-11-15 11:13:05 +01001992
Charles Manning3796e1f2012-05-09 16:55:17 +00001993 dev->n_obj = 0;
1994 dev->n_tnodes = 0;
1995 yaffs_init_raw_tnodes_and_objs(dev);
William Juulc051bbe2007-11-15 11:13:05 +01001996
Charles Manning3796e1f2012-05-09 16:55:17 +00001997 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 Juulc051bbe2007-11-15 11:13:05 +01002002
Charles Manning3796e1f2012-05-09 16:55:17 +00002003struct 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 Juulc051bbe2007-11-15 11:13:05 +01002008
Charles Manning3796e1f2012-05-09 16:55:17 +00002009 if (number > 0)
2010 the_obj = yaffs_find_by_number(dev, number);
William Juulc051bbe2007-11-15 11:13:05 +01002011
Charles Manning3796e1f2012-05-09 16:55:17 +00002012 if (!the_obj)
2013 the_obj = yaffs_new_obj(dev, number, type);
William Juulc051bbe2007-11-15 11:13:05 +01002014
Charles Manning3796e1f2012-05-09 16:55:17 +00002015 return the_obj;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02002016
Charles Manning3796e1f2012-05-09 16:55:17 +00002017}
William Juulc051bbe2007-11-15 11:13:05 +01002018
Charles Manning3796e1f2012-05-09 16:55:17 +00002019YCHAR *yaffs_clone_str(const YCHAR *str)
2020{
2021 YCHAR *new_str = NULL;
2022 int len;
William Juulc051bbe2007-11-15 11:13:05 +01002023
Charles Manning3796e1f2012-05-09 16:55:17 +00002024 if (!str)
2025 str = _Y("");
William Juulc051bbe2007-11-15 11:13:05 +01002026
Charles Manning3796e1f2012-05-09 16:55:17 +00002027 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 Juulc051bbe2007-11-15 11:13:05 +01002034
Charles Manning3796e1f2012-05-09 16:55:17 +00002035}
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 Juulc051bbe2007-11-15 11:13:05 +01002051
Charles Manning3796e1f2012-05-09 16:55:17 +00002052static void yaffs_update_parent(struct yaffs_obj *obj)
2053{
2054 struct yaffs_dev *dev;
William Juulc051bbe2007-11-15 11:13:05 +01002055
Charles Manning3796e1f2012-05-09 16:55:17 +00002056 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 Juulc051bbe2007-11-15 11:13:05 +01002063
Charles Manning3796e1f2012-05-09 16:55:17 +00002064 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 Juulc051bbe2007-11-15 11:13:05 +01002070
Charles Manning3796e1f2012-05-09 16:55:17 +00002071 } else {
2072 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2073 }
2074}
William Juulc051bbe2007-11-15 11:13:05 +01002075
Charles Manning3796e1f2012-05-09 16:55:17 +00002076void 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 Juulc051bbe2007-11-15 11:13:05 +01002082
Charles Manning3796e1f2012-05-09 16:55:17 +00002083 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
William Juulc051bbe2007-11-15 11:13:05 +01002084
Charles Manning3796e1f2012-05-09 16:55:17 +00002085 while (!list_empty(&dev->dirty_dirs)) {
2086 link = dev->dirty_dirs.next;
2087 list_del_init(link);
William Juulc051bbe2007-11-15 11:13:05 +01002088
Charles Manning3796e1f2012-05-09 16:55:17 +00002089 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 Juulc051bbe2007-11-15 11:13:05 +01002092
Charles Manning3796e1f2012-05-09 16:55:17 +00002093 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
2094 obj->obj_id);
William Juulc051bbe2007-11-15 11:13:05 +01002095
Charles Manning3796e1f2012-05-09 16:55:17 +00002096 if (obj->dirty)
2097 yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
2098 }
2099}
William Juulc051bbe2007-11-15 11:13:05 +01002100
Charles Manning3796e1f2012-05-09 16:55:17 +00002101/*
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 Juulc051bbe2007-11-15 11:13:05 +01002107
Charles Manning3796e1f2012-05-09 16:55:17 +00002108static 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 Juulc051bbe2007-11-15 11:13:05 +01002120
Charles Manning3796e1f2012-05-09 16:55:17 +00002121 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002125
Charles Manning3796e1f2012-05-09 16:55:17 +00002126 if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
2127 str = yaffs_clone_str(alias_str);
2128 if (!str)
2129 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01002130 }
2131
Charles Manning3796e1f2012-05-09 16:55:17 +00002132 in = yaffs_new_obj(dev, -1, type);
William Juulc051bbe2007-11-15 11:13:05 +01002133
Charles Manning3796e1f2012-05-09 16:55:17 +00002134 if (!in) {
2135 kfree(str);
2136 return NULL;
2137 }
William Juulc051bbe2007-11-15 11:13:05 +01002138
Charles Manning3796e1f2012-05-09 16:55:17 +00002139 in->hdr_chunk = 0;
2140 in->valid = 1;
2141 in->variant_type = type;
William Juulc051bbe2007-11-15 11:13:05 +01002142
Charles Manning3796e1f2012-05-09 16:55:17 +00002143 in->yst_mode = mode;
William Juulc051bbe2007-11-15 11:13:05 +01002144
Charles Manning3796e1f2012-05-09 16:55:17 +00002145 yaffs_attribs_init(in, gid, uid, rdev);
William Juulc051bbe2007-11-15 11:13:05 +01002146
Charles Manning3796e1f2012-05-09 16:55:17 +00002147 in->n_data_chunks = 0;
William Juulc051bbe2007-11-15 11:13:05 +01002148
Charles Manning3796e1f2012-05-09 16:55:17 +00002149 yaffs_set_obj_name(in, name);
2150 in->dirty = 1;
William Juulc051bbe2007-11-15 11:13:05 +01002151
Charles Manning3796e1f2012-05-09 16:55:17 +00002152 yaffs_add_obj_to_dir(parent, in);
William Juulc051bbe2007-11-15 11:13:05 +01002153
Charles Manning3796e1f2012-05-09 16:55:17 +00002154 in->my_dev = parent->my_dev;
William Juulc051bbe2007-11-15 11:13:05 +01002155
Charles Manning3796e1f2012-05-09 16:55:17 +00002156 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 Juulc051bbe2007-11-15 11:13:05 +01002177 }
2178
Charles Manning3796e1f2012-05-09 16:55:17 +00002179 if (in)
2180 yaffs_update_parent(parent);
2181
2182 return in;
William Juulc051bbe2007-11-15 11:13:05 +01002183}
2184
Charles Manning3796e1f2012-05-09 16:55:17 +00002185struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
2186 const YCHAR *name, u32 mode, u32 uid,
2187 u32 gid)
William Juulc051bbe2007-11-15 11:13:05 +01002188{
Charles Manning3796e1f2012-05-09 16:55:17 +00002189 return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
2190 uid, gid, NULL, NULL, 0);
2191}
William Juulc051bbe2007-11-15 11:13:05 +01002192
Charles Manning3796e1f2012-05-09 16:55:17 +00002193struct 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 Denk74e0dde2008-08-14 14:41:06 +02002199
Charles Manning3796e1f2012-05-09 16:55:17 +00002200struct 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 Juulc051bbe2007-11-15 11:13:05 +01002207
Charles Manning3796e1f2012-05-09 16:55:17 +00002208struct 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 Juulc051bbe2007-11-15 11:13:05 +01002215
Charles Manning3796e1f2012-05-09 16:55:17 +00002216/* yaffs_link_obj returns the object id of the equivalent object.*/
2217struct 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 Juulc051bbe2007-11-15 11:13:05 +01002222
Charles Manning3796e1f2012-05-09 16:55:17 +00002223 if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
2224 parent, name, 0, 0, 0,
2225 equiv_obj, NULL, 0))
2226 return equiv_obj;
William Juulc051bbe2007-11-15 11:13:05 +01002227
Charles Manning3796e1f2012-05-09 16:55:17 +00002228 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01002229
Charles Manning3796e1f2012-05-09 16:55:17 +00002230}
William Juulc051bbe2007-11-15 11:13:05 +01002231
Charles Manning3796e1f2012-05-09 16:55:17 +00002232/*---------------------- Block Management and Page Allocation -------------*/
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02002233
Charles Manning3796e1f2012-05-09 16:55:17 +00002234static 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 Juulc051bbe2007-11-15 11:13:05 +01002240
Charles Manning3796e1f2012-05-09 16:55:17 +00002241 dev->block_info_alt = 0;
William Juulc051bbe2007-11-15 11:13:05 +01002242
Charles Manning3796e1f2012-05-09 16:55:17 +00002243 dev->block_info = NULL;
William Juulc051bbe2007-11-15 11:13:05 +01002244
Charles Manning3796e1f2012-05-09 16:55:17 +00002245 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 Juulc051bbe2007-11-15 11:13:05 +01002252
Charles Manning3796e1f2012-05-09 16:55:17 +00002253static int yaffs_init_blocks(struct yaffs_dev *dev)
2254{
2255 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
William Juulc051bbe2007-11-15 11:13:05 +01002256
Charles Manning3796e1f2012-05-09 16:55:17 +00002257 dev->block_info = NULL;
2258 dev->chunk_bits = NULL;
2259 dev->alloc_block = -1; /* force it to get a new one */
William Juulc051bbe2007-11-15 11:13:05 +01002260
Charles Manning3796e1f2012-05-09 16:55:17 +00002261 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002268 } else {
Charles Manning3796e1f2012-05-09 16:55:17 +00002269 dev->block_info_alt = 0;
William Juulc051bbe2007-11-15 11:13:05 +01002270 }
2271
Charles Manning3796e1f2012-05-09 16:55:17 +00002272 if (!dev->block_info)
2273 goto alloc_error;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02002274
Charles Manning3796e1f2012-05-09 16:55:17 +00002275 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002285 }
Charles Manning3796e1f2012-05-09 16:55:17 +00002286 if (!dev->chunk_bits)
2287 goto alloc_error;
2288
Charles Manning3796e1f2012-05-09 16:55:17 +00002289 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 Juulc051bbe2007-11-15 11:13:05 +01002291 return YAFFS_OK;
Charles Manning3796e1f2012-05-09 16:55:17 +00002292
2293alloc_error:
2294 yaffs_deinit_blocks(dev);
2295 return YAFFS_FAIL;
William Juulc051bbe2007-11-15 11:13:05 +01002296}
2297
Charles Manning3796e1f2012-05-09 16:55:17 +00002298void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
William Juulc051bbe2007-11-15 11:13:05 +01002299{
Charles Manning3796e1f2012-05-09 16:55:17 +00002300 struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
2301 int erased_ok = 0;
2302 int i;
William Juulc051bbe2007-11-15 11:13:05 +01002303
Charles Manning3796e1f2012-05-09 16:55:17 +00002304 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002307
Charles Manning3796e1f2012-05-09 16:55:17 +00002308 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 Juulc051bbe2007-11-15 11:13:05 +01002312
Charles Manning3796e1f2012-05-09 16:55:17 +00002313 yaffs2_clear_oldest_dirty_seq(dev, bi);
William Juulc051bbe2007-11-15 11:13:05 +01002314
Charles Manning3796e1f2012-05-09 16:55:17 +00002315 bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
William Juulc051bbe2007-11-15 11:13:05 +01002316
Charles Manning3796e1f2012-05-09 16:55:17 +00002317 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002320
Charles Manning3796e1f2012-05-09 16:55:17 +00002321 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002335 }
Charles Manning3796e1f2012-05-09 16:55:17 +00002336 }
William Juulc051bbe2007-11-15 11:13:05 +01002337
Charles Manning3796e1f2012-05-09 16:55:17 +00002338 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002350 }
2351
Charles Manning3796e1f2012-05-09 16:55:17 +00002352 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 Juulc051bbe2007-11-15 11:13:05 +01002373
Charles Manning3796e1f2012-05-09 16:55:17 +00002374 yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
William Juulc051bbe2007-11-15 11:13:05 +01002375}
2376
Charles Manning3796e1f2012-05-09 16:55:17 +00002377static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
2378 struct yaffs_block_info *bi,
2379 int old_chunk, u8 *buffer)
William Juulc051bbe2007-11-15 11:13:05 +01002380{
Charles Manning3796e1f2012-05-09 16:55:17 +00002381 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 Juulc051bbe2007-11-15 11:13:05 +01002387
Charles Manning3796e1f2012-05-09 16:55:17 +00002388 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 Juulc051bbe2007-11-15 11:13:05 +01002392
Charles Manning3796e1f2012-05-09 16:55:17 +00002393 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 Juulc051bbe2007-11-15 11:13:05 +01002397
Charles Manning3796e1f2012-05-09 16:55:17 +00002398 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 Juulc051bbe2007-11-15 11:13:05 +01002410
Charles Manning3796e1f2012-05-09 16:55:17 +00002411 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 Juulc051bbe2007-11-15 11:13:05 +01002419
Charles Manning3796e1f2012-05-09 16:55:17 +00002420 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 Juulc051bbe2007-11-15 11:13:05 +01002427
Charles Manning3796e1f2012-05-09 16:55:17 +00002428 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 Juulc051bbe2007-11-15 11:13:05 +01002437
Charles Manning3796e1f2012-05-09 16:55:17 +00002438 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002447
Charles Manning3796e1f2012-05-09 16:55:17 +00002448 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 Juulc051bbe2007-11-15 11:13:05 +01002465
Charles Manning3796e1f2012-05-09 16:55:17 +00002466 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 Juulc051bbe2007-11-15 11:13:05 +01002476
Charles Manning3796e1f2012-05-09 16:55:17 +00002477 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 Juulc051bbe2007-11-15 11:13:05 +01002482
Charles Manning3796e1f2012-05-09 16:55:17 +00002483 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002489 }
Charles Manning3796e1f2012-05-09 16:55:17 +00002490
2491 yaffs_verify_oh(object, oh, &tags, 1);
2492 new_chunk =
2493 yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
William Juulc051bbe2007-11-15 11:13:05 +01002494 } else {
Charles Manning3796e1f2012-05-09 16:55:17 +00002495 new_chunk =
2496 yaffs_write_new_chunk(dev, buffer, &tags, 1);
William Juulc051bbe2007-11-15 11:13:05 +01002497 }
2498
Charles Manning3796e1f2012-05-09 16:55:17 +00002499 if (new_chunk < 0) {
2500 ret_val = YAFFS_FAIL;
2501 } else {
William Juulc051bbe2007-11-15 11:13:05 +01002502
Charles Manning3796e1f2012-05-09 16:55:17 +00002503 /* Now fix up the Tnodes etc. */
William Juulc051bbe2007-11-15 11:13:05 +01002504
Charles Manning3796e1f2012-05-09 16:55:17 +00002505 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 Juulc051bbe2007-11-15 11:13:05 +01002514 }
William Juulc051bbe2007-11-15 11:13:05 +01002515 }
Charles Manning3796e1f2012-05-09 16:55:17 +00002516 if (ret_val == YAFFS_OK)
2517 yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
2518 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +01002519}
2520
Charles Manning3796e1f2012-05-09 16:55:17 +00002521static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
William Juulc051bbe2007-11-15 11:13:05 +01002522{
Charles Manning3796e1f2012-05-09 16:55:17 +00002523 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 Juulc051bbe2007-11-15 11:13:05 +01002531
Charles Manning3796e1f2012-05-09 16:55:17 +00002532 is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
William Juulc051bbe2007-11-15 11:13:05 +01002533
Charles Manning3796e1f2012-05-09 16:55:17 +00002534 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 Juulc051bbe2007-11-15 11:13:05 +01002538
Charles Manning3796e1f2012-05-09 16:55:17 +00002539 /*yaffs_verify_free_chunks(dev); */
William Juulc051bbe2007-11-15 11:13:05 +01002540
Charles Manning3796e1f2012-05-09 16:55:17 +00002541 if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
2542 bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
William Juulc051bbe2007-11-15 11:13:05 +01002543
Charles Manning3796e1f2012-05-09 16:55:17 +00002544 bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
William Juulc051bbe2007-11-15 11:13:05 +01002545
Charles Manning3796e1f2012-05-09 16:55:17 +00002546 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 Juulc051bbe2007-11-15 11:13:05 +01002615 }
Charles Manning3796e1f2012-05-09 16:55:17 +00002616
2617 dev->gc_disable = 0;
2618
2619 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +01002620}
2621
Charles Manning3796e1f2012-05-09 16:55:17 +00002622/*
2623 * find_gc_block() selects the dirtiest block (or close enough)
2624 * for garbage collection.
2625 */
2626
2627static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
2628 int aggressive, int background)
William Juulc051bbe2007-11-15 11:13:05 +01002629{
Charles Manning3796e1f2012-05-09 16:55:17 +00002630 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 Juulc051bbe2007-11-15 11:13:05 +01002637
Charles Manning3796e1f2012-05-09 16:55:17 +00002638 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002660 */
2661
Charles Manning3796e1f2012-05-09 16:55:17 +00002662 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 Juulc051bbe2007-11-15 11:13:05 +01002675
Charles Manning3796e1f2012-05-09 16:55:17 +00002676 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 Juulc051bbe2007-11-15 11:13:05 +01002685
Charles Manning3796e1f2012-05-09 16:55:17 +00002686 if (background)
2687 max_threshold = dev->param.chunks_per_block / 2;
2688 else
2689 max_threshold = dev->param.chunks_per_block / 8;
William Juulc051bbe2007-11-15 11:13:05 +01002690
Charles Manning3796e1f2012-05-09 16:55:17 +00002691 if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
2692 max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
William Juulc051bbe2007-11-15 11:13:05 +01002693
Charles Manning3796e1f2012-05-09 16:55:17 +00002694 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 Juulc051bbe2007-11-15 11:13:05 +01002699
Charles Manning3796e1f2012-05-09 16:55:17 +00002700 iterations = n_blocks / 16 + 1;
2701 if (iterations > 100)
2702 iterations = 100;
William Juulc051bbe2007-11-15 11:13:05 +01002703 }
William Juulc051bbe2007-11-15 11:13:05 +01002704
Charles Manning3796e1f2012-05-09 16:55:17 +00002705 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 Juulc051bbe2007-11-15 11:13:05 +01002728 }
William Juulc051bbe2007-11-15 11:13:05 +01002729
Charles Manning3796e1f2012-05-09 16:55:17 +00002730 if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
2731 selected = dev->gc_dirtiest;
2732 }
William Juulc051bbe2007-11-15 11:13:05 +01002733
Charles Manning3796e1f2012-05-09 16:55:17 +00002734 /*
2735 * If nothing has been selected for a while, try the oldest dirty
2736 * because that's gumming up the works.
2737 */
William Juulc051bbe2007-11-15 11:13:05 +01002738
Charles Manning3796e1f2012-05-09 16:55:17 +00002739 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 Juulc051bbe2007-11-15 11:13:05 +01002752 }
2753
Charles Manning3796e1f2012-05-09 16:55:17 +00002754 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 Juulc051bbe2007-11-15 11:13:05 +01002760
Charles Manning3796e1f2012-05-09 16:55:17 +00002761 dev->n_gc_blocks++;
2762 if (background)
2763 dev->bg_gcs++;
William Juulc051bbe2007-11-15 11:13:05 +01002764
Charles Manning3796e1f2012-05-09 16:55:17 +00002765 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 Juulc051bbe2007-11-15 11:13:05 +01002778
Charles Manning3796e1f2012-05-09 16:55:17 +00002779 return selected;
2780}
William Juulc051bbe2007-11-15 11:13:05 +01002781
Charles Manning3796e1f2012-05-09 16:55:17 +00002782/* 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 */
2791static int yaffs_check_gc(struct yaffs_dev *dev, int background)
William Juulc051bbe2007-11-15 11:13:05 +01002792{
Charles Manning3796e1f2012-05-09 16:55:17 +00002793 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 Juulc051bbe2007-11-15 11:13:05 +01002799
Charles Manning3796e1f2012-05-09 16:55:17 +00002800 if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0)
2801 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01002802
Charles Manning3796e1f2012-05-09 16:55:17 +00002803 if (dev->gc_disable)
2804 /* Bail out so we don't get recursive gc */
2805 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01002806
Charles Manning3796e1f2012-05-09 16:55:17 +00002807 /* This loop should pass the first time.
2808 * Only loops here if the collection does not increase space.
William Juulc051bbe2007-11-15 11:13:05 +01002809 */
William Juulc051bbe2007-11-15 11:13:05 +01002810
Charles Manning3796e1f2012-05-09 16:55:17 +00002811 do {
2812 max_tries++;
William Juulc051bbe2007-11-15 11:13:05 +01002813
Charles Manning3796e1f2012-05-09 16:55:17 +00002814 checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
William Juulc051bbe2007-11-15 11:13:05 +01002815
Charles Manning3796e1f2012-05-09 16:55:17 +00002816 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 Juulc051bbe2007-11-15 11:13:05 +01002820
Charles Manning3796e1f2012-05-09 16:55:17 +00002821 /* 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 Denk74e0dde2008-08-14 14:41:06 +02002828
Charles Manning3796e1f2012-05-09 16:55:17 +00002829 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 Juulc051bbe2007-11-15 11:13:05 +01002839
Charles Manning3796e1f2012-05-09 16:55:17 +00002840 dev->gc_skip = 5;
William Juulc051bbe2007-11-15 11:13:05 +01002841
Charles Manning3796e1f2012-05-09 16:55:17 +00002842 /* If we don't already have a block being gc'd then see if we
2843 * should start another */
William Juulc051bbe2007-11-15 11:13:05 +01002844
Charles Manning3796e1f2012-05-09 16:55:17 +00002845 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 Juulc051bbe2007-11-15 11:13:05 +01002855 }
2856
Charles Manning3796e1f2012-05-09 16:55:17 +00002857 if (dev->gc_block > 0) {
2858 dev->all_gcs++;
2859 if (!aggressive)
2860 dev->passive_gc_count++;
William Juulc051bbe2007-11-15 11:13:05 +01002861
Charles Manning3796e1f2012-05-09 16:55:17 +00002862 yaffs_trace(YAFFS_TRACE_GC,
2863 "yaffs: GC n_erased_blocks %d aggressive %d",
2864 dev->n_erased_blocks, aggressive);
William Juulc051bbe2007-11-15 11:13:05 +01002865
Charles Manning3796e1f2012-05-09 16:55:17 +00002866 gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
2867 }
William Juulc051bbe2007-11-15 11:13:05 +01002868
Charles Manning3796e1f2012-05-09 16:55:17 +00002869 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 Juulc051bbe2007-11-15 11:13:05 +01002878
Charles Manning3796e1f2012-05-09 16:55:17 +00002879 return aggressive ? gc_ok : YAFFS_OK;
2880}
William Juulc051bbe2007-11-15 11:13:05 +01002881
Charles Manning3796e1f2012-05-09 16:55:17 +00002882/*
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 */
2887int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
2888{
2889 int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
William Juulc051bbe2007-11-15 11:13:05 +01002890
Charles Manning3796e1f2012-05-09 16:55:17 +00002891 yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
William Juulc051bbe2007-11-15 11:13:05 +01002892
Charles Manning3796e1f2012-05-09 16:55:17 +00002893 yaffs_check_gc(dev, 1);
2894 return erased_chunks > dev->n_free_chunks / 2;
William Juulc051bbe2007-11-15 11:13:05 +01002895}
2896
Charles Manning3796e1f2012-05-09 16:55:17 +00002897/*-------------------- Data file manipulation -----------------*/
2898
2899static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
William Juulc051bbe2007-11-15 11:13:05 +01002900{
Charles Manning3796e1f2012-05-09 16:55:17 +00002901 int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
William Juulc051bbe2007-11-15 11:13:05 +01002902
Charles Manning3796e1f2012-05-09 16:55:17 +00002903 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 Denk74e0dde2008-08-14 14:41:06 +02002914
Charles Manning3796e1f2012-05-09 16:55:17 +00002915}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02002916
Charles Manning3796e1f2012-05-09 16:55:17 +00002917void 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 Juulc051bbe2007-11-15 11:13:05 +01002924
Charles Manning3796e1f2012-05-09 16:55:17 +00002925 if (chunk_id <= 0)
2926 return;
William Juulc051bbe2007-11-15 11:13:05 +01002927
Charles Manning3796e1f2012-05-09 16:55:17 +00002928 dev->n_deletions++;
2929 block = chunk_id / dev->param.chunks_per_block;
2930 page = chunk_id % dev->param.chunks_per_block;
William Juulc051bbe2007-11-15 11:13:05 +01002931
Charles Manning3796e1f2012-05-09 16:55:17 +00002932 if (!yaffs_check_chunk_bit(dev, block, page))
2933 yaffs_trace(YAFFS_TRACE_VERIFY,
2934 "Deleting invalid chunk %d", chunk_id);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02002935
Charles Manning3796e1f2012-05-09 16:55:17 +00002936 bi = yaffs_get_block_info(dev, block);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02002937
Charles Manning3796e1f2012-05-09 16:55:17 +00002938 yaffs2_update_oldest_dirty_seq(dev, block, bi);
William Juulc051bbe2007-11-15 11:13:05 +01002939
Charles Manning3796e1f2012-05-09 16:55:17 +00002940 yaffs_trace(YAFFS_TRACE_DELETION,
2941 "line %d delete of chunk %d",
2942 lyn, chunk_id);
William Juulc051bbe2007-11-15 11:13:05 +01002943
Charles Manning3796e1f2012-05-09 16:55:17 +00002944 if (!dev->param.is_yaffs2 && mark_flash &&
2945 bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
William Juulc051bbe2007-11-15 11:13:05 +01002946
Charles Manning3796e1f2012-05-09 16:55:17 +00002947 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 Juulc051bbe2007-11-15 11:13:05 +01002953 }
2954
Charles Manning3796e1f2012-05-09 16:55:17 +00002955 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002965
Charles Manning3796e1f2012-05-09 16:55:17 +00002966 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 Juulc051bbe2007-11-15 11:13:05 +01002974
Charles Manning3796e1f2012-05-09 16:55:17 +00002975static 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 Juulc051bbe2007-11-15 11:13:05 +01002982
Charles Manning3796e1f2012-05-09 16:55:17 +00002983 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 Juulc051bbe2007-11-15 11:13:05 +01002988
Charles Manning3796e1f2012-05-09 16:55:17 +00002989 yaffs_check_gc(dev, 0);
William Juulc051bbe2007-11-15 11:13:05 +01002990
Charles Manning3796e1f2012-05-09 16:55:17 +00002991 /* 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 Juulc051bbe2007-11-15 11:13:05 +01002999
Charles Manning3796e1f2012-05-09 16:55:17 +00003000 /* Set up new tags */
3001 memset(&new_tags, 0, sizeof(new_tags));
William Juulc051bbe2007-11-15 11:13:05 +01003002
Charles Manning3796e1f2012-05-09 16:55:17 +00003003 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 Juulc051bbe2007-11-15 11:13:05 +01003008
Charles Manning3796e1f2012-05-09 16:55:17 +00003009 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 Juulc051bbe2007-11-15 11:13:05 +01003014 }
3015
Charles Manning3796e1f2012-05-09 16:55:17 +00003016 new_chunk_id =
3017 yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
William Juulc051bbe2007-11-15 11:13:05 +01003018
Charles Manning3796e1f2012-05-09 16:55:17 +00003019 if (new_chunk_id > 0) {
3020 yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003021
Charles Manning3796e1f2012-05-09 16:55:17 +00003022 if (prev_chunk_id > 0)
3023 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
3024
3025 yaffs_verify_file_sane(in);
William Juulc051bbe2007-11-15 11:13:05 +01003026 }
Charles Manning3796e1f2012-05-09 16:55:17 +00003027 return new_chunk_id;
William Juulc051bbe2007-11-15 11:13:05 +01003028
Charles Manning3796e1f2012-05-09 16:55:17 +00003029}
William Juulc051bbe2007-11-15 11:13:05 +01003030
Charles Manning3796e1f2012-05-09 16:55:17 +00003031static 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 Juulc051bbe2007-11-15 11:13:05 +01003037
Charles Manning3796e1f2012-05-09 16:55:17 +00003038 xmod.set = set;
3039 xmod.name = name;
3040 xmod.data = value;
3041 xmod.size = size;
3042 xmod.flags = flags;
3043 xmod.result = -ENOSPC;
William Juulc051bbe2007-11-15 11:13:05 +01003044
Charles Manning3796e1f2012-05-09 16:55:17 +00003045 result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
William Juulc051bbe2007-11-15 11:13:05 +01003046
Charles Manning3796e1f2012-05-09 16:55:17 +00003047 if (result > 0)
3048 return xmod.result;
3049 else
3050 return -ENOSPC;
3051}
William Juulc051bbe2007-11-15 11:13:05 +01003052
Charles Manning3796e1f2012-05-09 16:55:17 +00003053static 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 Juulc051bbe2007-11-15 11:13:05 +01003061
Charles Manning3796e1f2012-05-09 16:55:17 +00003062 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 Denk74e0dde2008-08-14 14:41:06 +02003068
Charles Manning3796e1f2012-05-09 16:55:17 +00003069 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3070 obj->xattr_known = 1;
3071 xmod->result = retval;
William Juulc051bbe2007-11-15 11:13:05 +01003072
Charles Manning3796e1f2012-05-09 16:55:17 +00003073 return retval;
3074}
William Juulc051bbe2007-11-15 11:13:05 +01003075
Charles Manning3796e1f2012-05-09 16:55:17 +00003076static 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 Juulc051bbe2007-11-15 11:13:05 +01003087
Charles Manning3796e1f2012-05-09 16:55:17 +00003088 if (obj->hdr_chunk < 1)
3089 return -ENODATA;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003090
Charles Manning3796e1f2012-05-09 16:55:17 +00003091 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003100
Charles Manning3796e1f2012-05-09 16:55:17 +00003101 buffer = (char *)yaffs_get_temp_buffer(dev);
3102 if (!buffer)
3103 return -ENOMEM;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003104
Charles Manning3796e1f2012-05-09 16:55:17 +00003105 result =
3106 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
William Juulc051bbe2007-11-15 11:13:05 +01003107
Charles Manning3796e1f2012-05-09 16:55:17 +00003108 if (result != YAFFS_OK)
3109 retval = -ENOENT;
3110 else {
3111 x_buffer = buffer + x_offs;
William Juulc051bbe2007-11-15 11:13:05 +01003112
Charles Manning3796e1f2012-05-09 16:55:17 +00003113 if (!obj->xattr_known) {
3114 obj->has_xattr = nval_hasvalues(x_buffer, x_size);
3115 obj->xattr_known = 1;
3116 }
William Juulc051bbe2007-11-15 11:13:05 +01003117
Charles Manning3796e1f2012-05-09 16:55:17 +00003118 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 Juulc051bbe2007-11-15 11:13:05 +01003126
Charles Manning3796e1f2012-05-09 16:55:17 +00003127int 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 Juulc051bbe2007-11-15 11:13:05 +01003132
Charles Manning3796e1f2012-05-09 16:55:17 +00003133int 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 Juulc051bbe2007-11-15 11:13:05 +01003137
Charles Manning3796e1f2012-05-09 16:55:17 +00003138int 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 Juulc051bbe2007-11-15 11:13:05 +01003143
Charles Manning3796e1f2012-05-09 16:55:17 +00003144int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
3145{
3146 return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
3147}
William Juulc051bbe2007-11-15 11:13:05 +01003148
Charles Manning3796e1f2012-05-09 16:55:17 +00003149static 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 Denk74e0dde2008-08-14 14:41:06 +02003155
Charles Manning3796e1f2012-05-09 16:55:17 +00003156 if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
3157 return;
William Juulc051bbe2007-11-15 11:13:05 +01003158
Charles Manning3796e1f2012-05-09 16:55:17 +00003159 dev = in->my_dev;
3160 in->lazy_loaded = 0;
3161 buf = yaffs_get_temp_buffer(dev);
William Juulc051bbe2007-11-15 11:13:05 +01003162
Anatolij Gustschin514c65c2012-10-05 23:31:03 +00003163 yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
Charles Manning3796e1f2012-05-09 16:55:17 +00003164 oh = (struct yaffs_obj_hdr *)buf;
William Juulc051bbe2007-11-15 11:13:05 +01003165
Charles Manning3796e1f2012-05-09 16:55:17 +00003166 in->yst_mode = oh->yst_mode;
3167 yaffs_load_attribs(in, oh);
3168 yaffs_set_obj_name_from_oh(in, oh);
William Juulc051bbe2007-11-15 11:13:05 +01003169
Charles Manning3796e1f2012-05-09 16:55:17 +00003170 if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
3171 in->variant.symlink_variant.alias =
3172 yaffs_clone_str(oh->alias);
Charles Manning3796e1f2012-05-09 16:55:17 +00003173 }
3174 yaffs_release_temp_buffer(dev, buf);
3175}
William Juulc051bbe2007-11-15 11:13:05 +01003176
Charles Manning3796e1f2012-05-09 16:55:17 +00003177static 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 Hofstee1990e042014-07-12 15:16:52 +02003196
3197 return;
Charles Manning3796e1f2012-05-09 16:55:17 +00003198 }
Jeroen Hofstee1990e042014-07-12 15:16:52 +02003199#endif
3200
3201 yaffs_strncpy(name, oh_name, buff_size - 1);
Charles Manning3796e1f2012-05-09 16:55:17 +00003202}
3203
3204static 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 Manning3796e1f2012-05-09 16:55:17 +00003208 int is_ascii;
3209 YCHAR *w;
William Juulc051bbe2007-11-15 11:13:05 +01003210
Charles Manning3796e1f2012-05-09 16:55:17 +00003211 if (dev->param.auto_unicode) {
William Juulc051bbe2007-11-15 11:13:05 +01003212
Charles Manning3796e1f2012-05-09 16:55:17 +00003213 is_ascii = 1;
3214 w = name;
William Juulc051bbe2007-11-15 11:13:05 +01003215
Charles Manning3796e1f2012-05-09 16:55:17 +00003216 /* 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 Hofstee1990e042014-07-12 15:16:52 +02003238
3239 return;
Charles Manning3796e1f2012-05-09 16:55:17 +00003240 }
Jeroen Hofstee1990e042014-07-12 15:16:52 +02003241#endif
3242
3243 yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
Charles Manning3796e1f2012-05-09 16:55:17 +00003244}
3245
3246/* UpdateObjectHeader updates the header on NAND for an object.
3247 * If name is not NULL, then that new name is used.
3248 */
3249int 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 Juulc051bbe2007-11-15 11:13:05 +01003252
Charles Manning3796e1f2012-05-09 16:55:17 +00003253 struct yaffs_block_info *bi;
3254 struct yaffs_dev *dev = in->my_dev;
3255 int prev_chunk_id;
3256 int ret_val = 0;
Charles Manning3796e1f2012-05-09 16:55:17 +00003257 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 Juulc051bbe2007-11-15 11:13:05 +01003265
Charles Manning3796e1f2012-05-09 16:55:17 +00003266 yaffs_strcpy(old_name, _Y("silly old name"));
William Juulc051bbe2007-11-15 11:13:05 +01003267
Charles Manning3796e1f2012-05-09 16:55:17 +00003268 if (in->fake && in != dev->root_dir && !force && !xmod)
3269 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +01003270
Charles Manning3796e1f2012-05-09 16:55:17 +00003271 yaffs_check_gc(dev, 0);
3272 yaffs_check_obj_details_loaded(in);
William Juulc051bbe2007-11-15 11:13:05 +01003273
Charles Manning3796e1f2012-05-09 16:55:17 +00003274 buffer = yaffs_get_temp_buffer(in->my_dev);
3275 oh = (struct yaffs_obj_hdr *)buffer;
William Juulc051bbe2007-11-15 11:13:05 +01003276
Charles Manning3796e1f2012-05-09 16:55:17 +00003277 prev_chunk_id = in->hdr_chunk;
3278
3279 if (prev_chunk_id > 0) {
Anatolij Gustschin514c65c2012-10-05 23:31:03 +00003280 yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
3281 buffer, &old_tags);
Charles Manning3796e1f2012-05-09 16:55:17 +00003282
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 Juulc051bbe2007-11-15 11:13:05 +01003339
Charles Manning3796e1f2012-05-09 16:55:17 +00003340 /* process any xattrib modifications */
3341 if (xmod)
3342 yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
William Juulc051bbe2007-11-15 11:13:05 +01003343
Charles Manning3796e1f2012-05-09 16:55:17 +00003344 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003350
Charles Manning3796e1f2012-05-09 16:55:17 +00003351 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003360
Charles Manning3796e1f2012-05-09 16:55:17 +00003361 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003365
Charles Manning3796e1f2012-05-09 16:55:17 +00003366 if (buffer)
3367 yaffs_release_temp_buffer(dev, buffer);
William Juulc051bbe2007-11-15 11:13:05 +01003368
Charles Manning3796e1f2012-05-09 16:55:17 +00003369 if (new_chunk_id < 0)
3370 return new_chunk_id;
William Juulc051bbe2007-11-15 11:13:05 +01003371
Charles Manning3796e1f2012-05-09 16:55:17 +00003372 in->hdr_chunk = new_chunk_id;
William Juulc051bbe2007-11-15 11:13:05 +01003373
Charles Manning3796e1f2012-05-09 16:55:17 +00003374 if (prev_chunk_id > 0)
3375 yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
William Juulc051bbe2007-11-15 11:13:05 +01003376
Charles Manning3796e1f2012-05-09 16:55:17 +00003377 if (!yaffs_obj_cache_dirty(in))
3378 in->dirty = 0;
William Juulc051bbe2007-11-15 11:13:05 +01003379
Charles Manning3796e1f2012-05-09 16:55:17 +00003380 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003387 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003388
Charles Manning3796e1f2012-05-09 16:55:17 +00003389 return new_chunk_id;
3390}
William Juulc051bbe2007-11-15 11:13:05 +01003391
Charles Manning3796e1f2012-05-09 16:55:17 +00003392/*--------------------- 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 Juulc051bbe2007-11-15 11:13:05 +01003401
Charles Manning3796e1f2012-05-09 16:55:17 +00003402int 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 Juulc051bbe2007-11-15 11:13:05 +01003411
Charles Manning3796e1f2012-05-09 16:55:17 +00003412 dev = in->my_dev;
William Juulc051bbe2007-11-15 11:13:05 +01003413
Charles Manning3796e1f2012-05-09 16:55:17 +00003414 while (n > 0) {
3415 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
3416 chunk++;
William Juulc051bbe2007-11-15 11:13:05 +01003417
Charles Manning3796e1f2012-05-09 16:55:17 +00003418 /* 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 Denk74e0dde2008-08-14 14:41:06 +02003425
Charles Manning3796e1f2012-05-09 16:55:17 +00003426 cache = yaffs_find_chunk_cache(in, chunk);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003427
Charles Manning3796e1f2012-05-09 16:55:17 +00003428 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003435
Charles Manning3796e1f2012-05-09 16:55:17 +00003436 /* If we can't find the data in the cache,
3437 * then load it up. */
William Juulc051bbe2007-11-15 11:13:05 +01003438
Charles Manning3796e1f2012-05-09 16:55:17 +00003439 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 Juulc051bbe2007-11-15 11:13:05 +01003450
Charles Manning3796e1f2012-05-09 16:55:17 +00003451 yaffs_use_cache(dev, cache, 0);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003452
Charles Manning3796e1f2012-05-09 16:55:17 +00003453 cache->locked = 1;
William Juulc051bbe2007-11-15 11:13:05 +01003454
Charles Manning3796e1f2012-05-09 16:55:17 +00003455 memcpy(buffer, &cache->data[start], n_copy);
William Juulc051bbe2007-11-15 11:13:05 +01003456
Charles Manning3796e1f2012-05-09 16:55:17 +00003457 cache->locked = 0;
3458 } else {
3459 /* Read into the local buffer then copy.. */
William Juulc051bbe2007-11-15 11:13:05 +01003460
Charles Manning3796e1f2012-05-09 16:55:17 +00003461 u8 *local_buffer =
3462 yaffs_get_temp_buffer(dev);
3463 yaffs_rd_data_obj(in, chunk, local_buffer);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003464
Charles Manning3796e1f2012-05-09 16:55:17 +00003465 memcpy(buffer, &local_buffer[start], n_copy);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003466
Charles Manning3796e1f2012-05-09 16:55:17 +00003467 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 Juulc051bbe2007-11-15 11:13:05 +01003472 }
Charles Manning3796e1f2012-05-09 16:55:17 +00003473 n -= n_copy;
3474 offset += n_copy;
3475 buffer += n_copy;
3476 n_done += n_copy;
William Juulc051bbe2007-11-15 11:13:05 +01003477 }
Charles Manning3796e1f2012-05-09 16:55:17 +00003478 return n_done;
William Juulc051bbe2007-11-15 11:13:05 +01003479}
3480
Charles Manning3796e1f2012-05-09 16:55:17 +00003481int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
3482 int n_bytes, int write_through)
William Juulc051bbe2007-11-15 11:13:05 +01003483{
William Juulc051bbe2007-11-15 11:13:05 +01003484
3485 int chunk;
Charles Manning3796e1f2012-05-09 16:55:17 +00003486 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 Denk74e0dde2008-08-14 14:41:06 +02003496
Charles Manning3796e1f2012-05-09 16:55:17 +00003497 dev = in->my_dev;
William Juulc051bbe2007-11-15 11:13:05 +01003498
Charles Manning3796e1f2012-05-09 16:55:17 +00003499 while (n > 0 && chunk_written >= 0) {
3500 yaffs_addr_to_chunk(dev, offset, &chunk, &start);
William Juulc051bbe2007-11-15 11:13:05 +01003501
Charles Manning3796e1f2012-05-09 16:55:17 +00003502 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 Juulc051bbe2007-11-15 11:13:05 +01003510
Charles Manning3796e1f2012-05-09 16:55:17 +00003511 /* OK now check for the curveball where the start and end are in
3512 * the same chunk.
3513 */
William Juulc051bbe2007-11-15 11:13:05 +01003514
Charles Manning3796e1f2012-05-09 16:55:17 +00003515 if ((start + n) < dev->data_bytes_per_chunk) {
3516 n_copy = n;
William Juulc051bbe2007-11-15 11:13:05 +01003517
Charles Manning3796e1f2012-05-09 16:55:17 +00003518 /* 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 Denk74e0dde2008-08-14 14:41:06 +02003523
Charles Manning3796e1f2012-05-09 16:55:17 +00003524 chunk_start = (((loff_t)(chunk - 1)) *
3525 dev->data_bytes_per_chunk);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003526
Charles Manning3796e1f2012-05-09 16:55:17 +00003527 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 Denk74e0dde2008-08-14 14:41:06 +02003533
Charles Manning3796e1f2012-05-09 16:55:17 +00003534 if (n_bytes_read > dev->data_bytes_per_chunk)
3535 n_bytes_read = dev->data_bytes_per_chunk;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003536
Charles Manning3796e1f2012-05-09 16:55:17 +00003537 n_writeback =
3538 (n_bytes_read >
3539 (start + n)) ? n_bytes_read : (start + n);
William Juulc051bbe2007-11-15 11:13:05 +01003540
Charles Manning3796e1f2012-05-09 16:55:17 +00003541 if (n_writeback < 0 ||
3542 n_writeback > dev->data_bytes_per_chunk)
3543 BUG();
William Juulc051bbe2007-11-15 11:13:05 +01003544
Charles Manning3796e1f2012-05-09 16:55:17 +00003545 } else {
3546 n_copy = dev->data_bytes_per_chunk - start;
3547 n_writeback = dev->data_bytes_per_chunk;
3548 }
William Juulc051bbe2007-11-15 11:13:05 +01003549
Charles Manning3796e1f2012-05-09 16:55:17 +00003550 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 Juulc051bbe2007-11-15 11:13:05 +01003558
Charles Manning3796e1f2012-05-09 16:55:17 +00003559 /* If we can't find the data in the cache, then
3560 * load the cache */
3561 cache = yaffs_find_chunk_cache(in, chunk);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003562
Charles Manning3796e1f2012-05-09 16:55:17 +00003563 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 Juulc051bbe2007-11-15 11:13:05 +01003582
Charles Manning3796e1f2012-05-09 16:55:17 +00003583 if (cache) {
3584 yaffs_use_cache(dev, cache, 1);
3585 cache->locked = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003586
Charles Manning3796e1f2012-05-09 16:55:17 +00003587 memcpy(&cache->data[start], buffer,
3588 n_copy);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003589
Charles Manning3796e1f2012-05-09 16:55:17 +00003590 cache->locked = 0;
3591 cache->n_bytes = n_writeback;
William Juulc051bbe2007-11-15 11:13:05 +01003592
Charles Manning3796e1f2012-05-09 16:55:17 +00003593 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 Juulc051bbe2007-11-15 11:13:05 +01003610
Charles Manning3796e1f2012-05-09 16:55:17 +00003611 u8 *local_buffer = yaffs_get_temp_buffer(dev);
William Juulc051bbe2007-11-15 11:13:05 +01003612
Charles Manning3796e1f2012-05-09 16:55:17 +00003613 yaffs_rd_data_obj(in, chunk, local_buffer);
3614 memcpy(&local_buffer[start], buffer, n_copy);
William Juulc051bbe2007-11-15 11:13:05 +01003615
Charles Manning3796e1f2012-05-09 16:55:17 +00003616 chunk_written =
3617 yaffs_wr_data_obj(in, chunk,
3618 local_buffer,
3619 n_writeback, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003620
Charles Manning3796e1f2012-05-09 16:55:17 +00003621 yaffs_release_temp_buffer(dev, local_buffer);
William Juulc051bbe2007-11-15 11:13:05 +01003622 }
Charles Manning3796e1f2012-05-09 16:55:17 +00003623 } else {
3624 /* A full chunk. Write directly from the buffer. */
William Juulc051bbe2007-11-15 11:13:05 +01003625
Charles Manning3796e1f2012-05-09 16:55:17 +00003626 chunk_written =
3627 yaffs_wr_data_obj(in, chunk, buffer,
3628 dev->data_bytes_per_chunk, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003629
Charles Manning3796e1f2012-05-09 16:55:17 +00003630 /* Since we've overwritten the cached data,
3631 * we better invalidate it. */
3632 yaffs_invalidate_chunk_cache(in, chunk);
3633 }
William Juulc051bbe2007-11-15 11:13:05 +01003634
Charles Manning3796e1f2012-05-09 16:55:17 +00003635 if (chunk_written >= 0) {
3636 n -= n_copy;
3637 offset += n_copy;
3638 buffer += n_copy;
3639 n_done += n_copy;
3640 }
William Juulc051bbe2007-11-15 11:13:05 +01003641 }
William Juulc051bbe2007-11-15 11:13:05 +01003642
Charles Manning3796e1f2012-05-09 16:55:17 +00003643 /* Update file object */
William Juulc051bbe2007-11-15 11:13:05 +01003644
Charles Manning3796e1f2012-05-09 16:55:17 +00003645 if ((start_write + n_done) > in->variant.file_variant.file_size)
3646 in->variant.file_variant.file_size = (start_write + n_done);
William Juulc051bbe2007-11-15 11:13:05 +01003647
Charles Manning3796e1f2012-05-09 16:55:17 +00003648 in->dirty = 1;
3649 return n_done;
3650}
William Juulc051bbe2007-11-15 11:13:05 +01003651
Charles Manning3796e1f2012-05-09 16:55:17 +00003652int 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 Juulc051bbe2007-11-15 11:13:05 +01003658
Charles Manning3796e1f2012-05-09 16:55:17 +00003659/* ---------------------- File resizing stuff ------------------ */
William Juulc051bbe2007-11-15 11:13:05 +01003660
Charles Manning3796e1f2012-05-09 16:55:17 +00003661static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
3662{
William Juulc051bbe2007-11-15 11:13:05 +01003663
Charles Manning3796e1f2012-05-09 16:55:17 +00003664 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 Denk74e0dde2008-08-14 14:41:06 +02003671
Charles Manning3796e1f2012-05-09 16:55:17 +00003672 if (old_size > 0)
3673 yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
3674 else
3675 last_del = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003676
Charles Manning3796e1f2012-05-09 16:55:17 +00003677 yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
3678 &start_del, &dummy);
3679 last_del++;
3680 start_del++;
William Juulc051bbe2007-11-15 11:13:05 +01003681
Charles Manning3796e1f2012-05-09 16:55:17 +00003682 /* 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 Denk74e0dde2008-08-14 14:41:06 +02003690
Charles Manning3796e1f2012-05-09 16:55:17 +00003691 chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
William Juulc051bbe2007-11-15 11:13:05 +01003692
Charles Manning3796e1f2012-05-09 16:55:17 +00003693 if (chunk_id < 1)
3694 continue;
William Juulc051bbe2007-11-15 11:13:05 +01003695
Charles Manning3796e1f2012-05-09 16:55:17 +00003696 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 Juulc051bbe2007-11-15 11:13:05 +01003710
Charles Manning3796e1f2012-05-09 16:55:17 +00003711void 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 Juulc051bbe2007-11-15 11:13:05 +01003716
Charles Manning3796e1f2012-05-09 16:55:17 +00003717 yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003718
Charles Manning3796e1f2012-05-09 16:55:17 +00003719 yaffs_prune_chunks(obj, new_size);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003720
Charles Manning3796e1f2012-05-09 16:55:17 +00003721 if (new_partial != 0) {
3722 int last_chunk = 1 + new_full;
3723 u8 *local_buffer = yaffs_get_temp_buffer(dev);
William Juulc051bbe2007-11-15 11:13:05 +01003724
Charles Manning3796e1f2012-05-09 16:55:17 +00003725 /* 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 Denk74e0dde2008-08-14 14:41:06 +02003729
Charles Manning3796e1f2012-05-09 16:55:17 +00003730 yaffs_wr_data_obj(obj, last_chunk, local_buffer,
3731 new_partial, 1);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003732
Charles Manning3796e1f2012-05-09 16:55:17 +00003733 yaffs_release_temp_buffer(dev, local_buffer);
3734 }
William Juulc051bbe2007-11-15 11:13:05 +01003735
Charles Manning3796e1f2012-05-09 16:55:17 +00003736 obj->variant.file_variant.file_size = new_size;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003737
Charles Manning3796e1f2012-05-09 16:55:17 +00003738 yaffs_prune_tree(dev, &obj->variant.file_variant);
3739}
William Juulc051bbe2007-11-15 11:13:05 +01003740
Charles Manning3796e1f2012-05-09 16:55:17 +00003741int 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 Denk74e0dde2008-08-14 14:41:06 +02003745
Charles Manning3796e1f2012-05-09 16:55:17 +00003746 yaffs_flush_file_cache(in);
3747 yaffs_invalidate_whole_cache(in);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003748
Charles Manning3796e1f2012-05-09 16:55:17 +00003749 yaffs_check_gc(dev, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003750
Charles Manning3796e1f2012-05-09 16:55:17 +00003751 if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
3752 return YAFFS_FAIL;
William Juulc051bbe2007-11-15 11:13:05 +01003753
Charles Manning3796e1f2012-05-09 16:55:17 +00003754 if (new_size == old_size)
3755 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01003756
Charles Manning3796e1f2012-05-09 16:55:17 +00003757 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 Denk74e0dde2008-08-14 14:41:06 +02003764
Charles Manning3796e1f2012-05-09 16:55:17 +00003765 /* 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 Juulc051bbe2007-11-15 11:13:05 +01003775
Charles Manning3796e1f2012-05-09 16:55:17 +00003776 return YAFFS_OK;
3777}
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003778
Charles Manning3796e1f2012-05-09 16:55:17 +00003779int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
3780{
3781 if (!in->dirty)
3782 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01003783
Charles Manning3796e1f2012-05-09 16:55:17 +00003784 yaffs_flush_file_cache(in);
William Juulc051bbe2007-11-15 11:13:05 +01003785
Charles Manning3796e1f2012-05-09 16:55:17 +00003786 if (data_sync)
3787 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01003788
Charles Manning3796e1f2012-05-09 16:55:17 +00003789 if (update_time)
3790 yaffs_load_current_time(in, 0, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003791
Charles Manning3796e1f2012-05-09 16:55:17 +00003792 return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
3793 YAFFS_OK : YAFFS_FAIL;
3794}
William Juulc051bbe2007-11-15 11:13:05 +01003795
Charles Manning3796e1f2012-05-09 16:55:17 +00003796/* 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 */
3800static 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 Juulc051bbe2007-11-15 11:13:05 +01003805
Charles Manning3796e1f2012-05-09 16:55:17 +00003806 if (!in->my_inode)
3807 del_now = 1;
William Juulc051bbe2007-11-15 11:13:05 +01003808
Charles Manning3796e1f2012-05-09 16:55:17 +00003809 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 Juulc051bbe2007-11-15 11:13:05 +01003828
Charles Manning3796e1f2012-05-09 16:55:17 +00003829int 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 Juulc051bbe2007-11-15 11:13:05 +01003834
Charles Manning3796e1f2012-05-09 16:55:17 +00003835 if (dev->param.disable_soft_del || dev->param.is_yaffs2)
3836 yaffs_resize_file(in, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003837
Charles Manning3796e1f2012-05-09 16:55:17 +00003838 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 Juulc051bbe2007-11-15 11:13:05 +01003844
Charles Manning3796e1f2012-05-09 16:55:17 +00003845 deleted = in->deleted;
William Juulc051bbe2007-11-15 11:13:05 +01003846
Charles Manning3796e1f2012-05-09 16:55:17 +00003847 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 Juulc051bbe2007-11-15 11:13:05 +01003859
Charles Manning3796e1f2012-05-09 16:55:17 +00003860 return YAFFS_OK;
3861 }
3862}
William Juulc051bbe2007-11-15 11:13:05 +01003863
Charles Manning3796e1f2012-05-09 16:55:17 +00003864int 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 Juulc051bbe2007-11-15 11:13:05 +01003870
Charles Manning3796e1f2012-05-09 16:55:17 +00003871static 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 Juulc051bbe2007-11-15 11:13:05 +01003876
Charles Manning3796e1f2012-05-09 16:55:17 +00003877 return yaffs_generic_obj_del(obj);
3878}
William Juulc051bbe2007-11-15 11:13:05 +01003879
Charles Manning3796e1f2012-05-09 16:55:17 +00003880static int yaffs_del_symlink(struct yaffs_obj *in)
3881{
3882 kfree(in->variant.symlink_variant.alias);
3883 in->variant.symlink_variant.alias = NULL;
William Juulc051bbe2007-11-15 11:13:05 +01003884
Charles Manning3796e1f2012-05-09 16:55:17 +00003885 return yaffs_generic_obj_del(in);
3886}
William Juulc051bbe2007-11-15 11:13:05 +01003887
Charles Manning3796e1f2012-05-09 16:55:17 +00003888static 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 Juulc051bbe2007-11-15 11:13:05 +01003896
Charles Manning3796e1f2012-05-09 16:55:17 +00003897int yaffs_del_obj(struct yaffs_obj *obj)
3898{
3899 int ret_val = -1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003900
Charles Manning3796e1f2012-05-09 16:55:17 +00003901 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 Juulc051bbe2007-11-15 11:13:05 +01003929
Charles Manning3796e1f2012-05-09 16:55:17 +00003930static int yaffs_unlink_worker(struct yaffs_obj *obj)
3931{
3932 int del_now = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003933
Charles Manning3796e1f2012-05-09 16:55:17 +00003934 if (!obj)
3935 return YAFFS_FAIL;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003936
Charles Manning3796e1f2012-05-09 16:55:17 +00003937 if (!obj->my_inode)
3938 del_now = 1;
William Juulc051bbe2007-11-15 11:13:05 +01003939
Charles Manning3796e1f2012-05-09 16:55:17 +00003940 yaffs_update_parent(obj->parent);
William Juulc051bbe2007-11-15 11:13:05 +01003941
Charles Manning3796e1f2012-05-09 16:55:17 +00003942 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 Denk74e0dde2008-08-14 14:41:06 +02003958
Charles Manning3796e1f2012-05-09 16:55:17 +00003959 struct yaffs_obj *hl;
3960 struct yaffs_obj *parent;
3961 int ret_val;
3962 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
William Juulc051bbe2007-11-15 11:13:05 +01003963
Charles Manning3796e1f2012-05-09 16:55:17 +00003964 hl = list_entry(obj->hard_links.next, struct yaffs_obj,
3965 hard_links);
William Juulc051bbe2007-11-15 11:13:05 +01003966
Charles Manning3796e1f2012-05-09 16:55:17 +00003967 yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
3968 parent = hl->parent;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02003969
Charles Manning3796e1f2012-05-09 16:55:17 +00003970 list_del_init(&hl->hard_links);
William Juulc051bbe2007-11-15 11:13:05 +01003971
Charles Manning3796e1f2012-05-09 16:55:17 +00003972 yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
William Juulc051bbe2007-11-15 11:13:05 +01003973
Charles Manning3796e1f2012-05-09 16:55:17 +00003974 ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
William Juulc051bbe2007-11-15 11:13:05 +01003975
Charles Manning3796e1f2012-05-09 16:55:17 +00003976 if (ret_val == YAFFS_OK)
3977 ret_val = yaffs_generic_obj_del(hl);
William Juulc051bbe2007-11-15 11:13:05 +01003978
Charles Manning3796e1f2012-05-09 16:55:17 +00003979 return ret_val;
William Juulc051bbe2007-11-15 11:13:05 +01003980
Charles Manning3796e1f2012-05-09 16:55:17 +00003981 } 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 Juulc051bbe2007-11-15 11:13:05 +01004008
Charles Manning3796e1f2012-05-09 16:55:17 +00004009static int yaffs_unlink_obj(struct yaffs_obj *obj)
4010{
4011 if (obj && obj->unlink_allowed)
4012 return yaffs_unlink_worker(obj);
William Juulc051bbe2007-11-15 11:13:05 +01004013
Charles Manning3796e1f2012-05-09 16:55:17 +00004014 return YAFFS_FAIL;
4015}
William Juulc051bbe2007-11-15 11:13:05 +01004016
Charles Manning3796e1f2012-05-09 16:55:17 +00004017int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
4018{
4019 struct yaffs_obj *obj;
William Juulc051bbe2007-11-15 11:13:05 +01004020
Charles Manning3796e1f2012-05-09 16:55:17 +00004021 obj = yaffs_find_by_name(dir, name);
4022 return yaffs_unlink_obj(obj);
4023}
William Juulc051bbe2007-11-15 11:13:05 +01004024
Charles Manning3796e1f2012-05-09 16:55:17 +00004025/* Note:
4026 * If old_name is NULL then we take old_dir as the object to be renamed.
4027 */
4028int 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 Juulc051bbe2007-11-15 11:13:05 +01004036
Charles Manning3796e1f2012-05-09 16:55:17 +00004037 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 Juulc051bbe2007-11-15 11:13:05 +01004045
Charles Manning3796e1f2012-05-09 16:55:17 +00004046 dev = old_dir->my_dev;
William Juulc051bbe2007-11-15 11:13:05 +01004047
Charles Manning3796e1f2012-05-09 16:55:17 +00004048#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 Juulc051bbe2007-11-15 11:13:05 +01004058
Charles Manning3796e1f2012-05-09 16:55:17 +00004059 if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
4060 YAFFS_MAX_NAME_LENGTH)
4061 /* ENAMETOOLONG */
4062 return YAFFS_FAIL;
William Juulc051bbe2007-11-15 11:13:05 +01004063
Charles Manning3796e1f2012-05-09 16:55:17 +00004064 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 Juulc051bbe2007-11-15 11:13:05 +01004070
Charles Manning3796e1f2012-05-09 16:55:17 +00004071 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 Denk74e0dde2008-08-14 14:41:06 +02004091
Charles Manning3796e1f2012-05-09 16:55:17 +00004092 result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
William Juulc051bbe2007-11-15 11:13:05 +01004093
Charles Manning3796e1f2012-05-09 16:55:17 +00004094 yaffs_update_parent(old_dir);
4095 if (new_dir != old_dir)
4096 yaffs_update_parent(new_dir);
William Juulc051bbe2007-11-15 11:13:05 +01004097
Charles Manning3796e1f2012-05-09 16:55:17 +00004098 return result;
4099 }
4100 return YAFFS_FAIL;
4101}
William Juulc051bbe2007-11-15 11:13:05 +01004102
Charles Manning3796e1f2012-05-09 16:55:17 +00004103/*----------------------- Initialisation Scanning ---------------------- */
William Juulc051bbe2007-11-15 11:13:05 +01004104
Charles Manning3796e1f2012-05-09 16:55:17 +00004105void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
4106 int backward_scanning)
4107{
4108 struct yaffs_obj *obj;
William Juulc051bbe2007-11-15 11:13:05 +01004109
Charles Manning3796e1f2012-05-09 16:55:17 +00004110 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 Juulc051bbe2007-11-15 11:13:05 +01004117 }
4118
Charles Manning3796e1f2012-05-09 16:55:17 +00004119 /* 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 Juulc051bbe2007-11-15 11:13:05 +01004122 */
Charles Manning3796e1f2012-05-09 16:55:17 +00004123 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 Denk74e0dde2008-08-14 14:41:06 +02004132
Charles Manning3796e1f2012-05-09 16:55:17 +00004133void 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 Juulc051bbe2007-11-15 11:13:05 +01004139
Charles Manning3796e1f2012-05-09 16:55:17 +00004140 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 Juulc051bbe2007-11-15 11:13:05 +01004144
Charles Manning3796e1f2012-05-09 16:55:17 +00004145 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 Juulc051bbe2007-11-15 11:13:05 +01004155 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004156 }
4157}
William Juulc051bbe2007-11-15 11:13:05 +01004158
Charles Manning3796e1f2012-05-09 16:55:17 +00004159static 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 Juulc051bbe2007-11-15 11:13:05 +01004167
Charles Manning3796e1f2012-05-09 16:55:17 +00004168 if (dev->read_only)
4169 return;
William Juulc051bbe2007-11-15 11:13:05 +01004170
Charles Manning3796e1f2012-05-09 16:55:17 +00004171 /* 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 Denk74e0dde2008-08-14 14:41:06 +02004177
Charles Manning3796e1f2012-05-09 16:55:17 +00004178 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 Juulc051bbe2007-11-15 11:13:05 +01004181 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004182}
William Juulc051bbe2007-11-15 11:13:05 +01004183
Charles Manning3796e1f2012-05-09 16:55:17 +00004184/*
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 Juulc051bbe2007-11-15 11:13:05 +01004201
Charles Manning3796e1f2012-05-09 16:55:17 +00004202static 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 Juulc051bbe2007-11-15 11:13:05 +01004206}
4207
Charles Manning3796e1f2012-05-09 16:55:17 +00004208static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004209{
Charles Manning3796e1f2012-05-09 16:55:17 +00004210 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 Denk74e0dde2008-08-14 14:41:06 +02004217
Charles Manning3796e1f2012-05-09 16:55:17 +00004218 if (dev->read_only)
4219 return;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004220
Charles Manning3796e1f2012-05-09 16:55:17 +00004221 /* Iterate through the objects in each hash entry,
4222 * looking at each object.
4223 * Make sure it is rooted.
4224 */
William Juulc051bbe2007-11-15 11:13:05 +01004225
Charles Manning3796e1f2012-05-09 16:55:17 +00004226 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 Juulc051bbe2007-11-15 11:13:05 +01004230
Charles Manning3796e1f2012-05-09 16:55:17 +00004231 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 Juulc051bbe2007-11-15 11:13:05 +01004247
Charles Manning3796e1f2012-05-09 16:55:17 +00004248 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 Juulc051bbe2007-11-15 11:13:05 +01004266 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004267}
William Juulc051bbe2007-11-15 11:13:05 +01004268
Charles Manning3796e1f2012-05-09 16:55:17 +00004269/*
4270 * Delete directory contents for cleaning up lost and found.
4271 */
4272static 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 Juulc051bbe2007-11-15 11:13:05 +01004277
Charles Manning3796e1f2012-05-09 16:55:17 +00004278 if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
4279 BUG();
William Juulc051bbe2007-11-15 11:13:05 +01004280
Charles Manning3796e1f2012-05-09 16:55:17 +00004281 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 Juulc051bbe2007-11-15 11:13:05 +01004289 }
4290}
4291
Charles Manning3796e1f2012-05-09 16:55:17 +00004292static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004293{
Charles Manning3796e1f2012-05-09 16:55:17 +00004294 yaffs_del_dir_contents(dev->lost_n_found);
4295}
4296
Charles Manning3796e1f2012-05-09 16:55:17 +00004297struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
4298 const YCHAR *name)
4299{
4300 int sum;
William Juulc051bbe2007-11-15 11:13:05 +01004301 struct list_head *i;
4302 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
Charles Manning3796e1f2012-05-09 16:55:17 +00004303 struct yaffs_obj *l;
William Juulc051bbe2007-11-15 11:13:05 +01004304
Charles Manning3796e1f2012-05-09 16:55:17 +00004305 if (!name)
William Juulc051bbe2007-11-15 11:13:05 +01004306 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01004307
4308 if (!directory) {
Charles Manning3796e1f2012-05-09 16:55:17 +00004309 yaffs_trace(YAFFS_TRACE_ALWAYS,
4310 "tragedy: yaffs_find_by_name: null pointer directory"
4311 );
4312 BUG();
4313 return NULL;
William Juulc051bbe2007-11-15 11:13:05 +01004314 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004315 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 Juulc051bbe2007-11-15 11:13:05 +01004320 }
4321
Charles Manning3796e1f2012-05-09 16:55:17 +00004322 sum = yaffs_calc_name_sum(name);
William Juulc051bbe2007-11-15 11:13:05 +01004323
Charles Manning3796e1f2012-05-09 16:55:17 +00004324 list_for_each(i, &directory->variant.dir_variant.children) {
4325 l = list_entry(i, struct yaffs_obj, siblings);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004326
Charles Manning3796e1f2012-05-09 16:55:17 +00004327 if (l->parent != directory)
4328 BUG();
William Juulc051bbe2007-11-15 11:13:05 +01004329
Charles Manning3796e1f2012-05-09 16:55:17 +00004330 yaffs_check_obj_details_loaded(l);
William Juulc051bbe2007-11-15 11:13:05 +01004331
Charles Manning3796e1f2012-05-09 16:55:17 +00004332 /* 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 Juulc051bbe2007-11-15 11:13:05 +01004344 }
4345 }
William Juulc051bbe2007-11-15 11:13:05 +01004346 return NULL;
4347}
William Juulc051bbe2007-11-15 11:13:05 +01004348
4349/* GetEquivalentObject dereferences any hard links to get to the
4350 * actual object.
4351 */
4352
Charles Manning3796e1f2012-05-09 16:55:17 +00004353struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01004354{
Charles Manning3796e1f2012-05-09 16:55:17 +00004355 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 Juulc051bbe2007-11-15 11:13:05 +01004358 }
4359 return obj;
William Juulc051bbe2007-11-15 11:13:05 +01004360}
4361
Charles Manning3796e1f2012-05-09 16:55:17 +00004362/*
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 Juulc051bbe2007-11-15 11:13:05 +01004379
Charles Manning3796e1f2012-05-09 16:55:17 +00004380 */
4381static 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 Juulc051bbe2007-11-15 11:13:05 +01004396 /* make up a name */
Charles Manning3796e1f2012-05-09 16:55:17 +00004397 yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
4398 yaffs_strcat(local_name, x);
4399 yaffs_strncpy(name, local_name, buffer_size - 1);
William Juulc051bbe2007-11-15 11:13:05 +01004400 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004401}
William Juulc051bbe2007-11-15 11:13:05 +01004402
Charles Manning3796e1f2012-05-09 16:55:17 +00004403int 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 Manning3796e1f2012-05-09 16:55:17 +00004412 u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
4413
4414 struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
William Juulc051bbe2007-11-15 11:13:05 +01004415
Charles Manning3796e1f2012-05-09 16:55:17 +00004416 memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
William Juulc051bbe2007-11-15 11:13:05 +01004417
Charles Manning3796e1f2012-05-09 16:55:17 +00004418 if (obj->hdr_chunk > 0) {
Anatolij Gustschin514c65c2012-10-05 23:31:03 +00004419 yaffs_rd_chunk_tags_nand(obj->my_dev,
4420 obj->hdr_chunk,
4421 buffer, NULL);
William Juulc051bbe2007-11-15 11:13:05 +01004422 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004423 yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
4424 buffer_size);
William Juulc051bbe2007-11-15 11:13:05 +01004425
Charles Manning3796e1f2012-05-09 16:55:17 +00004426 yaffs_release_temp_buffer(obj->my_dev, buffer);
William Juulc051bbe2007-11-15 11:13:05 +01004427 }
4428
Charles Manning3796e1f2012-05-09 16:55:17 +00004429 yaffs_fix_null_name(obj, name, buffer_size);
4430
4431 return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH);
William Juulc051bbe2007-11-15 11:13:05 +01004432}
4433
Charles Manning3796e1f2012-05-09 16:55:17 +00004434loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01004435{
William Juulc051bbe2007-11-15 11:13:05 +01004436 /* Dereference any hard linking */
Charles Manning3796e1f2012-05-09 16:55:17 +00004437 obj = yaffs_get_equivalent_obj(obj);
William Juulc051bbe2007-11-15 11:13:05 +01004438
Charles Manning3796e1f2012-05-09 16:55:17 +00004439 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 Juulc051bbe2007-11-15 11:13:05 +01004446 } else {
4447 /* Only a directory should drop through to here */
Charles Manning3796e1f2012-05-09 16:55:17 +00004448 return obj->my_dev->data_bytes_per_chunk;
William Juulc051bbe2007-11-15 11:13:05 +01004449 }
4450}
4451
Charles Manning3796e1f2012-05-09 16:55:17 +00004452int yaffs_get_obj_link_count(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01004453{
4454 int count = 0;
4455 struct list_head *i;
4456
Charles Manning3796e1f2012-05-09 16:55:17 +00004457 if (!obj->unlinked)
William Juulc051bbe2007-11-15 11:13:05 +01004458 count++; /* the object itself */
Charles Manning3796e1f2012-05-09 16:55:17 +00004459
4460 list_for_each(i, &obj->hard_links)
4461 count++; /* add the hard links; */
William Juulc051bbe2007-11-15 11:13:05 +01004462
Charles Manning3796e1f2012-05-09 16:55:17 +00004463 return count;
William Juulc051bbe2007-11-15 11:13:05 +01004464}
4465
Charles Manning3796e1f2012-05-09 16:55:17 +00004466int yaffs_get_obj_inode(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01004467{
Charles Manning3796e1f2012-05-09 16:55:17 +00004468 obj = yaffs_get_equivalent_obj(obj);
William Juulc051bbe2007-11-15 11:13:05 +01004469
Charles Manning3796e1f2012-05-09 16:55:17 +00004470 return obj->obj_id;
William Juulc051bbe2007-11-15 11:13:05 +01004471}
4472
Charles Manning3796e1f2012-05-09 16:55:17 +00004473unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01004474{
Charles Manning3796e1f2012-05-09 16:55:17 +00004475 obj = yaffs_get_equivalent_obj(obj);
William Juulc051bbe2007-11-15 11:13:05 +01004476
Charles Manning3796e1f2012-05-09 16:55:17 +00004477 switch (obj->variant_type) {
William Juulc051bbe2007-11-15 11:13:05 +01004478 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 Manning3796e1f2012-05-09 16:55:17 +00004499 return DT_REG;
4500 break;
William Juulc051bbe2007-11-15 11:13:05 +01004501 default:
4502 return DT_REG;
4503 break;
4504 }
4505}
4506
Charles Manning3796e1f2012-05-09 16:55:17 +00004507YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
William Juulc051bbe2007-11-15 11:13:05 +01004508{
Charles Manning3796e1f2012-05-09 16:55:17 +00004509 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 Juulc051bbe2007-11-15 11:13:05 +01004514}
William Juulc051bbe2007-11-15 11:13:05 +01004515
Charles Manning3796e1f2012-05-09 16:55:17 +00004516/*--------------------------- Initialisation code -------------------------- */
William Juulc051bbe2007-11-15 11:13:05 +01004517
Charles Manning3796e1f2012-05-09 16:55:17 +00004518static int yaffs_check_dev_fns(const struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004519{
William Juulc051bbe2007-11-15 11:13:05 +01004520 /* Common functions, gotta have */
Charles Manning3796e1f2012-05-09 16:55:17 +00004521 if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
William Juulc051bbe2007-11-15 11:13:05 +01004522 return 0;
4523
William Juulc051bbe2007-11-15 11:13:05 +01004524 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
Charles Manning3796e1f2012-05-09 16:55:17 +00004525 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 Juulc051bbe2007-11-15 11:13:05 +01004530 return 1;
William Juulc051bbe2007-11-15 11:13:05 +01004531
4532 /* Can use the "spare" style interface for yaffs1 */
Charles Manning3796e1f2012-05-09 16:55:17 +00004533 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 Juulc051bbe2007-11-15 11:13:05 +01004539 return 1;
4540
4541 return 0; /* bad */
4542}
4543
Charles Manning3796e1f2012-05-09 16:55:17 +00004544static int yaffs_create_initial_dir(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004545{
Charles Manning3796e1f2012-05-09 16:55:17 +00004546 /* 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 Denk74e0dde2008-08-14 14:41:06 +02004559
Charles Manning3796e1f2012-05-09 16:55:17 +00004560 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 Juulc051bbe2007-11-15 11:13:05 +01004563 return YAFFS_OK;
4564 }
William Juulc051bbe2007-11-15 11:13:05 +01004565 return YAFFS_FAIL;
4566}
4567
Charles Manning3796e1f2012-05-09 16:55:17 +00004568int yaffs_guts_initialise(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004569{
4570 int init_failed = 0;
4571 unsigned x;
4572 int bits;
4573
Charles Manning3796e1f2012-05-09 16:55:17 +00004574 yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()");
William Juulc051bbe2007-11-15 11:13:05 +01004575
4576 /* Check stuff that must be set */
4577
4578 if (!dev) {
Charles Manning3796e1f2012-05-09 16:55:17 +00004579 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 Juulc051bbe2007-11-15 11:13:05 +01004587 return YAFFS_FAIL;
4588 }
4589
Charles Manning3796e1f2012-05-09 16:55:17 +00004590 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 Juulc051bbe2007-11-15 11:13:05 +01004595
Charles Manning3796e1f2012-05-09 16:55:17 +00004596 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 Juulc051bbe2007-11-15 11:13:05 +01004603 }
4604
4605 /* Check geometry parameters. */
4606
Charles Manning3796e1f2012-05-09 16:55:17 +00004607 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 Juulc051bbe2007-11-15 11:13:05 +01004625 return YAFFS_FAIL;
4626 }
4627
Charles Manning3796e1f2012-05-09 16:55:17 +00004628 if (yaffs_init_nand(dev) != YAFFS_OK) {
4629 yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
William Juulc051bbe2007-11-15 11:13:05 +01004630 return YAFFS_FAIL;
4631 }
4632
Charles Manning3796e1f2012-05-09 16:55:17 +00004633 /* 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 Juulc051bbe2007-11-15 11:13:05 +01004641 /* Got the right mix of functions? */
Charles Manning3796e1f2012-05-09 16:55:17 +00004642 if (!yaffs_check_dev_fns(dev)) {
William Juulc051bbe2007-11-15 11:13:05 +01004643 /* Function missing */
Charles Manning3796e1f2012-05-09 16:55:17 +00004644 yaffs_trace(YAFFS_TRACE_ALWAYS,
4645 "device function(s) missing or wrong");
William Juulc051bbe2007-11-15 11:13:05 +01004646
William Juulc051bbe2007-11-15 11:13:05 +01004647 return YAFFS_FAIL;
4648 }
4649
Charles Manning3796e1f2012-05-09 16:55:17 +00004650 /* Finished with most checks. Further checks happen later on too. */
William Juulc051bbe2007-11-15 11:13:05 +01004651
Charles Manning3796e1f2012-05-09 16:55:17 +00004652 dev->is_mounted = 1;
William Juulc051bbe2007-11-15 11:13:05 +01004653
4654 /* OK now calculate a few things for the device */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004655
William Juulc051bbe2007-11-15 11:13:05 +01004656 /*
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004657 * Calculate all the chunk size manipulation numbers:
William Juulc051bbe2007-11-15 11:13:05 +01004658 */
Charles Manning3796e1f2012-05-09 16:55:17 +00004659 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 Juulc051bbe2007-11-15 11:13:05 +01004666
4667 /*
Charles Manning3796e1f2012-05-09 16:55:17 +00004668 * Calculate chunk_grp_bits.
4669 * We need to find the next power of 2 > than internal_end_block
William Juulc051bbe2007-11-15 11:13:05 +01004670 */
4671
Charles Manning3796e1f2012-05-09 16:55:17 +00004672 x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004673
Charles Manning3796e1f2012-05-09 16:55:17 +00004674 bits = calc_shifts_ceiling(x);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004675
William Juulc051bbe2007-11-15 11:13:05 +01004676 /* Set up tnode width if wide tnodes are enabled. */
Charles Manning3796e1f2012-05-09 16:55:17 +00004677 if (!dev->param.wide_tnodes_disabled) {
William Juulc051bbe2007-11-15 11:13:05 +01004678 /* bits must be even so that we end up with 32-bit words */
Charles Manning3796e1f2012-05-09 16:55:17 +00004679 if (bits & 1)
William Juulc051bbe2007-11-15 11:13:05 +01004680 bits++;
Charles Manning3796e1f2012-05-09 16:55:17 +00004681 if (bits < 16)
4682 dev->tnode_width = 16;
William Juulc051bbe2007-11-15 11:13:05 +01004683 else
Charles Manning3796e1f2012-05-09 16:55:17 +00004684 dev->tnode_width = bits;
4685 } else {
4686 dev->tnode_width = 16;
William Juulc051bbe2007-11-15 11:13:05 +01004687 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004688
Charles Manning3796e1f2012-05-09 16:55:17 +00004689 dev->tnode_mask = (1 << dev->tnode_width) - 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004690
William Juulc051bbe2007-11-15 11:13:05 +01004691 /* 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 Manning3796e1f2012-05-09 16:55:17 +00004694 * to figure out chunk shift and chunk_grp_size
William Juulc051bbe2007-11-15 11:13:05 +01004695 */
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004696
Charles Manning3796e1f2012-05-09 16:55:17 +00004697 if (bits <= dev->tnode_width)
4698 dev->chunk_grp_bits = 0;
William Juulc051bbe2007-11-15 11:13:05 +01004699 else
Charles Manning3796e1f2012-05-09 16:55:17 +00004700 dev->chunk_grp_bits = bits - dev->tnode_width;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004701
Charles Manning3796e1f2012-05-09 16:55:17 +00004702 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 Juulc051bbe2007-11-15 11:13:05 +01004705
Charles Manning3796e1f2012-05-09 16:55:17 +00004706 dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
William Juulc051bbe2007-11-15 11:13:05 +01004707
Charles Manning3796e1f2012-05-09 16:55:17 +00004708 if (dev->param.chunks_per_block < dev->chunk_grp_size) {
William Juulc051bbe2007-11-15 11:13:05 +01004709 /* 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 Manning3796e1f2012-05-09 16:55:17 +00004713 yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
William Juulc051bbe2007-11-15 11:13:05 +01004714
4715 return YAFFS_FAIL;
4716 }
4717
Charles Manning3796e1f2012-05-09 16:55:17 +00004718 /* Finished verifying the device, continue with initialisation */
William Juulc051bbe2007-11-15 11:13:05 +01004719
4720 /* More device initialisation */
Charles Manning3796e1f2012-05-09 16:55:17 +00004721 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 Juulc051bbe2007-11-15 11:13:05 +01004743
4744 /* Initialise temporary buffers and caches. */
Charles Manning3796e1f2012-05-09 16:55:17 +00004745 if (!yaffs_init_tmp_buffers(dev))
William Juulc051bbe2007-11-15 11:13:05 +01004746 init_failed = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004747
Charles Manning3796e1f2012-05-09 16:55:17 +00004748 dev->cache = NULL;
4749 dev->gc_cleanup_list = NULL;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004750
Charles Manning3796e1f2012-05-09 16:55:17 +00004751 if (!init_failed && dev->param.n_caches > 0) {
William Juulc051bbe2007-11-15 11:13:05 +01004752 int i;
Charles Manning3796e1f2012-05-09 16:55:17 +00004753 void *buf;
4754 int cache_bytes =
4755 dev->param.n_caches * sizeof(struct yaffs_cache);
William Juulc051bbe2007-11-15 11:13:05 +01004756
Charles Manning3796e1f2012-05-09 16:55:17 +00004757 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
4758 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
William Juulc051bbe2007-11-15 11:13:05 +01004759
Charles Manning3796e1f2012-05-09 16:55:17 +00004760 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004761
Charles Manning3796e1f2012-05-09 16:55:17 +00004762 buf = (u8 *) dev->cache;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004763
Charles Manning3796e1f2012-05-09 16:55:17 +00004764 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 Juulc051bbe2007-11-15 11:13:05 +01004773 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004774 if (!buf)
William Juulc051bbe2007-11-15 11:13:05 +01004775 init_failed = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004776
Charles Manning3796e1f2012-05-09 16:55:17 +00004777 dev->cache_last_use = 0;
William Juulc051bbe2007-11-15 11:13:05 +01004778 }
4779
Charles Manning3796e1f2012-05-09 16:55:17 +00004780 dev->cache_hits = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004781
Charles Manning3796e1f2012-05-09 16:55:17 +00004782 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 Juulc051bbe2007-11-15 11:13:05 +01004787 init_failed = 1;
4788 }
4789
Charles Manning3796e1f2012-05-09 16:55:17 +00004790 if (dev->param.is_yaffs2)
4791 dev->param.use_header_file_size = 1;
4792
4793 if (!init_failed && !yaffs_init_blocks(dev))
William Juulc051bbe2007-11-15 11:13:05 +01004794 init_failed = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004795
Charles Manning3796e1f2012-05-09 16:55:17 +00004796 yaffs_init_tnodes_and_objs(dev);
William Juulc051bbe2007-11-15 11:13:05 +01004797
Charles Manning3796e1f2012-05-09 16:55:17 +00004798 if (!init_failed && !yaffs_create_initial_dir(dev))
William Juulc051bbe2007-11-15 11:13:05 +01004799 init_failed = 1;
4800
Charles Manning3796e1f2012-05-09 16:55:17 +00004801 if (!init_failed && dev->param.is_yaffs2 &&
4802 !dev->param.disable_summary &&
4803 !yaffs_summary_init(dev))
4804 init_failed = 1;
William Juulc051bbe2007-11-15 11:13:05 +01004805
Charles Manning3796e1f2012-05-09 16:55:17 +00004806 if (!init_failed) {
William Juulc051bbe2007-11-15 11:13:05 +01004807 /* Now scan the flash. */
Charles Manning3796e1f2012-05-09 16:55:17 +00004808 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 Juulc051bbe2007-11-15 11:13:05 +01004815 } else {
4816
Charles Manning3796e1f2012-05-09 16:55:17 +00004817 /* Clean up the mess caused by an aborted
4818 * checkpoint load then scan backwards.
William Juulc051bbe2007-11-15 11:13:05 +01004819 */
Charles Manning3796e1f2012-05-09 16:55:17 +00004820 yaffs_deinit_blocks(dev);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004821
Charles Manning3796e1f2012-05-09 16:55:17 +00004822 yaffs_deinit_tnodes_and_objs(dev);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004823
Charles Manning3796e1f2012-05-09 16:55:17 +00004824 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 Juulc051bbe2007-11-15 11:13:05 +01004831
Charles Manning3796e1f2012-05-09 16:55:17 +00004832 if (!init_failed && !yaffs_init_blocks(dev))
William Juulc051bbe2007-11-15 11:13:05 +01004833 init_failed = 1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004834
Charles Manning3796e1f2012-05-09 16:55:17 +00004835 yaffs_init_tnodes_and_objs(dev);
William Juulc051bbe2007-11-15 11:13:05 +01004836
Charles Manning3796e1f2012-05-09 16:55:17 +00004837 if (!init_failed
4838 && !yaffs_create_initial_dir(dev))
William Juulc051bbe2007-11-15 11:13:05 +01004839 init_failed = 1;
4840
Charles Manning3796e1f2012-05-09 16:55:17 +00004841 if (!init_failed && !yaffs2_scan_backwards(dev))
William Juulc051bbe2007-11-15 11:13:05 +01004842 init_failed = 1;
4843 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004844 } 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 Juulc051bbe2007-11-15 11:13:05 +01004852 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004853
Charles Manning3796e1f2012-05-09 16:55:17 +00004854 if (init_failed) {
William Juulc051bbe2007-11-15 11:13:05 +01004855 /* Clean up the mess */
Charles Manning3796e1f2012-05-09 16:55:17 +00004856 yaffs_trace(YAFFS_TRACE_TRACING,
4857 "yaffs: yaffs_guts_initialise() aborted.");
William Juulc051bbe2007-11-15 11:13:05 +01004858
Charles Manning3796e1f2012-05-09 16:55:17 +00004859 yaffs_deinitialise(dev);
William Juulc051bbe2007-11-15 11:13:05 +01004860 return YAFFS_FAIL;
4861 }
4862
4863 /* Zero out stats */
Charles Manning3796e1f2012-05-09 16:55:17 +00004864 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 Juulc051bbe2007-11-15 11:13:05 +01004869
Charles Manning3796e1f2012-05-09 16:55:17 +00004870 dev->n_retired_blocks = 0;
William Juulc051bbe2007-11-15 11:13:05 +01004871
Charles Manning3796e1f2012-05-09 16:55:17 +00004872 yaffs_verify_free_chunks(dev);
4873 yaffs_verify_blocks(dev);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004874
Charles Manning3796e1f2012-05-09 16:55:17 +00004875 /* Clean up any aborted checkpoint data */
4876 if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
4877 yaffs2_checkpt_invalidate(dev);
William Juulc051bbe2007-11-15 11:13:05 +01004878
Charles Manning3796e1f2012-05-09 16:55:17 +00004879 yaffs_trace(YAFFS_TRACE_TRACING,
4880 "yaffs: yaffs_guts_initialise() done.");
William Juulc051bbe2007-11-15 11:13:05 +01004881 return YAFFS_OK;
William Juulc051bbe2007-11-15 11:13:05 +01004882}
4883
Charles Manning3796e1f2012-05-09 16:55:17 +00004884void yaffs_deinitialise(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004885{
Charles Manning3796e1f2012-05-09 16:55:17 +00004886 if (dev->is_mounted) {
William Juulc051bbe2007-11-15 11:13:05 +01004887 int i;
4888
Charles Manning3796e1f2012-05-09 16:55:17 +00004889 yaffs_deinit_blocks(dev);
4890 yaffs_deinit_tnodes_and_objs(dev);
4891 yaffs_summary_deinit(dev);
William Juulc051bbe2007-11-15 11:13:05 +01004892
Charles Manning3796e1f2012-05-09 16:55:17 +00004893 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 Juulc051bbe2007-11-15 11:13:05 +01004898 }
4899
Charles Manning3796e1f2012-05-09 16:55:17 +00004900 kfree(dev->cache);
4901 dev->cache = NULL;
William Juulc051bbe2007-11-15 11:13:05 +01004902 }
4903
Charles Manning3796e1f2012-05-09 16:55:17 +00004904 kfree(dev->gc_cleanup_list);
William Juulc051bbe2007-11-15 11:13:05 +01004905
Charles Manning3796e1f2012-05-09 16:55:17 +00004906 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4907 kfree(dev->temp_buffer[i].buffer);
William Juulc051bbe2007-11-15 11:13:05 +01004908
Charles Manning3796e1f2012-05-09 16:55:17 +00004909 dev->is_mounted = 0;
William Juulc051bbe2007-11-15 11:13:05 +01004910
Charles Manning3796e1f2012-05-09 16:55:17 +00004911 if (dev->param.deinitialise_flash_fn)
4912 dev->param.deinitialise_flash_fn(dev);
4913 }
William Juulc051bbe2007-11-15 11:13:05 +01004914}
4915
Charles Manning3796e1f2012-05-09 16:55:17 +00004916int yaffs_count_free_chunks(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004917{
Charles Manning3796e1f2012-05-09 16:55:17 +00004918 int n_free = 0;
William Juulc051bbe2007-11-15 11:13:05 +01004919 int b;
Charles Manning3796e1f2012-05-09 16:55:17 +00004920 struct yaffs_block_info *blk;
William Juulc051bbe2007-11-15 11:13:05 +01004921
Charles Manning3796e1f2012-05-09 16:55:17 +00004922 blk = dev->block_info;
4923 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
4924 switch (blk->block_state) {
William Juulc051bbe2007-11-15 11:13:05 +01004925 case YAFFS_BLOCK_STATE_EMPTY:
4926 case YAFFS_BLOCK_STATE_ALLOCATING:
4927 case YAFFS_BLOCK_STATE_COLLECTING:
4928 case YAFFS_BLOCK_STATE_FULL:
Charles Manning3796e1f2012-05-09 16:55:17 +00004929 n_free +=
4930 (dev->param.chunks_per_block - blk->pages_in_use +
4931 blk->soft_del_pages);
William Juulc051bbe2007-11-15 11:13:05 +01004932 break;
4933 default:
4934 break;
4935 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004936 blk++;
William Juulc051bbe2007-11-15 11:13:05 +01004937 }
Charles Manning3796e1f2012-05-09 16:55:17 +00004938 return n_free;
William Juulc051bbe2007-11-15 11:13:05 +01004939}
4940
Charles Manning3796e1f2012-05-09 16:55:17 +00004941int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +01004942{
4943 /* This is what we report to the outside world */
Charles Manning3796e1f2012-05-09 16:55:17 +00004944 int n_free;
4945 int n_dirty_caches;
4946 int blocks_for_checkpt;
4947 int i;
William Juulc051bbe2007-11-15 11:13:05 +01004948
Charles Manning3796e1f2012-05-09 16:55:17 +00004949 n_free = dev->n_free_chunks;
4950 n_free += dev->n_deleted_files;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004951
Charles Manning3796e1f2012-05-09 16:55:17 +00004952 /* Now count and subtract the number of dirty chunks in the cache. */
William Juulc051bbe2007-11-15 11:13:05 +01004953
Charles Manning3796e1f2012-05-09 16:55:17 +00004954 for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
4955 if (dev->cache[i].dirty)
4956 n_dirty_caches++;
William Juulc051bbe2007-11-15 11:13:05 +01004957 }
4958
Charles Manning3796e1f2012-05-09 16:55:17 +00004959 n_free -= n_dirty_caches;
William Juulc051bbe2007-11-15 11:13:05 +01004960
Charles Manning3796e1f2012-05-09 16:55:17 +00004961 n_free -=
4962 ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004963
Charles Manning3796e1f2012-05-09 16:55:17 +00004964 /* Now figure checkpoint space and report that... */
4965 blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +02004966
Charles Manning3796e1f2012-05-09 16:55:17 +00004967 n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
William Juulc051bbe2007-11-15 11:13:05 +01004968
Charles Manning3796e1f2012-05-09 16:55:17 +00004969 if (n_free < 0)
4970 n_free = 0;
William Juulc051bbe2007-11-15 11:13:05 +01004971
Charles Manning3796e1f2012-05-09 16:55:17 +00004972 return n_free;
William Juulc051bbe2007-11-15 11:13:05 +01004973}
4974
Charles Manning3796e1f2012-05-09 16:55:17 +00004975/*\
4976 * Marshalling functions to get loff_t file sizes into aand out of
4977 * object headers.
4978 */
4979void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize)
William Juulc051bbe2007-11-15 11:13:05 +01004980{
Charles Manning3796e1f2012-05-09 16:55:17 +00004981 oh->file_size_low = (fsize & 0xFFFFFFFF);
4982 oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF);
William Juulc051bbe2007-11-15 11:13:05 +01004983}
4984
Charles Manning3796e1f2012-05-09 16:55:17 +00004985loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh)
William Juulc051bbe2007-11-15 11:13:05 +01004986{
Charles Manning3796e1f2012-05-09 16:55:17 +00004987 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 Juulc051bbe2007-11-15 11:13:05 +01004994
Charles Manning3796e1f2012-05-09 16:55:17 +00004995 return retval;
William Juulc051bbe2007-11-15 11:13:05 +01004996}