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,
60 struct jffs2_raw_inode *ri, const unsigned char *data,
61 uint32_t datalen, int alloc_mode)
64 struct jffs2_full_dnode *fn;
70 unsigned long cnt = 2;
72 D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
73 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
77 vecs[0].iov_base = ri;
78 vecs[0].iov_len = sizeof(*ri);
79 vecs[1].iov_base = (unsigned char *)data;
80 vecs[1].iov_len = datalen;
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);
86 fn = jffs2_alloc_full_dnode();
88 return ERR_PTR(-ENOMEM);
90 /* check number of valid vecs */
91 if (!datalen || !data)
94 flash_ofs = write_ofs(c);
96 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
98 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
100 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
101 "highest version %d -> updating dnode\n",
102 je32_to_cpu(ri->version), f->highest_version));
103 ri->version = cpu_to_je32(++f->highest_version);
104 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
107 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
108 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
110 if (ret || (retlen != sizeof(*ri) + datalen)) {
111 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
112 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
114 /* Mark the space as dirtied */
116 /* Don't change raw->size to match retlen. We may have
117 written the node header already, and only the data will
118 seem corrupted, in which case the scan would skip over
119 any node we write before the original intended end of
121 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
123 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
125 if (!retried && alloc_mode != ALLOC_NORETRY) {
126 /* Try to reallocate space and retry */
128 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
132 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
134 jffs2_dbg_acct_sanity_check(c,jeb);
135 jffs2_dbg_acct_paranoia_check(c, jeb);
137 if (alloc_mode == ALLOC_GC) {
138 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
139 JFFS2_SUMMARY_INODE_SIZE);
143 jffs2_complete_reservation(c);
145 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
146 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
151 flash_ofs = write_ofs(c);
152 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
154 jffs2_dbg_acct_sanity_check(c,jeb);
155 jffs2_dbg_acct_paranoia_check(c, jeb);
159 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
161 /* Release the full_dnode which is now useless, and return */
162 jffs2_free_full_dnode(fn);
163 return ERR_PTR(ret?ret:-EIO);
165 /* Mark the space used */
166 /* If node covers at least a whole page, or if it starts at the
167 beginning of a page and runs to the end of the file, or if
168 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
170 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
171 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
172 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
173 flash_ofs |= REF_PRISTINE;
175 flash_ofs |= REF_NORMAL;
177 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
178 fn->ofs = je32_to_cpu(ri->offset);
179 fn->size = je32_to_cpu(ri->dsize);
182 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",
183 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
184 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
185 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
188 jffs2_dbg_acct_sanity_check(c,NULL);
194 struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
195 struct jffs2_raw_dirent *rd, const unsigned char *name,
196 uint32_t namelen, int alloc_mode)
198 struct jffs2_full_dirent *fd;
205 D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
206 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
207 je32_to_cpu(rd->name_crc)));
209 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
210 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
214 vecs[0].iov_base = rd;
215 vecs[0].iov_len = sizeof(*rd);
216 vecs[1].iov_base = (unsigned char *)name;
217 vecs[1].iov_len = namelen;
219 fd = jffs2_alloc_full_dirent(namelen+1);
221 return ERR_PTR(-ENOMEM);
223 fd->version = je32_to_cpu(rd->version);
224 fd->ino = je32_to_cpu(rd->ino);
225 fd->nhash = full_name_hash(name, strlen(name));
227 memcpy(fd->name, name, namelen);
231 flash_ofs = write_ofs(c);
233 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
235 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
237 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
238 "highest version %d -> updating dirent\n",
239 je32_to_cpu(rd->version), f->highest_version));
240 rd->version = cpu_to_je32(++f->highest_version);
241 fd->version = je32_to_cpu(rd->version);
242 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
245 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
246 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
247 if (ret || (retlen != sizeof(*rd) + namelen)) {
248 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
249 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
250 /* Mark the space as dirtied */
252 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
254 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
257 /* Try to reallocate space and retry */
259 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
263 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
265 jffs2_dbg_acct_sanity_check(c,jeb);
266 jffs2_dbg_acct_paranoia_check(c, jeb);
268 if (alloc_mode == ALLOC_GC) {
269 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
270 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
274 jffs2_complete_reservation(c);
276 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
277 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
282 flash_ofs = write_ofs(c);
283 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
284 jffs2_dbg_acct_sanity_check(c,jeb);
285 jffs2_dbg_acct_paranoia_check(c, jeb);
288 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
290 /* Release the full_dnode which is now useless, and return */
291 jffs2_free_full_dirent(fd);
292 return ERR_PTR(ret?ret:-EIO);
294 /* Mark the space used */
295 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | REF_PRISTINE, PAD(sizeof(*rd)+namelen), f->inocache);
298 jffs2_dbg_acct_sanity_check(c,NULL);
304 /* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
305 we don't have to go digging in struct inode or its equivalent. It should set:
306 mode, uid, gid, (starting)isize, atime, ctime, mtime */
307 int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
308 struct jffs2_raw_inode *ri, unsigned char *buf,
309 uint32_t offset, uint32_t writelen, uint32_t *retlen)
312 uint32_t writtenlen = 0;
314 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
315 f->inocache->ino, offset, writelen));
318 struct jffs2_full_dnode *fn;
319 unsigned char *comprbuf = NULL;
320 uint16_t comprtype = JFFS2_COMPR_NONE;
322 uint32_t datalen, cdatalen;
326 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
328 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
329 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
331 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
335 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
336 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
338 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
340 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
341 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
342 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
343 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
345 ri->ino = cpu_to_je32(f->inocache->ino);
346 ri->version = cpu_to_je32(++f->highest_version);
347 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
348 ri->offset = cpu_to_je32(offset);
349 ri->csize = cpu_to_je32(cdatalen);
350 ri->dsize = cpu_to_je32(datalen);
351 ri->compr = comprtype & 0xff;
352 ri->usercompr = (comprtype >> 8 ) & 0xff;
353 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
354 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
356 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
358 jffs2_free_comprbuf(comprbuf, buf);
363 jffs2_complete_reservation(c);
365 /* Write error to be retried */
367 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
372 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
374 jffs2_mark_node_obsolete(c, f->metadata->raw);
375 jffs2_free_full_dnode(f->metadata);
380 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
381 jffs2_mark_node_obsolete(c, fn->raw);
382 jffs2_free_full_dnode(fn);
385 jffs2_complete_reservation(c);
389 jffs2_complete_reservation(c);
391 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
395 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
396 writtenlen += datalen;
401 *retlen = writtenlen;
405 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)
407 struct jffs2_raw_dirent *rd;
408 struct jffs2_full_dnode *fn;
409 struct jffs2_full_dirent *fd;
413 /* Try to reserve enough space for both node and dirent.
414 * Just the node will do for now, though
416 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
417 JFFS2_SUMMARY_INODE_SIZE);
418 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
424 ri->data_crc = cpu_to_je32(0);
425 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
427 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
429 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
430 jemode_to_cpu(ri->mode)));
433 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
434 /* Eeek. Wave bye bye */
436 jffs2_complete_reservation(c);
439 /* No data here. Only a metadata node, which will be
440 obsoleted by the first data write
445 jffs2_complete_reservation(c);
446 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
447 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
451 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
455 rd = jffs2_alloc_raw_dirent();
457 /* Argh. Now we treat it like a normal delete */
458 jffs2_complete_reservation(c);
464 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
465 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
466 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
467 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
469 rd->pino = cpu_to_je32(dir_f->inocache->ino);
470 rd->version = cpu_to_je32(++dir_f->highest_version);
472 rd->mctime = ri->ctime;
475 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
476 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
478 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
480 jffs2_free_raw_dirent(rd);
483 /* dirent failed to write. Delete the inode normally
484 as if it were the final unlink() */
485 jffs2_complete_reservation(c);
490 /* Link the fd into the inode's list, obsoleting an old
492 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
494 jffs2_complete_reservation(c);
501 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
502 const char *name, int namelen, struct jffs2_inode_info *dead_f,
505 struct jffs2_raw_dirent *rd;
506 struct jffs2_full_dirent *fd;
510 if (1 /* alternative branch needs testing */ ||
511 !jffs2_can_mark_obsolete(c)) {
512 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
514 rd = jffs2_alloc_raw_dirent();
518 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
519 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
521 jffs2_free_raw_dirent(rd);
527 /* Build a deletion node */
528 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
529 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
530 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
531 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
533 rd->pino = cpu_to_je32(dir_f->inocache->ino);
534 rd->version = cpu_to_je32(++dir_f->highest_version);
535 rd->ino = cpu_to_je32(0);
536 rd->mctime = cpu_to_je32(time);
538 rd->type = DT_UNKNOWN;
539 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
540 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
542 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
544 jffs2_free_raw_dirent(rd);
547 jffs2_complete_reservation(c);
552 /* File it. This will mark the old one obsolete. */
553 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
556 struct jffs2_full_dirent **prev = &dir_f->dents;
557 uint32_t nhash = full_name_hash(name, namelen);
561 while ((*prev) && (*prev)->nhash <= nhash) {
562 if ((*prev)->nhash == nhash &&
563 !memcmp((*prev)->name, name, namelen) &&
564 !(*prev)->name[namelen]) {
565 struct jffs2_full_dirent *this = *prev;
567 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
568 this->ino, ref_offset(this->raw)));
571 jffs2_mark_node_obsolete(c, (this->raw));
572 jffs2_free_full_dirent(this);
575 prev = &((*prev)->next);
580 /* dead_f is NULL if this was a rename not a real unlink */
581 /* Also catch the !f->inocache case, where there was a dirent
582 pointing to an inode which didn't exist. */
583 if (dead_f && dead_f->inocache) {
587 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
588 while (dead_f->dents) {
589 /* There can be only deleted ones */
592 dead_f->dents = fd->next;
595 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
596 dead_f->inocache->ino, fd->name, fd->ino);
598 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
599 fd->name, dead_f->inocache->ino));
601 jffs2_mark_node_obsolete(c, fd->raw);
602 jffs2_free_full_dirent(fd);
606 dead_f->inocache->nlink--;
607 /* NB: Caller must set inode nlink if appropriate */
611 jffs2_complete_reservation(c);
617 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)
619 struct jffs2_raw_dirent *rd;
620 struct jffs2_full_dirent *fd;
624 rd = jffs2_alloc_raw_dirent();
628 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
629 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
631 jffs2_free_raw_dirent(rd);
637 /* Build a deletion node */
638 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
639 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
640 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
641 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
643 rd->pino = cpu_to_je32(dir_f->inocache->ino);
644 rd->version = cpu_to_je32(++dir_f->highest_version);
645 rd->ino = cpu_to_je32(ino);
646 rd->mctime = cpu_to_je32(time);
651 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
652 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
654 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
656 jffs2_free_raw_dirent(rd);
659 jffs2_complete_reservation(c);
664 /* File it. This will mark the old one obsolete. */
665 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
667 jffs2_complete_reservation(c);