1/*
2 *  linux/fs/hpfs/file.c
3 *
4 *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5 *
6 *  file VFS functions
7 */
8
9#include "hpfs_fn.h"
10#include <linux/mpage.h>
11
12#define BLOCKS(size) (((size) + 511) >> 9)
13
14static int hpfs_file_release(struct inode *inode, struct file *file)
15{
16	hpfs_lock(inode->i_sb);
17	hpfs_write_if_changed(inode);
18	hpfs_unlock(inode->i_sb);
19	return 0;
20}
21
22int hpfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
23{
24	struct inode *inode = file->f_mapping->host;
25	int ret;
26
27	ret = filemap_write_and_wait_range(file->f_mapping, start, end);
28	if (ret)
29		return ret;
30	return sync_blockdev(inode->i_sb->s_bdev);
31}
32
33/*
34 * generic_file_read often calls bmap with non-existing sector,
35 * so we must ignore such errors.
36 */
37
38static secno hpfs_bmap(struct inode *inode, unsigned file_secno, unsigned *n_secs)
39{
40	struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
41	unsigned n, disk_secno;
42	struct fnode *fnode;
43	struct buffer_head *bh;
44	if (BLOCKS(hpfs_i(inode)->mmu_private) <= file_secno) return 0;
45	n = file_secno - hpfs_inode->i_file_sec;
46	if (n < hpfs_inode->i_n_secs) {
47		*n_secs = hpfs_inode->i_n_secs - n;
48		return hpfs_inode->i_disk_sec + n;
49	}
50	if (!(fnode = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) return 0;
51	disk_secno = hpfs_bplus_lookup(inode->i_sb, inode, &fnode->btree, file_secno, bh);
52	if (disk_secno == -1) return 0;
53	if (hpfs_chk_sectors(inode->i_sb, disk_secno, 1, "bmap")) return 0;
54	n = file_secno - hpfs_inode->i_file_sec;
55	if (n < hpfs_inode->i_n_secs) {
56		*n_secs = hpfs_inode->i_n_secs - n;
57		return hpfs_inode->i_disk_sec + n;
58	}
59	*n_secs = 1;
60	return disk_secno;
61}
62
63void hpfs_truncate(struct inode *i)
64{
65	if (IS_IMMUTABLE(i)) return /*-EPERM*/;
66	hpfs_lock_assert(i->i_sb);
67
68	hpfs_i(i)->i_n_secs = 0;
69	i->i_blocks = 1 + ((i->i_size + 511) >> 9);
70	hpfs_i(i)->mmu_private = i->i_size;
71	hpfs_truncate_btree(i->i_sb, i->i_ino, 1, ((i->i_size + 511) >> 9));
72	hpfs_write_inode(i);
73	hpfs_i(i)->i_n_secs = 0;
74}
75
76static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
77{
78	int r;
79	secno s;
80	unsigned n_secs;
81	hpfs_lock(inode->i_sb);
82	s = hpfs_bmap(inode, iblock, &n_secs);
83	if (s) {
84		if (bh_result->b_size >> 9 < n_secs)
85			n_secs = bh_result->b_size >> 9;
86		map_bh(bh_result, inode->i_sb, s);
87		bh_result->b_size = n_secs << 9;
88		goto ret_0;
89	}
90	if (!create) goto ret_0;
91	if (iblock<<9 != hpfs_i(inode)->mmu_private) {
92		BUG();
93		r = -EIO;
94		goto ret_r;
95	}
96	if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) {
97		hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1);
98		r = -ENOSPC;
99		goto ret_r;
100	}
101	inode->i_blocks++;
102	hpfs_i(inode)->mmu_private += 512;
103	set_buffer_new(bh_result);
104	map_bh(bh_result, inode->i_sb, s);
105	ret_0:
106	r = 0;
107	ret_r:
108	hpfs_unlock(inode->i_sb);
109	return r;
110}
111
112static int hpfs_readpage(struct file *file, struct page *page)
113{
114	return mpage_readpage(page, hpfs_get_block);
115}
116
117static int hpfs_writepage(struct page *page, struct writeback_control *wbc)
118{
119	return block_write_full_page(page, hpfs_get_block, wbc);
120}
121
122static int hpfs_readpages(struct file *file, struct address_space *mapping,
123			  struct list_head *pages, unsigned nr_pages)
124{
125	return mpage_readpages(mapping, pages, nr_pages, hpfs_get_block);
126}
127
128static int hpfs_writepages(struct address_space *mapping,
129			   struct writeback_control *wbc)
130{
131	return mpage_writepages(mapping, wbc, hpfs_get_block);
132}
133
134static void hpfs_write_failed(struct address_space *mapping, loff_t to)
135{
136	struct inode *inode = mapping->host;
137
138	hpfs_lock(inode->i_sb);
139
140	if (to > inode->i_size) {
141		truncate_pagecache(inode, inode->i_size);
142		hpfs_truncate(inode);
143	}
144
145	hpfs_unlock(inode->i_sb);
146}
147
148static int hpfs_write_begin(struct file *file, struct address_space *mapping,
149			loff_t pos, unsigned len, unsigned flags,
150			struct page **pagep, void **fsdata)
151{
152	int ret;
153
154	*pagep = NULL;
155	ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
156				hpfs_get_block,
157				&hpfs_i(mapping->host)->mmu_private);
158	if (unlikely(ret))
159		hpfs_write_failed(mapping, pos + len);
160
161	return ret;
162}
163
164static int hpfs_write_end(struct file *file, struct address_space *mapping,
165			loff_t pos, unsigned len, unsigned copied,
166			struct page *pagep, void *fsdata)
167{
168	struct inode *inode = mapping->host;
169	int err;
170	err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
171	if (err < len)
172		hpfs_write_failed(mapping, pos + len);
173	if (!(err < 0)) {
174		/* make sure we write it on close, if not earlier */
175		hpfs_lock(inode->i_sb);
176		hpfs_i(inode)->i_dirty = 1;
177		hpfs_unlock(inode->i_sb);
178	}
179	return err;
180}
181
182static sector_t _hpfs_bmap(struct address_space *mapping, sector_t block)
183{
184	return generic_block_bmap(mapping,block,hpfs_get_block);
185}
186
187const struct address_space_operations hpfs_aops = {
188	.readpage = hpfs_readpage,
189	.writepage = hpfs_writepage,
190	.readpages = hpfs_readpages,
191	.writepages = hpfs_writepages,
192	.write_begin = hpfs_write_begin,
193	.write_end = hpfs_write_end,
194	.bmap = _hpfs_bmap
195};
196
197const struct file_operations hpfs_file_ops =
198{
199	.llseek		= generic_file_llseek,
200	.read_iter	= generic_file_read_iter,
201	.write_iter	= generic_file_write_iter,
202	.mmap		= generic_file_mmap,
203	.release	= hpfs_file_release,
204	.fsync		= hpfs_file_fsync,
205	.splice_read	= generic_file_splice_read,
206};
207
208const struct inode_operations hpfs_file_iops =
209{
210	.setattr	= hpfs_setattr,
211};
212