^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * JFFS2 -- Journalling Flash File System, Version 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright © 2001-2007 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Created by David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * For licensing information, see the file 'LICENCE' in this directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/jffs2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "nodelist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int jffs2_write_end(struct file *filp, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct page *pg, void *fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct page **pagep, void **fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int jffs2_readpage (struct file *filp, struct page *pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int jffs2_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct inode *inode = filp->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ret = file_write_and_wait_range(filp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Trigger GC to flush any pending writes for this inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) jffs2_flush_wbuf_gc(c, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const struct file_operations jffs2_file_operations =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .open = generic_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .read_iter = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .write_iter = generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .unlocked_ioctl=jffs2_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .mmap = generic_file_readonly_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .fsync = jffs2_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .splice_write = iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* jffs2_file_inode_operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) const struct inode_operations jffs2_file_inode_operations =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .get_acl = jffs2_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .set_acl = jffs2_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .setattr = jffs2_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .listxattr = jffs2_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) const struct address_space_operations jffs2_file_address_operations =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .readpage = jffs2_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .write_begin = jffs2_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .write_end = jffs2_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned char *pg_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) jffs2_dbg(2, "%s(): ino #%lu, page at offset 0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) __func__, inode->i_ino, pg->index << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) BUG_ON(!PageLocked(pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) pg_buf = kmap(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* FIXME: Can kmap fail? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = jffs2_read_inode_range(c, f, pg_buf, pg->index << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ClearPageUptodate(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) SetPageError(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) SetPageUptodate(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ClearPageError(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) flush_dcache_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) kunmap(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) jffs2_dbg(2, "readpage finished\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int jffs2_do_readpage_unlock(void *data, struct page *pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int ret = jffs2_do_readpage_nolock(data, pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unlock_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int jffs2_readpage (struct file *filp, struct page *pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct jffs2_inode_info *f = JFFS2_INODE_INFO(pg->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ret = jffs2_do_readpage_unlock(pg->mapping->host, pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int jffs2_write_begin(struct file *filp, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct page *pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pgoff_t index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) uint32_t pageofs = index << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) jffs2_dbg(1, "%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (pageofs > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Make new hole frag from old EOF to new page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct jffs2_raw_inode ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct jffs2_full_dnode *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) uint32_t alloc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) jffs2_dbg(1, "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) (unsigned int)inode->i_size, pageofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ret = jffs2_reserve_space(c, sizeof(ri), &alloc_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) memset(&ri, 0, sizeof(ri));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ri.totlen = cpu_to_je32(sizeof(ri));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ri.hdr_crc = cpu_to_je32(crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ri.ino = cpu_to_je32(f->inocache->ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ri.version = cpu_to_je32(++f->highest_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ri.mode = cpu_to_jemode(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ri.uid = cpu_to_je16(i_uid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ri.gid = cpu_to_je16(i_gid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ri.isize = cpu_to_je32(max((uint32_t)inode->i_size, pageofs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ri.atime = ri.ctime = ri.mtime = cpu_to_je32(JFFS2_NOW());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ri.offset = cpu_to_je32(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ri.dsize = cpu_to_je32(pageofs - inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ri.csize = cpu_to_je32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ri.compr = JFFS2_COMPR_ZERO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ri.data_crc = cpu_to_je32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (IS_ERR(fn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = PTR_ERR(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ret = jffs2_add_full_dnode_to_inode(c, f, fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (f->metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) jffs2_mark_node_obsolete(c, f->metadata->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) jffs2_free_full_dnode(f->metadata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) f->metadata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) jffs2_dbg(1, "Eep. add_full_dnode_to_inode() failed in write_begin, returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) jffs2_mark_node_obsolete(c, fn->raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) jffs2_free_full_dnode(fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) jffs2_complete_reservation(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) inode->i_size = pageofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * While getting a page and reading data in, lock c->alloc_sem until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * the page is Uptodate. Otherwise GC task may attempt to read the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * page in read_cache_page(), which causes a deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) mutex_lock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pg = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!pg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) goto release_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *pagep = pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * Read in the page if it wasn't already present. Cannot optimize away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * the whole page write case until jffs2_write_end can handle the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * case of a short-copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!PageUptodate(pg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) mutex_lock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ret = jffs2_do_readpage_nolock(inode, pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) mutex_unlock(&f->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unlock_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) put_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto release_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) jffs2_dbg(1, "end write_begin(). pg->flags %lx\n", pg->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) release_sem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) mutex_unlock(&c->alloc_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static int jffs2_write_end(struct file *filp, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct page *pg, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* Actually commit the write from the page cache page we're looking at.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * For now, we write the full page out each time. It sucks, but it's simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct jffs2_raw_inode *ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) unsigned start = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) unsigned end = start + copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) unsigned aligned_start = start & ~3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) uint32_t writtenlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) jffs2_dbg(1, "%s(): ino #%lu, page at 0x%lx, range %d-%d, flags %lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) __func__, inode->i_ino, pg->index << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) start, end, pg->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* We need to avoid deadlock with page_cache_read() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) jffs2_garbage_collect_pass(). So the page must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) up to date to prevent page_cache_read() from trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) to re-lock it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) BUG_ON(!PageUptodate(pg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (end == PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* When writing out the end of a page, write out the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) _whole_ page. This helps to reduce the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) nodes in files which have many short writes, like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) syslog files. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) aligned_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) ri = jffs2_alloc_raw_inode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (!ri) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) jffs2_dbg(1, "%s(): Allocation of raw inode failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) unlock_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) put_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* Set the fields that the generic jffs2_write_inode_range() code can't find */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ri->ino = cpu_to_je32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) ri->mode = cpu_to_jemode(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ri->uid = cpu_to_je16(i_uid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ri->gid = cpu_to_je16(i_gid_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ri->isize = cpu_to_je32((uint32_t)inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ri->atime = ri->ctime = ri->mtime = cpu_to_je32(JFFS2_NOW());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* In 2.4, it was already kmapped by generic_file_write(). Doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) hurt to do it again. The alternative is ifdefs, which are ugly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) kmap(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ret = jffs2_write_inode_range(c, f, ri, page_address(pg) + aligned_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) (pg->index << PAGE_SHIFT) + aligned_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) end - aligned_start, &writtenlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) kunmap(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* There was an error writing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) SetPageError(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* Adjust writtenlen for the padding we did, so we don't confuse our caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) writtenlen -= min(writtenlen, (start - aligned_start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (writtenlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (inode->i_size < pos + writtenlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) inode->i_size = pos + writtenlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) inode->i_blocks = (inode->i_size + 511) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) inode->i_ctime = inode->i_mtime = ITIME(je32_to_cpu(ri->ctime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) jffs2_free_raw_inode(ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (start+writtenlen < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* generic_file_write has written more to the page cache than we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) actually written to the medium. Mark the page !Uptodate so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) it gets reread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) jffs2_dbg(1, "%s(): Not all bytes written. Marking page !uptodate\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) SetPageError(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ClearPageUptodate(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) jffs2_dbg(1, "%s() returning %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) __func__, writtenlen > 0 ? writtenlen : ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) unlock_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) put_page(pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return writtenlen > 0 ? writtenlen : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }