blob: 1406f2ce20a72058ed5ea8c8ff3de727b612a40c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/fs.h>
14#include <linux/crc32.h>
15#include <linux/slab.h>
16#include <linux/pagemap.h>
17#include <linux/mtd/mtd.h>
18#include "nodelist.h"
19#include "compr.h"
20
21
22int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, uint32_t mode, struct jffs2_raw_inode *ri)
23{
24 struct jffs2_inode_cache *ic;
25
26 ic = jffs2_alloc_inode_cache();
27 if (!ic) {
28 return -ENOMEM;
29 }
30
31 memset(ic, 0, sizeof(*ic));
32
33 f->inocache = ic;
34 f->inocache->nlink = 1;
35 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 f->inocache->state = INO_STATE_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 jffs2_add_ino_cache(c, f->inocache);
David Woodhouse7d200962005-04-13 14:22:38 +010039 D1(printk(KERN_DEBUG "jffs2_do_new_inode(): Assigned ino# %d\n", f->inocache->ino));
40 ri->ino = cpu_to_je32(f->inocache->ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
43 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
44 ri->totlen = cpu_to_je32(PAD(sizeof(*ri)));
45 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
46 ri->mode = cpu_to_jemode(mode);
47
48 f->highest_version = 1;
49 ri->version = cpu_to_je32(f->highest_version);
50
51 return 0;
52}
53
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000054/* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 write it to the flash, link it into the existing inode/fragment list */
56
David Woodhouse9fe48542006-05-23 00:38:06 +010057struct jffs2_full_dnode *jffs2_write_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
58 struct jffs2_raw_inode *ri, const unsigned char *data,
59 uint32_t datalen, int alloc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct jffs2_full_dnode *fn;
63 size_t retlen;
David Woodhouse9fe48542006-05-23 00:38:06 +010064 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (je32_to_cpu(ri->totlen) != sizeof(*ri) + datalen) {
81 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);
82 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 fn = jffs2_alloc_full_dnode();
David Woodhouse2f785402006-05-24 02:04:45 +010085 if (!fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* check number of valid vecs */
89 if (!datalen || !data)
90 cnt = 1;
91 retry:
David Woodhouse2f785402006-05-24 02:04:45 +010092 flash_ofs = write_ofs(c);
David Woodhouse9fe48542006-05-23 00:38:06 +010093
94 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Estelle Hammache9b88f472005-01-28 18:53:05 +000096 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(ri->version) < f->highest_version)) {
97 BUG_ON(!retried);
98 D1(printk(KERN_DEBUG "jffs2_write_dnode : dnode_version %d, "
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000099 "highest version %d -> updating dnode\n",
Estelle Hammache9b88f472005-01-28 18:53:05 +0000100 je32_to_cpu(ri->version), f->highest_version));
101 ri->version = cpu_to_je32(++f->highest_version);
102 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 ret = jffs2_flash_writev(c, vecs, cnt, flash_ofs, &retlen,
106 (alloc_mode==ALLOC_GC)?0:f->inocache->ino);
107
108 if (ret || (retlen != sizeof(*ri) + datalen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000109 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
111
112 /* Mark the space as dirtied */
113 if (retlen) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000114 /* Don't change raw->size to match retlen. We may have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 written the node header already, and only the data will
116 seem corrupted, in which case the scan would skip over
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000117 any node we write before the original intended end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 this node */
David Woodhouse2f785402006-05-24 02:04:45 +0100119 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*ri)+datalen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100121 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 -0700122 }
David Woodhouse2f785402006-05-24 02:04:45 +0100123 if (!retried && alloc_mode != ALLOC_NORETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Try to reallocate space and retry */
125 uint32_t dummy;
126 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
127
128 retried = 1;
129
130 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000131
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100132 jffs2_dbg_acct_sanity_check(c,jeb);
133 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100136 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
137 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 } else {
139 /* Locking pain */
140 up(&f->sem);
141 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000142
David Woodhouse9fe48542006-05-23 00:38:06 +0100143 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
144 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 down(&f->sem);
146 }
147
148 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100149 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
151
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 goto retry;
156 }
157 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 /* Release the full_dnode which is now useless, and return */
160 jffs2_free_full_dnode(fn);
161 return ERR_PTR(ret?ret:-EIO);
162 }
163 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000164 /* If node covers at least a whole page, or if it starts at the
165 beginning of a page and runs to the end of the file, or if
166 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
169 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
170 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
David Woodhouse2f785402006-05-24 02:04:45 +0100171 flash_ofs |= REF_PRISTINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100173 flash_ofs |= REF_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
David Woodhouse2f785402006-05-24 02:04:45 +0100175 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200176 if (IS_ERR(fn->raw)) {
177 void *hold_err = fn->raw;
178 /* Release the full_dnode which is now useless, and return */
179 jffs2_free_full_dnode(fn);
180 return ERR_PTR(PTR_ERR(hold_err));
181 }
David Woodhouse2f785402006-05-24 02:04:45 +0100182 fn->ofs = je32_to_cpu(ri->offset);
183 fn->size = je32_to_cpu(ri->dsize);
184 fn->frags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 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 +0100187 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
189 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen)));
190
191 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100192 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 return fn;
196}
197
David Woodhouse9fe48542006-05-23 00:38:06 +0100198struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
199 struct jffs2_raw_dirent *rd, const unsigned char *name,
200 uint32_t namelen, int alloc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct jffs2_full_dirent *fd;
203 size_t retlen;
204 struct kvec vecs[2];
David Woodhouse2f785402006-05-24 02:04:45 +0100205 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int retried = 0;
207 int ret;
208
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000209 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 -0700210 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
211 je32_to_cpu(rd->name_crc)));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
214 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
215 BUG();
David Woodhouse2f785402006-05-24 02:04:45 +0100216 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 vecs[0].iov_base = rd;
219 vecs[0].iov_len = sizeof(*rd);
220 vecs[1].iov_base = (unsigned char *)name;
221 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 fd = jffs2_alloc_full_dirent(namelen+1);
David Woodhouse2f785402006-05-24 02:04:45 +0100224 if (!fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 fd->version = je32_to_cpu(rd->version);
228 fd->ino = je32_to_cpu(rd->ino);
229 fd->nhash = full_name_hash(name, strlen(name));
230 fd->type = rd->type;
231 memcpy(fd->name, name, namelen);
232 fd->name[namelen]=0;
233
234 retry:
David Woodhouse2f785402006-05-24 02:04:45 +0100235 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
David Woodhouse2f785402006-05-24 02:04:45 +0100237 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Estelle Hammache9b88f472005-01-28 18:53:05 +0000239 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
240 BUG_ON(!retried);
241 D1(printk(KERN_DEBUG "jffs2_write_dirent : dirent_version %d, "
242 "highest version %d -> updating dirent\n",
243 je32_to_cpu(rd->version), f->highest_version));
244 rd->version = cpu_to_je32(++f->highest_version);
245 fd->version = je32_to_cpu(rd->version);
246 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000247 }
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
250 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
251 if (ret || (retlen != sizeof(*rd) + namelen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000252 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
254 /* Mark the space as dirtied */
255 if (retlen) {
David Woodhouse2f785402006-05-24 02:04:45 +0100256 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100258 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 -0700259 }
David Woodhouse2f785402006-05-24 02:04:45 +0100260 if (!retried) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Try to reallocate space and retry */
262 uint32_t dummy;
263 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
264
265 retried = 1;
266
267 D1(printk(KERN_DEBUG "Retrying failed write.\n"));
268
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100269 jffs2_dbg_acct_sanity_check(c,jeb);
270 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100273 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
274 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 } else {
276 /* Locking pain */
277 up(&f->sem);
278 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000279
David Woodhouse9fe48542006-05-23 00:38:06 +0100280 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
281 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 down(&f->sem);
283 }
284
285 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100286 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", flash_ofs));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100288 jffs2_dbg_acct_sanity_check(c,jeb);
289 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 goto retry;
291 }
292 D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 /* Release the full_dnode which is now useless, and return */
295 jffs2_free_full_dirent(fd);
296 return ERR_PTR(ret?ret:-EIO);
297 }
298 /* Mark the space used */
David Woodhouse2f785402006-05-24 02:04:45 +0100299 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | REF_PRISTINE, PAD(sizeof(*rd)+namelen), f->inocache);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200300 if (IS_ERR(fd->raw)) {
301 void *hold_err = fd->raw;
302 /* Release the full_dirent which is now useless, and return */
303 jffs2_free_full_dirent(fd);
304 return ERR_PTR(PTR_ERR(hold_err));
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100308 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 return fd;
312}
313
314/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
315 we don't have to go digging in struct inode or its equivalent. It should set:
316 mode, uid, gid, (starting)isize, atime, ctime, mtime */
317int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000318 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 uint32_t offset, uint32_t writelen, uint32_t *retlen)
320{
321 int ret = 0;
322 uint32_t writtenlen = 0;
323
324 D1(printk(KERN_DEBUG "jffs2_write_inode_range(): Ino #%u, ofs 0x%x, len 0x%x\n",
325 f->inocache->ino, offset, writelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 while(writelen) {
328 struct jffs2_full_dnode *fn;
329 unsigned char *comprbuf = NULL;
330 uint16_t comprtype = JFFS2_COMPR_NONE;
David Woodhouse9fe48542006-05-23 00:38:06 +0100331 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 uint32_t datalen, cdatalen;
333 int retried = 0;
334
335 retry:
336 D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, offset));
337
David Woodhouse9fe48542006-05-23 00:38:06 +0100338 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100339 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (ret) {
341 D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
342 break;
343 }
344 down(&f->sem);
345 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
346 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
347
348 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
349
350 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
351 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
352 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
353 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
354
355 ri->ino = cpu_to_je32(f->inocache->ino);
356 ri->version = cpu_to_je32(++f->highest_version);
357 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
358 ri->offset = cpu_to_je32(offset);
359 ri->csize = cpu_to_je32(cdatalen);
360 ri->dsize = cpu_to_je32(datalen);
361 ri->compr = comprtype & 0xff;
362 ri->usercompr = (comprtype >> 8 ) & 0xff;
363 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
364 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
365
David Woodhouse9fe48542006-05-23 00:38:06 +0100366 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 jffs2_free_comprbuf(comprbuf, buf);
369
370 if (IS_ERR(fn)) {
371 ret = PTR_ERR(fn);
372 up(&f->sem);
373 jffs2_complete_reservation(c);
374 if (!retried) {
375 /* Write error to be retried */
376 retried = 1;
377 D1(printk(KERN_DEBUG "Retrying node write in jffs2_write_inode_range()\n"));
378 goto retry;
379 }
380 break;
381 }
382 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
383 if (f->metadata) {
384 jffs2_mark_node_obsolete(c, f->metadata->raw);
385 jffs2_free_full_dnode(f->metadata);
386 f->metadata = NULL;
387 }
388 if (ret) {
389 /* Eep */
390 D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
391 jffs2_mark_node_obsolete(c, fn->raw);
392 jffs2_free_full_dnode(fn);
393
394 up(&f->sem);
395 jffs2_complete_reservation(c);
396 break;
397 }
398 up(&f->sem);
399 jffs2_complete_reservation(c);
400 if (!datalen) {
401 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
402 ret = -EIO;
403 break;
404 }
405 D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
406 writtenlen += datalen;
407 offset += datalen;
408 writelen -= datalen;
409 buf += datalen;
410 }
411 *retlen = writtenlen;
412 return ret;
413}
414
415int 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)
416{
417 struct jffs2_raw_dirent *rd;
418 struct jffs2_full_dnode *fn;
419 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100420 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int ret;
422
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000423 /* Try to reserve enough space for both node and dirent.
424 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
David Woodhouse9fe48542006-05-23 00:38:06 +0100426 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100427 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 D1(printk(KERN_DEBUG "jffs2_do_create(): reserved 0x%x bytes\n", alloclen));
429 if (ret) {
430 up(&f->sem);
431 return ret;
432 }
433
434 ri->data_crc = cpu_to_je32(0);
435 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
436
David Woodhouse9fe48542006-05-23 00:38:06 +0100437 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 D1(printk(KERN_DEBUG "jffs2_do_create created file with mode 0x%x\n",
440 jemode_to_cpu(ri->mode)));
441
442 if (IS_ERR(fn)) {
443 D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
444 /* Eeek. Wave bye bye */
445 up(&f->sem);
446 jffs2_complete_reservation(c);
447 return PTR_ERR(fn);
448 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000449 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 obsoleted by the first data write
451 */
452 f->metadata = fn;
453
454 up(&f->sem);
455 jffs2_complete_reservation(c);
David Woodhouse9fe48542006-05-23 00:38:06 +0100456 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100457 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (ret) {
460 /* Eep. */
461 D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
462 return ret;
463 }
464
465 rd = jffs2_alloc_raw_dirent();
466 if (!rd) {
467 /* Argh. Now we treat it like a normal delete */
468 jffs2_complete_reservation(c);
469 return -ENOMEM;
470 }
471
472 down(&dir_f->sem);
473
474 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
475 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
476 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
477 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
478
479 rd->pino = cpu_to_je32(dir_f->inocache->ino);
480 rd->version = cpu_to_je32(++dir_f->highest_version);
481 rd->ino = ri->ino;
482 rd->mctime = ri->ctime;
483 rd->nsize = namelen;
484 rd->type = DT_REG;
485 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
486 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
487
David Woodhouse9fe48542006-05-23 00:38:06 +0100488 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000493 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 as if it were the final unlink() */
495 jffs2_complete_reservation(c);
496 up(&dir_f->sem);
497 return PTR_ERR(fd);
498 }
499
500 /* Link the fd into the inode's list, obsoleting an old
501 one if necessary. */
502 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
503
504 jffs2_complete_reservation(c);
505 up(&dir_f->sem);
506
507 return 0;
508}
509
510
511int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100512 const char *name, int namelen, struct jffs2_inode_info *dead_f,
513 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct jffs2_raw_dirent *rd;
516 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100517 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int ret;
519
Joakim Tjernlunda4914862007-03-16 16:15:45 +0100520 if (!jffs2_can_mark_obsolete(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
522
523 rd = jffs2_alloc_raw_dirent();
524 if (!rd)
525 return -ENOMEM;
526
David Woodhouse9fe48542006-05-23 00:38:06 +0100527 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100528 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (ret) {
530 jffs2_free_raw_dirent(rd);
531 return ret;
532 }
533
534 down(&dir_f->sem);
535
536 /* Build a deletion node */
537 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
538 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
539 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
540 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 rd->pino = cpu_to_je32(dir_f->inocache->ino);
543 rd->version = cpu_to_je32(++dir_f->highest_version);
544 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100545 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 rd->nsize = namelen;
547 rd->type = DT_UNKNOWN;
548 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
549 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
550
David Woodhouse9fe48542006-05-23 00:38:06 +0100551 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 jffs2_free_raw_dirent(rd);
554
555 if (IS_ERR(fd)) {
556 jffs2_complete_reservation(c);
557 up(&dir_f->sem);
558 return PTR_ERR(fd);
559 }
560
561 /* File it. This will mark the old one obsolete. */
562 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
563 up(&dir_f->sem);
564 } else {
565 struct jffs2_full_dirent **prev = &dir_f->dents;
566 uint32_t nhash = full_name_hash(name, namelen);
567
568 down(&dir_f->sem);
569
570 while ((*prev) && (*prev)->nhash <= nhash) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000571 if ((*prev)->nhash == nhash &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 !memcmp((*prev)->name, name, namelen) &&
573 !(*prev)->name[namelen]) {
574 struct jffs2_full_dirent *this = *prev;
575
576 D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) @%08x obsolete\n",
577 this->ino, ref_offset(this->raw)));
578
579 *prev = this->next;
580 jffs2_mark_node_obsolete(c, (this->raw));
581 jffs2_free_full_dirent(this);
582 break;
583 }
584 prev = &((*prev)->next);
585 }
586 up(&dir_f->sem);
587 }
588
589 /* dead_f is NULL if this was a rename not a real unlink */
590 /* Also catch the !f->inocache case, where there was a dirent
591 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000592 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 down(&dead_f->sem);
595
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000596 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
597 while (dead_f->dents) {
598 /* There can be only deleted ones */
599 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000600
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000601 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000602
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000603 if (fd->ino) {
604 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
605 dead_f->inocache->ino, fd->name, fd->ino);
606 } else {
607 D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n",
608 fd->name, dead_f->inocache->ino));
609 }
610 jffs2_mark_node_obsolete(c, fd->raw);
611 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 dead_f->inocache->nlink--;
616 /* NB: Caller must set inode nlink if appropriate */
617 up(&dead_f->sem);
618 }
619
620 jffs2_complete_reservation(c);
621
622 return 0;
623}
624
625
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100626int 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 -0700627{
628 struct jffs2_raw_dirent *rd;
629 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100630 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int ret;
632
633 rd = jffs2_alloc_raw_dirent();
634 if (!rd)
635 return -ENOMEM;
636
David Woodhouse9fe48542006-05-23 00:38:06 +0100637 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100638 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (ret) {
640 jffs2_free_raw_dirent(rd);
641 return ret;
642 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 down(&dir_f->sem);
645
646 /* Build a deletion node */
647 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
648 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
649 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
650 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
651
652 rd->pino = cpu_to_je32(dir_f->inocache->ino);
653 rd->version = cpu_to_je32(++dir_f->highest_version);
654 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100655 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 rd->nsize = namelen;
657
658 rd->type = type;
659
660 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
661 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
662
David Woodhouse9fe48542006-05-23 00:38:06 +0100663 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 jffs2_free_raw_dirent(rd);
666
667 if (IS_ERR(fd)) {
668 jffs2_complete_reservation(c);
669 up(&dir_f->sem);
670 return PTR_ERR(fd);
671 }
672
673 /* File it. This will mark the old one obsolete. */
674 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
675
676 jffs2_complete_reservation(c);
677 up(&dir_f->sem);
678
679 return 0;
680}