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.91 2005/03/01 10:34:03 dedekind 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->ino = ++c->highest_ino;
39 f->inocache->state = INO_STATE_PRESENT;
41 ri->ino = cpu_to_je32(f->inocache->ino);
43 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
44 jffs2_add_ino_cache(c, f->inocache);
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);
52 f->highest_version = 1;
53 ri->version = cpu_to_je32(f->highest_version);
58 #if CONFIG_JFFS2_FS_DEBUG > 0
59 static void writecheck(struct jffs2_sb_info *c, uint32_t ofs)
61 unsigned char buf[16];
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));
71 for (i=0; i<16; i++) {
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",
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]);
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 */
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)
92 struct jffs2_raw_node_ref *raw;
93 struct jffs2_full_dnode *fn;
98 unsigned long cnt = 2;
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");
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;
110 D1(writecheck(c, flash_ofs));
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);
115 raw = jffs2_alloc_raw_node_ref();
117 return ERR_PTR(-ENOMEM);
119 fn = jffs2_alloc_full_dnode();
121 jffs2_free_raw_node_ref(raw);
122 return ERR_PTR(-ENOMEM);
125 fn->ofs = je32_to_cpu(ri->offset);
126 fn->size = je32_to_cpu(ri->dsize);
129 /* check number of valid vecs */
130 if (!datalen || !data)
135 raw->flash_offset = flash_ofs;
136 raw->__totlen = PAD(sizeof(*ri)+datalen);
137 raw->next_phys = NULL;
139 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
141 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
142 "highest version %d -> updating dnode\n",
143 je32_to_cpu(ri->version), f->highest_version));
144 ri->version = cpu_to_je32(++f->highest_version);
145 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
148 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
149 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
151 if (ret || (retlen != sizeof(*ri) + datalen)) {
152 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
153 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
155 /* Mark the space as dirtied */
157 /* Doesn't belong to any inode */
158 raw->next_in_ino = NULL;
160 /* Don't change raw->size to match retlen. We may have
161 written the node header already, and only the data will
162 seem corrupted, in which case the scan would skip over
163 any node we write before the original intended end of
165 raw->flash_offset |= REF_OBSOLETE;
166 jffs2_add_physical_node_ref(c, raw);
167 jffs2_mark_node_obsolete(c, raw);
169 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
170 jffs2_free_raw_node_ref(raw);
172 if (!retried && alloc_mode != ALLOC_NORETRY && (raw = jffs2_alloc_raw_node_ref())) {
173 /* Try to reallocate space and retry */
175 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
179 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
181 ACCT_SANITY_CHECK(c,jeb);
182 D1(ACCT_PARANOIA_CHECK(jeb));
184 if (alloc_mode == ALLOC_GC) {
185 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs, &dummy);
189 jffs2_complete_reservation(c);
191 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs, &dummy, alloc_mode);
196 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
198 ACCT_SANITY_CHECK(c,jeb);
199 D1(ACCT_PARANOIA_CHECK(jeb));
203 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
204 jffs2_free_raw_node_ref(raw);
206 /* Release the full_dnode which is now useless, and return */
207 jffs2_free_full_dnode(fn);
208 return ERR_PTR(ret?ret:-EIO);
210 /* Mark the space used */
211 /* If node covers at least a whole page, or if it starts at the
212 beginning of a page and runs to the end of the file, or if
213 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
215 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
216 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
217 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
218 raw->flash_offset |= REF_PRISTINE;
220 raw->flash_offset |= REF_NORMAL;
222 jffs2_add_physical_node_ref(c, raw);
224 /* Link into per-inode list */
225 spin_lock(&c->erase_completion_lock);
226 raw->next_in_ino = f->inocache->nodes;
227 f->inocache->nodes = raw;
228 spin_unlock(&c->erase_completion_lock);
230 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",
231 flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
232 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
233 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
236 ACCT_SANITY_CHECK(c,NULL);
242 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)
244 struct jffs2_raw_node_ref *raw;
245 struct jffs2_full_dirent *fd;
251 D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
252 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
253 je32_to_cpu(rd->name_crc)));
254 D1(writecheck(c, flash_ofs));
256 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
257 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
262 vecs[0].iov_base = rd;
263 vecs[0].iov_len = sizeof(*rd);
264 vecs[1].iov_base = (unsigned char *)name;
265 vecs[1].iov_len = namelen;
267 raw = jffs2_alloc_raw_node_ref();
270 return ERR_PTR(-ENOMEM);
272 fd = jffs2_alloc_full_dirent(namelen+1);
274 jffs2_free_raw_node_ref(raw);
275 return ERR_PTR(-ENOMEM);
278 fd->version = je32_to_cpu(rd->version);
279 fd->ino = je32_to_cpu(rd->ino);
280 fd->nhash = full_name_hash(name, strlen(name));
282 memcpy(fd->name, name, namelen);
288 raw->flash_offset = flash_ofs;
289 raw->__totlen = PAD(sizeof(*rd)+namelen);
290 raw->next_phys = NULL;
292 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
294 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
295 "highest version %d -> updating dirent\n",
296 je32_to_cpu(rd->version), f->highest_version));
297 rd->version = cpu_to_je32(++f->highest_version);
298 fd->version = je32_to_cpu(rd->version);
299 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
302 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
303 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
304 if (ret || (retlen != sizeof(*rd) + namelen)) {
305 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
306 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
307 /* Mark the space as dirtied */
309 raw->next_in_ino = NULL;
310 raw->flash_offset |= REF_OBSOLETE;
311 jffs2_add_physical_node_ref(c, raw);
312 jffs2_mark_node_obsolete(c, raw);
314 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
315 jffs2_free_raw_node_ref(raw);
317 if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
318 /* Try to reallocate space and retry */
320 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
324 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
326 ACCT_SANITY_CHECK(c,jeb);
327 D1(ACCT_PARANOIA_CHECK(jeb));
329 if (alloc_mode == ALLOC_GC) {
330 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs, &dummy);
334 jffs2_complete_reservation(c);
336 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs, &dummy, alloc_mode);
341 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
342 ACCT_SANITY_CHECK(c,jeb);
343 D1(ACCT_PARANOIA_CHECK(jeb));
346 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
347 jffs2_free_raw_node_ref(raw);
349 /* Release the full_dnode which is now useless, and return */
350 jffs2_free_full_dirent(fd);
351 return ERR_PTR(ret?ret:-EIO);
353 /* Mark the space used */
354 raw->flash_offset |= REF_PRISTINE;
355 jffs2_add_physical_node_ref(c, raw);
357 spin_lock(&c->erase_completion_lock);
358 raw->next_in_ino = f->inocache->nodes;
359 f->inocache->nodes = raw;
360 spin_unlock(&c->erase_completion_lock);
363 ACCT_SANITY_CHECK(c,NULL);
369 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
370 we don't have to go digging in struct inode or its equivalent. It should set:
371 mode, uid, gid, (starting)isize, atime, ctime, mtime */
372 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
373 struct jffs2_raw_inode *ri, unsigned char *buf,
374 uint32_t offset, uint32_t writelen, uint32_t *retlen)
377 uint32_t writtenlen = 0;
379 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
380 f->inocache->ino, offset, writelen));
383 struct jffs2_full_dnode *fn;
384 unsigned char *comprbuf = NULL;
385 uint16_t comprtype = JFFS2_COMPR_NONE;
386 uint32_t phys_ofs, alloclen;
387 uint32_t datalen, cdatalen;
391 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
393 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL);
395 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
399 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
400 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
402 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
404 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
405 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
406 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
407 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
409 ri->ino = cpu_to_je32(f->inocache->ino);
410 ri->version = cpu_to_je32(++f->highest_version);
411 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
412 ri->offset = cpu_to_je32(offset);
413 ri->csize = cpu_to_je32(cdatalen);
414 ri->dsize = cpu_to_je32(datalen);
415 ri->compr = comprtype & 0xff;
416 ri->usercompr = (comprtype >> 8 ) & 0xff;
417 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
418 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
420 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
422 jffs2_free_comprbuf(comprbuf, buf);
427 jffs2_complete_reservation(c);
429 /* Write error to be retried */
431 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
436 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
438 jffs2_mark_node_obsolete(c, f->metadata->raw);
439 jffs2_free_full_dnode(f->metadata);
444 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
445 jffs2_mark_node_obsolete(c, fn->raw);
446 jffs2_free_full_dnode(fn);
449 jffs2_complete_reservation(c);
453 jffs2_complete_reservation(c);
455 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
459 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
460 writtenlen += datalen;
465 *retlen = writtenlen;
469 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)
471 struct jffs2_raw_dirent *rd;
472 struct jffs2_full_dnode *fn;
473 struct jffs2_full_dirent *fd;
474 uint32_t alloclen, phys_ofs;
477 /* Try to reserve enough space for both node and dirent.
478 * Just the node will do for now, though
480 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
481 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
487 ri->data_crc = cpu_to_je32(0);
488 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
490 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
492 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
493 jemode_to_cpu(ri->mode)));
496 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
497 /* Eeek. Wave bye bye */
499 jffs2_complete_reservation(c);
502 /* No data here. Only a metadata node, which will be
503 obsoleted by the first data write
508 jffs2_complete_reservation(c);
509 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
513 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
517 rd = jffs2_alloc_raw_dirent();
519 /* Argh. Now we treat it like a normal delete */
520 jffs2_complete_reservation(c);
526 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
527 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
528 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
529 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
531 rd->pino = cpu_to_je32(dir_f->inocache->ino);
532 rd->version = cpu_to_je32(++dir_f->highest_version);
534 rd->mctime = ri->ctime;
537 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
538 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
540 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
542 jffs2_free_raw_dirent(rd);
545 /* dirent failed to write. Delete the inode normally
546 as if it were the final unlink() */
547 jffs2_complete_reservation(c);
552 /* Link the fd into the inode's list, obsoleting an old
554 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
556 jffs2_complete_reservation(c);
563 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
564 const char *name, int namelen, struct jffs2_inode_info *dead_f)
566 struct jffs2_raw_dirent *rd;
567 struct jffs2_full_dirent *fd;
568 uint32_t alloclen, phys_ofs;
571 if (1 /* alternative branch needs testing */ ||
572 !jffs2_can_mark_obsolete(c)) {
573 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
575 rd = jffs2_alloc_raw_dirent();
579 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_DELETION);
581 jffs2_free_raw_dirent(rd);
587 /* Build a deletion node */
588 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
589 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
590 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
591 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
593 rd->pino = cpu_to_je32(dir_f->inocache->ino);
594 rd->version = cpu_to_je32(++dir_f->highest_version);
595 rd->ino = cpu_to_je32(0);
596 rd->mctime = cpu_to_je32(get_seconds());
598 rd->type = DT_UNKNOWN;
599 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
600 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
602 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION);
604 jffs2_free_raw_dirent(rd);
607 jffs2_complete_reservation(c);
612 /* File it. This will mark the old one obsolete. */
613 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
616 struct jffs2_full_dirent **prev = &dir_f->dents;
617 uint32_t nhash = full_name_hash(name, namelen);
621 while ((*prev) && (*prev)->nhash <= nhash) {
622 if ((*prev)->nhash == nhash &&
623 !memcmp((*prev)->name, name, namelen) &&
624 !(*prev)->name[namelen]) {
625 struct jffs2_full_dirent *this = *prev;
627 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
628 this->ino, ref_offset(this->raw)));
631 jffs2_mark_node_obsolete(c, (this->raw));
632 jffs2_free_full_dirent(this);
635 prev = &((*prev)->next);
640 /* dead_f is NULL if this was a rename not a real unlink */
641 /* Also catch the !f->inocache case, where there was a dirent
642 pointing to an inode which didn't exist. */
643 if (dead_f && dead_f->inocache) {
647 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
648 while (dead_f->dents) {
649 /* There can be only deleted ones */
652 dead_f->dents = fd->next;
655 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
656 dead_f->inocache->ino, fd->name, fd->ino);
658 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
659 fd->name, dead_f->inocache->ino));
661 jffs2_mark_node_obsolete(c, fd->raw);
662 jffs2_free_full_dirent(fd);
666 dead_f->inocache->nlink--;
667 /* NB: Caller must set inode nlink if appropriate */
671 jffs2_complete_reservation(c);
677 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)
679 struct jffs2_raw_dirent *rd;
680 struct jffs2_full_dirent *fd;
681 uint32_t alloclen, phys_ofs;
684 rd = jffs2_alloc_raw_dirent();
688 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
690 jffs2_free_raw_dirent(rd);
696 /* Build a deletion node */
697 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
698 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
699 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
700 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
702 rd->pino = cpu_to_je32(dir_f->inocache->ino);
703 rd->version = cpu_to_je32(++dir_f->highest_version);
704 rd->ino = cpu_to_je32(ino);
705 rd->mctime = cpu_to_je32(get_seconds());
710 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
711 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
713 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
715 jffs2_free_raw_dirent(rd);
718 jffs2_complete_reservation(c);
723 /* File it. This will mark the old one obsolete. */
724 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
726 jffs2_complete_reservation(c);