Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
0006  *
0007  * Created by David Woodhouse <dwmw2@infradead.org>
0008  *
0009  * For licensing information, see the file 'LICENCE' in this directory.
0010  *
0011  */
0012 
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014 
0015 #include <linux/capability.h>
0016 #include <linux/kernel.h>
0017 #include <linux/sched.h>
0018 #include <linux/cred.h>
0019 #include <linux/fs.h>
0020 #include <linux/fs_context.h>
0021 #include <linux/list.h>
0022 #include <linux/mtd/mtd.h>
0023 #include <linux/pagemap.h>
0024 #include <linux/slab.h>
0025 #include <linux/vmalloc.h>
0026 #include <linux/vfs.h>
0027 #include <linux/crc32.h>
0028 #include "nodelist.h"
0029 
0030 static int jffs2_flash_setup(struct jffs2_sb_info *c);
0031 
0032 int jffs2_do_setattr (struct inode *inode, struct iattr *iattr)
0033 {
0034     struct jffs2_full_dnode *old_metadata, *new_metadata;
0035     struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
0036     struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
0037     struct jffs2_raw_inode *ri;
0038     union jffs2_device_node dev;
0039     unsigned char *mdata = NULL;
0040     int mdatalen = 0;
0041     unsigned int ivalid;
0042     uint32_t alloclen;
0043     int ret;
0044     int alloc_type = ALLOC_NORMAL;
0045 
0046     jffs2_dbg(1, "%s(): ino #%lu\n", __func__, inode->i_ino);
0047 
0048     /* Special cases - we don't want more than one data node
0049        for these types on the medium at any time. So setattr
0050        must read the original data associated with the node
0051        (i.e. the device numbers or the target name) and write
0052        it out again with the appropriate data attached */
0053     if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
0054         /* For these, we don't actually need to read the old node */
0055         mdatalen = jffs2_encode_dev(&dev, inode->i_rdev);
0056         mdata = (char *)&dev;
0057         jffs2_dbg(1, "%s(): Writing %d bytes of kdev_t\n",
0058               __func__, mdatalen);
0059     } else if (S_ISLNK(inode->i_mode)) {
0060         mutex_lock(&f->sem);
0061         mdatalen = f->metadata->size;
0062         mdata = kmalloc(f->metadata->size, GFP_USER);
0063         if (!mdata) {
0064             mutex_unlock(&f->sem);
0065             return -ENOMEM;
0066         }
0067         ret = jffs2_read_dnode(c, f, f->metadata, mdata, 0, mdatalen);
0068         if (ret) {
0069             mutex_unlock(&f->sem);
0070             kfree(mdata);
0071             return ret;
0072         }
0073         mutex_unlock(&f->sem);
0074         jffs2_dbg(1, "%s(): Writing %d bytes of symlink target\n",
0075               __func__, mdatalen);
0076     }
0077 
0078     ri = jffs2_alloc_raw_inode();
0079     if (!ri) {
0080         if (S_ISLNK(inode->i_mode))
0081             kfree(mdata);
0082         return -ENOMEM;
0083     }
0084 
0085     ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &alloclen,
0086                   ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
0087     if (ret) {
0088         jffs2_free_raw_inode(ri);
0089         if (S_ISLNK(inode->i_mode))
0090              kfree(mdata);
0091         return ret;
0092     }
0093     mutex_lock(&f->sem);
0094     ivalid = iattr->ia_valid;
0095 
0096     ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0097     ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
0098     ri->totlen = cpu_to_je32(sizeof(*ri) + mdatalen);
0099     ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
0100 
0101     ri->ino = cpu_to_je32(inode->i_ino);
0102     ri->version = cpu_to_je32(++f->highest_version);
0103 
0104     ri->uid = cpu_to_je16((ivalid & ATTR_UID)?
0105         from_kuid(&init_user_ns, iattr->ia_uid):i_uid_read(inode));
0106     ri->gid = cpu_to_je16((ivalid & ATTR_GID)?
0107         from_kgid(&init_user_ns, iattr->ia_gid):i_gid_read(inode));
0108 
0109     if (ivalid & ATTR_MODE)
0110         ri->mode = cpu_to_jemode(iattr->ia_mode);
0111     else
0112         ri->mode = cpu_to_jemode(inode->i_mode);
0113 
0114 
0115     ri->isize = cpu_to_je32((ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size);
0116     ri->atime = cpu_to_je32(I_SEC((ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime));
0117     ri->mtime = cpu_to_je32(I_SEC((ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime));
0118     ri->ctime = cpu_to_je32(I_SEC((ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime));
0119 
0120     ri->offset = cpu_to_je32(0);
0121     ri->csize = ri->dsize = cpu_to_je32(mdatalen);
0122     ri->compr = JFFS2_COMPR_NONE;
0123     if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
0124         /* It's an extension. Make it a hole node */
0125         ri->compr = JFFS2_COMPR_ZERO;
0126         ri->dsize = cpu_to_je32(iattr->ia_size - inode->i_size);
0127         ri->offset = cpu_to_je32(inode->i_size);
0128     } else if (ivalid & ATTR_SIZE && !iattr->ia_size) {
0129         /* For truncate-to-zero, treat it as deletion because
0130            it'll always be obsoleting all previous nodes */
0131         alloc_type = ALLOC_DELETION;
0132     }
0133     ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
0134     if (mdatalen)
0135         ri->data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
0136     else
0137         ri->data_crc = cpu_to_je32(0);
0138 
0139     new_metadata = jffs2_write_dnode(c, f, ri, mdata, mdatalen, alloc_type);
0140     if (S_ISLNK(inode->i_mode))
0141         kfree(mdata);
0142 
0143     if (IS_ERR(new_metadata)) {
0144         jffs2_complete_reservation(c);
0145         jffs2_free_raw_inode(ri);
0146         mutex_unlock(&f->sem);
0147         return PTR_ERR(new_metadata);
0148     }
0149     /* It worked. Update the inode */
0150     inode->i_atime = ITIME(je32_to_cpu(ri->atime));
0151     inode->i_ctime = ITIME(je32_to_cpu(ri->ctime));
0152     inode->i_mtime = ITIME(je32_to_cpu(ri->mtime));
0153     inode->i_mode = jemode_to_cpu(ri->mode);
0154     i_uid_write(inode, je16_to_cpu(ri->uid));
0155     i_gid_write(inode, je16_to_cpu(ri->gid));
0156 
0157 
0158     old_metadata = f->metadata;
0159 
0160     if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size)
0161         jffs2_truncate_fragtree (c, &f->fragtree, iattr->ia_size);
0162 
0163     if (ivalid & ATTR_SIZE && inode->i_size < iattr->ia_size) {
0164         jffs2_add_full_dnode_to_inode(c, f, new_metadata);
0165         inode->i_size = iattr->ia_size;
0166         inode->i_blocks = (inode->i_size + 511) >> 9;
0167         f->metadata = NULL;
0168     } else {
0169         f->metadata = new_metadata;
0170     }
0171     if (old_metadata) {
0172         jffs2_mark_node_obsolete(c, old_metadata->raw);
0173         jffs2_free_full_dnode(old_metadata);
0174     }
0175     jffs2_free_raw_inode(ri);
0176 
0177     mutex_unlock(&f->sem);
0178     jffs2_complete_reservation(c);
0179 
0180     /* We have to do the truncate_setsize() without f->sem held, since
0181        some pages may be locked and waiting for it in read_folio().
0182        We are protected from a simultaneous write() extending i_size
0183        back past iattr->ia_size, because do_truncate() holds the
0184        generic inode semaphore. */
0185     if (ivalid & ATTR_SIZE && inode->i_size > iattr->ia_size) {
0186         truncate_setsize(inode, iattr->ia_size);
0187         inode->i_blocks = (inode->i_size + 511) >> 9;
0188     }
0189 
0190     return 0;
0191 }
0192 
0193 int jffs2_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0194           struct iattr *iattr)
0195 {
0196     struct inode *inode = d_inode(dentry);
0197     int rc;
0198 
0199     rc = setattr_prepare(&init_user_ns, dentry, iattr);
0200     if (rc)
0201         return rc;
0202 
0203     rc = jffs2_do_setattr(inode, iattr);
0204     if (!rc && (iattr->ia_valid & ATTR_MODE))
0205         rc = posix_acl_chmod(&init_user_ns, inode, inode->i_mode);
0206 
0207     return rc;
0208 }
0209 
0210 int jffs2_statfs(struct dentry *dentry, struct kstatfs *buf)
0211 {
0212     struct jffs2_sb_info *c = JFFS2_SB_INFO(dentry->d_sb);
0213     unsigned long avail;
0214 
0215     buf->f_type = JFFS2_SUPER_MAGIC;
0216     buf->f_bsize = 1 << PAGE_SHIFT;
0217     buf->f_blocks = c->flash_size >> PAGE_SHIFT;
0218     buf->f_files = 0;
0219     buf->f_ffree = 0;
0220     buf->f_namelen = JFFS2_MAX_NAME_LEN;
0221     buf->f_fsid.val[0] = JFFS2_SUPER_MAGIC;
0222     buf->f_fsid.val[1] = c->mtd->index;
0223 
0224     spin_lock(&c->erase_completion_lock);
0225     avail = c->dirty_size + c->free_size;
0226     if (avail > c->sector_size * c->resv_blocks_write)
0227         avail -= c->sector_size * c->resv_blocks_write;
0228     else
0229         avail = 0;
0230     spin_unlock(&c->erase_completion_lock);
0231 
0232     buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
0233 
0234     return 0;
0235 }
0236 
0237 
0238 void jffs2_evict_inode (struct inode *inode)
0239 {
0240     /* We can forget about this inode for now - drop all
0241      *  the nodelists associated with it, etc.
0242      */
0243     struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
0244     struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
0245 
0246     jffs2_dbg(1, "%s(): ino #%lu mode %o\n",
0247           __func__, inode->i_ino, inode->i_mode);
0248     truncate_inode_pages_final(&inode->i_data);
0249     clear_inode(inode);
0250     jffs2_do_clear_inode(c, f);
0251 }
0252 
0253 struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
0254 {
0255     struct jffs2_inode_info *f;
0256     struct jffs2_sb_info *c;
0257     struct jffs2_raw_inode latest_node;
0258     union jffs2_device_node jdev;
0259     struct inode *inode;
0260     dev_t rdev = 0;
0261     int ret;
0262 
0263     jffs2_dbg(1, "%s(): ino == %lu\n", __func__, ino);
0264 
0265     inode = iget_locked(sb, ino);
0266     if (!inode)
0267         return ERR_PTR(-ENOMEM);
0268     if (!(inode->i_state & I_NEW))
0269         return inode;
0270 
0271     f = JFFS2_INODE_INFO(inode);
0272     c = JFFS2_SB_INFO(inode->i_sb);
0273 
0274     jffs2_init_inode_info(f);
0275     mutex_lock(&f->sem);
0276 
0277     ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
0278     if (ret)
0279         goto error;
0280 
0281     inode->i_mode = jemode_to_cpu(latest_node.mode);
0282     i_uid_write(inode, je16_to_cpu(latest_node.uid));
0283     i_gid_write(inode, je16_to_cpu(latest_node.gid));
0284     inode->i_size = je32_to_cpu(latest_node.isize);
0285     inode->i_atime = ITIME(je32_to_cpu(latest_node.atime));
0286     inode->i_mtime = ITIME(je32_to_cpu(latest_node.mtime));
0287     inode->i_ctime = ITIME(je32_to_cpu(latest_node.ctime));
0288 
0289     set_nlink(inode, f->inocache->pino_nlink);
0290 
0291     inode->i_blocks = (inode->i_size + 511) >> 9;
0292 
0293     switch (inode->i_mode & S_IFMT) {
0294 
0295     case S_IFLNK:
0296         inode->i_op = &jffs2_symlink_inode_operations;
0297         inode->i_link = f->target;
0298         break;
0299 
0300     case S_IFDIR:
0301     {
0302         struct jffs2_full_dirent *fd;
0303         set_nlink(inode, 2); /* parent and '.' */
0304 
0305         for (fd=f->dents; fd; fd = fd->next) {
0306             if (fd->type == DT_DIR && fd->ino)
0307                 inc_nlink(inode);
0308         }
0309         /* Root dir gets i_nlink 3 for some reason */
0310         if (inode->i_ino == 1)
0311             inc_nlink(inode);
0312 
0313         inode->i_op = &jffs2_dir_inode_operations;
0314         inode->i_fop = &jffs2_dir_operations;
0315         break;
0316     }
0317     case S_IFREG:
0318         inode->i_op = &jffs2_file_inode_operations;
0319         inode->i_fop = &jffs2_file_operations;
0320         inode->i_mapping->a_ops = &jffs2_file_address_operations;
0321         inode->i_mapping->nrpages = 0;
0322         break;
0323 
0324     case S_IFBLK:
0325     case S_IFCHR:
0326         /* Read the device numbers from the media */
0327         if (f->metadata->size != sizeof(jdev.old_id) &&
0328             f->metadata->size != sizeof(jdev.new_id)) {
0329             pr_notice("Device node has strange size %d\n",
0330                   f->metadata->size);
0331             goto error_io;
0332         }
0333         jffs2_dbg(1, "Reading device numbers from flash\n");
0334         ret = jffs2_read_dnode(c, f, f->metadata, (char *)&jdev, 0, f->metadata->size);
0335         if (ret < 0) {
0336             /* Eep */
0337             pr_notice("Read device numbers for inode %lu failed\n",
0338                   (unsigned long)inode->i_ino);
0339             goto error;
0340         }
0341         if (f->metadata->size == sizeof(jdev.old_id))
0342             rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
0343         else
0344             rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
0345         fallthrough;
0346 
0347     case S_IFSOCK:
0348     case S_IFIFO:
0349         inode->i_op = &jffs2_file_inode_operations;
0350         init_special_inode(inode, inode->i_mode, rdev);
0351         break;
0352 
0353     default:
0354         pr_warn("%s(): Bogus i_mode %o for ino %lu\n",
0355             __func__, inode->i_mode, (unsigned long)inode->i_ino);
0356     }
0357 
0358     mutex_unlock(&f->sem);
0359 
0360     jffs2_dbg(1, "jffs2_read_inode() returning\n");
0361     unlock_new_inode(inode);
0362     return inode;
0363 
0364 error_io:
0365     ret = -EIO;
0366 error:
0367     mutex_unlock(&f->sem);
0368     iget_failed(inode);
0369     return ERR_PTR(ret);
0370 }
0371 
0372 void jffs2_dirty_inode(struct inode *inode, int flags)
0373 {
0374     struct iattr iattr;
0375 
0376     if (!(inode->i_state & I_DIRTY_DATASYNC)) {
0377         jffs2_dbg(2, "%s(): not calling setattr() for ino #%lu\n",
0378               __func__, inode->i_ino);
0379         return;
0380     }
0381 
0382     jffs2_dbg(1, "%s(): calling setattr() for ino #%lu\n",
0383           __func__, inode->i_ino);
0384 
0385     iattr.ia_valid = ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_MTIME|ATTR_CTIME;
0386     iattr.ia_mode = inode->i_mode;
0387     iattr.ia_uid = inode->i_uid;
0388     iattr.ia_gid = inode->i_gid;
0389     iattr.ia_atime = inode->i_atime;
0390     iattr.ia_mtime = inode->i_mtime;
0391     iattr.ia_ctime = inode->i_ctime;
0392 
0393     jffs2_do_setattr(inode, &iattr);
0394 }
0395 
0396 int jffs2_do_remount_fs(struct super_block *sb, struct fs_context *fc)
0397 {
0398     struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
0399 
0400     if (c->flags & JFFS2_SB_FLAG_RO && !sb_rdonly(sb))
0401         return -EROFS;
0402 
0403     /* We stop if it was running, then restart if it needs to.
0404        This also catches the case where it was stopped and this
0405        is just a remount to restart it.
0406        Flush the writebuffer, if neccecary, else we loose it */
0407     if (!sb_rdonly(sb)) {
0408         jffs2_stop_garbage_collect_thread(c);
0409         mutex_lock(&c->alloc_sem);
0410         jffs2_flush_wbuf_pad(c);
0411         mutex_unlock(&c->alloc_sem);
0412     }
0413 
0414     if (!(fc->sb_flags & SB_RDONLY))
0415         jffs2_start_garbage_collect_thread(c);
0416 
0417     fc->sb_flags |= SB_NOATIME;
0418     return 0;
0419 }
0420 
0421 /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
0422    fill in the raw_inode while you're at it. */
0423 struct inode *jffs2_new_inode (struct inode *dir_i, umode_t mode, struct jffs2_raw_inode *ri)
0424 {
0425     struct inode *inode;
0426     struct super_block *sb = dir_i->i_sb;
0427     struct jffs2_sb_info *c;
0428     struct jffs2_inode_info *f;
0429     int ret;
0430 
0431     jffs2_dbg(1, "%s(): dir_i %ld, mode 0x%x\n",
0432           __func__, dir_i->i_ino, mode);
0433 
0434     c = JFFS2_SB_INFO(sb);
0435 
0436     inode = new_inode(sb);
0437 
0438     if (!inode)
0439         return ERR_PTR(-ENOMEM);
0440 
0441     f = JFFS2_INODE_INFO(inode);
0442     jffs2_init_inode_info(f);
0443     mutex_lock(&f->sem);
0444 
0445     memset(ri, 0, sizeof(*ri));
0446     /* Set OS-specific defaults for new inodes */
0447     ri->uid = cpu_to_je16(from_kuid(&init_user_ns, current_fsuid()));
0448 
0449     if (dir_i->i_mode & S_ISGID) {
0450         ri->gid = cpu_to_je16(i_gid_read(dir_i));
0451         if (S_ISDIR(mode))
0452             mode |= S_ISGID;
0453     } else {
0454         ri->gid = cpu_to_je16(from_kgid(&init_user_ns, current_fsgid()));
0455     }
0456 
0457     /* POSIX ACLs have to be processed now, at least partly.
0458        The umask is only applied if there's no default ACL */
0459     ret = jffs2_init_acl_pre(dir_i, inode, &mode);
0460     if (ret) {
0461         mutex_unlock(&f->sem);
0462         make_bad_inode(inode);
0463         iput(inode);
0464         return ERR_PTR(ret);
0465     }
0466     ret = jffs2_do_new_inode (c, f, mode, ri);
0467     if (ret) {
0468         mutex_unlock(&f->sem);
0469         make_bad_inode(inode);
0470         iput(inode);
0471         return ERR_PTR(ret);
0472     }
0473     set_nlink(inode, 1);
0474     inode->i_ino = je32_to_cpu(ri->ino);
0475     inode->i_mode = jemode_to_cpu(ri->mode);
0476     i_gid_write(inode, je16_to_cpu(ri->gid));
0477     i_uid_write(inode, je16_to_cpu(ri->uid));
0478     inode->i_atime = inode->i_ctime = inode->i_mtime = current_time(inode);
0479     ri->atime = ri->mtime = ri->ctime = cpu_to_je32(I_SEC(inode->i_mtime));
0480 
0481     inode->i_blocks = 0;
0482     inode->i_size = 0;
0483 
0484     if (insert_inode_locked(inode) < 0) {
0485         mutex_unlock(&f->sem);
0486         make_bad_inode(inode);
0487         iput(inode);
0488         return ERR_PTR(-EINVAL);
0489     }
0490 
0491     return inode;
0492 }
0493 
0494 static int calculate_inocache_hashsize(uint32_t flash_size)
0495 {
0496     /*
0497      * Pick a inocache hash size based on the size of the medium.
0498      * Count how many megabytes we're dealing with, apply a hashsize twice
0499      * that size, but rounding down to the usual big powers of 2. And keep
0500      * to sensible bounds.
0501      */
0502 
0503     int size_mb = flash_size / 1024 / 1024;
0504     int hashsize = (size_mb * 2) & ~0x3f;
0505 
0506     if (hashsize < INOCACHE_HASHSIZE_MIN)
0507         return INOCACHE_HASHSIZE_MIN;
0508     if (hashsize > INOCACHE_HASHSIZE_MAX)
0509         return INOCACHE_HASHSIZE_MAX;
0510 
0511     return hashsize;
0512 }
0513 
0514 int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc)
0515 {
0516     struct jffs2_sb_info *c;
0517     struct inode *root_i;
0518     int ret;
0519     size_t blocks;
0520 
0521     c = JFFS2_SB_INFO(sb);
0522 
0523     /* Do not support the MLC nand */
0524     if (c->mtd->type == MTD_MLCNANDFLASH)
0525         return -EINVAL;
0526 
0527 #ifndef CONFIG_JFFS2_FS_WRITEBUFFER
0528     if (c->mtd->type == MTD_NANDFLASH) {
0529         errorf(fc, "Cannot operate on NAND flash unless jffs2 NAND support is compiled in");
0530         return -EINVAL;
0531     }
0532     if (c->mtd->type == MTD_DATAFLASH) {
0533         errorf(fc, "Cannot operate on DataFlash unless jffs2 DataFlash support is compiled in");
0534         return -EINVAL;
0535     }
0536 #endif
0537 
0538     c->flash_size = c->mtd->size;
0539     c->sector_size = c->mtd->erasesize;
0540     blocks = c->flash_size / c->sector_size;
0541 
0542     /*
0543      * Size alignment check
0544      */
0545     if ((c->sector_size * blocks) != c->flash_size) {
0546         c->flash_size = c->sector_size * blocks;
0547         infof(fc, "Flash size not aligned to erasesize, reducing to %dKiB",
0548               c->flash_size / 1024);
0549     }
0550 
0551     if (c->flash_size < 5*c->sector_size) {
0552         errorf(fc, "Too few erase blocks (%d)",
0553                c->flash_size / c->sector_size);
0554         return -EINVAL;
0555     }
0556 
0557     c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
0558 
0559     /* NAND (or other bizarre) flash... do setup accordingly */
0560     ret = jffs2_flash_setup(c);
0561     if (ret)
0562         return ret;
0563 
0564     c->inocache_hashsize = calculate_inocache_hashsize(c->flash_size);
0565     c->inocache_list = kcalloc(c->inocache_hashsize, sizeof(struct jffs2_inode_cache *), GFP_KERNEL);
0566     if (!c->inocache_list) {
0567         ret = -ENOMEM;
0568         goto out_wbuf;
0569     }
0570 
0571     jffs2_init_xattr_subsystem(c);
0572 
0573     if ((ret = jffs2_do_mount_fs(c)))
0574         goto out_inohash;
0575 
0576     jffs2_dbg(1, "%s(): Getting root inode\n", __func__);
0577     root_i = jffs2_iget(sb, 1);
0578     if (IS_ERR(root_i)) {
0579         jffs2_dbg(1, "get root inode failed\n");
0580         ret = PTR_ERR(root_i);
0581         goto out_root;
0582     }
0583 
0584     ret = -ENOMEM;
0585 
0586     jffs2_dbg(1, "%s(): d_make_root()\n", __func__);
0587     sb->s_root = d_make_root(root_i);
0588     if (!sb->s_root)
0589         goto out_root;
0590 
0591     sb->s_maxbytes = 0xFFFFFFFF;
0592     sb->s_blocksize = PAGE_SIZE;
0593     sb->s_blocksize_bits = PAGE_SHIFT;
0594     sb->s_magic = JFFS2_SUPER_MAGIC;
0595     sb->s_time_min = 0;
0596     sb->s_time_max = U32_MAX;
0597 
0598     if (!sb_rdonly(sb))
0599         jffs2_start_garbage_collect_thread(c);
0600     return 0;
0601 
0602 out_root:
0603     jffs2_free_ino_caches(c);
0604     jffs2_free_raw_node_refs(c);
0605     kvfree(c->blocks);
0606     jffs2_clear_xattr_subsystem(c);
0607     jffs2_sum_exit(c);
0608  out_inohash:
0609     kfree(c->inocache_list);
0610  out_wbuf:
0611     jffs2_flash_cleanup(c);
0612 
0613     return ret;
0614 }
0615 
0616 void jffs2_gc_release_inode(struct jffs2_sb_info *c,
0617                    struct jffs2_inode_info *f)
0618 {
0619     iput(OFNI_EDONI_2SFFJ(f));
0620 }
0621 
0622 struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
0623                           int inum, int unlinked)
0624 {
0625     struct inode *inode;
0626     struct jffs2_inode_cache *ic;
0627 
0628     if (unlinked) {
0629         /* The inode has zero nlink but its nodes weren't yet marked
0630            obsolete. This has to be because we're still waiting for
0631            the final (close() and) iput() to happen.
0632 
0633            There's a possibility that the final iput() could have
0634            happened while we were contemplating. In order to ensure
0635            that we don't cause a new read_inode() (which would fail)
0636            for the inode in question, we use ilookup() in this case
0637            instead of iget().
0638 
0639            The nlink can't _become_ zero at this point because we're
0640            holding the alloc_sem, and jffs2_do_unlink() would also
0641            need that while decrementing nlink on any inode.
0642         */
0643         inode = ilookup(OFNI_BS_2SFFJ(c), inum);
0644         if (!inode) {
0645             jffs2_dbg(1, "ilookup() failed for ino #%u; inode is probably deleted.\n",
0646                   inum);
0647 
0648             spin_lock(&c->inocache_lock);
0649             ic = jffs2_get_ino_cache(c, inum);
0650             if (!ic) {
0651                 jffs2_dbg(1, "Inode cache for ino #%u is gone\n",
0652                       inum);
0653                 spin_unlock(&c->inocache_lock);
0654                 return NULL;
0655             }
0656             if (ic->state != INO_STATE_CHECKEDABSENT) {
0657                 /* Wait for progress. Don't just loop */
0658                 jffs2_dbg(1, "Waiting for ino #%u in state %d\n",
0659                       ic->ino, ic->state);
0660                 sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
0661             } else {
0662                 spin_unlock(&c->inocache_lock);
0663             }
0664 
0665             return NULL;
0666         }
0667     } else {
0668         /* Inode has links to it still; they're not going away because
0669            jffs2_do_unlink() would need the alloc_sem and we have it.
0670            Just iget() it, and if read_inode() is necessary that's OK.
0671         */
0672         inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
0673         if (IS_ERR(inode))
0674             return ERR_CAST(inode);
0675     }
0676     if (is_bad_inode(inode)) {
0677         pr_notice("Eep. read_inode() failed for ino #%u. unlinked %d\n",
0678               inum, unlinked);
0679         /* NB. This will happen again. We need to do something appropriate here. */
0680         iput(inode);
0681         return ERR_PTR(-EIO);
0682     }
0683 
0684     return JFFS2_INODE_INFO(inode);
0685 }
0686 
0687 static int jffs2_flash_setup(struct jffs2_sb_info *c) {
0688     int ret = 0;
0689 
0690     if (jffs2_cleanmarker_oob(c)) {
0691         /* NAND flash... do setup accordingly */
0692         ret = jffs2_nand_flash_setup(c);
0693         if (ret)
0694             return ret;
0695     }
0696 
0697     /* and Dataflash */
0698     if (jffs2_dataflash(c)) {
0699         ret = jffs2_dataflash_setup(c);
0700         if (ret)
0701             return ret;
0702     }
0703 
0704     /* and Intel "Sibley" flash */
0705     if (jffs2_nor_wbuf_flash(c)) {
0706         ret = jffs2_nor_wbuf_flash_setup(c);
0707         if (ret)
0708             return ret;
0709     }
0710 
0711     /* and an UBI volume */
0712     if (jffs2_ubivol(c)) {
0713         ret = jffs2_ubivol_setup(c);
0714         if (ret)
0715             return ret;
0716     }
0717 
0718     return ret;
0719 }
0720 
0721 void jffs2_flash_cleanup(struct jffs2_sb_info *c) {
0722 
0723     if (jffs2_cleanmarker_oob(c)) {
0724         jffs2_nand_flash_cleanup(c);
0725     }
0726 
0727     /* and DataFlash */
0728     if (jffs2_dataflash(c)) {
0729         jffs2_dataflash_cleanup(c);
0730     }
0731 
0732     /* and Intel "Sibley" flash */
0733     if (jffs2_nor_wbuf_flash(c)) {
0734         jffs2_nor_wbuf_flash_cleanup(c);
0735     }
0736 
0737     /* and an UBI volume */
0738     if (jffs2_ubivol(c)) {
0739         jffs2_ubivol_cleanup(c);
0740     }
0741 }