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