blob: 4462541d11f8820b1d54a294316a82dc26e9b8e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000010 * $Id: write.c,v 1.97 2005/11/07 11:14:42 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
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"
22
23
24int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri)
25{
26 struct jffs2_inode_cache *ic;
27
28 ic = jffs2_alloc_inode_cache();
29 if (!ic) {
30 return -ENOMEM;
31 }
32
33 memset(ic, 0, sizeof(*ic));
34
35 f->inocache = ic;
36 f->inocache->nlink = 1;
37 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 f->inocache->state = INO_STATE_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 jffs2_add_ino_cache(c, f->inocache);
David Woodhouse7d200962005-04-13 14:22:38 +010041 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
42 ri->ino = cpu_to_je32(f->inocache->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
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);
49
50 f->highest_version = 1;
51 ri->version = cpu_to_je32(f->highest_version);
52
53 return 0;
54}
55
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000056/* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 write it to the flash, link it into the existing inode/fragment list */
58
59struct 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)
60
61{
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;
69
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();
73 }
74 );
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;
79
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +010080 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
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);
84 }
85 raw = jffs2_alloc_raw_node_ref();
86 if (!raw)
87 return ERR_PTR(-ENOMEM);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 fn = jffs2_alloc_full_dnode();
90 if (!fn) {
91 jffs2_free_raw_node_ref(raw);
92 return ERR_PTR(-ENOMEM);
93 }
94
95 fn->ofs = je32_to_cpu(ri->offset);
96 fn->size = je32_to_cpu(ri->dsize);
97 fn->frags = 0;
98
99 /* check number of valid vecs */
100 if (!datalen || !data)
101 cnt = 1;
102 retry:
103 fn->raw = raw;
104
105 raw->flash_offset = flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Estelle Hammache9b88f472005-01-28 18:53:05 +0000107 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, "
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000110 "highest version %d -> updating dnode\n",
Estelle Hammache9b88f472005-01-28 18:53:05 +0000111 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));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000114 }
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
117 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
118
119 if (ret || (retlen != sizeof(*ri) + datalen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000120 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
122
123 /* Mark the space as dirtied */
124 if (retlen) {
125 /* Doesn't belong to any inode */
126 raw->next_in_ino = NULL;
127
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000128 /* Don't change raw->size to match retlen. We may have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 written the node header already, and only the data will
130 seem corrupted, in which case the scan would skip over
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000131 any node we write before the original intended end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 this node */
133 raw->flash_offset |= REF_OBSOLETE;
David Woodhouseb64335f2006-05-21 04:36:45 +0100134 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 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);
139 }
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];
144
145 retried = 1;
146
147 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000148
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100149 jffs2_dbg_acct_sanity_check(c,jeb);
150 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 if (alloc_mode == ALLOC_GC) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100153 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs,
154 &dummy, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
156 /* Locking pain */
157 up(&f->sem);
158 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000159
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100160 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs,
161 &dummy, alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 down(&f->sem);
163 }
164
165 if (!ret) {
166 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
167
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100168 jffs2_dbg_acct_sanity_check(c,jeb);
169 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 goto retry;
172 }
173 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
174 jffs2_free_raw_node_ref(raw);
175 }
176 /* Release the full_dnode which is now useless, and return */
177 jffs2_free_full_dnode(fn);
178 return ERR_PTR(ret?ret:-EIO);
179 }
180 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000181 /* 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
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;
191 }
David Woodhouseb64335f2006-05-21 04:36:45 +0100192 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
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);
199
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",
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000201 flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
203 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
204
205 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100206 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 return fn;
210}
211
212struct 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)
213{
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;
220
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000221 D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
223 je32_to_cpu(rd->name_crc)));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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();
228 }
229 );
230
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;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000235
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100236 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 raw = jffs2_alloc_raw_node_ref();
239
240 if (!raw)
241 return ERR_PTR(-ENOMEM);
242
243 fd = jffs2_alloc_full_dirent(namelen+1);
244 if (!fd) {
245 jffs2_free_raw_node_ref(raw);
246 return ERR_PTR(-ENOMEM);
247 }
248
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;
255
256 retry:
257 fd->raw = raw;
258
259 raw->flash_offset = flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Estelle Hammache9b88f472005-01-28 18:53:05 +0000261 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));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 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)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000274 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 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;
David Woodhouseb64335f2006-05-21 04:36:45 +0100280 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 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);
285 }
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];
290
291 retried = 1;
292
293 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
294
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100295 jffs2_dbg_acct_sanity_check(c,jeb);
296 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (alloc_mode == ALLOC_GC) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100299 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs,
300 &dummy, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 } else {
302 /* Locking pain */
303 up(&f->sem);
304 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000305
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100306 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs,
307 &dummy, alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 down(&f->sem);
309 }
310
311 if (!ret) {
312 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100313 jffs2_dbg_acct_sanity_check(c,jeb);
314 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto retry;
316 }
317 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
318 jffs2_free_raw_node_ref(raw);
319 }
320 /* Release the full_dnode which is now useless, and return */
321 jffs2_free_full_dirent(fd);
322 return ERR_PTR(ret?ret:-EIO);
323 }
324 /* Mark the space used */
325 raw->flash_offset |= REF_PRISTINE;
David Woodhouseb64335f2006-05-21 04:36:45 +0100326 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
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);
332
333 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100334 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 return fd;
338}
339
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 */
343int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000344 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 uint32_t offset, uint32_t writelen, uint32_t *retlen)
346{
347 int ret = 0;
348 uint32_t writtenlen = 0;
349
350 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
351 f->inocache->ino, offset, writelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 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;
360
361 retry:
362 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
363
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100364 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs,
365 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ret) {
367 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
368 break;
369 }
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);
373
374 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
375
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));
380
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));
391
392 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
393
394 jffs2_free_comprbuf(comprbuf, buf);
395
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;
405 }
406 break;
407 }
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;
413 }
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);
419
420 up(&f->sem);
421 jffs2_complete_reservation(c);
422 break;
423 }
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;
430 }
431 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
432 writtenlen += datalen;
433 offset += datalen;
434 writelen -= datalen;
435 buf += datalen;
436 }
437 *retlen = writtenlen;
438 return ret;
439}
440
441int 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)
442{
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;
448
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000449 /* Try to reserve enough space for both node and dirent.
450 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100452 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL,
453 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
455 if (ret) {
456 up(&f->sem);
457 return ret;
458 }
459
460 ri->data_crc = cpu_to_je32(0);
461 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
462
463 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
464
465 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
466 jemode_to_cpu(ri->mode)));
467
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);
474 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000475 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 obsoleted by the first data write
477 */
478 f->metadata = fn;
479
480 up(&f->sem);
481 jffs2_complete_reservation(c);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100482 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
483 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ret) {
486 /* Eep. */
487 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
488 return ret;
489 }
490
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;
496 }
497
498 down(&dir_f->sem);
499
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));
504
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));
513
514 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
515
516 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000519 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 as if it were the final unlink() */
521 jffs2_complete_reservation(c);
522 up(&dir_f->sem);
523 return PTR_ERR(fd);
524 }
525
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);
529
530 jffs2_complete_reservation(c);
531 up(&dir_f->sem);
532
533 return 0;
534}
535
536
537int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100538 const char *name, int namelen, struct jffs2_inode_info *dead_f,
539 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct jffs2_raw_dirent *rd;
542 struct jffs2_full_dirent *fd;
543 uint32_t alloclen, phys_ofs;
544 int ret;
545
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000546 if (1 /* alternative branch needs testing */ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 !jffs2_can_mark_obsolete(c)) {
548 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
549
550 rd = jffs2_alloc_raw_dirent();
551 if (!rd)
552 return -ENOMEM;
553
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100554 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
555 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (ret) {
557 jffs2_free_raw_dirent(rd);
558 return ret;
559 }
560
561 down(&dir_f->sem);
562
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));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 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);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100572 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 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));
577
578 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 jffs2_free_raw_dirent(rd);
581
582 if (IS_ERR(fd)) {
583 jffs2_complete_reservation(c);
584 up(&dir_f->sem);
585 return PTR_ERR(fd);
586 }
587
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);
594
595 down(&dir_f->sem);
596
597 while ((*prev) && (*prev)->nhash <= nhash) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000598 if ((*prev)->nhash == nhash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 !memcmp((*prev)->name, name, namelen) &&
600 !(*prev)->name[namelen]) {
601 struct jffs2_full_dirent *this = *prev;
602
603 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
604 this->ino, ref_offset(this->raw)));
605
606 *prev = this->next;
607 jffs2_mark_node_obsolete(c, (this->raw));
608 jffs2_free_full_dirent(this);
609 break;
610 }
611 prev = &((*prev)->next);
612 }
613 up(&dir_f->sem);
614 }
615
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. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000619 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 down(&dead_f->sem);
622
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000623 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;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000627
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000628 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000629
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000630 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));
636 }
637 jffs2_mark_node_obsolete(c, fd->raw);
638 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 dead_f->inocache->nlink--;
643 /* NB: Caller must set inode nlink if appropriate */
644 up(&dead_f->sem);
645 }
646
647 jffs2_complete_reservation(c);
648
649 return 0;
650}
651
652
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100653int 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct jffs2_raw_dirent *rd;
656 struct jffs2_full_dirent *fd;
657 uint32_t alloclen, phys_ofs;
658 int ret;
659
660 rd = jffs2_alloc_raw_dirent();
661 if (!rd)
662 return -ENOMEM;
663
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100664 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
665 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (ret) {
667 jffs2_free_raw_dirent(rd);
668 return ret;
669 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 down(&dir_f->sem);
672
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));
678
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);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100682 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 rd->nsize = namelen;
684
685 rd->type = type;
686
687 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
688 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
689
690 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 jffs2_free_raw_dirent(rd);
693
694 if (IS_ERR(fd)) {
695 jffs2_complete_reservation(c);
696 up(&dir_f->sem);
697 return PTR_ERR(fd);
698 }
699
700 /* File it. This will mark the old one obsolete. */
701 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
702
703 jffs2_complete_reservation(c);
704 up(&dir_f->sem);
705
706 return 0;
707}