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