blob: e137839a157d2c7441b12ca227627d2b5368763d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pagemap.h>
16#include <linux/mtd/mtd.h>
17#include "nodelist.h"
18#include "compr.h"
19
20
David Woodhouse27c72b02008-05-01 18:47:17 +010021int jffs2_do_new_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
22 uint32_t mode, struct jffs2_raw_inode *ri)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
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;
David Woodhouse27c72b02008-05-01 18:47:17 +010034 f->inocache->pino_nlink = 1; /* Will be overwritten shortly for directories */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 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);
Joe Perches9c261b32012-02-15 15:56:43 -080039 jffs2_dbg(1, "%s(): Assigned ino# %d\n", __func__, f->inocache->ino);
David Woodhouse7d200962005-04-13 14:22:38 +010040 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);
Joe Perches9c261b32012-02-15 15:56:43 -080098 jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n",
99 __func__,
100 je32_to_cpu(ri->version), f->highest_version);
Estelle Hammache9b88f472005-01-28 18:53:05 +0000101 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
Joe Perches9c261b32012-02-15 15:56:43 -0800130 jffs2_dbg(1, "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 */
David Woodhouseced22072008-04-22 15:13:40 +0100140 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 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);
David Woodhouseced22072008-04-22 15:13:40 +0100145 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100149 flash_ofs = write_ofs(c);
Joe Perches9c261b32012-02-15 15:56:43 -0800150 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
151 flash_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100153 jffs2_dbg_acct_sanity_check(c,jeb);
154 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 goto retry;
157 }
Joe Perches9c261b32012-02-15 15:56:43 -0800158 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
159 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);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200178 if (IS_ERR(fn->raw)) {
179 void *hold_err = fn->raw;
180 /* Release the full_dnode which is now useless, and return */
181 jffs2_free_full_dnode(fn);
David Howellse231c2e2008-02-07 00:15:26 -0800182 return ERR_CAST(hold_err);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200183 }
David Woodhouse2f785402006-05-24 02:04:45 +0100184 fn->ofs = je32_to_cpu(ri->offset);
185 fn->size = je32_to_cpu(ri->dsize);
186 fn->frags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Joe Perches9c261b32012-02-15 15:56:43 -0800188 jffs2_dbg(1, "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 +0100189 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
Joe Perches9c261b32012-02-15 15:56:43 -0800191 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100194 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 return fn;
198}
199
David Woodhouse9fe48542006-05-23 00:38:06 +0100200struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
201 struct jffs2_raw_dirent *rd, const unsigned char *name,
202 uint32_t namelen, int alloc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct jffs2_full_dirent *fd;
205 size_t retlen;
206 struct kvec vecs[2];
David Woodhouse2f785402006-05-24 02:04:45 +0100207 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int retried = 0;
209 int ret;
210
Joe Perches9c261b32012-02-15 15:56:43 -0800211 jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
212 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
Joe Perches9c261b32012-02-15 15:56:43 -0800214 je32_to_cpu(rd->name_crc));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
217 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
218 BUG();
David Woodhouse2f785402006-05-24 02:04:45 +0100219 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Woodhouse69ca4372007-10-13 11:33:50 +0100221 if (strnlen(name, namelen) != namelen) {
222 /* This should never happen, but seems to have done on at least one
223 occasion: https://dev.laptop.org/ticket/4184 */
224 printk(KERN_CRIT "Error in jffs2_write_dirent() -- name contains zero bytes!\n");
225 printk(KERN_CRIT "Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
226 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
227 je32_to_cpu(rd->name_crc));
228 WARN_ON(1);
229 return ERR_PTR(-EIO);
230 }
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 vecs[0].iov_base = rd;
233 vecs[0].iov_len = sizeof(*rd);
234 vecs[1].iov_base = (unsigned char *)name;
235 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 fd = jffs2_alloc_full_dirent(namelen+1);
David Woodhouse2f785402006-05-24 02:04:45 +0100238 if (!fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 fd->version = je32_to_cpu(rd->version);
242 fd->ino = je32_to_cpu(rd->ino);
David Woodhouse69ca4372007-10-13 11:33:50 +0100243 fd->nhash = full_name_hash(name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 fd->type = rd->type;
245 memcpy(fd->name, name, namelen);
246 fd->name[namelen]=0;
247
248 retry:
David Woodhouse2f785402006-05-24 02:04:45 +0100249 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David Woodhouse2f785402006-05-24 02:04:45 +0100251 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Estelle Hammache9b88f472005-01-28 18:53:05 +0000253 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
254 BUG_ON(!retried);
Joe Perches9c261b32012-02-15 15:56:43 -0800255 jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n",
256 __func__,
257 je32_to_cpu(rd->version), f->highest_version);
Estelle Hammache9b88f472005-01-28 18:53:05 +0000258 rd->version = cpu_to_je32(++f->highest_version);
259 fd->version = je32_to_cpu(rd->version);
260 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
264 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
265 if (ret || (retlen != sizeof(*rd) + namelen)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000266 printk(KERN_NOTICE "Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
268 /* Mark the space as dirtied */
269 if (retlen) {
David Woodhouse2f785402006-05-24 02:04:45 +0100270 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100272 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 -0700273 }
David Woodhouse2f785402006-05-24 02:04:45 +0100274 if (!retried) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Try to reallocate space and retry */
276 uint32_t dummy;
277 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
278
279 retried = 1;
280
Joe Perches9c261b32012-02-15 15:56:43 -0800281 jffs2_dbg(1, "Retrying failed write.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100283 jffs2_dbg_acct_sanity_check(c,jeb);
284 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100287 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
288 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 } else {
290 /* Locking pain */
David Woodhouseced22072008-04-22 15:13:40 +0100291 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000293
David Woodhouse9fe48542006-05-23 00:38:06 +0100294 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
295 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
David Woodhouseced22072008-04-22 15:13:40 +0100296 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100300 flash_ofs = write_ofs(c);
Joe Perches9c261b32012-02-15 15:56:43 -0800301 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n",
302 flash_ofs);
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100303 jffs2_dbg_acct_sanity_check(c,jeb);
304 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto retry;
306 }
Joe Perches9c261b32012-02-15 15:56:43 -0800307 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
308 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 /* Release the full_dnode which is now useless, and return */
311 jffs2_free_full_dirent(fd);
312 return ERR_PTR(ret?ret:-EIO);
313 }
314 /* Mark the space used */
David Woodhouse71c23392007-06-29 13:39:57 +0100315 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
316 PAD(sizeof(*rd)+namelen), f->inocache);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200317 if (IS_ERR(fd->raw)) {
318 void *hold_err = fd->raw;
319 /* Release the full_dirent which is now useless, and return */
320 jffs2_free_full_dirent(fd);
David Howellse231c2e2008-02-07 00:15:26 -0800321 return ERR_CAST(hold_err);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100325 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 return fd;
329}
330
331/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
332 we don't have to go digging in struct inode or its equivalent. It should set:
333 mode, uid, gid, (starting)isize, atime, ctime, mtime */
334int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000335 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 uint32_t offset, uint32_t writelen, uint32_t *retlen)
337{
338 int ret = 0;
339 uint32_t writtenlen = 0;
340
Joe Perches9c261b32012-02-15 15:56:43 -0800341 jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
342 __func__, f->inocache->ino, offset, writelen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 while(writelen) {
345 struct jffs2_full_dnode *fn;
346 unsigned char *comprbuf = NULL;
347 uint16_t comprtype = JFFS2_COMPR_NONE;
David Woodhouse9fe48542006-05-23 00:38:06 +0100348 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 uint32_t datalen, cdatalen;
350 int retried = 0;
351
352 retry:
Joe Perches9c261b32012-02-15 15:56:43 -0800353 jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n",
354 writelen, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Woodhouse9fe48542006-05-23 00:38:06 +0100356 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100357 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800359 jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
David Woodhouseced22072008-04-22 15:13:40 +0100362 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
364 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
365
366 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
367
368 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
369 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
370 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
371 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
372
373 ri->ino = cpu_to_je32(f->inocache->ino);
374 ri->version = cpu_to_je32(++f->highest_version);
375 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
376 ri->offset = cpu_to_je32(offset);
377 ri->csize = cpu_to_je32(cdatalen);
378 ri->dsize = cpu_to_je32(datalen);
379 ri->compr = comprtype & 0xff;
380 ri->usercompr = (comprtype >> 8 ) & 0xff;
381 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
382 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
383
David Woodhouse9fe48542006-05-23 00:38:06 +0100384 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 jffs2_free_comprbuf(comprbuf, buf);
387
388 if (IS_ERR(fn)) {
389 ret = PTR_ERR(fn);
David Woodhouseced22072008-04-22 15:13:40 +0100390 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 jffs2_complete_reservation(c);
392 if (!retried) {
393 /* Write error to be retried */
394 retried = 1;
Joe Perches9c261b32012-02-15 15:56:43 -0800395 jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto retry;
397 }
398 break;
399 }
400 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
401 if (f->metadata) {
402 jffs2_mark_node_obsolete(c, f->metadata->raw);
403 jffs2_free_full_dnode(f->metadata);
404 f->metadata = NULL;
405 }
406 if (ret) {
407 /* Eep */
Joe Perches9c261b32012-02-15 15:56:43 -0800408 jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n",
409 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 jffs2_mark_node_obsolete(c, fn->raw);
411 jffs2_free_full_dnode(fn);
412
David Woodhouseced22072008-04-22 15:13:40 +0100413 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 jffs2_complete_reservation(c);
415 break;
416 }
David Woodhouseced22072008-04-22 15:13:40 +0100417 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 jffs2_complete_reservation(c);
419 if (!datalen) {
420 printk(KERN_WARNING "Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
421 ret = -EIO;
422 break;
423 }
Joe Perches9c261b32012-02-15 15:56:43 -0800424 jffs2_dbg(1, "increasing writtenlen by %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 writtenlen += datalen;
426 offset += datalen;
427 writelen -= datalen;
428 buf += datalen;
429 }
430 *retlen = writtenlen;
431 return ret;
432}
433
Eric Paris2a7dba32011-02-01 11:05:39 -0500434int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
435 struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
436 const struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct jffs2_raw_dirent *rd;
439 struct jffs2_full_dnode *fn;
440 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100441 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int ret;
443
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000444 /* Try to reserve enough space for both node and dirent.
445 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
David Woodhouse9fe48542006-05-23 00:38:06 +0100447 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100448 JFFS2_SUMMARY_INODE_SIZE);
Joe Perches9c261b32012-02-15 15:56:43 -0800449 jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen);
David Woodhouse590fe342008-05-01 15:53:28 +0100450 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ret;
David Woodhouse590fe342008-05-01 15:53:28 +0100452
453 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 ri->data_crc = cpu_to_je32(0);
456 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
457
David Woodhouse9fe48542006-05-23 00:38:06 +0100458 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Joe Perches9c261b32012-02-15 15:56:43 -0800460 jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n",
461 jemode_to_cpu(ri->mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (IS_ERR(fn)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800464 jffs2_dbg(1, "jffs2_write_dnode() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100466 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 jffs2_complete_reservation(c);
468 return PTR_ERR(fn);
469 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000470 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 obsoleted by the first data write
472 */
473 f->metadata = fn;
474
David Woodhouseced22072008-04-22 15:13:40 +0100475 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 jffs2_complete_reservation(c);
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900477
Eric Paris2a7dba32011-02-01 11:05:39 -0500478 ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900479 if (ret)
480 return ret;
481 ret = jffs2_init_acl_post(&f->vfs_inode);
482 if (ret)
483 return ret;
484
Eric Paris2a7dba32011-02-01 11:05:39 -0500485 ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
486 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (ret) {
489 /* Eep. */
Joe Perches9c261b32012-02-15 15:56:43 -0800490 jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return ret;
492 }
493
494 rd = jffs2_alloc_raw_dirent();
495 if (!rd) {
496 /* Argh. Now we treat it like a normal delete */
497 jffs2_complete_reservation(c);
498 return -ENOMEM;
499 }
500
David Woodhouseced22072008-04-22 15:13:40 +0100501 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
504 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
Eric Paris2a7dba32011-02-01 11:05:39 -0500505 rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
507
508 rd->pino = cpu_to_je32(dir_f->inocache->ino);
509 rd->version = cpu_to_je32(++dir_f->highest_version);
510 rd->ino = ri->ino;
511 rd->mctime = ri->ctime;
Eric Paris2a7dba32011-02-01 11:05:39 -0500512 rd->nsize = qstr->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 rd->type = DT_REG;
514 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Eric Paris2a7dba32011-02-01 11:05:39 -0500515 rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Eric Paris2a7dba32011-02-01 11:05:39 -0500517 fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000522 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 as if it were the final unlink() */
524 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100525 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return PTR_ERR(fd);
527 }
528
529 /* Link the fd into the inode's list, obsoleting an old
530 one if necessary. */
531 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
532
533 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100534 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 return 0;
537}
538
539
540int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100541 const char *name, int namelen, struct jffs2_inode_info *dead_f,
542 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct jffs2_raw_dirent *rd;
545 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100546 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int ret;
548
Joakim Tjernlunda4914862007-03-16 16:15:45 +0100549 if (!jffs2_can_mark_obsolete(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
551
552 rd = jffs2_alloc_raw_dirent();
553 if (!rd)
554 return -ENOMEM;
555
David Woodhouse9fe48542006-05-23 00:38:06 +0100556 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100557 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (ret) {
559 jffs2_free_raw_dirent(rd);
560 return ret;
561 }
562
David Woodhouseced22072008-04-22 15:13:40 +0100563 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Build a deletion node */
566 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
567 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
568 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
569 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 rd->pino = cpu_to_je32(dir_f->inocache->ino);
572 rd->version = cpu_to_je32(++dir_f->highest_version);
573 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100574 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 rd->nsize = namelen;
576 rd->type = DT_UNKNOWN;
577 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
578 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
579
David Woodhouse9fe48542006-05-23 00:38:06 +0100580 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 jffs2_free_raw_dirent(rd);
583
584 if (IS_ERR(fd)) {
585 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100586 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return PTR_ERR(fd);
588 }
589
590 /* File it. This will mark the old one obsolete. */
591 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
David Woodhouseced22072008-04-22 15:13:40 +0100592 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 uint32_t nhash = full_name_hash(name, namelen);
595
Harvey Harrisonbf667372008-04-18 13:44:14 -0700596 fd = dir_f->dents;
David Woodhouseb5748642007-08-20 11:05:29 +0100597 /* We don't actually want to reserve any space, but we do
598 want to be holding the alloc_sem when we write to flash */
David Woodhouseced22072008-04-22 15:13:40 +0100599 mutex_lock(&c->alloc_sem);
600 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
David Woodhouse15953582007-11-01 16:25:56 -0400602 for (fd = dir_f->dents; fd; fd = fd->next) {
603 if (fd->nhash == nhash &&
604 !memcmp(fd->name, name, namelen) &&
605 !fd->name[namelen]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Joe Perches9c261b32012-02-15 15:56:43 -0800607 jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n",
608 fd->ino, ref_offset(fd->raw));
David Woodhouse15953582007-11-01 16:25:56 -0400609 jffs2_mark_node_obsolete(c, fd->raw);
610 /* We don't want to remove it from the list immediately,
611 because that screws up getdents()/seek() semantics even
612 more than they're screwed already. Turn it into a
613 node-less deletion dirent instead -- a placeholder */
614 fd->raw = NULL;
615 fd->ino = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
David Woodhouseced22072008-04-22 15:13:40 +0100619 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 /* dead_f is NULL if this was a rename not a real unlink */
623 /* Also catch the !f->inocache case, where there was a dirent
624 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000625 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David Woodhouseced22072008-04-22 15:13:40 +0100627 mutex_lock(&dead_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000629 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
630 while (dead_f->dents) {
631 /* There can be only deleted ones */
632 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000633
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000634 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000635
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000636 if (fd->ino) {
637 printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
638 dead_f->inocache->ino, fd->name, fd->ino);
639 } else {
Joe Perches9c261b32012-02-15 15:56:43 -0800640 jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n",
641 fd->name,
642 dead_f->inocache->ino);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000643 }
David Woodhouse15953582007-11-01 16:25:56 -0400644 if (fd->raw)
645 jffs2_mark_node_obsolete(c, fd->raw);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000646 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
David Woodhouse27c72b02008-05-01 18:47:17 +0100648 dead_f->inocache->pino_nlink = 0;
649 } else
650 dead_f->inocache->pino_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* NB: Caller must set inode nlink if appropriate */
David Woodhouseced22072008-04-22 15:13:40 +0100652 mutex_unlock(&dead_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
655 jffs2_complete_reservation(c);
656
657 return 0;
658}
659
660
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100661int 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 -0700662{
663 struct jffs2_raw_dirent *rd;
664 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100665 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int ret;
667
668 rd = jffs2_alloc_raw_dirent();
669 if (!rd)
670 return -ENOMEM;
671
David Woodhouse9fe48542006-05-23 00:38:06 +0100672 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100673 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (ret) {
675 jffs2_free_raw_dirent(rd);
676 return ret;
677 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000678
David Woodhouseced22072008-04-22 15:13:40 +0100679 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /* Build a deletion node */
682 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
683 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
684 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
685 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
686
687 rd->pino = cpu_to_je32(dir_f->inocache->ino);
688 rd->version = cpu_to_je32(++dir_f->highest_version);
689 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100690 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rd->nsize = namelen;
692
693 rd->type = type;
694
695 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
696 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
697
David Woodhouse9fe48542006-05-23 00:38:06 +0100698 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 jffs2_free_raw_dirent(rd);
701
702 if (IS_ERR(fd)) {
703 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100704 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return PTR_ERR(fd);
706 }
707
708 /* File it. This will mark the old one obsolete. */
709 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
710
711 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100712 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 return 0;
715}