^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) * fs/bfs/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * BFS file operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1999-2018 Tigran Aivazian <aivazian.tigran@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Make the file block allocation algorithm understand the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * of the underlying block device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "bfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #undef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define dprintf(x...) printf(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define dprintf(x...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) const struct file_operations bfs_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .read_iter = generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .write_iter = generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .mmap = generic_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int bfs_move_block(unsigned long from, unsigned long to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct buffer_head *bh, *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bh = sb_bread(sb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) new = sb_getblk(sb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) memcpy(new->b_data, bh->b_data, bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) mark_buffer_dirty(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) bforget(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) brelse(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int bfs_move_blocks(struct super_block *sb, unsigned long start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned long end, unsigned long where)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) dprintf("%08lx-%08lx->%08lx\n", start, end, where);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) for (i = start; i <= end; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if(bfs_move_block(i, where + i, sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) dprintf("failed to move block %08lx -> %08lx\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) where + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^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) static int bfs_get_block(struct inode *inode, sector_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned long phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct bfs_sb_info *info = BFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct bfs_inode_info *bi = BFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) phys = bi->i_sblock + block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (phys <= bi->i_eblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) create, (unsigned long)block, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * If the file is not empty and the requested block is within the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * range of blocks allocated for this file, we can grant it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (bi->i_sblock && (phys <= bi->i_eblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) create, (unsigned long)block, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* The file will be extended, so let's see if there is enough space. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (phys >= info->si_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* The rest has to be protected against itself. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) mutex_lock(&info->bfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * If the last data block for this file is the last allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * block, we can extend the file trivially, without moving it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * anywhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (bi->i_eblock == info->si_lf_eblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) create, (unsigned long)block, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) info->si_freeb -= phys - bi->i_eblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) info->si_lf_eblk = bi->i_eblock = phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* Ok, we have to move this entire file to the next free block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) phys = info->si_lf_eblk + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (phys + block >= info->si_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (bi->i_sblock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) bi->i_eblock, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dprintf("failed to move ino=%08lx -> fs corruption\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) create, (unsigned long)block, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) bi->i_sblock = phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) phys += block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) info->si_lf_eblk = bi->i_eblock = phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * This assumes nothing can write the inode back while we are here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * and thus update inode->i_blocks! (XXX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mutex_unlock(&info->bfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int bfs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return block_write_full_page(page, bfs_get_block, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int bfs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return block_read_full_page(page, bfs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void bfs_write_failed(struct address_space *mapping, loff_t to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (to > inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) truncate_pagecache(inode, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int bfs_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) ret = block_write_begin(mapping, pos, len, flags, pagep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) bfs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) bfs_write_failed(mapping, pos + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return generic_block_bmap(mapping, block, bfs_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) const struct address_space_operations bfs_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .readpage = bfs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .writepage = bfs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .write_begin = bfs_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .write_end = generic_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .bmap = bfs_bmap,
^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) const struct inode_operations bfs_file_inops;