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