blob: 67176792e138e8e7a3cbeb5efeaf423d600eaf05 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct jffs2_full_dnode *fn;
65 size_t retlen;
David Woodhouse9fe48542006-05-23 00:38:06 +010066 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct kvec vecs[2];
68 int ret;
69 int retried = 0;
70 unsigned long cnt = 2;
71
72 D1(if(je32_to_cpu(ri->hdr_crc) != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
73 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
74 BUG();
75 }
76 );
77 vecs[0].iov_base = ri;
78 vecs[0].iov_len = sizeof(*ri);
79 vecs[1].iov_base = (unsigned char *)data;
80 vecs[1].iov_len = datalen;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 fn = jffs2_alloc_full_dnode();
David Woodhouse2f785402006-05-24 02:04:45 +010087 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /* check number of valid vecs */
91 if (!datalen || !data)
92 cnt = 1;
93 retry:
David Woodhouse2f785402006-05-24 02:04:45 +010094 flash_ofs = write_ofs(c);
David Woodhouse9fe48542006-05-23 00:38:06 +010095
96 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Estelle Hammache9b88f472005-01-28 18:53:05 +000098 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
99 BUG_ON(!retried);
100 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000101 "highest version %d -> updating dnode\n",
Estelle Hammache9b88f472005-01-28 18:53:05 +0000102 je32_to_cpu(ri->version), f->highest_version));
103 ri->version = cpu_to_je32(++f->highest_version);
104 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
108 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
109
110 if (ret || (retlen != sizeof(*ri) + datalen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000111 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
113
114 /* Mark the space as dirtied */
115 if (retlen) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000116 /* Don't change raw->size to match retlen. We may have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 written the node header already, and only the data will
118 seem corrupted, in which case the scan would skip over
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000119 any node we write before the original intended end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 this node */
David Woodhouse2f785402006-05-24 02:04:45 +0100121 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100123 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
David Woodhouse2f785402006-05-24 02:04:45 +0100125 if (!retried && alloc_mode != ALLOC_NORETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /* Try to reallocate space and retry */
127 uint32_t dummy;
128 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
129
130 retried = 1;
131
132 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000133
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100134 jffs2_dbg_acct_sanity_check(c,jeb);
135 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100138 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
139 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 } else {
141 /* Locking pain */
142 up(&f->sem);
143 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000144
David Woodhouse9fe48542006-05-23 00:38:06 +0100145 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
146 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 down(&f->sem);
148 }
149
150 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100151 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
153
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100154 jffs2_dbg_acct_sanity_check(c,jeb);
155 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 goto retry;
158 }
159 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161 /* Release the full_dnode which is now useless, and return */
162 jffs2_free_full_dnode(fn);
163 return ERR_PTR(ret?ret:-EIO);
164 }
165 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000166 /* If node covers at least a whole page, or if it starts at the
167 beginning of a page and runs to the end of the file, or if
168 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
171 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
172 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
David Woodhouse2f785402006-05-24 02:04:45 +0100173 flash_ofs |= REF_PRISTINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100175 flash_ofs |= REF_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
David Woodhouse2f785402006-05-24 02:04:45 +0100177 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
178 fn->ofs = je32_to_cpu(ri->offset);
179 fn->size = je32_to_cpu(ri->dsize);
180 fn->frags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 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",
David Woodhouse2f785402006-05-24 02:04:45 +0100183 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
185 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
186
187 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100188 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 return fn;
192}
193
David Woodhouse9fe48542006-05-23 00:38:06 +0100194struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
195 struct jffs2_raw_dirent *rd, const unsigned char *name,
196 uint32_t namelen, int alloc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct jffs2_full_dirent *fd;
199 size_t retlen;
200 struct kvec vecs[2];
David Woodhouse2f785402006-05-24 02:04:45 +0100201 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int retried = 0;
203 int ret;
204
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000205 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 -0700206 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
207 je32_to_cpu(rd->name_crc)));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
210 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
211 BUG();
David Woodhouse2f785402006-05-24 02:04:45 +0100212 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 vecs[0].iov_base = rd;
215 vecs[0].iov_len = sizeof(*rd);
216 vecs[1].iov_base = (unsigned char *)name;
217 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 fd = jffs2_alloc_full_dirent(namelen+1);
David Woodhouse2f785402006-05-24 02:04:45 +0100220 if (!fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 fd->version = je32_to_cpu(rd->version);
224 fd->ino = je32_to_cpu(rd->ino);
225 fd->nhash = full_name_hash(name, strlen(name));
226 fd->type = rd->type;
227 memcpy(fd->name, name, namelen);
228 fd->name[namelen]=0;
229
230 retry:
David Woodhouse2f785402006-05-24 02:04:45 +0100231 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
David Woodhouse2f785402006-05-24 02:04:45 +0100233 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Estelle Hammache9b88f472005-01-28 18:53:05 +0000235 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
236 BUG_ON(!retried);
237 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
238 "highest version %d -> updating dirent\n",
239 je32_to_cpu(rd->version), f->highest_version));
240 rd->version = cpu_to_je32(++f->highest_version);
241 fd->version = je32_to_cpu(rd->version);
242 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
246 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
247 if (ret || (retlen != sizeof(*rd) + namelen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000248 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
250 /* Mark the space as dirtied */
251 if (retlen) {
David Woodhouse2f785402006-05-24 02:04:45 +0100252 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100254 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", flash_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
David Woodhouse2f785402006-05-24 02:04:45 +0100256 if (!retried) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* Try to reallocate space and retry */
258 uint32_t dummy;
259 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
260
261 retried = 1;
262
263 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
264
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100265 jffs2_dbg_acct_sanity_check(c,jeb);
266 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100269 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
270 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else {
272 /* Locking pain */
273 up(&f->sem);
274 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000275
David Woodhouse9fe48542006-05-23 00:38:06 +0100276 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
277 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 down(&f->sem);
279 }
280
281 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100282 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100284 jffs2_dbg_acct_sanity_check(c,jeb);
285 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto retry;
287 }
288 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 /* Release the full_dnode which is now useless, and return */
291 jffs2_free_full_dirent(fd);
292 return ERR_PTR(ret?ret:-EIO);
293 }
294 /* Mark the space used */
David Woodhouse2f785402006-05-24 02:04:45 +0100295 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | REF_PRISTINE, PAD(sizeof(*rd)+namelen), f->inocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100298 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 return fd;
302}
303
304/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
305 we don't have to go digging in struct inode or its equivalent. It should set:
306 mode, uid, gid, (starting)isize, atime, ctime, mtime */
307int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000308 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 uint32_t offset, uint32_t writelen, uint32_t *retlen)
310{
311 int ret = 0;
312 uint32_t writtenlen = 0;
313
314 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
315 f->inocache->ino, offset, writelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 while(writelen) {
318 struct jffs2_full_dnode *fn;
319 unsigned char *comprbuf = NULL;
320 uint16_t comprtype = JFFS2_COMPR_NONE;
David Woodhouse9fe48542006-05-23 00:38:06 +0100321 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 uint32_t datalen, cdatalen;
323 int retried = 0;
324
325 retry:
326 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
327
David Woodhouse9fe48542006-05-23 00:38:06 +0100328 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100329 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (ret) {
331 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
332 break;
333 }
334 down(&f->sem);
335 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
336 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
337
338 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
339
340 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
341 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
342 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
343 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
344
345 ri->ino = cpu_to_je32(f->inocache->ino);
346 ri->version = cpu_to_je32(++f->highest_version);
347 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
348 ri->offset = cpu_to_je32(offset);
349 ri->csize = cpu_to_je32(cdatalen);
350 ri->dsize = cpu_to_je32(datalen);
351 ri->compr = comprtype & 0xff;
352 ri->usercompr = (comprtype >> 8 ) & 0xff;
353 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
354 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
355
David Woodhouse9fe48542006-05-23 00:38:06 +0100356 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 jffs2_free_comprbuf(comprbuf, buf);
359
360 if (IS_ERR(fn)) {
361 ret = PTR_ERR(fn);
362 up(&f->sem);
363 jffs2_complete_reservation(c);
364 if (!retried) {
365 /* Write error to be retried */
366 retried = 1;
367 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
368 goto retry;
369 }
370 break;
371 }
372 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
373 if (f->metadata) {
374 jffs2_mark_node_obsolete(c, f->metadata->raw);
375 jffs2_free_full_dnode(f->metadata);
376 f->metadata = NULL;
377 }
378 if (ret) {
379 /* Eep */
380 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
381 jffs2_mark_node_obsolete(c, fn->raw);
382 jffs2_free_full_dnode(fn);
383
384 up(&f->sem);
385 jffs2_complete_reservation(c);
386 break;
387 }
388 up(&f->sem);
389 jffs2_complete_reservation(c);
390 if (!datalen) {
391 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
392 ret = -EIO;
393 break;
394 }
395 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
396 writtenlen += datalen;
397 offset += datalen;
398 writelen -= datalen;
399 buf += datalen;
400 }
401 *retlen = writtenlen;
402 return ret;
403}
404
405int 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)
406{
407 struct jffs2_raw_dirent *rd;
408 struct jffs2_full_dnode *fn;
409 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100410 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int ret;
412
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000413 /* Try to reserve enough space for both node and dirent.
414 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 */
David Woodhouse9fe48542006-05-23 00:38:06 +0100416 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100417 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
419 if (ret) {
420 up(&f->sem);
421 return ret;
422 }
423
424 ri->data_crc = cpu_to_je32(0);
425 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
426
David Woodhouse9fe48542006-05-23 00:38:06 +0100427 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
430 jemode_to_cpu(ri->mode)));
431
432 if (IS_ERR(fn)) {
433 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
434 /* Eeek. Wave bye bye */
435 up(&f->sem);
436 jffs2_complete_reservation(c);
437 return PTR_ERR(fn);
438 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000439 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 obsoleted by the first data write
441 */
442 f->metadata = fn;
443
444 up(&f->sem);
445 jffs2_complete_reservation(c);
David Woodhouse9fe48542006-05-23 00:38:06 +0100446 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100447 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (ret) {
450 /* Eep. */
451 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
452 return ret;
453 }
454
455 rd = jffs2_alloc_raw_dirent();
456 if (!rd) {
457 /* Argh. Now we treat it like a normal delete */
458 jffs2_complete_reservation(c);
459 return -ENOMEM;
460 }
461
462 down(&dir_f->sem);
463
464 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
465 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
466 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
467 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
468
469 rd->pino = cpu_to_je32(dir_f->inocache->ino);
470 rd->version = cpu_to_je32(++dir_f->highest_version);
471 rd->ino = ri->ino;
472 rd->mctime = ri->ctime;
473 rd->nsize = namelen;
474 rd->type = DT_REG;
475 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
476 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
477
David Woodhouse9fe48542006-05-23 00:38:06 +0100478 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000483 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 as if it were the final unlink() */
485 jffs2_complete_reservation(c);
486 up(&dir_f->sem);
487 return PTR_ERR(fd);
488 }
489
490 /* Link the fd into the inode's list, obsoleting an old
491 one if necessary. */
492 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
493
494 jffs2_complete_reservation(c);
495 up(&dir_f->sem);
496
497 return 0;
498}
499
500
501int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100502 const char *name, int namelen, struct jffs2_inode_info *dead_f,
503 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 struct jffs2_raw_dirent *rd;
506 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100507 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int ret;
509
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000510 if (1 /* alternative branch needs testing */ ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 !jffs2_can_mark_obsolete(c)) {
512 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
513
514 rd = jffs2_alloc_raw_dirent();
515 if (!rd)
516 return -ENOMEM;
517
David Woodhouse9fe48542006-05-23 00:38:06 +0100518 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100519 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (ret) {
521 jffs2_free_raw_dirent(rd);
522 return ret;
523 }
524
525 down(&dir_f->sem);
526
527 /* Build a deletion node */
528 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
529 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
530 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
531 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 rd->pino = cpu_to_je32(dir_f->inocache->ino);
534 rd->version = cpu_to_je32(++dir_f->highest_version);
535 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100536 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 rd->nsize = namelen;
538 rd->type = DT_UNKNOWN;
539 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
540 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
541
David Woodhouse9fe48542006-05-23 00:38:06 +0100542 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 jffs2_free_raw_dirent(rd);
545
546 if (IS_ERR(fd)) {
547 jffs2_complete_reservation(c);
548 up(&dir_f->sem);
549 return PTR_ERR(fd);
550 }
551
552 /* File it. This will mark the old one obsolete. */
553 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
554 up(&dir_f->sem);
555 } else {
556 struct jffs2_full_dirent **prev = &dir_f->dents;
557 uint32_t nhash = full_name_hash(name, namelen);
558
559 down(&dir_f->sem);
560
561 while ((*prev) && (*prev)->nhash <= nhash) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000562 if ((*prev)->nhash == nhash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 !memcmp((*prev)->name, name, namelen) &&
564 !(*prev)->name[namelen]) {
565 struct jffs2_full_dirent *this = *prev;
566
567 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
568 this->ino, ref_offset(this->raw)));
569
570 *prev = this->next;
571 jffs2_mark_node_obsolete(c, (this->raw));
572 jffs2_free_full_dirent(this);
573 break;
574 }
575 prev = &((*prev)->next);
576 }
577 up(&dir_f->sem);
578 }
579
580 /* dead_f is NULL if this was a rename not a real unlink */
581 /* Also catch the !f->inocache case, where there was a dirent
582 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000583 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 down(&dead_f->sem);
586
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000587 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
588 while (dead_f->dents) {
589 /* There can be only deleted ones */
590 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000591
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000592 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000593
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000594 if (fd->ino) {
595 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
596 dead_f->inocache->ino, fd->name, fd->ino);
597 } else {
598 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
599 fd->name, dead_f->inocache->ino));
600 }
601 jffs2_mark_node_obsolete(c, fd->raw);
602 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 dead_f->inocache->nlink--;
607 /* NB: Caller must set inode nlink if appropriate */
608 up(&dead_f->sem);
609 }
610
611 jffs2_complete_reservation(c);
612
613 return 0;
614}
615
616
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100617int 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 -0700618{
619 struct jffs2_raw_dirent *rd;
620 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100621 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int ret;
623
624 rd = jffs2_alloc_raw_dirent();
625 if (!rd)
626 return -ENOMEM;
627
David Woodhouse9fe48542006-05-23 00:38:06 +0100628 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100629 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (ret) {
631 jffs2_free_raw_dirent(rd);
632 return ret;
633 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 down(&dir_f->sem);
636
637 /* Build a deletion node */
638 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
639 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
640 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
641 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
642
643 rd->pino = cpu_to_je32(dir_f->inocache->ino);
644 rd->version = cpu_to_je32(++dir_f->highest_version);
645 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100646 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 rd->nsize = namelen;
648
649 rd->type = type;
650
651 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
652 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
653
David Woodhouse9fe48542006-05-23 00:38:06 +0100654 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 jffs2_free_raw_dirent(rd);
657
658 if (IS_ERR(fd)) {
659 jffs2_complete_reservation(c);
660 up(&dir_f->sem);
661 return PTR_ERR(fd);
662 }
663
664 /* File it. This will mark the old one obsolete. */
665 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
666
667 jffs2_complete_reservation(c);
668 up(&dir_f->sem);
669
670 return 0;
671}