[JFFS2] Avoid alloc/dealloc for zero sized nodes
[deliverable/linux.git] / fs / jffs2 / readinode.c
CommitLineData
1da177e4
LT
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
336d2ff7 10 * $Id: readinode.c,v 1.124 2005/07/07 15:45:29 dedekind Exp $
1da177e4
LT
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/crc32.h>
18#include <linux/pagemap.h>
19#include <linux/mtd/mtd.h>
20#include <linux/compiler.h>
21#include "nodelist.h"
22
23static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag);
24
25#if CONFIG_JFFS2_FS_DEBUG >= 2
26static void jffs2_print_fragtree(struct rb_root *list, int permitbug)
27{
28 struct jffs2_node_frag *this = frag_first(list);
29 uint32_t lastofs = 0;
30 int buggy = 0;
31
32 while(this) {
33 if (this->node)
34 printk(KERN_DEBUG "frag %04x-%04x: 0x%08x(%d) on flash (*%p). left (%p), right (%p), parent (%p)\n",
35 this->ofs, this->ofs+this->size, ref_offset(this->node->raw), ref_flags(this->node->raw),
36 this, frag_left(this), frag_right(this), frag_parent(this));
37 else
38 printk(KERN_DEBUG "frag %04x-%04x: hole (*%p). left (%p} right (%p), parent (%p)\n", this->ofs,
39 this->ofs+this->size, this, frag_left(this), frag_right(this), frag_parent(this));
40 if (this->ofs != lastofs)
41 buggy = 1;
42 lastofs = this->ofs+this->size;
43 this = frag_next(this);
44 }
45 if (buggy && !permitbug) {
46 printk(KERN_CRIT "Frag tree got a hole in it\n");
47 BUG();
48 }
49}
50
51void jffs2_print_frag_list(struct jffs2_inode_info *f)
52{
53 jffs2_print_fragtree(&f->fragtree, 0);
54
55 if (f->metadata) {
56 printk(KERN_DEBUG "metadata at 0x%08x\n", ref_offset(f->metadata->raw));
57 }
58}
59#endif
60
61#if CONFIG_JFFS2_FS_DEBUG >= 1
62static int jffs2_sanitycheck_fragtree(struct jffs2_inode_info *f)
63{
64 struct jffs2_node_frag *frag;
65 int bitched = 0;
66
67 for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
68
69 struct jffs2_full_dnode *fn = frag->node;
70 if (!fn || !fn->raw)
71 continue;
72
73 if (ref_flags(fn->raw) == REF_PRISTINE) {
74
75 if (fn->frags > 1) {
76 printk(KERN_WARNING "REF_PRISTINE node at 0x%08x had %d frags. Tell dwmw2\n", ref_offset(fn->raw), fn->frags);
77 bitched = 1;
78 }
79 /* A hole node which isn't multi-page should be garbage-collected
80 and merged anyway, so we just check for the frag size here,
81 rather than mucking around with actually reading the node
82 and checking the compression type, which is the real way
83 to tell a hole node. */
84 if (frag->ofs & (PAGE_CACHE_SIZE-1) && frag_prev(frag) && frag_prev(frag)->size < PAGE_CACHE_SIZE && frag_prev(frag)->node) {
85 printk(KERN_WARNING "REF_PRISTINE node at 0x%08x had a previous non-hole frag in the same page. Tell dwmw2\n",
86 ref_offset(fn->raw));
87 bitched = 1;
88 }
89
90 if ((frag->ofs+frag->size) & (PAGE_CACHE_SIZE-1) && frag_next(frag) && frag_next(frag)->size < PAGE_CACHE_SIZE && frag_next(frag)->node) {
91 printk(KERN_WARNING "REF_PRISTINE node at 0x%08x (%08x-%08x) had a following non-hole frag in the same page. Tell dwmw2\n",
92 ref_offset(fn->raw), frag->ofs, frag->ofs+frag->size);
93 bitched = 1;
94 }
95 }
96 }
97
98 if (bitched) {
99 struct jffs2_node_frag *thisfrag;
100
101 printk(KERN_WARNING "Inode is #%u\n", f->inocache->ino);
102 thisfrag = frag_first(&f->fragtree);
103 while (thisfrag) {
104 if (!thisfrag->node) {
105 printk("Frag @0x%x-0x%x; node-less hole\n",
106 thisfrag->ofs, thisfrag->size + thisfrag->ofs);
107 } else if (!thisfrag->node->raw) {
108 printk("Frag @0x%x-0x%x; raw-less hole\n",
109 thisfrag->ofs, thisfrag->size + thisfrag->ofs);
110 } else {
111 printk("Frag @0x%x-0x%x; raw at 0x%08x(%d) (0x%x-0x%x)\n",
112 thisfrag->ofs, thisfrag->size + thisfrag->ofs,
113 ref_offset(thisfrag->node->raw), ref_flags(thisfrag->node->raw),
114 thisfrag->node->ofs, thisfrag->node->ofs+thisfrag->node->size);
115 }
116 thisfrag = frag_next(thisfrag);
117 }
118 }
119 return bitched;
120}
121#endif /* D1 */
122
123static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
124{
125 if (this->node) {
126 this->node->frags--;
127 if (!this->node->frags) {
128 /* The node has no valid frags left. It's totally obsoleted */
129 D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
130 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size));
131 jffs2_mark_node_obsolete(c, this->node->raw);
132 jffs2_free_full_dnode(this->node);
133 } else {
134 D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) REF_NORMAL. frags is %d\n",
135 ref_offset(this->node->raw), this->node->ofs, this->node->ofs+this->node->size,
136 this->node->frags));
137 mark_ref_normal(this->node->raw);
138 }
139
140 }
141 jffs2_free_node_frag(this);
142}
143
144/* Given an inode, probably with existing list of fragments, add the new node
145 * to the fragment list.
146 */
147int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
148{
149 int ret;
150 struct jffs2_node_frag *newfrag;
151
152 D1(printk(KERN_DEBUG "jffs2_add_full_dnode_to_inode(ino #%u, f %p, fn %p)\n", f->inocache->ino, f, fn));
153
336d2ff7
AB
154 if (unlikely(!fn->size))
155 return 0;
156
1da177e4
LT
157 newfrag = jffs2_alloc_node_frag();
158 if (unlikely(!newfrag))
159 return -ENOMEM;
160
161 D2(printk(KERN_DEBUG "adding node %04x-%04x @0x%08x on flash, newfrag *%p\n",
162 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag));
163
1da177e4
LT
164 newfrag->ofs = fn->ofs;
165 newfrag->size = fn->size;
166 newfrag->node = fn;
167 newfrag->node->frags = 1;
168
169 ret = jffs2_add_frag_to_fragtree(c, &f->fragtree, newfrag);
170 if (ret)
171 return ret;
172
173 /* If we now share a page with other nodes, mark either previous
174 or next node REF_NORMAL, as appropriate. */
175 if (newfrag->ofs & (PAGE_CACHE_SIZE-1)) {
176 struct jffs2_node_frag *prev = frag_prev(newfrag);
177
178 mark_ref_normal(fn->raw);
179 /* If we don't start at zero there's _always_ a previous */
180 if (prev->node)
181 mark_ref_normal(prev->node->raw);
182 }
183
184 if ((newfrag->ofs+newfrag->size) & (PAGE_CACHE_SIZE-1)) {
185 struct jffs2_node_frag *next = frag_next(newfrag);
186
187 if (next) {
188 mark_ref_normal(fn->raw);
189 if (next->node)
190 mark_ref_normal(next->node->raw);
191 }
192 }
193 D2(if (jffs2_sanitycheck_fragtree(f)) {
194 printk(KERN_WARNING "Just added node %04x-%04x @0x%08x on flash, newfrag *%p\n",
195 fn->ofs, fn->ofs+fn->size, ref_offset(fn->raw), newfrag);
196 return 0;
197 })
198 D2(jffs2_print_frag_list(f));
199 return 0;
200}
201
202/* Doesn't set inode->i_size */
203static int jffs2_add_frag_to_fragtree(struct jffs2_sb_info *c, struct rb_root *list, struct jffs2_node_frag *newfrag)
204{
205 struct jffs2_node_frag *this;
206 uint32_t lastend;
207
208 /* Skip all the nodes which are completed before this one starts */
209 this = jffs2_lookup_node_frag(list, newfrag->node->ofs);
210
211 if (this) {
212 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
213 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this));
214 lastend = this->ofs + this->size;
215 } else {
216 D2(printk(KERN_DEBUG "j_a_f_d_t_f: Lookup gave no frag\n"));
217 lastend = 0;
218 }
219
220 /* See if we ran off the end of the list */
221 if (lastend <= newfrag->ofs) {
222 /* We did */
223
224 /* Check if 'this' node was on the same page as the new node.
225 If so, both 'this' and the new node get marked REF_NORMAL so
226 the GC can take a look.
227 */
228 if (lastend && (lastend-1) >> PAGE_CACHE_SHIFT == newfrag->ofs >> PAGE_CACHE_SHIFT) {
229 if (this->node)
230 mark_ref_normal(this->node->raw);
231 mark_ref_normal(newfrag->node->raw);
232 }
233
234 if (lastend < newfrag->node->ofs) {
235 /* ... and we need to put a hole in before the new node */
236 struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag();
237 if (!holefrag) {
238 jffs2_free_node_frag(newfrag);
239 return -ENOMEM;
240 }
241 holefrag->ofs = lastend;
242 holefrag->size = newfrag->node->ofs - lastend;
243 holefrag->node = NULL;
244 if (this) {
245 /* By definition, the 'this' node has no right-hand child,
246 because there are no frags with offset greater than it.
247 So that's where we want to put the hole */
248 D2(printk(KERN_DEBUG "Adding hole frag (%p) on right of node at (%p)\n", holefrag, this));
249 rb_link_node(&holefrag->rb, &this->rb, &this->rb.rb_right);
250 } else {
251 D2(printk(KERN_DEBUG "Adding hole frag (%p) at root of tree\n", holefrag));
252 rb_link_node(&holefrag->rb, NULL, &list->rb_node);
253 }
254 rb_insert_color(&holefrag->rb, list);
255 this = holefrag;
256 }
257 if (this) {
258 /* By definition, the 'this' node has no right-hand child,
259 because there are no frags with offset greater than it.
260 So that's where we want to put the hole */
261 D2(printk(KERN_DEBUG "Adding new frag (%p) on right of node at (%p)\n", newfrag, this));
262 rb_link_node(&newfrag->rb, &this->rb, &this->rb.rb_right);
263 } else {
264 D2(printk(KERN_DEBUG "Adding new frag (%p) at root of tree\n", newfrag));
265 rb_link_node(&newfrag->rb, NULL, &list->rb_node);
266 }
267 rb_insert_color(&newfrag->rb, list);
268 return 0;
269 }
270
271 D2(printk(KERN_DEBUG "j_a_f_d_t_f: dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p)\n",
272 this->ofs, this->ofs+this->size, this->node?(ref_offset(this->node->raw)):0xffffffff, this));
273
274 /* OK. 'this' is pointing at the first frag that newfrag->ofs at least partially obsoletes,
275 * - i.e. newfrag->ofs < this->ofs+this->size && newfrag->ofs >= this->ofs
276 */
277 if (newfrag->ofs > this->ofs) {
278 /* This node isn't completely obsoleted. The start of it remains valid */
279
280 /* Mark the new node and the partially covered node REF_NORMAL -- let
281 the GC take a look at them */
282 mark_ref_normal(newfrag->node->raw);
283 if (this->node)
284 mark_ref_normal(this->node->raw);
285
286 if (this->ofs + this->size > newfrag->ofs + newfrag->size) {
287 /* The new node splits 'this' frag into two */
288 struct jffs2_node_frag *newfrag2 = jffs2_alloc_node_frag();
289 if (!newfrag2) {
290 jffs2_free_node_frag(newfrag);
291 return -ENOMEM;
292 }
293 D2(printk(KERN_DEBUG "split old frag 0x%04x-0x%04x -->", this->ofs, this->ofs+this->size);
294 if (this->node)
295 printk("phys 0x%08x\n", ref_offset(this->node->raw));
296 else
297 printk("hole\n");
298 )
299
300 /* New second frag pointing to this's node */
301 newfrag2->ofs = newfrag->ofs + newfrag->size;
302 newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
303 newfrag2->node = this->node;
304 if (this->node)
305 this->node->frags++;
306
307 /* Adjust size of original 'this' */
308 this->size = newfrag->ofs - this->ofs;
309
310 /* Now, we know there's no node with offset
311 greater than this->ofs but smaller than
312 newfrag2->ofs or newfrag->ofs, for obvious
313 reasons. So we can do a tree insert from
314 'this' to insert newfrag, and a tree insert
315 from newfrag to insert newfrag2. */
316 jffs2_fragtree_insert(newfrag, this);
317 rb_insert_color(&newfrag->rb, list);
318
319 jffs2_fragtree_insert(newfrag2, newfrag);
320 rb_insert_color(&newfrag2->rb, list);
321
322 return 0;
323 }
324 /* New node just reduces 'this' frag in size, doesn't split it */
325 this->size = newfrag->ofs - this->ofs;
326
327 /* Again, we know it lives down here in the tree */
328 jffs2_fragtree_insert(newfrag, this);
329 rb_insert_color(&newfrag->rb, list);
330 } else {
331 /* New frag starts at the same point as 'this' used to. Replace
332 it in the tree without doing a delete and insertion */
333 D2(printk(KERN_DEBUG "Inserting newfrag (*%p),%d-%d in before 'this' (*%p),%d-%d\n",
334 newfrag, newfrag->ofs, newfrag->ofs+newfrag->size,
335 this, this->ofs, this->ofs+this->size));
336
337 rb_replace_node(&this->rb, &newfrag->rb, list);
338
339 if (newfrag->ofs + newfrag->size >= this->ofs+this->size) {
340 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x)\n", this, this->ofs, this->ofs+this->size));
341 jffs2_obsolete_node_frag(c, this);
342 } else {
343 this->ofs += newfrag->size;
344 this->size -= newfrag->size;
345
346 jffs2_fragtree_insert(this, newfrag);
347 rb_insert_color(&this->rb, list);
348 return 0;
349 }
350 }
351 /* OK, now we have newfrag added in the correct place in the tree, but
352 frag_next(newfrag) may be a fragment which is overlapped by it
353 */
354 while ((this = frag_next(newfrag)) && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
355 /* 'this' frag is obsoleted completely. */
356 D2(printk(KERN_DEBUG "Obsoleting node frag %p (%x-%x) and removing from tree\n", this, this->ofs, this->ofs+this->size));
357 rb_erase(&this->rb, list);
358 jffs2_obsolete_node_frag(c, this);
359 }
360 /* Now we're pointing at the first frag which isn't totally obsoleted by
361 the new frag */
362
363 if (!this || newfrag->ofs + newfrag->size == this->ofs) {
364 return 0;
365 }
366 /* Still some overlap but we don't need to move it in the tree */
367 this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
368 this->ofs = newfrag->ofs + newfrag->size;
369
370 /* And mark them REF_NORMAL so the GC takes a look at them */
371 if (this->node)
372 mark_ref_normal(this->node->raw);
373 mark_ref_normal(newfrag->node->raw);
374
375 return 0;
376}
377
378void jffs2_truncate_fraglist (struct jffs2_sb_info *c, struct rb_root *list, uint32_t size)
379{
380 struct jffs2_node_frag *frag = jffs2_lookup_node_frag(list, size);
381
382 D1(printk(KERN_DEBUG "Truncating fraglist to 0x%08x bytes\n", size));
383
384 /* We know frag->ofs <= size. That's what lookup does for us */
385 if (frag && frag->ofs != size) {
386 if (frag->ofs+frag->size >= size) {
387 D1(printk(KERN_DEBUG "Truncating frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size));
388 frag->size = size - frag->ofs;
389 }
390 frag = frag_next(frag);
391 }
392 while (frag && frag->ofs >= size) {
393 struct jffs2_node_frag *next = frag_next(frag);
394
395 D1(printk(KERN_DEBUG "Removing frag 0x%08x-0x%08x\n", frag->ofs, frag->ofs+frag->size));
396 frag_erase(frag, list);
397 jffs2_obsolete_node_frag(c, frag);
398 frag = next;
399 }
400}
401
402/* Scan the list of all nodes present for this ino, build map of versions, etc. */
403
404static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
405 struct jffs2_inode_info *f,
406 struct jffs2_raw_inode *latest_node);
407
408int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
409 uint32_t ino, struct jffs2_raw_inode *latest_node)
410{
411 D2(printk(KERN_DEBUG "jffs2_do_read_inode(): getting inocache\n"));
412
413 retry_inocache:
414 spin_lock(&c->inocache_lock);
415 f->inocache = jffs2_get_ino_cache(c, ino);
416
417 D2(printk(KERN_DEBUG "jffs2_do_read_inode(): Got inocache at %p\n", f->inocache));
418
419 if (f->inocache) {
420 /* Check its state. We may need to wait before we can use it */
421 switch(f->inocache->state) {
422 case INO_STATE_UNCHECKED:
423 case INO_STATE_CHECKEDABSENT:
424 f->inocache->state = INO_STATE_READING;
425 break;
426
427 case INO_STATE_CHECKING:
428 case INO_STATE_GC:
429 /* If it's in either of these states, we need
430 to wait for whoever's got it to finish and
431 put it back. */
432 D1(printk(KERN_DEBUG "jffs2_get_ino_cache_read waiting for ino #%u in state %d\n",
433 ino, f->inocache->state));
434 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
435 goto retry_inocache;
436
437 case INO_STATE_READING:
438 case INO_STATE_PRESENT:
439 /* Eep. This should never happen. It can
440 happen if Linux calls read_inode() again
441 before clear_inode() has finished though. */
442 printk(KERN_WARNING "Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
443 /* Fail. That's probably better than allowing it to succeed */
444 f->inocache = NULL;
445 break;
446
447 default:
448 BUG();
449 }
450 }
451 spin_unlock(&c->inocache_lock);
452
453 if (!f->inocache && ino == 1) {
454 /* Special case - no root inode on medium */
455 f->inocache = jffs2_alloc_inode_cache();
456 if (!f->inocache) {
457 printk(KERN_CRIT "jffs2_do_read_inode(): Cannot allocate inocache for root inode\n");
458 return -ENOMEM;
459 }
460 D1(printk(KERN_DEBUG "jffs2_do_read_inode(): Creating inocache for root inode\n"));
461 memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
462 f->inocache->ino = f->inocache->nlink = 1;
463 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
464 f->inocache->state = INO_STATE_READING;
465 jffs2_add_ino_cache(c, f->inocache);
466 }
467 if (!f->inocache) {
468 printk(KERN_WARNING "jffs2_do_read_inode() on nonexistent ino %u\n", ino);
469 return -ENOENT;
470 }
471
472 return jffs2_do_read_inode_internal(c, f, latest_node);
473}
474
475int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
476{
477 struct jffs2_raw_inode n;
478 struct jffs2_inode_info *f = kmalloc(sizeof(*f), GFP_KERNEL);
479 int ret;
480
481 if (!f)
482 return -ENOMEM;
483
484 memset(f, 0, sizeof(*f));
485 init_MUTEX_LOCKED(&f->sem);
486 f->inocache = ic;
487
488 ret = jffs2_do_read_inode_internal(c, f, &n);
489 if (!ret) {
490 up(&f->sem);
491 jffs2_do_clear_inode(c, f);
492 }
493 kfree (f);
494 return ret;
495}
496
497static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
498 struct jffs2_inode_info *f,
499 struct jffs2_raw_inode *latest_node)
500{
9dee7503
DW
501 struct jffs2_tmp_dnode_info *tn = NULL;
502 struct rb_root tn_list;
503 struct rb_node *rb, *repl_rb;
1da177e4
LT
504 struct jffs2_full_dirent *fd_list;
505 struct jffs2_full_dnode *fn = NULL;
506 uint32_t crc;
507 uint32_t latest_mctime, mctime_ver;
508 uint32_t mdata_ver = 0;
509 size_t retlen;
510 int ret;
511
512 D1(printk(KERN_DEBUG "jffs2_do_read_inode_internal(): ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink));
513
514 /* Grab all nodes relevant to this ino */
515 ret = jffs2_get_inode_nodes(c, f, &tn_list, &fd_list, &f->highest_version, &latest_mctime, &mctime_ver);
516
517 if (ret) {
518 printk(KERN_CRIT "jffs2_get_inode_nodes() for ino %u returned %d\n", f->inocache->ino, ret);
519 if (f->inocache->state == INO_STATE_READING)
520 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
521 return ret;
522 }
523 f->dents = fd_list;
524
9dee7503 525 rb = rb_first(&tn_list);
1da177e4 526
9dee7503
DW
527 while (rb) {
528 tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb);
1da177e4
LT
529 fn = tn->fn;
530
531 if (f->metadata) {
532 if (likely(tn->version >= mdata_ver)) {
533 D1(printk(KERN_DEBUG "Obsoleting old metadata at 0x%08x\n", ref_offset(f->metadata->raw)));
534 jffs2_mark_node_obsolete(c, f->metadata->raw);
535 jffs2_free_full_dnode(f->metadata);
536 f->metadata = NULL;
537
538 mdata_ver = 0;
539 } else {
540 /* This should never happen. */
541 printk(KERN_WARNING "Er. New metadata at 0x%08x with ver %d is actually older than previous ver %d at 0x%08x\n",
542 ref_offset(fn->raw), tn->version, mdata_ver, ref_offset(f->metadata->raw));
543 jffs2_mark_node_obsolete(c, fn->raw);
544 jffs2_free_full_dnode(fn);
545 /* Fill in latest_node from the metadata, not this one we're about to free... */
546 fn = f->metadata;
547 goto next_tn;
548 }
549 }
550
551 if (fn->size) {
552 jffs2_add_full_dnode_to_inode(c, f, fn);
553 } else {
554 /* Zero-sized node at end of version list. Just a metadata update */
555 D1(printk(KERN_DEBUG "metadata @%08x: ver %d\n", ref_offset(fn->raw), tn->version));
556 f->metadata = fn;
557 mdata_ver = tn->version;
558 }
559 next_tn:
9dee7503
DW
560 BUG_ON(rb->rb_left);
561 repl_rb = NULL;
562 if (rb->rb_parent && rb->rb_parent->rb_left == rb) {
563 /* We were then left-hand child of our parent. We need
564 to move our own right-hand child into our place. */
565 repl_rb = rb->rb_right;
566 if (repl_rb)
567 repl_rb->rb_parent = rb->rb_parent;
568 } else
569 repl_rb = NULL;
570
571 rb = rb_next(rb);
572
573 /* Remove the spent tn from the tree; don't bother rebalancing
574 but put our right-hand child in our own place. */
575 if (tn->rb.rb_parent) {
576 if (tn->rb.rb_parent->rb_left == &tn->rb)
577 tn->rb.rb_parent->rb_left = repl_rb;
578 else if (tn->rb.rb_parent->rb_right == &tn->rb)
579 tn->rb.rb_parent->rb_right = repl_rb;
580 else BUG();
581 } else if (tn->rb.rb_right)
582 tn->rb.rb_right->rb_parent = NULL;
583
1da177e4
LT
584 jffs2_free_tmp_dnode_info(tn);
585 }
586 D1(jffs2_sanitycheck_fragtree(f));
587
588 if (!fn) {
589 /* No data nodes for this inode. */
590 if (f->inocache->ino != 1) {
591 printk(KERN_WARNING "jffs2_do_read_inode(): No data nodes found for ino #%u\n", f->inocache->ino);
592 if (!fd_list) {
593 if (f->inocache->state == INO_STATE_READING)
594 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
595 return -EIO;
596 }
597 printk(KERN_WARNING "jffs2_do_read_inode(): But it has children so we fake some modes for it\n");
598 }
599 latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
600 latest_node->version = cpu_to_je32(0);
601 latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
602 latest_node->isize = cpu_to_je32(0);
603 latest_node->gid = cpu_to_je16(0);
604 latest_node->uid = cpu_to_je16(0);
605 if (f->inocache->state == INO_STATE_READING)
606 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
607 return 0;
608 }
609
610 ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(*latest_node), &retlen, (void *)latest_node);
611 if (ret || retlen != sizeof(*latest_node)) {
612 printk(KERN_NOTICE "MTD read in jffs2_do_read_inode() failed: Returned %d, %zd of %zd bytes read\n",
613 ret, retlen, sizeof(*latest_node));
614 /* FIXME: If this fails, there seems to be a memory leak. Find it. */
615 up(&f->sem);
616 jffs2_do_clear_inode(c, f);
617 return ret?ret:-EIO;
618 }
619
620 crc = crc32(0, latest_node, sizeof(*latest_node)-8);
621 if (crc != je32_to_cpu(latest_node->node_crc)) {
622 printk(KERN_NOTICE "CRC failed for read_inode of inode %u at physical location 0x%x\n", f->inocache->ino, ref_offset(fn->raw));
623 up(&f->sem);
624 jffs2_do_clear_inode(c, f);
625 return -EIO;
626 }
627
628 switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
629 case S_IFDIR:
630 if (mctime_ver > je32_to_cpu(latest_node->version)) {
631 /* The times in the latest_node are actually older than
632 mctime in the latest dirent. Cheat. */
633 latest_node->ctime = latest_node->mtime = cpu_to_je32(latest_mctime);
634 }
635 break;
636
637
638 case S_IFREG:
639 /* If it was a regular file, truncate it to the latest node's isize */
640 jffs2_truncate_fraglist(c, &f->fragtree, je32_to_cpu(latest_node->isize));
641 break;
642
643 case S_IFLNK:
644 /* Hack to work around broken isize in old symlink code.
645 Remove this when dwmw2 comes to his senses and stops
646 symlinks from being an entirely gratuitous special
647 case. */
648 if (!je32_to_cpu(latest_node->isize))
649 latest_node->isize = latest_node->dsize;
32f1a95d
AB
650
651 if (f->inocache->state != INO_STATE_CHECKING) {
652 /* Symlink's inode data is the target path. Read it and
653 * keep in RAM to facilitate quick follow symlink operation.
654 * We use f->dents field to store the target path, which
655 * is somewhat ugly. */
656 f->dents = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
657 if (!f->dents) {
658 printk(KERN_WARNING "Can't allocate %d bytes of memory "
659 "for the symlink target path cache\n",
660 je32_to_cpu(latest_node->csize));
661 up(&f->sem);
662 jffs2_do_clear_inode(c, f);
663 return -ENOMEM;
664 }
665
666 ret = jffs2_flash_read(c, ref_offset(fn->raw) + sizeof(*latest_node),
667 je32_to_cpu(latest_node->csize), &retlen, (char *)f->dents);
668
669 if (ret || retlen != je32_to_cpu(latest_node->csize)) {
670 if (retlen != je32_to_cpu(latest_node->csize))
671 ret = -EIO;
672 kfree(f->dents);
673 f->dents = NULL;
674 up(&f->sem);
675 jffs2_do_clear_inode(c, f);
676 return -ret;
677 }
678
679 ((char *)f->dents)[je32_to_cpu(latest_node->csize)] = '\0';
680 D1(printk(KERN_DEBUG "jffs2_do_read_inode(): symlink's target '%s' cached\n",
681 (char *)f->dents));
682 }
683
1da177e4
LT
684 /* fall through... */
685
686 case S_IFBLK:
687 case S_IFCHR:
688 /* Certain inode types should have only one data node, and it's
689 kept as the metadata node */
690 if (f->metadata) {
691 printk(KERN_WARNING "Argh. Special inode #%u with mode 0%o had metadata node\n",
692 f->inocache->ino, jemode_to_cpu(latest_node->mode));
693 up(&f->sem);
694 jffs2_do_clear_inode(c, f);
695 return -EIO;
696 }
697 if (!frag_first(&f->fragtree)) {
698 printk(KERN_WARNING "Argh. Special inode #%u with mode 0%o has no fragments\n",
699 f->inocache->ino, jemode_to_cpu(latest_node->mode));
700 up(&f->sem);
701 jffs2_do_clear_inode(c, f);
702 return -EIO;
703 }
704 /* ASSERT: f->fraglist != NULL */
705 if (frag_next(frag_first(&f->fragtree))) {
706 printk(KERN_WARNING "Argh. Special inode #%u with mode 0x%x had more than one node\n",
707 f->inocache->ino, jemode_to_cpu(latest_node->mode));
708 /* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
709 up(&f->sem);
710 jffs2_do_clear_inode(c, f);
711 return -EIO;
712 }
713 /* OK. We're happy */
714 f->metadata = frag_first(&f->fragtree)->node;
715 jffs2_free_node_frag(frag_first(&f->fragtree));
716 f->fragtree = RB_ROOT;
717 break;
718 }
719 if (f->inocache->state == INO_STATE_READING)
720 jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
721
722 return 0;
723}
724
725void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
726{
727 struct jffs2_full_dirent *fd, *fds;
728 int deleted;
729
730 down(&f->sem);
731 deleted = f->inocache && !f->inocache->nlink;
732
67e345d1
DW
733 if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
734 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
735
1da177e4
LT
736 if (f->metadata) {
737 if (deleted)
738 jffs2_mark_node_obsolete(c, f->metadata->raw);
739 jffs2_free_full_dnode(f->metadata);
740 }
741
742 jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
743
32f1a95d
AB
744 /* For symlink inodes we us f->dents to store the target path name */
745 if (S_ISLNK(OFNI_EDONI_2SFFJ(f)->i_mode)) {
746 if (f->dents) {
747 kfree(f->dents);
748 f->dents = NULL;
749 }
750 } else {
751 fds = f->dents;
1da177e4 752
32f1a95d
AB
753 while(fds) {
754 fd = fds;
755 fds = fd->next;
756 jffs2_free_full_dirent(fd);
757 }
1da177e4
LT
758 }
759
67e345d1 760 if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
1da177e4 761 jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
67e345d1
DW
762 if (f->inocache->nodes == (void *)f->inocache)
763 jffs2_del_ino_cache(c, f->inocache);
764 }
1da177e4
LT
765
766 up(&f->sem);
767}
This page took 0.117684 seconds and 5 git commands to generate.