]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/hpfs/file.c | |
3 | * | |
4 | * Mikulas Patocka ([email protected]), 1998-1999 | |
5 | * | |
6 | * file VFS functions | |
7 | */ | |
8 | ||
9 | #include "hpfs_fn.h" | |
a0c1b759 | 10 | #include <linux/mpage.h> |
1da177e4 LT |
11 | |
12 | #define BLOCKS(size) (((size) + 511) >> 9) | |
13 | ||
14 | static int hpfs_file_release(struct inode *inode, struct file *file) | |
15 | { | |
9a311b96 | 16 | hpfs_lock(inode->i_sb); |
1da177e4 | 17 | hpfs_write_if_changed(inode); |
9a311b96 | 18 | hpfs_unlock(inode->i_sb); |
1da177e4 LT |
19 | return 0; |
20 | } | |
21 | ||
02c24a82 | 22 | int hpfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
1da177e4 | 23 | { |
bc8728ee | 24 | struct inode *inode = file->f_mapping->host; |
02c24a82 JB |
25 | int ret; |
26 | ||
27 | ret = filemap_write_and_wait_range(file->f_mapping, start, end); | |
28 | if (ret) | |
29 | return ret; | |
bc8728ee | 30 | return sync_blockdev(inode->i_sb->s_bdev); |
1da177e4 LT |
31 | } |
32 | ||
33 | /* | |
34 | * generic_file_read often calls bmap with non-existing sector, | |
35 | * so we must ignore such errors. | |
36 | */ | |
37 | ||
a0c1b759 | 38 | static secno hpfs_bmap(struct inode *inode, unsigned file_secno, unsigned *n_secs) |
1da177e4 LT |
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; | |
a0c1b759 MP |
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 | } | |
1da177e4 LT |
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; | |
a0c1b759 MP |
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; | |
1da177e4 LT |
60 | return disk_secno; |
61 | } | |
62 | ||
70b31c4c | 63 | void hpfs_truncate(struct inode *i) |
1da177e4 LT |
64 | { |
65 | if (IS_IMMUTABLE(i)) return /*-EPERM*/; | |
7dd29d8d MP |
66 | hpfs_lock_assert(i->i_sb); |
67 | ||
1da177e4 LT |
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; | |
1da177e4 LT |
74 | } |
75 | ||
76 | static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) | |
77 | { | |
7dd29d8d | 78 | int r; |
1da177e4 | 79 | secno s; |
a0c1b759 | 80 | unsigned n_secs; |
7dd29d8d | 81 | hpfs_lock(inode->i_sb); |
a0c1b759 | 82 | s = hpfs_bmap(inode, iblock, &n_secs); |
1da177e4 | 83 | if (s) { |
a0c1b759 MP |
84 | if (bh_result->b_size >> 9 < n_secs) |
85 | n_secs = bh_result->b_size >> 9; | |
a64eefaa MP |
86 | n_secs = hpfs_search_hotfix_map_for_range(inode->i_sb, s, n_secs); |
87 | if (unlikely(!n_secs)) { | |
88 | s = hpfs_search_hotfix_map(inode->i_sb, s); | |
89 | n_secs = 1; | |
90 | } | |
1da177e4 | 91 | map_bh(bh_result, inode->i_sb, s); |
a0c1b759 | 92 | bh_result->b_size = n_secs << 9; |
7dd29d8d | 93 | goto ret_0; |
1da177e4 | 94 | } |
7dd29d8d | 95 | if (!create) goto ret_0; |
1da177e4 LT |
96 | if (iblock<<9 != hpfs_i(inode)->mmu_private) { |
97 | BUG(); | |
7dd29d8d MP |
98 | r = -EIO; |
99 | goto ret_r; | |
1da177e4 LT |
100 | } |
101 | if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) { | |
102 | hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1); | |
7dd29d8d MP |
103 | r = -ENOSPC; |
104 | goto ret_r; | |
1da177e4 LT |
105 | } |
106 | inode->i_blocks++; | |
107 | hpfs_i(inode)->mmu_private += 512; | |
108 | set_buffer_new(bh_result); | |
a64eefaa | 109 | map_bh(bh_result, inode->i_sb, hpfs_search_hotfix_map(inode->i_sb, s)); |
7dd29d8d MP |
110 | ret_0: |
111 | r = 0; | |
112 | ret_r: | |
113 | hpfs_unlock(inode->i_sb); | |
114 | return r; | |
1da177e4 LT |
115 | } |
116 | ||
a0c1b759 MP |
117 | static int hpfs_readpage(struct file *file, struct page *page) |
118 | { | |
119 | return mpage_readpage(page, hpfs_get_block); | |
120 | } | |
121 | ||
1da177e4 LT |
122 | static int hpfs_writepage(struct page *page, struct writeback_control *wbc) |
123 | { | |
a0c1b759 | 124 | return block_write_full_page(page, hpfs_get_block, wbc); |
1da177e4 | 125 | } |
d6091b72 | 126 | |
a0c1b759 MP |
127 | static int hpfs_readpages(struct file *file, struct address_space *mapping, |
128 | struct list_head *pages, unsigned nr_pages) | |
129 | { | |
130 | return mpage_readpages(mapping, pages, nr_pages, hpfs_get_block); | |
131 | } | |
132 | ||
133 | static int hpfs_writepages(struct address_space *mapping, | |
134 | struct writeback_control *wbc) | |
1da177e4 | 135 | { |
a0c1b759 | 136 | return mpage_writepages(mapping, wbc, hpfs_get_block); |
1da177e4 | 137 | } |
d6091b72 | 138 | |
70b31c4c MS |
139 | static void hpfs_write_failed(struct address_space *mapping, loff_t to) |
140 | { | |
141 | struct inode *inode = mapping->host; | |
142 | ||
bbd465df MP |
143 | hpfs_lock(inode->i_sb); |
144 | ||
70b31c4c | 145 | if (to > inode->i_size) { |
7caef267 | 146 | truncate_pagecache(inode, inode->i_size); |
70b31c4c MS |
147 | hpfs_truncate(inode); |
148 | } | |
bbd465df MP |
149 | |
150 | hpfs_unlock(inode->i_sb); | |
70b31c4c MS |
151 | } |
152 | ||
d6091b72 NP |
153 | static int hpfs_write_begin(struct file *file, struct address_space *mapping, |
154 | loff_t pos, unsigned len, unsigned flags, | |
155 | struct page **pagep, void **fsdata) | |
1da177e4 | 156 | { |
282dc178 CH |
157 | int ret; |
158 | ||
d6091b72 | 159 | *pagep = NULL; |
282dc178 | 160 | ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
d6091b72 NP |
161 | hpfs_get_block, |
162 | &hpfs_i(mapping->host)->mmu_private); | |
70b31c4c MS |
163 | if (unlikely(ret)) |
164 | hpfs_write_failed(mapping, pos + len); | |
282dc178 CH |
165 | |
166 | return ret; | |
1da177e4 | 167 | } |
d6091b72 | 168 | |
5f2e354f AV |
169 | static int hpfs_write_end(struct file *file, struct address_space *mapping, |
170 | loff_t pos, unsigned len, unsigned copied, | |
171 | struct page *pagep, void *fsdata) | |
172 | { | |
173 | struct inode *inode = mapping->host; | |
174 | int err; | |
175 | err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); | |
176 | if (err < len) | |
177 | hpfs_write_failed(mapping, pos + len); | |
178 | if (!(err < 0)) { | |
179 | /* make sure we write it on close, if not earlier */ | |
180 | hpfs_lock(inode->i_sb); | |
181 | hpfs_i(inode)->i_dirty = 1; | |
182 | hpfs_unlock(inode->i_sb); | |
183 | } | |
184 | return err; | |
185 | } | |
186 | ||
1da177e4 LT |
187 | static sector_t _hpfs_bmap(struct address_space *mapping, sector_t block) |
188 | { | |
a64eefaa | 189 | return generic_block_bmap(mapping, block, hpfs_get_block); |
1da177e4 | 190 | } |
d6091b72 | 191 | |
f5e54d6e | 192 | const struct address_space_operations hpfs_aops = { |
1da177e4 LT |
193 | .readpage = hpfs_readpage, |
194 | .writepage = hpfs_writepage, | |
a0c1b759 MP |
195 | .readpages = hpfs_readpages, |
196 | .writepages = hpfs_writepages, | |
d6091b72 | 197 | .write_begin = hpfs_write_begin, |
5f2e354f | 198 | .write_end = hpfs_write_end, |
1da177e4 LT |
199 | .bmap = _hpfs_bmap |
200 | }; | |
201 | ||
4b6f5d20 | 202 | const struct file_operations hpfs_file_ops = |
1da177e4 LT |
203 | { |
204 | .llseek = generic_file_llseek, | |
aad4f8bb | 205 | .read_iter = generic_file_read_iter, |
8174202b | 206 | .write_iter = generic_file_write_iter, |
1da177e4 LT |
207 | .mmap = generic_file_mmap, |
208 | .release = hpfs_file_release, | |
209 | .fsync = hpfs_file_fsync, | |
5ffc4ef4 | 210 | .splice_read = generic_file_splice_read, |
a27b5b97 | 211 | .unlocked_ioctl = hpfs_ioctl, |
1da177e4 LT |
212 | }; |
213 | ||
92e1d5be | 214 | const struct inode_operations hpfs_file_iops = |
1da177e4 | 215 | { |
ca30bc99 | 216 | .setattr = hpfs_setattr, |
1da177e4 | 217 | }; |