[JFFS2] Fix write buffer retry case
[linux-2.6.git] / fs / jffs2 / write.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: write.c,v 1.88 2005/01/24 21:13:39 hammache Exp $
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/fs.h>
16 #include <linux/crc32.h>
17 #include <linux/slab.h>
18 #include <linux/pagemap.h>
19 #include <linux/mtd/mtd.h>
20 #include "nodelist.h"
21 #include "compr.h"
22
23
24 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri)
25 {
26         struct jffs2_inode_cache *ic;
27
28         ic = jffs2_alloc_inode_cache();
29         if (!ic) {
30                 return -ENOMEM;
31         }
32
33         memset(ic, 0, sizeof(*ic));
34
35         f->inocache = ic;
36         f->inocache->nlink = 1;
37         f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
38         f->inocache->ino = ++c->highest_ino;
39         f->inocache->state = INO_STATE_PRESENT;
40
41         ri->ino = cpu_to_je32(f->inocache->ino);
42
43         D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
44         jffs2_add_ino_cache(c, f->inocache);
45
46         ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
47         ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
48         ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
49         ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
50         ri->mode = cpu_to_jemode(mode);
51
52         f->highest_version = 1;
53         ri->version = cpu_to_je32(f->highest_version);
54
55         return 0;
56 }
57
58 #if CONFIG_JFFS2_FS_DEBUG > 0
59 static void writecheck(struct jffs2_sb_info *c, uint32_t ofs)
60 {
61         unsigned char buf[16];
62         size_t retlen;
63         int ret, i;
64
65         ret = jffs2_flash_read(c, ofs, 16, &retlen, buf);
66         if (ret || (retlen != 16)) {
67                 D1(printk(KERN_DEBUG "read failed or short in writecheck(). ret %d, retlen %zd\n", ret, retlen));
68                 return;
69         }
70         ret = 0;
71         for (i=0; i<16; i++) {
72                 if (buf[i] != 0xff)
73                         ret = 1;
74         }
75         if (ret) {
76                 printk(KERN_WARNING "ARGH. About to write node to 0x%08x on flash, but there are data already there:\n", ofs);
77                 printk(KERN_WARNING "0x%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", 
78                        ofs,
79                        buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
80                        buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
81         }
82 }
83 #endif
84
85
86 /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it, 
87    write it to the flash, link it into the existing inode/fragment list */
88
89 struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const unsigned char *data, uint32_t datalen, uint32_t flash_ofs, int alloc_mode)
90
91 {
92         struct jffs2_raw_node_ref *raw;
93         struct jffs2_full_dnode *fn;
94         size_t retlen;
95         struct kvec vecs[2];
96         int ret;
97         int retried = 0;
98         unsigned long cnt = 2;
99
100         D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
101                 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
102                 BUG();
103         }
104            );
105         vecs[0].iov_base = ri;
106         vecs[0].iov_len = sizeof(*ri);
107         vecs[1].iov_base = (unsigned char *)data;
108         vecs[1].iov_len = datalen;
109
110         D1(writecheck(c, flash_ofs));
111
112         if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
113                 printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n", je32_to_cpu(ri->totlen), sizeof(*ri), datalen);
114         }
115         raw = jffs2_alloc_raw_node_ref();
116         if (!raw)
117                 return ERR_PTR(-ENOMEM);
118         
119         fn = jffs2_alloc_full_dnode();
120         if (!fn) {
121                 jffs2_free_raw_node_ref(raw);
122                 return ERR_PTR(-ENOMEM);
123         }
124
125         fn->ofs = je32_to_cpu(ri->offset);
126         fn->size = je32_to_cpu(ri->dsize);
127         fn->frags = 0;
128
129         /* check number of valid vecs */
130         if (!datalen || !data)
131                 cnt = 1;
132  retry:
133         fn->raw = raw;
134
135         raw->flash_offset = flash_ofs;
136         raw->__totlen = PAD(sizeof(*ri)+datalen);
137         raw->next_phys = NULL;
138
139         if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version))
140         {
141                 if (! retried)
142                 {
143                         BUG();
144                 }
145                 else
146                 {
147                         D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d,  highest version %d -> updating dnode\n", 
148                                              je32_to_cpu(ri->version), f->highest_version));
149                         ri->version = cpu_to_je32(++f->highest_version);
150                         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
151                 }
152         }
153
154         ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
155                                  (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
156
157         if (ret || (retlen != sizeof(*ri) + datalen)) {
158                 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", 
159                        sizeof(*ri)+datalen, flash_ofs, ret, retlen);
160
161                 /* Mark the space as dirtied */
162                 if (retlen) {
163                         /* Doesn't belong to any inode */
164                         raw->next_in_ino = NULL;
165
166                         /* Don't change raw->size to match retlen. We may have 
167                            written the node header already, and only the data will
168                            seem corrupted, in which case the scan would skip over
169                            any node we write before the original intended end of 
170                            this node */
171                         raw->flash_offset |= REF_OBSOLETE;
172                         jffs2_add_physical_node_ref(c, raw);
173                         jffs2_mark_node_obsolete(c, raw);
174                 } else {
175                         printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
176                         jffs2_free_raw_node_ref(raw);
177                 }
178                 if (!retried && alloc_mode != ALLOC_NORETRY && (raw = jffs2_alloc_raw_node_ref())) {
179                         /* Try to reallocate space and retry */
180                         uint32_t dummy;
181                         struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
182
183                         retried = 1;
184
185                         D1(printk(KERN_DEBUG "Retrying failed write.\n"));
186                         
187                         ACCT_SANITY_CHECK(c,jeb);
188                         D1(ACCT_PARANOIA_CHECK(jeb));
189
190                         if (alloc_mode == ALLOC_GC) {
191                                 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs, &dummy);
192                         } else {
193                                 /* Locking pain */
194                                 up(&f->sem);
195                                 jffs2_complete_reservation(c);
196                         
197                                 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs, &dummy, alloc_mode);
198                                 down(&f->sem);
199                         }
200
201                         if (!ret) {
202                                 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
203
204                                 ACCT_SANITY_CHECK(c,jeb);
205                                 D1(ACCT_PARANOIA_CHECK(jeb));
206
207                                 goto retry;
208                         }
209                         D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
210                         jffs2_free_raw_node_ref(raw);
211                 }
212                 /* Release the full_dnode which is now useless, and return */
213                 jffs2_free_full_dnode(fn);
214                 return ERR_PTR(ret?ret:-EIO);
215         }
216         /* Mark the space used */
217         /* If node covers at least a whole page, or if it starts at the 
218            beginning of a page and runs to the end of the file, or if 
219            it's a hole node, mark it REF_PRISTINE, else REF_NORMAL. 
220         */
221         if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
222             ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
223               (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) ==  je32_to_cpu(ri->isize)))) {
224                 raw->flash_offset |= REF_PRISTINE;
225         } else {
226                 raw->flash_offset |= REF_NORMAL;
227         }
228         jffs2_add_physical_node_ref(c, raw);
229
230         /* Link into per-inode list */
231         spin_lock(&c->erase_completion_lock);
232         raw->next_in_ino = f->inocache->nodes;
233         f->inocache->nodes = raw;
234         spin_unlock(&c->erase_completion_lock);
235
236         D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n",
237                   flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize), 
238                   je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
239                   je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
240
241         if (retried) {
242                 ACCT_SANITY_CHECK(c,NULL);
243         }
244
245         return fn;
246 }
247
248 struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_raw_dirent *rd, const unsigned char *name, uint32_t namelen, uint32_t flash_ofs, int alloc_mode)
249 {
250         struct jffs2_raw_node_ref *raw;
251         struct jffs2_full_dirent *fd;
252         size_t retlen;
253         struct kvec vecs[2];
254         int retried = 0;
255         int ret;
256
257         D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n", 
258                   je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
259                   je32_to_cpu(rd->name_crc)));
260         D1(writecheck(c, flash_ofs));
261
262         D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
263                 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
264                 BUG();
265         }
266            );
267
268         vecs[0].iov_base = rd;
269         vecs[0].iov_len = sizeof(*rd);
270         vecs[1].iov_base = (unsigned char *)name;
271         vecs[1].iov_len = namelen;
272         
273         raw = jffs2_alloc_raw_node_ref();
274
275         if (!raw)
276                 return ERR_PTR(-ENOMEM);
277
278         fd = jffs2_alloc_full_dirent(namelen+1);
279         if (!fd) {
280                 jffs2_free_raw_node_ref(raw);
281                 return ERR_PTR(-ENOMEM);
282         }
283
284         fd->version = je32_to_cpu(rd->version);
285         fd->ino = je32_to_cpu(rd->ino);
286         fd->nhash = full_name_hash(name, strlen(name));
287         fd->type = rd->type;
288         memcpy(fd->name, name, namelen);
289         fd->name[namelen]=0;
290
291  retry:
292         fd->raw = raw;
293
294         raw->flash_offset = flash_ofs;
295         raw->__totlen = PAD(sizeof(*rd)+namelen);
296         raw->next_phys = NULL;
297
298         if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version))
299         {
300                 if (! retried)
301                 {
302                         BUG();
303                 }
304                 else
305                 {
306                         D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d,  highest version %d -> updating dirent\n", 
307                                              je32_to_cpu(rd->version), f->highest_version));
308                         rd->version = cpu_to_je32(++f->highest_version);
309                         fd->version = je32_to_cpu(rd->version);
310                         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
311                 }
312         }
313
314         ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
315                                  (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
316         if (ret || (retlen != sizeof(*rd) + namelen)) {
317                 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n", 
318                                sizeof(*rd)+namelen, flash_ofs, ret, retlen);
319                 /* Mark the space as dirtied */
320                 if (retlen) {
321                         raw->next_in_ino = NULL;
322                         raw->flash_offset |= REF_OBSOLETE;
323                         jffs2_add_physical_node_ref(c, raw);
324                         jffs2_mark_node_obsolete(c, raw);
325                 } else {
326                         printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
327                         jffs2_free_raw_node_ref(raw);
328                 }
329                 if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
330                         /* Try to reallocate space and retry */
331                         uint32_t dummy;
332                         struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
333
334                         retried = 1;
335
336                         D1(printk(KERN_DEBUG "Retrying failed write.\n"));
337
338                         ACCT_SANITY_CHECK(c,jeb);
339                         D1(ACCT_PARANOIA_CHECK(jeb));
340
341                         if (alloc_mode == ALLOC_GC) {
342                                 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs, &dummy);
343                         } else {
344                                 /* Locking pain */
345                                 up(&f->sem);
346                                 jffs2_complete_reservation(c);
347                         
348                                 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs, &dummy, alloc_mode);
349                                 down(&f->sem);
350                         }
351
352                         if (!ret) {
353                                 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
354                                 ACCT_SANITY_CHECK(c,jeb);
355                                 D1(ACCT_PARANOIA_CHECK(jeb));
356                                 goto retry;
357                         }
358                         D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
359                         jffs2_free_raw_node_ref(raw);
360                 }
361                 /* Release the full_dnode which is now useless, and return */
362                 jffs2_free_full_dirent(fd);
363                 return ERR_PTR(ret?ret:-EIO);
364         }
365         /* Mark the space used */
366         raw->flash_offset |= REF_PRISTINE;
367         jffs2_add_physical_node_ref(c, raw);
368
369         spin_lock(&c->erase_completion_lock);
370         raw->next_in_ino = f->inocache->nodes;
371         f->inocache->nodes = raw;
372         spin_unlock(&c->erase_completion_lock);
373
374         if (retried) {
375                 ACCT_SANITY_CHECK(c,NULL);
376         }
377
378         return fd;
379 }
380
381 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
382    we don't have to go digging in struct inode or its equivalent. It should set:
383    mode, uid, gid, (starting)isize, atime, ctime, mtime */
384 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
385                             struct jffs2_raw_inode *ri, unsigned char *buf, 
386                             uint32_t offset, uint32_t writelen, uint32_t *retlen)
387 {
388         int ret = 0;
389         uint32_t writtenlen = 0;
390
391         D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
392                   f->inocache->ino, offset, writelen));
393                 
394         while(writelen) {
395                 struct jffs2_full_dnode *fn;
396                 unsigned char *comprbuf = NULL;
397                 uint16_t comprtype = JFFS2_COMPR_NONE;
398                 uint32_t phys_ofs, alloclen;
399                 uint32_t datalen, cdatalen;
400                 int retried = 0;
401
402         retry:
403                 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
404
405                 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL);
406                 if (ret) {
407                         D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
408                         break;
409                 }
410                 down(&f->sem);
411                 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
412                 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
413
414                 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
415
416                 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
417                 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
418                 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
419                 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
420
421                 ri->ino = cpu_to_je32(f->inocache->ino);
422                 ri->version = cpu_to_je32(++f->highest_version);
423                 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
424                 ri->offset = cpu_to_je32(offset);
425                 ri->csize = cpu_to_je32(cdatalen);
426                 ri->dsize = cpu_to_je32(datalen);
427                 ri->compr = comprtype & 0xff;
428                 ri->usercompr = (comprtype >> 8 ) & 0xff;
429                 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
430                 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
431
432                 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
433
434                 jffs2_free_comprbuf(comprbuf, buf);
435
436                 if (IS_ERR(fn)) {
437                         ret = PTR_ERR(fn);
438                         up(&f->sem);
439                         jffs2_complete_reservation(c);
440                         if (!retried) {
441                                 /* Write error to be retried */
442                                 retried = 1;
443                                 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
444                                 goto retry;
445                         }
446                         break;
447                 }
448                 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
449                 if (f->metadata) {
450                         jffs2_mark_node_obsolete(c, f->metadata->raw);
451                         jffs2_free_full_dnode(f->metadata);
452                         f->metadata = NULL;
453                 }
454                 if (ret) {
455                         /* Eep */
456                         D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
457                         jffs2_mark_node_obsolete(c, fn->raw);
458                         jffs2_free_full_dnode(fn);
459
460                         up(&f->sem);
461                         jffs2_complete_reservation(c);
462                         break;
463                 }
464                 up(&f->sem);
465                 jffs2_complete_reservation(c);
466                 if (!datalen) {
467                         printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
468                         ret = -EIO;
469                         break;
470                 }
471                 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
472                 writtenlen += datalen;
473                 offset += datalen;
474                 writelen -= datalen;
475                 buf += datalen;
476         }
477         *retlen = writtenlen;
478         return ret;
479 }
480
481 int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, struct jffs2_inode_info *f, struct jffs2_raw_inode *ri, const char *name, int namelen)
482 {
483         struct jffs2_raw_dirent *rd;
484         struct jffs2_full_dnode *fn;
485         struct jffs2_full_dirent *fd;
486         uint32_t alloclen, phys_ofs;
487         int ret;
488
489         /* Try to reserve enough space for both node and dirent. 
490          * Just the node will do for now, though 
491          */
492         ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
493         D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
494         if (ret) {
495                 up(&f->sem);
496                 return ret;
497         }
498
499         ri->data_crc = cpu_to_je32(0);
500         ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
501
502         fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
503
504         D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
505                   jemode_to_cpu(ri->mode)));
506
507         if (IS_ERR(fn)) {
508                 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
509                 /* Eeek. Wave bye bye */
510                 up(&f->sem);
511                 jffs2_complete_reservation(c);
512                 return PTR_ERR(fn);
513         }
514         /* No data here. Only a metadata node, which will be 
515            obsoleted by the first data write
516         */
517         f->metadata = fn;
518
519         up(&f->sem);
520         jffs2_complete_reservation(c);
521         ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
522                 
523         if (ret) {
524                 /* Eep. */
525                 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
526                 return ret;
527         }
528
529         rd = jffs2_alloc_raw_dirent();
530         if (!rd) {
531                 /* Argh. Now we treat it like a normal delete */
532                 jffs2_complete_reservation(c);
533                 return -ENOMEM;
534         }
535
536         down(&dir_f->sem);
537
538         rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
539         rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
540         rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
541         rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
542
543         rd->pino = cpu_to_je32(dir_f->inocache->ino);
544         rd->version = cpu_to_je32(++dir_f->highest_version);
545         rd->ino = ri->ino;
546         rd->mctime = ri->ctime;
547         rd->nsize = namelen;
548         rd->type = DT_REG;
549         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
550         rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
551
552         fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
553
554         jffs2_free_raw_dirent(rd);
555         
556         if (IS_ERR(fd)) {
557                 /* dirent failed to write. Delete the inode normally 
558                    as if it were the final unlink() */
559                 jffs2_complete_reservation(c);
560                 up(&dir_f->sem);
561                 return PTR_ERR(fd);
562         }
563
564         /* Link the fd into the inode's list, obsoleting an old
565            one if necessary. */
566         jffs2_add_fd_to_list(c, fd, &dir_f->dents);
567
568         jffs2_complete_reservation(c);
569         up(&dir_f->sem);
570
571         return 0;
572 }
573
574
575 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
576                     const char *name, int namelen, struct jffs2_inode_info *dead_f)
577 {
578         struct jffs2_raw_dirent *rd;
579         struct jffs2_full_dirent *fd;
580         uint32_t alloclen, phys_ofs;
581         int ret;
582
583         if (1 /* alternative branch needs testing */ || 
584             !jffs2_can_mark_obsolete(c)) {
585                 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
586
587                 rd = jffs2_alloc_raw_dirent();
588                 if (!rd)
589                         return -ENOMEM;
590
591                 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_DELETION);
592                 if (ret) {
593                         jffs2_free_raw_dirent(rd);
594                         return ret;
595                 }
596
597                 down(&dir_f->sem);
598
599                 /* Build a deletion node */
600                 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
601                 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
602                 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
603                 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
604                 
605                 rd->pino = cpu_to_je32(dir_f->inocache->ino);
606                 rd->version = cpu_to_je32(++dir_f->highest_version);
607                 rd->ino = cpu_to_je32(0);
608                 rd->mctime = cpu_to_je32(get_seconds());
609                 rd->nsize = namelen;
610                 rd->type = DT_UNKNOWN;
611                 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
612                 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
613
614                 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION);
615                 
616                 jffs2_free_raw_dirent(rd);
617
618                 if (IS_ERR(fd)) {
619                         jffs2_complete_reservation(c);
620                         up(&dir_f->sem);
621                         return PTR_ERR(fd);
622                 }
623
624                 /* File it. This will mark the old one obsolete. */
625                 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
626                 up(&dir_f->sem);
627         } else {
628                 struct jffs2_full_dirent **prev = &dir_f->dents;
629                 uint32_t nhash = full_name_hash(name, namelen);
630
631                 down(&dir_f->sem);
632
633                 while ((*prev) && (*prev)->nhash <= nhash) {
634                         if ((*prev)->nhash == nhash && 
635                             !memcmp((*prev)->name, name, namelen) &&
636                             !(*prev)->name[namelen]) {
637                                 struct jffs2_full_dirent *this = *prev;
638
639                                 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
640                                           this->ino, ref_offset(this->raw)));
641
642                                 *prev = this->next;
643                                 jffs2_mark_node_obsolete(c, (this->raw));
644                                 jffs2_free_full_dirent(this);
645                                 break;
646                         }
647                         prev = &((*prev)->next);
648                 }
649                 up(&dir_f->sem);
650         }
651
652         /* dead_f is NULL if this was a rename not a real unlink */
653         /* Also catch the !f->inocache case, where there was a dirent
654            pointing to an inode which didn't exist. */
655         if (dead_f && dead_f->inocache) { 
656
657                 down(&dead_f->sem);
658
659                 while (dead_f->dents) {
660                         /* There can be only deleted ones */
661                         fd = dead_f->dents;
662                         
663                         dead_f->dents = fd->next;
664                         
665                         if (fd->ino) {
666                                 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
667                                        dead_f->inocache->ino, fd->name, fd->ino);
668                         } else {
669                                 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n", fd->name, dead_f->inocache->ino));
670                         }
671                         jffs2_mark_node_obsolete(c, fd->raw);
672                         jffs2_free_full_dirent(fd);
673                 }
674
675                 dead_f->inocache->nlink--;
676                 /* NB: Caller must set inode nlink if appropriate */
677                 up(&dead_f->sem);
678         }
679
680         jffs2_complete_reservation(c);
681
682         return 0;
683 }
684
685
686 int jffs2_do_link (struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f, uint32_t ino, uint8_t type, const char *name, int namelen)
687 {
688         struct jffs2_raw_dirent *rd;
689         struct jffs2_full_dirent *fd;
690         uint32_t alloclen, phys_ofs;
691         int ret;
692
693         rd = jffs2_alloc_raw_dirent();
694         if (!rd)
695                 return -ENOMEM;
696
697         ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
698         if (ret) {
699                 jffs2_free_raw_dirent(rd);
700                 return ret;
701         }
702         
703         down(&dir_f->sem);
704
705         /* Build a deletion node */
706         rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
707         rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
708         rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
709         rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
710
711         rd->pino = cpu_to_je32(dir_f->inocache->ino);
712         rd->version = cpu_to_je32(++dir_f->highest_version);
713         rd->ino = cpu_to_je32(ino);
714         rd->mctime = cpu_to_je32(get_seconds());
715         rd->nsize = namelen;
716
717         rd->type = type;
718
719         rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
720         rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
721
722         fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
723         
724         jffs2_free_raw_dirent(rd);
725
726         if (IS_ERR(fd)) {
727                 jffs2_complete_reservation(c);
728                 up(&dir_f->sem);
729                 return PTR_ERR(fd);
730         }
731
732         /* File it. This will mark the old one obsolete. */
733         jffs2_add_fd_to_list(c, fd, &dir_f->dents);
734
735         jffs2_complete_reservation(c);
736         up(&dir_f->sem);
737
738         return 0;
739 }