Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/hpfs/file.c |
| 4 | * |
| 5 | * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 |
| 6 | * |
| 7 | * file VFS functions |
| 8 | */ |
| 9 | |
| 10 | #include "hpfs_fn.h" |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 11 | #include <linux/mpage.h> |
Christoph Hellwig | e0cba89 | 2021-07-20 15:33:40 +0200 | [diff] [blame] | 12 | #include <linux/iomap.h> |
Christoph Hellwig | 10c5db2 | 2020-05-23 09:30:11 +0200 | [diff] [blame] | 13 | #include <linux/fiemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #define BLOCKS(size) (((size) + 511) >> 9) |
| 16 | |
| 17 | static int hpfs_file_release(struct inode *inode, struct file *file) |
| 18 | { |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 19 | hpfs_lock(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | hpfs_write_if_changed(inode); |
Arnd Bergmann | 9a311b9 | 2011-01-22 20:26:12 +0100 | [diff] [blame] | 21 | hpfs_unlock(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | return 0; |
| 23 | } |
| 24 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 25 | int hpfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Mikulas Patocka | bc8728e | 2011-05-08 20:44:19 +0200 | [diff] [blame] | 27 | struct inode *inode = file->f_mapping->host; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 28 | int ret; |
| 29 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 30 | ret = file_write_and_wait_range(file, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 31 | if (ret) |
| 32 | return ret; |
Mikulas Patocka | bc8728e | 2011-05-08 20:44:19 +0200 | [diff] [blame] | 33 | return sync_blockdev(inode->i_sb->s_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* |
| 37 | * generic_file_read often calls bmap with non-existing sector, |
| 38 | * so we must ignore such errors. |
| 39 | */ |
| 40 | |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 41 | static secno hpfs_bmap(struct inode *inode, unsigned file_secno, unsigned *n_secs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | struct hpfs_inode_info *hpfs_inode = hpfs_i(inode); |
| 44 | unsigned n, disk_secno; |
| 45 | struct fnode *fnode; |
| 46 | struct buffer_head *bh; |
| 47 | if (BLOCKS(hpfs_i(inode)->mmu_private) <= file_secno) return 0; |
| 48 | n = file_secno - hpfs_inode->i_file_sec; |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 49 | if (n < hpfs_inode->i_n_secs) { |
| 50 | *n_secs = hpfs_inode->i_n_secs - n; |
| 51 | return hpfs_inode->i_disk_sec + n; |
| 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (!(fnode = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) return 0; |
| 54 | disk_secno = hpfs_bplus_lookup(inode->i_sb, inode, &fnode->btree, file_secno, bh); |
| 55 | if (disk_secno == -1) return 0; |
| 56 | if (hpfs_chk_sectors(inode->i_sb, disk_secno, 1, "bmap")) return 0; |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 57 | n = file_secno - hpfs_inode->i_file_sec; |
| 58 | if (n < hpfs_inode->i_n_secs) { |
| 59 | *n_secs = hpfs_inode->i_n_secs - n; |
| 60 | return hpfs_inode->i_disk_sec + n; |
| 61 | } |
| 62 | *n_secs = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return disk_secno; |
| 64 | } |
| 65 | |
Marco Stornelli | 70b31c4 | 2012-12-15 11:53:50 +0100 | [diff] [blame] | 66 | void hpfs_truncate(struct inode *i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
| 68 | if (IS_IMMUTABLE(i)) return /*-EPERM*/; |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 69 | hpfs_lock_assert(i->i_sb); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | hpfs_i(i)->i_n_secs = 0; |
| 72 | i->i_blocks = 1 + ((i->i_size + 511) >> 9); |
| 73 | hpfs_i(i)->mmu_private = i->i_size; |
| 74 | hpfs_truncate_btree(i->i_sb, i->i_ino, 1, ((i->i_size + 511) >> 9)); |
| 75 | hpfs_write_inode(i); |
| 76 | hpfs_i(i)->i_n_secs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) |
| 80 | { |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 81 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | secno s; |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 83 | unsigned n_secs; |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 84 | hpfs_lock(inode->i_sb); |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 85 | s = hpfs_bmap(inode, iblock, &n_secs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (s) { |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 87 | if (bh_result->b_size >> 9 < n_secs) |
| 88 | n_secs = bh_result->b_size >> 9; |
Mikulas Patocka | a64eefa | 2015-09-02 22:50:12 +0200 | [diff] [blame] | 89 | n_secs = hpfs_search_hotfix_map_for_range(inode->i_sb, s, n_secs); |
| 90 | if (unlikely(!n_secs)) { |
| 91 | s = hpfs_search_hotfix_map(inode->i_sb, s); |
| 92 | n_secs = 1; |
| 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | map_bh(bh_result, inode->i_sb, s); |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 95 | bh_result->b_size = n_secs << 9; |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 96 | goto ret_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 98 | if (!create) goto ret_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (iblock<<9 != hpfs_i(inode)->mmu_private) { |
| 100 | BUG(); |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 101 | r = -EIO; |
| 102 | goto ret_r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) { |
| 105 | hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1); |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 106 | r = -ENOSPC; |
| 107 | goto ret_r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | inode->i_blocks++; |
| 110 | hpfs_i(inode)->mmu_private += 512; |
| 111 | set_buffer_new(bh_result); |
Mikulas Patocka | a64eefa | 2015-09-02 22:50:12 +0200 | [diff] [blame] | 112 | map_bh(bh_result, inode->i_sb, hpfs_search_hotfix_map(inode->i_sb, s)); |
Mikulas Patocka | 7dd29d8 | 2011-05-08 20:42:54 +0200 | [diff] [blame] | 113 | ret_0: |
| 114 | r = 0; |
| 115 | ret_r: |
| 116 | hpfs_unlock(inode->i_sb); |
| 117 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Christoph Hellwig | e0cba89 | 2021-07-20 15:33:40 +0200 | [diff] [blame] | 120 | static int hpfs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, |
| 121 | unsigned flags, struct iomap *iomap, struct iomap *srcmap) |
| 122 | { |
| 123 | struct super_block *sb = inode->i_sb; |
| 124 | unsigned int blkbits = inode->i_blkbits; |
| 125 | unsigned int n_secs; |
| 126 | secno s; |
| 127 | |
| 128 | if (WARN_ON_ONCE(flags & (IOMAP_WRITE | IOMAP_ZERO))) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | iomap->bdev = inode->i_sb->s_bdev; |
| 132 | iomap->offset = offset; |
| 133 | |
| 134 | hpfs_lock(sb); |
| 135 | s = hpfs_bmap(inode, offset >> blkbits, &n_secs); |
| 136 | if (s) { |
| 137 | n_secs = hpfs_search_hotfix_map_for_range(sb, s, |
| 138 | min_t(loff_t, n_secs, length)); |
| 139 | if (unlikely(!n_secs)) { |
| 140 | s = hpfs_search_hotfix_map(sb, s); |
| 141 | n_secs = 1; |
| 142 | } |
| 143 | iomap->type = IOMAP_MAPPED; |
| 144 | iomap->flags = IOMAP_F_MERGED; |
| 145 | iomap->addr = (u64)s << blkbits; |
| 146 | iomap->length = (u64)n_secs << blkbits; |
| 147 | } else { |
| 148 | iomap->type = IOMAP_HOLE; |
| 149 | iomap->addr = IOMAP_NULL_ADDR; |
| 150 | iomap->length = 1 << blkbits; |
| 151 | } |
| 152 | |
| 153 | hpfs_unlock(sb); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static const struct iomap_ops hpfs_iomap_ops = { |
| 158 | .iomap_begin = hpfs_iomap_begin, |
| 159 | }; |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | static int hpfs_readpage(struct file *file, struct page *page) |
| 162 | { |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 163 | return mpage_readpage(page, hpfs_get_block); |
| 164 | } |
| 165 | |
| 166 | static int hpfs_writepage(struct page *page, struct writeback_control *wbc) |
| 167 | { |
| 168 | return block_write_full_page(page, hpfs_get_block, wbc); |
| 169 | } |
| 170 | |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 171 | static void hpfs_readahead(struct readahead_control *rac) |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 172 | { |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 173 | mpage_readahead(rac, hpfs_get_block); |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static int hpfs_writepages(struct address_space *mapping, |
| 177 | struct writeback_control *wbc) |
| 178 | { |
| 179 | return mpage_writepages(mapping, wbc, hpfs_get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 181 | |
Marco Stornelli | 70b31c4 | 2012-12-15 11:53:50 +0100 | [diff] [blame] | 182 | static void hpfs_write_failed(struct address_space *mapping, loff_t to) |
| 183 | { |
| 184 | struct inode *inode = mapping->host; |
| 185 | |
Mikulas Patocka | bbd465d | 2013-06-09 01:25:57 +0200 | [diff] [blame] | 186 | hpfs_lock(inode->i_sb); |
| 187 | |
Marco Stornelli | 70b31c4 | 2012-12-15 11:53:50 +0100 | [diff] [blame] | 188 | if (to > inode->i_size) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 189 | truncate_pagecache(inode, inode->i_size); |
Marco Stornelli | 70b31c4 | 2012-12-15 11:53:50 +0100 | [diff] [blame] | 190 | hpfs_truncate(inode); |
| 191 | } |
Mikulas Patocka | bbd465d | 2013-06-09 01:25:57 +0200 | [diff] [blame] | 192 | |
| 193 | hpfs_unlock(inode->i_sb); |
Marco Stornelli | 70b31c4 | 2012-12-15 11:53:50 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 196 | static int hpfs_write_begin(struct file *file, struct address_space *mapping, |
| 197 | loff_t pos, unsigned len, unsigned flags, |
| 198 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Christoph Hellwig | 282dc17 | 2010-06-04 11:29:55 +0200 | [diff] [blame] | 200 | int ret; |
| 201 | |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 202 | *pagep = NULL; |
Christoph Hellwig | 282dc17 | 2010-06-04 11:29:55 +0200 | [diff] [blame] | 203 | ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 204 | hpfs_get_block, |
| 205 | &hpfs_i(mapping->host)->mmu_private); |
Marco Stornelli | 70b31c4 | 2012-12-15 11:53:50 +0100 | [diff] [blame] | 206 | if (unlikely(ret)) |
| 207 | hpfs_write_failed(mapping, pos + len); |
Christoph Hellwig | 282dc17 | 2010-06-04 11:29:55 +0200 | [diff] [blame] | 208 | |
| 209 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 211 | |
Al Viro | 5f2e354 | 2013-03-19 20:35:00 -0400 | [diff] [blame] | 212 | static int hpfs_write_end(struct file *file, struct address_space *mapping, |
| 213 | loff_t pos, unsigned len, unsigned copied, |
| 214 | struct page *pagep, void *fsdata) |
| 215 | { |
| 216 | struct inode *inode = mapping->host; |
| 217 | int err; |
| 218 | err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); |
| 219 | if (err < len) |
| 220 | hpfs_write_failed(mapping, pos + len); |
| 221 | if (!(err < 0)) { |
| 222 | /* make sure we write it on close, if not earlier */ |
| 223 | hpfs_lock(inode->i_sb); |
| 224 | hpfs_i(inode)->i_dirty = 1; |
| 225 | hpfs_unlock(inode->i_sb); |
| 226 | } |
| 227 | return err; |
| 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | static sector_t _hpfs_bmap(struct address_space *mapping, sector_t block) |
| 231 | { |
Mikulas Patocka | a64eefa | 2015-09-02 22:50:12 +0200 | [diff] [blame] | 232 | return generic_block_bmap(mapping, block, hpfs_get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 234 | |
Mikulas Patocka | 91fff9b | 2016-10-03 23:00:19 +0200 | [diff] [blame] | 235 | static int hpfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len) |
| 236 | { |
Christoph Hellwig | e0cba89 | 2021-07-20 15:33:40 +0200 | [diff] [blame] | 237 | int ret; |
| 238 | |
| 239 | inode_lock(inode); |
| 240 | len = min_t(u64, len, i_size_read(inode)); |
| 241 | ret = iomap_fiemap(inode, fieinfo, start, len, &hpfs_iomap_ops); |
| 242 | inode_unlock(inode); |
| 243 | |
| 244 | return ret; |
Mikulas Patocka | 91fff9b | 2016-10-03 23:00:19 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 247 | const struct address_space_operations hpfs_aops = { |
Christoph Hellwig | 0af5737 | 2021-06-28 19:36:12 -0700 | [diff] [blame] | 248 | .set_page_dirty = __set_page_dirty_buffers, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | .readpage = hpfs_readpage, |
| 250 | .writepage = hpfs_writepage, |
Matthew Wilcox (Oracle) | d438834 | 2020-06-01 21:47:02 -0700 | [diff] [blame] | 251 | .readahead = hpfs_readahead, |
Mikulas Patocka | a0c1b75 | 2013-07-04 18:44:27 +0200 | [diff] [blame] | 252 | .writepages = hpfs_writepages, |
Nick Piggin | d6091b7 | 2007-10-16 01:25:10 -0700 | [diff] [blame] | 253 | .write_begin = hpfs_write_begin, |
Al Viro | 5f2e354 | 2013-03-19 20:35:00 -0400 | [diff] [blame] | 254 | .write_end = hpfs_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | .bmap = _hpfs_bmap |
| 256 | }; |
| 257 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 258 | const struct file_operations hpfs_file_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 261 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 262 | .write_iter = generic_file_write_iter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | .mmap = generic_file_mmap, |
| 264 | .release = hpfs_file_release, |
| 265 | .fsync = hpfs_file_fsync, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 266 | .splice_read = generic_file_splice_read, |
Mikulas Patocka | a27b5b9 | 2015-06-28 15:16:57 +0200 | [diff] [blame] | 267 | .unlocked_ioctl = hpfs_ioctl, |
Arnd Bergmann | 314999d | 2019-06-03 13:51:58 +0200 | [diff] [blame] | 268 | .compat_ioctl = compat_ptr_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 271 | const struct inode_operations hpfs_file_iops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
Christoph Hellwig | ca30bc9 | 2008-08-11 00:27:59 +0200 | [diff] [blame] | 273 | .setattr = hpfs_setattr, |
Mikulas Patocka | 91fff9b | 2016-10-03 23:00:19 +0200 | [diff] [blame] | 274 | .fiemap = hpfs_fiemap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | }; |