11e51764aSArtem Bityutskiy /* 21e51764aSArtem Bityutskiy * This file is part of UBIFS. 31e51764aSArtem Bityutskiy * 41e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation. 51e51764aSArtem Bityutskiy * 61e51764aSArtem Bityutskiy * This program is free software; you can redistribute it and/or modify it 71e51764aSArtem Bityutskiy * under the terms of the GNU General Public License version 2 as published by 81e51764aSArtem Bityutskiy * the Free Software Foundation. 91e51764aSArtem Bityutskiy * 101e51764aSArtem Bityutskiy * This program is distributed in the hope that it will be useful, but WITHOUT 111e51764aSArtem Bityutskiy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 121e51764aSArtem Bityutskiy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 131e51764aSArtem Bityutskiy * more details. 141e51764aSArtem Bityutskiy * 151e51764aSArtem Bityutskiy * You should have received a copy of the GNU General Public License along with 161e51764aSArtem Bityutskiy * this program; if not, write to the Free Software Foundation, Inc., 51 171e51764aSArtem Bityutskiy * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 181e51764aSArtem Bityutskiy * 191e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём) 201e51764aSArtem Bityutskiy * Adrian Hunter 211e51764aSArtem Bityutskiy */ 221e51764aSArtem Bityutskiy 231e51764aSArtem Bityutskiy /* 241e51764aSArtem Bityutskiy * This file implements VFS file and inode operations of regular files, device 251e51764aSArtem Bityutskiy * nodes and symlinks as well as address space operations. 261e51764aSArtem Bityutskiy * 271e51764aSArtem Bityutskiy * UBIFS uses 2 page flags: PG_private and PG_checked. PG_private is set if the 281e51764aSArtem Bityutskiy * page is dirty and is used for budgeting purposes - dirty pages should not be 291e51764aSArtem Bityutskiy * budgeted. The PG_checked flag is set if full budgeting is required for the 301e51764aSArtem Bityutskiy * page e.g., when it corresponds to a file hole or it is just beyond the file 311e51764aSArtem Bityutskiy * size. The budgeting is done in 'ubifs_write_begin()', because it is OK to 321e51764aSArtem Bityutskiy * fail in this function, and the budget is released in 'ubifs_write_end()'. So 331e51764aSArtem Bityutskiy * the PG_private and PG_checked flags carry the information about how the page 341e51764aSArtem Bityutskiy * was budgeted, to make it possible to release the budget properly. 351e51764aSArtem Bityutskiy * 361e51764aSArtem Bityutskiy * A thing to keep in mind: inode's 'i_mutex' is locked in most VFS operations 371e51764aSArtem Bityutskiy * we implement. However, this is not true for '->writepage()', which might be 381e51764aSArtem Bityutskiy * called with 'i_mutex' unlocked. For example, when pdflush is performing 391e51764aSArtem Bityutskiy * write-back, it calls 'writepage()' with unlocked 'i_mutex', although the 401e51764aSArtem Bityutskiy * inode has 'I_LOCK' flag in this case. At "normal" work-paths 'i_mutex' is 411e51764aSArtem Bityutskiy * locked in '->writepage', e.g. in "sys_write -> alloc_pages -> direct reclaim 421e51764aSArtem Bityutskiy * path'. So, in '->writepage()' we are only guaranteed that the page is 431e51764aSArtem Bityutskiy * locked. 441e51764aSArtem Bityutskiy * 451e51764aSArtem Bityutskiy * Similarly, 'i_mutex' does not have to be locked in readpage(), e.g., 461e51764aSArtem Bityutskiy * readahead path does not have it locked ("sys_read -> generic_file_aio_read 471e51764aSArtem Bityutskiy * -> ondemand_readahead -> readpage"). In case of readahead, 'I_LOCK' flag is 481e51764aSArtem Bityutskiy * not set as well. However, UBIFS disables readahead. 491e51764aSArtem Bityutskiy * 501e51764aSArtem Bityutskiy * This, for example means that there might be 2 concurrent '->writepage()' 511e51764aSArtem Bityutskiy * calls for the same inode, but different inode dirty pages. 521e51764aSArtem Bityutskiy */ 531e51764aSArtem Bityutskiy 541e51764aSArtem Bityutskiy #include "ubifs.h" 551e51764aSArtem Bityutskiy #include <linux/mount.h> 563f8206d4SAl Viro #include <linux/namei.h> 571e51764aSArtem Bityutskiy 581e51764aSArtem Bityutskiy static int read_block(struct inode *inode, void *addr, unsigned int block, 591e51764aSArtem Bityutskiy struct ubifs_data_node *dn) 601e51764aSArtem Bityutskiy { 611e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 621e51764aSArtem Bityutskiy int err, len, out_len; 631e51764aSArtem Bityutskiy union ubifs_key key; 641e51764aSArtem Bityutskiy unsigned int dlen; 651e51764aSArtem Bityutskiy 661e51764aSArtem Bityutskiy data_key_init(c, &key, inode->i_ino, block); 671e51764aSArtem Bityutskiy err = ubifs_tnc_lookup(c, &key, dn); 681e51764aSArtem Bityutskiy if (err) { 691e51764aSArtem Bityutskiy if (err == -ENOENT) 701e51764aSArtem Bityutskiy /* Not found, so it must be a hole */ 711e51764aSArtem Bityutskiy memset(addr, 0, UBIFS_BLOCK_SIZE); 721e51764aSArtem Bityutskiy return err; 731e51764aSArtem Bityutskiy } 741e51764aSArtem Bityutskiy 75f92b9826SArtem Bityutskiy ubifs_assert(le64_to_cpu(dn->ch.sqnum) > 76f92b9826SArtem Bityutskiy ubifs_inode(inode)->creat_sqnum); 771e51764aSArtem Bityutskiy len = le32_to_cpu(dn->size); 781e51764aSArtem Bityutskiy if (len <= 0 || len > UBIFS_BLOCK_SIZE) 791e51764aSArtem Bityutskiy goto dump; 801e51764aSArtem Bityutskiy 811e51764aSArtem Bityutskiy dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; 821e51764aSArtem Bityutskiy out_len = UBIFS_BLOCK_SIZE; 831e51764aSArtem Bityutskiy err = ubifs_decompress(&dn->data, dlen, addr, &out_len, 841e51764aSArtem Bityutskiy le16_to_cpu(dn->compr_type)); 851e51764aSArtem Bityutskiy if (err || len != out_len) 861e51764aSArtem Bityutskiy goto dump; 871e51764aSArtem Bityutskiy 881e51764aSArtem Bityutskiy /* 891e51764aSArtem Bityutskiy * Data length can be less than a full block, even for blocks that are 901e51764aSArtem Bityutskiy * not the last in the file (e.g., as a result of making a hole and 911e51764aSArtem Bityutskiy * appending data). Ensure that the remainder is zeroed out. 921e51764aSArtem Bityutskiy */ 931e51764aSArtem Bityutskiy if (len < UBIFS_BLOCK_SIZE) 941e51764aSArtem Bityutskiy memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); 951e51764aSArtem Bityutskiy 961e51764aSArtem Bityutskiy return 0; 971e51764aSArtem Bityutskiy 981e51764aSArtem Bityutskiy dump: 991e51764aSArtem Bityutskiy ubifs_err("bad data node (block %u, inode %lu)", 1001e51764aSArtem Bityutskiy block, inode->i_ino); 1011e51764aSArtem Bityutskiy dbg_dump_node(c, dn); 1021e51764aSArtem Bityutskiy return -EINVAL; 1031e51764aSArtem Bityutskiy } 1041e51764aSArtem Bityutskiy 1051e51764aSArtem Bityutskiy static int do_readpage(struct page *page) 1061e51764aSArtem Bityutskiy { 1071e51764aSArtem Bityutskiy void *addr; 1081e51764aSArtem Bityutskiy int err = 0, i; 1091e51764aSArtem Bityutskiy unsigned int block, beyond; 1101e51764aSArtem Bityutskiy struct ubifs_data_node *dn; 1111e51764aSArtem Bityutskiy struct inode *inode = page->mapping->host; 1121e51764aSArtem Bityutskiy loff_t i_size = i_size_read(inode); 1131e51764aSArtem Bityutskiy 1141e51764aSArtem Bityutskiy dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx", 1151e51764aSArtem Bityutskiy inode->i_ino, page->index, i_size, page->flags); 1161e51764aSArtem Bityutskiy ubifs_assert(!PageChecked(page)); 1171e51764aSArtem Bityutskiy ubifs_assert(!PagePrivate(page)); 1181e51764aSArtem Bityutskiy 1191e51764aSArtem Bityutskiy addr = kmap(page); 1201e51764aSArtem Bityutskiy 1211e51764aSArtem Bityutskiy block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; 1221e51764aSArtem Bityutskiy beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; 1231e51764aSArtem Bityutskiy if (block >= beyond) { 1241e51764aSArtem Bityutskiy /* Reading beyond inode */ 1251e51764aSArtem Bityutskiy SetPageChecked(page); 1261e51764aSArtem Bityutskiy memset(addr, 0, PAGE_CACHE_SIZE); 1271e51764aSArtem Bityutskiy goto out; 1281e51764aSArtem Bityutskiy } 1291e51764aSArtem Bityutskiy 1301e51764aSArtem Bityutskiy dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS); 1311e51764aSArtem Bityutskiy if (!dn) { 1321e51764aSArtem Bityutskiy err = -ENOMEM; 1331e51764aSArtem Bityutskiy goto error; 1341e51764aSArtem Bityutskiy } 1351e51764aSArtem Bityutskiy 1361e51764aSArtem Bityutskiy i = 0; 1371e51764aSArtem Bityutskiy while (1) { 1381e51764aSArtem Bityutskiy int ret; 1391e51764aSArtem Bityutskiy 1401e51764aSArtem Bityutskiy if (block >= beyond) { 1411e51764aSArtem Bityutskiy /* Reading beyond inode */ 1421e51764aSArtem Bityutskiy err = -ENOENT; 1431e51764aSArtem Bityutskiy memset(addr, 0, UBIFS_BLOCK_SIZE); 1441e51764aSArtem Bityutskiy } else { 1451e51764aSArtem Bityutskiy ret = read_block(inode, addr, block, dn); 1461e51764aSArtem Bityutskiy if (ret) { 1471e51764aSArtem Bityutskiy err = ret; 1481e51764aSArtem Bityutskiy if (err != -ENOENT) 1491e51764aSArtem Bityutskiy break; 150ed382d58SAdrian Hunter } else if (block + 1 == beyond) { 151ed382d58SAdrian Hunter int dlen = le32_to_cpu(dn->size); 152ed382d58SAdrian Hunter int ilen = i_size & (UBIFS_BLOCK_SIZE - 1); 153ed382d58SAdrian Hunter 154ed382d58SAdrian Hunter if (ilen && ilen < dlen) 155ed382d58SAdrian Hunter memset(addr + ilen, 0, dlen - ilen); 1561e51764aSArtem Bityutskiy } 1571e51764aSArtem Bityutskiy } 1581e51764aSArtem Bityutskiy if (++i >= UBIFS_BLOCKS_PER_PAGE) 1591e51764aSArtem Bityutskiy break; 1601e51764aSArtem Bityutskiy block += 1; 1611e51764aSArtem Bityutskiy addr += UBIFS_BLOCK_SIZE; 1621e51764aSArtem Bityutskiy } 1631e51764aSArtem Bityutskiy if (err) { 1641e51764aSArtem Bityutskiy if (err == -ENOENT) { 1651e51764aSArtem Bityutskiy /* Not found, so it must be a hole */ 1661e51764aSArtem Bityutskiy SetPageChecked(page); 1671e51764aSArtem Bityutskiy dbg_gen("hole"); 1681e51764aSArtem Bityutskiy goto out_free; 1691e51764aSArtem Bityutskiy } 1701e51764aSArtem Bityutskiy ubifs_err("cannot read page %lu of inode %lu, error %d", 1711e51764aSArtem Bityutskiy page->index, inode->i_ino, err); 1721e51764aSArtem Bityutskiy goto error; 1731e51764aSArtem Bityutskiy } 1741e51764aSArtem Bityutskiy 1751e51764aSArtem Bityutskiy out_free: 1761e51764aSArtem Bityutskiy kfree(dn); 1771e51764aSArtem Bityutskiy out: 1781e51764aSArtem Bityutskiy SetPageUptodate(page); 1791e51764aSArtem Bityutskiy ClearPageError(page); 1801e51764aSArtem Bityutskiy flush_dcache_page(page); 1811e51764aSArtem Bityutskiy kunmap(page); 1821e51764aSArtem Bityutskiy return 0; 1831e51764aSArtem Bityutskiy 1841e51764aSArtem Bityutskiy error: 1851e51764aSArtem Bityutskiy kfree(dn); 1861e51764aSArtem Bityutskiy ClearPageUptodate(page); 1871e51764aSArtem Bityutskiy SetPageError(page); 1881e51764aSArtem Bityutskiy flush_dcache_page(page); 1891e51764aSArtem Bityutskiy kunmap(page); 1901e51764aSArtem Bityutskiy return err; 1911e51764aSArtem Bityutskiy } 1921e51764aSArtem Bityutskiy 1931e51764aSArtem Bityutskiy /** 1941e51764aSArtem Bityutskiy * release_new_page_budget - release budget of a new page. 1951e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 1961e51764aSArtem Bityutskiy * 1971e51764aSArtem Bityutskiy * This is a helper function which releases budget corresponding to the budget 1981e51764aSArtem Bityutskiy * of one new page of data. 1991e51764aSArtem Bityutskiy */ 2001e51764aSArtem Bityutskiy static void release_new_page_budget(struct ubifs_info *c) 2011e51764aSArtem Bityutskiy { 2021e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .recalculate = 1, .new_page = 1 }; 2031e51764aSArtem Bityutskiy 2041e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 2051e51764aSArtem Bityutskiy } 2061e51764aSArtem Bityutskiy 2071e51764aSArtem Bityutskiy /** 2081e51764aSArtem Bityutskiy * release_existing_page_budget - release budget of an existing page. 2091e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 2101e51764aSArtem Bityutskiy * 2111e51764aSArtem Bityutskiy * This is a helper function which releases budget corresponding to the budget 2121e51764aSArtem Bityutskiy * of changing one one page of data which already exists on the flash media. 2131e51764aSArtem Bityutskiy */ 2141e51764aSArtem Bityutskiy static void release_existing_page_budget(struct ubifs_info *c) 2151e51764aSArtem Bityutskiy { 2161e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .dd_growth = c->page_budget}; 2171e51764aSArtem Bityutskiy 2181e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 2191e51764aSArtem Bityutskiy } 2201e51764aSArtem Bityutskiy 2211e51764aSArtem Bityutskiy static int write_begin_slow(struct address_space *mapping, 22254566b2cSNick Piggin loff_t pos, unsigned len, struct page **pagep, 22354566b2cSNick Piggin unsigned flags) 2241e51764aSArtem Bityutskiy { 2251e51764aSArtem Bityutskiy struct inode *inode = mapping->host; 2261e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 2271e51764aSArtem Bityutskiy pgoff_t index = pos >> PAGE_CACHE_SHIFT; 2281e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .new_page = 1 }; 2291e51764aSArtem Bityutskiy int uninitialized_var(err), appending = !!(pos + len > inode->i_size); 2301e51764aSArtem Bityutskiy struct page *page; 2311e51764aSArtem Bityutskiy 2321e51764aSArtem Bityutskiy dbg_gen("ino %lu, pos %llu, len %u, i_size %lld", 2331e51764aSArtem Bityutskiy inode->i_ino, pos, len, inode->i_size); 2341e51764aSArtem Bityutskiy 2351e51764aSArtem Bityutskiy /* 2361e51764aSArtem Bityutskiy * At the slow path we have to budget before locking the page, because 2371e51764aSArtem Bityutskiy * budgeting may force write-back, which would wait on locked pages and 2381e51764aSArtem Bityutskiy * deadlock if we had the page locked. At this point we do not know 2391e51764aSArtem Bityutskiy * anything about the page, so assume that this is a new page which is 2401e51764aSArtem Bityutskiy * written to a hole. This corresponds to largest budget. Later the 2411e51764aSArtem Bityutskiy * budget will be amended if this is not true. 2421e51764aSArtem Bityutskiy */ 2431e51764aSArtem Bityutskiy if (appending) 2441e51764aSArtem Bityutskiy /* We are appending data, budget for inode change */ 2451e51764aSArtem Bityutskiy req.dirtied_ino = 1; 2461e51764aSArtem Bityutskiy 2471e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 2481e51764aSArtem Bityutskiy if (unlikely(err)) 2491e51764aSArtem Bityutskiy return err; 2501e51764aSArtem Bityutskiy 25154566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2521e51764aSArtem Bityutskiy if (unlikely(!page)) { 2531e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 2541e51764aSArtem Bityutskiy return -ENOMEM; 2551e51764aSArtem Bityutskiy } 2561e51764aSArtem Bityutskiy 2571e51764aSArtem Bityutskiy if (!PageUptodate(page)) { 2587bbe5b5aSArtem Bityutskiy if (!(pos & ~PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) 2591e51764aSArtem Bityutskiy SetPageChecked(page); 2601e51764aSArtem Bityutskiy else { 2611e51764aSArtem Bityutskiy err = do_readpage(page); 2621e51764aSArtem Bityutskiy if (err) { 2631e51764aSArtem Bityutskiy unlock_page(page); 2641e51764aSArtem Bityutskiy page_cache_release(page); 2651e51764aSArtem Bityutskiy return err; 2661e51764aSArtem Bityutskiy } 2671e51764aSArtem Bityutskiy } 2681e51764aSArtem Bityutskiy 2691e51764aSArtem Bityutskiy SetPageUptodate(page); 2701e51764aSArtem Bityutskiy ClearPageError(page); 2711e51764aSArtem Bityutskiy } 2721e51764aSArtem Bityutskiy 2731e51764aSArtem Bityutskiy if (PagePrivate(page)) 2741e51764aSArtem Bityutskiy /* 2751e51764aSArtem Bityutskiy * The page is dirty, which means it was budgeted twice: 2761e51764aSArtem Bityutskiy * o first time the budget was allocated by the task which 2771e51764aSArtem Bityutskiy * made the page dirty and set the PG_private flag; 2781e51764aSArtem Bityutskiy * o and then we budgeted for it for the second time at the 2791e51764aSArtem Bityutskiy * very beginning of this function. 2801e51764aSArtem Bityutskiy * 2811e51764aSArtem Bityutskiy * So what we have to do is to release the page budget we 2821e51764aSArtem Bityutskiy * allocated. 2831e51764aSArtem Bityutskiy */ 2841e51764aSArtem Bityutskiy release_new_page_budget(c); 2851e51764aSArtem Bityutskiy else if (!PageChecked(page)) 2861e51764aSArtem Bityutskiy /* 2871e51764aSArtem Bityutskiy * We are changing a page which already exists on the media. 2881e51764aSArtem Bityutskiy * This means that changing the page does not make the amount 2891e51764aSArtem Bityutskiy * of indexing information larger, and this part of the budget 2901e51764aSArtem Bityutskiy * which we have already acquired may be released. 2911e51764aSArtem Bityutskiy */ 2921e51764aSArtem Bityutskiy ubifs_convert_page_budget(c); 2931e51764aSArtem Bityutskiy 2941e51764aSArtem Bityutskiy if (appending) { 2951e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 2961e51764aSArtem Bityutskiy 2971e51764aSArtem Bityutskiy /* 2981e51764aSArtem Bityutskiy * 'ubifs_write_end()' is optimized from the fast-path part of 2991e51764aSArtem Bityutskiy * 'ubifs_write_begin()' and expects the @ui_mutex to be locked 3001e51764aSArtem Bityutskiy * if data is appended. 3011e51764aSArtem Bityutskiy */ 3021e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 3031e51764aSArtem Bityutskiy if (ui->dirty) 3041e51764aSArtem Bityutskiy /* 3051e51764aSArtem Bityutskiy * The inode is dirty already, so we may free the 3061e51764aSArtem Bityutskiy * budget we allocated. 3071e51764aSArtem Bityutskiy */ 3081e51764aSArtem Bityutskiy ubifs_release_dirty_inode_budget(c, ui); 3091e51764aSArtem Bityutskiy } 3101e51764aSArtem Bityutskiy 3111e51764aSArtem Bityutskiy *pagep = page; 3121e51764aSArtem Bityutskiy return 0; 3131e51764aSArtem Bityutskiy } 3141e51764aSArtem Bityutskiy 3151e51764aSArtem Bityutskiy /** 3161e51764aSArtem Bityutskiy * allocate_budget - allocate budget for 'ubifs_write_begin()'. 3171e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 3181e51764aSArtem Bityutskiy * @page: page to allocate budget for 3191e51764aSArtem Bityutskiy * @ui: UBIFS inode object the page belongs to 3201e51764aSArtem Bityutskiy * @appending: non-zero if the page is appended 3211e51764aSArtem Bityutskiy * 3221e51764aSArtem Bityutskiy * This is a helper function for 'ubifs_write_begin()' which allocates budget 3231e51764aSArtem Bityutskiy * for the operation. The budget is allocated differently depending on whether 3241e51764aSArtem Bityutskiy * this is appending, whether the page is dirty or not, and so on. This 3251e51764aSArtem Bityutskiy * function leaves the @ui->ui_mutex locked in case of appending. Returns zero 3261e51764aSArtem Bityutskiy * in case of success and %-ENOSPC in case of failure. 3271e51764aSArtem Bityutskiy */ 3281e51764aSArtem Bityutskiy static int allocate_budget(struct ubifs_info *c, struct page *page, 3291e51764aSArtem Bityutskiy struct ubifs_inode *ui, int appending) 3301e51764aSArtem Bityutskiy { 3311e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .fast = 1 }; 3321e51764aSArtem Bityutskiy 3331e51764aSArtem Bityutskiy if (PagePrivate(page)) { 3341e51764aSArtem Bityutskiy if (!appending) 3351e51764aSArtem Bityutskiy /* 3361e51764aSArtem Bityutskiy * The page is dirty and we are not appending, which 3371e51764aSArtem Bityutskiy * means no budget is needed at all. 3381e51764aSArtem Bityutskiy */ 3391e51764aSArtem Bityutskiy return 0; 3401e51764aSArtem Bityutskiy 3411e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 3421e51764aSArtem Bityutskiy if (ui->dirty) 3431e51764aSArtem Bityutskiy /* 3441e51764aSArtem Bityutskiy * The page is dirty and we are appending, so the inode 3451e51764aSArtem Bityutskiy * has to be marked as dirty. However, it is already 3461e51764aSArtem Bityutskiy * dirty, so we do not need any budget. We may return, 3471e51764aSArtem Bityutskiy * but @ui->ui_mutex hast to be left locked because we 3481e51764aSArtem Bityutskiy * should prevent write-back from flushing the inode 3491e51764aSArtem Bityutskiy * and freeing the budget. The lock will be released in 3501e51764aSArtem Bityutskiy * 'ubifs_write_end()'. 3511e51764aSArtem Bityutskiy */ 3521e51764aSArtem Bityutskiy return 0; 3531e51764aSArtem Bityutskiy 3541e51764aSArtem Bityutskiy /* 3551e51764aSArtem Bityutskiy * The page is dirty, we are appending, the inode is clean, so 3561e51764aSArtem Bityutskiy * we need to budget the inode change. 3571e51764aSArtem Bityutskiy */ 3581e51764aSArtem Bityutskiy req.dirtied_ino = 1; 3591e51764aSArtem Bityutskiy } else { 3601e51764aSArtem Bityutskiy if (PageChecked(page)) 3611e51764aSArtem Bityutskiy /* 3621e51764aSArtem Bityutskiy * The page corresponds to a hole and does not 3631e51764aSArtem Bityutskiy * exist on the media. So changing it makes 3641e51764aSArtem Bityutskiy * make the amount of indexing information 3651e51764aSArtem Bityutskiy * larger, and we have to budget for a new 3661e51764aSArtem Bityutskiy * page. 3671e51764aSArtem Bityutskiy */ 3681e51764aSArtem Bityutskiy req.new_page = 1; 3691e51764aSArtem Bityutskiy else 3701e51764aSArtem Bityutskiy /* 3711e51764aSArtem Bityutskiy * Not a hole, the change will not add any new 3721e51764aSArtem Bityutskiy * indexing information, budget for page 3731e51764aSArtem Bityutskiy * change. 3741e51764aSArtem Bityutskiy */ 3751e51764aSArtem Bityutskiy req.dirtied_page = 1; 3761e51764aSArtem Bityutskiy 3771e51764aSArtem Bityutskiy if (appending) { 3781e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 3791e51764aSArtem Bityutskiy if (!ui->dirty) 3801e51764aSArtem Bityutskiy /* 3811e51764aSArtem Bityutskiy * The inode is clean but we will have to mark 3821e51764aSArtem Bityutskiy * it as dirty because we are appending. This 3831e51764aSArtem Bityutskiy * needs a budget. 3841e51764aSArtem Bityutskiy */ 3851e51764aSArtem Bityutskiy req.dirtied_ino = 1; 3861e51764aSArtem Bityutskiy } 3871e51764aSArtem Bityutskiy } 3881e51764aSArtem Bityutskiy 3891e51764aSArtem Bityutskiy return ubifs_budget_space(c, &req); 3901e51764aSArtem Bityutskiy } 3911e51764aSArtem Bityutskiy 3921e51764aSArtem Bityutskiy /* 3931e51764aSArtem Bityutskiy * This function is called when a page of data is going to be written. Since 3941e51764aSArtem Bityutskiy * the page of data will not necessarily go to the flash straight away, UBIFS 3951e51764aSArtem Bityutskiy * has to reserve space on the media for it, which is done by means of 3961e51764aSArtem Bityutskiy * budgeting. 3971e51764aSArtem Bityutskiy * 3981e51764aSArtem Bityutskiy * This is the hot-path of the file-system and we are trying to optimize it as 3991e51764aSArtem Bityutskiy * much as possible. For this reasons it is split on 2 parts - slow and fast. 4001e51764aSArtem Bityutskiy * 4011e51764aSArtem Bityutskiy * There many budgeting cases: 4021e51764aSArtem Bityutskiy * o a new page is appended - we have to budget for a new page and for 4031e51764aSArtem Bityutskiy * changing the inode; however, if the inode is already dirty, there is 4041e51764aSArtem Bityutskiy * no need to budget for it; 4051e51764aSArtem Bityutskiy * o an existing clean page is changed - we have budget for it; if the page 4061e51764aSArtem Bityutskiy * does not exist on the media (a hole), we have to budget for a new 4071e51764aSArtem Bityutskiy * page; otherwise, we may budget for changing an existing page; the 4081e51764aSArtem Bityutskiy * difference between these cases is that changing an existing page does 4091e51764aSArtem Bityutskiy * not introduce anything new to the FS indexing information, so it does 4101e51764aSArtem Bityutskiy * not grow, and smaller budget is acquired in this case; 4111e51764aSArtem Bityutskiy * o an existing dirty page is changed - no need to budget at all, because 4121e51764aSArtem Bityutskiy * the page budget has been acquired by earlier, when the page has been 4131e51764aSArtem Bityutskiy * marked dirty. 4141e51764aSArtem Bityutskiy * 4151e51764aSArtem Bityutskiy * UBIFS budgeting sub-system may force write-back if it thinks there is no 4161e51764aSArtem Bityutskiy * space to reserve. This imposes some locking restrictions and makes it 4171e51764aSArtem Bityutskiy * impossible to take into account the above cases, and makes it impossible to 4181e51764aSArtem Bityutskiy * optimize budgeting. 4191e51764aSArtem Bityutskiy * 4201e51764aSArtem Bityutskiy * The solution for this is that the fast path of 'ubifs_write_begin()' assumes 4211e51764aSArtem Bityutskiy * there is a plenty of flash space and the budget will be acquired quickly, 4221e51764aSArtem Bityutskiy * without forcing write-back. The slow path does not make this assumption. 4231e51764aSArtem Bityutskiy */ 4241e51764aSArtem Bityutskiy static int ubifs_write_begin(struct file *file, struct address_space *mapping, 4251e51764aSArtem Bityutskiy loff_t pos, unsigned len, unsigned flags, 4261e51764aSArtem Bityutskiy struct page **pagep, void **fsdata) 4271e51764aSArtem Bityutskiy { 4281e51764aSArtem Bityutskiy struct inode *inode = mapping->host; 4291e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 4301e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 4311e51764aSArtem Bityutskiy pgoff_t index = pos >> PAGE_CACHE_SHIFT; 4321e51764aSArtem Bityutskiy int uninitialized_var(err), appending = !!(pos + len > inode->i_size); 433f55aa591SAdrian Hunter int skipped_read = 0; 4341e51764aSArtem Bityutskiy struct page *page; 4351e51764aSArtem Bityutskiy 4361e51764aSArtem Bityutskiy ubifs_assert(ubifs_inode(inode)->ui_size == inode->i_size); 4371e51764aSArtem Bityutskiy 4381e51764aSArtem Bityutskiy if (unlikely(c->ro_media)) 4391e51764aSArtem Bityutskiy return -EROFS; 4401e51764aSArtem Bityutskiy 4411e51764aSArtem Bityutskiy /* Try out the fast-path part first */ 44254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 4431e51764aSArtem Bityutskiy if (unlikely(!page)) 4441e51764aSArtem Bityutskiy return -ENOMEM; 4451e51764aSArtem Bityutskiy 4461e51764aSArtem Bityutskiy if (!PageUptodate(page)) { 4471e51764aSArtem Bityutskiy /* The page is not loaded from the flash */ 448f55aa591SAdrian Hunter if (!(pos & ~PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE) { 4491e51764aSArtem Bityutskiy /* 4501e51764aSArtem Bityutskiy * We change whole page so no need to load it. But we 4511e51764aSArtem Bityutskiy * have to set the @PG_checked flag to make the further 4521e51764aSArtem Bityutskiy * code the page is new. This might be not true, but it 4531e51764aSArtem Bityutskiy * is better to budget more that to read the page from 4541e51764aSArtem Bityutskiy * the media. 4551e51764aSArtem Bityutskiy */ 4561e51764aSArtem Bityutskiy SetPageChecked(page); 457f55aa591SAdrian Hunter skipped_read = 1; 458f55aa591SAdrian Hunter } else { 4591e51764aSArtem Bityutskiy err = do_readpage(page); 4601e51764aSArtem Bityutskiy if (err) { 4611e51764aSArtem Bityutskiy unlock_page(page); 4621e51764aSArtem Bityutskiy page_cache_release(page); 4631e51764aSArtem Bityutskiy return err; 4641e51764aSArtem Bityutskiy } 4651e51764aSArtem Bityutskiy } 4661e51764aSArtem Bityutskiy 4671e51764aSArtem Bityutskiy SetPageUptodate(page); 4681e51764aSArtem Bityutskiy ClearPageError(page); 4691e51764aSArtem Bityutskiy } 4701e51764aSArtem Bityutskiy 4711e51764aSArtem Bityutskiy err = allocate_budget(c, page, ui, appending); 4721e51764aSArtem Bityutskiy if (unlikely(err)) { 4731e51764aSArtem Bityutskiy ubifs_assert(err == -ENOSPC); 4741e51764aSArtem Bityutskiy /* 475f55aa591SAdrian Hunter * If we skipped reading the page because we were going to 476f55aa591SAdrian Hunter * write all of it, then it is not up to date. 477f55aa591SAdrian Hunter */ 478f55aa591SAdrian Hunter if (skipped_read) { 479f55aa591SAdrian Hunter ClearPageChecked(page); 480f55aa591SAdrian Hunter ClearPageUptodate(page); 481f55aa591SAdrian Hunter } 482f55aa591SAdrian Hunter /* 4831e51764aSArtem Bityutskiy * Budgeting failed which means it would have to force 4841e51764aSArtem Bityutskiy * write-back but didn't, because we set the @fast flag in the 4851e51764aSArtem Bityutskiy * request. Write-back cannot be done now, while we have the 4861e51764aSArtem Bityutskiy * page locked, because it would deadlock. Unlock and free 4871e51764aSArtem Bityutskiy * everything and fall-back to slow-path. 4881e51764aSArtem Bityutskiy */ 4891e51764aSArtem Bityutskiy if (appending) { 4901e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&ui->ui_mutex)); 4911e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 4921e51764aSArtem Bityutskiy } 4931e51764aSArtem Bityutskiy unlock_page(page); 4941e51764aSArtem Bityutskiy page_cache_release(page); 4951e51764aSArtem Bityutskiy 49654566b2cSNick Piggin return write_begin_slow(mapping, pos, len, pagep, flags); 4971e51764aSArtem Bityutskiy } 4981e51764aSArtem Bityutskiy 4991e51764aSArtem Bityutskiy /* 5001e51764aSArtem Bityutskiy * Whee, we aquired budgeting quickly - without involving 5011e51764aSArtem Bityutskiy * garbage-collection, committing or forceing write-back. We return 5021e51764aSArtem Bityutskiy * with @ui->ui_mutex locked if we are appending pages, and unlocked 5031e51764aSArtem Bityutskiy * otherwise. This is an optimization (slightly hacky though). 5041e51764aSArtem Bityutskiy */ 5051e51764aSArtem Bityutskiy *pagep = page; 5061e51764aSArtem Bityutskiy return 0; 5071e51764aSArtem Bityutskiy 5081e51764aSArtem Bityutskiy } 5091e51764aSArtem Bityutskiy 5101e51764aSArtem Bityutskiy /** 5111e51764aSArtem Bityutskiy * cancel_budget - cancel budget. 5121e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 5131e51764aSArtem Bityutskiy * @page: page to cancel budget for 5141e51764aSArtem Bityutskiy * @ui: UBIFS inode object the page belongs to 5151e51764aSArtem Bityutskiy * @appending: non-zero if the page is appended 5161e51764aSArtem Bityutskiy * 5171e51764aSArtem Bityutskiy * This is a helper function for a page write operation. It unlocks the 5181e51764aSArtem Bityutskiy * @ui->ui_mutex in case of appending. 5191e51764aSArtem Bityutskiy */ 5201e51764aSArtem Bityutskiy static void cancel_budget(struct ubifs_info *c, struct page *page, 5211e51764aSArtem Bityutskiy struct ubifs_inode *ui, int appending) 5221e51764aSArtem Bityutskiy { 5231e51764aSArtem Bityutskiy if (appending) { 5241e51764aSArtem Bityutskiy if (!ui->dirty) 5251e51764aSArtem Bityutskiy ubifs_release_dirty_inode_budget(c, ui); 5261e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 5271e51764aSArtem Bityutskiy } 5281e51764aSArtem Bityutskiy if (!PagePrivate(page)) { 5291e51764aSArtem Bityutskiy if (PageChecked(page)) 5301e51764aSArtem Bityutskiy release_new_page_budget(c); 5311e51764aSArtem Bityutskiy else 5321e51764aSArtem Bityutskiy release_existing_page_budget(c); 5331e51764aSArtem Bityutskiy } 5341e51764aSArtem Bityutskiy } 5351e51764aSArtem Bityutskiy 5361e51764aSArtem Bityutskiy static int ubifs_write_end(struct file *file, struct address_space *mapping, 5371e51764aSArtem Bityutskiy loff_t pos, unsigned len, unsigned copied, 5381e51764aSArtem Bityutskiy struct page *page, void *fsdata) 5391e51764aSArtem Bityutskiy { 5401e51764aSArtem Bityutskiy struct inode *inode = mapping->host; 5411e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 5421e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 5431e51764aSArtem Bityutskiy loff_t end_pos = pos + len; 5441e51764aSArtem Bityutskiy int appending = !!(end_pos > inode->i_size); 5451e51764aSArtem Bityutskiy 5461e51764aSArtem Bityutskiy dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld", 5471e51764aSArtem Bityutskiy inode->i_ino, pos, page->index, len, copied, inode->i_size); 5481e51764aSArtem Bityutskiy 5491e51764aSArtem Bityutskiy if (unlikely(copied < len && len == PAGE_CACHE_SIZE)) { 5501e51764aSArtem Bityutskiy /* 5511e51764aSArtem Bityutskiy * VFS copied less data to the page that it intended and 5521e51764aSArtem Bityutskiy * declared in its '->write_begin()' call via the @len 5531e51764aSArtem Bityutskiy * argument. If the page was not up-to-date, and @len was 5541e51764aSArtem Bityutskiy * @PAGE_CACHE_SIZE, the 'ubifs_write_begin()' function did 5551e51764aSArtem Bityutskiy * not load it from the media (for optimization reasons). This 5561e51764aSArtem Bityutskiy * means that part of the page contains garbage. So read the 5571e51764aSArtem Bityutskiy * page now. 5581e51764aSArtem Bityutskiy */ 5591e51764aSArtem Bityutskiy dbg_gen("copied %d instead of %d, read page and repeat", 5601e51764aSArtem Bityutskiy copied, len); 5611e51764aSArtem Bityutskiy cancel_budget(c, page, ui, appending); 5621e51764aSArtem Bityutskiy 5631e51764aSArtem Bityutskiy /* 5641e51764aSArtem Bityutskiy * Return 0 to force VFS to repeat the whole operation, or the 5651e51764aSArtem Bityutskiy * error code if 'do_readpage()' failes. 5661e51764aSArtem Bityutskiy */ 5671e51764aSArtem Bityutskiy copied = do_readpage(page); 5681e51764aSArtem Bityutskiy goto out; 5691e51764aSArtem Bityutskiy } 5701e51764aSArtem Bityutskiy 5711e51764aSArtem Bityutskiy if (!PagePrivate(page)) { 5721e51764aSArtem Bityutskiy SetPagePrivate(page); 5731e51764aSArtem Bityutskiy atomic_long_inc(&c->dirty_pg_cnt); 5741e51764aSArtem Bityutskiy __set_page_dirty_nobuffers(page); 5751e51764aSArtem Bityutskiy } 5761e51764aSArtem Bityutskiy 5771e51764aSArtem Bityutskiy if (appending) { 5781e51764aSArtem Bityutskiy i_size_write(inode, end_pos); 5791e51764aSArtem Bityutskiy ui->ui_size = end_pos; 5801e51764aSArtem Bityutskiy /* 5811e51764aSArtem Bityutskiy * Note, we do not set @I_DIRTY_PAGES (which means that the 5821e51764aSArtem Bityutskiy * inode has dirty pages), this has been done in 5831e51764aSArtem Bityutskiy * '__set_page_dirty_nobuffers()'. 5841e51764aSArtem Bityutskiy */ 5851e51764aSArtem Bityutskiy __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 5861e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&ui->ui_mutex)); 5871e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 5881e51764aSArtem Bityutskiy } 5891e51764aSArtem Bityutskiy 5901e51764aSArtem Bityutskiy out: 5911e51764aSArtem Bityutskiy unlock_page(page); 5921e51764aSArtem Bityutskiy page_cache_release(page); 5931e51764aSArtem Bityutskiy return copied; 5941e51764aSArtem Bityutskiy } 5951e51764aSArtem Bityutskiy 5964793e7c5SAdrian Hunter /** 5974793e7c5SAdrian Hunter * populate_page - copy data nodes into a page for bulk-read. 5984793e7c5SAdrian Hunter * @c: UBIFS file-system description object 5994793e7c5SAdrian Hunter * @page: page 6004793e7c5SAdrian Hunter * @bu: bulk-read information 6014793e7c5SAdrian Hunter * @n: next zbranch slot 6024793e7c5SAdrian Hunter * 6034793e7c5SAdrian Hunter * This function returns %0 on success and a negative error code on failure. 6044793e7c5SAdrian Hunter */ 6054793e7c5SAdrian Hunter static int populate_page(struct ubifs_info *c, struct page *page, 6064793e7c5SAdrian Hunter struct bu_info *bu, int *n) 6074793e7c5SAdrian Hunter { 6085c0013c1SAdrian Hunter int i = 0, nn = *n, offs = bu->zbranch[0].offs, hole = 0, read = 0; 6094793e7c5SAdrian Hunter struct inode *inode = page->mapping->host; 6104793e7c5SAdrian Hunter loff_t i_size = i_size_read(inode); 6114793e7c5SAdrian Hunter unsigned int page_block; 6124793e7c5SAdrian Hunter void *addr, *zaddr; 6134793e7c5SAdrian Hunter pgoff_t end_index; 6144793e7c5SAdrian Hunter 6154793e7c5SAdrian Hunter dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx", 6164793e7c5SAdrian Hunter inode->i_ino, page->index, i_size, page->flags); 6174793e7c5SAdrian Hunter 6184793e7c5SAdrian Hunter addr = zaddr = kmap(page); 6194793e7c5SAdrian Hunter 620ed382d58SAdrian Hunter end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 6214793e7c5SAdrian Hunter if (!i_size || page->index > end_index) { 6225c0013c1SAdrian Hunter hole = 1; 6234793e7c5SAdrian Hunter memset(addr, 0, PAGE_CACHE_SIZE); 6244793e7c5SAdrian Hunter goto out_hole; 6254793e7c5SAdrian Hunter } 6264793e7c5SAdrian Hunter 6274793e7c5SAdrian Hunter page_block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; 6284793e7c5SAdrian Hunter while (1) { 6294793e7c5SAdrian Hunter int err, len, out_len, dlen; 6304793e7c5SAdrian Hunter 6315c0013c1SAdrian Hunter if (nn >= bu->cnt) { 6325c0013c1SAdrian Hunter hole = 1; 6334793e7c5SAdrian Hunter memset(addr, 0, UBIFS_BLOCK_SIZE); 6345c0013c1SAdrian Hunter } else if (key_block(c, &bu->zbranch[nn].key) == page_block) { 6354793e7c5SAdrian Hunter struct ubifs_data_node *dn; 6364793e7c5SAdrian Hunter 6374793e7c5SAdrian Hunter dn = bu->buf + (bu->zbranch[nn].offs - offs); 6384793e7c5SAdrian Hunter 6390ecb9529SHarvey Harrison ubifs_assert(le64_to_cpu(dn->ch.sqnum) > 6404793e7c5SAdrian Hunter ubifs_inode(inode)->creat_sqnum); 6414793e7c5SAdrian Hunter 6424793e7c5SAdrian Hunter len = le32_to_cpu(dn->size); 6434793e7c5SAdrian Hunter if (len <= 0 || len > UBIFS_BLOCK_SIZE) 6444793e7c5SAdrian Hunter goto out_err; 6454793e7c5SAdrian Hunter 6464793e7c5SAdrian Hunter dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; 6474793e7c5SAdrian Hunter out_len = UBIFS_BLOCK_SIZE; 6484793e7c5SAdrian Hunter err = ubifs_decompress(&dn->data, dlen, addr, &out_len, 6494793e7c5SAdrian Hunter le16_to_cpu(dn->compr_type)); 6504793e7c5SAdrian Hunter if (err || len != out_len) 6514793e7c5SAdrian Hunter goto out_err; 6524793e7c5SAdrian Hunter 6534793e7c5SAdrian Hunter if (len < UBIFS_BLOCK_SIZE) 6544793e7c5SAdrian Hunter memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); 6554793e7c5SAdrian Hunter 6564793e7c5SAdrian Hunter nn += 1; 6574793e7c5SAdrian Hunter read = (i << UBIFS_BLOCK_SHIFT) + len; 6585c0013c1SAdrian Hunter } else if (key_block(c, &bu->zbranch[nn].key) < page_block) { 6595c0013c1SAdrian Hunter nn += 1; 6605c0013c1SAdrian Hunter continue; 6615c0013c1SAdrian Hunter } else { 6625c0013c1SAdrian Hunter hole = 1; 6635c0013c1SAdrian Hunter memset(addr, 0, UBIFS_BLOCK_SIZE); 6644793e7c5SAdrian Hunter } 6654793e7c5SAdrian Hunter if (++i >= UBIFS_BLOCKS_PER_PAGE) 6664793e7c5SAdrian Hunter break; 6674793e7c5SAdrian Hunter addr += UBIFS_BLOCK_SIZE; 6684793e7c5SAdrian Hunter page_block += 1; 6694793e7c5SAdrian Hunter } 6704793e7c5SAdrian Hunter 6714793e7c5SAdrian Hunter if (end_index == page->index) { 6724793e7c5SAdrian Hunter int len = i_size & (PAGE_CACHE_SIZE - 1); 6734793e7c5SAdrian Hunter 674ed382d58SAdrian Hunter if (len && len < read) 6754793e7c5SAdrian Hunter memset(zaddr + len, 0, read - len); 6764793e7c5SAdrian Hunter } 6774793e7c5SAdrian Hunter 6784793e7c5SAdrian Hunter out_hole: 6794793e7c5SAdrian Hunter if (hole) { 6804793e7c5SAdrian Hunter SetPageChecked(page); 6814793e7c5SAdrian Hunter dbg_gen("hole"); 6824793e7c5SAdrian Hunter } 6834793e7c5SAdrian Hunter 6844793e7c5SAdrian Hunter SetPageUptodate(page); 6854793e7c5SAdrian Hunter ClearPageError(page); 6864793e7c5SAdrian Hunter flush_dcache_page(page); 6874793e7c5SAdrian Hunter kunmap(page); 6884793e7c5SAdrian Hunter *n = nn; 6894793e7c5SAdrian Hunter return 0; 6904793e7c5SAdrian Hunter 6914793e7c5SAdrian Hunter out_err: 6924793e7c5SAdrian Hunter ClearPageUptodate(page); 6934793e7c5SAdrian Hunter SetPageError(page); 6944793e7c5SAdrian Hunter flush_dcache_page(page); 6954793e7c5SAdrian Hunter kunmap(page); 6964793e7c5SAdrian Hunter ubifs_err("bad data node (block %u, inode %lu)", 6974793e7c5SAdrian Hunter page_block, inode->i_ino); 6984793e7c5SAdrian Hunter return -EINVAL; 6994793e7c5SAdrian Hunter } 7004793e7c5SAdrian Hunter 7014793e7c5SAdrian Hunter /** 7024793e7c5SAdrian Hunter * ubifs_do_bulk_read - do bulk-read. 7034793e7c5SAdrian Hunter * @c: UBIFS file-system description object 7046c0c42cdSArtem Bityutskiy * @bu: bulk-read information 7056c0c42cdSArtem Bityutskiy * @page1: first page to read 7064793e7c5SAdrian Hunter * 7074793e7c5SAdrian Hunter * This function returns %1 if the bulk-read is done, otherwise %0 is returned. 7084793e7c5SAdrian Hunter */ 7096c0c42cdSArtem Bityutskiy static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu, 7106c0c42cdSArtem Bityutskiy struct page *page1) 7114793e7c5SAdrian Hunter { 7124793e7c5SAdrian Hunter pgoff_t offset = page1->index, end_index; 7134793e7c5SAdrian Hunter struct address_space *mapping = page1->mapping; 7144793e7c5SAdrian Hunter struct inode *inode = mapping->host; 7154793e7c5SAdrian Hunter struct ubifs_inode *ui = ubifs_inode(inode); 7164793e7c5SAdrian Hunter int err, page_idx, page_cnt, ret = 0, n = 0; 7176c0c42cdSArtem Bityutskiy int allocate = bu->buf ? 0 : 1; 7184793e7c5SAdrian Hunter loff_t isize; 7194793e7c5SAdrian Hunter 7204793e7c5SAdrian Hunter err = ubifs_tnc_get_bu_keys(c, bu); 7214793e7c5SAdrian Hunter if (err) 7224793e7c5SAdrian Hunter goto out_warn; 7234793e7c5SAdrian Hunter 7244793e7c5SAdrian Hunter if (bu->eof) { 7254793e7c5SAdrian Hunter /* Turn off bulk-read at the end of the file */ 7264793e7c5SAdrian Hunter ui->read_in_a_row = 1; 7274793e7c5SAdrian Hunter ui->bulk_read = 0; 7284793e7c5SAdrian Hunter } 7294793e7c5SAdrian Hunter 7304793e7c5SAdrian Hunter page_cnt = bu->blk_cnt >> UBIFS_BLOCKS_PER_PAGE_SHIFT; 7314793e7c5SAdrian Hunter if (!page_cnt) { 7324793e7c5SAdrian Hunter /* 7334793e7c5SAdrian Hunter * This happens when there are multiple blocks per page and the 7344793e7c5SAdrian Hunter * blocks for the first page we are looking for, are not 7354793e7c5SAdrian Hunter * together. If all the pages were like this, bulk-read would 7364793e7c5SAdrian Hunter * reduce performance, so we turn it off for a while. 7374793e7c5SAdrian Hunter */ 7386c0c42cdSArtem Bityutskiy goto out_bu_off; 7394793e7c5SAdrian Hunter } 7404793e7c5SAdrian Hunter 7414793e7c5SAdrian Hunter if (bu->cnt) { 7426c0c42cdSArtem Bityutskiy if (allocate) { 7436c0c42cdSArtem Bityutskiy /* 7446c0c42cdSArtem Bityutskiy * Allocate bulk-read buffer depending on how many data 7456c0c42cdSArtem Bityutskiy * nodes we are going to read. 7466c0c42cdSArtem Bityutskiy */ 7476c0c42cdSArtem Bityutskiy bu->buf_len = bu->zbranch[bu->cnt - 1].offs + 7486c0c42cdSArtem Bityutskiy bu->zbranch[bu->cnt - 1].len - 7496c0c42cdSArtem Bityutskiy bu->zbranch[0].offs; 7506c0c42cdSArtem Bityutskiy ubifs_assert(bu->buf_len > 0); 7516c0c42cdSArtem Bityutskiy ubifs_assert(bu->buf_len <= c->leb_size); 7526c0c42cdSArtem Bityutskiy bu->buf = kmalloc(bu->buf_len, GFP_NOFS | __GFP_NOWARN); 7536c0c42cdSArtem Bityutskiy if (!bu->buf) 7546c0c42cdSArtem Bityutskiy goto out_bu_off; 7556c0c42cdSArtem Bityutskiy } 7566c0c42cdSArtem Bityutskiy 7574793e7c5SAdrian Hunter err = ubifs_tnc_bulk_read(c, bu); 7584793e7c5SAdrian Hunter if (err) 7594793e7c5SAdrian Hunter goto out_warn; 7604793e7c5SAdrian Hunter } 7614793e7c5SAdrian Hunter 7624793e7c5SAdrian Hunter err = populate_page(c, page1, bu, &n); 7634793e7c5SAdrian Hunter if (err) 7644793e7c5SAdrian Hunter goto out_warn; 7654793e7c5SAdrian Hunter 7664793e7c5SAdrian Hunter unlock_page(page1); 7674793e7c5SAdrian Hunter ret = 1; 7684793e7c5SAdrian Hunter 7694793e7c5SAdrian Hunter isize = i_size_read(inode); 7704793e7c5SAdrian Hunter if (isize == 0) 7714793e7c5SAdrian Hunter goto out_free; 7724793e7c5SAdrian Hunter end_index = ((isize - 1) >> PAGE_CACHE_SHIFT); 7734793e7c5SAdrian Hunter 7744793e7c5SAdrian Hunter for (page_idx = 1; page_idx < page_cnt; page_idx++) { 7754793e7c5SAdrian Hunter pgoff_t page_offset = offset + page_idx; 7764793e7c5SAdrian Hunter struct page *page; 7774793e7c5SAdrian Hunter 7784793e7c5SAdrian Hunter if (page_offset > end_index) 7794793e7c5SAdrian Hunter break; 7804793e7c5SAdrian Hunter page = find_or_create_page(mapping, page_offset, 7814793e7c5SAdrian Hunter GFP_NOFS | __GFP_COLD); 7824793e7c5SAdrian Hunter if (!page) 7834793e7c5SAdrian Hunter break; 7844793e7c5SAdrian Hunter if (!PageUptodate(page)) 7854793e7c5SAdrian Hunter err = populate_page(c, page, bu, &n); 7864793e7c5SAdrian Hunter unlock_page(page); 7874793e7c5SAdrian Hunter page_cache_release(page); 7884793e7c5SAdrian Hunter if (err) 7894793e7c5SAdrian Hunter break; 7904793e7c5SAdrian Hunter } 7914793e7c5SAdrian Hunter 7924793e7c5SAdrian Hunter ui->last_page_read = offset + page_idx - 1; 7934793e7c5SAdrian Hunter 7944793e7c5SAdrian Hunter out_free: 7956c0c42cdSArtem Bityutskiy if (allocate) 7964793e7c5SAdrian Hunter kfree(bu->buf); 7974793e7c5SAdrian Hunter return ret; 7984793e7c5SAdrian Hunter 7994793e7c5SAdrian Hunter out_warn: 8004793e7c5SAdrian Hunter ubifs_warn("ignoring error %d and skipping bulk-read", err); 8014793e7c5SAdrian Hunter goto out_free; 8026c0c42cdSArtem Bityutskiy 8036c0c42cdSArtem Bityutskiy out_bu_off: 8046c0c42cdSArtem Bityutskiy ui->read_in_a_row = ui->bulk_read = 0; 8056c0c42cdSArtem Bityutskiy goto out_free; 8064793e7c5SAdrian Hunter } 8074793e7c5SAdrian Hunter 8084793e7c5SAdrian Hunter /** 8094793e7c5SAdrian Hunter * ubifs_bulk_read - determine whether to bulk-read and, if so, do it. 8104793e7c5SAdrian Hunter * @page: page from which to start bulk-read. 8114793e7c5SAdrian Hunter * 8124793e7c5SAdrian Hunter * Some flash media are capable of reading sequentially at faster rates. UBIFS 8134793e7c5SAdrian Hunter * bulk-read facility is designed to take advantage of that, by reading in one 8144793e7c5SAdrian Hunter * go consecutive data nodes that are also located consecutively in the same 8154793e7c5SAdrian Hunter * LEB. This function returns %1 if a bulk-read is done and %0 otherwise. 8164793e7c5SAdrian Hunter */ 8174793e7c5SAdrian Hunter static int ubifs_bulk_read(struct page *page) 8184793e7c5SAdrian Hunter { 8194793e7c5SAdrian Hunter struct inode *inode = page->mapping->host; 8204793e7c5SAdrian Hunter struct ubifs_info *c = inode->i_sb->s_fs_info; 8214793e7c5SAdrian Hunter struct ubifs_inode *ui = ubifs_inode(inode); 8224793e7c5SAdrian Hunter pgoff_t index = page->index, last_page_read = ui->last_page_read; 8236c0c42cdSArtem Bityutskiy struct bu_info *bu; 8243477d204SArtem Bityutskiy int err = 0, allocated = 0; 8254793e7c5SAdrian Hunter 8264793e7c5SAdrian Hunter ui->last_page_read = index; 8274793e7c5SAdrian Hunter if (!c->bulk_read) 8284793e7c5SAdrian Hunter return 0; 8296c0c42cdSArtem Bityutskiy 8304793e7c5SAdrian Hunter /* 8313477d204SArtem Bityutskiy * Bulk-read is protected by @ui->ui_mutex, but it is an optimization, 8323477d204SArtem Bityutskiy * so don't bother if we cannot lock the mutex. 8334793e7c5SAdrian Hunter */ 8344793e7c5SAdrian Hunter if (!mutex_trylock(&ui->ui_mutex)) 8354793e7c5SAdrian Hunter return 0; 8366c0c42cdSArtem Bityutskiy 8374793e7c5SAdrian Hunter if (index != last_page_read + 1) { 8384793e7c5SAdrian Hunter /* Turn off bulk-read if we stop reading sequentially */ 8394793e7c5SAdrian Hunter ui->read_in_a_row = 1; 8404793e7c5SAdrian Hunter if (ui->bulk_read) 8414793e7c5SAdrian Hunter ui->bulk_read = 0; 8424793e7c5SAdrian Hunter goto out_unlock; 8434793e7c5SAdrian Hunter } 8446c0c42cdSArtem Bityutskiy 8454793e7c5SAdrian Hunter if (!ui->bulk_read) { 8464793e7c5SAdrian Hunter ui->read_in_a_row += 1; 8474793e7c5SAdrian Hunter if (ui->read_in_a_row < 3) 8484793e7c5SAdrian Hunter goto out_unlock; 8494793e7c5SAdrian Hunter /* Three reads in a row, so switch on bulk-read */ 8504793e7c5SAdrian Hunter ui->bulk_read = 1; 8514793e7c5SAdrian Hunter } 8526c0c42cdSArtem Bityutskiy 8533477d204SArtem Bityutskiy /* 8543477d204SArtem Bityutskiy * If possible, try to use pre-allocated bulk-read information, which 8553477d204SArtem Bityutskiy * is protected by @c->bu_mutex. 8563477d204SArtem Bityutskiy */ 8573477d204SArtem Bityutskiy if (mutex_trylock(&c->bu_mutex)) 8583477d204SArtem Bityutskiy bu = &c->bu; 8593477d204SArtem Bityutskiy else { 8606c0c42cdSArtem Bityutskiy bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN); 8616c0c42cdSArtem Bityutskiy if (!bu) 8623477d204SArtem Bityutskiy goto out_unlock; 8636c0c42cdSArtem Bityutskiy 8646c0c42cdSArtem Bityutskiy bu->buf = NULL; 8653477d204SArtem Bityutskiy allocated = 1; 8663477d204SArtem Bityutskiy } 8673477d204SArtem Bityutskiy 8686c0c42cdSArtem Bityutskiy bu->buf_len = c->max_bu_buf_len; 8696c0c42cdSArtem Bityutskiy data_key_init(c, &bu->key, inode->i_ino, 8706c0c42cdSArtem Bityutskiy page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT); 8716c0c42cdSArtem Bityutskiy err = ubifs_do_bulk_read(c, bu, page); 8723477d204SArtem Bityutskiy 8733477d204SArtem Bityutskiy if (!allocated) 8743477d204SArtem Bityutskiy mutex_unlock(&c->bu_mutex); 8753477d204SArtem Bityutskiy else 8766c0c42cdSArtem Bityutskiy kfree(bu); 8776c0c42cdSArtem Bityutskiy 8784793e7c5SAdrian Hunter out_unlock: 8794793e7c5SAdrian Hunter mutex_unlock(&ui->ui_mutex); 8806c0c42cdSArtem Bityutskiy return err; 8814793e7c5SAdrian Hunter } 8824793e7c5SAdrian Hunter 8831e51764aSArtem Bityutskiy static int ubifs_readpage(struct file *file, struct page *page) 8841e51764aSArtem Bityutskiy { 8854793e7c5SAdrian Hunter if (ubifs_bulk_read(page)) 8864793e7c5SAdrian Hunter return 0; 8871e51764aSArtem Bityutskiy do_readpage(page); 8881e51764aSArtem Bityutskiy unlock_page(page); 8891e51764aSArtem Bityutskiy return 0; 8901e51764aSArtem Bityutskiy } 8911e51764aSArtem Bityutskiy 8921e51764aSArtem Bityutskiy static int do_writepage(struct page *page, int len) 8931e51764aSArtem Bityutskiy { 8941e51764aSArtem Bityutskiy int err = 0, i, blen; 8951e51764aSArtem Bityutskiy unsigned int block; 8961e51764aSArtem Bityutskiy void *addr; 8971e51764aSArtem Bityutskiy union ubifs_key key; 8981e51764aSArtem Bityutskiy struct inode *inode = page->mapping->host; 8991e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 9001e51764aSArtem Bityutskiy 9011e51764aSArtem Bityutskiy #ifdef UBIFS_DEBUG 9021e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 9031e51764aSArtem Bityutskiy ubifs_assert(page->index <= ui->synced_i_size << PAGE_CACHE_SIZE); 9041e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 9051e51764aSArtem Bityutskiy #endif 9061e51764aSArtem Bityutskiy 9071e51764aSArtem Bityutskiy /* Update radix tree tags */ 9081e51764aSArtem Bityutskiy set_page_writeback(page); 9091e51764aSArtem Bityutskiy 9101e51764aSArtem Bityutskiy addr = kmap(page); 9111e51764aSArtem Bityutskiy block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; 9121e51764aSArtem Bityutskiy i = 0; 9131e51764aSArtem Bityutskiy while (len) { 9141e51764aSArtem Bityutskiy blen = min_t(int, len, UBIFS_BLOCK_SIZE); 9151e51764aSArtem Bityutskiy data_key_init(c, &key, inode->i_ino, block); 9161e51764aSArtem Bityutskiy err = ubifs_jnl_write_data(c, inode, &key, addr, blen); 9171e51764aSArtem Bityutskiy if (err) 9181e51764aSArtem Bityutskiy break; 9191e51764aSArtem Bityutskiy if (++i >= UBIFS_BLOCKS_PER_PAGE) 9201e51764aSArtem Bityutskiy break; 9211e51764aSArtem Bityutskiy block += 1; 9221e51764aSArtem Bityutskiy addr += blen; 9231e51764aSArtem Bityutskiy len -= blen; 9241e51764aSArtem Bityutskiy } 9251e51764aSArtem Bityutskiy if (err) { 9261e51764aSArtem Bityutskiy SetPageError(page); 9271e51764aSArtem Bityutskiy ubifs_err("cannot write page %lu of inode %lu, error %d", 9281e51764aSArtem Bityutskiy page->index, inode->i_ino, err); 9291e51764aSArtem Bityutskiy ubifs_ro_mode(c, err); 9301e51764aSArtem Bityutskiy } 9311e51764aSArtem Bityutskiy 9321e51764aSArtem Bityutskiy ubifs_assert(PagePrivate(page)); 9331e51764aSArtem Bityutskiy if (PageChecked(page)) 9341e51764aSArtem Bityutskiy release_new_page_budget(c); 9351e51764aSArtem Bityutskiy else 9361e51764aSArtem Bityutskiy release_existing_page_budget(c); 9371e51764aSArtem Bityutskiy 9381e51764aSArtem Bityutskiy atomic_long_dec(&c->dirty_pg_cnt); 9391e51764aSArtem Bityutskiy ClearPagePrivate(page); 9401e51764aSArtem Bityutskiy ClearPageChecked(page); 9411e51764aSArtem Bityutskiy 9421e51764aSArtem Bityutskiy kunmap(page); 9431e51764aSArtem Bityutskiy unlock_page(page); 9441e51764aSArtem Bityutskiy end_page_writeback(page); 9451e51764aSArtem Bityutskiy return err; 9461e51764aSArtem Bityutskiy } 9471e51764aSArtem Bityutskiy 9481e51764aSArtem Bityutskiy /* 9491e51764aSArtem Bityutskiy * When writing-back dirty inodes, VFS first writes-back pages belonging to the 9501e51764aSArtem Bityutskiy * inode, then the inode itself. For UBIFS this may cause a problem. Consider a 9511e51764aSArtem Bityutskiy * situation when a we have an inode with size 0, then a megabyte of data is 9521e51764aSArtem Bityutskiy * appended to the inode, then write-back starts and flushes some amount of the 9531e51764aSArtem Bityutskiy * dirty pages, the journal becomes full, commit happens and finishes, and then 9541e51764aSArtem Bityutskiy * an unclean reboot happens. When the file system is mounted next time, the 9551e51764aSArtem Bityutskiy * inode size would still be 0, but there would be many pages which are beyond 9561e51764aSArtem Bityutskiy * the inode size, they would be indexed and consume flash space. Because the 9571e51764aSArtem Bityutskiy * journal has been committed, the replay would not be able to detect this 9581e51764aSArtem Bityutskiy * situation and correct the inode size. This means UBIFS would have to scan 9591e51764aSArtem Bityutskiy * whole index and correct all inode sizes, which is long an unacceptable. 9601e51764aSArtem Bityutskiy * 9611e51764aSArtem Bityutskiy * To prevent situations like this, UBIFS writes pages back only if they are 9627d4e9ccbSArtem Bityutskiy * within the last synchronized inode size, i.e. the size which has been 9631e51764aSArtem Bityutskiy * written to the flash media last time. Otherwise, UBIFS forces inode 9641e51764aSArtem Bityutskiy * write-back, thus making sure the on-flash inode contains current inode size, 9651e51764aSArtem Bityutskiy * and then keeps writing pages back. 9661e51764aSArtem Bityutskiy * 9671e51764aSArtem Bityutskiy * Some locking issues explanation. 'ubifs_writepage()' first is called with 9681e51764aSArtem Bityutskiy * the page locked, and it locks @ui_mutex. However, write-back does take inode 9691e51764aSArtem Bityutskiy * @i_mutex, which means other VFS operations may be run on this inode at the 9701e51764aSArtem Bityutskiy * same time. And the problematic one is truncation to smaller size, from where 9711e51764aSArtem Bityutskiy * we have to call 'vmtruncate()', which first changes @inode->i_size, then 9721e51764aSArtem Bityutskiy * drops the truncated pages. And while dropping the pages, it takes the page 9731e51764aSArtem Bityutskiy * lock. This means that 'do_truncation()' cannot call 'vmtruncate()' with 9741e51764aSArtem Bityutskiy * @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'. This 9751e51764aSArtem Bityutskiy * means that @inode->i_size is changed while @ui_mutex is unlocked. 9761e51764aSArtem Bityutskiy * 9771e51764aSArtem Bityutskiy * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond 9781e51764aSArtem Bityutskiy * inode size. How do we do this if @inode->i_size may became smaller while we 9791e51764aSArtem Bityutskiy * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the 9801e51764aSArtem Bityutskiy * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size 9811e51764aSArtem Bityutskiy * internally and updates it under @ui_mutex. 9821e51764aSArtem Bityutskiy * 9831e51764aSArtem Bityutskiy * Q: why we do not worry that if we race with truncation, we may end up with a 9841e51764aSArtem Bityutskiy * situation when the inode is truncated while we are in the middle of 9851e51764aSArtem Bityutskiy * 'do_writepage()', so we do write beyond inode size? 9861e51764aSArtem Bityutskiy * A: If we are in the middle of 'do_writepage()', truncation would be locked 9871e51764aSArtem Bityutskiy * on the page lock and it would not write the truncated inode node to the 9881e51764aSArtem Bityutskiy * journal before we have finished. 9891e51764aSArtem Bityutskiy */ 9901e51764aSArtem Bityutskiy static int ubifs_writepage(struct page *page, struct writeback_control *wbc) 9911e51764aSArtem Bityutskiy { 9921e51764aSArtem Bityutskiy struct inode *inode = page->mapping->host; 9931e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 9941e51764aSArtem Bityutskiy loff_t i_size = i_size_read(inode), synced_i_size; 9951e51764aSArtem Bityutskiy pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; 9961e51764aSArtem Bityutskiy int err, len = i_size & (PAGE_CACHE_SIZE - 1); 9971e51764aSArtem Bityutskiy void *kaddr; 9981e51764aSArtem Bityutskiy 9991e51764aSArtem Bityutskiy dbg_gen("ino %lu, pg %lu, pg flags %#lx", 10001e51764aSArtem Bityutskiy inode->i_ino, page->index, page->flags); 10011e51764aSArtem Bityutskiy ubifs_assert(PagePrivate(page)); 10021e51764aSArtem Bityutskiy 10031e51764aSArtem Bityutskiy /* Is the page fully outside @i_size? (truncate in progress) */ 10041e51764aSArtem Bityutskiy if (page->index > end_index || (page->index == end_index && !len)) { 10051e51764aSArtem Bityutskiy err = 0; 10061e51764aSArtem Bityutskiy goto out_unlock; 10071e51764aSArtem Bityutskiy } 10081e51764aSArtem Bityutskiy 10091e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 10101e51764aSArtem Bityutskiy synced_i_size = ui->synced_i_size; 10111e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 10121e51764aSArtem Bityutskiy 10131e51764aSArtem Bityutskiy /* Is the page fully inside @i_size? */ 10141e51764aSArtem Bityutskiy if (page->index < end_index) { 10151e51764aSArtem Bityutskiy if (page->index >= synced_i_size >> PAGE_CACHE_SHIFT) { 10161e51764aSArtem Bityutskiy err = inode->i_sb->s_op->write_inode(inode, 1); 10171e51764aSArtem Bityutskiy if (err) 10181e51764aSArtem Bityutskiy goto out_unlock; 10191e51764aSArtem Bityutskiy /* 10201e51764aSArtem Bityutskiy * The inode has been written, but the write-buffer has 10211e51764aSArtem Bityutskiy * not been synchronized, so in case of an unclean 10221e51764aSArtem Bityutskiy * reboot we may end up with some pages beyond inode 10231e51764aSArtem Bityutskiy * size, but they would be in the journal (because 10241e51764aSArtem Bityutskiy * commit flushes write buffers) and recovery would deal 10251e51764aSArtem Bityutskiy * with this. 10261e51764aSArtem Bityutskiy */ 10271e51764aSArtem Bityutskiy } 10281e51764aSArtem Bityutskiy return do_writepage(page, PAGE_CACHE_SIZE); 10291e51764aSArtem Bityutskiy } 10301e51764aSArtem Bityutskiy 10311e51764aSArtem Bityutskiy /* 10321e51764aSArtem Bityutskiy * The page straddles @i_size. It must be zeroed out on each and every 10331e51764aSArtem Bityutskiy * writepage invocation because it may be mmapped. "A file is mapped 10341e51764aSArtem Bityutskiy * in multiples of the page size. For a file that is not a multiple of 10351e51764aSArtem Bityutskiy * the page size, the remaining memory is zeroed when mapped, and 10361e51764aSArtem Bityutskiy * writes to that region are not written out to the file." 10371e51764aSArtem Bityutskiy */ 10381e51764aSArtem Bityutskiy kaddr = kmap_atomic(page, KM_USER0); 10391e51764aSArtem Bityutskiy memset(kaddr + len, 0, PAGE_CACHE_SIZE - len); 10401e51764aSArtem Bityutskiy flush_dcache_page(page); 10411e51764aSArtem Bityutskiy kunmap_atomic(kaddr, KM_USER0); 10421e51764aSArtem Bityutskiy 10431e51764aSArtem Bityutskiy if (i_size > synced_i_size) { 10441e51764aSArtem Bityutskiy err = inode->i_sb->s_op->write_inode(inode, 1); 10451e51764aSArtem Bityutskiy if (err) 10461e51764aSArtem Bityutskiy goto out_unlock; 10471e51764aSArtem Bityutskiy } 10481e51764aSArtem Bityutskiy 10491e51764aSArtem Bityutskiy return do_writepage(page, len); 10501e51764aSArtem Bityutskiy 10511e51764aSArtem Bityutskiy out_unlock: 10521e51764aSArtem Bityutskiy unlock_page(page); 10531e51764aSArtem Bityutskiy return err; 10541e51764aSArtem Bityutskiy } 10551e51764aSArtem Bityutskiy 10561e51764aSArtem Bityutskiy /** 10571e51764aSArtem Bityutskiy * do_attr_changes - change inode attributes. 10581e51764aSArtem Bityutskiy * @inode: inode to change attributes for 10591e51764aSArtem Bityutskiy * @attr: describes attributes to change 10601e51764aSArtem Bityutskiy */ 10611e51764aSArtem Bityutskiy static void do_attr_changes(struct inode *inode, const struct iattr *attr) 10621e51764aSArtem Bityutskiy { 10631e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_UID) 10641e51764aSArtem Bityutskiy inode->i_uid = attr->ia_uid; 10651e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_GID) 10661e51764aSArtem Bityutskiy inode->i_gid = attr->ia_gid; 10671e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_ATIME) 10681e51764aSArtem Bityutskiy inode->i_atime = timespec_trunc(attr->ia_atime, 10691e51764aSArtem Bityutskiy inode->i_sb->s_time_gran); 10701e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_MTIME) 10711e51764aSArtem Bityutskiy inode->i_mtime = timespec_trunc(attr->ia_mtime, 10721e51764aSArtem Bityutskiy inode->i_sb->s_time_gran); 10731e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_CTIME) 10741e51764aSArtem Bityutskiy inode->i_ctime = timespec_trunc(attr->ia_ctime, 10751e51764aSArtem Bityutskiy inode->i_sb->s_time_gran); 10761e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_MODE) { 10771e51764aSArtem Bityutskiy umode_t mode = attr->ia_mode; 10781e51764aSArtem Bityutskiy 10791e51764aSArtem Bityutskiy if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) 10801e51764aSArtem Bityutskiy mode &= ~S_ISGID; 10811e51764aSArtem Bityutskiy inode->i_mode = mode; 10821e51764aSArtem Bityutskiy } 10831e51764aSArtem Bityutskiy } 10841e51764aSArtem Bityutskiy 10851e51764aSArtem Bityutskiy /** 10861e51764aSArtem Bityutskiy * do_truncation - truncate an inode. 10871e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 10881e51764aSArtem Bityutskiy * @inode: inode to truncate 10891e51764aSArtem Bityutskiy * @attr: inode attribute changes description 10901e51764aSArtem Bityutskiy * 10911e51764aSArtem Bityutskiy * This function implements VFS '->setattr()' call when the inode is truncated 10921e51764aSArtem Bityutskiy * to a smaller size. Returns zero in case of success and a negative error code 10931e51764aSArtem Bityutskiy * in case of failure. 10941e51764aSArtem Bityutskiy */ 10951e51764aSArtem Bityutskiy static int do_truncation(struct ubifs_info *c, struct inode *inode, 10961e51764aSArtem Bityutskiy const struct iattr *attr) 10971e51764aSArtem Bityutskiy { 10981e51764aSArtem Bityutskiy int err; 10991e51764aSArtem Bityutskiy struct ubifs_budget_req req; 11001e51764aSArtem Bityutskiy loff_t old_size = inode->i_size, new_size = attr->ia_size; 110104da11bfSArtem Bityutskiy int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1; 11021e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 11031e51764aSArtem Bityutskiy 11041e51764aSArtem Bityutskiy dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size); 11051e51764aSArtem Bityutskiy memset(&req, 0, sizeof(struct ubifs_budget_req)); 11061e51764aSArtem Bityutskiy 11071e51764aSArtem Bityutskiy /* 11081e51764aSArtem Bityutskiy * If this is truncation to a smaller size, and we do not truncate on a 11091e51764aSArtem Bityutskiy * block boundary, budget for changing one data block, because the last 11101e51764aSArtem Bityutskiy * block will be re-written. 11111e51764aSArtem Bityutskiy */ 11121e51764aSArtem Bityutskiy if (new_size & (UBIFS_BLOCK_SIZE - 1)) 11131e51764aSArtem Bityutskiy req.dirtied_page = 1; 11141e51764aSArtem Bityutskiy 11151e51764aSArtem Bityutskiy req.dirtied_ino = 1; 11161e51764aSArtem Bityutskiy /* A funny way to budget for truncation node */ 11171e51764aSArtem Bityutskiy req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ; 11181e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 111904da11bfSArtem Bityutskiy if (err) { 112004da11bfSArtem Bityutskiy /* 112104da11bfSArtem Bityutskiy * Treat truncations to zero as deletion and always allow them, 112204da11bfSArtem Bityutskiy * just like we do for '->unlink()'. 112304da11bfSArtem Bityutskiy */ 112404da11bfSArtem Bityutskiy if (new_size || err != -ENOSPC) 11251e51764aSArtem Bityutskiy return err; 112604da11bfSArtem Bityutskiy budgeted = 0; 112704da11bfSArtem Bityutskiy } 11281e51764aSArtem Bityutskiy 11291e51764aSArtem Bityutskiy err = vmtruncate(inode, new_size); 11301e51764aSArtem Bityutskiy if (err) 11311e51764aSArtem Bityutskiy goto out_budg; 11321e51764aSArtem Bityutskiy 11331e51764aSArtem Bityutskiy if (offset) { 11341e51764aSArtem Bityutskiy pgoff_t index = new_size >> PAGE_CACHE_SHIFT; 11351e51764aSArtem Bityutskiy struct page *page; 11361e51764aSArtem Bityutskiy 11371e51764aSArtem Bityutskiy page = find_lock_page(inode->i_mapping, index); 11381e51764aSArtem Bityutskiy if (page) { 11391e51764aSArtem Bityutskiy if (PageDirty(page)) { 11401e51764aSArtem Bityutskiy /* 11411e51764aSArtem Bityutskiy * 'ubifs_jnl_truncate()' will try to truncate 11421e51764aSArtem Bityutskiy * the last data node, but it contains 11431e51764aSArtem Bityutskiy * out-of-date data because the page is dirty. 11441e51764aSArtem Bityutskiy * Write the page now, so that 11451e51764aSArtem Bityutskiy * 'ubifs_jnl_truncate()' will see an already 11461e51764aSArtem Bityutskiy * truncated (and up to date) data node. 11471e51764aSArtem Bityutskiy */ 11481e51764aSArtem Bityutskiy ubifs_assert(PagePrivate(page)); 11491e51764aSArtem Bityutskiy 11501e51764aSArtem Bityutskiy clear_page_dirty_for_io(page); 11511e51764aSArtem Bityutskiy if (UBIFS_BLOCKS_PER_PAGE_SHIFT) 11521e51764aSArtem Bityutskiy offset = new_size & 11531e51764aSArtem Bityutskiy (PAGE_CACHE_SIZE - 1); 11541e51764aSArtem Bityutskiy err = do_writepage(page, offset); 11551e51764aSArtem Bityutskiy page_cache_release(page); 11561e51764aSArtem Bityutskiy if (err) 11571e51764aSArtem Bityutskiy goto out_budg; 11581e51764aSArtem Bityutskiy /* 11591e51764aSArtem Bityutskiy * We could now tell 'ubifs_jnl_truncate()' not 11601e51764aSArtem Bityutskiy * to read the last block. 11611e51764aSArtem Bityutskiy */ 11621e51764aSArtem Bityutskiy } else { 11631e51764aSArtem Bityutskiy /* 11641e51764aSArtem Bityutskiy * We could 'kmap()' the page and pass the data 11651e51764aSArtem Bityutskiy * to 'ubifs_jnl_truncate()' to save it from 11661e51764aSArtem Bityutskiy * having to read it. 11671e51764aSArtem Bityutskiy */ 11681e51764aSArtem Bityutskiy unlock_page(page); 11691e51764aSArtem Bityutskiy page_cache_release(page); 11701e51764aSArtem Bityutskiy } 11711e51764aSArtem Bityutskiy } 11721e51764aSArtem Bityutskiy } 11731e51764aSArtem Bityutskiy 11741e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 11751e51764aSArtem Bityutskiy ui->ui_size = inode->i_size; 11761e51764aSArtem Bityutskiy /* Truncation changes inode [mc]time */ 11771e51764aSArtem Bityutskiy inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); 11781e51764aSArtem Bityutskiy /* The other attributes may be changed at the same time as well */ 11791e51764aSArtem Bityutskiy do_attr_changes(inode, attr); 11801e51764aSArtem Bityutskiy 11811e51764aSArtem Bityutskiy err = ubifs_jnl_truncate(c, inode, old_size, new_size); 11821e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 11831e51764aSArtem Bityutskiy out_budg: 118404da11bfSArtem Bityutskiy if (budgeted) 11851e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 118604da11bfSArtem Bityutskiy else { 118704da11bfSArtem Bityutskiy c->nospace = c->nospace_rp = 0; 118804da11bfSArtem Bityutskiy smp_wmb(); 118904da11bfSArtem Bityutskiy } 11901e51764aSArtem Bityutskiy return err; 11911e51764aSArtem Bityutskiy } 11921e51764aSArtem Bityutskiy 11931e51764aSArtem Bityutskiy /** 11941e51764aSArtem Bityutskiy * do_setattr - change inode attributes. 11951e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11961e51764aSArtem Bityutskiy * @inode: inode to change attributes for 11971e51764aSArtem Bityutskiy * @attr: inode attribute changes description 11981e51764aSArtem Bityutskiy * 11991e51764aSArtem Bityutskiy * This function implements VFS '->setattr()' call for all cases except 12001e51764aSArtem Bityutskiy * truncations to smaller size. Returns zero in case of success and a negative 12011e51764aSArtem Bityutskiy * error code in case of failure. 12021e51764aSArtem Bityutskiy */ 12031e51764aSArtem Bityutskiy static int do_setattr(struct ubifs_info *c, struct inode *inode, 12041e51764aSArtem Bityutskiy const struct iattr *attr) 12051e51764aSArtem Bityutskiy { 12061e51764aSArtem Bityutskiy int err, release; 12071e51764aSArtem Bityutskiy loff_t new_size = attr->ia_size; 12081e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 12091e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .dirtied_ino = 1, 1210dab4b4d2SArtem Bityutskiy .dirtied_ino_d = ALIGN(ui->data_len, 8) }; 12111e51764aSArtem Bityutskiy 12121e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 12131e51764aSArtem Bityutskiy if (err) 12141e51764aSArtem Bityutskiy return err; 12151e51764aSArtem Bityutskiy 12161e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_SIZE) { 12171e51764aSArtem Bityutskiy dbg_gen("size %lld -> %lld", inode->i_size, new_size); 12181e51764aSArtem Bityutskiy err = vmtruncate(inode, new_size); 12191e51764aSArtem Bityutskiy if (err) 12201e51764aSArtem Bityutskiy goto out; 12211e51764aSArtem Bityutskiy } 12221e51764aSArtem Bityutskiy 12231e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 12241e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_SIZE) { 12251e51764aSArtem Bityutskiy /* Truncation changes inode [mc]time */ 12261e51764aSArtem Bityutskiy inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); 12271e51764aSArtem Bityutskiy /* 'vmtruncate()' changed @i_size, update @ui_size */ 12281e51764aSArtem Bityutskiy ui->ui_size = inode->i_size; 12291e51764aSArtem Bityutskiy } 12301e51764aSArtem Bityutskiy 12311e51764aSArtem Bityutskiy do_attr_changes(inode, attr); 12321e51764aSArtem Bityutskiy 12331e51764aSArtem Bityutskiy release = ui->dirty; 12341e51764aSArtem Bityutskiy if (attr->ia_valid & ATTR_SIZE) 12351e51764aSArtem Bityutskiy /* 12361e51764aSArtem Bityutskiy * Inode length changed, so we have to make sure 12371e51764aSArtem Bityutskiy * @I_DIRTY_DATASYNC is set. 12381e51764aSArtem Bityutskiy */ 12391e51764aSArtem Bityutskiy __mark_inode_dirty(inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC); 12401e51764aSArtem Bityutskiy else 12411e51764aSArtem Bityutskiy mark_inode_dirty_sync(inode); 12421e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 12431e51764aSArtem Bityutskiy 12441e51764aSArtem Bityutskiy if (release) 12451e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 12461e51764aSArtem Bityutskiy if (IS_SYNC(inode)) 12471e51764aSArtem Bityutskiy err = inode->i_sb->s_op->write_inode(inode, 1); 12481e51764aSArtem Bityutskiy return err; 12491e51764aSArtem Bityutskiy 12501e51764aSArtem Bityutskiy out: 12511e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 12521e51764aSArtem Bityutskiy return err; 12531e51764aSArtem Bityutskiy } 12541e51764aSArtem Bityutskiy 12551e51764aSArtem Bityutskiy int ubifs_setattr(struct dentry *dentry, struct iattr *attr) 12561e51764aSArtem Bityutskiy { 12571e51764aSArtem Bityutskiy int err; 12581e51764aSArtem Bityutskiy struct inode *inode = dentry->d_inode; 12591e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 12601e51764aSArtem Bityutskiy 12617d32c2bbSArtem Bityutskiy dbg_gen("ino %lu, mode %#x, ia_valid %#x", 12627d32c2bbSArtem Bityutskiy inode->i_ino, inode->i_mode, attr->ia_valid); 12631e51764aSArtem Bityutskiy err = inode_change_ok(inode, attr); 12641e51764aSArtem Bityutskiy if (err) 12651e51764aSArtem Bityutskiy return err; 12661e51764aSArtem Bityutskiy 12671e51764aSArtem Bityutskiy err = dbg_check_synced_i_size(inode); 12681e51764aSArtem Bityutskiy if (err) 12691e51764aSArtem Bityutskiy return err; 12701e51764aSArtem Bityutskiy 12711e51764aSArtem Bityutskiy if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size < inode->i_size) 12721e51764aSArtem Bityutskiy /* Truncation to a smaller size */ 12731e51764aSArtem Bityutskiy err = do_truncation(c, inode, attr); 12741e51764aSArtem Bityutskiy else 12751e51764aSArtem Bityutskiy err = do_setattr(c, inode, attr); 12761e51764aSArtem Bityutskiy 12771e51764aSArtem Bityutskiy return err; 12781e51764aSArtem Bityutskiy } 12791e51764aSArtem Bityutskiy 12801e51764aSArtem Bityutskiy static void ubifs_invalidatepage(struct page *page, unsigned long offset) 12811e51764aSArtem Bityutskiy { 12821e51764aSArtem Bityutskiy struct inode *inode = page->mapping->host; 12831e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 12841e51764aSArtem Bityutskiy 12851e51764aSArtem Bityutskiy ubifs_assert(PagePrivate(page)); 12861e51764aSArtem Bityutskiy if (offset) 12871e51764aSArtem Bityutskiy /* Partial page remains dirty */ 12881e51764aSArtem Bityutskiy return; 12891e51764aSArtem Bityutskiy 12901e51764aSArtem Bityutskiy if (PageChecked(page)) 12911e51764aSArtem Bityutskiy release_new_page_budget(c); 12921e51764aSArtem Bityutskiy else 12931e51764aSArtem Bityutskiy release_existing_page_budget(c); 12941e51764aSArtem Bityutskiy 12951e51764aSArtem Bityutskiy atomic_long_dec(&c->dirty_pg_cnt); 12961e51764aSArtem Bityutskiy ClearPagePrivate(page); 12971e51764aSArtem Bityutskiy ClearPageChecked(page); 12981e51764aSArtem Bityutskiy } 12991e51764aSArtem Bityutskiy 13001e51764aSArtem Bityutskiy static void *ubifs_follow_link(struct dentry *dentry, struct nameidata *nd) 13011e51764aSArtem Bityutskiy { 13021e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(dentry->d_inode); 13031e51764aSArtem Bityutskiy 13041e51764aSArtem Bityutskiy nd_set_link(nd, ui->data); 13051e51764aSArtem Bityutskiy return NULL; 13061e51764aSArtem Bityutskiy } 13071e51764aSArtem Bityutskiy 13081e51764aSArtem Bityutskiy int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync) 13091e51764aSArtem Bityutskiy { 13101e51764aSArtem Bityutskiy struct inode *inode = dentry->d_inode; 13111e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 13121e51764aSArtem Bityutskiy int err; 13131e51764aSArtem Bityutskiy 13141e51764aSArtem Bityutskiy dbg_gen("syncing inode %lu", inode->i_ino); 13151e51764aSArtem Bityutskiy 13161e51764aSArtem Bityutskiy /* 13171e51764aSArtem Bityutskiy * VFS has already synchronized dirty pages for this inode. Synchronize 13181e51764aSArtem Bityutskiy * the inode unless this is a 'datasync()' call. 13191e51764aSArtem Bityutskiy */ 13201e51764aSArtem Bityutskiy if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) { 13211e51764aSArtem Bityutskiy err = inode->i_sb->s_op->write_inode(inode, 1); 13221e51764aSArtem Bityutskiy if (err) 13231e51764aSArtem Bityutskiy return err; 13241e51764aSArtem Bityutskiy } 13251e51764aSArtem Bityutskiy 13261e51764aSArtem Bityutskiy /* 13271e51764aSArtem Bityutskiy * Nodes related to this inode may still sit in a write-buffer. Flush 13281e51764aSArtem Bityutskiy * them. 13291e51764aSArtem Bityutskiy */ 13301e51764aSArtem Bityutskiy err = ubifs_sync_wbufs_by_inode(c, inode); 13311e51764aSArtem Bityutskiy if (err) 13321e51764aSArtem Bityutskiy return err; 13331e51764aSArtem Bityutskiy 13341e51764aSArtem Bityutskiy return 0; 13351e51764aSArtem Bityutskiy } 13361e51764aSArtem Bityutskiy 13371e51764aSArtem Bityutskiy /** 13381e51764aSArtem Bityutskiy * mctime_update_needed - check if mtime or ctime update is needed. 13391e51764aSArtem Bityutskiy * @inode: the inode to do the check for 13401e51764aSArtem Bityutskiy * @now: current time 13411e51764aSArtem Bityutskiy * 13421e51764aSArtem Bityutskiy * This helper function checks if the inode mtime/ctime should be updated or 13431e51764aSArtem Bityutskiy * not. If current values of the time-stamps are within the UBIFS inode time 13441e51764aSArtem Bityutskiy * granularity, they are not updated. This is an optimization. 13451e51764aSArtem Bityutskiy */ 13461e51764aSArtem Bityutskiy static inline int mctime_update_needed(const struct inode *inode, 13471e51764aSArtem Bityutskiy const struct timespec *now) 13481e51764aSArtem Bityutskiy { 13491e51764aSArtem Bityutskiy if (!timespec_equal(&inode->i_mtime, now) || 13501e51764aSArtem Bityutskiy !timespec_equal(&inode->i_ctime, now)) 13511e51764aSArtem Bityutskiy return 1; 13521e51764aSArtem Bityutskiy return 0; 13531e51764aSArtem Bityutskiy } 13541e51764aSArtem Bityutskiy 13551e51764aSArtem Bityutskiy /** 13561e51764aSArtem Bityutskiy * update_ctime - update mtime and ctime of an inode. 13571e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13581e51764aSArtem Bityutskiy * @inode: inode to update 13591e51764aSArtem Bityutskiy * 13601e51764aSArtem Bityutskiy * This function updates mtime and ctime of the inode if it is not equivalent to 13611e51764aSArtem Bityutskiy * current time. Returns zero in case of success and a negative error code in 13621e51764aSArtem Bityutskiy * case of failure. 13631e51764aSArtem Bityutskiy */ 13641e51764aSArtem Bityutskiy static int update_mctime(struct ubifs_info *c, struct inode *inode) 13651e51764aSArtem Bityutskiy { 13661e51764aSArtem Bityutskiy struct timespec now = ubifs_current_time(inode); 13671e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 13681e51764aSArtem Bityutskiy 13691e51764aSArtem Bityutskiy if (mctime_update_needed(inode, &now)) { 13701e51764aSArtem Bityutskiy int err, release; 13711e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .dirtied_ino = 1, 1372dab4b4d2SArtem Bityutskiy .dirtied_ino_d = ALIGN(ui->data_len, 8) }; 13731e51764aSArtem Bityutskiy 13741e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 13751e51764aSArtem Bityutskiy if (err) 13761e51764aSArtem Bityutskiy return err; 13771e51764aSArtem Bityutskiy 13781e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 13791e51764aSArtem Bityutskiy inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); 13801e51764aSArtem Bityutskiy release = ui->dirty; 13811e51764aSArtem Bityutskiy mark_inode_dirty_sync(inode); 13821e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 13831e51764aSArtem Bityutskiy if (release) 13841e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 13851e51764aSArtem Bityutskiy } 13861e51764aSArtem Bityutskiy 13871e51764aSArtem Bityutskiy return 0; 13881e51764aSArtem Bityutskiy } 13891e51764aSArtem Bityutskiy 13901e51764aSArtem Bityutskiy static ssize_t ubifs_aio_write(struct kiocb *iocb, const struct iovec *iov, 13911e51764aSArtem Bityutskiy unsigned long nr_segs, loff_t pos) 13921e51764aSArtem Bityutskiy { 13931e51764aSArtem Bityutskiy int err; 13941e51764aSArtem Bityutskiy ssize_t ret; 13951e51764aSArtem Bityutskiy struct inode *inode = iocb->ki_filp->f_mapping->host; 13961e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 13971e51764aSArtem Bityutskiy 13981e51764aSArtem Bityutskiy err = update_mctime(c, inode); 13991e51764aSArtem Bityutskiy if (err) 14001e51764aSArtem Bityutskiy return err; 14011e51764aSArtem Bityutskiy 14021e51764aSArtem Bityutskiy ret = generic_file_aio_write(iocb, iov, nr_segs, pos); 14031e51764aSArtem Bityutskiy if (ret < 0) 14041e51764aSArtem Bityutskiy return ret; 14051e51764aSArtem Bityutskiy 14061e51764aSArtem Bityutskiy if (ret > 0 && (IS_SYNC(inode) || iocb->ki_filp->f_flags & O_SYNC)) { 14071e51764aSArtem Bityutskiy err = ubifs_sync_wbufs_by_inode(c, inode); 14081e51764aSArtem Bityutskiy if (err) 14091e51764aSArtem Bityutskiy return err; 14101e51764aSArtem Bityutskiy } 14111e51764aSArtem Bityutskiy 14121e51764aSArtem Bityutskiy return ret; 14131e51764aSArtem Bityutskiy } 14141e51764aSArtem Bityutskiy 14151e51764aSArtem Bityutskiy static int ubifs_set_page_dirty(struct page *page) 14161e51764aSArtem Bityutskiy { 14171e51764aSArtem Bityutskiy int ret; 14181e51764aSArtem Bityutskiy 14191e51764aSArtem Bityutskiy ret = __set_page_dirty_nobuffers(page); 14201e51764aSArtem Bityutskiy /* 14211e51764aSArtem Bityutskiy * An attempt to dirty a page without budgeting for it - should not 14221e51764aSArtem Bityutskiy * happen. 14231e51764aSArtem Bityutskiy */ 14241e51764aSArtem Bityutskiy ubifs_assert(ret == 0); 14251e51764aSArtem Bityutskiy return ret; 14261e51764aSArtem Bityutskiy } 14271e51764aSArtem Bityutskiy 14281e51764aSArtem Bityutskiy static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags) 14291e51764aSArtem Bityutskiy { 14301e51764aSArtem Bityutskiy /* 14311e51764aSArtem Bityutskiy * An attempt to release a dirty page without budgeting for it - should 14321e51764aSArtem Bityutskiy * not happen. 14331e51764aSArtem Bityutskiy */ 14341e51764aSArtem Bityutskiy if (PageWriteback(page)) 14351e51764aSArtem Bityutskiy return 0; 14361e51764aSArtem Bityutskiy ubifs_assert(PagePrivate(page)); 14371e51764aSArtem Bityutskiy ubifs_assert(0); 14381e51764aSArtem Bityutskiy ClearPagePrivate(page); 14391e51764aSArtem Bityutskiy ClearPageChecked(page); 14401e51764aSArtem Bityutskiy return 1; 14411e51764aSArtem Bityutskiy } 14421e51764aSArtem Bityutskiy 14431e51764aSArtem Bityutskiy /* 14441e51764aSArtem Bityutskiy * mmap()d file has taken write protection fault and is being made 14451e51764aSArtem Bityutskiy * writable. UBIFS must ensure page is budgeted for. 14461e51764aSArtem Bityutskiy */ 14471e51764aSArtem Bityutskiy static int ubifs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) 14481e51764aSArtem Bityutskiy { 14491e51764aSArtem Bityutskiy struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 14501e51764aSArtem Bityutskiy struct ubifs_info *c = inode->i_sb->s_fs_info; 14511e51764aSArtem Bityutskiy struct timespec now = ubifs_current_time(inode); 14521e51764aSArtem Bityutskiy struct ubifs_budget_req req = { .new_page = 1 }; 14531e51764aSArtem Bityutskiy int err, update_time; 14541e51764aSArtem Bityutskiy 14551e51764aSArtem Bityutskiy dbg_gen("ino %lu, pg %lu, i_size %lld", inode->i_ino, page->index, 14561e51764aSArtem Bityutskiy i_size_read(inode)); 14571e51764aSArtem Bityutskiy ubifs_assert(!(inode->i_sb->s_flags & MS_RDONLY)); 14581e51764aSArtem Bityutskiy 14591e51764aSArtem Bityutskiy if (unlikely(c->ro_media)) 14601e51764aSArtem Bityutskiy return -EROFS; 14611e51764aSArtem Bityutskiy 14621e51764aSArtem Bityutskiy /* 14631e51764aSArtem Bityutskiy * We have not locked @page so far so we may budget for changing the 14641e51764aSArtem Bityutskiy * page. Note, we cannot do this after we locked the page, because 14651e51764aSArtem Bityutskiy * budgeting may cause write-back which would cause deadlock. 14661e51764aSArtem Bityutskiy * 14671e51764aSArtem Bityutskiy * At the moment we do not know whether the page is dirty or not, so we 14681e51764aSArtem Bityutskiy * assume that it is not and budget for a new page. We could look at 14691e51764aSArtem Bityutskiy * the @PG_private flag and figure this out, but we may race with write 14701e51764aSArtem Bityutskiy * back and the page state may change by the time we lock it, so this 14711e51764aSArtem Bityutskiy * would need additional care. We do not bother with this at the 14721e51764aSArtem Bityutskiy * moment, although it might be good idea to do. Instead, we allocate 14731e51764aSArtem Bityutskiy * budget for a new page and amend it later on if the page was in fact 14741e51764aSArtem Bityutskiy * dirty. 14751e51764aSArtem Bityutskiy * 14761e51764aSArtem Bityutskiy * The budgeting-related logic of this function is similar to what we 14771e51764aSArtem Bityutskiy * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there 14781e51764aSArtem Bityutskiy * for more comments. 14791e51764aSArtem Bityutskiy */ 14801e51764aSArtem Bityutskiy update_time = mctime_update_needed(inode, &now); 14811e51764aSArtem Bityutskiy if (update_time) 14821e51764aSArtem Bityutskiy /* 14831e51764aSArtem Bityutskiy * We have to change inode time stamp which requires extra 14841e51764aSArtem Bityutskiy * budgeting. 14851e51764aSArtem Bityutskiy */ 14861e51764aSArtem Bityutskiy req.dirtied_ino = 1; 14871e51764aSArtem Bityutskiy 14881e51764aSArtem Bityutskiy err = ubifs_budget_space(c, &req); 14891e51764aSArtem Bityutskiy if (unlikely(err)) { 14901e51764aSArtem Bityutskiy if (err == -ENOSPC) 14911e51764aSArtem Bityutskiy ubifs_warn("out of space for mmapped file " 14921e51764aSArtem Bityutskiy "(inode number %lu)", inode->i_ino); 14931e51764aSArtem Bityutskiy return err; 14941e51764aSArtem Bityutskiy } 14951e51764aSArtem Bityutskiy 14961e51764aSArtem Bityutskiy lock_page(page); 14971e51764aSArtem Bityutskiy if (unlikely(page->mapping != inode->i_mapping || 14981e51764aSArtem Bityutskiy page_offset(page) > i_size_read(inode))) { 14991e51764aSArtem Bityutskiy /* Page got truncated out from underneath us */ 15001e51764aSArtem Bityutskiy err = -EINVAL; 15011e51764aSArtem Bityutskiy goto out_unlock; 15021e51764aSArtem Bityutskiy } 15031e51764aSArtem Bityutskiy 15041e51764aSArtem Bityutskiy if (PagePrivate(page)) 15051e51764aSArtem Bityutskiy release_new_page_budget(c); 15061e51764aSArtem Bityutskiy else { 15071e51764aSArtem Bityutskiy if (!PageChecked(page)) 15081e51764aSArtem Bityutskiy ubifs_convert_page_budget(c); 15091e51764aSArtem Bityutskiy SetPagePrivate(page); 15101e51764aSArtem Bityutskiy atomic_long_inc(&c->dirty_pg_cnt); 15111e51764aSArtem Bityutskiy __set_page_dirty_nobuffers(page); 15121e51764aSArtem Bityutskiy } 15131e51764aSArtem Bityutskiy 15141e51764aSArtem Bityutskiy if (update_time) { 15151e51764aSArtem Bityutskiy int release; 15161e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 15171e51764aSArtem Bityutskiy 15181e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 15191e51764aSArtem Bityutskiy inode->i_mtime = inode->i_ctime = ubifs_current_time(inode); 15201e51764aSArtem Bityutskiy release = ui->dirty; 15211e51764aSArtem Bityutskiy mark_inode_dirty_sync(inode); 15221e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 15231e51764aSArtem Bityutskiy if (release) 15241e51764aSArtem Bityutskiy ubifs_release_dirty_inode_budget(c, ui); 15251e51764aSArtem Bityutskiy } 15261e51764aSArtem Bityutskiy 15271e51764aSArtem Bityutskiy unlock_page(page); 15281e51764aSArtem Bityutskiy return 0; 15291e51764aSArtem Bityutskiy 15301e51764aSArtem Bityutskiy out_unlock: 15311e51764aSArtem Bityutskiy unlock_page(page); 15321e51764aSArtem Bityutskiy ubifs_release_budget(c, &req); 15331e51764aSArtem Bityutskiy return err; 15341e51764aSArtem Bityutskiy } 15351e51764aSArtem Bityutskiy 15361e51764aSArtem Bityutskiy static struct vm_operations_struct ubifs_file_vm_ops = { 15371e51764aSArtem Bityutskiy .fault = filemap_fault, 15381e51764aSArtem Bityutskiy .page_mkwrite = ubifs_vm_page_mkwrite, 15391e51764aSArtem Bityutskiy }; 15401e51764aSArtem Bityutskiy 15411e51764aSArtem Bityutskiy static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma) 15421e51764aSArtem Bityutskiy { 15431e51764aSArtem Bityutskiy int err; 15441e51764aSArtem Bityutskiy 15451e51764aSArtem Bityutskiy /* 'generic_file_mmap()' takes care of NOMMU case */ 15461e51764aSArtem Bityutskiy err = generic_file_mmap(file, vma); 15471e51764aSArtem Bityutskiy if (err) 15481e51764aSArtem Bityutskiy return err; 15491e51764aSArtem Bityutskiy vma->vm_ops = &ubifs_file_vm_ops; 15501e51764aSArtem Bityutskiy return 0; 15511e51764aSArtem Bityutskiy } 15521e51764aSArtem Bityutskiy 1553e8b81566SArtem Bityutskiy const struct address_space_operations ubifs_file_address_operations = { 15541e51764aSArtem Bityutskiy .readpage = ubifs_readpage, 15551e51764aSArtem Bityutskiy .writepage = ubifs_writepage, 15561e51764aSArtem Bityutskiy .write_begin = ubifs_write_begin, 15571e51764aSArtem Bityutskiy .write_end = ubifs_write_end, 15581e51764aSArtem Bityutskiy .invalidatepage = ubifs_invalidatepage, 15591e51764aSArtem Bityutskiy .set_page_dirty = ubifs_set_page_dirty, 15601e51764aSArtem Bityutskiy .releasepage = ubifs_releasepage, 15611e51764aSArtem Bityutskiy }; 15621e51764aSArtem Bityutskiy 1563e8b81566SArtem Bityutskiy const struct inode_operations ubifs_file_inode_operations = { 15641e51764aSArtem Bityutskiy .setattr = ubifs_setattr, 15651e51764aSArtem Bityutskiy .getattr = ubifs_getattr, 15661e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_XATTR 15671e51764aSArtem Bityutskiy .setxattr = ubifs_setxattr, 15681e51764aSArtem Bityutskiy .getxattr = ubifs_getxattr, 15691e51764aSArtem Bityutskiy .listxattr = ubifs_listxattr, 15701e51764aSArtem Bityutskiy .removexattr = ubifs_removexattr, 15711e51764aSArtem Bityutskiy #endif 15721e51764aSArtem Bityutskiy }; 15731e51764aSArtem Bityutskiy 1574e8b81566SArtem Bityutskiy const struct inode_operations ubifs_symlink_inode_operations = { 15751e51764aSArtem Bityutskiy .readlink = generic_readlink, 15761e51764aSArtem Bityutskiy .follow_link = ubifs_follow_link, 15771e51764aSArtem Bityutskiy .setattr = ubifs_setattr, 15781e51764aSArtem Bityutskiy .getattr = ubifs_getattr, 15791e51764aSArtem Bityutskiy }; 15801e51764aSArtem Bityutskiy 1581e8b81566SArtem Bityutskiy const struct file_operations ubifs_file_operations = { 15821e51764aSArtem Bityutskiy .llseek = generic_file_llseek, 15831e51764aSArtem Bityutskiy .read = do_sync_read, 15841e51764aSArtem Bityutskiy .write = do_sync_write, 15851e51764aSArtem Bityutskiy .aio_read = generic_file_aio_read, 15861e51764aSArtem Bityutskiy .aio_write = ubifs_aio_write, 15871e51764aSArtem Bityutskiy .mmap = ubifs_file_mmap, 15881e51764aSArtem Bityutskiy .fsync = ubifs_fsync, 15891e51764aSArtem Bityutskiy .unlocked_ioctl = ubifs_ioctl, 15901e51764aSArtem Bityutskiy .splice_read = generic_file_splice_read, 159122bc7fa8SZoltan Sogor .splice_write = generic_file_splice_write, 15921e51764aSArtem Bityutskiy #ifdef CONFIG_COMPAT 15931e51764aSArtem Bityutskiy .compat_ioctl = ubifs_compat_ioctl, 15941e51764aSArtem Bityutskiy #endif 15951e51764aSArtem Bityutskiy }; 1596