blob: 077c25128eb7417adaedfba9cdfdce4522d43c30 [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 * 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 Patockaa0c1b752013-07-04 18:44:27 +020011#include <linux/mpage.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020012#include <linux/fiemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define BLOCKS(size) (((size) + 511) >> 9)
15
16static int hpfs_file_release(struct inode *inode, struct file *file)
17{
Arnd Bergmann9a311b92011-01-22 20:26:12 +010018 hpfs_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 hpfs_write_if_changed(inode);
Arnd Bergmann9a311b92011-01-22 20:26:12 +010020 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 return 0;
22}
23
Josef Bacik02c24a82011-07-16 20:44:56 -040024int hpfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Mikulas Patockabc8728e2011-05-08 20:44:19 +020026 struct inode *inode = file->f_mapping->host;
Josef Bacik02c24a82011-07-16 20:44:56 -040027 int ret;
28
Jeff Layton3b49c9a2017-07-07 15:20:52 -040029 ret = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -040030 if (ret)
31 return ret;
Mikulas Patockabc8728e2011-05-08 20:44:19 +020032 return sync_blockdev(inode->i_sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
35/*
36 * generic_file_read often calls bmap with non-existing sector,
37 * so we must ignore such errors.
38 */
39
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020040static secno hpfs_bmap(struct inode *inode, unsigned file_secno, unsigned *n_secs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
43 unsigned n, disk_secno;
44 struct fnode *fnode;
45 struct buffer_head *bh;
46 if (BLOCKS(hpfs_i(inode)->mmu_private) <= file_secno) return 0;
47 n = file_secno - hpfs_inode->i_file_sec;
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020048 if (n < hpfs_inode->i_n_secs) {
49 *n_secs = hpfs_inode->i_n_secs - n;
50 return hpfs_inode->i_disk_sec + n;
51 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!(fnode = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) return 0;
53 disk_secno = hpfs_bplus_lookup(inode->i_sb, inode, &fnode->btree, file_secno, bh);
54 if (disk_secno == -1) return 0;
55 if (hpfs_chk_sectors(inode->i_sb, disk_secno, 1, "bmap")) return 0;
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020056 n = file_secno - hpfs_inode->i_file_sec;
57 if (n < hpfs_inode->i_n_secs) {
58 *n_secs = hpfs_inode->i_n_secs - n;
59 return hpfs_inode->i_disk_sec + n;
60 }
61 *n_secs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return disk_secno;
63}
64
Marco Stornelli70b31c42012-12-15 11:53:50 +010065void hpfs_truncate(struct inode *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 if (IS_IMMUTABLE(i)) return /*-EPERM*/;
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020068 hpfs_lock_assert(i->i_sb);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 hpfs_i(i)->i_n_secs = 0;
71 i->i_blocks = 1 + ((i->i_size + 511) >> 9);
72 hpfs_i(i)->mmu_private = i->i_size;
73 hpfs_truncate_btree(i->i_sb, i->i_ino, 1, ((i->i_size + 511) >> 9));
74 hpfs_write_inode(i);
75 hpfs_i(i)->i_n_secs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
79{
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020080 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 secno s;
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020082 unsigned n_secs;
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020083 hpfs_lock(inode->i_sb);
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020084 s = hpfs_bmap(inode, iblock, &n_secs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (s) {
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020086 if (bh_result->b_size >> 9 < n_secs)
87 n_secs = bh_result->b_size >> 9;
Mikulas Patockaa64eefa2015-09-02 22:50:12 +020088 n_secs = hpfs_search_hotfix_map_for_range(inode->i_sb, s, n_secs);
89 if (unlikely(!n_secs)) {
90 s = hpfs_search_hotfix_map(inode->i_sb, s);
91 n_secs = 1;
92 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 map_bh(bh_result, inode->i_sb, s);
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020094 bh_result->b_size = n_secs << 9;
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020095 goto ret_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020097 if (!create) goto ret_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (iblock<<9 != hpfs_i(inode)->mmu_private) {
99 BUG();
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200100 r = -EIO;
101 goto ret_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) {
104 hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1);
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200105 r = -ENOSPC;
106 goto ret_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 inode->i_blocks++;
109 hpfs_i(inode)->mmu_private += 512;
110 set_buffer_new(bh_result);
Mikulas Patockaa64eefa2015-09-02 22:50:12 +0200111 map_bh(bh_result, inode->i_sb, hpfs_search_hotfix_map(inode->i_sb, s));
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200112 ret_0:
113 r = 0;
114 ret_r:
115 hpfs_unlock(inode->i_sb);
116 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static int hpfs_readpage(struct file *file, struct page *page)
120{
Mikulas Patockaa0c1b752013-07-04 18:44:27 +0200121 return mpage_readpage(page, hpfs_get_block);
122}
123
124static int hpfs_writepage(struct page *page, struct writeback_control *wbc)
125{
126 return block_write_full_page(page, hpfs_get_block, wbc);
127}
128
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700129static void hpfs_readahead(struct readahead_control *rac)
Mikulas Patockaa0c1b752013-07-04 18:44:27 +0200130{
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700131 mpage_readahead(rac, hpfs_get_block);
Mikulas Patockaa0c1b752013-07-04 18:44:27 +0200132}
133
134static int hpfs_writepages(struct address_space *mapping,
135 struct writeback_control *wbc)
136{
137 return mpage_writepages(mapping, wbc, hpfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Nick Piggind6091b72007-10-16 01:25:10 -0700139
Marco Stornelli70b31c42012-12-15 11:53:50 +0100140static void hpfs_write_failed(struct address_space *mapping, loff_t to)
141{
142 struct inode *inode = mapping->host;
143
Mikulas Patockabbd465d2013-06-09 01:25:57 +0200144 hpfs_lock(inode->i_sb);
145
Marco Stornelli70b31c42012-12-15 11:53:50 +0100146 if (to > inode->i_size) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -0700147 truncate_pagecache(inode, inode->i_size);
Marco Stornelli70b31c42012-12-15 11:53:50 +0100148 hpfs_truncate(inode);
149 }
Mikulas Patockabbd465d2013-06-09 01:25:57 +0200150
151 hpfs_unlock(inode->i_sb);
Marco Stornelli70b31c42012-12-15 11:53:50 +0100152}
153
Nick Piggind6091b72007-10-16 01:25:10 -0700154static int hpfs_write_begin(struct file *file, struct address_space *mapping,
155 loff_t pos, unsigned len, unsigned flags,
156 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Christoph Hellwig282dc172010-06-04 11:29:55 +0200158 int ret;
159
Nick Piggind6091b72007-10-16 01:25:10 -0700160 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +0200161 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Nick Piggind6091b72007-10-16 01:25:10 -0700162 hpfs_get_block,
163 &hpfs_i(mapping->host)->mmu_private);
Marco Stornelli70b31c42012-12-15 11:53:50 +0100164 if (unlikely(ret))
165 hpfs_write_failed(mapping, pos + len);
Christoph Hellwig282dc172010-06-04 11:29:55 +0200166
167 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Nick Piggind6091b72007-10-16 01:25:10 -0700169
Al Viro5f2e3542013-03-19 20:35:00 -0400170static int hpfs_write_end(struct file *file, struct address_space *mapping,
171 loff_t pos, unsigned len, unsigned copied,
172 struct page *pagep, void *fsdata)
173{
174 struct inode *inode = mapping->host;
175 int err;
176 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
177 if (err < len)
178 hpfs_write_failed(mapping, pos + len);
179 if (!(err < 0)) {
180 /* make sure we write it on close, if not earlier */
181 hpfs_lock(inode->i_sb);
182 hpfs_i(inode)->i_dirty = 1;
183 hpfs_unlock(inode->i_sb);
184 }
185 return err;
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static sector_t _hpfs_bmap(struct address_space *mapping, sector_t block)
189{
Mikulas Patockaa64eefa2015-09-02 22:50:12 +0200190 return generic_block_bmap(mapping, block, hpfs_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Nick Piggind6091b72007-10-16 01:25:10 -0700192
Mikulas Patocka91fff9b2016-10-03 23:00:19 +0200193static int hpfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, u64 start, u64 len)
194{
195 return generic_block_fiemap(inode, fieinfo, start, len, hpfs_get_block);
196}
197
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700198const struct address_space_operations hpfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .readpage = hpfs_readpage,
200 .writepage = hpfs_writepage,
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700201 .readahead = hpfs_readahead,
Mikulas Patockaa0c1b752013-07-04 18:44:27 +0200202 .writepages = hpfs_writepages,
Nick Piggind6091b72007-10-16 01:25:10 -0700203 .write_begin = hpfs_write_begin,
Al Viro5f2e3542013-03-19 20:35:00 -0400204 .write_end = hpfs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .bmap = _hpfs_bmap
206};
207
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800208const struct file_operations hpfs_file_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400211 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400212 .write_iter = generic_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .mmap = generic_file_mmap,
214 .release = hpfs_file_release,
215 .fsync = hpfs_file_fsync,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200216 .splice_read = generic_file_splice_read,
Mikulas Patockaa27b5b92015-06-28 15:16:57 +0200217 .unlocked_ioctl = hpfs_ioctl,
Arnd Bergmann314999d2019-06-03 13:51:58 +0200218 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800221const struct inode_operations hpfs_file_iops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200223 .setattr = hpfs_setattr,
Mikulas Patocka91fff9b2016-10-03 23:00:19 +0200224 .fiemap = hpfs_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};