20
#include <linux/kernel.h>
22
#include <linux/crc32.h>
23
#include <linux/slab.h>
24
#include <linux/pagemap.h>
25
#include <linux/mtd/mtd.h>
30
int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
31
uint32_t mode, struct jffs2_raw_inode *ri) {
32
struct jffs2_inode_cache *ic;
34
ic = jffs2_alloc_inode_cache();
39
memset(ic, 0, sizeof(*ic));
42
f->inocache->nlink = 1;
43
f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
44
f->inocache->state = INO_STATE_PRESENT;
47
jffs2_add_ino_cache(c, f->inocache);
48
D1(printk( "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
49
ri->ino = cpu_to_je32(f->inocache->ino);
51
ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
52
ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
53
ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
54
ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
55
ri->mode = cpu_to_jemode(mode);
57
f->highest_version = 1;
58
ri->version = cpu_to_je32(f->highest_version);
67
struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c,
68
struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
69
const unsigned char *data,
70
uint32_t datalen, uint32_t flash_ofs, int alloc_mode) {
71
struct jffs2_raw_node_ref *raw;
72
struct jffs2_full_dnode *fn;
77
struct jffs2_eraseblock *jeb;
79
unsigned long cnt = 2;
82
if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
83
printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
87
vecs[0].iov_base = ri;
88
vecs[0].iov_len = sizeof(*ri);
89
vecs[1].iov_base = (unsigned char *)data;
90
vecs[1].iov_len = datalen;
92
jffs2_dbg_prewrite_paranoia_check(c, flash_ofs,
93
vecs[0].iov_len + vecs[1].iov_len);
95
if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
96
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);
98
raw = jffs2_alloc_raw_node_ref();
100
return ERR_PTR(-ENOMEM);
103
fn = jffs2_alloc_full_dnode();
105
jffs2_free_raw_node_ref(raw);
106
return ERR_PTR(-ENOMEM);
109
fn->ofs = je32_to_cpu(ri->offset);
110
fn->size = je32_to_cpu(ri->dsize);
114
if (!datalen || !data) {
120
raw->flash_offset = flash_ofs;
121
raw->__totlen = PAD(sizeof(*ri)+datalen);
122
raw->next_phys = NULL;
124
if ((alloc_mode!=ALLOC_GC) &&
125
(je32_to_cpu(ri->version) < f->highest_version)) {
127
D1(printk( "jffs2_write_dnode : dnode_version %d, "
128
"highest version %d -> updating dnode\n",
129
je32_to_cpu(ri->version), f->highest_version));
130
ri->version = cpu_to_je32(++f->highest_version);
131
ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
134
ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
135
(alloc_mode==ALLOC_GC)?0:f->inocache->ino);
137
if (ret || (retlen != sizeof(*ri) + datalen)) {
138
printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
139
sizeof(*ri)+datalen, flash_ofs, ret, retlen);
144
raw->next_in_ino = NULL;
152
raw->flash_offset |= REF_OBSOLETE;
153
jffs2_add_physical_node_ref(c, raw);
154
jffs2_mark_node_obsolete(c, raw);
156
printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
157
jffs2_free_raw_node_ref(raw);
159
if (!retried && alloc_mode != ALLOC_NORETRY &&
160
(raw = jffs2_alloc_raw_node_ref())) {
162
jeb = &c->blocks[flash_ofs / c->sector_size];
166
D1(printk( "Retrying failed write.\n"));
168
jffs2_dbg_acct_sanity_check(c,jeb);
169
jffs2_dbg_acct_paranoia_check(c, jeb);
171
if (alloc_mode == ALLOC_GC) {
172
ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs, &dummy);
176
jffs2_complete_reservation(c);
178
ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs, &dummy, alloc_mode);
183
D1(printk( "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
185
jffs2_dbg_acct_sanity_check(c,jeb);
186
jffs2_dbg_acct_paranoia_check(c, jeb);
190
D1(printk( "Failed to allocate space to retry failed write: %d!\n", ret));
191
jffs2_free_raw_node_ref(raw);
194
jffs2_free_full_dnode(fn);
195
return ERR_PTR(ret?ret:-EIO);
202
if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
203
( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
204
(je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
205
raw->flash_offset |= REF_PRISTINE;
207
raw->flash_offset |= REF_NORMAL;
209
jffs2_add_physical_node_ref(c, raw);
212
spin_lock(&c->erase_completion_lock);
213
raw->next_in_ino = f->inocache->nodes;
214
f->inocache->nodes = raw;
215
spin_unlock(&c->erase_completion_lock);
217
D1(printk( "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",
218
flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
219
je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
220
je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
223
jffs2_dbg_acct_sanity_check(c,NULL);
229
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)
231
struct jffs2_raw_node_ref *raw;
232
struct jffs2_full_dirent *fd;
234
struct iovec vecs[2];
238
D1(printk( "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
239
je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
240
je32_to_cpu(rd->name_crc)));
243
if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
244
printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
249
vecs[0].iov_base = rd;
250
vecs[0].iov_len = sizeof(*rd);
251
vecs[1].iov_base = (unsigned char *)name;
252
vecs[1].iov_len = namelen;
254
jffs2_dbg_prewrite_paranoia_check(c, flash_ofs,
255
vecs[0].iov_len + vecs[1].iov_len);
257
raw = jffs2_alloc_raw_node_ref();
260
return ERR_PTR(-ENOMEM);
263
fd = jffs2_alloc_full_dirent(namelen+1);
265
jffs2_free_raw_node_ref(raw);
266
return ERR_PTR(-ENOMEM);
269
fd->version = je32_to_cpu(rd->version);
270
fd->ino = je32_to_cpu(rd->ino);
271
fd->nhash = full_name_hash(name, strlen((const char *) name));
273
memcpy(fd->name, name, namelen);
279
raw->flash_offset = flash_ofs;
280
raw->__totlen = PAD(sizeof(*rd)+namelen);
281
raw->next_phys = NULL;
283
if ((alloc_mode!=ALLOC_GC) &&
284
(je32_to_cpu(rd->version) < f->highest_version)) {
286
D1(printk( "jffs2_write_dirent : dirent_version %d, "
287
"highest version %d -> updating dirent\n",
288
je32_to_cpu(rd->version), f->highest_version));
289
rd->version = cpu_to_je32(++f->highest_version);
290
fd->version = je32_to_cpu(rd->version);
291
rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
294
ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
295
(alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
296
if (ret || (retlen != sizeof(*rd) + namelen)) {
297
printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
298
sizeof(*rd)+namelen, flash_ofs, ret, retlen);
301
raw->next_in_ino = NULL;
302
raw->flash_offset |= REF_OBSOLETE;
303
jffs2_add_physical_node_ref(c, raw);
304
jffs2_mark_node_obsolete(c, raw);
306
printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
307
jffs2_free_raw_node_ref(raw);
309
if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
312
struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
316
D1(printk( "Retrying failed write.\n"));
318
jffs2_dbg_acct_sanity_check(c,jeb);
319
jffs2_dbg_acct_paranoia_check(c, jeb);
321
if (alloc_mode == ALLOC_GC) {
322
ret = jffs2_reserve_space_gc(c,
323
sizeof(*rd) + namelen, &flash_ofs, &dummy);
327
jffs2_complete_reservation(c);
329
ret = jffs2_reserve_space(c,
330
sizeof(*rd) + namelen, &flash_ofs, &dummy, alloc_mode);
335
D1(printk( "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
336
jffs2_dbg_acct_sanity_check(c,jeb);
337
jffs2_dbg_acct_paranoia_check(c, jeb);
340
D1(printk( "Failed to allocate space to retry failed write: %d!\n", ret));
341
jffs2_free_raw_node_ref(raw);
344
jffs2_free_full_dirent(fd);
345
return ERR_PTR(ret?ret:-EIO);
348
raw->flash_offset |= REF_PRISTINE;
349
jffs2_add_physical_node_ref(c, raw);
351
spin_lock(&c->erase_completion_lock);
352
raw->next_in_ino = f->inocache->nodes;
353
f->inocache->nodes = raw;
354
spin_unlock(&c->erase_completion_lock);
357
jffs2_dbg_acct_sanity_check(c,NULL);
368
int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
369
struct jffs2_raw_inode *ri, unsigned char *buf,
370
uint32_t offset, uint32_t writelen, uint32_t *retlen) {
372
uint32_t writtenlen = 0;
374
D1(printk( "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
375
f->inocache->ino, offset, writelen));
378
struct jffs2_full_dnode *fn;
379
unsigned char *comprbuf = NULL;
380
uint16_t comprtype = JFFS2_COMPR_NONE;
381
uint32_t phys_ofs, alloclen;
382
uint32_t datalen, cdatalen;
386
D2(printk( "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
388
ret = jffs2_reserve_space(c, sizeof(*ri) +
389
JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL);
391
D1(printk( "jffs2_reserve_space returned %d\n", ret));
395
datalen = min_t(uint32_t, writelen,
396
PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
397
cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
399
comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
401
ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
402
ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
403
ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
404
ri->hdr_crc = cpu_to_je32(crc32(0, ri,
405
sizeof(struct jffs2_unknown_node)-4));
407
ri->ino = cpu_to_je32(f->inocache->ino);
408
ri->version = cpu_to_je32(++f->highest_version);
409
ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
410
ri->offset = cpu_to_je32(offset);
411
ri->csize = cpu_to_je32(cdatalen);
412
ri->dsize = cpu_to_je32(datalen);
413
ri->compr = comprtype & 0xff;
414
ri->usercompr = (comprtype >> 8 ) & 0xff;
415
ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
416
ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
418
fn = jffs2_write_dnode(c, f, ri,
419
comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
421
jffs2_free_comprbuf(comprbuf, buf);
426
jffs2_complete_reservation(c);
430
D1(printk( "Retrying node write in jffs2_write_inode_range()\n"));
435
ret = jffs2_add_full_dnode_to_inode(c, f, fn);
437
jffs2_mark_node_obsolete(c, f->metadata->raw);
438
jffs2_free_full_dnode(f->metadata);
443
D1(printk( "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
444
jffs2_mark_node_obsolete(c, fn->raw);
445
jffs2_free_full_dnode(fn);
448
jffs2_complete_reservation(c);
452
jffs2_complete_reservation(c);
454
printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
458
D1(printk( "increasing writtenlen by %d\n", datalen));
459
writtenlen += datalen;
464
*retlen = writtenlen;
468
int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
469
struct jffs2_inode_info *f,
470
struct jffs2_raw_inode *ri,
471
const char *name, int namelen) {
472
struct jffs2_raw_dirent *rd;
473
struct jffs2_full_dnode *fn;
474
struct jffs2_full_dirent *fd;
475
uint32_t alloclen, phys_ofs;
481
ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
482
D1(printk( "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
488
ri->data_crc = cpu_to_je32(0);
489
ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
491
fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
493
D1(printk( "jffs2_do_create created file with mode 0x%x\n",
494
jemode_to_cpu(ri->mode)));
497
D1(printk( "jffs2_write_dnode() failed\n"));
500
jffs2_complete_reservation(c);
509
jffs2_complete_reservation(c);
510
ret = jffs2_reserve_space(c,
511
sizeof(*rd) + namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
515
D1(printk( "jffs2_reserve_space() for dirent failed\n"));
519
rd = jffs2_alloc_raw_dirent();
522
jffs2_complete_reservation(c);
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);
536
rd->mctime = ri->ctime;
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,
543
(const unsigned char *) name, namelen, phys_ofs, ALLOC_NORMAL);
545
jffs2_free_raw_dirent(rd);
551
jffs2_complete_reservation(c);
558
jffs2_add_fd_to_list(c, fd, &dir_f->dents);
560
jffs2_complete_reservation(c);
567
int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
568
const char *name, int namelen, struct jffs2_inode_info *dead_f) {
569
struct jffs2_raw_dirent *rd;
570
struct jffs2_full_dirent *fd;
571
uint32_t alloclen, phys_ofs;
575
!jffs2_can_mark_obsolete(c)) {
579
rd = jffs2_alloc_raw_dirent();
584
ret = jffs2_reserve_space(c,
585
sizeof(*rd) + namelen, &phys_ofs, &alloclen, ALLOC_DELETION);
587
jffs2_free_raw_dirent(rd);
594
rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
595
rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
596
rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
597
rd->hdr_crc = cpu_to_je32(crc32(0, rd,
598
sizeof(struct jffs2_unknown_node)-4));
600
rd->pino = cpu_to_je32(dir_f->inocache->ino);
601
rd->version = cpu_to_je32(++dir_f->highest_version);
602
rd->ino = cpu_to_je32(0);
603
rd->mctime = cpu_to_je32(get_seconds());
605
rd->type = DT_UNKNOWN;
606
rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
607
rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
609
fd = jffs2_write_dirent(c, dir_f, rd, (const unsigned char *) name,
610
namelen, phys_ofs, ALLOC_DELETION);
612
jffs2_free_raw_dirent(rd);
615
jffs2_complete_reservation(c);
621
jffs2_add_fd_to_list(c, fd, &dir_f->dents);
624
struct jffs2_full_dirent **prev = &dir_f->dents;
625
uint32_t nhash = full_name_hash((const unsigned char *) name, namelen);
629
while ((*prev) && (*prev)->nhash <= nhash) {
630
if ((*prev)->nhash == nhash &&
631
!memcmp((*prev)->name, name, namelen) &&
632
!(*prev)->name[namelen]) {
633
struct jffs2_full_dirent *this = *prev;
635
D1(printk( "Marking old dirent node (ino #%u) @%08x obsolete\n",
636
this->ino, ref_offset(this->raw)));
639
jffs2_mark_node_obsolete(c, (this->raw));
640
jffs2_free_full_dirent(this);
643
prev = &((*prev)->next);
651
if (dead_f && dead_f->inocache) {
655
if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
656
while (dead_f->dents) {
660
dead_f->dents = fd->next;
663
printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
664
dead_f->inocache->ino, fd->name, fd->ino);
666
D1(printk( "Removing deletion dirent for \"%s\" from dir ino #%u\n",
667
fd->name, dead_f->inocache->ino));
669
jffs2_mark_node_obsolete(c, fd->raw);
670
jffs2_free_full_dirent(fd);
674
dead_f->inocache->nlink--;
679
jffs2_complete_reservation(c);
684
int jffs2_do_link (struct jffs2_sb_info *c,
685
struct jffs2_inode_info *dir_f, uint32_t ino,
686
uint8_t type, const char *name, int namelen) {
687
struct jffs2_raw_dirent *rd;
688
struct jffs2_full_dirent *fd;
689
uint32_t alloclen, phys_ofs;
692
rd = jffs2_alloc_raw_dirent();
696
ret = jffs2_reserve_space(c,
697
sizeof(*rd) + namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
699
jffs2_free_raw_dirent(rd);
706
rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
707
rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
708
rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
709
rd->hdr_crc = cpu_to_je32(crc32(0, rd,
710
sizeof(struct jffs2_unknown_node) - 4));
712
rd->pino = cpu_to_je32(dir_f->inocache->ino);
713
rd->version = cpu_to_je32(++dir_f->highest_version);
714
rd->ino = cpu_to_je32(ino);
715
rd->mctime = cpu_to_je32(get_seconds());
720
rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
721
rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
723
fd = jffs2_write_dirent(c, dir_f, rd,
724
(const unsigned char *) name, namelen, phys_ofs, ALLOC_NORMAL);
726
jffs2_free_raw_dirent(rd);
729
jffs2_complete_reservation(c);
735
jffs2_add_fd_to_list(c, fd, &dir_f->dents);
737
jffs2_complete_reservation(c);