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/kernel.h>
0016 #include <linux/fs.h>
0017 #include <linux/time.h>
0018 #include <linux/pagemap.h>
0019 #include <linux/highmem.h>
0020 #include <linux/crc32.h>
0021 #include <linux/jffs2.h>
0022 #include "nodelist.h"
0023 
0024 static int jffs2_write_end(struct file *filp, struct address_space *mapping,
0025             loff_t pos, unsigned len, unsigned copied,
0026             struct page *pg, void *fsdata);
0027 static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
0028             loff_t pos, unsigned len,
0029             struct page **pagep, void **fsdata);
0030 static int jffs2_read_folio(struct file *filp, struct folio *folio);
0031 
0032 int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
0033 {
0034     struct inode *inode = filp->f_mapping->host;
0035     struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
0036     int ret;
0037 
0038     ret = file_write_and_wait_range(filp, start, end);
0039     if (ret)
0040         return ret;
0041 
0042     inode_lock(inode);
0043     /* Trigger GC to flush any pending writes for this inode */
0044     jffs2_flush_wbuf_gc(c, inode->i_ino);
0045     inode_unlock(inode);
0046 
0047     return 0;
0048 }
0049 
0050 const struct file_operations jffs2_file_operations =
0051 {
0052     .llseek =   generic_file_llseek,
0053     .open =     generic_file_open,
0054     .read_iter =    generic_file_read_iter,
0055     .write_iter =   generic_file_write_iter,
0056     .unlocked_ioctl=jffs2_ioctl,
0057     .mmap =     generic_file_readonly_mmap,
0058     .fsync =    jffs2_fsync,
0059     .splice_read =  generic_file_splice_read,
0060     .splice_write = iter_file_splice_write,
0061 };
0062 
0063 /* jffs2_file_inode_operations */
0064 
0065 const struct inode_operations jffs2_file_inode_operations =
0066 {
0067     .get_acl =  jffs2_get_acl,
0068     .set_acl =  jffs2_set_acl,
0069     .setattr =  jffs2_setattr,
0070     .listxattr =    jffs2_listxattr,
0071 };
0072 
0073 const struct address_space_operations jffs2_file_address_operations =
0074 {
0075     .read_folio =   jffs2_read_folio,
0076     .write_begin =  jffs2_write_begin,
0077     .write_end =    jffs2_write_end,
0078 };
0079 
0080 static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
0081 {
0082     struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
0083     struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
0084     unsigned char *pg_buf;
0085     int ret;
0086 
0087     jffs2_dbg(2, "%s(): ino #%lu, page at offset 0x%lx\n",
0088           __func__, inode->i_ino, pg->index << PAGE_SHIFT);
0089 
0090     BUG_ON(!PageLocked(pg));
0091 
0092     pg_buf = kmap(pg);
0093     /* FIXME: Can kmap fail? */
0094 
0095     ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_SHIFT,
0096                      PAGE_SIZE);
0097 
0098     if (ret) {
0099         ClearPageUptodate(pg);
0100         SetPageError(pg);
0101     } else {
0102         SetPageUptodate(pg);
0103         ClearPageError(pg);
0104     }
0105 
0106     flush_dcache_page(pg);
0107     kunmap(pg);
0108 
0109     jffs2_dbg(2, "readpage finished\n");
0110     return ret;
0111 }
0112 
0113 int __jffs2_read_folio(struct file *file, struct folio *folio)
0114 {
0115     int ret = jffs2_do_readpage_nolock(folio->mapping->host, &folio->page);
0116     folio_unlock(folio);
0117     return ret;
0118 }
0119 
0120 static int jffs2_read_folio(struct file *file, struct folio *folio)
0121 {
0122     struct jffs2_inode_info *f = JFFS2_INODE_INFO(folio->mapping->host);
0123     int ret;
0124 
0125     mutex_lock(&f->sem);
0126     ret = __jffs2_read_folio(file, folio);
0127     mutex_unlock(&f->sem);
0128     return ret;
0129 }
0130 
0131 static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
0132             loff_t pos, unsigned len,
0133             struct page **pagep, void **fsdata)
0134 {
0135     struct page *pg;
0136     struct inode *inode = mapping->host;
0137     struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
0138     struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
0139     pgoff_t index = pos >> PAGE_SHIFT;
0140     uint32_t pageofs = index << PAGE_SHIFT;
0141     int ret = 0;
0142 
0143     jffs2_dbg(1, "%s()\n", __func__);
0144 
0145     if (pageofs > inode->i_size) {
0146         /* Make new hole frag from old EOF to new page */
0147         struct jffs2_raw_inode ri;
0148         struct jffs2_full_dnode *fn;
0149         uint32_t alloc_len;
0150 
0151         jffs2_dbg(1, "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
0152               (unsigned int)inode->i_size, pageofs);
0153 
0154         ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
0155                       ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
0156         if (ret)
0157             goto out_err;
0158 
0159         mutex_lock(&f->sem);
0160         memset(&ri, 0, sizeof(ri));
0161 
0162         ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
0163         ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
0164         ri.totlen = cpu_to_je32(sizeof(ri));
0165         ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
0166 
0167         ri.ino = cpu_to_je32(f->inocache->ino);
0168         ri.version = cpu_to_je32(++f->highest_version);
0169         ri.mode = cpu_to_jemode(inode->i_mode);
0170         ri.uid = cpu_to_je16(i_uid_read(inode));
0171         ri.gid = cpu_to_je16(i_gid_read(inode));
0172         ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
0173         ri.atime = ri.ctime = ri.mtime = cpu_to_je32(JFFS2_NOW());
0174         ri.offset = cpu_to_je32(inode->i_size);
0175         ri.dsize = cpu_to_je32(pageofs - inode->i_size);
0176         ri.csize = cpu_to_je32(0);
0177         ri.compr = JFFS2_COMPR_ZERO;
0178         ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
0179         ri.data_crc = cpu_to_je32(0);
0180 
0181         fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
0182 
0183         if (IS_ERR(fn)) {
0184             ret = PTR_ERR(fn);
0185             jffs2_complete_reservation(c);
0186             mutex_unlock(&f->sem);
0187             goto out_err;
0188         }
0189         ret = jffs2_add_full_dnode_to_inode(c, f, fn);
0190         if (f->metadata) {
0191             jffs2_mark_node_obsolete(c, f->metadata->raw);
0192             jffs2_free_full_dnode(f->metadata);
0193             f->metadata = NULL;
0194         }
0195         if (ret) {
0196             jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n",
0197                   ret);
0198             jffs2_mark_node_obsolete(c, fn->raw);
0199             jffs2_free_full_dnode(fn);
0200             jffs2_complete_reservation(c);
0201             mutex_unlock(&f->sem);
0202             goto out_err;
0203         }
0204         jffs2_complete_reservation(c);
0205         inode->i_size = pageofs;
0206         mutex_unlock(&f->sem);
0207     }
0208 
0209     /*
0210      * While getting a page and reading data in, lock c->alloc_sem until
0211      * the page is Uptodate. Otherwise GC task may attempt to read the same
0212      * page in read_cache_page(), which causes a deadlock.
0213      */
0214     mutex_lock(&c->alloc_sem);
0215     pg = grab_cache_page_write_begin(mapping, index);
0216     if (!pg) {
0217         ret = -ENOMEM;
0218         goto release_sem;
0219     }
0220     *pagep = pg;
0221 
0222     /*
0223      * Read in the page if it wasn't already present. Cannot optimize away
0224      * the whole page write case until jffs2_write_end can handle the
0225      * case of a short-copy.
0226      */
0227     if (!PageUptodate(pg)) {
0228         mutex_lock(&f->sem);
0229         ret = jffs2_do_readpage_nolock(inode, pg);
0230         mutex_unlock(&f->sem);
0231         if (ret) {
0232             unlock_page(pg);
0233             put_page(pg);
0234             goto release_sem;
0235         }
0236     }
0237     jffs2_dbg(1, "end write_begin(). pg->flags %lx\n", pg->flags);
0238 
0239 release_sem:
0240     mutex_unlock(&c->alloc_sem);
0241 out_err:
0242     return ret;
0243 }
0244 
0245 static int jffs2_write_end(struct file *filp, struct address_space *mapping,
0246             loff_t pos, unsigned len, unsigned copied,
0247             struct page *pg, void *fsdata)
0248 {
0249     /* Actually commit the write from the page cache page we're looking at.
0250      * For now, we write the full page out each time. It sucks, but it's simple
0251      */
0252     struct inode *inode = mapping->host;
0253     struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
0254     struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
0255     struct jffs2_raw_inode *ri;
0256     unsigned start = pos & (PAGE_SIZE - 1);
0257     unsigned end = start + copied;
0258     unsigned aligned_start = start & ~3;
0259     int ret = 0;
0260     uint32_t writtenlen = 0;
0261 
0262     jffs2_dbg(1, "%s(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
0263           __func__, inode->i_ino, pg->index << PAGE_SHIFT,
0264           start, end, pg->flags);
0265 
0266     /* We need to avoid deadlock with page_cache_read() in
0267        jffs2_garbage_collect_pass(). So the page must be
0268        up to date to prevent page_cache_read() from trying
0269        to re-lock it. */
0270     BUG_ON(!PageUptodate(pg));
0271 
0272     if (end == PAGE_SIZE) {
0273         /* When writing out the end of a page, write out the
0274            _whole_ page. This helps to reduce the number of
0275            nodes in files which have many short writes, like
0276            syslog files. */
0277         aligned_start = 0;
0278     }
0279 
0280     ri = jffs2_alloc_raw_inode();
0281 
0282     if (!ri) {
0283         jffs2_dbg(1, "%s(): Allocation of raw inode failed\n",
0284               __func__);
0285         unlock_page(pg);
0286         put_page(pg);
0287         return -ENOMEM;
0288     }
0289 
0290     /* Set the fields that the generic jffs2_write_inode_range() code can't find */
0291     ri->ino = cpu_to_je32(inode->i_ino);
0292     ri->mode = cpu_to_jemode(inode->i_mode);
0293     ri->uid = cpu_to_je16(i_uid_read(inode));
0294     ri->gid = cpu_to_je16(i_gid_read(inode));
0295     ri->isize = cpu_to_je32((uint32_t)inode->i_size);
0296     ri->atime = ri->ctime = ri->mtime = cpu_to_je32(JFFS2_NOW());
0297 
0298     /* In 2.4, it was already kmapped by generic_file_write(). Doesn't
0299        hurt to do it again. The alternative is ifdefs, which are ugly. */
0300     kmap(pg);
0301 
0302     ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
0303                       (pg->index << PAGE_SHIFT) + aligned_start,
0304                       end - aligned_start, &writtenlen);
0305 
0306     kunmap(pg);
0307 
0308     if (ret) {
0309         /* There was an error writing. */
0310         SetPageError(pg);
0311     }
0312 
0313     /* Adjust writtenlen for the padding we did, so we don't confuse our caller */
0314     writtenlen -= min(writtenlen, (start - aligned_start));
0315 
0316     if (writtenlen) {
0317         if (inode->i_size < pos + writtenlen) {
0318             inode->i_size = pos + writtenlen;
0319             inode->i_blocks = (inode->i_size + 511) >> 9;
0320 
0321             inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
0322         }
0323     }
0324 
0325     jffs2_free_raw_inode(ri);
0326 
0327     if (start+writtenlen < end) {
0328         /* generic_file_write has written more to the page cache than we've
0329            actually written to the medium. Mark the page !Uptodate so that
0330            it gets reread */
0331         jffs2_dbg(1, "%s(): Not all bytes written. Marking page !uptodate\n",
0332             __func__);
0333         SetPageError(pg);
0334         ClearPageUptodate(pg);
0335     }
0336 
0337     jffs2_dbg(1, "%s() returning %d\n",
0338           __func__, writtenlen > 0 ? writtenlen : ret);
0339     unlock_page(pg);
0340     put_page(pg);
0341     return writtenlen > 0 ? writtenlen : ret;
0342 }