blob: 1ecec124e76f17866ce8b8450a71291bae7534c8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#define BLOCKS(size) (((size) + 511) >> 9)
14
15static int hpfs_file_release(struct inode *inode, struct file *file)
16{
Arnd Bergmann9a311b92011-01-22 20:26:12 +010017 hpfs_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 hpfs_write_if_changed(inode);
Arnd Bergmann9a311b92011-01-22 20:26:12 +010019 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 return 0;
21}
22
Josef Bacik02c24a82011-07-16 20:44:56 -040023int hpfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Mikulas Patockabc8728e2011-05-08 20:44:19 +020025 struct inode *inode = file->f_mapping->host;
Josef Bacik02c24a82011-07-16 20:44:56 -040026 int ret;
27
Jeff Layton3b49c9a2017-07-07 15:20:52 -040028 ret = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -040029 if (ret)
30 return ret;
Mikulas Patockabc8728e2011-05-08 20:44:19 +020031 return sync_blockdev(inode->i_sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
34/*
35 * generic_file_read often calls bmap with non-existing sector,
36 * so we must ignore such errors.
37 */
38
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020039static secno hpfs_bmap(struct inode *inode, unsigned file_secno, unsigned *n_secs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
42 unsigned n, disk_secno;
43 struct fnode *fnode;
44 struct buffer_head *bh;
45 if (BLOCKS(hpfs_i(inode)->mmu_private) <= file_secno) return 0;
46 n = file_secno - hpfs_inode->i_file_sec;
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020047 if (n < hpfs_inode->i_n_secs) {
48 *n_secs = hpfs_inode->i_n_secs - n;
49 return hpfs_inode->i_disk_sec + n;
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (!(fnode = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) return 0;
52 disk_secno = hpfs_bplus_lookup(inode->i_sb, inode, &fnode->btree, file_secno, bh);
53 if (disk_secno == -1) return 0;
54 if (hpfs_chk_sectors(inode->i_sb, disk_secno, 1, "bmap")) return 0;
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020055 n = file_secno - hpfs_inode->i_file_sec;
56 if (n < hpfs_inode->i_n_secs) {
57 *n_secs = hpfs_inode->i_n_secs - n;
58 return hpfs_inode->i_disk_sec + n;
59 }
60 *n_secs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return disk_secno;
62}
63
Marco Stornelli70b31c42012-12-15 11:53:50 +010064void hpfs_truncate(struct inode *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 if (IS_IMMUTABLE(i)) return /*-EPERM*/;
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020067 hpfs_lock_assert(i->i_sb);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 hpfs_i(i)->i_n_secs = 0;
70 i->i_blocks = 1 + ((i->i_size + 511) >> 9);
71 hpfs_i(i)->mmu_private = i->i_size;
72 hpfs_truncate_btree(i->i_sb, i->i_ino, 1, ((i->i_size + 511) >> 9));
73 hpfs_write_inode(i);
74 hpfs_i(i)->i_n_secs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static int hpfs_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
78{
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020079 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 secno s;
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020081 unsigned n_secs;
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020082 hpfs_lock(inode->i_sb);
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020083 s = hpfs_bmap(inode, iblock, &n_secs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (s) {
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020085 if (bh_result->b_size >> 9 < n_secs)
86 n_secs = bh_result->b_size >> 9;
Mikulas Patockaa64eefa2015-09-02 22:50:12 +020087 n_secs = hpfs_search_hotfix_map_for_range(inode->i_sb, s, n_secs);
88 if (unlikely(!n_secs)) {
89 s = hpfs_search_hotfix_map(inode->i_sb, s);
90 n_secs = 1;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 map_bh(bh_result, inode->i_sb, s);
Mikulas Patockaa0c1b752013-07-04 18:44:27 +020093 bh_result->b_size = n_secs << 9;
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020094 goto ret_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020096 if (!create) goto ret_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (iblock<<9 != hpfs_i(inode)->mmu_private) {
98 BUG();
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020099 r = -EIO;
100 goto ret_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 if ((s = hpfs_add_sector_to_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1)) == -1) {
103 hpfs_truncate_btree(inode->i_sb, inode->i_ino, 1, inode->i_blocks - 1);
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200104 r = -ENOSPC;
105 goto ret_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107 inode->i_blocks++;
108 hpfs_i(inode)->mmu_private += 512;
109 set_buffer_new(bh_result);
Mikulas Patockaa64eefa2015-09-02 22:50:12 +0200110 map_bh(bh_result, inode->i_sb, hpfs_search_hotfix_map(inode->i_sb, s));
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200111 ret_0:
112 r = 0;
113 ret_r:
114 hpfs_unlock(inode->i_sb);
115 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int hpfs_readpage(struct file *file, struct page *page)
119{
Mikulas Patockaa0c1b752013-07-04 18:44:27 +0200120 return mpage_readpage(page, hpfs_get_block);
121}
122
123static int hpfs_writepage(struct page *page, struct writeback_control *wbc)
124{
125 return block_write_full_page(page, hpfs_get_block, wbc);
126}
127
128static int hpfs_readpages(struct file *file, struct address_space *mapping,
129 struct list_head *pages, unsigned nr_pages)
130{
131 return mpage_readpages(mapping, pages, nr_pages, hpfs_get_block);
132}
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,
Mikulas Patockaa0c1b752013-07-04 18:44:27 +0200201 .readpages = hpfs_readpages,
202 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800220const struct inode_operations hpfs_file_iops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200222 .setattr = hpfs_setattr,
Mikulas Patocka91fff9b2016-10-03 23:00:19 +0200223 .fiemap = hpfs_fiemap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};