2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: write.c,v 1.30.2.2 2003/11/02 13:51:18 dwmw2 Exp $
38 #include <linux/kernel.h>
40 #include <linux/jffs2.h>
41 #include <linux/mtd/mtd.h>
43 #include <linux/crc32.h>
45 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
46 fill in the raw_inode while you're at it. */
47 struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri)
50 struct super_block *sb = dir_i->i_sb;
51 struct jffs2_inode_cache *ic;
52 struct jffs2_sb_info *c;
53 struct jffs2_inode_info *f;
55 D1(printk(KERN_DEBUG "jffs2_new_inode(): dir_i %ld, mode 0x%x\n", dir_i->i_ino, mode));
57 c = JFFS2_SB_INFO(sb);
58 memset(ri, 0, sizeof(*ri));
60 ic = jffs2_alloc_inode_cache();
62 return ERR_PTR(-ENOMEM);
64 memset(ic, 0, sizeof(*ic));
66 inode = new_inode(sb);
69 jffs2_free_inode_cache(ic);
70 return ERR_PTR(-ENOMEM);
73 /* Alloc jffs2_inode_info when that's split in 2.5 */
75 f = JFFS2_INODE_INFO(inode);
76 memset(f, 0, sizeof(*f));
77 init_MUTEX_LOCKED(&f->sem);
79 inode->i_nlink = f->inocache->nlink = 1;
80 f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
81 f->inocache->ino = ri->ino = inode->i_ino = ++c->highest_ino;
82 D1(printk(KERN_DEBUG "jffs2_new_inode(): Assigned ino# %d\n", ri->ino));
83 jffs2_add_ino_cache(c, f->inocache);
85 ri->magic = JFFS2_MAGIC_BITMASK;
86 ri->nodetype = JFFS2_NODETYPE_INODE;
87 ri->totlen = PAD(sizeof(*ri));
88 ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
90 f->highest_version = ri->version = 1;
91 ri->uid = current->fsuid;
92 if (dir_i->i_mode & S_ISGID) {
93 ri->gid = dir_i->i_gid;
97 ri->gid = current->fsgid;
99 inode->i_mode = ri->mode;
100 inode->i_gid = ri->gid;
101 inode->i_uid = ri->uid;
102 inode->i_atime = inode->i_ctime = inode->i_mtime =
103 ri->atime = ri->mtime = ri->ctime = CURRENT_TIME;
104 inode->i_blksize = PAGE_SIZE;
108 insert_inode_hash(inode);
113 /* This ought to be in core MTD code. All registered MTD devices
114 without writev should have this put in place. Bug the MTD
116 static int mtd_fake_writev(struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, size_t *retlen)
119 size_t totlen = 0, thislen;
122 for (i=0; i<count; i++) {
123 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
125 if (ret || thislen != vecs[i].iov_len)
127 to += vecs[i].iov_len;
135 static inline int mtd_writev(struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, size_t *retlen)
138 return mtd->writev(mtd,vecs,count,to,retlen);
140 return mtd_fake_writev(mtd, vecs, count, to, retlen);
143 static void writecheck(struct mtd_info *mtd, __u32 ofs)
145 unsigned char buf[16];
149 ret = mtd->read(mtd, ofs, 16, &retlen, buf);
150 if (ret && retlen != 16) {
151 D1(printk(KERN_DEBUG "read failed or short in writecheck(). ret %d, retlen %d\n", ret, retlen));
155 for (i=0; i<16; i++) {
160 printk(KERN_WARNING "ARGH. About to write node to 0x%08x on flash, but there's data already there:\n", ofs);
161 printk(KERN_WARNING "0x%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
163 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
164 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
171 /* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it,
172 write it to the flash, link it into the existing inode/fragment list */
174 struct jffs2_full_dnode *jffs2_write_dnode(struct inode *inode, struct jffs2_raw_inode *ri, const unsigned char *data, __u32 datalen, __u32 flash_ofs, __u32 *writelen)
177 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
178 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
179 struct jffs2_raw_node_ref *raw;
180 struct jffs2_full_dnode *fn;
182 struct iovec vecs[2];
185 D1(if(ri->hdr_crc != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
186 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
190 vecs[0].iov_base = ri;
191 vecs[0].iov_len = sizeof(*ri);
192 vecs[1].iov_base = (unsigned char *)data;
193 vecs[1].iov_len = datalen;
195 writecheck(c->mtd, flash_ofs);
197 if (ri->totlen != sizeof(*ri) + datalen) {
198 printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08x) + datalen (0x%08x)\n", ri->totlen, sizeof(*ri), datalen);
200 raw = jffs2_alloc_raw_node_ref();
202 return ERR_PTR(-ENOMEM);
204 fn = jffs2_alloc_full_dnode();
206 jffs2_free_raw_node_ref(raw);
207 return ERR_PTR(-ENOMEM);
209 raw->flash_offset = flash_ofs;
210 raw->totlen = PAD(ri->totlen);
211 raw->next_phys = NULL;
213 fn->ofs = ri->offset;
214 fn->size = ri->dsize;
218 ret = mtd_writev(c->mtd, vecs, 2, flash_ofs, &retlen);
219 if (ret || (retlen != sizeof(*ri) + datalen)) {
220 printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %d\n",
221 sizeof(*ri)+datalen, flash_ofs, ret, retlen);
222 /* Mark the space as dirtied */
224 /* Doesn't belong to any inode */
225 raw->next_in_ino = NULL;
227 /* Don't change raw->size to match retlen. We may have
228 written the node header already, and only the data will
229 seem corrupted, in which case the scan would skip over
230 any node we write before the original intended end of
232 jffs2_add_physical_node_ref(c, raw, sizeof(*ri)+datalen, 1);
233 jffs2_mark_node_obsolete(c, raw);
235 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
236 jffs2_free_raw_node_ref(raw);
239 /* Release the full_dnode which is now useless, and return */
240 jffs2_free_full_dnode(fn);
243 return ERR_PTR(ret?ret:-EIO);
245 /* Mark the space used */
246 jffs2_add_physical_node_ref(c, raw, retlen, 0);
248 /* Link into per-inode list */
249 raw->next_in_ino = f->inocache->nodes;
250 f->inocache->nodes = raw;
252 D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n", flash_ofs, ri->dsize, ri->csize, ri->node_crc, ri->data_crc, ri->totlen));
256 f->inocache->nodes = raw;
260 struct jffs2_full_dirent *jffs2_write_dirent(struct inode *inode, struct jffs2_raw_dirent *rd, const unsigned char *name, __u32 namelen, __u32 flash_ofs, __u32 *writelen)
262 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
263 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
264 struct jffs2_raw_node_ref *raw;
265 struct jffs2_full_dirent *fd;
267 struct iovec vecs[2];
270 D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name at *0x%p \"%s\"->ino #%u, name_crc 0x%08x)\n", rd->pino, name, name, rd->ino, rd->name_crc));
271 writecheck(c->mtd, flash_ofs);
273 D1(if(rd->hdr_crc != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
274 printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
279 vecs[0].iov_base = rd;
280 vecs[0].iov_len = sizeof(*rd);
281 vecs[1].iov_base = (unsigned char *)name;
282 vecs[1].iov_len = namelen;
284 raw = jffs2_alloc_raw_node_ref();
287 return ERR_PTR(-ENOMEM);
289 fd = jffs2_alloc_full_dirent(namelen+1);
291 jffs2_free_raw_node_ref(raw);
292 return ERR_PTR(-ENOMEM);
294 raw->flash_offset = flash_ofs;
295 raw->totlen = PAD(rd->totlen);
296 raw->next_in_ino = f->inocache->nodes;
297 f->inocache->nodes = raw;
298 raw->next_phys = NULL;
300 fd->version = rd->version;
302 fd->nhash = full_name_hash(name, strlen(name));
304 memcpy(fd->name, name, namelen);
308 ret = mtd_writev(c->mtd, vecs, 2, flash_ofs, &retlen);
309 if (ret || (retlen != sizeof(*rd) + namelen)) {
310 printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %d\n",
311 sizeof(*rd)+namelen, flash_ofs, ret, retlen);
312 /* Mark the space as dirtied */
314 jffs2_add_physical_node_ref(c, raw, sizeof(*rd)+namelen, 1);
315 jffs2_mark_node_obsolete(c, raw);
317 printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
318 jffs2_free_raw_node_ref(raw);
321 /* Release the full_dnode which is now useless, and return */
322 jffs2_free_full_dirent(fd);
325 return ERR_PTR(ret?ret:-EIO);
327 /* Mark the space used */
328 jffs2_add_physical_node_ref(c, raw, retlen, 0);
332 f->inocache->nodes = raw;