blob: 1342f0158e9b0d0da2565c99c18eb5f088c501a5 [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;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 jffs2_add_ino_cache(c, f->inocache);
David Woodhouse7d200962005-04-13 14:22:38 +010042 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
43 ri->ino = cpu_to_je32(f->inocache->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
46 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
47 ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
48 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
49 ri->mode = cpu_to_jemode(mode);
50
51 f->highest_version = 1;
52 ri->version = cpu_to_je32(f->highest_version);
53
54 return 0;
55}
56
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000057/* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 write it to the flash, link it into the existing inode/fragment list */
59
60struct 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)
61
62{
63 struct jffs2_raw_node_ref *raw;
64 struct jffs2_full_dnode *fn;
65 size_t retlen;
66 struct kvec vecs[2];
67 int ret;
68 int retried = 0;
69 unsigned long cnt = 2;
70
71 D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
72 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
73 BUG();
74 }
75 );
76 vecs[0].iov_base = ri;
77 vecs[0].iov_len = sizeof(*ri);
78 vecs[1].iov_base = (unsigned char *)data;
79 vecs[1].iov_len = datalen;
80
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +010081 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 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
106 raw->flash_offset = flash_ofs;
107 raw->__totlen = PAD(sizeof(*ri)+datalen);
108 raw->next_phys = NULL;
109
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) {
128 /* Doesn't belong to any inode */
129 raw->next_in_ino = NULL;
130
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000131 /* Don't change raw->size to match retlen. We may have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 written the node header already, and only the data will
133 seem corrupted, in which case the scan would skip over
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000134 any node we write before the original intended end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 this node */
136 raw->flash_offset |= REF_OBSOLETE;
137 jffs2_add_physical_node_ref(c, raw);
138 jffs2_mark_node_obsolete(c, raw);
139 } else {
140 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
141 jffs2_free_raw_node_ref(raw);
142 }
143 if (!retried && alloc_mode != ALLOC_NORETRY && (raw = jffs2_alloc_raw_node_ref())) {
144 /* Try to reallocate space and retry */
145 uint32_t dummy;
146 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
147
148 retried = 1;
149
150 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000151
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100152 jffs2_dbg_acct_sanity_check(c,jeb);
153 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 if (alloc_mode == ALLOC_GC) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100156 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &flash_ofs,
157 &dummy, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else {
159 /* Locking pain */
160 up(&f->sem);
161 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000162
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100163 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &flash_ofs,
164 &dummy, alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 down(&f->sem);
166 }
167
168 if (!ret) {
169 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
170
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100171 jffs2_dbg_acct_sanity_check(c,jeb);
172 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 goto retry;
175 }
176 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
177 jffs2_free_raw_node_ref(raw);
178 }
179 /* Release the full_dnode which is now useless, and return */
180 jffs2_free_full_dnode(fn);
181 return ERR_PTR(ret?ret:-EIO);
182 }
183 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000184 /* If node covers at least a whole page, or if it starts at the
185 beginning of a page and runs to the end of the file, or if
186 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
189 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
190 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
191 raw->flash_offset |= REF_PRISTINE;
192 } else {
193 raw->flash_offset |= REF_NORMAL;
194 }
195 jffs2_add_physical_node_ref(c, raw);
196
197 /* Link into per-inode list */
198 spin_lock(&c->erase_completion_lock);
199 raw->next_in_ino = f->inocache->nodes;
200 f->inocache->nodes = raw;
201 spin_unlock(&c->erase_completion_lock);
202
203 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 +0000204 flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
206 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
207
208 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100209 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 return fn;
213}
214
215struct 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)
216{
217 struct jffs2_raw_node_ref *raw;
218 struct jffs2_full_dirent *fd;
219 size_t retlen;
220 struct kvec vecs[2];
221 int retried = 0;
222 int ret;
223
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000224 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 -0700225 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
226 je32_to_cpu(rd->name_crc)));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
229 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
230 BUG();
231 }
232 );
233
234 vecs[0].iov_base = rd;
235 vecs[0].iov_len = sizeof(*rd);
236 vecs[1].iov_base = (unsigned char *)name;
237 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000238
Artem B. Bityutskiye0c8e422005-07-24 16:14:17 +0100239 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 raw = jffs2_alloc_raw_node_ref();
242
243 if (!raw)
244 return ERR_PTR(-ENOMEM);
245
246 fd = jffs2_alloc_full_dirent(namelen+1);
247 if (!fd) {
248 jffs2_free_raw_node_ref(raw);
249 return ERR_PTR(-ENOMEM);
250 }
251
252 fd->version = je32_to_cpu(rd->version);
253 fd->ino = je32_to_cpu(rd->ino);
254 fd->nhash = full_name_hash(name, strlen(name));
255 fd->type = rd->type;
256 memcpy(fd->name, name, namelen);
257 fd->name[namelen]=0;
258
259 retry:
260 fd->raw = raw;
261
262 raw->flash_offset = flash_ofs;
263 raw->__totlen = PAD(sizeof(*rd)+namelen);
264 raw->next_phys = NULL;
265
Estelle Hammache9b88f472005-01-28 18:53:05 +0000266 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
267 BUG_ON(!retried);
268 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
269 "highest version %d -> updating dirent\n",
270 je32_to_cpu(rd->version), f->highest_version));
271 rd->version = cpu_to_je32(++f->highest_version);
272 fd->version = je32_to_cpu(rd->version);
273 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
277 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
278 if (ret || (retlen != sizeof(*rd) + namelen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000279 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
281 /* Mark the space as dirtied */
282 if (retlen) {
283 raw->next_in_ino = NULL;
284 raw->flash_offset |= REF_OBSOLETE;
285 jffs2_add_physical_node_ref(c, raw);
286 jffs2_mark_node_obsolete(c, raw);
287 } else {
288 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
289 jffs2_free_raw_node_ref(raw);
290 }
291 if (!retried && (raw = jffs2_alloc_raw_node_ref())) {
292 /* Try to reallocate space and retry */
293 uint32_t dummy;
294 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
295
296 retried = 1;
297
298 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
299
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100300 jffs2_dbg_acct_sanity_check(c,jeb);
301 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (alloc_mode == ALLOC_GC) {
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100304 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &flash_ofs,
305 &dummy, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 } else {
307 /* Locking pain */
308 up(&f->sem);
309 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000310
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100311 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &flash_ofs,
312 &dummy, alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 down(&f->sem);
314 }
315
316 if (!ret) {
317 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100318 jffs2_dbg_acct_sanity_check(c,jeb);
319 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto retry;
321 }
322 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
323 jffs2_free_raw_node_ref(raw);
324 }
325 /* Release the full_dnode which is now useless, and return */
326 jffs2_free_full_dirent(fd);
327 return ERR_PTR(ret?ret:-EIO);
328 }
329 /* Mark the space used */
330 raw->flash_offset |= REF_PRISTINE;
331 jffs2_add_physical_node_ref(c, raw);
332
333 spin_lock(&c->erase_completion_lock);
334 raw->next_in_ino = f->inocache->nodes;
335 f->inocache->nodes = raw;
336 spin_unlock(&c->erase_completion_lock);
337
338 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100339 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 return fd;
343}
344
345/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
346 we don't have to go digging in struct inode or its equivalent. It should set:
347 mode, uid, gid, (starting)isize, atime, ctime, mtime */
348int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000349 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 uint32_t offset, uint32_t writelen, uint32_t *retlen)
351{
352 int ret = 0;
353 uint32_t writtenlen = 0;
354
355 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
356 f->inocache->ino, offset, writelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 while(writelen) {
359 struct jffs2_full_dnode *fn;
360 unsigned char *comprbuf = NULL;
361 uint16_t comprtype = JFFS2_COMPR_NONE;
362 uint32_t phys_ofs, alloclen;
363 uint32_t datalen, cdatalen;
364 int retried = 0;
365
366 retry:
367 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
368
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100369 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs,
370 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (ret) {
372 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
373 break;
374 }
375 down(&f->sem);
376 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
377 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
378
379 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
380
381 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
382 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
383 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
384 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
385
386 ri->ino = cpu_to_je32(f->inocache->ino);
387 ri->version = cpu_to_je32(++f->highest_version);
388 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
389 ri->offset = cpu_to_je32(offset);
390 ri->csize = cpu_to_je32(cdatalen);
391 ri->dsize = cpu_to_je32(datalen);
392 ri->compr = comprtype & 0xff;
393 ri->usercompr = (comprtype >> 8 ) & 0xff;
394 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
395 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
396
397 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, phys_ofs, ALLOC_NORETRY);
398
399 jffs2_free_comprbuf(comprbuf, buf);
400
401 if (IS_ERR(fn)) {
402 ret = PTR_ERR(fn);
403 up(&f->sem);
404 jffs2_complete_reservation(c);
405 if (!retried) {
406 /* Write error to be retried */
407 retried = 1;
408 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
409 goto retry;
410 }
411 break;
412 }
413 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
414 if (f->metadata) {
415 jffs2_mark_node_obsolete(c, f->metadata->raw);
416 jffs2_free_full_dnode(f->metadata);
417 f->metadata = NULL;
418 }
419 if (ret) {
420 /* Eep */
421 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
422 jffs2_mark_node_obsolete(c, fn->raw);
423 jffs2_free_full_dnode(fn);
424
425 up(&f->sem);
426 jffs2_complete_reservation(c);
427 break;
428 }
429 up(&f->sem);
430 jffs2_complete_reservation(c);
431 if (!datalen) {
432 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
433 ret = -EIO;
434 break;
435 }
436 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
437 writtenlen += datalen;
438 offset += datalen;
439 writelen -= datalen;
440 buf += datalen;
441 }
442 *retlen = writtenlen;
443 return ret;
444}
445
446int 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)
447{
448 struct jffs2_raw_dirent *rd;
449 struct jffs2_full_dnode *fn;
450 struct jffs2_full_dirent *fd;
451 uint32_t alloclen, phys_ofs;
452 int ret;
453
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000454 /* Try to reserve enough space for both node and dirent.
455 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100457 ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL,
458 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
460 if (ret) {
461 up(&f->sem);
462 return ret;
463 }
464
465 ri->data_crc = cpu_to_je32(0);
466 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
467
468 fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
469
470 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
471 jemode_to_cpu(ri->mode)));
472
473 if (IS_ERR(fn)) {
474 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
475 /* Eeek. Wave bye bye */
476 up(&f->sem);
477 jffs2_complete_reservation(c);
478 return PTR_ERR(fn);
479 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000480 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 obsoleted by the first data write
482 */
483 f->metadata = fn;
484
485 up(&f->sem);
486 jffs2_complete_reservation(c);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100487 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
488 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (ret) {
491 /* Eep. */
492 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
493 return ret;
494 }
495
496 rd = jffs2_alloc_raw_dirent();
497 if (!rd) {
498 /* Argh. Now we treat it like a normal delete */
499 jffs2_complete_reservation(c);
500 return -ENOMEM;
501 }
502
503 down(&dir_f->sem);
504
505 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
506 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
507 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
508 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
509
510 rd->pino = cpu_to_je32(dir_f->inocache->ino);
511 rd->version = cpu_to_je32(++dir_f->highest_version);
512 rd->ino = ri->ino;
513 rd->mctime = ri->ctime;
514 rd->nsize = namelen;
515 rd->type = DT_REG;
516 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
517 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
518
519 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
520
521 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000524 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 as if it were the final unlink() */
526 jffs2_complete_reservation(c);
527 up(&dir_f->sem);
528 return PTR_ERR(fd);
529 }
530
531 /* Link the fd into the inode's list, obsoleting an old
532 one if necessary. */
533 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
534
535 jffs2_complete_reservation(c);
536 up(&dir_f->sem);
537
538 return 0;
539}
540
541
542int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100543 const char *name, int namelen, struct jffs2_inode_info *dead_f,
544 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct jffs2_raw_dirent *rd;
547 struct jffs2_full_dirent *fd;
548 uint32_t alloclen, phys_ofs;
549 int ret;
550
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000551 if (1 /* alternative branch needs testing */ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 !jffs2_can_mark_obsolete(c)) {
553 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
554
555 rd = jffs2_alloc_raw_dirent();
556 if (!rd)
557 return -ENOMEM;
558
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100559 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
560 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (ret) {
562 jffs2_free_raw_dirent(rd);
563 return ret;
564 }
565
566 down(&dir_f->sem);
567
568 /* Build a deletion node */
569 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
570 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
571 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
572 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rd->pino = cpu_to_je32(dir_f->inocache->ino);
575 rd->version = cpu_to_je32(++dir_f->highest_version);
576 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100577 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 rd->nsize = namelen;
579 rd->type = DT_UNKNOWN;
580 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
581 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
582
583 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 jffs2_free_raw_dirent(rd);
586
587 if (IS_ERR(fd)) {
588 jffs2_complete_reservation(c);
589 up(&dir_f->sem);
590 return PTR_ERR(fd);
591 }
592
593 /* File it. This will mark the old one obsolete. */
594 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
595 up(&dir_f->sem);
596 } else {
597 struct jffs2_full_dirent **prev = &dir_f->dents;
598 uint32_t nhash = full_name_hash(name, namelen);
599
600 down(&dir_f->sem);
601
602 while ((*prev) && (*prev)->nhash <= nhash) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000603 if ((*prev)->nhash == nhash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 !memcmp((*prev)->name, name, namelen) &&
605 !(*prev)->name[namelen]) {
606 struct jffs2_full_dirent *this = *prev;
607
608 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
609 this->ino, ref_offset(this->raw)));
610
611 *prev = this->next;
612 jffs2_mark_node_obsolete(c, (this->raw));
613 jffs2_free_full_dirent(this);
614 break;
615 }
616 prev = &((*prev)->next);
617 }
618 up(&dir_f->sem);
619 }
620
621 /* dead_f is NULL if this was a rename not a real unlink */
622 /* Also catch the !f->inocache case, where there was a dirent
623 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000624 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 down(&dead_f->sem);
627
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000628 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
629 while (dead_f->dents) {
630 /* There can be only deleted ones */
631 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000632
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000633 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000634
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000635 if (fd->ino) {
636 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
637 dead_f->inocache->ino, fd->name, fd->ino);
638 } else {
639 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
640 fd->name, dead_f->inocache->ino));
641 }
642 jffs2_mark_node_obsolete(c, fd->raw);
643 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 dead_f->inocache->nlink--;
648 /* NB: Caller must set inode nlink if appropriate */
649 up(&dead_f->sem);
650 }
651
652 jffs2_complete_reservation(c);
653
654 return 0;
655}
656
657
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100658int 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 -0700659{
660 struct jffs2_raw_dirent *rd;
661 struct jffs2_full_dirent *fd;
662 uint32_t alloclen, phys_ofs;
663 int ret;
664
665 rd = jffs2_alloc_raw_dirent();
666 if (!rd)
667 return -ENOMEM;
668
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100669 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
670 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (ret) {
672 jffs2_free_raw_dirent(rd);
673 return ret;
674 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 down(&dir_f->sem);
677
678 /* Build a deletion node */
679 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
680 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
681 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
682 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
683
684 rd->pino = cpu_to_je32(dir_f->inocache->ino);
685 rd->version = cpu_to_je32(++dir_f->highest_version);
686 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100687 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 rd->nsize = namelen;
689
690 rd->type = type;
691
692 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
693 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
694
695 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, phys_ofs, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 jffs2_free_raw_dirent(rd);
698
699 if (IS_ERR(fd)) {
700 jffs2_complete_reservation(c);
701 up(&dir_f->sem);
702 return PTR_ERR(fd);
703 }
704
705 /* File it. This will mark the old one obsolete. */
706 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
707
708 jffs2_complete_reservation(c);
709 up(&dir_f->sem);
710
711 return 0;
712}