file.c 5.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 *  linux/fs/hpfs/file.c
 *
 *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
 *
 *  file VFS functions
 */

#include "hpfs_fn.h"
M
Mikulas Patocka 已提交
10
#include <linux/mpage.h>
L
Linus Torvalds 已提交
11 12 13 14 15

#define BLOCKS(size) (((size) + 511) >> 9)

static int hpfs_file_release(struct inode *inode, struct file *file)
{
A
Arnd Bergmann 已提交
16
	hpfs_lock(inode->i_sb);
L
Linus Torvalds 已提交
17
	hpfs_write_if_changed(inode);
A
Arnd Bergmann 已提交
18
	hpfs_unlock(inode->i_sb);
L
Linus Torvalds 已提交
19 20 21
	return 0;
}

22
int hpfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
L
Linus Torvalds 已提交
23
{
M
Mikulas Patocka 已提交
24
	struct inode *inode = file->f_mapping->host;
25 26 27 28 29
	int ret;

	ret = filemap_write_and_wait_range(file->f_mapping, start, end);
	if (ret)
		return ret;
M
Mikulas Patocka 已提交
30
	return sync_blockdev(inode->i_sb->s_bdev);
L
Linus Torvalds 已提交
31 32 33 34 35 36 37
}

/*
 * generic_file_read often calls bmap with non-existing sector,
 * so we must ignore such errors.
 */

M
Mikulas Patocka 已提交
38
static secno hpfs_bmap(struct inode *inode, unsigned file_secno, unsigned *n_secs)
L
Linus Torvalds 已提交
39 40 41 42 43 44 45
{
	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
	unsigned n, disk_secno;
	struct fnode *fnode;
	struct buffer_head *bh;
	if (BLOCKS(hpfs_i(inode)->mmu_private) <= file_secno) return 0;
	n = file_secno - hpfs_inode->i_file_sec;
M
Mikulas Patocka 已提交
46 47 48 49
	if (n < hpfs_inode->i_n_secs) {
		*n_secs = hpfs_inode->i_n_secs - n;
		return hpfs_inode->i_disk_sec + n;
	}
L
Linus Torvalds 已提交
50 51 52 53
	if (!(fnode = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) return 0;
	disk_secno = hpfs_bplus_lookup(inode->i_sb, inode, &fnode->btree, file_secno, bh);
	if (disk_secno == -1) return 0;
	if (hpfs_chk_sectors(inode->i_sb, disk_secno, 1, "bmap")) return 0;
M
Mikulas Patocka 已提交
54 55 56 57 58 59
	n = file_secno - hpfs_inode->i_file_sec;
	if (n < hpfs_inode->i_n_secs) {
		*n_secs = hpfs_inode->i_n_secs - n;
		return hpfs_inode->i_disk_sec + n;
	}
	*n_secs = 1;
L
Linus Torvalds 已提交
60 61 62
	return disk_secno;
}

M
Marco Stornelli 已提交
63
void hpfs_truncate(struct inode *i)
L
Linus Torvalds 已提交
64 65
{
	if (IS_IMMUTABLE(i)) return /*-EPERM*/;
66 67
	hpfs_lock_assert(i->i_sb);

L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77
	hpfs_i(i)->i_n_secs = 0;
	i->i_blocks = 1 + ((i->i_size + 511) >> 9);
	hpfs_i(i)->mmu_private = i->i_size;
	hpfs_truncate_btree(i->i_sb, i->i_ino, 1, ((i->i_size + 511) >> 9));
	hpfs_write_inode(i);
	hpfs_i(i)->i_n_secs = 0;
}

static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
{
78
	int r;
L
Linus Torvalds 已提交
79
	secno s;
M
Mikulas Patocka 已提交
80
	unsigned n_secs;
81
	hpfs_lock(inode->i_sb);
M
Mikulas Patocka 已提交
82
	s = hpfs_bmap(inode, iblock, &n_secs);
L
Linus Torvalds 已提交
83
	if (s) {
M
Mikulas Patocka 已提交
84 85
		if (bh_result->b_size >> 9 < n_secs)
			n_secs = bh_result->b_size >> 9;
L
Linus Torvalds 已提交
86
		map_bh(bh_result, inode->i_sb, s);
M
Mikulas Patocka 已提交
87
		bh_result->b_size = n_secs << 9;
88
		goto ret_0;
L
Linus Torvalds 已提交
89
	}
90
	if (!create) goto ret_0;
L
Linus Torvalds 已提交
91 92
	if (iblock<<9 != hpfs_i(inode)->mmu_private) {
		BUG();
93 94
		r = -EIO;
		goto ret_r;
L
Linus Torvalds 已提交
95 96 97
	}
	if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) {
		hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1);
98 99
		r = -ENOSPC;
		goto ret_r;
L
Linus Torvalds 已提交
100 101 102 103 104
	}
	inode->i_blocks++;
	hpfs_i(inode)->mmu_private += 512;
	set_buffer_new(bh_result);
	map_bh(bh_result, inode->i_sb, s);
105 106 107 108 109
	ret_0:
	r = 0;
	ret_r:
	hpfs_unlock(inode->i_sb);
	return r;
L
Linus Torvalds 已提交
110 111
}

M
Mikulas Patocka 已提交
112 113 114 115 116
static int hpfs_readpage(struct file *file, struct page *page)
{
	return mpage_readpage(page, hpfs_get_block);
}

L
Linus Torvalds 已提交
117 118
static int hpfs_writepage(struct page *page, struct writeback_control *wbc)
{
M
Mikulas Patocka 已提交
119
	return block_write_full_page(page, hpfs_get_block, wbc);
L
Linus Torvalds 已提交
120
}
N
Nick Piggin 已提交
121

M
Mikulas Patocka 已提交
122 123 124 125 126 127 128 129
static int hpfs_readpages(struct file *file, struct address_space *mapping,
			  struct list_head *pages, unsigned nr_pages)
{
	return mpage_readpages(mapping, pages, nr_pages, hpfs_get_block);
}

static int hpfs_writepages(struct address_space *mapping,
			   struct writeback_control *wbc)
L
Linus Torvalds 已提交
130
{
M
Mikulas Patocka 已提交
131
	return mpage_writepages(mapping, wbc, hpfs_get_block);
L
Linus Torvalds 已提交
132
}
N
Nick Piggin 已提交
133

M
Marco Stornelli 已提交
134 135 136 137
static void hpfs_write_failed(struct address_space *mapping, loff_t to)
{
	struct inode *inode = mapping->host;

138 139
	hpfs_lock(inode->i_sb);

M
Marco Stornelli 已提交
140
	if (to > inode->i_size) {
141
		truncate_pagecache(inode, inode->i_size);
M
Marco Stornelli 已提交
142 143
		hpfs_truncate(inode);
	}
144 145

	hpfs_unlock(inode->i_sb);
M
Marco Stornelli 已提交
146 147
}

N
Nick Piggin 已提交
148 149 150
static int hpfs_write_begin(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned flags,
			struct page **pagep, void **fsdata)
L
Linus Torvalds 已提交
151
{
152 153
	int ret;

N
Nick Piggin 已提交
154
	*pagep = NULL;
155
	ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
N
Nick Piggin 已提交
156 157
				hpfs_get_block,
				&hpfs_i(mapping->host)->mmu_private);
M
Marco Stornelli 已提交
158 159
	if (unlikely(ret))
		hpfs_write_failed(mapping, pos + len);
160 161

	return ret;
L
Linus Torvalds 已提交
162
}
N
Nick Piggin 已提交
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static int hpfs_write_end(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *pagep, void *fsdata)
{
	struct inode *inode = mapping->host;
	int err;
	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
	if (err < len)
		hpfs_write_failed(mapping, pos + len);
	if (!(err < 0)) {
		/* make sure we write it on close, if not earlier */
		hpfs_lock(inode->i_sb);
		hpfs_i(inode)->i_dirty = 1;
		hpfs_unlock(inode->i_sb);
	}
	return err;
}

L
Linus Torvalds 已提交
182 183 184 185
static sector_t _hpfs_bmap(struct address_space *mapping, sector_t block)
{
	return generic_block_bmap(mapping,block,hpfs_get_block);
}
N
Nick Piggin 已提交
186

187
const struct address_space_operations hpfs_aops = {
L
Linus Torvalds 已提交
188 189
	.readpage = hpfs_readpage,
	.writepage = hpfs_writepage,
M
Mikulas Patocka 已提交
190 191
	.readpages = hpfs_readpages,
	.writepages = hpfs_writepages,
N
Nick Piggin 已提交
192
	.write_begin = hpfs_write_begin,
193
	.write_end = hpfs_write_end,
L
Linus Torvalds 已提交
194 195 196
	.bmap = _hpfs_bmap
};

197
const struct file_operations hpfs_file_ops =
L
Linus Torvalds 已提交
198 199
{
	.llseek		= generic_file_llseek,
200 201
	.read		= new_sync_read,
	.read_iter	= generic_file_read_iter,
202
	.write		= do_sync_write,
203
	.aio_write	= generic_file_aio_write,
L
Linus Torvalds 已提交
204 205 206
	.mmap		= generic_file_mmap,
	.release	= hpfs_file_release,
	.fsync		= hpfs_file_fsync,
207
	.splice_read	= generic_file_splice_read,
L
Linus Torvalds 已提交
208 209
};

210
const struct inode_operations hpfs_file_iops =
L
Linus Torvalds 已提交
211
{
212
	.setattr	= hpfs_setattr,
L
Linus Torvalds 已提交
213
};