Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2006-2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Authors: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *          Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  */
^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)  * This file implements VFS file and inode operations for regular files, device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * nodes and symlinks as well as address space operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * UBIFS uses 2 page flags: @PG_private and @PG_checked. @PG_private is set if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * the page is dirty and is used for optimization purposes - dirty pages are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * not budgeted so the flag shows that 'ubifs_write_end()' should not release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * the budget for this page. The @PG_checked flag is set if full budgeting is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * required for the page e.g., when it corresponds to a file hole or it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * beyond the file size. The budgeting is done in 'ubifs_write_begin()', because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * it is OK to fail in this function, and the budget is released in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * 'ubifs_write_end()'. So the @PG_private and @PG_checked flags carry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * information about how the page was budgeted, to make it possible to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * the budget properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * A thing to keep in mind: inode @i_mutex is locked in most VFS operations we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * implement. However, this is not true for 'ubifs_writepage()', which may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * called with @i_mutex unlocked. For example, when flusher thread is doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * background write-back, it calls 'ubifs_writepage()' with unlocked @i_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  * At "normal" work-paths the @i_mutex is locked in 'ubifs_writepage()', e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * in the "sys_write -> alloc_pages -> direct reclaim path". So, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * 'ubifs_writepage()' we are only guaranteed that the page is locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * Similarly, @i_mutex is not always locked in 'ubifs_readpage()', e.g., the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * read-ahead path does not lock it ("sys_read -> generic_file_aio_read ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * ondemand_readahead -> readpage"). In case of readahead, @I_SYNC flag is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * set as well. However, UBIFS disables readahead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/migrate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static int read_block(struct inode *inode, void *addr, unsigned int block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 		      struct ubifs_data_node *dn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	int err, len, out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	unsigned int dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	data_key_init(c, &key, inode->i_ino, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	err = ubifs_tnc_lookup(c, &key, dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 			/* Not found, so it must be a hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 			memset(addr, 0, UBIFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	ubifs_assert(c, le64_to_cpu(dn->ch.sqnum) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		     ubifs_inode(inode)->creat_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	len = le32_to_cpu(dn->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	if (len <= 0 || len > UBIFS_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		goto dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		err = ubifs_decrypt(inode, dn, &dlen, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 			goto dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	out_len = UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 			       le16_to_cpu(dn->compr_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	if (err || len != out_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		goto dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	 * Data length can be less than a full block, even for blocks that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	 * not the last in the file (e.g., as a result of making a hole and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	 * appending data). Ensure that the remainder is zeroed out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	if (len < UBIFS_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) dump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	ubifs_err(c, "bad data node (block %u, inode %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		  block, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	ubifs_dump_node(c, dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) static int do_readpage(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	int err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	unsigned int block, beyond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	struct ubifs_data_node *dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	loff_t i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		inode->i_ino, page->index, i_size, page->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	ubifs_assert(c, !PageChecked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	ubifs_assert(c, !PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	addr = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	if (block >= beyond) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		/* Reading beyond inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		memset(addr, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	if (!dn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		if (block >= beyond) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 			/* Reading beyond inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 			err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 			memset(addr, 0, UBIFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 			ret = read_block(inode, addr, block, dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 				err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 				if (err != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 			} else if (block + 1 == beyond) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 				int dlen = le32_to_cpu(dn->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 				int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 				if (ilen && ilen < dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 					memset(addr + ilen, 0, dlen - ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		if (++i >= UBIFS_BLOCKS_PER_PAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		block += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		addr += UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			/* Not found, so it must be a hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			dbg_gen("hole");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			  page->index, inode->i_ino, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	kfree(dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	kfree(dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  * release_new_page_budget - release budget of a new page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * This is a helper function which releases budget corresponding to the budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * of one new page of data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static void release_new_page_budget(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	struct ubifs_budget_req req = { .recalculate = 1, .new_page = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * release_existing_page_budget - release budget of an existing page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * This is a helper function which releases budget corresponding to the budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * of changing one one page of data which already exists on the flash media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) static void release_existing_page_budget(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	struct ubifs_budget_req req = { .dd_growth = c->bi.page_budget};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static int write_begin_slow(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			    loff_t pos, unsigned len, struct page **pagep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			    unsigned flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	pgoff_t index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	struct ubifs_budget_req req = { .new_page = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	int err, appending = !!(pos + len > inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	dbg_gen("ino %lu, pos %llu, len %u, i_size %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		inode->i_ino, pos, len, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	 * At the slow path we have to budget before locking the page, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	 * budgeting may force write-back, which would wait on locked pages and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	 * deadlock if we had the page locked. At this point we do not know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	 * anything about the page, so assume that this is a new page which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	 * written to a hole. This corresponds to largest budget. Later the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	 * budget will be amended if this is not true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	if (appending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		/* We are appending data, budget for inode change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		req.dirtied_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (unlikely(!page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		if (!(pos & ~PAGE_MASK) && len == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 			SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 			err = do_readpage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 				ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	if (PagePrivate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		 * The page is dirty, which means it was budgeted twice:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		 *   o first time the budget was allocated by the task which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		 *     made the page dirty and set the PG_private flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		 *   o and then we budgeted for it for the second time at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		 *     very beginning of this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		 * So what we have to do is to release the page budget we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		 * allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		release_new_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	else if (!PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		 * We are changing a page which already exists on the media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		 * This means that changing the page does not make the amount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		 * of indexing information larger, and this part of the budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		 * which we have already acquired may be released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		ubifs_convert_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (appending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		 * 'ubifs_write_end()' is optimized from the fast-path part of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		 * 'ubifs_write_begin()' and expects the @ui_mutex to be locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		 * if data is appended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		if (ui->dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			 * The inode is dirty already, so we may free the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			 * budget we allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			ubifs_release_dirty_inode_budget(c, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  * allocate_budget - allocate budget for 'ubifs_write_begin()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * @page: page to allocate budget for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  * @ui: UBIFS inode object the page belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  * @appending: non-zero if the page is appended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319)  * This is a helper function for 'ubifs_write_begin()' which allocates budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320)  * for the operation. The budget is allocated differently depending on whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321)  * this is appending, whether the page is dirty or not, and so on. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322)  * function leaves the @ui->ui_mutex locked in case of appending. Returns zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  * in case of success and %-ENOSPC in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) static int allocate_budget(struct ubifs_info *c, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			   struct ubifs_inode *ui, int appending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	struct ubifs_budget_req req = { .fast = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	if (PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		if (!appending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 			 * The page is dirty and we are not appending, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 			 * means no budget is needed at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		if (ui->dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			 * The page is dirty and we are appending, so the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			 * has to be marked as dirty. However, it is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			 * dirty, so we do not need any budget. We may return,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 			 * but @ui->ui_mutex hast to be left locked because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			 * should prevent write-back from flushing the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 			 * and freeing the budget. The lock will be released in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			 * 'ubifs_write_end()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		 * The page is dirty, we are appending, the inode is clean, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		 * we need to budget the inode change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		req.dirtied_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		if (PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			 * The page corresponds to a hole and does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			 * exist on the media. So changing it makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			 * make the amount of indexing information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 			 * larger, and we have to budget for a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			 * page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			req.new_page = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			 * Not a hole, the change will not add any new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			 * indexing information, budget for page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 			 * change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 			req.dirtied_page = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		if (appending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 			mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 			if (!ui->dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 				 * The inode is clean but we will have to mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 				 * it as dirty because we are appending. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 				 * needs a budget.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 				req.dirtied_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	return ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390)  * This function is called when a page of data is going to be written. Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391)  * the page of data will not necessarily go to the flash straight away, UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392)  * has to reserve space on the media for it, which is done by means of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393)  * budgeting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * This is the hot-path of the file-system and we are trying to optimize it as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * much as possible. For this reasons it is split on 2 parts - slow and fast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * There many budgeting cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  *     o a new page is appended - we have to budget for a new page and for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400)  *       changing the inode; however, if the inode is already dirty, there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  *       no need to budget for it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  *     o an existing clean page is changed - we have budget for it; if the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403)  *       does not exist on the media (a hole), we have to budget for a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  *       page; otherwise, we may budget for changing an existing page; the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  *       difference between these cases is that changing an existing page does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  *       not introduce anything new to the FS indexing information, so it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  *       not grow, and smaller budget is acquired in this case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  *     o an existing dirty page is changed - no need to budget at all, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  *       the page budget has been acquired by earlier, when the page has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  *       marked dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412)  * UBIFS budgeting sub-system may force write-back if it thinks there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413)  * space to reserve. This imposes some locking restrictions and makes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414)  * impossible to take into account the above cases, and makes it impossible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415)  * optimize budgeting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * The solution for this is that the fast path of 'ubifs_write_begin()' assumes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * there is a plenty of flash space and the budget will be acquired quickly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * without forcing write-back. The slow path does not make this assumption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static int ubifs_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 			     loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 			     struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	pgoff_t index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	int err, appending = !!(pos + len > inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	int skipped_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	ubifs_assert(c, ubifs_inode(inode)->ui_size == inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	if (unlikely(c->ro_error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	/* Try out the fast-path part first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (unlikely(!page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		/* The page is not loaded from the flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		if (!(pos & ~PAGE_MASK) && len == PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			 * We change whole page so no need to load it. But we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			 * do not know whether this page exists on the media or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			 * not, so we assume the latter because it requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 			 * larger budget. The assumption is that it is better
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			 * to budget a bit more than to read the page from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 			 * media. Thus, we are setting the @PG_checked flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			 * here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 			skipped_read = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			err = do_readpage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	err = allocate_budget(c, page, ui, appending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		ubifs_assert(c, err == -ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		 * If we skipped reading the page because we were going to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		 * write all of it, then it is not up to date.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		if (skipped_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 			ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		 * Budgeting failed which means it would have to force
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		 * write-back but didn't, because we set the @fast flag in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		 * request. Write-back cannot be done now, while we have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		 * page locked, because it would deadlock. Unlock and free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		 * everything and fall-back to slow-path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		if (appending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		return write_begin_slow(mapping, pos, len, pagep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	 * Whee, we acquired budgeting quickly - without involving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	 * garbage-collection, committing or forcing write-back. We return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	 * with @ui->ui_mutex locked if we are appending pages, and unlocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	 * otherwise. This is an optimization (slightly hacky though).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  * cancel_budget - cancel budget.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513)  * @page: page to cancel budget for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  * @ui: UBIFS inode object the page belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515)  * @appending: non-zero if the page is appended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  * This is a helper function for a page write operation. It unlocks the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  * @ui->ui_mutex in case of appending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) static void cancel_budget(struct ubifs_info *c, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			  struct ubifs_inode *ui, int appending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (appending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		if (!ui->dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 			ubifs_release_dirty_inode_budget(c, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	if (!PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		if (PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 			release_new_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			release_existing_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) static int ubifs_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			   loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 			   struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	loff_t end_pos = pos + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	int appending = !!(end_pos > inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		inode->i_ino, pos, page->index, len, copied, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	if (unlikely(copied < len && len == PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		 * VFS copied less data to the page that it intended and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		 * declared in its '->write_begin()' call via the @len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		 * argument. If the page was not up-to-date, and @len was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		 * @PAGE_SIZE, the 'ubifs_write_begin()' function did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		 * not load it from the media (for optimization reasons). This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		 * means that part of the page contains garbage. So read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		 * page now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		dbg_gen("copied %d instead of %d, read page and repeat",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			copied, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		cancel_budget(c, page, ui, appending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		 * Return 0 to force VFS to repeat the whole operation, or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		 * error code if 'do_readpage()' fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		copied = do_readpage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (!PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		attach_page_private(page, (void *)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		atomic_long_inc(&c->dirty_pg_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		__set_page_dirty_nobuffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (appending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		i_size_write(inode, end_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		ui->ui_size = end_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		 * Note, we do not set @I_DIRTY_PAGES (which means that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		 * inode has dirty pages), this has been done in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		 * '__set_page_dirty_nobuffers()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * populate_page - copy data nodes into a page for bulk-read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * @page: page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * @bu: bulk-read information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * @n: next zbranch slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  * This function returns %0 on success and a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) static int populate_page(struct ubifs_info *c, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			 struct bu_info *bu, int *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	int i = 0, nn = *n, offs = bu->zbranch[0].offs, hole = 0, read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	loff_t i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	unsigned int page_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	void *addr, *zaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	pgoff_t end_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		inode->i_ino, page->index, i_size, page->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	addr = zaddr = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	end_index = (i_size - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	if (!i_size || page->index > end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		hole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		memset(addr, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		goto out_hole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	page_block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		int err, len, out_len, dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		if (nn >= bu->cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			hole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			memset(addr, 0, UBIFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		} else if (key_block(c, &bu->zbranch[nn].key) == page_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			struct ubifs_data_node *dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			dn = bu->buf + (bu->zbranch[nn].offs - offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			ubifs_assert(c, le64_to_cpu(dn->ch.sqnum) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 				     ubifs_inode(inode)->creat_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			len = le32_to_cpu(dn->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			if (len <= 0 || len > UBIFS_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 				goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			out_len = UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 				err = ubifs_decrypt(inode, dn, &dlen, page_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 					goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 					       le16_to_cpu(dn->compr_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			if (err || len != out_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 				goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			if (len < UBIFS_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			nn += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			read = (i << UBIFS_BLOCK_SHIFT) + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		} else if (key_block(c, &bu->zbranch[nn].key) < page_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			nn += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			hole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			memset(addr, 0, UBIFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		if (++i >= UBIFS_BLOCKS_PER_PAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		addr += UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		page_block += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	if (end_index == page->index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		int len = i_size & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		if (len && len < read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			memset(zaddr + len, 0, read - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) out_hole:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	if (hole) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		dbg_gen("hole");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	ClearPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	*n = nn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	ubifs_err(c, "bad data node (block %u, inode %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		  page_block, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  * ubifs_do_bulk_read - do bulk-read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  * @bu: bulk-read information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  * @page1: first page to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * This function returns %1 if the bulk-read is done, otherwise %0 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 			      struct page *page1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	pgoff_t offset = page1->index, end_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct address_space *mapping = page1->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	int err, page_idx, page_cnt, ret = 0, n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	int allocate = bu->buf ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	loff_t isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	gfp_t ra_gfp_mask = readahead_gfp_mask(mapping) & ~__GFP_FS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	err = ubifs_tnc_get_bu_keys(c, bu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		goto out_warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (bu->eof) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		/* Turn off bulk-read at the end of the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		ui->read_in_a_row = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		ui->bulk_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	page_cnt = bu->blk_cnt >> UBIFS_BLOCKS_PER_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	if (!page_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		 * This happens when there are multiple blocks per page and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		 * blocks for the first page we are looking for, are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		 * together. If all the pages were like this, bulk-read would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		 * reduce performance, so we turn it off for a while.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		goto out_bu_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (bu->cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		if (allocate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			 * Allocate bulk-read buffer depending on how many data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			 * nodes we are going to read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 			bu->buf_len = bu->zbranch[bu->cnt - 1].offs +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 				      bu->zbranch[bu->cnt - 1].len -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 				      bu->zbranch[0].offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			ubifs_assert(c, bu->buf_len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			ubifs_assert(c, bu->buf_len <= c->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			bu->buf = kmalloc(bu->buf_len, GFP_NOFS | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			if (!bu->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 				goto out_bu_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		err = ubifs_tnc_bulk_read(c, bu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			goto out_warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	err = populate_page(c, page1, bu, &n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		goto out_warn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	unlock_page(page1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	if (isize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	end_index = ((isize - 1) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	for (page_idx = 1; page_idx < page_cnt; page_idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		pgoff_t page_offset = offset + page_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		if (page_offset > end_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		page = pagecache_get_page(mapping, page_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				 FGP_LOCK|FGP_ACCESSED|FGP_CREAT|FGP_NOWAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 				 ra_gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		if (!PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			err = populate_page(c, page, bu, &n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	ui->last_page_read = offset + page_idx - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	if (allocate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		kfree(bu->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) out_warn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	ubifs_warn(c, "ignoring error %d and skipping bulk-read", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) out_bu_off:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	ui->read_in_a_row = ui->bulk_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  * ubifs_bulk_read - determine whether to bulk-read and, if so, do it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * @page: page from which to start bulk-read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * Some flash media are capable of reading sequentially at faster rates. UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  * bulk-read facility is designed to take advantage of that, by reading in one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824)  * go consecutive data nodes that are also located consecutively in the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825)  * LEB. This function returns %1 if a bulk-read is done and %0 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) static int ubifs_bulk_read(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	pgoff_t index = page->index, last_page_read = ui->last_page_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	struct bu_info *bu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	int err = 0, allocated = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	ui->last_page_read = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (!c->bulk_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	 * Bulk-read is protected by @ui->ui_mutex, but it is an optimization,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	 * so don't bother if we cannot lock the mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	if (!mutex_trylock(&ui->ui_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (index != last_page_read + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		/* Turn off bulk-read if we stop reading sequentially */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		ui->read_in_a_row = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		if (ui->bulk_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			ui->bulk_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (!ui->bulk_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		ui->read_in_a_row += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (ui->read_in_a_row < 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		/* Three reads in a row, so switch on bulk-read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		ui->bulk_read = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	 * If possible, try to use pre-allocated bulk-read information, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 * is protected by @c->bu_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (mutex_trylock(&c->bu_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		bu = &c->bu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		if (!bu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		bu->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		allocated = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	bu->buf_len = c->max_bu_buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	data_key_init(c, &bu->key, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		      page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	err = ubifs_do_bulk_read(c, bu, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	if (!allocated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		mutex_unlock(&c->bu_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		kfree(bu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) static int ubifs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (ubifs_bulk_read(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	do_readpage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) static int do_writepage(struct page *page, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	int err = 0, i, blen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	unsigned int block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) #ifdef UBIFS_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	spin_lock(&ui->ui_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	ubifs_assert(c, page->index <= ui->synced_i_size >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	spin_unlock(&ui->ui_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	/* Update radix tree tags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	set_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	addr = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		blen = min_t(int, len, UBIFS_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		data_key_init(c, &key, inode->i_ino, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		err = ubifs_jnl_write_data(c, inode, &key, addr, blen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		if (++i >= UBIFS_BLOCKS_PER_PAGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		block += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		addr += blen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		len -= blen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		ubifs_err(c, "cannot write page %lu of inode %lu, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			  page->index, inode->i_ino, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		ubifs_ro_mode(c, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	ubifs_assert(c, PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	if (PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		release_new_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		release_existing_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	atomic_long_dec(&c->dirty_pg_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960)  * When writing-back dirty inodes, VFS first writes-back pages belonging to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961)  * inode, then the inode itself. For UBIFS this may cause a problem. Consider a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962)  * situation when a we have an inode with size 0, then a megabyte of data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  * appended to the inode, then write-back starts and flushes some amount of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  * dirty pages, the journal becomes full, commit happens and finishes, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  * an unclean reboot happens. When the file system is mounted next time, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966)  * inode size would still be 0, but there would be many pages which are beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967)  * the inode size, they would be indexed and consume flash space. Because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968)  * journal has been committed, the replay would not be able to detect this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969)  * situation and correct the inode size. This means UBIFS would have to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970)  * whole index and correct all inode sizes, which is long an unacceptable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  * To prevent situations like this, UBIFS writes pages back only if they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  * within the last synchronized inode size, i.e. the size which has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974)  * written to the flash media last time. Otherwise, UBIFS forces inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975)  * write-back, thus making sure the on-flash inode contains current inode size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976)  * and then keeps writing pages back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978)  * Some locking issues explanation. 'ubifs_writepage()' first is called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979)  * the page locked, and it locks @ui_mutex. However, write-back does take inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980)  * @i_mutex, which means other VFS operations may be run on this inode at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981)  * same time. And the problematic one is truncation to smaller size, from where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982)  * we have to call 'truncate_setsize()', which first changes @inode->i_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983)  * then drops the truncated pages. And while dropping the pages, it takes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984)  * page lock. This means that 'do_truncation()' cannot call 'truncate_setsize()'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985)  * with @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986)  * This means that @inode->i_size is changed while @ui_mutex is unlocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988)  * XXX(truncate): with the new truncate sequence this is not true anymore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  * and the calls to truncate_setsize can be move around freely.  They should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990)  * be moved to the very end of the truncate sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992)  * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993)  * inode size. How do we do this if @inode->i_size may became smaller while we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  * internally and updates it under @ui_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * Q: why we do not worry that if we race with truncation, we may end up with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * situation when the inode is truncated while we are in the middle of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * 'do_writepage()', so we do write beyond inode size?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * A: If we are in the middle of 'do_writepage()', truncation would be locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  * on the page lock and it would not write the truncated inode node to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * journal before we have finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int ubifs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	loff_t i_size =  i_size_read(inode), synced_i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	pgoff_t end_index = i_size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	int err, len = i_size & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	void *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	dbg_gen("ino %lu, pg %lu, pg flags %#lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		inode->i_ino, page->index, page->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	ubifs_assert(c, PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	/* Is the page fully outside @i_size? (truncate in progress) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	if (page->index > end_index || (page->index == end_index && !len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	spin_lock(&ui->ui_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	synced_i_size = ui->synced_i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	spin_unlock(&ui->ui_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	/* Is the page fully inside @i_size? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (page->index < end_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		if (page->index >= synced_i_size >> PAGE_SHIFT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 			err = inode->i_sb->s_op->write_inode(inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 				goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			 * The inode has been written, but the write-buffer has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			 * not been synchronized, so in case of an unclean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 			 * reboot we may end up with some pages beyond inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			 * size, but they would be in the journal (because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			 * commit flushes write buffers) and recovery would deal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			 * with this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		return do_writepage(page, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 * The page straddles @i_size. It must be zeroed out on each and every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	 * writepage invocation because it may be mmapped. "A file is mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	 * in multiples of the page size. For a file that is not a multiple of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	 * the page size, the remaining memory is zeroed when mapped, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	 * writes to that region are not written out to the file."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	kaddr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	memset(kaddr + len, 0, PAGE_SIZE - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	if (i_size > synced_i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		err = inode->i_sb->s_op->write_inode(inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	return do_writepage(page, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)  * do_attr_changes - change inode attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)  * @inode: inode to change attributes for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)  * @attr: describes attributes to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static void do_attr_changes(struct inode *inode, const struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	if (attr->ia_valid & ATTR_UID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		inode->i_uid = attr->ia_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (attr->ia_valid & ATTR_GID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		inode->i_gid = attr->ia_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (attr->ia_valid & ATTR_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		inode->i_atime = attr->ia_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (attr->ia_valid & ATTR_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		inode->i_mtime = attr->ia_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	if (attr->ia_valid & ATTR_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		inode->i_ctime = attr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (attr->ia_valid & ATTR_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		umode_t mode = attr->ia_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			mode &= ~S_ISGID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099)  * do_truncation - truncate an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)  * @inode: inode to truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)  * @attr: inode attribute changes description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)  * This function implements VFS '->setattr()' call when the inode is truncated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)  * to a smaller size. Returns zero in case of success and a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)  * in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) static int do_truncation(struct ubifs_info *c, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 			 const struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	struct ubifs_budget_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	loff_t old_size = inode->i_size, new_size = attr->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	memset(&req, 0, sizeof(struct ubifs_budget_req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	 * If this is truncation to a smaller size, and we do not truncate on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	 * block boundary, budget for changing one data block, because the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	 * block will be re-written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	if (new_size & (UBIFS_BLOCK_SIZE - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		req.dirtied_page = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	req.dirtied_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	/* A funny way to budget for truncation node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		 * Treat truncations to zero as deletion and always allow them,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		 * just like we do for '->unlink()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		if (new_size || err != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		budgeted = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	truncate_setsize(inode, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	if (offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		pgoff_t index = new_size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		page = find_lock_page(inode->i_mapping, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			if (PageDirty(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 				 * 'ubifs_jnl_truncate()' will try to truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 				 * the last data node, but it contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 				 * out-of-date data because the page is dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 				 * Write the page now, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 				 * 'ubifs_jnl_truncate()' will see an already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 				 * truncated (and up to date) data node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 				ubifs_assert(c, PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 				clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 				if (UBIFS_BLOCKS_PER_PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 					offset = new_size &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 						 (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 				err = do_writepage(page, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 				if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 					goto out_budg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 				 * We could now tell 'ubifs_jnl_truncate()' not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				 * to read the last block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 				 * We could 'kmap()' the page and pass the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 				 * to 'ubifs_jnl_truncate()' to save it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 				 * having to read it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	ui->ui_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	/* Truncation changes inode [mc]time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	/* Other attributes may be changed at the same time as well */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	do_attr_changes(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	err = ubifs_jnl_truncate(c, inode, old_size, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) out_budg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	if (budgeted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		c->bi.nospace = c->bi.nospace_rp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)  * do_setattr - change inode attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)  * @inode: inode to change attributes for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  * @attr: inode attribute changes description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  * This function implements VFS '->setattr()' call for all cases except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  * truncations to smaller size. Returns zero in case of success and a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  * error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) static int do_setattr(struct ubifs_info *c, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		      const struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	int err, release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	loff_t new_size = attr->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	struct ubifs_budget_req req = { .dirtied_ino = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		dbg_gen("size %lld -> %lld", inode->i_size, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		truncate_setsize(inode, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		/* Truncation changes inode [mc]time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		/* 'truncate_setsize()' changed @i_size, update @ui_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		ui->ui_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	do_attr_changes(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	release = ui->dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	if (attr->ia_valid & ATTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		 * Inode length changed, so we have to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		 * @I_DIRTY_DATASYNC is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		mark_inode_dirty_sync(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	if (release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		err = inode->i_sb->s_op->write_inode(inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) int ubifs_setattr(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	dbg_gen("ino %lu, mode %#x, ia_valid %#x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		inode->i_ino, inode->i_mode, attr->ia_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	err = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	err = dbg_check_synced_i_size(c, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	err = fscrypt_prepare_setattr(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size < inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		/* Truncation to a smaller size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		err = do_truncation(c, inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		err = do_setattr(c, inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) static void ubifs_invalidatepage(struct page *page, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				 unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	ubifs_assert(c, PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	if (offset || length < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		/* Partial page remains dirty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		release_new_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		release_existing_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	atomic_long_dec(&c->dirty_pg_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) int ubifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	dbg_gen("syncing inode %lu", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	if (c->ro_mount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		 * For some really strange reasons VFS does not filter out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		 * 'fsync()' for R/O mounted file-systems as per 2.6.39.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	err = file_write_and_wait_range(file, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	/* Synchronize the inode unless this is a 'datasync()' call. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		err = inode->i_sb->s_op->write_inode(inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	 * Nodes related to this inode may still sit in a write-buffer. Flush
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	 * them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	err = ubifs_sync_wbufs_by_inode(c, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)  * mctime_update_needed - check if mtime or ctime update is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)  * @inode: the inode to do the check for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)  * @now: current time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)  * This helper function checks if the inode mtime/ctime should be updated or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)  * not. If current values of the time-stamps are within the UBIFS inode time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)  * granularity, they are not updated. This is an optimization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) static inline int mctime_update_needed(const struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 				       const struct timespec64 *now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	if (!timespec64_equal(&inode->i_mtime, now) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	    !timespec64_equal(&inode->i_ctime, now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  * ubifs_update_time - update time of inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * @inode: inode to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  * This function updates time of the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) int ubifs_update_time(struct inode *inode, struct timespec64 *time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 			     int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	struct ubifs_budget_req req = { .dirtied_ino = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			.dirtied_ino_d = ALIGN(ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	int err, release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	if (!IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		return generic_update_time(inode, time, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	if (flags & S_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		inode->i_atime = *time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (flags & S_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		inode->i_ctime = *time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	if (flags & S_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		inode->i_mtime = *time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	release = ui->dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	__mark_inode_dirty(inode, I_DIRTY_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	if (release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)  * update_mctime - update mtime and ctime of an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)  * @inode: inode to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * This function updates mtime and ctime of the inode if it is not equivalent to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  * current time. Returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) static int update_mctime(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	struct timespec64 now = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	if (mctime_update_needed(inode, &now)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		int err, release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		struct ubifs_budget_req req = { .dirtied_ino = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		release = ui->dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		mark_inode_dirty_sync(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		if (release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 			ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static ssize_t ubifs_write_iter(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	int err = update_mctime(file_inode(iocb->ki_filp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	return generic_file_write_iter(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) static int ubifs_set_page_dirty(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	ret = __set_page_dirty_nobuffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	 * An attempt to dirty a page without budgeting for it - should not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	 * happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	ubifs_assert(c, ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) #ifdef CONFIG_MIGRATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) static int ubifs_migrate_page(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		struct page *newpage, struct page *page, enum migrate_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	rc = migrate_page_move_mapping(mapping, newpage, page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	if (rc != MIGRATEPAGE_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	if (PagePrivate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		attach_page_private(newpage, (void *)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	if (mode != MIGRATE_SYNC_NO_COPY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		migrate_page_copy(newpage, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		migrate_page_states(newpage, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	return MIGRATEPAGE_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	 * An attempt to release a dirty page without budgeting for it - should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	 * not happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	if (PageWriteback(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	ubifs_assert(c, PagePrivate(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	ubifs_assert(c, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	detach_page_private(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  * mmap()d file has taken write protection fault and is being made writable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * UBIFS must ensure page is budgeted for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) static vm_fault_t ubifs_vm_page_mkwrite(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	struct page *page = vmf->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	struct inode *inode = file_inode(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	struct ubifs_info *c = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct timespec64 now = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct ubifs_budget_req req = { .new_page = 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	int err, update_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	dbg_gen("ino %lu, pg %lu, i_size %lld",	inode->i_ino, page->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		i_size_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	if (unlikely(c->ro_error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		return VM_FAULT_SIGBUS; /* -EROFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	 * We have not locked @page so far so we may budget for changing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	 * page. Note, we cannot do this after we locked the page, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	 * budgeting may cause write-back which would cause deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	 * At the moment we do not know whether the page is dirty or not, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	 * assume that it is not and budget for a new page. We could look at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	 * the @PG_private flag and figure this out, but we may race with write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	 * back and the page state may change by the time we lock it, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	 * would need additional care. We do not bother with this at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	 * moment, although it might be good idea to do. Instead, we allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	 * budget for a new page and amend it later on if the page was in fact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	 * dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	 * The budgeting-related logic of this function is similar to what we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	 * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	 * for more comments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	update_time = mctime_update_needed(inode, &now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	if (update_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		 * We have to change inode time stamp which requires extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 		 * budgeting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		req.dirtied_ino = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	err = ubifs_budget_space(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		if (err == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			ubifs_warn(c, "out of space for mmapped file (inode number %lu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 				   inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	if (unlikely(page->mapping != inode->i_mapping ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		     page_offset(page) > i_size_read(inode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		/* Page got truncated out from underneath us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	if (PagePrivate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		release_new_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		if (!PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			ubifs_convert_page_budget(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		attach_page_private(page, (void *)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		atomic_long_inc(&c->dirty_pg_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		__set_page_dirty_nobuffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	if (update_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		int release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		mutex_lock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 		inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		release = ui->dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		mark_inode_dirty_sync(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		mutex_unlock(&ui->ui_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		if (release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			ubifs_release_dirty_inode_budget(c, ui);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	wait_for_stable_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	return VM_FAULT_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) sigbus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	ubifs_release_budget(c, &req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) static const struct vm_operations_struct ubifs_file_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	.fault        = filemap_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	.map_pages = filemap_map_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	.page_mkwrite = ubifs_vm_page_mkwrite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	err = generic_file_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	vma->vm_ops = &ubifs_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	if (IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		file_accessed(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) static const char *ubifs_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 					    struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 					    struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	struct ubifs_inode *ui = ubifs_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	if (!IS_ENCRYPTED(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		return ui->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		return ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	return fscrypt_get_symlink(inode, ui->data, ui->data_len, done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) static int ubifs_symlink_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 				 u32 request_mask, unsigned int query_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	ubifs_getattr(path, stat, request_mask, query_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	if (IS_ENCRYPTED(d_inode(path->dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		return fscrypt_symlink_getattr(path, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) const struct address_space_operations ubifs_file_address_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	.readpage       = ubifs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	.writepage      = ubifs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	.write_begin    = ubifs_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	.write_end      = ubifs_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	.invalidatepage = ubifs_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	.set_page_dirty = ubifs_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) #ifdef CONFIG_MIGRATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	.migratepage	= ubifs_migrate_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	.releasepage    = ubifs_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) const struct inode_operations ubifs_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	.setattr     = ubifs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	.getattr     = ubifs_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) #ifdef CONFIG_UBIFS_FS_XATTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	.listxattr   = ubifs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	.update_time = ubifs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) const struct inode_operations ubifs_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	.get_link    = ubifs_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	.setattr     = ubifs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	.getattr     = ubifs_symlink_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) #ifdef CONFIG_UBIFS_FS_XATTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	.listxattr   = ubifs_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	.update_time = ubifs_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) const struct file_operations ubifs_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	.llseek         = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	.read_iter      = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	.write_iter     = ubifs_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	.mmap           = ubifs_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	.fsync          = ubifs_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	.unlocked_ioctl = ubifs_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	.splice_read	= generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	.splice_write	= iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	.open		= fscrypt_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	.compat_ioctl   = ubifs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) };