blob: ff2b00b604ecdfd02935128a049cf351fae10fd2 [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;
106 raw->__totlen = PAD(sizeof(*ri)+datalen);
107 raw->next_phys = NULL;
108
Estelle Hammache9b88f472005-01-28 18:53:05 +0000109 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
110 BUG_ON(!retried);
111 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000112 "highest version %d -> updating dnode\n",
Estelle Hammache9b88f472005-01-28 18:53:05 +0000113 je32_to_cpu(ri->version), f->highest_version));
114 ri->version = cpu_to_je32(++f->highest_version);
115 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000116 }
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
119 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
120
121 if (ret || (retlen != sizeof(*ri) + datalen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000122 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
124
125 /* Mark the space as dirtied */
126 if (retlen) {
127 /* Doesn't belong to any inode */
128 raw->next_in_ino = NULL;
129
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000130 /* Don't change raw->size to match retlen. We may have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 written the node header already, and only the data will
132 seem corrupted, in which case the scan would skip over
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000133 any node we write before the original intended end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 this node */
135 raw->flash_offset |= REF_OBSOLETE;
136 jffs2_add_physical_node_ref(c, raw);
137 jffs2_mark_node_obsolete(c, raw);
138 } else {
139 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
140 jffs2_free_raw_node_ref(raw);
141 }
142 if (!retried && alloc_mode != ALLOC_NORETRY && (raw = jffs2_alloc_raw_node_ref())) {
143 /* Try to reallocate space and retry */
144 uint32_t dummy;
145 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
146
147 retried = 1;
148
149 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000150
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100151 jffs2_dbg_acct_sanity_check(c,jeb);
152 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 if (alloc_mode == ALLOC_GC) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100155 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs,
156 &dummy, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 } else {
158 /* Locking pain */
159 up(&f->sem);
160 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000161
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100162 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs,
163 &dummy, alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 down(&f->sem);
165 }
166
167 if (!ret) {
168 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
169
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100170 jffs2_dbg_acct_sanity_check(c,jeb);
171 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 goto retry;
174 }
175 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
176 jffs2_free_raw_node_ref(raw);
177 }
178 /* Release the full_dnode which is now useless, and return */
179 jffs2_free_full_dnode(fn);
180 return ERR_PTR(ret?ret:-EIO);
181 }
182 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000183 /* If node covers at least a whole page, or if it starts at the
184 beginning of a page and runs to the end of the file, or if
185 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
187 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
188 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
189 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
190 raw->flash_offset |= REF_PRISTINE;
191 } else {
192 raw->flash_offset |= REF_NORMAL;
193 }
194 jffs2_add_physical_node_ref(c, raw);
195
196 /* Link into per-inode list */
197 spin_lock(&c->erase_completion_lock);
198 raw->next_in_ino = f->inocache->nodes;
199 f->inocache->nodes = raw;
200 spin_unlock(&c->erase_completion_lock);
201
202 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 +0000203 flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
205 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
206
207 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100208 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 return fn;
212}
213
214struct 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)
215{
216 struct jffs2_raw_node_ref *raw;
217 struct jffs2_full_dirent *fd;
218 size_t retlen;
219 struct kvec vecs[2];
220 int retried = 0;
221 int ret;
222
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000223 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 -0700224 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
225 je32_to_cpu(rd->name_crc)));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
228 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
229 BUG();
230 }
231 );
232
233 vecs[0].iov_base = rd;
234 vecs[0].iov_len = sizeof(*rd);
235 vecs[1].iov_base = (unsigned char *)name;
236 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000237
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100238 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 raw = jffs2_alloc_raw_node_ref();
241
242 if (!raw)
243 return ERR_PTR(-ENOMEM);
244
245 fd = jffs2_alloc_full_dirent(namelen+1);
246 if (!fd) {
247 jffs2_free_raw_node_ref(raw);
248 return ERR_PTR(-ENOMEM);
249 }
250
251 fd->version = je32_to_cpu(rd->version);
252 fd->ino = je32_to_cpu(rd->ino);
253 fd->nhash = full_name_hash(name, strlen(name));
254 fd->type = rd->type;
255 memcpy(fd->name, name, namelen);
256 fd->name[namelen]=0;
257
258 retry:
259 fd->raw = raw;
260
261 raw->flash_offset = flash_ofs;
262 raw->__totlen = PAD(sizeof(*rd)+namelen);
263 raw->next_phys = NULL;
264
Estelle Hammache9b88f472005-01-28 18:53:05 +0000265 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
266 BUG_ON(!retried);
267 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
268 "highest version %d -> updating dirent\n",
269 je32_to_cpu(rd->version), f->highest_version));
270 rd->version = cpu_to_je32(++f->highest_version);
271 fd->version = je32_to_cpu(rd->version);
272 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
276 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
277 if (ret || (retlen != sizeof(*rd) + namelen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000278 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
280 /* Mark the space as dirtied */
281 if (retlen) {
282 raw->next_in_ino = NULL;
283 raw->flash_offset |= REF_OBSOLETE;
284 jffs2_add_physical_node_ref(c, raw);
285 jffs2_mark_node_obsolete(c, raw);
286 } else {
287 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
288 jffs2_free_raw_node_ref(raw);
289 }
290 if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
291 /* Try to reallocate space and retry */
292 uint32_t dummy;
293 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
294
295 retried = 1;
296
297 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
298
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100299 jffs2_dbg_acct_sanity_check(c,jeb);
300 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (alloc_mode == ALLOC_GC) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100303 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs,
304 &dummy, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 } else {
306 /* Locking pain */
307 up(&f->sem);
308 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000309
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100310 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs,
311 &dummy, alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 down(&f->sem);
313 }
314
315 if (!ret) {
316 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100317 jffs2_dbg_acct_sanity_check(c,jeb);
318 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 goto retry;
320 }
321 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
322 jffs2_free_raw_node_ref(raw);
323 }
324 /* Release the full_dnode which is now useless, and return */
325 jffs2_free_full_dirent(fd);
326 return ERR_PTR(ret?ret:-EIO);
327 }
328 /* Mark the space used */
329 raw->flash_offset |= REF_PRISTINE;
330 jffs2_add_physical_node_ref(c, raw);
331
332 spin_lock(&c->erase_completion_lock);
333 raw->next_in_ino = f->inocache->nodes;
334 f->inocache->nodes = raw;
335 spin_unlock(&c->erase_completion_lock);
336
337 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100338 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 return fd;
342}
343
344/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
345 we don't have to go digging in struct inode or its equivalent. It should set:
346 mode, uid, gid, (starting)isize, atime, ctime, mtime */
347int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000348 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 uint32_t offset, uint32_t writelen, uint32_t *retlen)
350{
351 int ret = 0;
352 uint32_t writtenlen = 0;
353
354 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
355 f->inocache->ino, offset, writelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 while(writelen) {
358 struct jffs2_full_dnode *fn;
359 unsigned char *comprbuf = NULL;
360 uint16_t comprtype = JFFS2_COMPR_NONE;
361 uint32_t phys_ofs, alloclen;
362 uint32_t datalen, cdatalen;
363 int retried = 0;
364
365 retry:
366 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
367
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100368 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs,
369 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (ret) {
371 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
372 break;
373 }
374 down(&f->sem);
375 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
376 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
377
378 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
379
380 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
381 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
382 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
383 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
384
385 ri->ino = cpu_to_je32(f->inocache->ino);
386 ri->version = cpu_to_je32(++f->highest_version);
387 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
388 ri->offset = cpu_to_je32(offset);
389 ri->csize = cpu_to_je32(cdatalen);
390 ri->dsize = cpu_to_je32(datalen);
391 ri->compr = comprtype & 0xff;
392 ri->usercompr = (comprtype >> 8 ) & 0xff;
393 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
394 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
395
396 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
397
398 jffs2_free_comprbuf(comprbuf, buf);
399
400 if (IS_ERR(fn)) {
401 ret = PTR_ERR(fn);
402 up(&f->sem);
403 jffs2_complete_reservation(c);
404 if (!retried) {
405 /* Write error to be retried */
406 retried = 1;
407 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
408 goto retry;
409 }
410 break;
411 }
412 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
413 if (f->metadata) {
414 jffs2_mark_node_obsolete(c, f->metadata->raw);
415 jffs2_free_full_dnode(f->metadata);
416 f->metadata = NULL;
417 }
418 if (ret) {
419 /* Eep */
420 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
421 jffs2_mark_node_obsolete(c, fn->raw);
422 jffs2_free_full_dnode(fn);
423
424 up(&f->sem);
425 jffs2_complete_reservation(c);
426 break;
427 }
428 up(&f->sem);
429 jffs2_complete_reservation(c);
430 if (!datalen) {
431 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
432 ret = -EIO;
433 break;
434 }
435 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
436 writtenlen += datalen;
437 offset += datalen;
438 writelen -= datalen;
439 buf += datalen;
440 }
441 *retlen = writtenlen;
442 return ret;
443}
444
445int 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)
446{
447 struct jffs2_raw_dirent *rd;
448 struct jffs2_full_dnode *fn;
449 struct jffs2_full_dirent *fd;
450 uint32_t alloclen, phys_ofs;
451 int ret;
452
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000453 /* Try to reserve enough space for both node and dirent.
454 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100456 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL,
457 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
459 if (ret) {
460 up(&f->sem);
461 return ret;
462 }
463
464 ri->data_crc = cpu_to_je32(0);
465 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
466
467 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
468
469 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
470 jemode_to_cpu(ri->mode)));
471
472 if (IS_ERR(fn)) {
473 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
474 /* Eeek. Wave bye bye */
475 up(&f->sem);
476 jffs2_complete_reservation(c);
477 return PTR_ERR(fn);
478 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000479 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 obsoleted by the first data write
481 */
482 f->metadata = fn;
483
484 up(&f->sem);
485 jffs2_complete_reservation(c);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100486 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
487 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (ret) {
490 /* Eep. */
491 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
492 return ret;
493 }
494
495 rd = jffs2_alloc_raw_dirent();
496 if (!rd) {
497 /* Argh. Now we treat it like a normal delete */
498 jffs2_complete_reservation(c);
499 return -ENOMEM;
500 }
501
502 down(&dir_f->sem);
503
504 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
505 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
506 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
507 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
508
509 rd->pino = cpu_to_je32(dir_f->inocache->ino);
510 rd->version = cpu_to_je32(++dir_f->highest_version);
511 rd->ino = ri->ino;
512 rd->mctime = ri->ctime;
513 rd->nsize = namelen;
514 rd->type = DT_REG;
515 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
516 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
517
518 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
519
520 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000523 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 as if it were the final unlink() */
525 jffs2_complete_reservation(c);
526 up(&dir_f->sem);
527 return PTR_ERR(fd);
528 }
529
530 /* Link the fd into the inode's list, obsoleting an old
531 one if necessary. */
532 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
533
534 jffs2_complete_reservation(c);
535 up(&dir_f->sem);
536
537 return 0;
538}
539
540
541int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100542 const char *name, int namelen, struct jffs2_inode_info *dead_f,
543 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct jffs2_raw_dirent *rd;
546 struct jffs2_full_dirent *fd;
547 uint32_t alloclen, phys_ofs;
548 int ret;
549
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000550 if (1 /* alternative branch needs testing */ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 !jffs2_can_mark_obsolete(c)) {
552 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
553
554 rd = jffs2_alloc_raw_dirent();
555 if (!rd)
556 return -ENOMEM;
557
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100558 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
559 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (ret) {
561 jffs2_free_raw_dirent(rd);
562 return ret;
563 }
564
565 down(&dir_f->sem);
566
567 /* Build a deletion node */
568 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
569 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
570 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
571 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 rd->pino = cpu_to_je32(dir_f->inocache->ino);
574 rd->version = cpu_to_je32(++dir_f->highest_version);
575 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100576 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rd->nsize = namelen;
578 rd->type = DT_UNKNOWN;
579 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
580 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
581
582 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 jffs2_free_raw_dirent(rd);
585
586 if (IS_ERR(fd)) {
587 jffs2_complete_reservation(c);
588 up(&dir_f->sem);
589 return PTR_ERR(fd);
590 }
591
592 /* File it. This will mark the old one obsolete. */
593 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
594 up(&dir_f->sem);
595 } else {
596 struct jffs2_full_dirent **prev = &dir_f->dents;
597 uint32_t nhash = full_name_hash(name, namelen);
598
599 down(&dir_f->sem);
600
601 while ((*prev) && (*prev)->nhash <= nhash) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000602 if ((*prev)->nhash == nhash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 !memcmp((*prev)->name, name, namelen) &&
604 !(*prev)->name[namelen]) {
605 struct jffs2_full_dirent *this = *prev;
606
607 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
608 this->ino, ref_offset(this->raw)));
609
610 *prev = this->next;
611 jffs2_mark_node_obsolete(c, (this->raw));
612 jffs2_free_full_dirent(this);
613 break;
614 }
615 prev = &((*prev)->next);
616 }
617 up(&dir_f->sem);
618 }
619
620 /* dead_f is NULL if this was a rename not a real unlink */
621 /* Also catch the !f->inocache case, where there was a dirent
622 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000623 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 down(&dead_f->sem);
626
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000627 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
628 while (dead_f->dents) {
629 /* There can be only deleted ones */
630 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000631
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000632 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000633
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000634 if (fd->ino) {
635 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
636 dead_f->inocache->ino, fd->name, fd->ino);
637 } else {
638 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
639 fd->name, dead_f->inocache->ino));
640 }
641 jffs2_mark_node_obsolete(c, fd->raw);
642 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 dead_f->inocache->nlink--;
647 /* NB: Caller must set inode nlink if appropriate */
648 up(&dead_f->sem);
649 }
650
651 jffs2_complete_reservation(c);
652
653 return 0;
654}
655
656
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100657int 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 -0700658{
659 struct jffs2_raw_dirent *rd;
660 struct jffs2_full_dirent *fd;
661 uint32_t alloclen, phys_ofs;
662 int ret;
663
664 rd = jffs2_alloc_raw_dirent();
665 if (!rd)
666 return -ENOMEM;
667
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100668 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
669 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (ret) {
671 jffs2_free_raw_dirent(rd);
672 return ret;
673 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 down(&dir_f->sem);
676
677 /* Build a deletion node */
678 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
679 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
680 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
681 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
682
683 rd->pino = cpu_to_je32(dir_f->inocache->ino);
684 rd->version = cpu_to_je32(++dir_f->highest_version);
685 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100686 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 rd->nsize = namelen;
688
689 rd->type = type;
690
691 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
692 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
693
694 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 jffs2_free_raw_dirent(rd);
697
698 if (IS_ERR(fd)) {
699 jffs2_complete_reservation(c);
700 up(&dir_f->sem);
701 return PTR_ERR(fd);
702 }
703
704 /* File it. This will mark the old one obsolete. */
705 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
706
707 jffs2_complete_reservation(c);
708 up(&dir_f->sem);
709
710 return 0;
711}