2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: write.c,v 1.75 2003/10/08 11:45:11 dwmw2 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>
23 int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri)
25 struct jffs2_inode_cache *ic;
27 ic = jffs2_alloc_inode_cache();
32 memset(ic, 0, sizeof(*ic));
34 init_MUTEX_LOCKED(&f->sem);
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 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
140 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
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);
146 /* Mark the space as dirtied */
148 /* Doesn't belong to any inode */
149 raw->next_in_ino = NULL;
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
156 raw->flash_offset |= REF_OBSOLETE;
157 jffs2_add_physical_node_ref(c, raw);
158 jffs2_mark_node_obsolete(c, raw);
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);
163 if (!retried && alloc_mode != ALLOC_NORETRY && (raw = jffs2_alloc_raw_node_ref())) {
164 /* Try to reallocate space and retry */
166 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
170 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
172 ACCT_SANITY_CHECK(c,jeb);
173 D1(ACCT_PARANOIA_CHECK(jeb));
175 if (alloc_mode == ALLOC_GC) {
176 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs, &dummy);
180 jffs2_complete_reservation(c);
182 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs, &dummy, alloc_mode);
187 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
189 ACCT_SANITY_CHECK(c,jeb);
190 D1(ACCT_PARANOIA_CHECK(jeb));
194 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
195 jffs2_free_raw_node_ref(raw);
197 /* Release the full_dnode which is now useless, and return */
198 jffs2_free_full_dnode(fn);
199 return ERR_PTR(ret?ret:-EIO);
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.
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;
211 raw->flash_offset |= REF_NORMAL;
213 jffs2_add_physical_node_ref(c, raw);
215 /* Link into per-inode list */
216 raw->next_in_ino = f->inocache->nodes;
217 f->inocache->nodes = raw;
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)));
225 ACCT_SANITY_CHECK(c,NULL);
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)
233 struct jffs2_raw_node_ref *raw;
234 struct jffs2_full_dirent *fd;
236 struct iovec vecs[2];
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));
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");
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;
256 raw = jffs2_alloc_raw_node_ref();
259 return ERR_PTR(-ENOMEM);
261 fd = jffs2_alloc_full_dirent(namelen+1);
263 jffs2_free_raw_node_ref(raw);
264 return ERR_PTR(-ENOMEM);
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));
271 memcpy(fd->name, name, namelen);
277 raw->flash_offset = flash_ofs;
278 raw->totlen = PAD(sizeof(*rd)+namelen);
279 raw->next_phys = NULL;
281 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
282 (alloc_mode==ALLOC_GC)?0:fd->ino);
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 */
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);
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);
296 if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
297 /* Try to reallocate space and retry */
299 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
303 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
305 ACCT_SANITY_CHECK(c,jeb);
306 D1(ACCT_PARANOIA_CHECK(jeb));
308 if (alloc_mode == ALLOC_GC) {
309 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs, &dummy);
313 jffs2_complete_reservation(c);
315 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs, &dummy, alloc_mode);
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));
325 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
326 jffs2_free_raw_node_ref(raw);
328 /* Release the full_dnode which is now useless, and return */
329 jffs2_free_full_dirent(fd);
330 return ERR_PTR(ret?ret:-EIO);
332 /* Mark the space used */
333 raw->flash_offset |= REF_PRISTINE;
334 jffs2_add_physical_node_ref(c, raw);
336 raw->next_in_ino = f->inocache->nodes;
337 f->inocache->nodes = raw;
340 ACCT_SANITY_CHECK(c,NULL);
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)
354 uint32_t writtenlen = 0;
356 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
357 f->inocache->ino, offset, writelen));
360 struct jffs2_full_dnode *fn;
361 unsigned char *comprbuf = NULL;
362 unsigned char comprtype = JFFS2_COMPR_NONE;
363 uint32_t phys_ofs, alloclen;
364 uint32_t datalen, cdatalen;
368 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
370 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL);
372 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
377 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), writelen);
379 comprbuf = kmalloc(cdatalen, GFP_KERNEL);
381 comprtype = jffs2_compress(buf, comprbuf, &datalen, &cdatalen);
383 if (comprtype == JFFS2_COMPR_NONE) {
384 /* Either compression failed, or the allocation of comprbuf failed */
390 /* Now comprbuf points to the data to be written, be it compressed or not.
391 comprtype holds the compression type, and comprtype == JFFS2_COMPR_NONE means
392 that the comprbuf doesn't need to be kfree()d.
395 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
396 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
397 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
398 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
400 ri->ino = cpu_to_je32(f->inocache->ino);
401 ri->version = cpu_to_je32(++f->highest_version);
402 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
403 ri->offset = cpu_to_je32(offset);
404 ri->csize = cpu_to_je32(cdatalen);
405 ri->dsize = cpu_to_je32(datalen);
406 ri->compr = comprtype;
407 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
408 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
410 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
412 if (comprtype != JFFS2_COMPR_NONE)
418 jffs2_complete_reservation(c);
420 /* Write error to be retried */
422 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
427 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
429 jffs2_mark_node_obsolete(c, f->metadata->raw);
430 jffs2_free_full_dnode(f->metadata);
435 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
436 jffs2_mark_node_obsolete(c, fn->raw);
437 jffs2_free_full_dnode(fn);
440 jffs2_complete_reservation(c);
444 jffs2_complete_reservation(c);
446 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
450 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
451 writtenlen += datalen;
456 *retlen = writtenlen;
460 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)
462 struct jffs2_raw_dirent *rd;
463 struct jffs2_full_dnode *fn;
464 struct jffs2_full_dirent *fd;
465 uint32_t alloclen, phys_ofs;
468 /* Try to reserve enough space for both node and dirent.
469 * Just the node will do for now, though
471 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
472 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
478 ri->data_crc = cpu_to_je32(0);
479 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
481 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
483 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
484 jemode_to_cpu(ri->mode)));
487 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
488 /* Eeek. Wave bye bye */
490 jffs2_complete_reservation(c);
493 /* No data here. Only a metadata node, which will be
494 obsoleted by the first data write
499 jffs2_complete_reservation(c);
500 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
504 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
508 rd = jffs2_alloc_raw_dirent();
510 /* Argh. Now we treat it like a normal delete */
511 jffs2_complete_reservation(c);
517 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
518 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
519 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
520 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
522 rd->pino = cpu_to_je32(dir_f->inocache->ino);
523 rd->version = cpu_to_je32(++dir_f->highest_version);
525 rd->mctime = ri->ctime;
528 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
529 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
531 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
533 jffs2_free_raw_dirent(rd);
536 /* dirent failed to write. Delete the inode normally
537 as if it were the final unlink() */
538 jffs2_complete_reservation(c);
543 /* Link the fd into the inode's list, obsoleting an old
545 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
547 jffs2_complete_reservation(c);
554 int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
555 const char *name, int namelen, struct jffs2_inode_info *dead_f)
557 struct jffs2_raw_dirent *rd;
558 struct jffs2_full_dirent *fd;
559 uint32_t alloclen, phys_ofs;
562 rd = jffs2_alloc_raw_dirent();
566 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_DELETION);
568 jffs2_free_raw_dirent(rd);
574 /* Build a deletion node */
575 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
576 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
577 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
578 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
580 rd->pino = cpu_to_je32(dir_f->inocache->ino);
581 rd->version = cpu_to_je32(++dir_f->highest_version);
582 rd->ino = cpu_to_je32(0);
583 rd->mctime = cpu_to_je32(get_seconds());
585 rd->type = DT_UNKNOWN;
586 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
587 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
589 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION);
591 jffs2_free_raw_dirent(rd);
594 jffs2_complete_reservation(c);
599 /* File it. This will mark the old one obsolete. */
600 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
604 /* dead_f is NULL if this was a rename not a real unlink */
605 /* Also catch the !f->inocache case, where there was a dirent
606 pointing to an inode which didn't exist. */
607 if (dead_f && dead_f->inocache) {
611 while (dead_f->dents) {
612 /* There can be only deleted ones */
615 dead_f->dents = fd->next;
618 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
619 dead_f->inocache->ino, fd->name, fd->ino);
621 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n", fd->name, dead_f->inocache->ino));
623 jffs2_mark_node_obsolete(c, fd->raw);
624 jffs2_free_full_dirent(fd);
627 dead_f->inocache->nlink--;
628 /* NB: Caller must set inode nlink if appropriate */
632 jffs2_complete_reservation(c);
638 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)
640 struct jffs2_raw_dirent *rd;
641 struct jffs2_full_dirent *fd;
642 uint32_t alloclen, phys_ofs;
645 rd = jffs2_alloc_raw_dirent();
649 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
651 jffs2_free_raw_dirent(rd);
657 /* Build a deletion node */
658 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
659 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
660 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
661 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
663 rd->pino = cpu_to_je32(dir_f->inocache->ino);
664 rd->version = cpu_to_je32(++dir_f->highest_version);
665 rd->ino = cpu_to_je32(ino);
666 rd->mctime = cpu_to_je32(get_seconds());
671 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
672 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
674 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
676 jffs2_free_raw_dirent(rd);
679 jffs2_complete_reservation(c);
684 /* File it. This will mark the old one obsolete. */
685 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
687 jffs2_complete_reservation(c);