xref: /linux/fs/ubifs/file.c (revision 25e79a7f)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy  *
71e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
81e51764aSArtem Bityutskiy  *          Adrian Hunter
91e51764aSArtem Bityutskiy  */
101e51764aSArtem Bityutskiy 
111e51764aSArtem Bityutskiy /*
12873a64c7SArtem Bityutskiy  * This file implements VFS file and inode operations for regular files, device
131e51764aSArtem Bityutskiy  * nodes and symlinks as well as address space operations.
141e51764aSArtem Bityutskiy  *
15873a64c7SArtem Bityutskiy  * UBIFS uses 2 page flags: @PG_private and @PG_checked. @PG_private is set if
16873a64c7SArtem Bityutskiy  * the page is dirty and is used for optimization purposes - dirty pages are
17873a64c7SArtem Bityutskiy  * not budgeted so the flag shows that 'ubifs_write_end()' should not release
18873a64c7SArtem Bityutskiy  * the budget for this page. The @PG_checked flag is set if full budgeting is
19873a64c7SArtem Bityutskiy  * required for the page e.g., when it corresponds to a file hole or it is
20873a64c7SArtem Bityutskiy  * beyond the file size. The budgeting is done in 'ubifs_write_begin()', because
21873a64c7SArtem Bityutskiy  * it is OK to fail in this function, and the budget is released in
22873a64c7SArtem Bityutskiy  * 'ubifs_write_end()'. So the @PG_private and @PG_checked flags carry
23873a64c7SArtem Bityutskiy  * information about how the page was budgeted, to make it possible to release
24873a64c7SArtem Bityutskiy  * the budget properly.
251e51764aSArtem Bityutskiy  *
26873a64c7SArtem Bityutskiy  * A thing to keep in mind: inode @i_mutex is locked in most VFS operations we
27873a64c7SArtem Bityutskiy  * implement. However, this is not true for 'ubifs_writepage()', which may be
285c57f20bSArtem Bityutskiy  * called with @i_mutex unlocked. For example, when flusher thread is doing
295c57f20bSArtem Bityutskiy  * background write-back, it calls 'ubifs_writepage()' with unlocked @i_mutex.
305c57f20bSArtem Bityutskiy  * At "normal" work-paths the @i_mutex is locked in 'ubifs_writepage()', e.g.
315c57f20bSArtem Bityutskiy  * in the "sys_write -> alloc_pages -> direct reclaim path". So, in
325c57f20bSArtem Bityutskiy  * 'ubifs_writepage()' we are only guaranteed that the page is locked.
331e51764aSArtem Bityutskiy  *
340b7bf483SMatthew Wilcox (Oracle)  * Similarly, @i_mutex is not always locked in 'ubifs_read_folio()', e.g., the
35873a64c7SArtem Bityutskiy  * read-ahead path does not lock it ("sys_read -> generic_file_aio_read ->
360b7bf483SMatthew Wilcox (Oracle)  * ondemand_readahead -> read_folio"). In case of readahead, @I_SYNC flag is not
37873a64c7SArtem Bityutskiy  * set as well. However, UBIFS disables readahead.
381e51764aSArtem Bityutskiy  */
391e51764aSArtem Bityutskiy 
401e51764aSArtem Bityutskiy #include "ubifs.h"
411e51764aSArtem Bityutskiy #include <linux/mount.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
434ac1c17bSKirill A. Shutemov #include <linux/migrate.h>
441e51764aSArtem Bityutskiy 
read_block(struct inode * inode,void * addr,unsigned int block,struct ubifs_data_node * dn)451e51764aSArtem Bityutskiy static int read_block(struct inode *inode, void *addr, unsigned int block,
461e51764aSArtem Bityutskiy 		      struct ubifs_data_node *dn)
471e51764aSArtem Bityutskiy {
481e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
491e51764aSArtem Bityutskiy 	int err, len, out_len;
501e51764aSArtem Bityutskiy 	union ubifs_key key;
511e51764aSArtem Bityutskiy 	unsigned int dlen;
521e51764aSArtem Bityutskiy 
531e51764aSArtem Bityutskiy 	data_key_init(c, &key, inode->i_ino, block);
541e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup(c, &key, dn);
551e51764aSArtem Bityutskiy 	if (err) {
561e51764aSArtem Bityutskiy 		if (err == -ENOENT)
571e51764aSArtem Bityutskiy 			/* Not found, so it must be a hole */
581e51764aSArtem Bityutskiy 			memset(addr, 0, UBIFS_BLOCK_SIZE);
591e51764aSArtem Bityutskiy 		return err;
601e51764aSArtem Bityutskiy 	}
611e51764aSArtem Bityutskiy 
626eb61d58SRichard Weinberger 	ubifs_assert(c, le64_to_cpu(dn->ch.sqnum) >
63f92b9826SArtem Bityutskiy 		     ubifs_inode(inode)->creat_sqnum);
641e51764aSArtem Bityutskiy 	len = le32_to_cpu(dn->size);
651e51764aSArtem Bityutskiy 	if (len <= 0 || len > UBIFS_BLOCK_SIZE)
661e51764aSArtem Bityutskiy 		goto dump;
671e51764aSArtem Bityutskiy 
681e51764aSArtem Bityutskiy 	dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
697799953bSRichard Weinberger 
7050d9fad7SEric Biggers 	if (IS_ENCRYPTED(inode)) {
717799953bSRichard Weinberger 		err = ubifs_decrypt(inode, dn, &dlen, block);
727799953bSRichard Weinberger 		if (err)
737799953bSRichard Weinberger 			goto dump;
747799953bSRichard Weinberger 	}
757799953bSRichard Weinberger 
761e51764aSArtem Bityutskiy 	out_len = UBIFS_BLOCK_SIZE;
77235c362bSSheng Yong 	err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
781e51764aSArtem Bityutskiy 			       le16_to_cpu(dn->compr_type));
791e51764aSArtem Bityutskiy 	if (err || len != out_len)
801e51764aSArtem Bityutskiy 		goto dump;
811e51764aSArtem Bityutskiy 
821e51764aSArtem Bityutskiy 	/*
831e51764aSArtem Bityutskiy 	 * Data length can be less than a full block, even for blocks that are
841e51764aSArtem Bityutskiy 	 * not the last in the file (e.g., as a result of making a hole and
851e51764aSArtem Bityutskiy 	 * appending data). Ensure that the remainder is zeroed out.
861e51764aSArtem Bityutskiy 	 */
871e51764aSArtem Bityutskiy 	if (len < UBIFS_BLOCK_SIZE)
881e51764aSArtem Bityutskiy 		memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
891e51764aSArtem Bityutskiy 
901e51764aSArtem Bityutskiy 	return 0;
911e51764aSArtem Bityutskiy 
921e51764aSArtem Bityutskiy dump:
93235c362bSSheng Yong 	ubifs_err(c, "bad data node (block %u, inode %lu)",
941e51764aSArtem Bityutskiy 		  block, inode->i_ino);
95a33e30a0SZhihao Cheng 	ubifs_dump_node(c, dn, UBIFS_MAX_DATA_NODE_SZ);
961e51764aSArtem Bityutskiy 	return -EINVAL;
971e51764aSArtem Bityutskiy }
981e51764aSArtem Bityutskiy 
do_readpage(struct folio * folio)99b96af1fdSMatthew Wilcox (Oracle) static int do_readpage(struct folio *folio)
1001e51764aSArtem Bityutskiy {
1011e51764aSArtem Bityutskiy 	void *addr;
1021e51764aSArtem Bityutskiy 	int err = 0, i;
1031e51764aSArtem Bityutskiy 	unsigned int block, beyond;
104b96af1fdSMatthew Wilcox (Oracle) 	struct ubifs_data_node *dn = NULL;
105b96af1fdSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
1066eb61d58SRichard Weinberger 	struct ubifs_info *c = inode->i_sb->s_fs_info;
1071e51764aSArtem Bityutskiy 	loff_t i_size = i_size_read(inode);
1081e51764aSArtem Bityutskiy 
1091e51764aSArtem Bityutskiy 	dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
110b96af1fdSMatthew Wilcox (Oracle) 		inode->i_ino, folio->index, i_size, folio->flags);
111b96af1fdSMatthew Wilcox (Oracle) 	ubifs_assert(c, !folio_test_checked(folio));
112b96af1fdSMatthew Wilcox (Oracle) 	ubifs_assert(c, !folio->private);
1131e51764aSArtem Bityutskiy 
114b96af1fdSMatthew Wilcox (Oracle) 	addr = kmap_local_folio(folio, 0);
1151e51764aSArtem Bityutskiy 
116b96af1fdSMatthew Wilcox (Oracle) 	block = folio->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
1171e51764aSArtem Bityutskiy 	beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
1181e51764aSArtem Bityutskiy 	if (block >= beyond) {
1191e51764aSArtem Bityutskiy 		/* Reading beyond inode */
120b96af1fdSMatthew Wilcox (Oracle) 		folio_set_checked(folio);
121b96af1fdSMatthew Wilcox (Oracle) 		addr = folio_zero_tail(folio, 0, addr);
1221e51764aSArtem Bityutskiy 		goto out;
1231e51764aSArtem Bityutskiy 	}
1241e51764aSArtem Bityutskiy 
1251e51764aSArtem Bityutskiy 	dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
1261e51764aSArtem Bityutskiy 	if (!dn) {
1271e51764aSArtem Bityutskiy 		err = -ENOMEM;
128b96af1fdSMatthew Wilcox (Oracle) 		goto out;
1291e51764aSArtem Bityutskiy 	}
1301e51764aSArtem Bityutskiy 
1311e51764aSArtem Bityutskiy 	i = 0;
1321e51764aSArtem Bityutskiy 	while (1) {
1331e51764aSArtem Bityutskiy 		int ret;
1341e51764aSArtem Bityutskiy 
1351e51764aSArtem Bityutskiy 		if (block >= beyond) {
1361e51764aSArtem Bityutskiy 			/* Reading beyond inode */
1371e51764aSArtem Bityutskiy 			err = -ENOENT;
1381e51764aSArtem Bityutskiy 			memset(addr, 0, UBIFS_BLOCK_SIZE);
1391e51764aSArtem Bityutskiy 		} else {
1401e51764aSArtem Bityutskiy 			ret = read_block(inode, addr, block, dn);
1411e51764aSArtem Bityutskiy 			if (ret) {
1421e51764aSArtem Bityutskiy 				err = ret;
1431e51764aSArtem Bityutskiy 				if (err != -ENOENT)
1441e51764aSArtem Bityutskiy 					break;
145ed382d58SAdrian Hunter 			} else if (block + 1 == beyond) {
146ed382d58SAdrian Hunter 				int dlen = le32_to_cpu(dn->size);
147ed382d58SAdrian Hunter 				int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
148ed382d58SAdrian Hunter 
149ed382d58SAdrian Hunter 				if (ilen && ilen < dlen)
150ed382d58SAdrian Hunter 					memset(addr + ilen, 0, dlen - ilen);
1511e51764aSArtem Bityutskiy 			}
1521e51764aSArtem Bityutskiy 		}
153b96af1fdSMatthew Wilcox (Oracle) 		if (++i >= (UBIFS_BLOCKS_PER_PAGE << folio_order(folio)))
1541e51764aSArtem Bityutskiy 			break;
1551e51764aSArtem Bityutskiy 		block += 1;
1561e51764aSArtem Bityutskiy 		addr += UBIFS_BLOCK_SIZE;
157b96af1fdSMatthew Wilcox (Oracle) 		if (folio_test_highmem(folio) && (offset_in_page(addr) == 0)) {
158b96af1fdSMatthew Wilcox (Oracle) 			kunmap_local(addr - UBIFS_BLOCK_SIZE);
159b96af1fdSMatthew Wilcox (Oracle) 			addr = kmap_local_folio(folio, i * UBIFS_BLOCK_SIZE);
1601e51764aSArtem Bityutskiy 		}
161b96af1fdSMatthew Wilcox (Oracle) 	}
162b96af1fdSMatthew Wilcox (Oracle) 
1631e51764aSArtem Bityutskiy 	if (err) {
164235c362bSSheng Yong 		struct ubifs_info *c = inode->i_sb->s_fs_info;
1651e51764aSArtem Bityutskiy 		if (err == -ENOENT) {
1661e51764aSArtem Bityutskiy 			/* Not found, so it must be a hole */
167b96af1fdSMatthew Wilcox (Oracle) 			folio_set_checked(folio);
1681e51764aSArtem Bityutskiy 			dbg_gen("hole");
169b96af1fdSMatthew Wilcox (Oracle) 			err = 0;
170b96af1fdSMatthew Wilcox (Oracle) 		} else {
171235c362bSSheng Yong 			ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
172b96af1fdSMatthew Wilcox (Oracle) 				  folio->index, inode->i_ino, err);
173b96af1fdSMatthew Wilcox (Oracle) 		}
1741e51764aSArtem Bityutskiy 	}
1751e51764aSArtem Bityutskiy 
1761e51764aSArtem Bityutskiy out:
1771e51764aSArtem Bityutskiy 	kfree(dn);
178b96af1fdSMatthew Wilcox (Oracle) 	if (!err)
179b96af1fdSMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
180b96af1fdSMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
181b96af1fdSMatthew Wilcox (Oracle) 	kunmap_local(addr);
1821e51764aSArtem Bityutskiy 	return err;
1831e51764aSArtem Bityutskiy }
1841e51764aSArtem Bityutskiy 
1851e51764aSArtem Bityutskiy /**
1861e51764aSArtem Bityutskiy  * release_new_page_budget - release budget of a new page.
1871e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1881e51764aSArtem Bityutskiy  *
1891e51764aSArtem Bityutskiy  * This is a helper function which releases budget corresponding to the budget
1901e51764aSArtem Bityutskiy  * of one new page of data.
1911e51764aSArtem Bityutskiy  */
release_new_page_budget(struct ubifs_info * c)1921e51764aSArtem Bityutskiy static void release_new_page_budget(struct ubifs_info *c)
1931e51764aSArtem Bityutskiy {
1941e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .recalculate = 1, .new_page = 1 };
1951e51764aSArtem Bityutskiy 
1961e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
1971e51764aSArtem Bityutskiy }
1981e51764aSArtem Bityutskiy 
1991e51764aSArtem Bityutskiy /**
2001e51764aSArtem Bityutskiy  * release_existing_page_budget - release budget of an existing page.
2011e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2021e51764aSArtem Bityutskiy  *
2031e51764aSArtem Bityutskiy  * This is a helper function which releases budget corresponding to the budget
204b8f1da98SRandy Dunlap  * of changing one page of data which already exists on the flash media.
2051e51764aSArtem Bityutskiy  */
release_existing_page_budget(struct ubifs_info * c)2061e51764aSArtem Bityutskiy static void release_existing_page_budget(struct ubifs_info *c)
2071e51764aSArtem Bityutskiy {
208b137545cSArtem Bityutskiy 	struct ubifs_budget_req req = { .dd_growth = c->bi.page_budget};
2091e51764aSArtem Bityutskiy 
2101e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
2111e51764aSArtem Bityutskiy }
2121e51764aSArtem Bityutskiy 
write_begin_slow(struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep)2131e51764aSArtem Bityutskiy static int write_begin_slow(struct address_space *mapping,
2149d6b0cd7SMatthew Wilcox (Oracle) 			    loff_t pos, unsigned len, struct page **pagep)
2151e51764aSArtem Bityutskiy {
2161e51764aSArtem Bityutskiy 	struct inode *inode = mapping->host;
2171e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
21809cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
2191e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .new_page = 1 };
2203f649ab7SKees Cook 	int err, appending = !!(pos + len > inode->i_size);
2212ec71843SMatthew Wilcox (Oracle) 	struct folio *folio;
2221e51764aSArtem Bityutskiy 
2231e51764aSArtem Bityutskiy 	dbg_gen("ino %lu, pos %llu, len %u, i_size %lld",
2241e51764aSArtem Bityutskiy 		inode->i_ino, pos, len, inode->i_size);
2251e51764aSArtem Bityutskiy 
2261e51764aSArtem Bityutskiy 	/*
2272ec71843SMatthew Wilcox (Oracle) 	 * At the slow path we have to budget before locking the folio, because
2282ec71843SMatthew Wilcox (Oracle) 	 * budgeting may force write-back, which would wait on locked folios and
2292ec71843SMatthew Wilcox (Oracle) 	 * deadlock if we had the folio locked. At this point we do not know
2302ec71843SMatthew Wilcox (Oracle) 	 * anything about the folio, so assume that this is a new folio which is
2311e51764aSArtem Bityutskiy 	 * written to a hole. This corresponds to largest budget. Later the
2321e51764aSArtem Bityutskiy 	 * budget will be amended if this is not true.
2331e51764aSArtem Bityutskiy 	 */
2341e51764aSArtem Bityutskiy 	if (appending)
2351e51764aSArtem Bityutskiy 		/* We are appending data, budget for inode change */
2361e51764aSArtem Bityutskiy 		req.dirtied_ino = 1;
2371e51764aSArtem Bityutskiy 
2381e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
2391e51764aSArtem Bityutskiy 	if (unlikely(err))
2401e51764aSArtem Bityutskiy 		return err;
2411e51764aSArtem Bityutskiy 
2422ec71843SMatthew Wilcox (Oracle) 	folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
2432ec71843SMatthew Wilcox (Oracle) 			mapping_gfp_mask(mapping));
2442ec71843SMatthew Wilcox (Oracle) 	if (IS_ERR(folio)) {
2451e51764aSArtem Bityutskiy 		ubifs_release_budget(c, &req);
2462ec71843SMatthew Wilcox (Oracle) 		return PTR_ERR(folio);
2471e51764aSArtem Bityutskiy 	}
2481e51764aSArtem Bityutskiy 
2492ec71843SMatthew Wilcox (Oracle) 	if (!folio_test_uptodate(folio)) {
2502ec71843SMatthew Wilcox (Oracle) 		if (pos == folio_pos(folio) && len >= folio_size(folio))
2512ec71843SMatthew Wilcox (Oracle) 			folio_set_checked(folio);
2521e51764aSArtem Bityutskiy 		else {
253b96af1fdSMatthew Wilcox (Oracle) 			err = do_readpage(folio);
2541e51764aSArtem Bityutskiy 			if (err) {
2552ec71843SMatthew Wilcox (Oracle) 				folio_unlock(folio);
2562ec71843SMatthew Wilcox (Oracle) 				folio_put(folio);
257789c8993SArtem Bityutskiy 				ubifs_release_budget(c, &req);
2581e51764aSArtem Bityutskiy 				return err;
2591e51764aSArtem Bityutskiy 			}
2601e51764aSArtem Bityutskiy 		}
2611e51764aSArtem Bityutskiy 	}
2621e51764aSArtem Bityutskiy 
2632ec71843SMatthew Wilcox (Oracle) 	if (folio->private)
2641e51764aSArtem Bityutskiy 		/*
2652ec71843SMatthew Wilcox (Oracle) 		 * The folio is dirty, which means it was budgeted twice:
2661e51764aSArtem Bityutskiy 		 *   o first time the budget was allocated by the task which
2672ec71843SMatthew Wilcox (Oracle) 		 *     made the folio dirty and set the private field;
2681e51764aSArtem Bityutskiy 		 *   o and then we budgeted for it for the second time at the
2691e51764aSArtem Bityutskiy 		 *     very beginning of this function.
2701e51764aSArtem Bityutskiy 		 *
2712ec71843SMatthew Wilcox (Oracle) 		 * So what we have to do is to release the folio budget we
2721e51764aSArtem Bityutskiy 		 * allocated.
2731e51764aSArtem Bityutskiy 		 */
2741e51764aSArtem Bityutskiy 		release_new_page_budget(c);
2752ec71843SMatthew Wilcox (Oracle) 	else if (!folio_test_checked(folio))
2761e51764aSArtem Bityutskiy 		/*
2772ec71843SMatthew Wilcox (Oracle) 		 * We are changing a folio which already exists on the media.
2782ec71843SMatthew Wilcox (Oracle) 		 * This means that changing the folio does not make the amount
2791e51764aSArtem Bityutskiy 		 * of indexing information larger, and this part of the budget
2801e51764aSArtem Bityutskiy 		 * which we have already acquired may be released.
2811e51764aSArtem Bityutskiy 		 */
2821e51764aSArtem Bityutskiy 		ubifs_convert_page_budget(c);
2831e51764aSArtem Bityutskiy 
2841e51764aSArtem Bityutskiy 	if (appending) {
2851e51764aSArtem Bityutskiy 		struct ubifs_inode *ui = ubifs_inode(inode);
2861e51764aSArtem Bityutskiy 
2871e51764aSArtem Bityutskiy 		/*
2881e51764aSArtem Bityutskiy 		 * 'ubifs_write_end()' is optimized from the fast-path part of
2891e51764aSArtem Bityutskiy 		 * 'ubifs_write_begin()' and expects the @ui_mutex to be locked
2901e51764aSArtem Bityutskiy 		 * if data is appended.
2911e51764aSArtem Bityutskiy 		 */
2921e51764aSArtem Bityutskiy 		mutex_lock(&ui->ui_mutex);
2931e51764aSArtem Bityutskiy 		if (ui->dirty)
2941e51764aSArtem Bityutskiy 			/*
2951e51764aSArtem Bityutskiy 			 * The inode is dirty already, so we may free the
2961e51764aSArtem Bityutskiy 			 * budget we allocated.
2971e51764aSArtem Bityutskiy 			 */
2981e51764aSArtem Bityutskiy 			ubifs_release_dirty_inode_budget(c, ui);
2991e51764aSArtem Bityutskiy 	}
3001e51764aSArtem Bityutskiy 
3012ec71843SMatthew Wilcox (Oracle) 	*pagep = &folio->page;
3021e51764aSArtem Bityutskiy 	return 0;
3031e51764aSArtem Bityutskiy }
3041e51764aSArtem Bityutskiy 
3051e51764aSArtem Bityutskiy /**
3061e51764aSArtem Bityutskiy  * allocate_budget - allocate budget for 'ubifs_write_begin()'.
3071e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
308a3c2f196SMatthew Wilcox (Oracle)  * @folio: folio to allocate budget for
3091e51764aSArtem Bityutskiy  * @ui: UBIFS inode object the page belongs to
3101e51764aSArtem Bityutskiy  * @appending: non-zero if the page is appended
3111e51764aSArtem Bityutskiy  *
3121e51764aSArtem Bityutskiy  * This is a helper function for 'ubifs_write_begin()' which allocates budget
3131e51764aSArtem Bityutskiy  * for the operation. The budget is allocated differently depending on whether
3141e51764aSArtem Bityutskiy  * this is appending, whether the page is dirty or not, and so on. This
315ac8e9f64SRandy Dunlap  * function leaves the @ui->ui_mutex locked in case of appending.
316ac8e9f64SRandy Dunlap  *
317ac8e9f64SRandy Dunlap  * Returns: %0 in case of success and %-ENOSPC in case of failure.
3181e51764aSArtem Bityutskiy  */
allocate_budget(struct ubifs_info * c,struct folio * folio,struct ubifs_inode * ui,int appending)319a3c2f196SMatthew Wilcox (Oracle) static int allocate_budget(struct ubifs_info *c, struct folio *folio,
3201e51764aSArtem Bityutskiy 			   struct ubifs_inode *ui, int appending)
3211e51764aSArtem Bityutskiy {
3221e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .fast = 1 };
3231e51764aSArtem Bityutskiy 
324a3c2f196SMatthew Wilcox (Oracle) 	if (folio->private) {
3251e51764aSArtem Bityutskiy 		if (!appending)
3261e51764aSArtem Bityutskiy 			/*
327a3c2f196SMatthew Wilcox (Oracle) 			 * The folio is dirty and we are not appending, which
3281e51764aSArtem Bityutskiy 			 * means no budget is needed at all.
3291e51764aSArtem Bityutskiy 			 */
3301e51764aSArtem Bityutskiy 			return 0;
3311e51764aSArtem Bityutskiy 
3321e51764aSArtem Bityutskiy 		mutex_lock(&ui->ui_mutex);
3331e51764aSArtem Bityutskiy 		if (ui->dirty)
3341e51764aSArtem Bityutskiy 			/*
3351e51764aSArtem Bityutskiy 			 * The page is dirty and we are appending, so the inode
3361e51764aSArtem Bityutskiy 			 * has to be marked as dirty. However, it is already
3371e51764aSArtem Bityutskiy 			 * dirty, so we do not need any budget. We may return,
3381e51764aSArtem Bityutskiy 			 * but @ui->ui_mutex hast to be left locked because we
3391e51764aSArtem Bityutskiy 			 * should prevent write-back from flushing the inode
3401e51764aSArtem Bityutskiy 			 * and freeing the budget. The lock will be released in
3411e51764aSArtem Bityutskiy 			 * 'ubifs_write_end()'.
3421e51764aSArtem Bityutskiy 			 */
3431e51764aSArtem Bityutskiy 			return 0;
3441e51764aSArtem Bityutskiy 
3451e51764aSArtem Bityutskiy 		/*
3461e51764aSArtem Bityutskiy 		 * The page is dirty, we are appending, the inode is clean, so
3471e51764aSArtem Bityutskiy 		 * we need to budget the inode change.
3481e51764aSArtem Bityutskiy 		 */
3491e51764aSArtem Bityutskiy 		req.dirtied_ino = 1;
3501e51764aSArtem Bityutskiy 	} else {
351a3c2f196SMatthew Wilcox (Oracle) 		if (folio_test_checked(folio))
3521e51764aSArtem Bityutskiy 			/*
3531e51764aSArtem Bityutskiy 			 * The page corresponds to a hole and does not
3541e51764aSArtem Bityutskiy 			 * exist on the media. So changing it makes
355a3c2f196SMatthew Wilcox (Oracle) 			 * the amount of indexing information
3561e51764aSArtem Bityutskiy 			 * larger, and we have to budget for a new
3571e51764aSArtem Bityutskiy 			 * page.
3581e51764aSArtem Bityutskiy 			 */
3591e51764aSArtem Bityutskiy 			req.new_page = 1;
3601e51764aSArtem Bityutskiy 		else
3611e51764aSArtem Bityutskiy 			/*
3621e51764aSArtem Bityutskiy 			 * Not a hole, the change will not add any new
3631e51764aSArtem Bityutskiy 			 * indexing information, budget for page
3641e51764aSArtem Bityutskiy 			 * change.
3651e51764aSArtem Bityutskiy 			 */
3661e51764aSArtem Bityutskiy 			req.dirtied_page = 1;
3671e51764aSArtem Bityutskiy 
3681e51764aSArtem Bityutskiy 		if (appending) {
3691e51764aSArtem Bityutskiy 			mutex_lock(&ui->ui_mutex);
3701e51764aSArtem Bityutskiy 			if (!ui->dirty)
3711e51764aSArtem Bityutskiy 				/*
3721e51764aSArtem Bityutskiy 				 * The inode is clean but we will have to mark
3731e51764aSArtem Bityutskiy 				 * it as dirty because we are appending. This
3741e51764aSArtem Bityutskiy 				 * needs a budget.
3751e51764aSArtem Bityutskiy 				 */
3761e51764aSArtem Bityutskiy 				req.dirtied_ino = 1;
3771e51764aSArtem Bityutskiy 		}
3781e51764aSArtem Bityutskiy 	}
3791e51764aSArtem Bityutskiy 
3801e51764aSArtem Bityutskiy 	return ubifs_budget_space(c, &req);
3811e51764aSArtem Bityutskiy }
3821e51764aSArtem Bityutskiy 
3831e51764aSArtem Bityutskiy /*
3841e51764aSArtem Bityutskiy  * This function is called when a page of data is going to be written. Since
3851e51764aSArtem Bityutskiy  * the page of data will not necessarily go to the flash straight away, UBIFS
3861e51764aSArtem Bityutskiy  * has to reserve space on the media for it, which is done by means of
3871e51764aSArtem Bityutskiy  * budgeting.
3881e51764aSArtem Bityutskiy  *
3891e51764aSArtem Bityutskiy  * This is the hot-path of the file-system and we are trying to optimize it as
3901e51764aSArtem Bityutskiy  * much as possible. For this reasons it is split on 2 parts - slow and fast.
3911e51764aSArtem Bityutskiy  *
3921e51764aSArtem Bityutskiy  * There many budgeting cases:
3931e51764aSArtem Bityutskiy  *     o a new page is appended - we have to budget for a new page and for
3941e51764aSArtem Bityutskiy  *       changing the inode; however, if the inode is already dirty, there is
3951e51764aSArtem Bityutskiy  *       no need to budget for it;
3961e51764aSArtem Bityutskiy  *     o an existing clean page is changed - we have budget for it; if the page
3971e51764aSArtem Bityutskiy  *       does not exist on the media (a hole), we have to budget for a new
3981e51764aSArtem Bityutskiy  *       page; otherwise, we may budget for changing an existing page; the
3991e51764aSArtem Bityutskiy  *       difference between these cases is that changing an existing page does
4001e51764aSArtem Bityutskiy  *       not introduce anything new to the FS indexing information, so it does
4011e51764aSArtem Bityutskiy  *       not grow, and smaller budget is acquired in this case;
4021e51764aSArtem Bityutskiy  *     o an existing dirty page is changed - no need to budget at all, because
4031e51764aSArtem Bityutskiy  *       the page budget has been acquired by earlier, when the page has been
4041e51764aSArtem Bityutskiy  *       marked dirty.
4051e51764aSArtem Bityutskiy  *
4061e51764aSArtem Bityutskiy  * UBIFS budgeting sub-system may force write-back if it thinks there is no
4071e51764aSArtem Bityutskiy  * space to reserve. This imposes some locking restrictions and makes it
4081e51764aSArtem Bityutskiy  * impossible to take into account the above cases, and makes it impossible to
4091e51764aSArtem Bityutskiy  * optimize budgeting.
4101e51764aSArtem Bityutskiy  *
4111e51764aSArtem Bityutskiy  * The solution for this is that the fast path of 'ubifs_write_begin()' assumes
4121e51764aSArtem Bityutskiy  * there is a plenty of flash space and the budget will be acquired quickly,
4131e51764aSArtem Bityutskiy  * without forcing write-back. The slow path does not make this assumption.
4141e51764aSArtem Bityutskiy  */
ubifs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)4151e51764aSArtem Bityutskiy static int ubifs_write_begin(struct file *file, struct address_space *mapping,
4169d6b0cd7SMatthew Wilcox (Oracle) 			     loff_t pos, unsigned len,
4171e51764aSArtem Bityutskiy 			     struct page **pagep, void **fsdata)
4181e51764aSArtem Bityutskiy {
4191e51764aSArtem Bityutskiy 	struct inode *inode = mapping->host;
4201e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
4211e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
42209cbfeafSKirill A. Shutemov 	pgoff_t index = pos >> PAGE_SHIFT;
4233f649ab7SKees Cook 	int err, appending = !!(pos + len > inode->i_size);
424f55aa591SAdrian Hunter 	int skipped_read = 0;
425f60d356eSMatthew Wilcox (Oracle) 	struct folio *folio;
4261e51764aSArtem Bityutskiy 
4276eb61d58SRichard Weinberger 	ubifs_assert(c, ubifs_inode(inode)->ui_size == inode->i_size);
4286eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
4291e51764aSArtem Bityutskiy 
4302680d722SArtem Bityutskiy 	if (unlikely(c->ro_error))
4311e51764aSArtem Bityutskiy 		return -EROFS;
4321e51764aSArtem Bityutskiy 
4331e51764aSArtem Bityutskiy 	/* Try out the fast-path part first */
434f60d356eSMatthew Wilcox (Oracle) 	folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
435f60d356eSMatthew Wilcox (Oracle) 			mapping_gfp_mask(mapping));
436f60d356eSMatthew Wilcox (Oracle) 	if (IS_ERR(folio))
437f60d356eSMatthew Wilcox (Oracle) 		return PTR_ERR(folio);
4381e51764aSArtem Bityutskiy 
439f60d356eSMatthew Wilcox (Oracle) 	if (!folio_test_uptodate(folio)) {
4401e51764aSArtem Bityutskiy 		/* The page is not loaded from the flash */
441f60d356eSMatthew Wilcox (Oracle) 		if (pos == folio_pos(folio) && len >= folio_size(folio)) {
4421e51764aSArtem Bityutskiy 			/*
4431e51764aSArtem Bityutskiy 			 * We change whole page so no need to load it. But we
4446ed09c34SArtem Bityutskiy 			 * do not know whether this page exists on the media or
4456ed09c34SArtem Bityutskiy 			 * not, so we assume the latter because it requires
4466ed09c34SArtem Bityutskiy 			 * larger budget. The assumption is that it is better
4476ed09c34SArtem Bityutskiy 			 * to budget a bit more than to read the page from the
4486ed09c34SArtem Bityutskiy 			 * media. Thus, we are setting the @PG_checked flag
4496ed09c34SArtem Bityutskiy 			 * here.
4501e51764aSArtem Bityutskiy 			 */
451f60d356eSMatthew Wilcox (Oracle) 			folio_set_checked(folio);
452f55aa591SAdrian Hunter 			skipped_read = 1;
453f55aa591SAdrian Hunter 		} else {
454b96af1fdSMatthew Wilcox (Oracle) 			err = do_readpage(folio);
4551e51764aSArtem Bityutskiy 			if (err) {
456f60d356eSMatthew Wilcox (Oracle) 				folio_unlock(folio);
457f60d356eSMatthew Wilcox (Oracle) 				folio_put(folio);
4581e51764aSArtem Bityutskiy 				return err;
4591e51764aSArtem Bityutskiy 			}
4601e51764aSArtem Bityutskiy 		}
4611e51764aSArtem Bityutskiy 	}
4621e51764aSArtem Bityutskiy 
463a3c2f196SMatthew Wilcox (Oracle) 	err = allocate_budget(c, folio, ui, appending);
4641e51764aSArtem Bityutskiy 	if (unlikely(err)) {
4656eb61d58SRichard Weinberger 		ubifs_assert(c, err == -ENOSPC);
4661e51764aSArtem Bityutskiy 		/*
467f55aa591SAdrian Hunter 		 * If we skipped reading the page because we were going to
468f55aa591SAdrian Hunter 		 * write all of it, then it is not up to date.
469f55aa591SAdrian Hunter 		 */
470723012caSMatthew Wilcox (Oracle) 		if (skipped_read)
471f60d356eSMatthew Wilcox (Oracle) 			folio_clear_checked(folio);
472f55aa591SAdrian Hunter 		/*
4731e51764aSArtem Bityutskiy 		 * Budgeting failed which means it would have to force
4741e51764aSArtem Bityutskiy 		 * write-back but didn't, because we set the @fast flag in the
4751e51764aSArtem Bityutskiy 		 * request. Write-back cannot be done now, while we have the
4761e51764aSArtem Bityutskiy 		 * page locked, because it would deadlock. Unlock and free
4771e51764aSArtem Bityutskiy 		 * everything and fall-back to slow-path.
4781e51764aSArtem Bityutskiy 		 */
4791e51764aSArtem Bityutskiy 		if (appending) {
4806eb61d58SRichard Weinberger 			ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
4811e51764aSArtem Bityutskiy 			mutex_unlock(&ui->ui_mutex);
4821e51764aSArtem Bityutskiy 		}
483f60d356eSMatthew Wilcox (Oracle) 		folio_unlock(folio);
484f60d356eSMatthew Wilcox (Oracle) 		folio_put(folio);
4851e51764aSArtem Bityutskiy 
4869d6b0cd7SMatthew Wilcox (Oracle) 		return write_begin_slow(mapping, pos, len, pagep);
4871e51764aSArtem Bityutskiy 	}
4881e51764aSArtem Bityutskiy 
4891e51764aSArtem Bityutskiy 	/*
490873a64c7SArtem Bityutskiy 	 * Whee, we acquired budgeting quickly - without involving
491873a64c7SArtem Bityutskiy 	 * garbage-collection, committing or forcing write-back. We return
4921e51764aSArtem Bityutskiy 	 * with @ui->ui_mutex locked if we are appending pages, and unlocked
4931e51764aSArtem Bityutskiy 	 * otherwise. This is an optimization (slightly hacky though).
4941e51764aSArtem Bityutskiy 	 */
495f60d356eSMatthew Wilcox (Oracle) 	*pagep = &folio->page;
4961e51764aSArtem Bityutskiy 	return 0;
4971e51764aSArtem Bityutskiy }
4981e51764aSArtem Bityutskiy 
4991e51764aSArtem Bityutskiy /**
5001e51764aSArtem Bityutskiy  * cancel_budget - cancel budget.
5011e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
50245d76698SMatthew Wilcox (Oracle)  * @folio: folio to cancel budget for
5031e51764aSArtem Bityutskiy  * @ui: UBIFS inode object the page belongs to
5041e51764aSArtem Bityutskiy  * @appending: non-zero if the page is appended
5051e51764aSArtem Bityutskiy  *
5061e51764aSArtem Bityutskiy  * This is a helper function for a page write operation. It unlocks the
5071e51764aSArtem Bityutskiy  * @ui->ui_mutex in case of appending.
5081e51764aSArtem Bityutskiy  */
cancel_budget(struct ubifs_info * c,struct folio * folio,struct ubifs_inode * ui,int appending)50945d76698SMatthew Wilcox (Oracle) static void cancel_budget(struct ubifs_info *c, struct folio *folio,
5101e51764aSArtem Bityutskiy 			  struct ubifs_inode *ui, int appending)
5111e51764aSArtem Bityutskiy {
5121e51764aSArtem Bityutskiy 	if (appending) {
5131e51764aSArtem Bityutskiy 		if (!ui->dirty)
5141e51764aSArtem Bityutskiy 			ubifs_release_dirty_inode_budget(c, ui);
5151e51764aSArtem Bityutskiy 		mutex_unlock(&ui->ui_mutex);
5161e51764aSArtem Bityutskiy 	}
51745d76698SMatthew Wilcox (Oracle) 	if (!folio->private) {
51845d76698SMatthew Wilcox (Oracle) 		if (folio_test_checked(folio))
5191e51764aSArtem Bityutskiy 			release_new_page_budget(c);
5201e51764aSArtem Bityutskiy 		else
5211e51764aSArtem Bityutskiy 			release_existing_page_budget(c);
5221e51764aSArtem Bityutskiy 	}
5231e51764aSArtem Bityutskiy }
5241e51764aSArtem Bityutskiy 
ubifs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)5251e51764aSArtem Bityutskiy static int ubifs_write_end(struct file *file, struct address_space *mapping,
5261e51764aSArtem Bityutskiy 			   loff_t pos, unsigned len, unsigned copied,
5271e51764aSArtem Bityutskiy 			   struct page *page, void *fsdata)
5281e51764aSArtem Bityutskiy {
529ffdff813SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
5301e51764aSArtem Bityutskiy 	struct inode *inode = mapping->host;
5311e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
5321e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
5331e51764aSArtem Bityutskiy 	loff_t end_pos = pos + len;
5341e51764aSArtem Bityutskiy 	int appending = !!(end_pos > inode->i_size);
5351e51764aSArtem Bityutskiy 
5361e51764aSArtem Bityutskiy 	dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
537ffdff813SMatthew Wilcox (Oracle) 		inode->i_ino, pos, folio->index, len, copied, inode->i_size);
5381e51764aSArtem Bityutskiy 
539ffdff813SMatthew Wilcox (Oracle) 	if (unlikely(copied < len && !folio_test_uptodate(folio))) {
5401e51764aSArtem Bityutskiy 		/*
541ffdff813SMatthew Wilcox (Oracle) 		 * VFS copied less data to the folio than it intended and
5421e51764aSArtem Bityutskiy 		 * declared in its '->write_begin()' call via the @len
543ffdff813SMatthew Wilcox (Oracle) 		 * argument. If the folio was not up-to-date,
544ffdff813SMatthew Wilcox (Oracle) 		 * the 'ubifs_write_begin()' function did
5451e51764aSArtem Bityutskiy 		 * not load it from the media (for optimization reasons). This
546ffdff813SMatthew Wilcox (Oracle) 		 * means that part of the folio contains garbage. So read the
547ffdff813SMatthew Wilcox (Oracle) 		 * folio now.
5481e51764aSArtem Bityutskiy 		 */
5491e51764aSArtem Bityutskiy 		dbg_gen("copied %d instead of %d, read page and repeat",
5501e51764aSArtem Bityutskiy 			copied, len);
55145d76698SMatthew Wilcox (Oracle) 		cancel_budget(c, folio, ui, appending);
552ffdff813SMatthew Wilcox (Oracle) 		folio_clear_checked(folio);
5531e51764aSArtem Bityutskiy 
5541e51764aSArtem Bityutskiy 		/*
5551e51764aSArtem Bityutskiy 		 * Return 0 to force VFS to repeat the whole operation, or the
556873a64c7SArtem Bityutskiy 		 * error code if 'do_readpage()' fails.
5571e51764aSArtem Bityutskiy 		 */
558b96af1fdSMatthew Wilcox (Oracle) 		copied = do_readpage(folio);
5591e51764aSArtem Bityutskiy 		goto out;
5601e51764aSArtem Bityutskiy 	}
5611e51764aSArtem Bityutskiy 
562ffdff813SMatthew Wilcox (Oracle) 	if (len == folio_size(folio))
563ffdff813SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
564723012caSMatthew Wilcox (Oracle) 
565ffdff813SMatthew Wilcox (Oracle) 	if (!folio->private) {
566ffdff813SMatthew Wilcox (Oracle) 		folio_attach_private(folio, (void *)1);
5671e51764aSArtem Bityutskiy 		atomic_long_inc(&c->dirty_pg_cnt);
568ffdff813SMatthew Wilcox (Oracle) 		filemap_dirty_folio(mapping, folio);
5691e51764aSArtem Bityutskiy 	}
5701e51764aSArtem Bityutskiy 
5711e51764aSArtem Bityutskiy 	if (appending) {
5721e51764aSArtem Bityutskiy 		i_size_write(inode, end_pos);
5731e51764aSArtem Bityutskiy 		ui->ui_size = end_pos;
5741e51764aSArtem Bityutskiy 		/*
575ffdff813SMatthew Wilcox (Oracle) 		 * We do not set @I_DIRTY_PAGES (which means that
576ffdff813SMatthew Wilcox (Oracle) 		 * the inode has dirty pages), this was done in
577ffdff813SMatthew Wilcox (Oracle) 		 * filemap_dirty_folio().
5781e51764aSArtem Bityutskiy 		 */
5791e51764aSArtem Bityutskiy 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
5806eb61d58SRichard Weinberger 		ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
5811e51764aSArtem Bityutskiy 		mutex_unlock(&ui->ui_mutex);
5821e51764aSArtem Bityutskiy 	}
5831e51764aSArtem Bityutskiy 
5841e51764aSArtem Bityutskiy out:
585ffdff813SMatthew Wilcox (Oracle) 	folio_unlock(folio);
586ffdff813SMatthew Wilcox (Oracle) 	folio_put(folio);
5871e51764aSArtem Bityutskiy 	return copied;
5881e51764aSArtem Bityutskiy }
5891e51764aSArtem Bityutskiy 
5904793e7c5SAdrian Hunter /**
5914793e7c5SAdrian Hunter  * populate_page - copy data nodes into a page for bulk-read.
5924793e7c5SAdrian Hunter  * @c: UBIFS file-system description object
593a16bfab3SMatthew Wilcox (Oracle)  * @folio: folio
5944793e7c5SAdrian Hunter  * @bu: bulk-read information
5954793e7c5SAdrian Hunter  * @n: next zbranch slot
5964793e7c5SAdrian Hunter  *
597ac8e9f64SRandy Dunlap  * Returns: %0 on success and a negative error code on failure.
5984793e7c5SAdrian Hunter  */
populate_page(struct ubifs_info * c,struct folio * folio,struct bu_info * bu,int * n)599a16bfab3SMatthew Wilcox (Oracle) static int populate_page(struct ubifs_info *c, struct folio *folio,
6004793e7c5SAdrian Hunter 			 struct bu_info *bu, int *n)
6014793e7c5SAdrian Hunter {
6025c0013c1SAdrian Hunter 	int i = 0, nn = *n, offs = bu->zbranch[0].offs, hole = 0, read = 0;
603a16bfab3SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
6044793e7c5SAdrian Hunter 	loff_t i_size = i_size_read(inode);
6054793e7c5SAdrian Hunter 	unsigned int page_block;
6064793e7c5SAdrian Hunter 	void *addr, *zaddr;
6074793e7c5SAdrian Hunter 	pgoff_t end_index;
6084793e7c5SAdrian Hunter 
6094793e7c5SAdrian Hunter 	dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
610a16bfab3SMatthew Wilcox (Oracle) 		inode->i_ino, folio->index, i_size, folio->flags);
6114793e7c5SAdrian Hunter 
612a16bfab3SMatthew Wilcox (Oracle) 	addr = zaddr = kmap_local_folio(folio, 0);
6134793e7c5SAdrian Hunter 
61409cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
615a16bfab3SMatthew Wilcox (Oracle) 	if (!i_size || folio->index > end_index) {
6165c0013c1SAdrian Hunter 		hole = 1;
617a16bfab3SMatthew Wilcox (Oracle) 		addr = folio_zero_tail(folio, 0, addr);
6184793e7c5SAdrian Hunter 		goto out_hole;
6194793e7c5SAdrian Hunter 	}
6204793e7c5SAdrian Hunter 
621a16bfab3SMatthew Wilcox (Oracle) 	page_block = folio->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
6224793e7c5SAdrian Hunter 	while (1) {
6234793e7c5SAdrian Hunter 		int err, len, out_len, dlen;
6244793e7c5SAdrian Hunter 
6255c0013c1SAdrian Hunter 		if (nn >= bu->cnt) {
6265c0013c1SAdrian Hunter 			hole = 1;
6274793e7c5SAdrian Hunter 			memset(addr, 0, UBIFS_BLOCK_SIZE);
6285c0013c1SAdrian Hunter 		} else if (key_block(c, &bu->zbranch[nn].key) == page_block) {
6294793e7c5SAdrian Hunter 			struct ubifs_data_node *dn;
6304793e7c5SAdrian Hunter 
6314793e7c5SAdrian Hunter 			dn = bu->buf + (bu->zbranch[nn].offs - offs);
6324793e7c5SAdrian Hunter 
6336eb61d58SRichard Weinberger 			ubifs_assert(c, le64_to_cpu(dn->ch.sqnum) >
6344793e7c5SAdrian Hunter 				     ubifs_inode(inode)->creat_sqnum);
6354793e7c5SAdrian Hunter 
6364793e7c5SAdrian Hunter 			len = le32_to_cpu(dn->size);
6374793e7c5SAdrian Hunter 			if (len <= 0 || len > UBIFS_BLOCK_SIZE)
6384793e7c5SAdrian Hunter 				goto out_err;
6394793e7c5SAdrian Hunter 
6404793e7c5SAdrian Hunter 			dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
6414793e7c5SAdrian Hunter 			out_len = UBIFS_BLOCK_SIZE;
6427799953bSRichard Weinberger 
64350d9fad7SEric Biggers 			if (IS_ENCRYPTED(inode)) {
6447799953bSRichard Weinberger 				err = ubifs_decrypt(inode, dn, &dlen, page_block);
6457799953bSRichard Weinberger 				if (err)
6467799953bSRichard Weinberger 					goto out_err;
6477799953bSRichard Weinberger 			}
6487799953bSRichard Weinberger 
649235c362bSSheng Yong 			err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
6504793e7c5SAdrian Hunter 					       le16_to_cpu(dn->compr_type));
6514793e7c5SAdrian Hunter 			if (err || len != out_len)
6524793e7c5SAdrian Hunter 				goto out_err;
6534793e7c5SAdrian Hunter 
6544793e7c5SAdrian Hunter 			if (len < UBIFS_BLOCK_SIZE)
6554793e7c5SAdrian Hunter 				memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
6564793e7c5SAdrian Hunter 
6574793e7c5SAdrian Hunter 			nn += 1;
6584793e7c5SAdrian Hunter 			read = (i << UBIFS_BLOCK_SHIFT) + len;
6595c0013c1SAdrian Hunter 		} else if (key_block(c, &bu->zbranch[nn].key) < page_block) {
6605c0013c1SAdrian Hunter 			nn += 1;
6615c0013c1SAdrian Hunter 			continue;
6625c0013c1SAdrian Hunter 		} else {
6635c0013c1SAdrian Hunter 			hole = 1;
6645c0013c1SAdrian Hunter 			memset(addr, 0, UBIFS_BLOCK_SIZE);
6654793e7c5SAdrian Hunter 		}
6664793e7c5SAdrian Hunter 		if (++i >= UBIFS_BLOCKS_PER_PAGE)
6674793e7c5SAdrian Hunter 			break;
6684793e7c5SAdrian Hunter 		addr += UBIFS_BLOCK_SIZE;
6694793e7c5SAdrian Hunter 		page_block += 1;
670a16bfab3SMatthew Wilcox (Oracle) 		if (folio_test_highmem(folio) && (offset_in_page(addr) == 0)) {
671a16bfab3SMatthew Wilcox (Oracle) 			kunmap_local(addr - UBIFS_BLOCK_SIZE);
672a16bfab3SMatthew Wilcox (Oracle) 			addr = kmap_local_folio(folio, i * UBIFS_BLOCK_SIZE);
673a16bfab3SMatthew Wilcox (Oracle) 		}
6744793e7c5SAdrian Hunter 	}
6754793e7c5SAdrian Hunter 
676a16bfab3SMatthew Wilcox (Oracle) 	if (end_index == folio->index) {
67709cbfeafSKirill A. Shutemov 		int len = i_size & (PAGE_SIZE - 1);
6784793e7c5SAdrian Hunter 
679ed382d58SAdrian Hunter 		if (len && len < read)
6804793e7c5SAdrian Hunter 			memset(zaddr + len, 0, read - len);
6814793e7c5SAdrian Hunter 	}
6824793e7c5SAdrian Hunter 
6834793e7c5SAdrian Hunter out_hole:
6844793e7c5SAdrian Hunter 	if (hole) {
685a16bfab3SMatthew Wilcox (Oracle) 		folio_set_checked(folio);
6864793e7c5SAdrian Hunter 		dbg_gen("hole");
6874793e7c5SAdrian Hunter 	}
6884793e7c5SAdrian Hunter 
689a16bfab3SMatthew Wilcox (Oracle) 	folio_mark_uptodate(folio);
690a16bfab3SMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
691a16bfab3SMatthew Wilcox (Oracle) 	kunmap_local(addr);
6924793e7c5SAdrian Hunter 	*n = nn;
6934793e7c5SAdrian Hunter 	return 0;
6944793e7c5SAdrian Hunter 
6954793e7c5SAdrian Hunter out_err:
696a16bfab3SMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
697a16bfab3SMatthew Wilcox (Oracle) 	kunmap_local(addr);
698235c362bSSheng Yong 	ubifs_err(c, "bad data node (block %u, inode %lu)",
6994793e7c5SAdrian Hunter 		  page_block, inode->i_ino);
7004793e7c5SAdrian Hunter 	return -EINVAL;
7014793e7c5SAdrian Hunter }
7024793e7c5SAdrian Hunter 
7034793e7c5SAdrian Hunter /**
7044793e7c5SAdrian Hunter  * ubifs_do_bulk_read - do bulk-read.
7054793e7c5SAdrian Hunter  * @c: UBIFS file-system description object
7066c0c42cdSArtem Bityutskiy  * @bu: bulk-read information
7077f348f8cSMatthew Wilcox (Oracle)  * @folio1: first folio to read
7084793e7c5SAdrian Hunter  *
709ac8e9f64SRandy Dunlap  * Returns: %1 if the bulk-read is done, otherwise %0 is returned.
7104793e7c5SAdrian Hunter  */
ubifs_do_bulk_read(struct ubifs_info * c,struct bu_info * bu,struct folio * folio1)7116c0c42cdSArtem Bityutskiy static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu,
7127f348f8cSMatthew Wilcox (Oracle) 			      struct folio *folio1)
7134793e7c5SAdrian Hunter {
7147f348f8cSMatthew Wilcox (Oracle) 	pgoff_t offset = folio1->index, end_index;
7157f348f8cSMatthew Wilcox (Oracle) 	struct address_space *mapping = folio1->mapping;
7164793e7c5SAdrian Hunter 	struct inode *inode = mapping->host;
7174793e7c5SAdrian Hunter 	struct ubifs_inode *ui = ubifs_inode(inode);
7184793e7c5SAdrian Hunter 	int err, page_idx, page_cnt, ret = 0, n = 0;
7196c0c42cdSArtem Bityutskiy 	int allocate = bu->buf ? 0 : 1;
7204793e7c5SAdrian Hunter 	loff_t isize;
721480a1a6aSHyunchul Lee 	gfp_t ra_gfp_mask = readahead_gfp_mask(mapping) & ~__GFP_FS;
7224793e7c5SAdrian Hunter 
7234793e7c5SAdrian Hunter 	err = ubifs_tnc_get_bu_keys(c, bu);
7244793e7c5SAdrian Hunter 	if (err)
7254793e7c5SAdrian Hunter 		goto out_warn;
7264793e7c5SAdrian Hunter 
7274793e7c5SAdrian Hunter 	if (bu->eof) {
7284793e7c5SAdrian Hunter 		/* Turn off bulk-read at the end of the file */
7294793e7c5SAdrian Hunter 		ui->read_in_a_row = 1;
7304793e7c5SAdrian Hunter 		ui->bulk_read = 0;
7314793e7c5SAdrian Hunter 	}
7324793e7c5SAdrian Hunter 
7334793e7c5SAdrian Hunter 	page_cnt = bu->blk_cnt >> UBIFS_BLOCKS_PER_PAGE_SHIFT;
7344793e7c5SAdrian Hunter 	if (!page_cnt) {
7354793e7c5SAdrian Hunter 		/*
7364793e7c5SAdrian Hunter 		 * This happens when there are multiple blocks per page and the
7374793e7c5SAdrian Hunter 		 * blocks for the first page we are looking for, are not
7384793e7c5SAdrian Hunter 		 * together. If all the pages were like this, bulk-read would
7394793e7c5SAdrian Hunter 		 * reduce performance, so we turn it off for a while.
7404793e7c5SAdrian Hunter 		 */
7416c0c42cdSArtem Bityutskiy 		goto out_bu_off;
7424793e7c5SAdrian Hunter 	}
7434793e7c5SAdrian Hunter 
7444793e7c5SAdrian Hunter 	if (bu->cnt) {
7456c0c42cdSArtem Bityutskiy 		if (allocate) {
7466c0c42cdSArtem Bityutskiy 			/*
7476c0c42cdSArtem Bityutskiy 			 * Allocate bulk-read buffer depending on how many data
7486c0c42cdSArtem Bityutskiy 			 * nodes we are going to read.
7496c0c42cdSArtem Bityutskiy 			 */
7506c0c42cdSArtem Bityutskiy 			bu->buf_len = bu->zbranch[bu->cnt - 1].offs +
7516c0c42cdSArtem Bityutskiy 				      bu->zbranch[bu->cnt - 1].len -
7526c0c42cdSArtem Bityutskiy 				      bu->zbranch[0].offs;
7536eb61d58SRichard Weinberger 			ubifs_assert(c, bu->buf_len > 0);
7546eb61d58SRichard Weinberger 			ubifs_assert(c, bu->buf_len <= c->leb_size);
7556c0c42cdSArtem Bityutskiy 			bu->buf = kmalloc(bu->buf_len, GFP_NOFS | __GFP_NOWARN);
7566c0c42cdSArtem Bityutskiy 			if (!bu->buf)
7576c0c42cdSArtem Bityutskiy 				goto out_bu_off;
7586c0c42cdSArtem Bityutskiy 		}
7596c0c42cdSArtem Bityutskiy 
7604793e7c5SAdrian Hunter 		err = ubifs_tnc_bulk_read(c, bu);
7614793e7c5SAdrian Hunter 		if (err)
7624793e7c5SAdrian Hunter 			goto out_warn;
7634793e7c5SAdrian Hunter 	}
7644793e7c5SAdrian Hunter 
765a16bfab3SMatthew Wilcox (Oracle) 	err = populate_page(c, folio1, bu, &n);
7664793e7c5SAdrian Hunter 	if (err)
7674793e7c5SAdrian Hunter 		goto out_warn;
7684793e7c5SAdrian Hunter 
7697f348f8cSMatthew Wilcox (Oracle) 	folio_unlock(folio1);
7704793e7c5SAdrian Hunter 	ret = 1;
7714793e7c5SAdrian Hunter 
7724793e7c5SAdrian Hunter 	isize = i_size_read(inode);
7734793e7c5SAdrian Hunter 	if (isize == 0)
7744793e7c5SAdrian Hunter 		goto out_free;
77509cbfeafSKirill A. Shutemov 	end_index = ((isize - 1) >> PAGE_SHIFT);
7764793e7c5SAdrian Hunter 
7774793e7c5SAdrian Hunter 	for (page_idx = 1; page_idx < page_cnt; page_idx++) {
7784793e7c5SAdrian Hunter 		pgoff_t page_offset = offset + page_idx;
779d0619273SMatthew Wilcox (Oracle) 		struct folio *folio;
7804793e7c5SAdrian Hunter 
7814793e7c5SAdrian Hunter 		if (page_offset > end_index)
7824793e7c5SAdrian Hunter 			break;
783d0619273SMatthew Wilcox (Oracle) 		folio = __filemap_get_folio(mapping, page_offset,
784f5de5b83SZhihao Cheng 				 FGP_LOCK|FGP_ACCESSED|FGP_CREAT|FGP_NOWAIT,
785f5de5b83SZhihao Cheng 				 ra_gfp_mask);
786d0619273SMatthew Wilcox (Oracle) 		if (IS_ERR(folio))
7874793e7c5SAdrian Hunter 			break;
788d0619273SMatthew Wilcox (Oracle) 		if (!folio_test_uptodate(folio))
789a16bfab3SMatthew Wilcox (Oracle) 			err = populate_page(c, folio, bu, &n);
790d0619273SMatthew Wilcox (Oracle) 		folio_unlock(folio);
791d0619273SMatthew Wilcox (Oracle) 		folio_put(folio);
7924793e7c5SAdrian Hunter 		if (err)
7934793e7c5SAdrian Hunter 			break;
7944793e7c5SAdrian Hunter 	}
7954793e7c5SAdrian Hunter 
7964793e7c5SAdrian Hunter 	ui->last_page_read = offset + page_idx - 1;
7974793e7c5SAdrian Hunter 
7984793e7c5SAdrian Hunter out_free:
7996c0c42cdSArtem Bityutskiy 	if (allocate)
8004793e7c5SAdrian Hunter 		kfree(bu->buf);
8014793e7c5SAdrian Hunter 	return ret;
8024793e7c5SAdrian Hunter 
8034793e7c5SAdrian Hunter out_warn:
804235c362bSSheng Yong 	ubifs_warn(c, "ignoring error %d and skipping bulk-read", err);
8054793e7c5SAdrian Hunter 	goto out_free;
8066c0c42cdSArtem Bityutskiy 
8076c0c42cdSArtem Bityutskiy out_bu_off:
8086c0c42cdSArtem Bityutskiy 	ui->read_in_a_row = ui->bulk_read = 0;
8096c0c42cdSArtem Bityutskiy 	goto out_free;
8104793e7c5SAdrian Hunter }
8114793e7c5SAdrian Hunter 
8124793e7c5SAdrian Hunter /**
8134793e7c5SAdrian Hunter  * ubifs_bulk_read - determine whether to bulk-read and, if so, do it.
8147f348f8cSMatthew Wilcox (Oracle)  * @folio: folio from which to start bulk-read.
8154793e7c5SAdrian Hunter  *
8164793e7c5SAdrian Hunter  * Some flash media are capable of reading sequentially at faster rates. UBIFS
8174793e7c5SAdrian Hunter  * bulk-read facility is designed to take advantage of that, by reading in one
8184793e7c5SAdrian Hunter  * go consecutive data nodes that are also located consecutively in the same
819ac8e9f64SRandy Dunlap  * LEB.
820ac8e9f64SRandy Dunlap  *
821ac8e9f64SRandy Dunlap  * Returns: %1 if a bulk-read is done and %0 otherwise.
8224793e7c5SAdrian Hunter  */
ubifs_bulk_read(struct folio * folio)8237f348f8cSMatthew Wilcox (Oracle) static int ubifs_bulk_read(struct folio *folio)
8244793e7c5SAdrian Hunter {
8257f348f8cSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
8264793e7c5SAdrian Hunter 	struct ubifs_info *c = inode->i_sb->s_fs_info;
8274793e7c5SAdrian Hunter 	struct ubifs_inode *ui = ubifs_inode(inode);
8287f348f8cSMatthew Wilcox (Oracle) 	pgoff_t index = folio->index, last_page_read = ui->last_page_read;
8296c0c42cdSArtem Bityutskiy 	struct bu_info *bu;
8303477d204SArtem Bityutskiy 	int err = 0, allocated = 0;
8314793e7c5SAdrian Hunter 
8324793e7c5SAdrian Hunter 	ui->last_page_read = index;
8334793e7c5SAdrian Hunter 	if (!c->bulk_read)
8344793e7c5SAdrian Hunter 		return 0;
8356c0c42cdSArtem Bityutskiy 
8364793e7c5SAdrian Hunter 	/*
8373477d204SArtem Bityutskiy 	 * Bulk-read is protected by @ui->ui_mutex, but it is an optimization,
8383477d204SArtem Bityutskiy 	 * so don't bother if we cannot lock the mutex.
8394793e7c5SAdrian Hunter 	 */
8404793e7c5SAdrian Hunter 	if (!mutex_trylock(&ui->ui_mutex))
8414793e7c5SAdrian Hunter 		return 0;
8426c0c42cdSArtem Bityutskiy 
8434793e7c5SAdrian Hunter 	if (index != last_page_read + 1) {
8444793e7c5SAdrian Hunter 		/* Turn off bulk-read if we stop reading sequentially */
8454793e7c5SAdrian Hunter 		ui->read_in_a_row = 1;
8464793e7c5SAdrian Hunter 		if (ui->bulk_read)
8474793e7c5SAdrian Hunter 			ui->bulk_read = 0;
8484793e7c5SAdrian Hunter 		goto out_unlock;
8494793e7c5SAdrian Hunter 	}
8506c0c42cdSArtem Bityutskiy 
8514793e7c5SAdrian Hunter 	if (!ui->bulk_read) {
8524793e7c5SAdrian Hunter 		ui->read_in_a_row += 1;
8534793e7c5SAdrian Hunter 		if (ui->read_in_a_row < 3)
8544793e7c5SAdrian Hunter 			goto out_unlock;
8554793e7c5SAdrian Hunter 		/* Three reads in a row, so switch on bulk-read */
8564793e7c5SAdrian Hunter 		ui->bulk_read = 1;
8574793e7c5SAdrian Hunter 	}
8586c0c42cdSArtem Bityutskiy 
8593477d204SArtem Bityutskiy 	/*
8603477d204SArtem Bityutskiy 	 * If possible, try to use pre-allocated bulk-read information, which
8613477d204SArtem Bityutskiy 	 * is protected by @c->bu_mutex.
8623477d204SArtem Bityutskiy 	 */
8633477d204SArtem Bityutskiy 	if (mutex_trylock(&c->bu_mutex))
8643477d204SArtem Bityutskiy 		bu = &c->bu;
8653477d204SArtem Bityutskiy 	else {
8666c0c42cdSArtem Bityutskiy 		bu = kmalloc(sizeof(struct bu_info), GFP_NOFS | __GFP_NOWARN);
8676c0c42cdSArtem Bityutskiy 		if (!bu)
8683477d204SArtem Bityutskiy 			goto out_unlock;
8696c0c42cdSArtem Bityutskiy 
8706c0c42cdSArtem Bityutskiy 		bu->buf = NULL;
8713477d204SArtem Bityutskiy 		allocated = 1;
8723477d204SArtem Bityutskiy 	}
8733477d204SArtem Bityutskiy 
8746c0c42cdSArtem Bityutskiy 	bu->buf_len = c->max_bu_buf_len;
8756c0c42cdSArtem Bityutskiy 	data_key_init(c, &bu->key, inode->i_ino,
8767f348f8cSMatthew Wilcox (Oracle) 		      folio->index << UBIFS_BLOCKS_PER_PAGE_SHIFT);
8777f348f8cSMatthew Wilcox (Oracle) 	err = ubifs_do_bulk_read(c, bu, folio);
8783477d204SArtem Bityutskiy 
8793477d204SArtem Bityutskiy 	if (!allocated)
8803477d204SArtem Bityutskiy 		mutex_unlock(&c->bu_mutex);
8813477d204SArtem Bityutskiy 	else
8826c0c42cdSArtem Bityutskiy 		kfree(bu);
8836c0c42cdSArtem Bityutskiy 
8844793e7c5SAdrian Hunter out_unlock:
8854793e7c5SAdrian Hunter 	mutex_unlock(&ui->ui_mutex);
8866c0c42cdSArtem Bityutskiy 	return err;
8874793e7c5SAdrian Hunter }
8884793e7c5SAdrian Hunter 
ubifs_read_folio(struct file * file,struct folio * folio)8890b7bf483SMatthew Wilcox (Oracle) static int ubifs_read_folio(struct file *file, struct folio *folio)
8901e51764aSArtem Bityutskiy {
8917f348f8cSMatthew Wilcox (Oracle) 	if (ubifs_bulk_read(folio))
8924793e7c5SAdrian Hunter 		return 0;
893b96af1fdSMatthew Wilcox (Oracle) 	do_readpage(folio);
8940b7bf483SMatthew Wilcox (Oracle) 	folio_unlock(folio);
8951e51764aSArtem Bityutskiy 	return 0;
8961e51764aSArtem Bityutskiy }
8971e51764aSArtem Bityutskiy 
do_writepage(struct folio * folio,size_t len)8980c2d140cSMatthew Wilcox (Oracle) static int do_writepage(struct folio *folio, size_t len)
8991e51764aSArtem Bityutskiy {
9000c2d140cSMatthew Wilcox (Oracle) 	int err = 0, blen;
9011e51764aSArtem Bityutskiy 	unsigned int block;
9021e51764aSArtem Bityutskiy 	void *addr;
9030c2d140cSMatthew Wilcox (Oracle) 	size_t offset = 0;
9041e51764aSArtem Bityutskiy 	union ubifs_key key;
9050c2d140cSMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
9061e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
9071e51764aSArtem Bityutskiy 
9081e51764aSArtem Bityutskiy #ifdef UBIFS_DEBUG
909a0fd5951SDaniel Golle 	struct ubifs_inode *ui = ubifs_inode(inode);
9101e51764aSArtem Bityutskiy 	spin_lock(&ui->ui_lock);
9110c2d140cSMatthew Wilcox (Oracle) 	ubifs_assert(c, folio->index <= ui->synced_i_size >> PAGE_SHIFT);
9121e51764aSArtem Bityutskiy 	spin_unlock(&ui->ui_lock);
9131e51764aSArtem Bityutskiy #endif
9141e51764aSArtem Bityutskiy 
9150c2d140cSMatthew Wilcox (Oracle) 	folio_start_writeback(folio);
9161e51764aSArtem Bityutskiy 
9170c2d140cSMatthew Wilcox (Oracle) 	addr = kmap_local_folio(folio, offset);
9180c2d140cSMatthew Wilcox (Oracle) 	block = folio->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
9190c2d140cSMatthew Wilcox (Oracle) 	for (;;) {
9200c2d140cSMatthew Wilcox (Oracle) 		blen = min_t(size_t, len, UBIFS_BLOCK_SIZE);
9211e51764aSArtem Bityutskiy 		data_key_init(c, &key, inode->i_ino, block);
9221e51764aSArtem Bityutskiy 		err = ubifs_jnl_write_data(c, inode, &key, addr, blen);
9231e51764aSArtem Bityutskiy 		if (err)
9241e51764aSArtem Bityutskiy 			break;
9250c2d140cSMatthew Wilcox (Oracle) 		len -= blen;
9260c2d140cSMatthew Wilcox (Oracle) 		if (!len)
9271e51764aSArtem Bityutskiy 			break;
9281e51764aSArtem Bityutskiy 		block += 1;
9291e51764aSArtem Bityutskiy 		addr += blen;
9300c2d140cSMatthew Wilcox (Oracle) 		if (folio_test_highmem(folio) && !offset_in_page(addr)) {
9310c2d140cSMatthew Wilcox (Oracle) 			kunmap_local(addr - blen);
9320c2d140cSMatthew Wilcox (Oracle) 			offset += PAGE_SIZE;
9330c2d140cSMatthew Wilcox (Oracle) 			addr = kmap_local_folio(folio, offset);
9341e51764aSArtem Bityutskiy 		}
9350c2d140cSMatthew Wilcox (Oracle) 	}
9360c2d140cSMatthew Wilcox (Oracle) 	kunmap_local(addr);
9371e51764aSArtem Bityutskiy 	if (err) {
9380c2d140cSMatthew Wilcox (Oracle) 		mapping_set_error(folio->mapping, err);
9390c2d140cSMatthew Wilcox (Oracle) 		ubifs_err(c, "cannot write folio %lu of inode %lu, error %d",
9400c2d140cSMatthew Wilcox (Oracle) 			  folio->index, inode->i_ino, err);
9411e51764aSArtem Bityutskiy 		ubifs_ro_mode(c, err);
9421e51764aSArtem Bityutskiy 	}
9431e51764aSArtem Bityutskiy 
9440c2d140cSMatthew Wilcox (Oracle) 	ubifs_assert(c, folio->private != NULL);
9450c2d140cSMatthew Wilcox (Oracle) 	if (folio_test_checked(folio))
9461e51764aSArtem Bityutskiy 		release_new_page_budget(c);
9471e51764aSArtem Bityutskiy 	else
9481e51764aSArtem Bityutskiy 		release_existing_page_budget(c);
9491e51764aSArtem Bityutskiy 
9501e51764aSArtem Bityutskiy 	atomic_long_dec(&c->dirty_pg_cnt);
9510c2d140cSMatthew Wilcox (Oracle) 	folio_detach_private(folio);
9520c2d140cSMatthew Wilcox (Oracle) 	folio_clear_checked(folio);
9531e51764aSArtem Bityutskiy 
9540c2d140cSMatthew Wilcox (Oracle) 	folio_unlock(folio);
9550c2d140cSMatthew Wilcox (Oracle) 	folio_end_writeback(folio);
9561e51764aSArtem Bityutskiy 	return err;
9571e51764aSArtem Bityutskiy }
9581e51764aSArtem Bityutskiy 
9591e51764aSArtem Bityutskiy /*
9601e51764aSArtem Bityutskiy  * When writing-back dirty inodes, VFS first writes-back pages belonging to the
9611e51764aSArtem Bityutskiy  * inode, then the inode itself. For UBIFS this may cause a problem. Consider a
9621e51764aSArtem Bityutskiy  * situation when a we have an inode with size 0, then a megabyte of data is
9631e51764aSArtem Bityutskiy  * appended to the inode, then write-back starts and flushes some amount of the
9641e51764aSArtem Bityutskiy  * dirty pages, the journal becomes full, commit happens and finishes, and then
9651e51764aSArtem Bityutskiy  * an unclean reboot happens. When the file system is mounted next time, the
9661e51764aSArtem Bityutskiy  * inode size would still be 0, but there would be many pages which are beyond
9671e51764aSArtem Bityutskiy  * the inode size, they would be indexed and consume flash space. Because the
9681e51764aSArtem Bityutskiy  * journal has been committed, the replay would not be able to detect this
9691e51764aSArtem Bityutskiy  * situation and correct the inode size. This means UBIFS would have to scan
9701e51764aSArtem Bityutskiy  * whole index and correct all inode sizes, which is long an unacceptable.
9711e51764aSArtem Bityutskiy  *
9721e51764aSArtem Bityutskiy  * To prevent situations like this, UBIFS writes pages back only if they are
9737d4e9ccbSArtem Bityutskiy  * within the last synchronized inode size, i.e. the size which has been
9741e51764aSArtem Bityutskiy  * written to the flash media last time. Otherwise, UBIFS forces inode
9751e51764aSArtem Bityutskiy  * write-back, thus making sure the on-flash inode contains current inode size,
9761e51764aSArtem Bityutskiy  * and then keeps writing pages back.
9771e51764aSArtem Bityutskiy  *
9781e51764aSArtem Bityutskiy  * Some locking issues explanation. 'ubifs_writepage()' first is called with
9791e51764aSArtem Bityutskiy  * the page locked, and it locks @ui_mutex. However, write-back does take inode
9801e51764aSArtem Bityutskiy  * @i_mutex, which means other VFS operations may be run on this inode at the
9811e51764aSArtem Bityutskiy  * same time. And the problematic one is truncation to smaller size, from where
982c4361570SArtem Bityutskiy  * we have to call 'truncate_setsize()', which first changes @inode->i_size,
983c4361570SArtem Bityutskiy  * then drops the truncated pages. And while dropping the pages, it takes the
984c4361570SArtem Bityutskiy  * page lock. This means that 'do_truncation()' cannot call 'truncate_setsize()'
985c4361570SArtem Bityutskiy  * with @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'.
986c4361570SArtem Bityutskiy  * This means that @inode->i_size is changed while @ui_mutex is unlocked.
9871e51764aSArtem Bityutskiy  *
9882c27c65eSChristoph Hellwig  * XXX(truncate): with the new truncate sequence this is not true anymore,
9892c27c65eSChristoph Hellwig  * and the calls to truncate_setsize can be move around freely.  They should
9902c27c65eSChristoph Hellwig  * be moved to the very end of the truncate sequence.
99115c6fd97Snpiggin@suse.de  *
9921e51764aSArtem Bityutskiy  * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond
9931e51764aSArtem Bityutskiy  * inode size. How do we do this if @inode->i_size may became smaller while we
9941e51764aSArtem Bityutskiy  * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the
9951e51764aSArtem Bityutskiy  * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size
9961e51764aSArtem Bityutskiy  * internally and updates it under @ui_mutex.
9971e51764aSArtem Bityutskiy  *
9981e51764aSArtem Bityutskiy  * Q: why we do not worry that if we race with truncation, we may end up with a
9991e51764aSArtem Bityutskiy  * situation when the inode is truncated while we are in the middle of
10001e51764aSArtem Bityutskiy  * 'do_writepage()', so we do write beyond inode size?
10011e51764aSArtem Bityutskiy  * A: If we are in the middle of 'do_writepage()', truncation would be locked
10021e51764aSArtem Bityutskiy  * on the page lock and it would not write the truncated inode node to the
10031e51764aSArtem Bityutskiy  * journal before we have finished.
10041e51764aSArtem Bityutskiy  */
ubifs_writepage(struct folio * folio,struct writeback_control * wbc,void * data)10050df030d0SMatthew Wilcox (Oracle) static int ubifs_writepage(struct folio *folio, struct writeback_control *wbc,
10060df030d0SMatthew Wilcox (Oracle) 		void *data)
10071e51764aSArtem Bityutskiy {
1008c35acef3SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
10096eb61d58SRichard Weinberger 	struct ubifs_info *c = inode->i_sb->s_fs_info;
10101e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
10111e51764aSArtem Bityutskiy 	loff_t i_size =  i_size_read(inode), synced_i_size;
1012c35acef3SMatthew Wilcox (Oracle) 	int err, len = folio_size(folio);
10131e51764aSArtem Bityutskiy 
10141e51764aSArtem Bityutskiy 	dbg_gen("ino %lu, pg %lu, pg flags %#lx",
1015c35acef3SMatthew Wilcox (Oracle) 		inode->i_ino, folio->index, folio->flags);
1016c35acef3SMatthew Wilcox (Oracle) 	ubifs_assert(c, folio->private != NULL);
10171e51764aSArtem Bityutskiy 
1018c35acef3SMatthew Wilcox (Oracle) 	/* Is the folio fully outside @i_size? (truncate in progress) */
1019c35acef3SMatthew Wilcox (Oracle) 	if (folio_pos(folio) >= i_size) {
10201e51764aSArtem Bityutskiy 		err = 0;
10211e51764aSArtem Bityutskiy 		goto out_unlock;
10221e51764aSArtem Bityutskiy 	}
10231e51764aSArtem Bityutskiy 
10241e51764aSArtem Bityutskiy 	spin_lock(&ui->ui_lock);
10251e51764aSArtem Bityutskiy 	synced_i_size = ui->synced_i_size;
10261e51764aSArtem Bityutskiy 	spin_unlock(&ui->ui_lock);
10271e51764aSArtem Bityutskiy 
1028c35acef3SMatthew Wilcox (Oracle) 	/* Is the folio fully inside i_size? */
1029c35acef3SMatthew Wilcox (Oracle) 	if (folio_pos(folio) + len <= i_size) {
1030*25e79a7fSZhihao Cheng 		if (folio_pos(folio) + len > synced_i_size) {
1031a9185b41SChristoph Hellwig 			err = inode->i_sb->s_op->write_inode(inode, NULL);
10321e51764aSArtem Bityutskiy 			if (err)
1033fb8bc4c7SZhihao Cheng 				goto out_redirty;
10341e51764aSArtem Bityutskiy 			/*
10351e51764aSArtem Bityutskiy 			 * The inode has been written, but the write-buffer has
10361e51764aSArtem Bityutskiy 			 * not been synchronized, so in case of an unclean
10371e51764aSArtem Bityutskiy 			 * reboot we may end up with some pages beyond inode
10381e51764aSArtem Bityutskiy 			 * size, but they would be in the journal (because
10391e51764aSArtem Bityutskiy 			 * commit flushes write buffers) and recovery would deal
10401e51764aSArtem Bityutskiy 			 * with this.
10411e51764aSArtem Bityutskiy 			 */
10421e51764aSArtem Bityutskiy 		}
10430c2d140cSMatthew Wilcox (Oracle) 		return do_writepage(folio, len);
10441e51764aSArtem Bityutskiy 	}
10451e51764aSArtem Bityutskiy 
10461e51764aSArtem Bityutskiy 	/*
1047c35acef3SMatthew Wilcox (Oracle) 	 * The folio straddles @i_size. It must be zeroed out on each and every
10481e51764aSArtem Bityutskiy 	 * writepage invocation because it may be mmapped. "A file is mapped
10491e51764aSArtem Bityutskiy 	 * in multiples of the page size. For a file that is not a multiple of
10501e51764aSArtem Bityutskiy 	 * the page size, the remaining memory is zeroed when mapped, and
10511e51764aSArtem Bityutskiy 	 * writes to that region are not written out to the file."
10521e51764aSArtem Bityutskiy 	 */
1053c35acef3SMatthew Wilcox (Oracle) 	len = i_size - folio_pos(folio);
1054c35acef3SMatthew Wilcox (Oracle) 	folio_zero_segment(folio, len, folio_size(folio));
10551e51764aSArtem Bityutskiy 
10561e51764aSArtem Bityutskiy 	if (i_size > synced_i_size) {
1057a9185b41SChristoph Hellwig 		err = inode->i_sb->s_op->write_inode(inode, NULL);
10581e51764aSArtem Bityutskiy 		if (err)
1059fb8bc4c7SZhihao Cheng 			goto out_redirty;
10601e51764aSArtem Bityutskiy 	}
10611e51764aSArtem Bityutskiy 
10620c2d140cSMatthew Wilcox (Oracle) 	return do_writepage(folio, len);
1063fb8bc4c7SZhihao Cheng out_redirty:
1064fb8bc4c7SZhihao Cheng 	/*
1065c35acef3SMatthew Wilcox (Oracle) 	 * folio_redirty_for_writepage() won't call ubifs_dirty_inode() because
1066fb8bc4c7SZhihao Cheng 	 * it passes I_DIRTY_PAGES flag while calling __mark_inode_dirty(), so
1067fb8bc4c7SZhihao Cheng 	 * there is no need to do space budget for dirty inode.
1068fb8bc4c7SZhihao Cheng 	 */
1069c35acef3SMatthew Wilcox (Oracle) 	folio_redirty_for_writepage(wbc, folio);
10701e51764aSArtem Bityutskiy out_unlock:
1071c35acef3SMatthew Wilcox (Oracle) 	folio_unlock(folio);
10721e51764aSArtem Bityutskiy 	return err;
10731e51764aSArtem Bityutskiy }
10741e51764aSArtem Bityutskiy 
ubifs_writepages(struct address_space * mapping,struct writeback_control * wbc)10750df030d0SMatthew Wilcox (Oracle) static int ubifs_writepages(struct address_space *mapping,
10760df030d0SMatthew Wilcox (Oracle) 		struct writeback_control *wbc)
10770df030d0SMatthew Wilcox (Oracle) {
10780df030d0SMatthew Wilcox (Oracle) 	return write_cache_pages(mapping, wbc, ubifs_writepage, NULL);
10790df030d0SMatthew Wilcox (Oracle) }
10800df030d0SMatthew Wilcox (Oracle) 
10811e51764aSArtem Bityutskiy /**
10821e51764aSArtem Bityutskiy  * do_attr_changes - change inode attributes.
10831e51764aSArtem Bityutskiy  * @inode: inode to change attributes for
10841e51764aSArtem Bityutskiy  * @attr: describes attributes to change
10851e51764aSArtem Bityutskiy  */
do_attr_changes(struct inode * inode,const struct iattr * attr)10861e51764aSArtem Bityutskiy static void do_attr_changes(struct inode *inode, const struct iattr *attr)
10871e51764aSArtem Bityutskiy {
10881e51764aSArtem Bityutskiy 	if (attr->ia_valid & ATTR_UID)
10891e51764aSArtem Bityutskiy 		inode->i_uid = attr->ia_uid;
10901e51764aSArtem Bityutskiy 	if (attr->ia_valid & ATTR_GID)
10911e51764aSArtem Bityutskiy 		inode->i_gid = attr->ia_gid;
1092eb31e2f6SAmir Goldstein 	if (attr->ia_valid & ATTR_ATIME)
1093e4cfef33SJeff Layton 		inode_set_atime_to_ts(inode, attr->ia_atime);
1094eb31e2f6SAmir Goldstein 	if (attr->ia_valid & ATTR_MTIME)
1095e4cfef33SJeff Layton 		inode_set_mtime_to_ts(inode, attr->ia_mtime);
1096eb31e2f6SAmir Goldstein 	if (attr->ia_valid & ATTR_CTIME)
1097d07d3a7eSJeff Layton 		inode_set_ctime_to_ts(inode, attr->ia_ctime);
10981e51764aSArtem Bityutskiy 	if (attr->ia_valid & ATTR_MODE) {
10991e51764aSArtem Bityutskiy 		umode_t mode = attr->ia_mode;
11001e51764aSArtem Bityutskiy 
11011e51764aSArtem Bityutskiy 		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
11021e51764aSArtem Bityutskiy 			mode &= ~S_ISGID;
11031e51764aSArtem Bityutskiy 		inode->i_mode = mode;
11041e51764aSArtem Bityutskiy 	}
11051e51764aSArtem Bityutskiy }
11061e51764aSArtem Bityutskiy 
11071e51764aSArtem Bityutskiy /**
11081e51764aSArtem Bityutskiy  * do_truncation - truncate an inode.
11091e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11101e51764aSArtem Bityutskiy  * @inode: inode to truncate
11111e51764aSArtem Bityutskiy  * @attr: inode attribute changes description
11121e51764aSArtem Bityutskiy  *
11131e51764aSArtem Bityutskiy  * This function implements VFS '->setattr()' call when the inode is truncated
1114ac8e9f64SRandy Dunlap  * to a smaller size.
1115ac8e9f64SRandy Dunlap  *
1116ac8e9f64SRandy Dunlap  * Returns: %0 in case of success and a negative error code
11171e51764aSArtem Bityutskiy  * in case of failure.
11181e51764aSArtem Bityutskiy  */
do_truncation(struct ubifs_info * c,struct inode * inode,const struct iattr * attr)11191e51764aSArtem Bityutskiy static int do_truncation(struct ubifs_info *c, struct inode *inode,
11201e51764aSArtem Bityutskiy 			 const struct iattr *attr)
11211e51764aSArtem Bityutskiy {
11221e51764aSArtem Bityutskiy 	int err;
11231e51764aSArtem Bityutskiy 	struct ubifs_budget_req req;
11241e51764aSArtem Bityutskiy 	loff_t old_size = inode->i_size, new_size = attr->ia_size;
112504da11bfSArtem Bityutskiy 	int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1;
11261e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
11271e51764aSArtem Bityutskiy 
11281e51764aSArtem Bityutskiy 	dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size);
11291e51764aSArtem Bityutskiy 	memset(&req, 0, sizeof(struct ubifs_budget_req));
11301e51764aSArtem Bityutskiy 
11311e51764aSArtem Bityutskiy 	/*
11321e51764aSArtem Bityutskiy 	 * If this is truncation to a smaller size, and we do not truncate on a
11331e51764aSArtem Bityutskiy 	 * block boundary, budget for changing one data block, because the last
11341e51764aSArtem Bityutskiy 	 * block will be re-written.
11351e51764aSArtem Bityutskiy 	 */
11361e51764aSArtem Bityutskiy 	if (new_size & (UBIFS_BLOCK_SIZE - 1))
11371e51764aSArtem Bityutskiy 		req.dirtied_page = 1;
11381e51764aSArtem Bityutskiy 
11391e51764aSArtem Bityutskiy 	req.dirtied_ino = 1;
11401e51764aSArtem Bityutskiy 	/* A funny way to budget for truncation node */
11411e51764aSArtem Bityutskiy 	req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ;
11421e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
114304da11bfSArtem Bityutskiy 	if (err) {
114404da11bfSArtem Bityutskiy 		/*
114504da11bfSArtem Bityutskiy 		 * Treat truncations to zero as deletion and always allow them,
114604da11bfSArtem Bityutskiy 		 * just like we do for '->unlink()'.
114704da11bfSArtem Bityutskiy 		 */
114804da11bfSArtem Bityutskiy 		if (new_size || err != -ENOSPC)
11491e51764aSArtem Bityutskiy 			return err;
115004da11bfSArtem Bityutskiy 		budgeted = 0;
115104da11bfSArtem Bityutskiy 	}
11521e51764aSArtem Bityutskiy 
11532c27c65eSChristoph Hellwig 	truncate_setsize(inode, new_size);
11541e51764aSArtem Bityutskiy 
11551e51764aSArtem Bityutskiy 	if (offset) {
115609cbfeafSKirill A. Shutemov 		pgoff_t index = new_size >> PAGE_SHIFT;
1157783d0741SMatthew Wilcox (Oracle) 		struct folio *folio;
11581e51764aSArtem Bityutskiy 
1159783d0741SMatthew Wilcox (Oracle) 		folio = filemap_lock_folio(inode->i_mapping, index);
1160783d0741SMatthew Wilcox (Oracle) 		if (!IS_ERR(folio)) {
1161783d0741SMatthew Wilcox (Oracle) 			if (folio_test_dirty(folio)) {
11621e51764aSArtem Bityutskiy 				/*
11631e51764aSArtem Bityutskiy 				 * 'ubifs_jnl_truncate()' will try to truncate
11641e51764aSArtem Bityutskiy 				 * the last data node, but it contains
11651e51764aSArtem Bityutskiy 				 * out-of-date data because the page is dirty.
11661e51764aSArtem Bityutskiy 				 * Write the page now, so that
11671e51764aSArtem Bityutskiy 				 * 'ubifs_jnl_truncate()' will see an already
11681e51764aSArtem Bityutskiy 				 * truncated (and up to date) data node.
11691e51764aSArtem Bityutskiy 				 */
1170783d0741SMatthew Wilcox (Oracle) 				ubifs_assert(c, folio->private != NULL);
11711e51764aSArtem Bityutskiy 
1172783d0741SMatthew Wilcox (Oracle) 				folio_clear_dirty_for_io(folio);
11731e51764aSArtem Bityutskiy 				if (UBIFS_BLOCKS_PER_PAGE_SHIFT)
1174783d0741SMatthew Wilcox (Oracle) 					offset = offset_in_folio(folio,
1175783d0741SMatthew Wilcox (Oracle) 							new_size);
11760c2d140cSMatthew Wilcox (Oracle) 				err = do_writepage(folio, offset);
1177783d0741SMatthew Wilcox (Oracle) 				folio_put(folio);
11781e51764aSArtem Bityutskiy 				if (err)
11791e51764aSArtem Bityutskiy 					goto out_budg;
11801e51764aSArtem Bityutskiy 				/*
11811e51764aSArtem Bityutskiy 				 * We could now tell 'ubifs_jnl_truncate()' not
11821e51764aSArtem Bityutskiy 				 * to read the last block.
11831e51764aSArtem Bityutskiy 				 */
11841e51764aSArtem Bityutskiy 			} else {
11851e51764aSArtem Bityutskiy 				/*
11861e51764aSArtem Bityutskiy 				 * We could 'kmap()' the page and pass the data
11871e51764aSArtem Bityutskiy 				 * to 'ubifs_jnl_truncate()' to save it from
11881e51764aSArtem Bityutskiy 				 * having to read it.
11891e51764aSArtem Bityutskiy 				 */
1190783d0741SMatthew Wilcox (Oracle) 				folio_unlock(folio);
1191783d0741SMatthew Wilcox (Oracle) 				folio_put(folio);
11921e51764aSArtem Bityutskiy 			}
11931e51764aSArtem Bityutskiy 		}
11941e51764aSArtem Bityutskiy 	}
11951e51764aSArtem Bityutskiy 
11961e51764aSArtem Bityutskiy 	mutex_lock(&ui->ui_mutex);
11971e51764aSArtem Bityutskiy 	ui->ui_size = inode->i_size;
11981e51764aSArtem Bityutskiy 	/* Truncation changes inode [mc]time */
1199e4cfef33SJeff Layton 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
1200873a64c7SArtem Bityutskiy 	/* Other attributes may be changed at the same time as well */
12011e51764aSArtem Bityutskiy 	do_attr_changes(inode, attr);
12021e51764aSArtem Bityutskiy 	err = ubifs_jnl_truncate(c, inode, old_size, new_size);
12031e51764aSArtem Bityutskiy 	mutex_unlock(&ui->ui_mutex);
1204873a64c7SArtem Bityutskiy 
12051e51764aSArtem Bityutskiy out_budg:
120604da11bfSArtem Bityutskiy 	if (budgeted)
12071e51764aSArtem Bityutskiy 		ubifs_release_budget(c, &req);
120804da11bfSArtem Bityutskiy 	else {
1209b137545cSArtem Bityutskiy 		c->bi.nospace = c->bi.nospace_rp = 0;
121004da11bfSArtem Bityutskiy 		smp_wmb();
121104da11bfSArtem Bityutskiy 	}
12121e51764aSArtem Bityutskiy 	return err;
12131e51764aSArtem Bityutskiy }
12141e51764aSArtem Bityutskiy 
12151e51764aSArtem Bityutskiy /**
12161e51764aSArtem Bityutskiy  * do_setattr - change inode attributes.
12171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
12181e51764aSArtem Bityutskiy  * @inode: inode to change attributes for
12191e51764aSArtem Bityutskiy  * @attr: inode attribute changes description
12201e51764aSArtem Bityutskiy  *
12211e51764aSArtem Bityutskiy  * This function implements VFS '->setattr()' call for all cases except
1222ac8e9f64SRandy Dunlap  * truncations to smaller size.
1223ac8e9f64SRandy Dunlap  *
1224ac8e9f64SRandy Dunlap  * Returns: %0 in case of success and a negative
12251e51764aSArtem Bityutskiy  * error code in case of failure.
12261e51764aSArtem Bityutskiy  */
do_setattr(struct ubifs_info * c,struct inode * inode,const struct iattr * attr)12271e51764aSArtem Bityutskiy static int do_setattr(struct ubifs_info *c, struct inode *inode,
12281e51764aSArtem Bityutskiy 		      const struct iattr *attr)
12291e51764aSArtem Bityutskiy {
12301e51764aSArtem Bityutskiy 	int err, release;
12311e51764aSArtem Bityutskiy 	loff_t new_size = attr->ia_size;
12321e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
12331e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .dirtied_ino = 1,
1234dab4b4d2SArtem Bityutskiy 				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
12351e51764aSArtem Bityutskiy 
12361e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
12371e51764aSArtem Bityutskiy 	if (err)
12381e51764aSArtem Bityutskiy 		return err;
12391e51764aSArtem Bityutskiy 
12401e51764aSArtem Bityutskiy 	if (attr->ia_valid & ATTR_SIZE) {
12411e51764aSArtem Bityutskiy 		dbg_gen("size %lld -> %lld", inode->i_size, new_size);
12422c27c65eSChristoph Hellwig 		truncate_setsize(inode, new_size);
12431e51764aSArtem Bityutskiy 	}
12441e51764aSArtem Bityutskiy 
12451e51764aSArtem Bityutskiy 	mutex_lock(&ui->ui_mutex);
12461e51764aSArtem Bityutskiy 	if (attr->ia_valid & ATTR_SIZE) {
12471e51764aSArtem Bityutskiy 		/* Truncation changes inode [mc]time */
1248e4cfef33SJeff Layton 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
12492c27c65eSChristoph Hellwig 		/* 'truncate_setsize()' changed @i_size, update @ui_size */
12501e51764aSArtem Bityutskiy 		ui->ui_size = inode->i_size;
12511e51764aSArtem Bityutskiy 	}
12521e51764aSArtem Bityutskiy 
12531e51764aSArtem Bityutskiy 	do_attr_changes(inode, attr);
12541e51764aSArtem Bityutskiy 
12551e51764aSArtem Bityutskiy 	release = ui->dirty;
12561e51764aSArtem Bityutskiy 	if (attr->ia_valid & ATTR_SIZE)
12571e51764aSArtem Bityutskiy 		/*
12581e51764aSArtem Bityutskiy 		 * Inode length changed, so we have to make sure
12591e51764aSArtem Bityutskiy 		 * @I_DIRTY_DATASYNC is set.
12601e51764aSArtem Bityutskiy 		 */
1261f3556254SChristoph Hellwig 		 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
12621e51764aSArtem Bityutskiy 	else
12631e51764aSArtem Bityutskiy 		mark_inode_dirty_sync(inode);
12641e51764aSArtem Bityutskiy 	mutex_unlock(&ui->ui_mutex);
12651e51764aSArtem Bityutskiy 
12661e51764aSArtem Bityutskiy 	if (release)
12671e51764aSArtem Bityutskiy 		ubifs_release_budget(c, &req);
12681e51764aSArtem Bityutskiy 	if (IS_SYNC(inode))
1269a9185b41SChristoph Hellwig 		err = inode->i_sb->s_op->write_inode(inode, NULL);
12701e51764aSArtem Bityutskiy 	return err;
12711e51764aSArtem Bityutskiy }
12721e51764aSArtem Bityutskiy 
ubifs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)1273c1632a0fSChristian Brauner int ubifs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
1274549c7297SChristian Brauner 		  struct iattr *attr)
12751e51764aSArtem Bityutskiy {
12761e51764aSArtem Bityutskiy 	int err;
12772b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
12781e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
12791e51764aSArtem Bityutskiy 
12807d32c2bbSArtem Bityutskiy 	dbg_gen("ino %lu, mode %#x, ia_valid %#x",
12817d32c2bbSArtem Bityutskiy 		inode->i_ino, inode->i_mode, attr->ia_valid);
1282c1632a0fSChristian Brauner 	err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
12831e51764aSArtem Bityutskiy 	if (err)
12841e51764aSArtem Bityutskiy 		return err;
12851e51764aSArtem Bityutskiy 
1286d808efb4SArtem Bityutskiy 	err = dbg_check_synced_i_size(c, inode);
12871e51764aSArtem Bityutskiy 	if (err)
12881e51764aSArtem Bityutskiy 		return err;
12891e51764aSArtem Bityutskiy 
1290252153baSEric Biggers 	err = fscrypt_prepare_setattr(dentry, attr);
12914afb9996SEric Biggers 	if (err)
12924afb9996SEric Biggers 		return err;
12934afb9996SEric Biggers 
12941e51764aSArtem Bityutskiy 	if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size < inode->i_size)
12951e51764aSArtem Bityutskiy 		/* Truncation to a smaller size */
12961e51764aSArtem Bityutskiy 		err = do_truncation(c, inode, attr);
12971e51764aSArtem Bityutskiy 	else
12981e51764aSArtem Bityutskiy 		err = do_setattr(c, inode, attr);
12991e51764aSArtem Bityutskiy 
13001e51764aSArtem Bityutskiy 	return err;
13011e51764aSArtem Bityutskiy }
13021e51764aSArtem Bityutskiy 
ubifs_invalidate_folio(struct folio * folio,size_t offset,size_t length)130358a2fdb6SMatthew Wilcox (Oracle) static void ubifs_invalidate_folio(struct folio *folio, size_t offset,
130458a2fdb6SMatthew Wilcox (Oracle) 				 size_t length)
13051e51764aSArtem Bityutskiy {
130658a2fdb6SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
13071e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
13081e51764aSArtem Bityutskiy 
130958a2fdb6SMatthew Wilcox (Oracle) 	ubifs_assert(c, folio_test_private(folio));
131058a2fdb6SMatthew Wilcox (Oracle) 	if (offset || length < folio_size(folio))
131158a2fdb6SMatthew Wilcox (Oracle) 		/* Partial folio remains dirty */
13121e51764aSArtem Bityutskiy 		return;
13131e51764aSArtem Bityutskiy 
131458a2fdb6SMatthew Wilcox (Oracle) 	if (folio_test_checked(folio))
13151e51764aSArtem Bityutskiy 		release_new_page_budget(c);
13161e51764aSArtem Bityutskiy 	else
13171e51764aSArtem Bityutskiy 		release_existing_page_budget(c);
13181e51764aSArtem Bityutskiy 
13191e51764aSArtem Bityutskiy 	atomic_long_dec(&c->dirty_pg_cnt);
1320a87a08e3SLinus Torvalds 	folio_detach_private(folio);
132158a2fdb6SMatthew Wilcox (Oracle) 	folio_clear_checked(folio);
13221e51764aSArtem Bityutskiy }
13231e51764aSArtem Bityutskiy 
ubifs_fsync(struct file * file,loff_t start,loff_t end,int datasync)132402c24a82SJosef Bacik int ubifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
13251e51764aSArtem Bityutskiy {
13267ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
13271e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
13281e51764aSArtem Bityutskiy 	int err;
13291e51764aSArtem Bityutskiy 
13301e51764aSArtem Bityutskiy 	dbg_gen("syncing inode %lu", inode->i_ino);
13311e51764aSArtem Bityutskiy 
13323b2f9a01SArtem Bityutskiy 	if (c->ro_mount)
13333b2f9a01SArtem Bityutskiy 		/*
13343b2f9a01SArtem Bityutskiy 		 * For some really strange reasons VFS does not filter out
13353b2f9a01SArtem Bityutskiy 		 * 'fsync()' for R/O mounted file-systems as per 2.6.39.
13363b2f9a01SArtem Bityutskiy 		 */
133778530bf7SArtem Bityutskiy 		return 0;
133878530bf7SArtem Bityutskiy 
13393b49c9a1SJeff Layton 	err = file_write_and_wait_range(file, start, end);
134002c24a82SJosef Bacik 	if (err)
134102c24a82SJosef Bacik 		return err;
13425955102cSAl Viro 	inode_lock(inode);
134302c24a82SJosef Bacik 
134402c24a82SJosef Bacik 	/* Synchronize the inode unless this is a 'datasync()' call. */
13451e51764aSArtem Bityutskiy 	if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) {
1346a9185b41SChristoph Hellwig 		err = inode->i_sb->s_op->write_inode(inode, NULL);
13471e51764aSArtem Bityutskiy 		if (err)
134802c24a82SJosef Bacik 			goto out;
13491e51764aSArtem Bityutskiy 	}
13501e51764aSArtem Bityutskiy 
13511e51764aSArtem Bityutskiy 	/*
13521e51764aSArtem Bityutskiy 	 * Nodes related to this inode may still sit in a write-buffer. Flush
13531e51764aSArtem Bityutskiy 	 * them.
13541e51764aSArtem Bityutskiy 	 */
13551e51764aSArtem Bityutskiy 	err = ubifs_sync_wbufs_by_inode(c, inode);
135602c24a82SJosef Bacik out:
13575955102cSAl Viro 	inode_unlock(inode);
13581e51764aSArtem Bityutskiy 	return err;
13591e51764aSArtem Bityutskiy }
13601e51764aSArtem Bityutskiy 
13611e51764aSArtem Bityutskiy /**
13621e51764aSArtem Bityutskiy  * mctime_update_needed - check if mtime or ctime update is needed.
13631e51764aSArtem Bityutskiy  * @inode: the inode to do the check for
13641e51764aSArtem Bityutskiy  * @now: current time
13651e51764aSArtem Bityutskiy  *
13661e51764aSArtem Bityutskiy  * This helper function checks if the inode mtime/ctime should be updated or
13671e51764aSArtem Bityutskiy  * not. If current values of the time-stamps are within the UBIFS inode time
13681e51764aSArtem Bityutskiy  * granularity, they are not updated. This is an optimization.
1369ac8e9f64SRandy Dunlap  *
1370ac8e9f64SRandy Dunlap  * Returns: %1 if time update is needed, %0 if not
13711e51764aSArtem Bityutskiy  */
mctime_update_needed(const struct inode * inode,const struct timespec64 * now)13721e51764aSArtem Bityutskiy static inline int mctime_update_needed(const struct inode *inode,
13730eca0b80SArnd Bergmann 				       const struct timespec64 *now)
13741e51764aSArtem Bityutskiy {
1375d07d3a7eSJeff Layton 	struct timespec64 ctime = inode_get_ctime(inode);
1376e4cfef33SJeff Layton 	struct timespec64 mtime = inode_get_mtime(inode);
1377d07d3a7eSJeff Layton 
1378e4cfef33SJeff Layton 	if (!timespec64_equal(&mtime, now) || !timespec64_equal(&ctime, now))
13791e51764aSArtem Bityutskiy 		return 1;
13801e51764aSArtem Bityutskiy 	return 0;
13811e51764aSArtem Bityutskiy }
13821e51764aSArtem Bityutskiy 
13838c1c5f26SDongsheng Yang /**
13848c1c5f26SDongsheng Yang  * ubifs_update_time - update time of inode.
13858c1c5f26SDongsheng Yang  * @inode: inode to update
138648ec6328SYang Li  * @flags: time updating control flag determines updating
138748ec6328SYang Li  *	    which time fields of @inode
13888c1c5f26SDongsheng Yang  *
13898c1c5f26SDongsheng Yang  * This function updates time of the inode.
1390ac8e9f64SRandy Dunlap  *
1391ac8e9f64SRandy Dunlap  * Returns: %0 for success or a negative error code otherwise.
13928c1c5f26SDongsheng Yang  */
ubifs_update_time(struct inode * inode,int flags)1393913e9928SJeff Layton int ubifs_update_time(struct inode *inode, int flags)
13948c1c5f26SDongsheng Yang {
13958c1c5f26SDongsheng Yang 	struct ubifs_inode *ui = ubifs_inode(inode);
13968c1c5f26SDongsheng Yang 	struct ubifs_info *c = inode->i_sb->s_fs_info;
13978c1c5f26SDongsheng Yang 	struct ubifs_budget_req req = { .dirtied_ino = 1,
13988c1c5f26SDongsheng Yang 			.dirtied_ino_d = ALIGN(ui->data_len, 8) };
13998c1c5f26SDongsheng Yang 	int err, release;
14008c1c5f26SDongsheng Yang 
1401541d4c79SJeff Layton 	if (!IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT)) {
1402541d4c79SJeff Layton 		generic_update_time(inode, flags);
1403541d4c79SJeff Layton 		return 0;
1404541d4c79SJeff Layton 	}
1405e3d73deaSSascha Hauer 
14068c1c5f26SDongsheng Yang 	err = ubifs_budget_space(c, &req);
14078c1c5f26SDongsheng Yang 	if (err)
14088c1c5f26SDongsheng Yang 		return err;
14098c1c5f26SDongsheng Yang 
14108c1c5f26SDongsheng Yang 	mutex_lock(&ui->ui_mutex);
141197ebfdb7SJeff Layton 	inode_update_timestamps(inode, flags);
14128c1c5f26SDongsheng Yang 	release = ui->dirty;
1413ecf84096SChristoph Hellwig 	__mark_inode_dirty(inode, I_DIRTY_SYNC);
14148c1c5f26SDongsheng Yang 	mutex_unlock(&ui->ui_mutex);
14158c1c5f26SDongsheng Yang 	if (release)
14168c1c5f26SDongsheng Yang 		ubifs_release_budget(c, &req);
14178c1c5f26SDongsheng Yang 	return 0;
14188c1c5f26SDongsheng Yang }
14198c1c5f26SDongsheng Yang 
14201e51764aSArtem Bityutskiy /**
1421ec037dfcSJulia Lawall  * update_mctime - update mtime and ctime of an inode.
14221e51764aSArtem Bityutskiy  * @inode: inode to update
14231e51764aSArtem Bityutskiy  *
14241e51764aSArtem Bityutskiy  * This function updates mtime and ctime of the inode if it is not equivalent to
1425ac8e9f64SRandy Dunlap  * current time.
1426ac8e9f64SRandy Dunlap  *
1427ac8e9f64SRandy Dunlap  * Returns: %0 in case of success and a negative error code in
14281e51764aSArtem Bityutskiy  * case of failure.
14291e51764aSArtem Bityutskiy  */
update_mctime(struct inode * inode)1430f5674c31SAl Viro static int update_mctime(struct inode *inode)
14311e51764aSArtem Bityutskiy {
14320eca0b80SArnd Bergmann 	struct timespec64 now = current_time(inode);
14331e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
1434f5674c31SAl Viro 	struct ubifs_info *c = inode->i_sb->s_fs_info;
14351e51764aSArtem Bityutskiy 
14361e51764aSArtem Bityutskiy 	if (mctime_update_needed(inode, &now)) {
14371e51764aSArtem Bityutskiy 		int err, release;
14381e51764aSArtem Bityutskiy 		struct ubifs_budget_req req = { .dirtied_ino = 1,
1439dab4b4d2SArtem Bityutskiy 				.dirtied_ino_d = ALIGN(ui->data_len, 8) };
14401e51764aSArtem Bityutskiy 
14411e51764aSArtem Bityutskiy 		err = ubifs_budget_space(c, &req);
14421e51764aSArtem Bityutskiy 		if (err)
14431e51764aSArtem Bityutskiy 			return err;
14441e51764aSArtem Bityutskiy 
14451e51764aSArtem Bityutskiy 		mutex_lock(&ui->ui_mutex);
1446e4cfef33SJeff Layton 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
14471e51764aSArtem Bityutskiy 		release = ui->dirty;
14481e51764aSArtem Bityutskiy 		mark_inode_dirty_sync(inode);
14491e51764aSArtem Bityutskiy 		mutex_unlock(&ui->ui_mutex);
14501e51764aSArtem Bityutskiy 		if (release)
14511e51764aSArtem Bityutskiy 			ubifs_release_budget(c, &req);
14521e51764aSArtem Bityutskiy 	}
14531e51764aSArtem Bityutskiy 
14541e51764aSArtem Bityutskiy 	return 0;
14551e51764aSArtem Bityutskiy }
14561e51764aSArtem Bityutskiy 
ubifs_write_iter(struct kiocb * iocb,struct iov_iter * from)1457f5674c31SAl Viro static ssize_t ubifs_write_iter(struct kiocb *iocb, struct iov_iter *from)
14581e51764aSArtem Bityutskiy {
1459f5674c31SAl Viro 	int err = update_mctime(file_inode(iocb->ki_filp));
14601e51764aSArtem Bityutskiy 	if (err)
14611e51764aSArtem Bityutskiy 		return err;
14621e51764aSArtem Bityutskiy 
1463f5674c31SAl Viro 	return generic_file_write_iter(iocb, from);
14641e51764aSArtem Bityutskiy }
14651e51764aSArtem Bityutskiy 
ubifs_dirty_folio(struct address_space * mapping,struct folio * folio)14661f1d14dbSMatthew Wilcox (Oracle) static bool ubifs_dirty_folio(struct address_space *mapping,
14671f1d14dbSMatthew Wilcox (Oracle) 		struct folio *folio)
14681e51764aSArtem Bityutskiy {
14691f1d14dbSMatthew Wilcox (Oracle) 	bool ret;
14701f1d14dbSMatthew Wilcox (Oracle) 	struct ubifs_info *c = mapping->host->i_sb->s_fs_info;
14711e51764aSArtem Bityutskiy 
14721f1d14dbSMatthew Wilcox (Oracle) 	ret = filemap_dirty_folio(mapping, folio);
14731e51764aSArtem Bityutskiy 	/*
14741e51764aSArtem Bityutskiy 	 * An attempt to dirty a page without budgeting for it - should not
14751e51764aSArtem Bityutskiy 	 * happen.
14761e51764aSArtem Bityutskiy 	 */
14771f1d14dbSMatthew Wilcox (Oracle) 	ubifs_assert(c, ret == false);
14781e51764aSArtem Bityutskiy 	return ret;
14791e51764aSArtem Bityutskiy }
14801e51764aSArtem Bityutskiy 
ubifs_release_folio(struct folio * folio,gfp_t unused_gfp_flags)1481bcaabc55SMatthew Wilcox (Oracle) static bool ubifs_release_folio(struct folio *folio, gfp_t unused_gfp_flags)
14821e51764aSArtem Bityutskiy {
1483bcaabc55SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
14846eb61d58SRichard Weinberger 	struct ubifs_info *c = inode->i_sb->s_fs_info;
14856eb61d58SRichard Weinberger 
1486bcaabc55SMatthew Wilcox (Oracle) 	if (folio_test_writeback(folio))
1487bcaabc55SMatthew Wilcox (Oracle) 		return false;
148866f4742eSZhihao Cheng 
148966f4742eSZhihao Cheng 	/*
149066f4742eSZhihao Cheng 	 * Page is private but not dirty, weird? There is one condition
149166f4742eSZhihao Cheng 	 * making it happened. ubifs_writepage skipped the page because
149266f4742eSZhihao Cheng 	 * page index beyonds isize (for example. truncated by other
149366f4742eSZhihao Cheng 	 * process named A), then the page is invalidated by fadvise64
149466f4742eSZhihao Cheng 	 * syscall before being truncated by process A.
149566f4742eSZhihao Cheng 	 */
1496bcaabc55SMatthew Wilcox (Oracle) 	ubifs_assert(c, folio_test_private(folio));
149766f4742eSZhihao Cheng 	if (folio_test_checked(folio))
149866f4742eSZhihao Cheng 		release_new_page_budget(c);
149966f4742eSZhihao Cheng 	else
150066f4742eSZhihao Cheng 		release_existing_page_budget(c);
150166f4742eSZhihao Cheng 
150266f4742eSZhihao Cheng 	atomic_long_dec(&c->dirty_pg_cnt);
1503bcaabc55SMatthew Wilcox (Oracle) 	folio_detach_private(folio);
1504bcaabc55SMatthew Wilcox (Oracle) 	folio_clear_checked(folio);
1505bcaabc55SMatthew Wilcox (Oracle) 	return true;
15061e51764aSArtem Bityutskiy }
15071e51764aSArtem Bityutskiy 
15081e51764aSArtem Bityutskiy /*
1509c4361570SArtem Bityutskiy  * mmap()d file has taken write protection fault and is being made writable.
1510c4361570SArtem Bityutskiy  * UBIFS must ensure page is budgeted for.
15111e51764aSArtem Bityutskiy  */
ubifs_vm_page_mkwrite(struct vm_fault * vmf)151231c49eacSSouptick Joarder static vm_fault_t ubifs_vm_page_mkwrite(struct vm_fault *vmf)
15131e51764aSArtem Bityutskiy {
151485ffbf55SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
151511bac800SDave Jiang 	struct inode *inode = file_inode(vmf->vma->vm_file);
15161e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
15170eca0b80SArnd Bergmann 	struct timespec64 now = current_time(inode);
15181e51764aSArtem Bityutskiy 	struct ubifs_budget_req req = { .new_page = 1 };
15191e51764aSArtem Bityutskiy 	int err, update_time;
15201e51764aSArtem Bityutskiy 
152185ffbf55SMatthew Wilcox (Oracle) 	dbg_gen("ino %lu, pg %lu, i_size %lld",	inode->i_ino, folio->index,
15221e51764aSArtem Bityutskiy 		i_size_read(inode));
15236eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
15241e51764aSArtem Bityutskiy 
15252680d722SArtem Bityutskiy 	if (unlikely(c->ro_error))
1526c2ec175cSNick Piggin 		return VM_FAULT_SIGBUS; /* -EROFS */
15271e51764aSArtem Bityutskiy 
15281e51764aSArtem Bityutskiy 	/*
152985ffbf55SMatthew Wilcox (Oracle) 	 * We have not locked @folio so far so we may budget for changing the
153085ffbf55SMatthew Wilcox (Oracle) 	 * folio. Note, we cannot do this after we locked the folio, because
15311e51764aSArtem Bityutskiy 	 * budgeting may cause write-back which would cause deadlock.
15321e51764aSArtem Bityutskiy 	 *
153385ffbf55SMatthew Wilcox (Oracle) 	 * At the moment we do not know whether the folio is dirty or not, so we
153485ffbf55SMatthew Wilcox (Oracle) 	 * assume that it is not and budget for a new folio. We could look at
15351e51764aSArtem Bityutskiy 	 * the @PG_private flag and figure this out, but we may race with write
153685ffbf55SMatthew Wilcox (Oracle) 	 * back and the folio state may change by the time we lock it, so this
15371e51764aSArtem Bityutskiy 	 * would need additional care. We do not bother with this at the
15381e51764aSArtem Bityutskiy 	 * moment, although it might be good idea to do. Instead, we allocate
153985ffbf55SMatthew Wilcox (Oracle) 	 * budget for a new folio and amend it later on if the folio was in fact
15401e51764aSArtem Bityutskiy 	 * dirty.
15411e51764aSArtem Bityutskiy 	 *
15421e51764aSArtem Bityutskiy 	 * The budgeting-related logic of this function is similar to what we
15431e51764aSArtem Bityutskiy 	 * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
15441e51764aSArtem Bityutskiy 	 * for more comments.
15451e51764aSArtem Bityutskiy 	 */
15461e51764aSArtem Bityutskiy 	update_time = mctime_update_needed(inode, &now);
15471e51764aSArtem Bityutskiy 	if (update_time)
15481e51764aSArtem Bityutskiy 		/*
15491e51764aSArtem Bityutskiy 		 * We have to change inode time stamp which requires extra
15501e51764aSArtem Bityutskiy 		 * budgeting.
15511e51764aSArtem Bityutskiy 		 */
15521e51764aSArtem Bityutskiy 		req.dirtied_ino = 1;
15531e51764aSArtem Bityutskiy 
15541e51764aSArtem Bityutskiy 	err = ubifs_budget_space(c, &req);
15551e51764aSArtem Bityutskiy 	if (unlikely(err)) {
15561e51764aSArtem Bityutskiy 		if (err == -ENOSPC)
1557235c362bSSheng Yong 			ubifs_warn(c, "out of space for mmapped file (inode number %lu)",
155879fda517SArtem Bityutskiy 				   inode->i_ino);
1559c2ec175cSNick Piggin 		return VM_FAULT_SIGBUS;
15601e51764aSArtem Bityutskiy 	}
15611e51764aSArtem Bityutskiy 
156285ffbf55SMatthew Wilcox (Oracle) 	folio_lock(folio);
156385ffbf55SMatthew Wilcox (Oracle) 	if (unlikely(folio->mapping != inode->i_mapping ||
156485ffbf55SMatthew Wilcox (Oracle) 		     folio_pos(folio) >= i_size_read(inode))) {
156585ffbf55SMatthew Wilcox (Oracle) 		/* Folio got truncated out from underneath us */
156631c49eacSSouptick Joarder 		goto sigbus;
15671e51764aSArtem Bityutskiy 	}
15681e51764aSArtem Bityutskiy 
156985ffbf55SMatthew Wilcox (Oracle) 	if (folio->private)
15701e51764aSArtem Bityutskiy 		release_new_page_budget(c);
15711e51764aSArtem Bityutskiy 	else {
157285ffbf55SMatthew Wilcox (Oracle) 		if (!folio_test_checked(folio))
15731e51764aSArtem Bityutskiy 			ubifs_convert_page_budget(c);
157485ffbf55SMatthew Wilcox (Oracle) 		folio_attach_private(folio, (void *)1);
15751e51764aSArtem Bityutskiy 		atomic_long_inc(&c->dirty_pg_cnt);
157685ffbf55SMatthew Wilcox (Oracle) 		filemap_dirty_folio(folio->mapping, folio);
15771e51764aSArtem Bityutskiy 	}
15781e51764aSArtem Bityutskiy 
15791e51764aSArtem Bityutskiy 	if (update_time) {
15801e51764aSArtem Bityutskiy 		int release;
15811e51764aSArtem Bityutskiy 		struct ubifs_inode *ui = ubifs_inode(inode);
15821e51764aSArtem Bityutskiy 
15831e51764aSArtem Bityutskiy 		mutex_lock(&ui->ui_mutex);
1584e4cfef33SJeff Layton 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
15851e51764aSArtem Bityutskiy 		release = ui->dirty;
15861e51764aSArtem Bityutskiy 		mark_inode_dirty_sync(inode);
15871e51764aSArtem Bityutskiy 		mutex_unlock(&ui->ui_mutex);
15881e51764aSArtem Bityutskiy 		if (release)
15891e51764aSArtem Bityutskiy 			ubifs_release_dirty_inode_budget(c, ui);
15901e51764aSArtem Bityutskiy 	}
15911e51764aSArtem Bityutskiy 
159285ffbf55SMatthew Wilcox (Oracle) 	folio_wait_stable(folio);
1593691a7c6fShujianyang 	return VM_FAULT_LOCKED;
15941e51764aSArtem Bityutskiy 
159531c49eacSSouptick Joarder sigbus:
159685ffbf55SMatthew Wilcox (Oracle) 	folio_unlock(folio);
15971e51764aSArtem Bityutskiy 	ubifs_release_budget(c, &req);
159831c49eacSSouptick Joarder 	return VM_FAULT_SIGBUS;
15991e51764aSArtem Bityutskiy }
16001e51764aSArtem Bityutskiy 
1601f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct ubifs_file_vm_ops = {
16021e51764aSArtem Bityutskiy 	.fault        = filemap_fault,
1603f1820361SKirill A. Shutemov 	.map_pages = filemap_map_pages,
16041e51764aSArtem Bityutskiy 	.page_mkwrite = ubifs_vm_page_mkwrite,
16051e51764aSArtem Bityutskiy };
16061e51764aSArtem Bityutskiy 
ubifs_file_mmap(struct file * file,struct vm_area_struct * vma)16071e51764aSArtem Bityutskiy static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
16081e51764aSArtem Bityutskiy {
16091e51764aSArtem Bityutskiy 	int err;
16101e51764aSArtem Bityutskiy 
16111e51764aSArtem Bityutskiy 	err = generic_file_mmap(file, vma);
16121e51764aSArtem Bityutskiy 	if (err)
16131e51764aSArtem Bityutskiy 		return err;
16141e51764aSArtem Bityutskiy 	vma->vm_ops = &ubifs_file_vm_ops;
1615e3d73deaSSascha Hauer 
1616e3d73deaSSascha Hauer 	if (IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
16178c1c5f26SDongsheng Yang 		file_accessed(file);
1618e3d73deaSSascha Hauer 
16191e51764aSArtem Bityutskiy 	return 0;
16201e51764aSArtem Bityutskiy }
16211e51764aSArtem Bityutskiy 
ubifs_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1622ca7f85beSRichard Weinberger static const char *ubifs_get_link(struct dentry *dentry,
1623ca7f85beSRichard Weinberger 					    struct inode *inode,
1624ca7f85beSRichard Weinberger 					    struct delayed_call *done)
1625ca7f85beSRichard Weinberger {
1626ca7f85beSRichard Weinberger 	struct ubifs_inode *ui = ubifs_inode(inode);
1627ca7f85beSRichard Weinberger 
162881dd76b2SEric Biggers 	if (!IS_ENCRYPTED(inode))
1629ca7f85beSRichard Weinberger 		return ui->data;
1630ca7f85beSRichard Weinberger 
1631ca7f85beSRichard Weinberger 	if (!dentry)
1632ca7f85beSRichard Weinberger 		return ERR_PTR(-ECHILD);
1633ca7f85beSRichard Weinberger 
163481dd76b2SEric Biggers 	return fscrypt_get_symlink(inode, ui->data, ui->data_len, done);
1635ca7f85beSRichard Weinberger }
1636ca7f85beSRichard Weinberger 
ubifs_symlink_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)1637b74d24f7SChristian Brauner static int ubifs_symlink_getattr(struct mnt_idmap *idmap,
1638064c7349SEric Biggers 				 const struct path *path, struct kstat *stat,
1639064c7349SEric Biggers 				 u32 request_mask, unsigned int query_flags)
1640064c7349SEric Biggers {
1641b74d24f7SChristian Brauner 	ubifs_getattr(idmap, path, stat, request_mask, query_flags);
1642064c7349SEric Biggers 
1643064c7349SEric Biggers 	if (IS_ENCRYPTED(d_inode(path->dentry)))
1644064c7349SEric Biggers 		return fscrypt_symlink_getattr(path, stat);
1645064c7349SEric Biggers 	return 0;
1646064c7349SEric Biggers }
1647064c7349SEric Biggers 
1648e8b81566SArtem Bityutskiy const struct address_space_operations ubifs_file_address_operations = {
16490b7bf483SMatthew Wilcox (Oracle) 	.read_folio     = ubifs_read_folio,
16500df030d0SMatthew Wilcox (Oracle) 	.writepages     = ubifs_writepages,
16511e51764aSArtem Bityutskiy 	.write_begin    = ubifs_write_begin,
16521e51764aSArtem Bityutskiy 	.write_end      = ubifs_write_end,
165358a2fdb6SMatthew Wilcox (Oracle) 	.invalidate_folio = ubifs_invalidate_folio,
16541f1d14dbSMatthew Wilcox (Oracle) 	.dirty_folio	= ubifs_dirty_folio,
1655e7b15baeSMatthew Wilcox (Oracle) 	.migrate_folio	= filemap_migrate_folio,
1656bcaabc55SMatthew Wilcox (Oracle) 	.release_folio	= ubifs_release_folio,
16571e51764aSArtem Bityutskiy };
16581e51764aSArtem Bityutskiy 
1659e8b81566SArtem Bityutskiy const struct inode_operations ubifs_file_inode_operations = {
16601e51764aSArtem Bityutskiy 	.setattr     = ubifs_setattr,
16611e51764aSArtem Bityutskiy 	.getattr     = ubifs_getattr,
16621e51764aSArtem Bityutskiy 	.listxattr   = ubifs_listxattr,
16638c1c5f26SDongsheng Yang 	.update_time = ubifs_update_time,
16648871d84cSMiklos Szeredi 	.fileattr_get = ubifs_fileattr_get,
16658871d84cSMiklos Szeredi 	.fileattr_set = ubifs_fileattr_set,
16661e51764aSArtem Bityutskiy };
16671e51764aSArtem Bityutskiy 
1668e8b81566SArtem Bityutskiy const struct inode_operations ubifs_symlink_inode_operations = {
1669ca7f85beSRichard Weinberger 	.get_link    = ubifs_get_link,
16701e51764aSArtem Bityutskiy 	.setattr     = ubifs_setattr,
1671064c7349SEric Biggers 	.getattr     = ubifs_symlink_getattr,
1672895d9db2SSubodh Nijsure 	.listxattr   = ubifs_listxattr,
16738c1c5f26SDongsheng Yang 	.update_time = ubifs_update_time,
16741e51764aSArtem Bityutskiy };
16751e51764aSArtem Bityutskiy 
1676e8b81566SArtem Bityutskiy const struct file_operations ubifs_file_operations = {
16771e51764aSArtem Bityutskiy 	.llseek         = generic_file_llseek,
1678aad4f8bbSAl Viro 	.read_iter      = generic_file_read_iter,
1679f5674c31SAl Viro 	.write_iter     = ubifs_write_iter,
16801e51764aSArtem Bityutskiy 	.mmap           = ubifs_file_mmap,
16811e51764aSArtem Bityutskiy 	.fsync          = ubifs_fsync,
16821e51764aSArtem Bityutskiy 	.unlocked_ioctl = ubifs_ioctl,
16832cb1e089SDavid Howells 	.splice_read	= filemap_splice_read,
16848d020765SAl Viro 	.splice_write	= iter_file_splice_write,
16857e35c4daSEric Biggers 	.open		= fscrypt_file_open,
16861e51764aSArtem Bityutskiy #ifdef CONFIG_COMPAT
16871e51764aSArtem Bityutskiy 	.compat_ioctl   = ubifs_compat_ioctl,
16881e51764aSArtem Bityutskiy #endif
16891e51764aSArtem Bityutskiy };
1690