2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: write.c,v 1.97 2005/11/07 11:14:42 gleixner Exp $
14 #include <linux/kernel.h>
16 #include <linux/crc32.h>
17 #include <linux/slab.h>
18 #include <linux/pagemap.h>
19 #include <linux/mtd/mtd.h>
24 int jffs2_do_new_inode(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
, uint32_t mode
, struct jffs2_raw_inode
*ri
)
26 struct jffs2_inode_cache
*ic
;
28 ic
= jffs2_alloc_inode_cache();
33 memset(ic
, 0, sizeof(*ic
));
36 f
->inocache
->nlink
= 1;
37 f
->inocache
->nodes
= (struct jffs2_raw_node_ref
*)f
->inocache
;
38 f
->inocache
->state
= INO_STATE_PRESENT
;
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
);
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
);
50 f
->highest_version
= 1;
51 ri
->version
= cpu_to_je32(f
->highest_version
);
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 */
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
)
62 struct jffs2_raw_node_ref
*raw
;
63 struct jffs2_full_dnode
*fn
;
68 unsigned long cnt
= 2;
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");
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
;
80 jffs2_dbg_prewrite_paranoia_check(c
, flash_ofs
, vecs
[0].iov_len
+ vecs
[1].iov_len
);
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
);
85 raw
= jffs2_alloc_raw_node_ref();
87 return ERR_PTR(-ENOMEM
);
89 fn
= jffs2_alloc_full_dnode();
91 jffs2_free_raw_node_ref(raw
);
92 return ERR_PTR(-ENOMEM
);
95 fn
->ofs
= je32_to_cpu(ri
->offset
);
96 fn
->size
= je32_to_cpu(ri
->dsize
);
99 /* check number of valid vecs */
100 if (!datalen
|| !data
)
105 raw
->flash_offset
= flash_ofs
;
107 if ((alloc_mode
!=ALLOC_GC
) && (je32_to_cpu(ri
->version
) < f
->highest_version
)) {
109 D1(printk(KERN_DEBUG
"jffs2_write_dnode : dnode_version %d, "
110 "highest version %d -> updating dnode\n",
111 je32_to_cpu(ri
->version
), f
->highest_version
));
112 ri
->version
= cpu_to_je32(++f
->highest_version
);
113 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
116 ret
= jffs2_flash_writev(c
, vecs
, cnt
, flash_ofs
, &retlen
,
117 (alloc_mode
==ALLOC_GC
)?0:f
->inocache
->ino
);
119 if (ret
|| (retlen
!= sizeof(*ri
) + datalen
)) {
120 printk(KERN_NOTICE
"Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
121 sizeof(*ri
)+datalen
, flash_ofs
, ret
, retlen
);
123 /* Mark the space as dirtied */
125 /* Doesn't belong to any inode */
126 raw
->next_in_ino
= NULL
;
128 /* Don't change raw->size to match retlen. We may have
129 written the node header already, and only the data will
130 seem corrupted, in which case the scan would skip over
131 any node we write before the original intended end of
133 raw
->flash_offset
|= REF_OBSOLETE
;
134 jffs2_add_physical_node_ref(c
, raw
, PAD(sizeof(*ri
)+datalen
));
135 jffs2_mark_node_obsolete(c
, raw
);
137 printk(KERN_NOTICE
"Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw
->flash_offset
);
138 jffs2_free_raw_node_ref(raw
);
140 if (!retried
&& alloc_mode
!= ALLOC_NORETRY
&& (raw
= jffs2_alloc_raw_node_ref())) {
141 /* Try to reallocate space and retry */
143 struct jffs2_eraseblock
*jeb
= &c
->blocks
[flash_ofs
/ c
->sector_size
];
147 D1(printk(KERN_DEBUG
"Retrying failed write.\n"));
149 jffs2_dbg_acct_sanity_check(c
,jeb
);
150 jffs2_dbg_acct_paranoia_check(c
, jeb
);
152 if (alloc_mode
== ALLOC_GC
) {
153 ret
= jffs2_reserve_space_gc(c
, sizeof(*ri
) + datalen
, &flash_ofs
,
154 &dummy
, JFFS2_SUMMARY_INODE_SIZE
);
158 jffs2_complete_reservation(c
);
160 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + datalen
, &flash_ofs
,
161 &dummy
, alloc_mode
, JFFS2_SUMMARY_INODE_SIZE
);
166 D1(printk(KERN_DEBUG
"Allocated space at 0x%08x to retry failed write.\n", flash_ofs
));
168 jffs2_dbg_acct_sanity_check(c
,jeb
);
169 jffs2_dbg_acct_paranoia_check(c
, jeb
);
173 D1(printk(KERN_DEBUG
"Failed to allocate space to retry failed write: %d!\n", ret
));
174 jffs2_free_raw_node_ref(raw
);
176 /* Release the full_dnode which is now useless, and return */
177 jffs2_free_full_dnode(fn
);
178 return ERR_PTR(ret
?ret
:-EIO
);
180 /* Mark the space used */
181 /* If node covers at least a whole page, or if it starts at the
182 beginning of a page and runs to the end of the file, or if
183 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
185 if ((je32_to_cpu(ri
->dsize
) >= PAGE_CACHE_SIZE
) ||
186 ( ((je32_to_cpu(ri
->offset
)&(PAGE_CACHE_SIZE
-1))==0) &&
187 (je32_to_cpu(ri
->dsize
)+je32_to_cpu(ri
->offset
) == je32_to_cpu(ri
->isize
)))) {
188 raw
->flash_offset
|= REF_PRISTINE
;
190 raw
->flash_offset
|= REF_NORMAL
;
192 jffs2_add_physical_node_ref(c
, raw
, PAD(sizeof(*ri
)+datalen
));
194 /* Link into per-inode list */
195 spin_lock(&c
->erase_completion_lock
);
196 raw
->next_in_ino
= f
->inocache
->nodes
;
197 f
->inocache
->nodes
= raw
;
198 spin_unlock(&c
->erase_completion_lock
);
200 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",
201 flash_ofs
, ref_flags(raw
), je32_to_cpu(ri
->dsize
),
202 je32_to_cpu(ri
->csize
), je32_to_cpu(ri
->node_crc
),
203 je32_to_cpu(ri
->data_crc
), je32_to_cpu(ri
->totlen
)));
206 jffs2_dbg_acct_sanity_check(c
,NULL
);
212 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
)
214 struct jffs2_raw_node_ref
*raw
;
215 struct jffs2_full_dirent
*fd
;
221 D1(printk(KERN_DEBUG
"jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
222 je32_to_cpu(rd
->pino
), name
, name
, je32_to_cpu(rd
->ino
),
223 je32_to_cpu(rd
->name_crc
)));
225 D1(if(je32_to_cpu(rd
->hdr_crc
) != crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4)) {
226 printk(KERN_CRIT
"Eep. CRC not correct in jffs2_write_dirent()\n");
231 vecs
[0].iov_base
= rd
;
232 vecs
[0].iov_len
= sizeof(*rd
);
233 vecs
[1].iov_base
= (unsigned char *)name
;
234 vecs
[1].iov_len
= namelen
;
236 jffs2_dbg_prewrite_paranoia_check(c
, flash_ofs
, vecs
[0].iov_len
+ vecs
[1].iov_len
);
238 raw
= jffs2_alloc_raw_node_ref();
241 return ERR_PTR(-ENOMEM
);
243 fd
= jffs2_alloc_full_dirent(namelen
+1);
245 jffs2_free_raw_node_ref(raw
);
246 return ERR_PTR(-ENOMEM
);
249 fd
->version
= je32_to_cpu(rd
->version
);
250 fd
->ino
= je32_to_cpu(rd
->ino
);
251 fd
->nhash
= full_name_hash(name
, strlen(name
));
253 memcpy(fd
->name
, name
, namelen
);
259 raw
->flash_offset
= flash_ofs
;
261 if ((alloc_mode
!=ALLOC_GC
) && (je32_to_cpu(rd
->version
) < f
->highest_version
)) {
263 D1(printk(KERN_DEBUG
"jffs2_write_dirent : dirent_version %d, "
264 "highest version %d -> updating dirent\n",
265 je32_to_cpu(rd
->version
), f
->highest_version
));
266 rd
->version
= cpu_to_je32(++f
->highest_version
);
267 fd
->version
= je32_to_cpu(rd
->version
);
268 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
271 ret
= jffs2_flash_writev(c
, vecs
, 2, flash_ofs
, &retlen
,
272 (alloc_mode
==ALLOC_GC
)?0:je32_to_cpu(rd
->pino
));
273 if (ret
|| (retlen
!= sizeof(*rd
) + namelen
)) {
274 printk(KERN_NOTICE
"Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
275 sizeof(*rd
)+namelen
, flash_ofs
, ret
, retlen
);
276 /* Mark the space as dirtied */
278 raw
->next_in_ino
= NULL
;
279 raw
->flash_offset
|= REF_OBSOLETE
;
280 jffs2_add_physical_node_ref(c
, raw
, PAD(sizeof(*rd
)+namelen
));
281 jffs2_mark_node_obsolete(c
, raw
);
283 printk(KERN_NOTICE
"Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw
->flash_offset
);
284 jffs2_free_raw_node_ref(raw
);
286 if (!retried
&& (raw
= jffs2_alloc_raw_node_ref())) {
287 /* Try to reallocate space and retry */
289 struct jffs2_eraseblock
*jeb
= &c
->blocks
[flash_ofs
/ c
->sector_size
];
293 D1(printk(KERN_DEBUG
"Retrying failed write.\n"));
295 jffs2_dbg_acct_sanity_check(c
,jeb
);
296 jffs2_dbg_acct_paranoia_check(c
, jeb
);
298 if (alloc_mode
== ALLOC_GC
) {
299 ret
= jffs2_reserve_space_gc(c
, sizeof(*rd
) + namelen
, &flash_ofs
,
300 &dummy
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
304 jffs2_complete_reservation(c
);
306 ret
= jffs2_reserve_space(c
, sizeof(*rd
) + namelen
, &flash_ofs
,
307 &dummy
, alloc_mode
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
312 D1(printk(KERN_DEBUG
"Allocated space at 0x%08x to retry failed write.\n", flash_ofs
));
313 jffs2_dbg_acct_sanity_check(c
,jeb
);
314 jffs2_dbg_acct_paranoia_check(c
, jeb
);
317 D1(printk(KERN_DEBUG
"Failed to allocate space to retry failed write: %d!\n", ret
));
318 jffs2_free_raw_node_ref(raw
);
320 /* Release the full_dnode which is now useless, and return */
321 jffs2_free_full_dirent(fd
);
322 return ERR_PTR(ret
?ret
:-EIO
);
324 /* Mark the space used */
325 raw
->flash_offset
|= REF_PRISTINE
;
326 jffs2_add_physical_node_ref(c
, raw
, PAD(sizeof(*rd
)+namelen
));
328 spin_lock(&c
->erase_completion_lock
);
329 raw
->next_in_ino
= f
->inocache
->nodes
;
330 f
->inocache
->nodes
= raw
;
331 spin_unlock(&c
->erase_completion_lock
);
334 jffs2_dbg_acct_sanity_check(c
,NULL
);
340 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
341 we don't have to go digging in struct inode or its equivalent. It should set:
342 mode, uid, gid, (starting)isize, atime, ctime, mtime */
343 int jffs2_write_inode_range(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*f
,
344 struct jffs2_raw_inode
*ri
, unsigned char *buf
,
345 uint32_t offset
, uint32_t writelen
, uint32_t *retlen
)
348 uint32_t writtenlen
= 0;
350 D1(printk(KERN_DEBUG
"jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
351 f
->inocache
->ino
, offset
, writelen
));
354 struct jffs2_full_dnode
*fn
;
355 unsigned char *comprbuf
= NULL
;
356 uint16_t comprtype
= JFFS2_COMPR_NONE
;
357 uint32_t phys_ofs
, alloclen
;
358 uint32_t datalen
, cdatalen
;
362 D2(printk(KERN_DEBUG
"jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen
, offset
));
364 ret
= jffs2_reserve_space(c
, sizeof(*ri
) + JFFS2_MIN_DATA_LEN
, &phys_ofs
,
365 &alloclen
, ALLOC_NORMAL
, JFFS2_SUMMARY_INODE_SIZE
);
367 D1(printk(KERN_DEBUG
"jffs2_reserve_space returned %d\n", ret
));
371 datalen
= min_t(uint32_t, writelen
, PAGE_CACHE_SIZE
- (offset
& (PAGE_CACHE_SIZE
-1)));
372 cdatalen
= min_t(uint32_t, alloclen
- sizeof(*ri
), datalen
);
374 comprtype
= jffs2_compress(c
, f
, buf
, &comprbuf
, &datalen
, &cdatalen
);
376 ri
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
377 ri
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_INODE
);
378 ri
->totlen
= cpu_to_je32(sizeof(*ri
) + cdatalen
);
379 ri
->hdr_crc
= cpu_to_je32(crc32(0, ri
, sizeof(struct jffs2_unknown_node
)-4));
381 ri
->ino
= cpu_to_je32(f
->inocache
->ino
);
382 ri
->version
= cpu_to_je32(++f
->highest_version
);
383 ri
->isize
= cpu_to_je32(max(je32_to_cpu(ri
->isize
), offset
+ datalen
));
384 ri
->offset
= cpu_to_je32(offset
);
385 ri
->csize
= cpu_to_je32(cdatalen
);
386 ri
->dsize
= cpu_to_je32(datalen
);
387 ri
->compr
= comprtype
& 0xff;
388 ri
->usercompr
= (comprtype
>> 8 ) & 0xff;
389 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
390 ri
->data_crc
= cpu_to_je32(crc32(0, comprbuf
, cdatalen
));
392 fn
= jffs2_write_dnode(c
, f
, ri
, comprbuf
, cdatalen
, phys_ofs
, ALLOC_NORETRY
);
394 jffs2_free_comprbuf(comprbuf
, buf
);
399 jffs2_complete_reservation(c
);
401 /* Write error to be retried */
403 D1(printk(KERN_DEBUG
"Retrying node write in jffs2_write_inode_range()\n"));
408 ret
= jffs2_add_full_dnode_to_inode(c
, f
, fn
);
410 jffs2_mark_node_obsolete(c
, f
->metadata
->raw
);
411 jffs2_free_full_dnode(f
->metadata
);
416 D1(printk(KERN_DEBUG
"Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret
));
417 jffs2_mark_node_obsolete(c
, fn
->raw
);
418 jffs2_free_full_dnode(fn
);
421 jffs2_complete_reservation(c
);
425 jffs2_complete_reservation(c
);
427 printk(KERN_WARNING
"Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
431 D1(printk(KERN_DEBUG
"increasing writtenlen by %d\n", datalen
));
432 writtenlen
+= datalen
;
437 *retlen
= writtenlen
;
441 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
)
443 struct jffs2_raw_dirent
*rd
;
444 struct jffs2_full_dnode
*fn
;
445 struct jffs2_full_dirent
*fd
;
446 uint32_t alloclen
, phys_ofs
;
449 /* Try to reserve enough space for both node and dirent.
450 * Just the node will do for now, though
452 ret
= jffs2_reserve_space(c
, sizeof(*ri
), &phys_ofs
, &alloclen
, ALLOC_NORMAL
,
453 JFFS2_SUMMARY_INODE_SIZE
);
454 D1(printk(KERN_DEBUG
"jffs2_do_create(): reserved 0x%x bytes\n", alloclen
));
460 ri
->data_crc
= cpu_to_je32(0);
461 ri
->node_crc
= cpu_to_je32(crc32(0, ri
, sizeof(*ri
)-8));
463 fn
= jffs2_write_dnode(c
, f
, ri
, NULL
, 0, phys_ofs
, ALLOC_NORMAL
);
465 D1(printk(KERN_DEBUG
"jffs2_do_create created file with mode 0x%x\n",
466 jemode_to_cpu(ri
->mode
)));
469 D1(printk(KERN_DEBUG
"jffs2_write_dnode() failed\n"));
470 /* Eeek. Wave bye bye */
472 jffs2_complete_reservation(c
);
475 /* No data here. Only a metadata node, which will be
476 obsoleted by the first data write
481 jffs2_complete_reservation(c
);
482 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
,
483 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
487 D1(printk(KERN_DEBUG
"jffs2_reserve_space() for dirent failed\n"));
491 rd
= jffs2_alloc_raw_dirent();
493 /* Argh. Now we treat it like a normal delete */
494 jffs2_complete_reservation(c
);
500 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
501 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
502 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
503 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
505 rd
->pino
= cpu_to_je32(dir_f
->inocache
->ino
);
506 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
508 rd
->mctime
= ri
->ctime
;
511 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
512 rd
->name_crc
= cpu_to_je32(crc32(0, name
, namelen
));
514 fd
= jffs2_write_dirent(c
, dir_f
, rd
, name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
516 jffs2_free_raw_dirent(rd
);
519 /* dirent failed to write. Delete the inode normally
520 as if it were the final unlink() */
521 jffs2_complete_reservation(c
);
526 /* Link the fd into the inode's list, obsoleting an old
528 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
530 jffs2_complete_reservation(c
);
537 int jffs2_do_unlink(struct jffs2_sb_info
*c
, struct jffs2_inode_info
*dir_f
,
538 const char *name
, int namelen
, struct jffs2_inode_info
*dead_f
,
541 struct jffs2_raw_dirent
*rd
;
542 struct jffs2_full_dirent
*fd
;
543 uint32_t alloclen
, phys_ofs
;
546 if (1 /* alternative branch needs testing */ ||
547 !jffs2_can_mark_obsolete(c
)) {
548 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
550 rd
= jffs2_alloc_raw_dirent();
554 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
,
555 ALLOC_DELETION
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
557 jffs2_free_raw_dirent(rd
);
563 /* Build a deletion node */
564 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
565 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
566 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
567 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
569 rd
->pino
= cpu_to_je32(dir_f
->inocache
->ino
);
570 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
571 rd
->ino
= cpu_to_je32(0);
572 rd
->mctime
= cpu_to_je32(time
);
574 rd
->type
= DT_UNKNOWN
;
575 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
576 rd
->name_crc
= cpu_to_je32(crc32(0, name
, namelen
));
578 fd
= jffs2_write_dirent(c
, dir_f
, rd
, name
, namelen
, phys_ofs
, ALLOC_DELETION
);
580 jffs2_free_raw_dirent(rd
);
583 jffs2_complete_reservation(c
);
588 /* File it. This will mark the old one obsolete. */
589 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
592 struct jffs2_full_dirent
**prev
= &dir_f
->dents
;
593 uint32_t nhash
= full_name_hash(name
, namelen
);
597 while ((*prev
) && (*prev
)->nhash
<= nhash
) {
598 if ((*prev
)->nhash
== nhash
&&
599 !memcmp((*prev
)->name
, name
, namelen
) &&
600 !(*prev
)->name
[namelen
]) {
601 struct jffs2_full_dirent
*this = *prev
;
603 D1(printk(KERN_DEBUG
"Marking old dirent node (ino #%u) @%08x obsolete\n",
604 this->ino
, ref_offset(this->raw
)));
607 jffs2_mark_node_obsolete(c
, (this->raw
));
608 jffs2_free_full_dirent(this);
611 prev
= &((*prev
)->next
);
616 /* dead_f is NULL if this was a rename not a real unlink */
617 /* Also catch the !f->inocache case, where there was a dirent
618 pointing to an inode which didn't exist. */
619 if (dead_f
&& dead_f
->inocache
) {
623 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f
)->i_mode
)) {
624 while (dead_f
->dents
) {
625 /* There can be only deleted ones */
628 dead_f
->dents
= fd
->next
;
631 printk(KERN_WARNING
"Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
632 dead_f
->inocache
->ino
, fd
->name
, fd
->ino
);
634 D1(printk(KERN_DEBUG
"Removing deletion dirent for \"%s\" from dir ino #%u\n",
635 fd
->name
, dead_f
->inocache
->ino
));
637 jffs2_mark_node_obsolete(c
, fd
->raw
);
638 jffs2_free_full_dirent(fd
);
642 dead_f
->inocache
->nlink
--;
643 /* NB: Caller must set inode nlink if appropriate */
647 jffs2_complete_reservation(c
);
653 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
)
655 struct jffs2_raw_dirent
*rd
;
656 struct jffs2_full_dirent
*fd
;
657 uint32_t alloclen
, phys_ofs
;
660 rd
= jffs2_alloc_raw_dirent();
664 ret
= jffs2_reserve_space(c
, sizeof(*rd
)+namelen
, &phys_ofs
, &alloclen
,
665 ALLOC_NORMAL
, JFFS2_SUMMARY_DIRENT_SIZE(namelen
));
667 jffs2_free_raw_dirent(rd
);
673 /* Build a deletion node */
674 rd
->magic
= cpu_to_je16(JFFS2_MAGIC_BITMASK
);
675 rd
->nodetype
= cpu_to_je16(JFFS2_NODETYPE_DIRENT
);
676 rd
->totlen
= cpu_to_je32(sizeof(*rd
) + namelen
);
677 rd
->hdr_crc
= cpu_to_je32(crc32(0, rd
, sizeof(struct jffs2_unknown_node
)-4));
679 rd
->pino
= cpu_to_je32(dir_f
->inocache
->ino
);
680 rd
->version
= cpu_to_je32(++dir_f
->highest_version
);
681 rd
->ino
= cpu_to_je32(ino
);
682 rd
->mctime
= cpu_to_je32(time
);
687 rd
->node_crc
= cpu_to_je32(crc32(0, rd
, sizeof(*rd
)-8));
688 rd
->name_crc
= cpu_to_je32(crc32(0, name
, namelen
));
690 fd
= jffs2_write_dirent(c
, dir_f
, rd
, name
, namelen
, phys_ofs
, ALLOC_NORMAL
);
692 jffs2_free_raw_dirent(rd
);
695 jffs2_complete_reservation(c
);
700 /* File it. This will mark the old one obsolete. */
701 jffs2_add_fd_to_list(c
, fd
, &dir_f
->dents
);
703 jffs2_complete_reservation(c
);