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