blob: 1476cdd90cfbb14a1d53b131ec7bef84b40b1c3b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * fs/bfs/file.c
4 * BFS file operations.
5 * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
Dmitri Vorobievf433dc52007-11-14 16:59:47 -08006 *
7 * Make the file block allocation algorithm understand the size
8 * of the underlying block device.
9 * Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com>
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/fs.h>
14#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "bfs.h"
16
17#undef DEBUG
18
19#ifdef DEBUG
20#define dprintf(x...) printf(x)
21#else
22#define dprintf(x...)
23#endif
24
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080025const struct file_operations bfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -040027 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -040028 .write_iter = generic_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .mmap = generic_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +020030 .splice_read = generic_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080033static int bfs_move_block(unsigned long from, unsigned long to,
34 struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 struct buffer_head *bh, *new;
37
38 bh = sb_bread(sb, from);
39 if (!bh)
40 return -EIO;
41 new = sb_getblk(sb, to);
42 memcpy(new->b_data, bh->b_data, bh->b_size);
43 mark_buffer_dirty(new);
44 bforget(bh);
45 brelse(new);
46 return 0;
47}
48
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070049static int bfs_move_blocks(struct super_block *sb, unsigned long start,
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080050 unsigned long end, unsigned long where)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 unsigned long i;
53
54 dprintf("%08lx-%08lx->%08lx\n", start, end, where);
55 for (i = start; i <= end; i++)
56 if(bfs_move_block(i, where + i, sb)) {
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080057 dprintf("failed to move block %08lx -> %08lx\n", i,
58 where + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -EIO;
60 }
61 return 0;
62}
63
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080064static int bfs_get_block(struct inode *inode, sector_t block,
65 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070067 unsigned long phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int err;
69 struct super_block *sb = inode->i_sb;
70 struct bfs_sb_info *info = BFS_SB(sb);
71 struct bfs_inode_info *bi = BFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 phys = bi->i_sblock + block;
74 if (!create) {
75 if (phys <= bi->i_eblock) {
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070076 dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n",
77 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 map_bh(bh_result, sb, phys);
79 }
80 return 0;
81 }
82
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080083 /*
84 * If the file is not empty and the requested block is within the
85 * range of blocks allocated for this file, we can grant it.
86 */
87 if (bi->i_sblock && (phys <= bi->i_eblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n",
Andrew Stribblehillfac92be2005-09-09 13:02:04 -070089 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 map_bh(bh_result, sb, phys);
91 return 0;
92 }
93
Dmitri Vorobievf433dc52007-11-14 16:59:47 -080094 /* The file will be extended, so let's see if there is enough space. */
95 if (phys >= info->si_blocks)
96 return -ENOSPC;
97
98 /* The rest has to be protected against itself. */
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -070099 mutex_lock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800101 /*
102 * If the last data block for this file is the last allocated
103 * block, we can extend the file trivially, without moving it
104 * anywhere.
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (bi->i_eblock == info->si_lf_eblk) {
107 dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n",
Andrew Stribblehillfac92be2005-09-09 13:02:04 -0700108 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 map_bh(bh_result, sb, phys);
110 info->si_freeb -= phys - bi->i_eblock;
111 info->si_lf_eblk = bi->i_eblock = phys;
112 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 err = 0;
114 goto out;
115 }
116
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800117 /* Ok, we have to move this entire file to the next free block. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 phys = info->si_lf_eblk + 1;
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800119 if (phys + block >= info->si_blocks) {
120 err = -ENOSPC;
121 goto out;
122 }
123
124 if (bi->i_sblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 err = bfs_move_blocks(inode->i_sb, bi->i_sblock,
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800126 bi->i_eblock, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (err) {
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800128 dprintf("failed to move ino=%08lx -> fs corruption\n",
129 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto out;
131 }
132 } else
133 err = 0;
134
Andrew Stribblehillfac92be2005-09-09 13:02:04 -0700135 dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n",
136 create, (unsigned long)block, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 bi->i_sblock = phys;
138 phys += block;
139 info->si_lf_eblk = bi->i_eblock = phys;
140
Dmitri Vorobievf433dc52007-11-14 16:59:47 -0800141 /*
142 * This assumes nothing can write the inode back while we are here
143 * and thus update inode->i_blocks! (XXX)
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks;
146 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 map_bh(bh_result, sb, phys);
148out:
Dmitri Vorobiev3f165e42008-07-25 19:44:54 -0700149 mutex_unlock(&info->bfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return err;
151}
152
153static int bfs_writepage(struct page *page, struct writeback_control *wbc)
154{
155 return block_write_full_page(page, bfs_get_block, wbc);
156}
157
158static int bfs_readpage(struct file *file, struct page *page)
159{
160 return block_read_full_page(page, bfs_get_block);
161}
162
Marco Stornelli41ddaee2012-12-15 11:52:33 +0100163static void bfs_write_failed(struct address_space *mapping, loff_t to)
164{
165 struct inode *inode = mapping->host;
166
167 if (to > inode->i_size)
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700168 truncate_pagecache(inode, inode->i_size);
Marco Stornelli41ddaee2012-12-15 11:52:33 +0100169}
170
Nick Piggineedcbba2007-10-16 01:25:11 -0700171static int bfs_write_begin(struct file *file, struct address_space *mapping,
172 loff_t pos, unsigned len, unsigned flags,
173 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200175 int ret;
176
177 ret = block_write_begin(mapping, pos, len, flags, pagep,
178 bfs_get_block);
Marco Stornelli41ddaee2012-12-15 11:52:33 +0100179 if (unlikely(ret))
180 bfs_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +0200181
182 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static sector_t bfs_bmap(struct address_space *mapping, sector_t block)
186{
187 return generic_block_bmap(mapping, block, bfs_get_block);
188}
189
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700190const struct address_space_operations bfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .readpage = bfs_readpage,
192 .writepage = bfs_writepage,
Nick Piggineedcbba2007-10-16 01:25:11 -0700193 .write_begin = bfs_write_begin,
194 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .bmap = bfs_bmap,
196};
197
Arjan van de Ven754661f2007-02-12 00:55:38 -0800198const struct inode_operations bfs_file_inops;