2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
7 * Created by David Woodhouse <dwmw2@infradead.org>
9 * For licensing information, see the file 'LICENCE' in this directory.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/capability.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/cred.h>
20 #include <linux/fs_context.h>
21 #include <linux/list.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/pagemap.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/vfs.h>
27 #include <linux/crc32.h>
30 static int jffs2_flash_setup(struct jffs2_sb_info *c);
32 int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
34 struct jffs2_full_dnode *old_metadata, *new_metadata;
35 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
36 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
37 struct jffs2_raw_inode *ri;
38 union jffs2_device_node dev;
39 unsigned char *mdata = NULL;
44 int alloc_type = ALLOC_NORMAL;
46 jffs2_dbg(1, "%s(): ino #%lu\n", __func__, inode->i_ino);
48 /* Special cases - we don't want more than one data node
49 for these types on the medium at any time. So setattr
50 must read the original data associated with the node
51 (i.e. the device numbers or the target name) and write
52 it out again with the appropriate data attached */
53 if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
54 /* For these, we don't actually need to read the old node */
55 mdatalen = jffs2_encode_dev(&dev, inode->i_rdev);
57 jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
59 } else if (S_ISLNK(inode->i_mode)) {
61 mdatalen = f->metadata->size;
62 mdata = kmalloc(f->metadata->size, GFP_USER);
64 mutex_unlock(&f->sem);
67 ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
69 mutex_unlock(&f->sem);
73 mutex_unlock(&f->sem);
74 jffs2_dbg(1, "%s(): Writing %d bytes of symlink target\n",
78 ri = jffs2_alloc_raw_inode();
80 if (S_ISLNK(inode->i_mode))
85 ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &alloclen,
86 ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
88 jffs2_free_raw_inode(ri);
89 if (S_ISLNK(inode->i_mode))
94 ivalid = iattr->ia_valid;
96 ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
97 ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
98 ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
99 ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
101 ri->ino = cpu_to_je32(inode->i_ino);
102 ri->version = cpu_to_je32(++f->highest_version);
104 ri->uid = cpu_to_je16((ivalid & ATTR_UID)?
105 from_kuid(&init_user_ns, iattr->ia_uid):i_uid_read(inode));
106 ri->gid = cpu_to_je16((ivalid & ATTR_GID)?
107 from_kgid(&init_user_ns, iattr->ia_gid):i_gid_read(inode));
109 if (ivalid & ATTR_MODE)
110 ri->mode = cpu_to_jemode(iattr->ia_mode);
112 ri->mode = cpu_to_jemode(inode->i_mode);
115 ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
116 ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
117 ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
118 ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
120 ri->offset = cpu_to_je32(0);
121 ri->csize = ri->dsize = cpu_to_je32(mdatalen);
122 ri->compr = JFFS2_COMPR_NONE;
123 if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
124 /* It's an extension. Make it a hole node */
125 ri->compr = JFFS2_COMPR_ZERO;
126 ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
127 ri->offset = cpu_to_je32(inode->i_size);
128 } else if (ivalid & ATTR_SIZE && !iattr->ia_size) {
129 /* For truncate-to-zero, treat it as deletion because
130 it'll always be obsoleting all previous nodes */
131 alloc_type = ALLOC_DELETION;
133 ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
135 ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
137 ri->data_crc = cpu_to_je32(0);
139 new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, alloc_type);
140 if (S_ISLNK(inode->i_mode))
143 if (IS_ERR(new_metadata)) {
144 jffs2_complete_reservation(c);
145 jffs2_free_raw_inode(ri);
146 mutex_unlock(&f->sem);
147 return PTR_ERR(new_metadata);
149 /* It worked. Update the inode */
150 inode->i_atime = ITIME(je32_to_cpu(ri->atime));
151 inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
152 inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
153 inode->i_mode = jemode_to_cpu(ri->mode);
154 i_uid_write(inode, je16_to_cpu(ri->uid));
155 i_gid_write(inode, je16_to_cpu(ri->gid));
158 old_metadata = f->metadata;
160 if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
161 jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
163 if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
164 jffs2_add_full_dnode_to_inode(c, f, new_metadata);
165 inode->i_size = iattr->ia_size;
166 inode->i_blocks = (inode->i_size + 511) >> 9;
169 f->metadata = new_metadata;
172 jffs2_mark_node_obsolete(c, old_metadata->raw);
173 jffs2_free_full_dnode(old_metadata);
175 jffs2_free_raw_inode(ri);
177 mutex_unlock(&f->sem);
178 jffs2_complete_reservation(c);
180 /* We have to do the truncate_setsize() without f->sem held, since
181 some pages may be locked and waiting for it in read_folio().
182 We are protected from a simultaneous write() extending i_size
183 back past iattr->ia_size, because do_truncate() holds the
184 generic inode semaphore. */
185 if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
186 truncate_setsize(inode, iattr->ia_size);
187 inode->i_blocks = (inode->i_size + 511) >> 9;
193 int jffs2_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
196 struct inode *inode = d_inode(dentry);
199 rc = setattr_prepare(&init_user_ns, dentry, iattr);
203 rc = jffs2_do_setattr(inode, iattr);
204 if (!rc && (iattr->ia_valid & ATTR_MODE))
205 rc = posix_acl_chmod(&init_user_ns, inode, inode->i_mode);
210 int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
212 struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
215 buf->f_type = JFFS2_SUPER_MAGIC;
216 buf->f_bsize = 1 << PAGE_SHIFT;
217 buf->f_blocks = c->flash_size >> PAGE_SHIFT;
220 buf->f_namelen = JFFS2_MAX_NAME_LEN;
221 buf->f_fsid.val[0] = JFFS2_SUPER_MAGIC;
222 buf->f_fsid.val[1] = c->mtd->index;
224 spin_lock(&c->erase_completion_lock);
225 avail = c->dirty_size + c->free_size;
226 if (avail > c->sector_size * c->resv_blocks_write)
227 avail -= c->sector_size * c->resv_blocks_write;
230 spin_unlock(&c->erase_completion_lock);
232 buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
238 void jffs2_evict_inode (struct inode *inode)
240 /* We can forget about this inode for now - drop all
241 * the nodelists associated with it, etc.
243 struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
244 struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
246 jffs2_dbg(1, "%s(): ino #%lu mode %o\n",
247 __func__, inode->i_ino, inode->i_mode);
248 truncate_inode_pages_final(&inode->i_data);
250 jffs2_do_clear_inode(c, f);
253 struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
255 struct jffs2_inode_info *f;
256 struct jffs2_sb_info *c;
257 struct jffs2_raw_inode latest_node;
258 union jffs2_device_node jdev;
263 jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
265 inode = iget_locked(sb, ino);
267 return ERR_PTR(-ENOMEM);
268 if (!(inode->i_state & I_NEW))
271 f = JFFS2_INODE_INFO(inode);
272 c = JFFS2_SB_INFO(inode->i_sb);
274 jffs2_init_inode_info(f);
277 ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
281 inode->i_mode = jemode_to_cpu(latest_node.mode);
282 i_uid_write(inode, je16_to_cpu(latest_node.uid));
283 i_gid_write(inode, je16_to_cpu(latest_node.gid));
284 inode->i_size = je32_to_cpu(latest_node.isize);
285 inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
286 inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
287 inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
289 set_nlink(inode, f->inocache->pino_nlink);
291 inode->i_blocks = (inode->i_size + 511) >> 9;
293 switch (inode->i_mode & S_IFMT) {
296 inode->i_op = &jffs2_symlink_inode_operations;
297 inode->i_link = f->target;
302 struct jffs2_full_dirent *fd;
303 set_nlink(inode, 2); /* parent and '.' */
305 for (fd=f->dents; fd; fd = fd->next) {
306 if (fd->type == DT_DIR && fd->ino)
309 /* Root dir gets i_nlink 3 for some reason */
310 if (inode->i_ino == 1)
313 inode->i_op = &jffs2_dir_inode_operations;
314 inode->i_fop = &jffs2_dir_operations;
318 inode->i_op = &jffs2_file_inode_operations;
319 inode->i_fop = &jffs2_file_operations;
320 inode->i_mapping->a_ops = &jffs2_file_address_operations;
321 inode->i_mapping->nrpages = 0;
326 /* Read the device numbers from the media */
327 if (f->metadata->size != sizeof(jdev.old_id) &&
328 f->metadata->size != sizeof(jdev.new_id)) {
329 pr_notice("Device node has strange size %d\n",
333 jffs2_dbg(1, "Reading device numbers from flash\n");
334 ret = jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size);
337 pr_notice("Read device numbers for inode %lu failed\n",
338 (unsigned long)inode->i_ino);
341 if (f->metadata->size == sizeof(jdev.old_id))
342 rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
344 rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
349 inode->i_op = &jffs2_file_inode_operations;
350 init_special_inode(inode, inode->i_mode, rdev);
354 pr_warn("%s(): Bogus i_mode %o for ino %lu\n",
355 __func__, inode->i_mode, (unsigned long)inode->i_ino);
358 mutex_unlock(&f->sem);
360 jffs2_dbg(1, "jffs2_read_inode() returning\n");
361 unlock_new_inode(inode);
367 mutex_unlock(&f->sem);
372 void jffs2_dirty_inode(struct inode *inode, int flags)
376 if (!(inode->i_state & I_DIRTY_DATASYNC)) {
377 jffs2_dbg(2, "%s(): not calling setattr() for ino #%lu\n",
378 __func__, inode->i_ino);
382 jffs2_dbg(1, "%s(): calling setattr() for ino #%lu\n",
383 __func__, inode->i_ino);
385 iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
386 iattr.ia_mode = inode->i_mode;
387 iattr.ia_uid = inode->i_uid;
388 iattr.ia_gid = inode->i_gid;
389 iattr.ia_atime = inode->i_atime;
390 iattr.ia_mtime = inode->i_mtime;
391 iattr.ia_ctime = inode->i_ctime;
393 jffs2_do_setattr(inode, &iattr);
396 int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc)
398 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
400 if (c->flags & JFFS2_SB_FLAG_RO && !sb_rdonly(sb))
403 /* We stop if it was running, then restart if it needs to.
404 This also catches the case where it was stopped and this
405 is just a remount to restart it.
406 Flush the writebuffer, if neccecary, else we loose it */
407 if (!sb_rdonly(sb)) {
408 jffs2_stop_garbage_collect_thread(c);
409 mutex_lock(&c->alloc_sem);
410 jffs2_flush_wbuf_pad(c);
411 mutex_unlock(&c->alloc_sem);
414 if (!(fc->sb_flags & SB_RDONLY))
415 jffs2_start_garbage_collect_thread(c);
417 fc->sb_flags |= SB_NOATIME;
421 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
422 fill in the raw_inode while you're at it. */
423 struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode, struct jffs2_raw_inode *ri)
426 struct super_block *sb = dir_i->i_sb;
427 struct jffs2_sb_info *c;
428 struct jffs2_inode_info *f;
431 jffs2_dbg(1, "%s(): dir_i %ld, mode 0x%x\n",
432 __func__, dir_i->i_ino, mode);
434 c = JFFS2_SB_INFO(sb);
436 inode = new_inode(sb);
439 return ERR_PTR(-ENOMEM);
441 f = JFFS2_INODE_INFO(inode);
442 jffs2_init_inode_info(f);
445 memset(ri, 0, sizeof(*ri));
446 /* Set OS-specific defaults for new inodes */
447 ri->uid = cpu_to_je16(from_kuid(&init_user_ns, current_fsuid()));
449 if (dir_i->i_mode & S_ISGID) {
450 ri->gid = cpu_to_je16(i_gid_read(dir_i));
454 ri->gid = cpu_to_je16(from_kgid(&init_user_ns, current_fsgid()));
457 /* POSIX ACLs have to be processed now, at least partly.
458 The umask is only applied if there's no default ACL */
459 ret = jffs2_init_acl_pre(dir_i, inode, &mode);
461 mutex_unlock(&f->sem);
462 make_bad_inode(inode);
466 ret = jffs2_do_new_inode (c, f, mode, ri);
468 mutex_unlock(&f->sem);
469 make_bad_inode(inode);
474 inode->i_ino = je32_to_cpu(ri->ino);
475 inode->i_mode = jemode_to_cpu(ri->mode);
476 i_gid_write(inode, je16_to_cpu(ri->gid));
477 i_uid_write(inode, je16_to_cpu(ri->uid));
478 inode->i_atime = inode->i_ctime = inode->i_mtime = current_time(inode);
479 ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
484 if (insert_inode_locked(inode) < 0) {
485 mutex_unlock(&f->sem);
486 make_bad_inode(inode);
488 return ERR_PTR(-EINVAL);
494 static int calculate_inocache_hashsize(uint32_t flash_size)
497 * Pick a inocache hash size based on the size of the medium.
498 * Count how many megabytes we're dealing with, apply a hashsize twice
499 * that size, but rounding down to the usual big powers of 2. And keep
500 * to sensible bounds.
503 int size_mb = flash_size / 1024 / 1024;
504 int hashsize = (size_mb * 2) & ~0x3f;
506 if (hashsize < INOCACHE_HASHSIZE_MIN)
507 return INOCACHE_HASHSIZE_MIN;
508 if (hashsize > INOCACHE_HASHSIZE_MAX)
509 return INOCACHE_HASHSIZE_MAX;
514 int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc)
516 struct jffs2_sb_info *c;
517 struct inode *root_i;
521 c = JFFS2_SB_INFO(sb);
523 /* Do not support the MLC nand */
524 if (c->mtd->type == MTD_MLCNANDFLASH)
527 #ifndef CONFIG_JFFS2_FS_WRITEBUFFER
528 if (c->mtd->type == MTD_NANDFLASH) {
529 errorf(fc, "Cannot operate on NAND flash unless jffs2 NAND support is compiled in");
532 if (c->mtd->type == MTD_DATAFLASH) {
533 errorf(fc, "Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in");
538 c->flash_size = c->mtd->size;
539 c->sector_size = c->mtd->erasesize;
540 blocks = c->flash_size / c->sector_size;
543 * Size alignment check
545 if ((c->sector_size * blocks) != c->flash_size) {
546 c->flash_size = c->sector_size * blocks;
547 infof(fc, "Flash size not aligned to erasesize, reducing to %dKiB",
548 c->flash_size / 1024);
551 if (c->flash_size < 5*c->sector_size) {
552 errorf(fc, "Too few erase blocks (%d)",
553 c->flash_size / c->sector_size);
557 c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
559 /* NAND (or other bizarre) flash... do setup accordingly */
560 ret = jffs2_flash_setup(c);
564 c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
565 c->inocache_list = kcalloc(c->inocache_hashsize, sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
566 if (!c->inocache_list) {
571 jffs2_init_xattr_subsystem(c);
573 if ((ret = jffs2_do_mount_fs(c)))
576 jffs2_dbg(1, "%s(): Getting root inode\n", __func__);
577 root_i = jffs2_iget(sb, 1);
578 if (IS_ERR(root_i)) {
579 jffs2_dbg(1, "get root inode failed\n");
580 ret = PTR_ERR(root_i);
586 jffs2_dbg(1, "%s(): d_make_root()\n", __func__);
587 sb->s_root = d_make_root(root_i);
591 sb->s_maxbytes = 0xFFFFFFFF;
592 sb->s_blocksize = PAGE_SIZE;
593 sb->s_blocksize_bits = PAGE_SHIFT;
594 sb->s_magic = JFFS2_SUPER_MAGIC;
596 sb->s_time_max = U32_MAX;
599 jffs2_start_garbage_collect_thread(c);
603 jffs2_free_ino_caches(c);
604 jffs2_free_raw_node_refs(c);
606 jffs2_clear_xattr_subsystem(c);
609 kfree(c->inocache_list);
611 jffs2_flash_cleanup(c);
616 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
617 struct jffs2_inode_info *f)
619 iput(OFNI_EDONI_2SFFJ(f));
622 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
623 int inum, int unlinked)
626 struct jffs2_inode_cache *ic;
629 /* The inode has zero nlink but its nodes weren't yet marked
630 obsolete. This has to be because we're still waiting for
631 the final (close() and) iput() to happen.
633 There's a possibility that the final iput() could have
634 happened while we were contemplating. In order to ensure
635 that we don't cause a new read_inode() (which would fail)
636 for the inode in question, we use ilookup() in this case
639 The nlink can't _become_ zero at this point because we're
640 holding the alloc_sem, and jffs2_do_unlink() would also
641 need that while decrementing nlink on any inode.
643 inode = ilookup(OFNI_BS_2SFFJ(c), inum);
645 jffs2_dbg(1, "ilookup() failed for ino #%u; inode is probably deleted.\n",
648 spin_lock(&c->inocache_lock);
649 ic = jffs2_get_ino_cache(c, inum);
651 jffs2_dbg(1, "Inode cache for ino #%u is gone\n",
653 spin_unlock(&c->inocache_lock);
656 if (ic->state != INO_STATE_CHECKEDABSENT) {
657 /* Wait for progress. Don't just loop */
658 jffs2_dbg(1, "Waiting for ino #%u in state %d\n",
660 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
662 spin_unlock(&c->inocache_lock);
668 /* Inode has links to it still; they're not going away because
669 jffs2_do_unlink() would need the alloc_sem and we have it.
670 Just iget() it, and if read_inode() is necessary that's OK.
672 inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
674 return ERR_CAST(inode);
676 if (is_bad_inode(inode)) {
677 pr_notice("Eep. read_inode() failed for ino #%u. unlinked %d\n",
679 /* NB. This will happen again. We need to do something appropriate here. */
681 return ERR_PTR(-EIO);
684 return JFFS2_INODE_INFO(inode);
687 static int jffs2_flash_setup(struct jffs2_sb_info *c) {
690 if (jffs2_cleanmarker_oob(c)) {
691 /* NAND flash... do setup accordingly */
692 ret = jffs2_nand_flash_setup(c);
698 if (jffs2_dataflash(c)) {
699 ret = jffs2_dataflash_setup(c);
704 /* and Intel "Sibley" flash */
705 if (jffs2_nor_wbuf_flash(c)) {
706 ret = jffs2_nor_wbuf_flash_setup(c);
711 /* and an UBI volume */
712 if (jffs2_ubivol(c)) {
713 ret = jffs2_ubivol_setup(c);
721 void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
723 if (jffs2_cleanmarker_oob(c)) {
724 jffs2_nand_flash_cleanup(c);
728 if (jffs2_dataflash(c)) {
729 jffs2_dataflash_cleanup(c);
732 /* and Intel "Sibley" flash */
733 if (jffs2_nor_wbuf_flash(c)) {
734 jffs2_nor_wbuf_flash_cleanup(c);
737 /* and an UBI volume */
738 if (jffs2_ubivol(c)) {
739 jffs2_ubivol_cleanup(c);