[JFFS2] Add length argument to jffs2_add_physical_node_ref()
[linux-2.6/libata-dev.git] / fs / jffs2 / write.c
blob4462541d11f8820b1d54a294316a82dc26e9b8e8
1 /*
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>
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"
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();
29 if (!ic) {
30 return -ENOMEM;
33 memset(ic, 0, sizeof(*ic));
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;
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);
53 return 0;
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;
64 size_t retlen;
65 struct kvec vecs[2];
66 int ret;
67 int retried = 0;
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");
72 BUG();
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();
86 if (!raw)
87 return ERR_PTR(-ENOMEM);
89 fn = jffs2_alloc_full_dnode();
90 if (!fn) {
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);
97 fn->frags = 0;
99 /* check number of valid vecs */
100 if (!datalen || !data)
101 cnt = 1;
102 retry:
103 fn->raw = raw;
105 raw->flash_offset = flash_ofs;
107 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
108 BUG_ON(!retried);
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 */
124 if (retlen) {
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
132 this node */
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);
136 } else {
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 */
142 uint32_t dummy;
143 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
145 retried = 1;
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);
155 } else {
156 /* Locking pain */
157 up(&f->sem);
158 jffs2_complete_reservation(c);
160 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs,
161 &dummy, alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
162 down(&f->sem);
165 if (!ret) {
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);
171 goto retry;
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;
189 } else {
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)));
205 if (retried) {
206 jffs2_dbg_acct_sanity_check(c,NULL);
209 return fn;
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;
216 size_t retlen;
217 struct kvec vecs[2];
218 int retried = 0;
219 int ret;
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");
227 BUG();
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();
240 if (!raw)
241 return ERR_PTR(-ENOMEM);
243 fd = jffs2_alloc_full_dirent(namelen+1);
244 if (!fd) {
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));
252 fd->type = rd->type;
253 memcpy(fd->name, name, namelen);
254 fd->name[namelen]=0;
256 retry:
257 fd->raw = raw;
259 raw->flash_offset = flash_ofs;
261 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
262 BUG_ON(!retried);
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 */
277 if (retlen) {
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);
282 } else {
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 */
288 uint32_t dummy;
289 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
291 retried = 1;
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));
301 } else {
302 /* Locking pain */
303 up(&f->sem);
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));
308 down(&f->sem);
311 if (!ret) {
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);
315 goto retry;
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);
333 if (retried) {
334 jffs2_dbg_acct_sanity_check(c,NULL);
337 return fd;
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)
347 int ret = 0;
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));
353 while(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;
359 int retried = 0;
361 retry:
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);
366 if (ret) {
367 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
368 break;
370 down(&f->sem);
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);
396 if (IS_ERR(fn)) {
397 ret = PTR_ERR(fn);
398 up(&f->sem);
399 jffs2_complete_reservation(c);
400 if (!retried) {
401 /* Write error to be retried */
402 retried = 1;
403 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
404 goto retry;
406 break;
408 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
409 if (f->metadata) {
410 jffs2_mark_node_obsolete(c, f->metadata->raw);
411 jffs2_free_full_dnode(f->metadata);
412 f->metadata = NULL;
414 if (ret) {
415 /* Eep */
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);
420 up(&f->sem);
421 jffs2_complete_reservation(c);
422 break;
424 up(&f->sem);
425 jffs2_complete_reservation(c);
426 if (!datalen) {
427 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
428 ret = -EIO;
429 break;
431 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
432 writtenlen += datalen;
433 offset += datalen;
434 writelen -= datalen;
435 buf += datalen;
437 *retlen = writtenlen;
438 return ret;
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;
447 int ret;
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));
455 if (ret) {
456 up(&f->sem);
457 return ret;
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)));
468 if (IS_ERR(fn)) {
469 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
470 /* Eeek. Wave bye bye */
471 up(&f->sem);
472 jffs2_complete_reservation(c);
473 return PTR_ERR(fn);
475 /* No data here. Only a metadata node, which will be
476 obsoleted by the first data write
478 f->metadata = fn;
480 up(&f->sem);
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));
485 if (ret) {
486 /* Eep. */
487 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
488 return ret;
491 rd = jffs2_alloc_raw_dirent();
492 if (!rd) {
493 /* Argh. Now we treat it like a normal delete */
494 jffs2_complete_reservation(c);
495 return -ENOMEM;
498 down(&dir_f->sem);
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);
507 rd->ino = ri->ino;
508 rd->mctime = ri->ctime;
509 rd->nsize = namelen;
510 rd->type = DT_REG;
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);
518 if (IS_ERR(fd)) {
519 /* dirent failed to write. Delete the inode normally
520 as if it were the final unlink() */
521 jffs2_complete_reservation(c);
522 up(&dir_f->sem);
523 return PTR_ERR(fd);
526 /* Link the fd into the inode's list, obsoleting an old
527 one if necessary. */
528 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
530 jffs2_complete_reservation(c);
531 up(&dir_f->sem);
533 return 0;
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,
539 uint32_t time)
541 struct jffs2_raw_dirent *rd;
542 struct jffs2_full_dirent *fd;
543 uint32_t alloclen, phys_ofs;
544 int ret;
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();
551 if (!rd)
552 return -ENOMEM;
554 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
555 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
556 if (ret) {
557 jffs2_free_raw_dirent(rd);
558 return ret;
561 down(&dir_f->sem);
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);
573 rd->nsize = namelen;
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);
582 if (IS_ERR(fd)) {
583 jffs2_complete_reservation(c);
584 up(&dir_f->sem);
585 return PTR_ERR(fd);
588 /* File it. This will mark the old one obsolete. */
589 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
590 up(&dir_f->sem);
591 } else {
592 struct jffs2_full_dirent **prev = &dir_f->dents;
593 uint32_t nhash = full_name_hash(name, namelen);
595 down(&dir_f->sem);
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)));
606 *prev = this->next;
607 jffs2_mark_node_obsolete(c, (this->raw));
608 jffs2_free_full_dirent(this);
609 break;
611 prev = &((*prev)->next);
613 up(&dir_f->sem);
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) {
621 down(&dead_f->sem);
623 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
624 while (dead_f->dents) {
625 /* There can be only deleted ones */
626 fd = dead_f->dents;
628 dead_f->dents = fd->next;
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",
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 */
644 up(&dead_f->sem);
647 jffs2_complete_reservation(c);
649 return 0;
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;
658 int ret;
660 rd = jffs2_alloc_raw_dirent();
661 if (!rd)
662 return -ENOMEM;
664 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
665 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
666 if (ret) {
667 jffs2_free_raw_dirent(rd);
668 return ret;
671 down(&dir_f->sem);
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);
683 rd->nsize = namelen;
685 rd->type = type;
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);
694 if (IS_ERR(fd)) {
695 jffs2_complete_reservation(c);
696 up(&dir_f->sem);
697 return PTR_ERR(fd);
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);
704 up(&dir_f->sem);
706 return 0;