blob: d98b22036738aeb9871c6511a97737956025e41e [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)) {
Joe Perchesda320f02012-02-15 15:56:44 -080071 pr_crit("Eep. CRC not correct in jffs2_write_dnode()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 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) {
Joe Perchesda320f02012-02-15 15:56:44 -080081 pr_warn("%s(): ri->totlen (0x%08x) != sizeof(*ri) (0x%08zx) + datalen (0x%08x)\n",
82 __func__, je32_to_cpu(ri->totlen),
83 sizeof(*ri), datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
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);
Joe Perches9c261b32012-02-15 15:56:43 -0800100 jffs2_dbg(1, "%s(): dnode_version %d, highest version %d -> updating dnode\n",
101 __func__,
102 je32_to_cpu(ri->version), f->highest_version);
Estelle Hammache9b88f472005-01-28 18:53:05 +0000103 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)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800111 pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
112 sizeof(*ri) + datalen, flash_ofs, ret, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
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 {
Joe Perchesda320f02012-02-15 15:56:44 -0800123 pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
124 flash_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
David Woodhouse2f785402006-05-24 02:04:45 +0100126 if (!retried && alloc_mode != ALLOC_NORETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* Try to reallocate space and retry */
128 uint32_t dummy;
129 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
130
131 retried = 1;
132
Joe Perches9c261b32012-02-15 15:56:43 -0800133 jffs2_dbg(1, "Retrying failed write.\n");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000134
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100135 jffs2_dbg_acct_sanity_check(c,jeb);
136 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100139 ret = jffs2_reserve_space_gc(c, sizeof(*ri) + datalen, &dummy,
140 JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 } else {
142 /* Locking pain */
David Woodhouseced22072008-04-22 15:13:40 +0100143 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000145
David Woodhouse9fe48542006-05-23 00:38:06 +0100146 ret = jffs2_reserve_space(c, sizeof(*ri) + datalen, &dummy,
147 alloc_mode, JFFS2_SUMMARY_INODE_SIZE);
David Woodhouseced22072008-04-22 15:13:40 +0100148 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
151 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100152 flash_ofs = write_ofs(c);
Joe Perches9c261b32012-02-15 15:56:43 -0800153 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write.\n",
154 flash_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100156 jffs2_dbg_acct_sanity_check(c,jeb);
157 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 goto retry;
160 }
Joe Perches9c261b32012-02-15 15:56:43 -0800161 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
162 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 /* Release the full_dnode which is now useless, and return */
165 jffs2_free_full_dnode(fn);
166 return ERR_PTR(ret?ret:-EIO);
167 }
168 /* Mark the space used */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000169 /* If node covers at least a whole page, or if it starts at the
170 beginning of a page and runs to the end of the file, or if
171 it's a hole node, mark it REF_PRISTINE, else REF_NORMAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173 if ((je32_to_cpu(ri->dsize) >= PAGE_CACHE_SIZE) ||
174 ( ((je32_to_cpu(ri->offset)&(PAGE_CACHE_SIZE-1))==0) &&
175 (je32_to_cpu(ri->dsize)+je32_to_cpu(ri->offset) == je32_to_cpu(ri->isize)))) {
David Woodhouse2f785402006-05-24 02:04:45 +0100176 flash_ofs |= REF_PRISTINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 } else {
David Woodhouse2f785402006-05-24 02:04:45 +0100178 flash_ofs |= REF_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
David Woodhouse2f785402006-05-24 02:04:45 +0100180 fn->raw = jffs2_add_physical_node_ref(c, flash_ofs, PAD(sizeof(*ri)+datalen), f->inocache);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200181 if (IS_ERR(fn->raw)) {
182 void *hold_err = fn->raw;
183 /* Release the full_dnode which is now useless, and return */
184 jffs2_free_full_dnode(fn);
David Howellse231c2e2008-02-07 00:15:26 -0800185 return ERR_CAST(hold_err);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200186 }
David Woodhouse2f785402006-05-24 02:04:45 +0100187 fn->ofs = je32_to_cpu(ri->offset);
188 fn->size = je32_to_cpu(ri->dsize);
189 fn->frags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Joe Perches9c261b32012-02-15 15:56:43 -0800191 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 +0100192 flash_ofs & ~3, flash_ofs & 3, je32_to_cpu(ri->dsize),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 je32_to_cpu(ri->csize), je32_to_cpu(ri->node_crc),
Joe Perches9c261b32012-02-15 15:56:43 -0800194 je32_to_cpu(ri->data_crc), je32_to_cpu(ri->totlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100197 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 return fn;
201}
202
David Woodhouse9fe48542006-05-23 00:38:06 +0100203struct jffs2_full_dirent *jffs2_write_dirent(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
204 struct jffs2_raw_dirent *rd, const unsigned char *name,
205 uint32_t namelen, int alloc_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct jffs2_full_dirent *fd;
208 size_t retlen;
209 struct kvec vecs[2];
David Woodhouse2f785402006-05-24 02:04:45 +0100210 uint32_t flash_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int retried = 0;
212 int ret;
213
Joe Perches9c261b32012-02-15 15:56:43 -0800214 jffs2_dbg(1, "%s(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n",
215 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
Joe Perches9c261b32012-02-15 15:56:43 -0800217 je32_to_cpu(rd->name_crc));
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 D1(if(je32_to_cpu(rd->hdr_crc) != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800220 pr_crit("Eep. CRC not correct in jffs2_write_dirent()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 BUG();
David Woodhouse2f785402006-05-24 02:04:45 +0100222 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David Woodhouse69ca4372007-10-13 11:33:50 +0100224 if (strnlen(name, namelen) != namelen) {
225 /* This should never happen, but seems to have done on at least one
226 occasion: https://dev.laptop.org/ticket/4184 */
Joe Perchesda320f02012-02-15 15:56:44 -0800227 pr_crit("Error in jffs2_write_dirent() -- name contains zero bytes!\n");
228 pr_crit("Directory inode #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x\n",
229 je32_to_cpu(rd->pino), name, name, je32_to_cpu(rd->ino),
230 je32_to_cpu(rd->name_crc));
David Woodhouse69ca4372007-10-13 11:33:50 +0100231 WARN_ON(1);
232 return ERR_PTR(-EIO);
233 }
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 vecs[0].iov_base = rd;
236 vecs[0].iov_len = sizeof(*rd);
237 vecs[1].iov_base = (unsigned char *)name;
238 vecs[1].iov_len = namelen;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 fd = jffs2_alloc_full_dirent(namelen+1);
David Woodhouse2f785402006-05-24 02:04:45 +0100241 if (!fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 fd->version = je32_to_cpu(rd->version);
245 fd->ino = je32_to_cpu(rd->ino);
David Woodhouse69ca4372007-10-13 11:33:50 +0100246 fd->nhash = full_name_hash(name, namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 fd->type = rd->type;
248 memcpy(fd->name, name, namelen);
249 fd->name[namelen]=0;
250
251 retry:
David Woodhouse2f785402006-05-24 02:04:45 +0100252 flash_ofs = write_ofs(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
David Woodhouse2f785402006-05-24 02:04:45 +0100254 jffs2_dbg_prewrite_paranoia_check(c, flash_ofs, vecs[0].iov_len + vecs[1].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Estelle Hammache9b88f472005-01-28 18:53:05 +0000256 if ((alloc_mode!=ALLOC_GC) && (je32_to_cpu(rd->version) < f->highest_version)) {
257 BUG_ON(!retried);
Joe Perches9c261b32012-02-15 15:56:43 -0800258 jffs2_dbg(1, "%s(): dirent_version %d, highest version %d -> updating dirent\n",
259 __func__,
260 je32_to_cpu(rd->version), f->highest_version);
Estelle Hammache9b88f472005-01-28 18:53:05 +0000261 rd->version = cpu_to_je32(++f->highest_version);
262 fd->version = je32_to_cpu(rd->version);
263 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Estelle Hammachee4803c32005-01-24 21:13:42 +0000264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 ret = jffs2_flash_writev(c, vecs, 2, flash_ofs, &retlen,
267 (alloc_mode==ALLOC_GC)?0:je32_to_cpu(rd->pino));
268 if (ret || (retlen != sizeof(*rd) + namelen)) {
Joe Perchesda320f02012-02-15 15:56:44 -0800269 pr_notice("Write of %zd bytes at 0x%08x failed. returned %d, retlen %zd\n",
270 sizeof(*rd) + namelen, flash_ofs, ret, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Mark the space as dirtied */
272 if (retlen) {
David Woodhouse2f785402006-05-24 02:04:45 +0100273 jffs2_add_physical_node_ref(c, flash_ofs | REF_OBSOLETE, PAD(sizeof(*rd)+namelen), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 } else {
Joe Perchesda320f02012-02-15 15:56:44 -0800275 pr_notice("Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n",
276 flash_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
David Woodhouse2f785402006-05-24 02:04:45 +0100278 if (!retried) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* Try to reallocate space and retry */
280 uint32_t dummy;
281 struct jffs2_eraseblock *jeb = &c->blocks[flash_ofs / c->sector_size];
282
283 retried = 1;
284
Joe Perches9c261b32012-02-15 15:56:43 -0800285 jffs2_dbg(1, "Retrying failed write.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100287 jffs2_dbg_acct_sanity_check(c,jeb);
288 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (alloc_mode == ALLOC_GC) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100291 ret = jffs2_reserve_space_gc(c, sizeof(*rd) + namelen, &dummy,
292 JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 } else {
294 /* Locking pain */
David Woodhouseced22072008-04-22 15:13:40 +0100295 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 jffs2_complete_reservation(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000297
David Woodhouse9fe48542006-05-23 00:38:06 +0100298 ret = jffs2_reserve_space(c, sizeof(*rd) + namelen, &dummy,
299 alloc_mode, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
David Woodhouseced22072008-04-22 15:13:40 +0100300 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 if (!ret) {
David Woodhouse9fe48542006-05-23 00:38:06 +0100304 flash_ofs = write_ofs(c);
Joe Perches9c261b32012-02-15 15:56:43 -0800305 jffs2_dbg(1, "Allocated space at 0x%08x to retry failed write\n",
306 flash_ofs);
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100307 jffs2_dbg_acct_sanity_check(c,jeb);
308 jffs2_dbg_acct_paranoia_check(c, jeb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto retry;
310 }
Joe Perches9c261b32012-02-15 15:56:43 -0800311 jffs2_dbg(1, "Failed to allocate space to retry failed write: %d!\n",
312 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 /* Release the full_dnode which is now useless, and return */
315 jffs2_free_full_dirent(fd);
316 return ERR_PTR(ret?ret:-EIO);
317 }
318 /* Mark the space used */
David Woodhouse71c23392007-06-29 13:39:57 +0100319 fd->raw = jffs2_add_physical_node_ref(c, flash_ofs | dirent_node_state(rd),
320 PAD(sizeof(*rd)+namelen), f->inocache);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200321 if (IS_ERR(fd->raw)) {
322 void *hold_err = fd->raw;
323 /* Release the full_dirent which is now useless, and return */
324 jffs2_free_full_dirent(fd);
David Howellse231c2e2008-02-07 00:15:26 -0800325 return ERR_CAST(hold_err);
Joakim Tjernlund5bd5c032007-06-24 19:22:29 +0200326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (retried) {
Artem B. Bityutskiy730554d2005-07-17 07:56:26 +0100329 jffs2_dbg_acct_sanity_check(c,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 return fd;
333}
334
335/* The OS-specific code fills in the metadata in the jffs2_raw_inode for us, so that
336 we don't have to go digging in struct inode or its equivalent. It should set:
337 mode, uid, gid, (starting)isize, atime, ctime, mtime */
338int jffs2_write_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000339 struct jffs2_raw_inode *ri, unsigned char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 uint32_t offset, uint32_t writelen, uint32_t *retlen)
341{
342 int ret = 0;
343 uint32_t writtenlen = 0;
344
Joe Perches9c261b32012-02-15 15:56:43 -0800345 jffs2_dbg(1, "%s(): Ino #%u, ofs 0x%x, len 0x%x\n",
346 __func__, f->inocache->ino, offset, writelen);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 while(writelen) {
349 struct jffs2_full_dnode *fn;
350 unsigned char *comprbuf = NULL;
351 uint16_t comprtype = JFFS2_COMPR_NONE;
David Woodhouse9fe48542006-05-23 00:38:06 +0100352 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 uint32_t datalen, cdatalen;
354 int retried = 0;
355
356 retry:
Joe Perches9c261b32012-02-15 15:56:43 -0800357 jffs2_dbg(2, "jffs2_commit_write() loop: 0x%x to write to 0x%x\n",
358 writelen, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Woodhouse9fe48542006-05-23 00:38:06 +0100360 ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100361 &alloclen, ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800363 jffs2_dbg(1, "jffs2_reserve_space returned %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 }
David Woodhouseced22072008-04-22 15:13:40 +0100366 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 datalen = min_t(uint32_t, writelen, PAGE_CACHE_SIZE - (offset & (PAGE_CACHE_SIZE-1)));
368 cdatalen = min_t(uint32_t, alloclen - sizeof(*ri), datalen);
369
370 comprtype = jffs2_compress(c, f, buf, &comprbuf, &datalen, &cdatalen);
371
372 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
373 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
374 ri->totlen = cpu_to_je32(sizeof(*ri) + cdatalen);
375 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
376
377 ri->ino = cpu_to_je32(f->inocache->ino);
378 ri->version = cpu_to_je32(++f->highest_version);
379 ri->isize = cpu_to_je32(max(je32_to_cpu(ri->isize), offset + datalen));
380 ri->offset = cpu_to_je32(offset);
381 ri->csize = cpu_to_je32(cdatalen);
382 ri->dsize = cpu_to_je32(datalen);
383 ri->compr = comprtype & 0xff;
384 ri->usercompr = (comprtype >> 8 ) & 0xff;
385 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
386 ri->data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
387
David Woodhouse9fe48542006-05-23 00:38:06 +0100388 fn = jffs2_write_dnode(c, f, ri, comprbuf, cdatalen, ALLOC_NORETRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 jffs2_free_comprbuf(comprbuf, buf);
391
392 if (IS_ERR(fn)) {
393 ret = PTR_ERR(fn);
David Woodhouseced22072008-04-22 15:13:40 +0100394 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 jffs2_complete_reservation(c);
396 if (!retried) {
397 /* Write error to be retried */
398 retried = 1;
Joe Perches9c261b32012-02-15 15:56:43 -0800399 jffs2_dbg(1, "Retrying node write in jffs2_write_inode_range()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto retry;
401 }
402 break;
403 }
404 ret = jffs2_add_full_dnode_to_inode(c, f, fn);
405 if (f->metadata) {
406 jffs2_mark_node_obsolete(c, f->metadata->raw);
407 jffs2_free_full_dnode(f->metadata);
408 f->metadata = NULL;
409 }
410 if (ret) {
411 /* Eep */
Joe Perches9c261b32012-02-15 15:56:43 -0800412 jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n",
413 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 jffs2_mark_node_obsolete(c, fn->raw);
415 jffs2_free_full_dnode(fn);
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 break;
420 }
David Woodhouseced22072008-04-22 15:13:40 +0100421 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 jffs2_complete_reservation(c);
423 if (!datalen) {
Joe Perchesda320f02012-02-15 15:56:44 -0800424 pr_warn("Eep. We didn't actually write any data in jffs2_write_inode_range()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 ret = -EIO;
426 break;
427 }
Joe Perches9c261b32012-02-15 15:56:43 -0800428 jffs2_dbg(1, "increasing writtenlen by %d\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 writtenlen += datalen;
430 offset += datalen;
431 writelen -= datalen;
432 buf += datalen;
433 }
434 *retlen = writtenlen;
435 return ret;
436}
437
Eric Paris2a7dba32011-02-01 11:05:39 -0500438int jffs2_do_create(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
439 struct jffs2_inode_info *f, struct jffs2_raw_inode *ri,
440 const struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct jffs2_raw_dirent *rd;
443 struct jffs2_full_dnode *fn;
444 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100445 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int ret;
447
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000448 /* Try to reserve enough space for both node and dirent.
449 * Just the node will do for now, though
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
David Woodhouse9fe48542006-05-23 00:38:06 +0100451 ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100452 JFFS2_SUMMARY_INODE_SIZE);
Joe Perches9c261b32012-02-15 15:56:43 -0800453 jffs2_dbg(1, "%s(): reserved 0x%x bytes\n", __func__, alloclen);
David Woodhouse590fe342008-05-01 15:53:28 +0100454 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return ret;
David Woodhouse590fe342008-05-01 15:53:28 +0100456
457 mutex_lock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 ri->data_crc = cpu_to_je32(0);
460 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
461
David Woodhouse9fe48542006-05-23 00:38:06 +0100462 fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Joe Perches9c261b32012-02-15 15:56:43 -0800464 jffs2_dbg(1, "jffs2_do_create created file with mode 0x%x\n",
465 jemode_to_cpu(ri->mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 if (IS_ERR(fn)) {
Joe Perches9c261b32012-02-15 15:56:43 -0800468 jffs2_dbg(1, "jffs2_write_dnode() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Eeek. Wave bye bye */
David Woodhouseced22072008-04-22 15:13:40 +0100470 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 jffs2_complete_reservation(c);
472 return PTR_ERR(fn);
473 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000474 /* No data here. Only a metadata node, which will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 obsoleted by the first data write
476 */
477 f->metadata = fn;
478
David Woodhouseced22072008-04-22 15:13:40 +0100479 mutex_unlock(&f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 jffs2_complete_reservation(c);
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900481
Eric Paris2a7dba32011-02-01 11:05:39 -0500482 ret = jffs2_init_security(&f->vfs_inode, &dir_f->vfs_inode, qstr);
KaiGai Koheicfc8dc62007-09-14 15:16:35 +0900483 if (ret)
484 return ret;
485 ret = jffs2_init_acl_post(&f->vfs_inode);
486 if (ret)
487 return ret;
488
Eric Paris2a7dba32011-02-01 11:05:39 -0500489 ret = jffs2_reserve_space(c, sizeof(*rd)+qstr->len, &alloclen,
490 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(qstr->len));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (ret) {
493 /* Eep. */
Joe Perches9c261b32012-02-15 15:56:43 -0800494 jffs2_dbg(1, "jffs2_reserve_space() for dirent failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return ret;
496 }
497
498 rd = jffs2_alloc_raw_dirent();
499 if (!rd) {
500 /* Argh. Now we treat it like a normal delete */
501 jffs2_complete_reservation(c);
502 return -ENOMEM;
503 }
504
David Woodhouseced22072008-04-22 15:13:40 +0100505 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
508 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
Eric Paris2a7dba32011-02-01 11:05:39 -0500509 rd->totlen = cpu_to_je32(sizeof(*rd) + qstr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
511
512 rd->pino = cpu_to_je32(dir_f->inocache->ino);
513 rd->version = cpu_to_je32(++dir_f->highest_version);
514 rd->ino = ri->ino;
515 rd->mctime = ri->ctime;
Eric Paris2a7dba32011-02-01 11:05:39 -0500516 rd->nsize = qstr->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 rd->type = DT_REG;
518 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
Eric Paris2a7dba32011-02-01 11:05:39 -0500519 rd->name_crc = cpu_to_je32(crc32(0, qstr->name, qstr->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Eric Paris2a7dba32011-02-01 11:05:39 -0500521 fd = jffs2_write_dirent(c, dir_f, rd, qstr->name, qstr->len, ALLOC_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 jffs2_free_raw_dirent(rd);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (IS_ERR(fd)) {
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000526 /* dirent failed to write. Delete the inode normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 as if it were the final unlink() */
528 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100529 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return PTR_ERR(fd);
531 }
532
533 /* Link the fd into the inode's list, obsoleting an old
534 one if necessary. */
535 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
536
537 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100538 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 return 0;
541}
542
543
544int jffs2_do_unlink(struct jffs2_sb_info *c, struct jffs2_inode_info *dir_f,
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100545 const char *name, int namelen, struct jffs2_inode_info *dead_f,
546 uint32_t time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct jffs2_raw_dirent *rd;
549 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100550 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int ret;
552
Joakim Tjernlunda4914862007-03-16 16:15:45 +0100553 if (!jffs2_can_mark_obsolete(c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* We can't mark stuff obsolete on the medium. We need to write a deletion dirent */
555
556 rd = jffs2_alloc_raw_dirent();
557 if (!rd)
558 return -ENOMEM;
559
David Woodhouse9fe48542006-05-23 00:38:06 +0100560 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100561 ALLOC_DELETION, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (ret) {
563 jffs2_free_raw_dirent(rd);
564 return ret;
565 }
566
David Woodhouseced22072008-04-22 15:13:40 +0100567 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Build a deletion node */
570 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
571 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
572 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
573 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 rd->pino = cpu_to_je32(dir_f->inocache->ino);
576 rd->version = cpu_to_je32(++dir_f->highest_version);
577 rd->ino = cpu_to_je32(0);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100578 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 rd->nsize = namelen;
580 rd->type = DT_UNKNOWN;
581 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
582 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
583
David Woodhouse9fe48542006-05-23 00:38:06 +0100584 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_DELETION);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 jffs2_free_raw_dirent(rd);
587
588 if (IS_ERR(fd)) {
589 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100590 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return PTR_ERR(fd);
592 }
593
594 /* File it. This will mark the old one obsolete. */
595 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
David Woodhouseced22072008-04-22 15:13:40 +0100596 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 uint32_t nhash = full_name_hash(name, namelen);
599
Harvey Harrisonbf667372008-04-18 13:44:14 -0700600 fd = dir_f->dents;
David Woodhouseb5748642007-08-20 11:05:29 +0100601 /* We don't actually want to reserve any space, but we do
602 want to be holding the alloc_sem when we write to flash */
David Woodhouseced22072008-04-22 15:13:40 +0100603 mutex_lock(&c->alloc_sem);
604 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
David Woodhouse15953582007-11-01 16:25:56 -0400606 for (fd = dir_f->dents; fd; fd = fd->next) {
607 if (fd->nhash == nhash &&
608 !memcmp(fd->name, name, namelen) &&
609 !fd->name[namelen]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Joe Perches9c261b32012-02-15 15:56:43 -0800611 jffs2_dbg(1, "Marking old dirent node (ino #%u) @%08x obsolete\n",
612 fd->ino, ref_offset(fd->raw));
David Woodhouse15953582007-11-01 16:25:56 -0400613 jffs2_mark_node_obsolete(c, fd->raw);
614 /* We don't want to remove it from the list immediately,
615 because that screws up getdents()/seek() semantics even
616 more than they're screwed already. Turn it into a
617 node-less deletion dirent instead -- a placeholder */
618 fd->raw = NULL;
619 fd->ino = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
David Woodhouseced22072008-04-22 15:13:40 +0100623 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
626 /* dead_f is NULL if this was a rename not a real unlink */
627 /* Also catch the !f->inocache case, where there was a dirent
628 pointing to an inode which didn't exist. */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000629 if (dead_f && dead_f->inocache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
David Woodhouseced22072008-04-22 15:13:40 +0100631 mutex_lock(&dead_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000633 if (S_ISDIR(OFNI_EDONI_2SFFJ(dead_f)->i_mode)) {
634 while (dead_f->dents) {
635 /* There can be only deleted ones */
636 fd = dead_f->dents;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000637
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000638 dead_f->dents = fd->next;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000639
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000640 if (fd->ino) {
Joe Perchesda320f02012-02-15 15:56:44 -0800641 pr_warn("Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
642 dead_f->inocache->ino,
643 fd->name, fd->ino);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000644 } else {
Joe Perches9c261b32012-02-15 15:56:43 -0800645 jffs2_dbg(1, "Removing deletion dirent for \"%s\" from dir ino #%u\n",
646 fd->name,
647 dead_f->inocache->ino);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000648 }
David Woodhouse15953582007-11-01 16:25:56 -0400649 if (fd->raw)
650 jffs2_mark_node_obsolete(c, fd->raw);
Artem B. Bityuckiy32f1a952005-03-01 10:50:52 +0000651 jffs2_free_full_dirent(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
David Woodhouse27c72b02008-05-01 18:47:17 +0100653 dead_f->inocache->pino_nlink = 0;
654 } else
655 dead_f->inocache->pino_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* NB: Caller must set inode nlink if appropriate */
David Woodhouseced22072008-04-22 15:13:40 +0100657 mutex_unlock(&dead_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
660 jffs2_complete_reservation(c);
661
662 return 0;
663}
664
665
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100666int 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 -0700667{
668 struct jffs2_raw_dirent *rd;
669 struct jffs2_full_dirent *fd;
David Woodhouse9fe48542006-05-23 00:38:06 +0100670 uint32_t alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int ret;
672
673 rd = jffs2_alloc_raw_dirent();
674 if (!rd)
675 return -ENOMEM;
676
David Woodhouse9fe48542006-05-23 00:38:06 +0100677 ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100678 ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (ret) {
680 jffs2_free_raw_dirent(rd);
681 return ret;
682 }
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000683
David Woodhouseced22072008-04-22 15:13:40 +0100684 mutex_lock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Build a deletion node */
687 rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
688 rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
689 rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
690 rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
691
692 rd->pino = cpu_to_je32(dir_f->inocache->ino);
693 rd->version = cpu_to_je32(++dir_f->highest_version);
694 rd->ino = cpu_to_je32(ino);
Artem B. Bityutskiy3a69e0c2005-08-17 14:46:26 +0100695 rd->mctime = cpu_to_je32(time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rd->nsize = namelen;
697
698 rd->type = type;
699
700 rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
701 rd->name_crc = cpu_to_je32(crc32(0, name, namelen));
702
David Woodhouse9fe48542006-05-23 00:38:06 +0100703 fd = jffs2_write_dirent(c, dir_f, rd, name, namelen, ALLOC_NORMAL);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 jffs2_free_raw_dirent(rd);
706
707 if (IS_ERR(fd)) {
708 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100709 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return PTR_ERR(fd);
711 }
712
713 /* File it. This will mark the old one obsolete. */
714 jffs2_add_fd_to_list(c, fd, &dir_f->dents);
715
716 jffs2_complete_reservation(c);
David Woodhouseced22072008-04-22 15:13:40 +0100717 mutex_unlock(&dir_f->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return 0;
720}