2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: fs.c,v 1.66 2005/09/27 13:17:29 dedekind Exp $
14 #include <linux/capability.h>
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
19 #include <linux/list.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/pagemap.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/vfs.h>
25 #include <linux/crc32.h>
28 static int jffs2_flash_setup(struct jffs2_sb_info *c);
30 static int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
32 struct jffs2_full_dnode *old_metadata, *new_metadata;
33 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
34 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
35 struct jffs2_raw_inode *ri;
37 unsigned char *mdata = NULL;
40 uint32_t phys_ofs, alloclen;
42 D1(printk(KERN_DEBUG "jffs2_setattr(): ino #%lu\n", inode->i_ino));
43 ret = inode_change_ok(inode, iattr);
47 /* Special cases - we don't want more than one data node
48 for these types on the medium at any time. So setattr
49 must read the original data associated with the node
50 (i.e. the device numbers or the target name) and write
51 it out again with the appropriate data attached */
52 if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
53 /* For these, we don't actually need to read the old node */
54 dev = old_encode_dev(inode->i_rdev);
56 mdatalen = sizeof(dev);
57 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of kdev_t\n", mdatalen));
58 } else if (S_ISLNK(inode->i_mode)) {
59 mdatalen = f->metadata->size;
60 mdata = kmalloc(f->metadata->size, GFP_USER);
63 ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
68 D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of symlink target\n", mdatalen));
71 ri = jffs2_alloc_raw_inode();
73 if (S_ISLNK(inode->i_mode))
78 ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &phys_ofs, &alloclen,
79 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
81 jffs2_free_raw_inode(ri);
82 if (S_ISLNK(inode->i_mode & S_IFMT))
87 ivalid = iattr->ia_valid;
89 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
90 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
91 ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
92 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
94 ri->ino = cpu_to_je32(inode->i_ino);
95 ri->version = cpu_to_je32(++f->highest_version);
97 ri->uid = cpu_to_je16((ivalid & ATTR_UID)?iattr->ia_uid:inode->i_uid);
98 ri->gid = cpu_to_je16((ivalid & ATTR_GID)?iattr->ia_gid:inode->i_gid);
100 if (ivalid & ATTR_MODE)
101 if (iattr->ia_mode & S_ISGID &&
102 !in_group_p(je16_to_cpu(ri->gid)) && !capable(CAP_FSETID))
103 ri->mode = cpu_to_jemode(iattr->ia_mode & ~S_ISGID);
105 ri->mode = cpu_to_jemode(iattr->ia_mode);
107 ri->mode = cpu_to_jemode(inode->i_mode);
110 ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
111 ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
112 ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
113 ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
115 ri->offset = cpu_to_je32(0);
116 ri->csize = ri->dsize = cpu_to_je32(mdatalen);
117 ri->compr = JFFS2_COMPR_NONE;
118 if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
119 /* It's an extension. Make it a hole node */
120 ri->compr = JFFS2_COMPR_ZERO;
121 ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
122 ri->offset = cpu_to_je32(inode->i_size);
124 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
126 ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
128 ri->data_crc = cpu_to_je32(0);
130 new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, phys_ofs, ALLOC_NORMAL);
131 if (S_ISLNK(inode->i_mode))
134 if (IS_ERR(new_metadata)) {
135 jffs2_complete_reservation(c);
136 jffs2_free_raw_inode(ri);
138 return PTR_ERR(new_metadata);
140 /* It worked. Update the inode */
141 inode->i_atime = ITIME(je32_to_cpu(ri->atime));
142 inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
143 inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
144 inode->i_mode = jemode_to_cpu(ri->mode);
145 inode->i_uid = je16_to_cpu(ri->uid);
146 inode->i_gid = je16_to_cpu(ri->gid);
149 old_metadata = f->metadata;
151 if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
152 jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
154 if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
155 jffs2_add_full_dnode_to_inode(c, f, new_metadata);
156 inode->i_size = iattr->ia_size;
159 f->metadata = new_metadata;
162 jffs2_mark_node_obsolete(c, old_metadata->raw);
163 jffs2_free_full_dnode(old_metadata);
165 jffs2_free_raw_inode(ri);
168 jffs2_complete_reservation(c);
170 /* We have to do the vmtruncate() without f->sem held, since
171 some pages may be locked and waiting for it in readpage().
172 We are protected from a simultaneous write() extending i_size
173 back past iattr->ia_size, because do_truncate() holds the
174 generic inode semaphore. */
175 if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
176 vmtruncate(inode, iattr->ia_size);
181 int jffs2_setattr(struct dentry *dentry, struct iattr *iattr)
183 return jffs2_do_setattr(dentry->d_inode, iattr);
186 int jffs2_statfs(struct super_block *sb, struct kstatfs *buf)
188 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
191 buf->f_type = JFFS2_SUPER_MAGIC;
192 buf->f_bsize = 1 << PAGE_SHIFT;
193 buf->f_blocks = c->flash_size >> PAGE_SHIFT;
196 buf->f_namelen = JFFS2_MAX_NAME_LEN;
198 spin_lock(&c->erase_completion_lock);
199 avail = c->dirty_size + c->free_size;
200 if (avail > c->sector_size * c->resv_blocks_write)
201 avail -= c->sector_size * c->resv_blocks_write;
204 spin_unlock(&c->erase_completion_lock);
206 buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
212 void jffs2_clear_inode (struct inode *inode)
214 /* We can forget about this inode for now - drop all
215 * the nodelists associated with it, etc.
217 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
218 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
220 D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode));
222 jffs2_do_clear_inode(c, f);
225 void jffs2_read_inode (struct inode *inode)
227 struct jffs2_inode_info *f;
228 struct jffs2_sb_info *c;
229 struct jffs2_raw_inode latest_node;
232 D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
234 f = JFFS2_INODE_INFO(inode);
235 c = JFFS2_SB_INFO(inode->i_sb);
237 jffs2_init_inode_info(f);
240 ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
243 make_bad_inode(inode);
247 inode->i_mode = jemode_to_cpu(latest_node.mode);
248 inode->i_uid = je16_to_cpu(latest_node.uid);
249 inode->i_gid = je16_to_cpu(latest_node.gid);
250 inode->i_size = je32_to_cpu(latest_node.isize);
251 inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
252 inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
253 inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
255 inode->i_nlink = f->inocache->nlink;
257 inode->i_blksize = PAGE_SIZE;
258 inode->i_blocks = (inode->i_size + 511) >> 9;
260 switch (inode->i_mode & S_IFMT) {
264 inode->i_op = &jffs2_symlink_inode_operations;
269 struct jffs2_full_dirent *fd;
271 for (fd=f->dents; fd; fd = fd->next) {
272 if (fd->type == DT_DIR && fd->ino)
277 /* Root dir gets i_nlink 3 for some reason */
278 if (inode->i_ino == 1)
281 inode->i_op = &jffs2_dir_inode_operations;
282 inode->i_fop = &jffs2_dir_operations;
286 inode->i_op = &jffs2_file_inode_operations;
287 inode->i_fop = &jffs2_file_operations;
288 inode->i_mapping->a_ops = &jffs2_file_address_operations;
289 inode->i_mapping->nrpages = 0;
294 /* Read the device numbers from the media */
295 D1(printk(KERN_DEBUG "Reading device numbers from flash\n"));
296 if (jffs2_read_dnode(c, f, f->metadata, (char *)&rdev, 0, sizeof(rdev)) < 0) {
298 printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino);
300 jffs2_do_clear_inode(c, f);
301 make_bad_inode(inode);
307 inode->i_op = &jffs2_file_inode_operations;
308 init_special_inode(inode, inode->i_mode,
309 old_decode_dev((je16_to_cpu(rdev))));
313 printk(KERN_WARNING "jffs2_read_inode(): Bogus imode %o for ino %lu\n", inode->i_mode, (unsigned long)inode->i_ino);
318 D1(printk(KERN_DEBUG "jffs2_read_inode() returning\n"));
321 void jffs2_dirty_inode(struct inode *inode)
325 if (!(inode->i_state & I_DIRTY_DATASYNC)) {
326 D2(printk(KERN_DEBUG "jffs2_dirty_inode() not calling setattr() for ino #%lu\n", inode->i_ino));
330 D1(printk(KERN_DEBUG "jffs2_dirty_inode() calling setattr() for ino #%lu\n", inode->i_ino));
332 iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
333 iattr.ia_mode = inode->i_mode;
334 iattr.ia_uid = inode->i_uid;
335 iattr.ia_gid = inode->i_gid;
336 iattr.ia_atime = inode->i_atime;
337 iattr.ia_mtime = inode->i_mtime;
338 iattr.ia_ctime = inode->i_ctime;
340 jffs2_do_setattr(inode, &iattr);
343 int jffs2_remount_fs (struct super_block *sb, int *flags, char *data)
345 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
347 if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
350 /* We stop if it was running, then restart if it needs to.
351 This also catches the case where it was stopped and this
352 is just a remount to restart it.
353 Flush the writebuffer, if neccecary, else we loose it */
354 if (!(sb->s_flags & MS_RDONLY)) {
355 jffs2_stop_garbage_collect_thread(c);
357 jffs2_flush_wbuf_pad(c);
361 if (!(*flags & MS_RDONLY))
362 jffs2_start_garbage_collect_thread(c);
364 *flags |= MS_NOATIME;
369 void jffs2_write_super (struct super_block *sb)
371 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
374 if (sb->s_flags & MS_RDONLY)
377 D1(printk(KERN_DEBUG "jffs2_write_super()\n"));
378 jffs2_garbage_collect_trigger(c);
379 jffs2_erase_pending_blocks(c, 0);
380 jffs2_flush_wbuf_gc(c, 0);
384 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
385 fill in the raw_inode while you're at it. */
386 struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri)
389 struct super_block *sb = dir_i->i_sb;
390 struct jffs2_sb_info *c;
391 struct jffs2_inode_info *f;
394 D1(printk(KERN_DEBUG "jffs2_new_inode(): dir_i %ld, mode 0x%x\n", dir_i->i_ino, mode));
396 c = JFFS2_SB_INFO(sb);
398 inode = new_inode(sb);
401 return ERR_PTR(-ENOMEM);
403 f = JFFS2_INODE_INFO(inode);
404 jffs2_init_inode_info(f);
407 memset(ri, 0, sizeof(*ri));
408 /* Set OS-specific defaults for new inodes */
409 ri->uid = cpu_to_je16(current->fsuid);
411 if (dir_i->i_mode & S_ISGID) {
412 ri->gid = cpu_to_je16(dir_i->i_gid);
416 ri->gid = cpu_to_je16(current->fsgid);
418 ri->mode = cpu_to_jemode(mode);
419 ret = jffs2_do_new_inode (c, f, mode, ri);
421 make_bad_inode(inode);
426 inode->i_ino = je32_to_cpu(ri->ino);
427 inode->i_mode = jemode_to_cpu(ri->mode);
428 inode->i_gid = je16_to_cpu(ri->gid);
429 inode->i_uid = je16_to_cpu(ri->uid);
430 inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
431 ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
433 inode->i_blksize = PAGE_SIZE;
437 insert_inode_hash(inode);
443 int jffs2_do_fill_super(struct super_block *sb, void *data, int silent)
445 struct jffs2_sb_info *c;
446 struct inode *root_i;
450 c = JFFS2_SB_INFO(sb);
452 #ifndef CONFIG_JFFS2_FS_WRITEBUFFER
453 if (c->mtd->type == MTD_NANDFLASH) {
454 printk(KERN_ERR "jffs2: Cannot operate on NAND flash unless jffs2 NAND support is compiled in.\n");
457 if (c->mtd->type == MTD_DATAFLASH) {
458 printk(KERN_ERR "jffs2: Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in.\n");
463 c->flash_size = c->mtd->size;
464 c->sector_size = c->mtd->erasesize;
465 blocks = c->flash_size / c->sector_size;
468 * Size alignment check
470 if ((c->sector_size * blocks) != c->flash_size) {
471 c->flash_size = c->sector_size * blocks;
472 printk(KERN_INFO "jffs2: Flash size not aligned to erasesize, reducing to %dKiB\n",
473 c->flash_size / 1024);
476 if (c->flash_size < 5*c->sector_size) {
477 printk(KERN_ERR "jffs2: Too few erase blocks (%d)\n", c->flash_size / c->sector_size);
481 c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
483 /* NAND (or other bizarre) flash... do setup accordingly */
484 ret = jffs2_flash_setup(c);
488 c->inocache_list = kmalloc(INOCACHE_HASHSIZE * sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
489 if (!c->inocache_list) {
493 memset(c->inocache_list, 0, INOCACHE_HASHSIZE * sizeof(struct jffs2_inode_cache *));
495 if ((ret = jffs2_do_mount_fs(c)))
500 D1(printk(KERN_DEBUG "jffs2_do_fill_super(): Getting root inode\n"));
501 root_i = iget(sb, 1);
502 if (is_bad_inode(root_i)) {
503 D1(printk(KERN_WARNING "get root inode failed\n"));
507 D1(printk(KERN_DEBUG "jffs2_do_fill_super(): d_alloc_root()\n"));
508 sb->s_root = d_alloc_root(root_i);
512 sb->s_maxbytes = 0xFFFFFFFF;
513 sb->s_blocksize = PAGE_CACHE_SIZE;
514 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
515 sb->s_magic = JFFS2_SUPER_MAGIC;
516 if (!(sb->s_flags & MS_RDONLY))
517 jffs2_start_garbage_collect_thread(c);
522 jffs2_free_ino_caches(c);
523 jffs2_free_raw_node_refs(c);
524 if (jffs2_blocks_use_vmalloc(c))
529 kfree(c->inocache_list);
531 jffs2_flash_cleanup(c);
536 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
537 struct jffs2_inode_info *f)
539 iput(OFNI_EDONI_2SFFJ(f));
542 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
546 struct jffs2_inode_cache *ic;
548 /* The inode has zero nlink but its nodes weren't yet marked
549 obsolete. This has to be because we're still waiting for
550 the final (close() and) iput() to happen.
552 There's a possibility that the final iput() could have
553 happened while we were contemplating. In order to ensure
554 that we don't cause a new read_inode() (which would fail)
555 for the inode in question, we use ilookup() in this case
558 The nlink can't _become_ zero at this point because we're
559 holding the alloc_sem, and jffs2_do_unlink() would also
560 need that while decrementing nlink on any inode.
562 inode = ilookup(OFNI_BS_2SFFJ(c), inum);
564 D1(printk(KERN_DEBUG "ilookup() failed for ino #%u; inode is probably deleted.\n",
567 spin_lock(&c->inocache_lock);
568 ic = jffs2_get_ino_cache(c, inum);
570 D1(printk(KERN_DEBUG "Inode cache for ino #%u is gone.\n", inum));
571 spin_unlock(&c->inocache_lock);
574 if (ic->state != INO_STATE_CHECKEDABSENT) {
575 /* Wait for progress. Don't just loop */
576 D1(printk(KERN_DEBUG "Waiting for ino #%u in state %d\n",
577 ic->ino, ic->state));
578 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
580 spin_unlock(&c->inocache_lock);
586 /* Inode has links to it still; they're not going away because
587 jffs2_do_unlink() would need the alloc_sem and we have it.
588 Just iget() it, and if read_inode() is necessary that's OK.
590 inode = iget(OFNI_BS_2SFFJ(c), inum);
592 return ERR_PTR(-ENOMEM);
594 if (is_bad_inode(inode)) {
595 printk(KERN_NOTICE "Eep. read_inode() failed for ino #%u. nlink %d\n",
597 /* NB. This will happen again. We need to do something appropriate here. */
599 return ERR_PTR(-EIO);
602 return JFFS2_INODE_INFO(inode);
605 unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
606 struct jffs2_inode_info *f,
607 unsigned long offset,
610 struct inode *inode = OFNI_EDONI_2SFFJ(f);
613 pg = read_cache_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT,
614 (void *)jffs2_do_readpage_unlock, inode);
618 *priv = (unsigned long)pg;
622 void jffs2_gc_release_page(struct jffs2_sb_info *c,
626 struct page *pg = (void *)*priv;
629 page_cache_release(pg);
632 static int jffs2_flash_setup(struct jffs2_sb_info *c) {
635 if (jffs2_cleanmarker_oob(c)) {
636 /* NAND flash... do setup accordingly */
637 ret = jffs2_nand_flash_setup(c);
642 /* add setups for other bizarre flashes here... */
643 if (jffs2_nor_ecc(c)) {
644 ret = jffs2_nor_ecc_flash_setup(c);
650 if (jffs2_dataflash(c)) {
651 ret = jffs2_dataflash_setup(c);
656 /* and Intel "Sibley" flash */
657 if (jffs2_nor_wbuf_flash(c)) {
658 ret = jffs2_nor_wbuf_flash_setup(c);
666 void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
668 if (jffs2_cleanmarker_oob(c)) {
669 jffs2_nand_flash_cleanup(c);
672 /* add cleanups for other bizarre flashes here... */
673 if (jffs2_nor_ecc(c)) {
674 jffs2_nor_ecc_flash_cleanup(c);
678 if (jffs2_dataflash(c)) {
679 jffs2_dataflash_cleanup(c);
682 /* and Intel "Sibley" flash */
683 if (jffs2_nor_wbuf_flash(c)) {
684 jffs2_nor_wbuf_flash_cleanup(c);