Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/fs/affs/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  (c) 1996  Hans-Joachim Widmaier - Rewritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  (C) 1993  Ray Burr - Modified for Amiga FFS filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  (C) 1992  Eric Youngdale Modified for ISO 9660 filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  (C) 1991  Linus Torvalds - minix filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *  affs regular file handling primitives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "affs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) static struct buffer_head *affs_get_extblock_slow(struct inode *inode, u32 ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) affs_file_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	pr_debug("open(%lu,%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 		 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	atomic_inc(&AFFS_I(inode)->i_opencnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) affs_file_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	pr_debug("release(%lu, %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 		 inode->i_ino, atomic_read(&AFFS_I(inode)->i_opencnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	if (atomic_dec_and_test(&AFFS_I(inode)->i_opencnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 		if (inode->i_size != AFFS_I(inode)->mmu_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 			affs_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 		affs_free_prealloc(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) affs_grow_extcache(struct inode *inode, u32 lc_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	struct super_block	*sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	struct buffer_head	*bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	u32 lc_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	int i, j, key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	if (!AFFS_I(inode)->i_lc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		char *ptr = (char *)get_zeroed_page(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		AFFS_I(inode)->i_lc = (u32 *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		AFFS_I(inode)->i_ac = (struct affs_ext_key *)(ptr + AFFS_CACHE_SIZE / 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	lc_max = AFFS_LC_SIZE << AFFS_I(inode)->i_lc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	if (AFFS_I(inode)->i_extcnt > lc_max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		u32 lc_shift, lc_mask, tmp, off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		/* need to recalculate linear cache, start from old size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 		lc_shift = AFFS_I(inode)->i_lc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		tmp = (AFFS_I(inode)->i_extcnt / AFFS_LC_SIZE) >> lc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		for (; tmp; tmp >>= 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 			lc_shift++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		lc_mask = (1 << lc_shift) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		/* fix idx and old size to new shift */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		lc_idx >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		AFFS_I(inode)->i_lc_size >>= (lc_shift - AFFS_I(inode)->i_lc_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		/* first shrink old cache to make more space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		off = 1 << (lc_shift - AFFS_I(inode)->i_lc_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		for (i = 1, j = off; j < AFFS_LC_SIZE; i++, j += off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			AFFS_I(inode)->i_ac[i] = AFFS_I(inode)->i_ac[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		AFFS_I(inode)->i_lc_shift = lc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		AFFS_I(inode)->i_lc_mask = lc_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	/* fill cache to the needed index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	i = AFFS_I(inode)->i_lc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	AFFS_I(inode)->i_lc_size = lc_idx + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	for (; i <= lc_idx; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		if (!i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 			AFFS_I(inode)->i_lc[0] = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		key = AFFS_I(inode)->i_lc[i - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		j = AFFS_I(inode)->i_lc_mask + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		// unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		for (; j > 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			bh = affs_bread(sb, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 			key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 			affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		// lock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		AFFS_I(inode)->i_lc[i] = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	// lock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) affs_alloc_extblock(struct inode *inode, struct buffer_head *bh, u32 ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct buffer_head *new_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	u32 blocknr, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	blocknr = affs_alloc_block(inode, bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	if (!blocknr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		return ERR_PTR(-ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	new_bh = affs_getzeroblk(sb, blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	if (!new_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		affs_free_block(sb, blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	AFFS_HEAD(new_bh)->ptype = cpu_to_be32(T_LIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	AFFS_HEAD(new_bh)->key = cpu_to_be32(blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	AFFS_TAIL(sb, new_bh)->stype = cpu_to_be32(ST_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	AFFS_TAIL(sb, new_bh)->parent = cpu_to_be32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	affs_fix_checksum(sb, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	mark_buffer_dirty_inode(new_bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	tmp = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		affs_warning(sb, "alloc_ext", "previous extension set (%x)", tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	AFFS_TAIL(sb, bh)->extension = cpu_to_be32(blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	affs_adjust_checksum(bh, blocknr - tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	AFFS_I(inode)->i_extcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	return new_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) static inline struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) affs_get_extblock(struct inode *inode, u32 ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	/* inline the simplest case: same extended block as last time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct buffer_head *bh = AFFS_I(inode)->i_ext_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	if (ext == AFFS_I(inode)->i_ext_last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		/* we have to do more (not inlined) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		bh = affs_get_extblock_slow(inode, ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) affs_get_extblock_slow(struct inode *inode, u32 ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	u32 ext_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	u32 lc_idx, lc_off, ac_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	u32 tmp, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	if (ext == AFFS_I(inode)->i_ext_last + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		/* read the next extended block from the current one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		bh = AFFS_I(inode)->i_ext_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		if (ext < AFFS_I(inode)->i_extcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			goto read_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		BUG_ON(ext > AFFS_I(inode)->i_extcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		bh = affs_alloc_extblock(inode, bh, ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		goto store_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (ext == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		/* we seek back to the file header block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		ext_key = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		goto read_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	if (ext >= AFFS_I(inode)->i_extcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		struct buffer_head *prev_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		/* allocate a new extended block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		BUG_ON(ext > AFFS_I(inode)->i_extcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		/* get previous extended block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		prev_bh = affs_get_extblock(inode, ext - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		if (IS_ERR(prev_bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			return prev_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		bh = affs_alloc_extblock(inode, prev_bh, ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		affs_brelse(prev_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		goto store_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	/* check if there is an extended cache and whether it's large enough */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	lc_idx = ext >> AFFS_I(inode)->i_lc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	lc_off = ext & AFFS_I(inode)->i_lc_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	if (lc_idx >= AFFS_I(inode)->i_lc_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		err = affs_grow_extcache(inode, lc_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	/* every n'th key we find in the linear cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (!lc_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		ext_key = AFFS_I(inode)->i_lc[lc_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		goto read_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	/* maybe it's still in the associative cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	ac_idx = (ext - lc_idx - 1) & AFFS_AC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (AFFS_I(inode)->i_ac[ac_idx].ext == ext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		ext_key = AFFS_I(inode)->i_ac[ac_idx].key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		goto read_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	/* try to find one of the previous extended blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	tmp = ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	idx = ac_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	while (--tmp, --lc_off > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		idx = (idx - 1) & AFFS_AC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		if (AFFS_I(inode)->i_ac[idx].ext == tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			ext_key = AFFS_I(inode)->i_ac[idx].key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 			goto find_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	/* fall back to the linear cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	ext_key = AFFS_I(inode)->i_lc[lc_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) find_ext:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	/* read all extended blocks until we find the one we need */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	//unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		bh = affs_bread(sb, ext_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 			goto err_bread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		ext_key = be32_to_cpu(AFFS_TAIL(sb, bh)->extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		tmp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	} while (tmp < ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	//lock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	/* store it in the associative cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	// recalculate ac_idx?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	AFFS_I(inode)->i_ac[ac_idx].ext = ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	AFFS_I(inode)->i_ac[ac_idx].key = ext_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) read_ext:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	/* finally read the right extended block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	//unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	bh = affs_bread(sb, ext_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		goto err_bread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	//lock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) store_ext:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	/* release old cached extended block and store the new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	affs_brelse(AFFS_I(inode)->i_ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	AFFS_I(inode)->i_ext_last = ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	AFFS_I(inode)->i_ext_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) err_bread:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) affs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	struct super_block	*sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct buffer_head	*ext_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	u32			 ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	pr_debug("%s(%lu, %llu)\n", __func__, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		 (unsigned long long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	BUG_ON(block > (sector_t)0x7fffffffUL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	if (block >= AFFS_I(inode)->i_blkcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		if (block > AFFS_I(inode)->i_blkcnt || !create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			goto err_big;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		create = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	//lock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	affs_lock_ext(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	ext = (u32)block / AFFS_SB(sb)->s_hashsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	block -= ext * AFFS_SB(sb)->s_hashsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	ext_bh = affs_get_extblock(inode, ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (IS_ERR(ext_bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		goto err_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	map_bh(bh_result, sb, (sector_t)be32_to_cpu(AFFS_BLOCK(sb, ext_bh, block)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	if (create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		u32 blocknr = affs_alloc_block(inode, ext_bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		if (!blocknr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		set_buffer_new(bh_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		AFFS_I(inode)->mmu_private += AFFS_SB(sb)->s_data_blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		AFFS_I(inode)->i_blkcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		/* store new block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		if (bh_result->b_blocknr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 			affs_warning(sb, "get_block",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 				     "block already set (%llx)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 				     (unsigned long long)bh_result->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		AFFS_BLOCK(sb, ext_bh, block) = cpu_to_be32(blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(block + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		affs_adjust_checksum(ext_bh, blocknr - bh_result->b_blocknr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		bh_result->b_blocknr = blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		if (!block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			/* insert first block into header block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			u32 tmp = be32_to_cpu(AFFS_HEAD(ext_bh)->first_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 			if (tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 				affs_warning(sb, "get_block", "first block already set (%d)", tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 			AFFS_HEAD(ext_bh)->first_data = cpu_to_be32(blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 			affs_adjust_checksum(ext_bh, blocknr - tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	affs_brelse(ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	//unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	affs_unlock_ext(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) err_big:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	affs_error(inode->i_sb, "get_block", "strange block request %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		   (unsigned long long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) err_ext:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	// unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	affs_unlock_ext(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	return PTR_ERR(ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	brelse(ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	clear_buffer_mapped(bh_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	bh_result->b_bdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	// unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	affs_unlock_ext(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) static int affs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	return block_write_full_page(page, affs_get_block, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) static int affs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	return block_read_full_page(page, affs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) static void affs_write_failed(struct address_space *mapping, loff_t to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if (to > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		truncate_pagecache(inode, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		affs_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) affs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	struct address_space *mapping = file->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	size_t count = iov_iter_count(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	loff_t offset = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	if (iov_iter_rw(iter) == WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		loff_t size = offset + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		if (AFFS_I(inode)->mmu_private < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	ret = blockdev_direct_IO(iocb, inode, iter, affs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (ret < 0 && iov_iter_rw(iter) == WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		affs_write_failed(mapping, offset + count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) static int affs_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 			struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	*pagep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 				affs_get_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 				&AFFS_I(mapping->host)->mmu_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		affs_write_failed(mapping, pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) static int affs_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			  loff_t pos, unsigned int len, unsigned int copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			  struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	/* Clear Archived bit on file writes, as AmigaOS would do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	if (AFFS_I(inode)->i_protect & FIBF_ARCHIVED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		AFFS_I(inode)->i_protect &= ~FIBF_ARCHIVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) static sector_t _affs_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	return generic_block_bmap(mapping,block,affs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) const struct address_space_operations affs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	.readpage = affs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	.writepage = affs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	.write_begin = affs_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	.write_end = affs_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	.direct_IO = affs_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	.bmap = _affs_bmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) static inline struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) affs_bread_ino(struct inode *inode, int block, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	struct buffer_head *bh, tmp_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	tmp_bh.b_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	err = affs_get_block(inode, block, &tmp_bh, create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		bh = affs_bread(inode->i_sb, tmp_bh.b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		if (bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			bh->b_state |= tmp_bh.b_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static inline struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) affs_getzeroblk_ino(struct inode *inode, int block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	struct buffer_head *bh, tmp_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	tmp_bh.b_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	err = affs_get_block(inode, block, &tmp_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		bh = affs_getzeroblk(inode->i_sb, tmp_bh.b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		if (bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			bh->b_state |= tmp_bh.b_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) static inline struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) affs_getemptyblk_ino(struct inode *inode, int block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct buffer_head *bh, tmp_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	tmp_bh.b_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	err = affs_get_block(inode, block, &tmp_bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		bh = affs_getemptyblk(inode->i_sb, tmp_bh.b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		if (bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			bh->b_state |= tmp_bh.b_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) affs_do_readpage_ofs(struct page *page, unsigned to, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	unsigned pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	u32 bidx, boff, bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	pr_debug("%s(%lu, %ld, 0, %d)\n", __func__, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		 page->index, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	BUG_ON(to > PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	bsize = AFFS_SB(sb)->s_data_blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	tmp = page->index << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	bidx = tmp / bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	boff = tmp % bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	while (pos < to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		bh = affs_bread_ino(inode, bidx, create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		tmp = min(bsize - boff, to - pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		BUG_ON(pos + tmp > to || tmp > bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		data = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		memcpy(data + pos, AFFS_DATA(bh) + boff, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		kunmap_atomic(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		bidx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		pos += tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		boff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) affs_extent_file_ofs(struct inode *inode, u32 newsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	struct buffer_head *bh, *prev_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	u32 bidx, boff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	u32 size, bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	pr_debug("%s(%lu, %d)\n", __func__, inode->i_ino, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	bsize = AFFS_SB(sb)->s_data_blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	size = AFFS_I(inode)->mmu_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	bidx = size / bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	boff = size % bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	if (boff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		bh = affs_bread_ino(inode, bidx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		tmp = min(bsize - boff, newsize - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		BUG_ON(boff + tmp > bsize || tmp > bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		memset(AFFS_DATA(bh) + boff, 0, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		size += tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		bidx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	} else if (bidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		bh = affs_bread_ino(inode, bidx - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	while (size < newsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		prev_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		bh = affs_getzeroblk_ino(inode, bidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		tmp = min(bsize, newsize - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		BUG_ON(tmp > bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		bh->b_state &= ~(1UL << BH_New);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		if (prev_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			if (tmp_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 				affs_warning(sb, "extent_file_ofs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 					     "next block already set for %d (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 					     bidx, tmp_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			mark_buffer_dirty_inode(prev_bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			affs_brelse(prev_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		size += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		bidx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	inode->i_size = AFFS_I(inode)->mmu_private = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	inode->i_size = AFFS_I(inode)->mmu_private = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) affs_readpage_ofs(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	u32 to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	pr_debug("%s(%lu, %ld)\n", __func__, inode->i_ino, page->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	to = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	if (((page->index + 1) << PAGE_SHIFT) > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		to = inode->i_size & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		memset(page_address(page) + to, 0, PAGE_SIZE - to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	err = affs_do_readpage_ofs(page, to, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) static int affs_write_begin_ofs(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 				loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	pr_debug("%s(%lu, %llu, %llu)\n", __func__, inode->i_ino, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		 pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (pos > AFFS_I(inode)->mmu_private) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		/* XXX: this probably leaves a too-big i_size in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		 * failure. Should really be updating i_size at write_end time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		err = affs_extent_file_ofs(inode, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	if (PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	/* XXX: inefficient but safe in the face of short writes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	err = affs_do_readpage_ofs(page, PAGE_SIZE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) static int affs_write_end_ofs(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 				loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 				struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct buffer_head *bh, *prev_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	u32 bidx, boff, bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	unsigned from, to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	int written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	from = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	to = from + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * XXX: not sure if this can handle short copies (len < copied), but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 * we don't have to, because the page should always be uptodate here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	 * due to write_begin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	pr_debug("%s(%lu, %llu, %llu)\n", __func__, inode->i_ino, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		 pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	bsize = AFFS_SB(sb)->s_data_blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	data = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	tmp = (page->index << PAGE_SHIFT) + from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	bidx = tmp / bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	boff = tmp % bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (boff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		bh = affs_bread_ino(inode, bidx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			written = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			goto err_first_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		tmp = min(bsize - boff, to - from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		BUG_ON(boff + tmp > bsize || tmp > bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		memcpy(AFFS_DATA(bh) + boff, data + from, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		be32_add_cpu(&AFFS_DATA_HEAD(bh)->size, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		written += tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		from += tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		bidx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	} else if (bidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		bh = affs_bread_ino(inode, bidx - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			written = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			goto err_first_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	while (from + bsize <= to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		prev_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		bh = affs_getemptyblk_ino(inode, bidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 			goto err_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		memcpy(AFFS_DATA(bh), data + from, bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		if (buffer_new(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			AFFS_DATA_HEAD(bh)->size = cpu_to_be32(bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			AFFS_DATA_HEAD(bh)->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			bh->b_state &= ~(1UL << BH_New);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			if (prev_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 				u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 				if (tmp_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 					affs_warning(sb, "commit_write_ofs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 						     "next block already set for %d (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 						     bidx, tmp_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 				AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 				affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 				mark_buffer_dirty_inode(prev_bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		affs_brelse(prev_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		written += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		from += bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		bidx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (from < to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		prev_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		bh = affs_bread_ino(inode, bidx, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			goto err_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		tmp = min(bsize, to - from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		BUG_ON(tmp > bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		memcpy(AFFS_DATA(bh), data + from, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		if (buffer_new(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			AFFS_DATA_HEAD(bh)->ptype = cpu_to_be32(T_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			AFFS_DATA_HEAD(bh)->key = cpu_to_be32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			AFFS_DATA_HEAD(bh)->sequence = cpu_to_be32(bidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			AFFS_DATA_HEAD(bh)->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			bh->b_state &= ~(1UL << BH_New);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 			if (prev_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 				u32 tmp_next = be32_to_cpu(AFFS_DATA_HEAD(prev_bh)->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 				if (tmp_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 					affs_warning(sb, "commit_write_ofs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 						     "next block already set for %d (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 						     bidx, tmp_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 				AFFS_DATA_HEAD(prev_bh)->next = cpu_to_be32(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 				affs_adjust_checksum(prev_bh, bh->b_blocknr - tmp_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 				mark_buffer_dirty_inode(prev_bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		} else if (be32_to_cpu(AFFS_DATA_HEAD(bh)->size) < tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			AFFS_DATA_HEAD(bh)->size = cpu_to_be32(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		affs_brelse(prev_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		written += tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		from += tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		bidx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	tmp = (page->index << PAGE_SHIFT) + from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (tmp > inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		inode->i_size = AFFS_I(inode)->mmu_private = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	/* Clear Archived bit on file writes, as AmigaOS would do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	if (AFFS_I(inode)->i_protect & FIBF_ARCHIVED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		AFFS_I(inode)->i_protect &= ~FIBF_ARCHIVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) err_first_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) err_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	bh = prev_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (!written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		written = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) const struct address_space_operations affs_aops_ofs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	.readpage = affs_readpage_ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	//.writepage = affs_writepage_ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	.write_begin = affs_write_begin_ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	.write_end = affs_write_end_ofs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) /* Free any preallocated blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) affs_free_prealloc(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	pr_debug("free_prealloc(ino=%lu)\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	while (AFFS_I(inode)->i_pa_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		AFFS_I(inode)->i_pa_cnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		affs_free_block(sb, ++AFFS_I(inode)->i_lastalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) /* Truncate (or enlarge) a file to the requested size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) affs_truncate(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	u32 ext, ext_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	u32 last_blk, blkcnt, blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	struct buffer_head *ext_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	pr_debug("truncate(inode=%lu, oldsize=%llu, newsize=%llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		 inode->i_ino, AFFS_I(inode)->mmu_private, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	last_blk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	ext = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	if (inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		last_blk = ((u32)inode->i_size - 1) / AFFS_SB(sb)->s_data_blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		ext = last_blk / AFFS_SB(sb)->s_hashsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	if (inode->i_size > AFFS_I(inode)->mmu_private) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		void *fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		loff_t isize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		res = mapping->a_ops->write_begin(NULL, mapping, isize, 0, 0, &page, &fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			res = mapping->a_ops->write_end(NULL, mapping, isize, 0, 0, page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			inode->i_size = AFFS_I(inode)->mmu_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	} else if (inode->i_size == AFFS_I(inode)->mmu_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	// lock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	ext_bh = affs_get_extblock(inode, ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (IS_ERR(ext_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		affs_warning(sb, "truncate",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			     "unexpected read error for ext block %u (%ld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 			     ext, PTR_ERR(ext_bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	if (AFFS_I(inode)->i_lc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		/* clear linear cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		i = (ext + 1) >> AFFS_I(inode)->i_lc_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		if (AFFS_I(inode)->i_lc_size > i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 			AFFS_I(inode)->i_lc_size = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 			for (; i < AFFS_LC_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 				AFFS_I(inode)->i_lc[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		/* clear associative cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		for (i = 0; i < AFFS_AC_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			if (AFFS_I(inode)->i_ac[i].ext >= ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 				AFFS_I(inode)->i_ac[i].ext = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	blkcnt = AFFS_I(inode)->i_blkcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	blk = last_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		i = last_blk % AFFS_SB(sb)->s_hashsize + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		blk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		AFFS_HEAD(ext_bh)->first_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	AFFS_HEAD(ext_bh)->block_count = cpu_to_be32(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	size = AFFS_SB(sb)->s_hashsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	if (size > blkcnt - blk + i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		size = blkcnt - blk + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	for (; i < size; i++, blk++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		AFFS_BLOCK(sb, ext_bh, i) = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	AFFS_TAIL(sb, ext_bh)->extension = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	affs_fix_checksum(sb, ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	mark_buffer_dirty_inode(ext_bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	affs_brelse(ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		AFFS_I(inode)->i_blkcnt = last_blk + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		AFFS_I(inode)->i_extcnt = ext + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			struct buffer_head *bh = affs_bread_ino(inode, last_blk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				affs_warning(sb, "truncate",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 					     "unexpected read error for last block %u (%ld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 					     ext, PTR_ERR(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			tmp = be32_to_cpu(AFFS_DATA_HEAD(bh)->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			AFFS_DATA_HEAD(bh)->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			affs_adjust_checksum(bh, -tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 			affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		AFFS_I(inode)->i_blkcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		AFFS_I(inode)->i_extcnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	AFFS_I(inode)->mmu_private = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	// unlock cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	while (ext_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		ext_bh = affs_bread(sb, ext_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		size = AFFS_SB(sb)->s_hashsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (size > blkcnt - blk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			size = blkcnt - blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		for (i = 0; i < size; i++, blk++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			affs_free_block(sb, be32_to_cpu(AFFS_BLOCK(sb, ext_bh, i)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		affs_free_block(sb, ext_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		ext_key = be32_to_cpu(AFFS_TAIL(sb, ext_bh)->extension);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		affs_brelse(ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	affs_free_prealloc(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) int affs_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	struct inode *inode = filp->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	int ret, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	err = file_write_and_wait_range(filp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	ret = write_inode_now(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	err = sync_blockdev(inode->i_sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) const struct file_operations affs_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	.read_iter	= generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	.write_iter	= generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	.mmap		= generic_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	.open		= affs_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	.release	= affs_file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	.fsync		= affs_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	.splice_read	= generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) const struct inode_operations affs_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	.setattr	= affs_notify_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) };