blob: 0e12b7561b71a0220fdd024aeea8cd7126239377 [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
David Woodhouse9fe48542006-05-23 00:38:06 +010059struct 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63{
64 struct jffs2_raw_node_ref *raw;
65 struct jffs2_full_dnode *fn;
66 size_t retlen;
David Woodhouse9fe48542006-05-23 00:38:06 +010067 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct kvec vecs[2];
69 int ret;
70 int retried = 0;
71 unsigned long cnt = 2;
72
73 D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
74 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
75 BUG();
76 }
77 );
78 vecs[0].iov_base = ri;
79 vecs[0].iov_len = sizeof(*ri);
80 vecs[1].iov_base = (unsigned char *)data;
81 vecs[1].iov_len = datalen;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
84 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);
85 }
86 raw = jffs2_alloc_raw_node_ref();
87 if (!raw)
88 return ERR_PTR(-ENOMEM);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 fn = jffs2_alloc_full_dnode();
91 if (!fn) {
92 jffs2_free_raw_node_ref(raw);
93 return ERR_PTR(-ENOMEM);
94 }
95
96 fn->ofs = je32_to_cpu(ri->offset);
97 fn->size = je32_to_cpu(ri->dsize);
98 fn->frags = 0;
99
100 /* check number of valid vecs */
101 if (!datalen || !data)
102 cnt = 1;
103 retry:
104 fn->raw = raw;
105
David Woodhouse9fe48542006-05-23 00:38:06 +0100106 raw->flash_offset = flash_ofs = write_ofs(c);
107
108 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Estelle Hammache9b88f472005-01-28 18:53:05 +0000110 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
111 BUG_ON(!retried);
112 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000113 "highest version %d -> updating dnode\n",
Estelle Hammache9b88f472005-01-28 18:53:05 +0000114 je32_to_cpu(ri->version), f->highest_version));
115 ri->version = cpu_to_je32(++f->highest_version);
116 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000117 }
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
120 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
121
122 if (ret || (retlen != sizeof(*ri) + datalen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000123 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
125
126 /* Mark the space as dirtied */
127 if (retlen) {
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 Woodhousefcb75782006-05-22 15:23:10 +0100134 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen), NULL);
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) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100153 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
154 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
David Woodhouse9fe48542006-05-23 00:38:06 +0100160 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
161 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 down(&f->sem);
163 }
164
165 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100166 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
168
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100169 jffs2_dbg_acct_sanity_check(c,jeb);
170 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 goto retry;
173 }
174 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
175 jffs2_free_raw_node_ref(raw);
176 }
177 /* Release the full_dnode which is now useless, and return */
178 jffs2_free_full_dnode(fn);
179 return ERR_PTR(ret?ret:-EIO);
180 }
181 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000182 /* If node covers at least a whole page, or if it starts at the
183 beginning of a page and runs to the end of the file, or if
184 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
186 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
187 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
188 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
189 raw->flash_offset |= REF_PRISTINE;
190 } else {
191 raw->flash_offset |= REF_NORMAL;
192 }
David Woodhousefcb75782006-05-22 15:23:10 +0100193 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen), f->inocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 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 +0000196 flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
198 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
199
200 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100201 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 return fn;
205}
206
David Woodhouse9fe48542006-05-23 00:38:06 +0100207struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
208 struct jffs2_raw_dirent *rd, const unsigned char *name,
209 uint32_t namelen, int alloc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct jffs2_raw_node_ref *raw;
212 struct jffs2_full_dirent *fd;
213 size_t retlen;
214 struct kvec vecs[2];
David Woodhouse9fe48542006-05-23 00:38:06 +0100215 uint32_t flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int retried = 0;
217 int ret;
218
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000219 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 -0700220 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
221 je32_to_cpu(rd->name_crc)));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
224 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
225 BUG();
226 }
227 );
228
229 vecs[0].iov_base = rd;
230 vecs[0].iov_len = sizeof(*rd);
231 vecs[1].iov_base = (unsigned char *)name;
232 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000233
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100234 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 raw = jffs2_alloc_raw_node_ref();
237
238 if (!raw)
239 return ERR_PTR(-ENOMEM);
240
241 fd = jffs2_alloc_full_dirent(namelen+1);
242 if (!fd) {
243 jffs2_free_raw_node_ref(raw);
244 return ERR_PTR(-ENOMEM);
245 }
246
247 fd->version = je32_to_cpu(rd->version);
248 fd->ino = je32_to_cpu(rd->ino);
249 fd->nhash = full_name_hash(name, strlen(name));
250 fd->type = rd->type;
251 memcpy(fd->name, name, namelen);
252 fd->name[namelen]=0;
253
254 retry:
255 fd->raw = raw;
256
257 raw->flash_offset = flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Estelle Hammache9b88f472005-01-28 18:53:05 +0000259 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
260 BUG_ON(!retried);
261 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
262 "highest version %d -> updating dirent\n",
263 je32_to_cpu(rd->version), f->highest_version));
264 rd->version = cpu_to_je32(++f->highest_version);
265 fd->version = je32_to_cpu(rd->version);
266 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000267 }
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
270 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
271 if (ret || (retlen != sizeof(*rd) + namelen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000272 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
274 /* Mark the space as dirtied */
275 if (retlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 raw->flash_offset |= REF_OBSOLETE;
David Woodhousefcb75782006-05-22 15:23:10 +0100277 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 jffs2_mark_node_obsolete(c, raw);
279 } else {
280 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
281 jffs2_free_raw_node_ref(raw);
282 }
283 if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
284 /* Try to reallocate space and retry */
285 uint32_t dummy;
286 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
287
288 retried = 1;
289
290 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
291
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100292 jffs2_dbg_acct_sanity_check(c,jeb);
293 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100296 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
297 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } else {
299 /* Locking pain */
300 up(&f->sem);
301 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000302
David Woodhouse9fe48542006-05-23 00:38:06 +0100303 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
304 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 down(&f->sem);
306 }
307
308 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100309 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100311 jffs2_dbg_acct_sanity_check(c,jeb);
312 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 goto retry;
314 }
315 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
316 jffs2_free_raw_node_ref(raw);
317 }
318 /* Release the full_dnode which is now useless, and return */
319 jffs2_free_full_dirent(fd);
320 return ERR_PTR(ret?ret:-EIO);
321 }
322 /* Mark the space used */
323 raw->flash_offset |= REF_PRISTINE;
David Woodhousefcb75782006-05-22 15:23:10 +0100324 jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen), f->inocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100327 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 return fd;
331}
332
333/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
334 we don't have to go digging in struct inode or its equivalent. It should set:
335 mode, uid, gid, (starting)isize, atime, ctime, mtime */
336int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000337 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 uint32_t offset, uint32_t writelen, uint32_t *retlen)
339{
340 int ret = 0;
341 uint32_t writtenlen = 0;
342
343 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
344 f->inocache->ino, offset, writelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 while(writelen) {
347 struct jffs2_full_dnode *fn;
348 unsigned char *comprbuf = NULL;
349 uint16_t comprtype = JFFS2_COMPR_NONE;
David Woodhouse9fe48542006-05-23 00:38:06 +0100350 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 uint32_t datalen, cdatalen;
352 int retried = 0;
353
354 retry:
355 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
356
David Woodhouse9fe48542006-05-23 00:38:06 +0100357 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100358 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (ret) {
360 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
361 break;
362 }
363 down(&f->sem);
364 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
365 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
366
367 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
368
369 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
370 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
371 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
372 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
373
374 ri->ino = cpu_to_je32(f->inocache->ino);
375 ri->version = cpu_to_je32(++f->highest_version);
376 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
377 ri->offset = cpu_to_je32(offset);
378 ri->csize = cpu_to_je32(cdatalen);
379 ri->dsize = cpu_to_je32(datalen);
380 ri->compr = comprtype & 0xff;
381 ri->usercompr = (comprtype >> 8 ) & 0xff;
382 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
383 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
384
David Woodhouse9fe48542006-05-23 00:38:06 +0100385 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 jffs2_free_comprbuf(comprbuf, buf);
388
389 if (IS_ERR(fn)) {
390 ret = PTR_ERR(fn);
391 up(&f->sem);
392 jffs2_complete_reservation(c);
393 if (!retried) {
394 /* Write error to be retried */
395 retried = 1;
396 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
397 goto retry;
398 }
399 break;
400 }
401 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
402 if (f->metadata) {
403 jffs2_mark_node_obsolete(c, f->metadata->raw);
404 jffs2_free_full_dnode(f->metadata);
405 f->metadata = NULL;
406 }
407 if (ret) {
408 /* Eep */
409 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
410 jffs2_mark_node_obsolete(c, fn->raw);
411 jffs2_free_full_dnode(fn);
412
413 up(&f->sem);
414 jffs2_complete_reservation(c);
415 break;
416 }
417 up(&f->sem);
418 jffs2_complete_reservation(c);
419 if (!datalen) {
420 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
421 ret = -EIO;
422 break;
423 }
424 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
425 writtenlen += datalen;
426 offset += datalen;
427 writelen -= datalen;
428 buf += datalen;
429 }
430 *retlen = writtenlen;
431 return ret;
432}
433
434int 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)
435{
436 struct jffs2_raw_dirent *rd;
437 struct jffs2_full_dnode *fn;
438 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100439 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int ret;
441
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000442 /* Try to reserve enough space for both node and dirent.
443 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
David Woodhouse9fe48542006-05-23 00:38:06 +0100445 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100446 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
448 if (ret) {
449 up(&f->sem);
450 return ret;
451 }
452
453 ri->data_crc = cpu_to_je32(0);
454 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
455
David Woodhouse9fe48542006-05-23 00:38:06 +0100456 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
459 jemode_to_cpu(ri->mode)));
460
461 if (IS_ERR(fn)) {
462 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
463 /* Eeek. Wave bye bye */
464 up(&f->sem);
465 jffs2_complete_reservation(c);
466 return PTR_ERR(fn);
467 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000468 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 obsoleted by the first data write
470 */
471 f->metadata = fn;
472
473 up(&f->sem);
474 jffs2_complete_reservation(c);
David Woodhouse9fe48542006-05-23 00:38:06 +0100475 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100476 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (ret) {
479 /* Eep. */
480 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
481 return ret;
482 }
483
484 rd = jffs2_alloc_raw_dirent();
485 if (!rd) {
486 /* Argh. Now we treat it like a normal delete */
487 jffs2_complete_reservation(c);
488 return -ENOMEM;
489 }
490
491 down(&dir_f->sem);
492
493 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
494 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
495 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
496 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
497
498 rd->pino = cpu_to_je32(dir_f->inocache->ino);
499 rd->version = cpu_to_je32(++dir_f->highest_version);
500 rd->ino = ri->ino;
501 rd->mctime = ri->ctime;
502 rd->nsize = namelen;
503 rd->type = DT_REG;
504 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
505 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
506
David Woodhouse9fe48542006-05-23 00:38:06 +0100507 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000512 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 as if it were the final unlink() */
514 jffs2_complete_reservation(c);
515 up(&dir_f->sem);
516 return PTR_ERR(fd);
517 }
518
519 /* Link the fd into the inode's list, obsoleting an old
520 one if necessary. */
521 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
522
523 jffs2_complete_reservation(c);
524 up(&dir_f->sem);
525
526 return 0;
527}
528
529
530int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100531 const char *name, int namelen, struct jffs2_inode_info *dead_f,
532 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct jffs2_raw_dirent *rd;
535 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100536 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int ret;
538
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000539 if (1 /* alternative branch needs testing */ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 !jffs2_can_mark_obsolete(c)) {
541 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
542
543 rd = jffs2_alloc_raw_dirent();
544 if (!rd)
545 return -ENOMEM;
546
David Woodhouse9fe48542006-05-23 00:38:06 +0100547 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100548 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (ret) {
550 jffs2_free_raw_dirent(rd);
551 return ret;
552 }
553
554 down(&dir_f->sem);
555
556 /* Build a deletion node */
557 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
558 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
559 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
560 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 rd->pino = cpu_to_je32(dir_f->inocache->ino);
563 rd->version = cpu_to_je32(++dir_f->highest_version);
564 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100565 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rd->nsize = namelen;
567 rd->type = DT_UNKNOWN;
568 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
569 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
570
David Woodhouse9fe48542006-05-23 00:38:06 +0100571 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 jffs2_free_raw_dirent(rd);
574
575 if (IS_ERR(fd)) {
576 jffs2_complete_reservation(c);
577 up(&dir_f->sem);
578 return PTR_ERR(fd);
579 }
580
581 /* File it. This will mark the old one obsolete. */
582 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
583 up(&dir_f->sem);
584 } else {
585 struct jffs2_full_dirent **prev = &dir_f->dents;
586 uint32_t nhash = full_name_hash(name, namelen);
587
588 down(&dir_f->sem);
589
590 while ((*prev) && (*prev)->nhash <= nhash) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000591 if ((*prev)->nhash == nhash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 !memcmp((*prev)->name, name, namelen) &&
593 !(*prev)->name[namelen]) {
594 struct jffs2_full_dirent *this = *prev;
595
596 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
597 this->ino, ref_offset(this->raw)));
598
599 *prev = this->next;
600 jffs2_mark_node_obsolete(c, (this->raw));
601 jffs2_free_full_dirent(this);
602 break;
603 }
604 prev = &((*prev)->next);
605 }
606 up(&dir_f->sem);
607 }
608
609 /* dead_f is NULL if this was a rename not a real unlink */
610 /* Also catch the !f->inocache case, where there was a dirent
611 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000612 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 down(&dead_f->sem);
615
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000616 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
617 while (dead_f->dents) {
618 /* There can be only deleted ones */
619 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000620
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000621 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000622
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000623 if (fd->ino) {
624 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
625 dead_f->inocache->ino, fd->name, fd->ino);
626 } else {
627 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
628 fd->name, dead_f->inocache->ino));
629 }
630 jffs2_mark_node_obsolete(c, fd->raw);
631 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 dead_f->inocache->nlink--;
636 /* NB: Caller must set inode nlink if appropriate */
637 up(&dead_f->sem);
638 }
639
640 jffs2_complete_reservation(c);
641
642 return 0;
643}
644
645
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100646int 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 -0700647{
648 struct jffs2_raw_dirent *rd;
649 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100650 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int ret;
652
653 rd = jffs2_alloc_raw_dirent();
654 if (!rd)
655 return -ENOMEM;
656
David Woodhouse9fe48542006-05-23 00:38:06 +0100657 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100658 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (ret) {
660 jffs2_free_raw_dirent(rd);
661 return ret;
662 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 down(&dir_f->sem);
665
666 /* Build a deletion node */
667 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
668 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
669 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
670 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
671
672 rd->pino = cpu_to_je32(dir_f->inocache->ino);
673 rd->version = cpu_to_je32(++dir_f->highest_version);
674 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100675 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 rd->nsize = namelen;
677
678 rd->type = type;
679
680 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
681 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
682
David Woodhouse9fe48542006-05-23 00:38:06 +0100683 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 jffs2_free_raw_dirent(rd);
686
687 if (IS_ERR(fd)) {
688 jffs2_complete_reservation(c);
689 up(&dir_f->sem);
690 return PTR_ERR(fd);
691 }
692
693 /* File it. This will mark the old one obsolete. */
694 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
695
696 jffs2_complete_reservation(c);
697 up(&dir_f->sem);
698
699 return 0;
700}