forked from cryptomilk/kernel-sdfat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblkdev.c
430 lines (348 loc) · 10.7 KB
/
blkdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : blkdev.c */
/* PURPOSE : sdFAT Block Device Driver Glue Layer */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/************************************************************************/
#include <linux/blkdev.h>
#include <linux/log2.h>
#include <linux/backing-dev.h>
#include "sdfat.h"
/*----------------------------------------------------------------------*/
/* Constant & Macro Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Global Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Local Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY */
/************************************************************************/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
/* EMPTY */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) */
static struct backing_dev_info *inode_to_bdi(struct inode *bd_inode)
{
return bd_inode->i_mapping->backing_dev_info;
}
#endif
/*======================================================================*/
/* Function Definitions */
/*======================================================================*/
s32 bdev_open_dev(struct super_block *sb)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
if (fsi->bd_opened)
return 0;
fsi->bd_opened = true;
return 0;
}
s32 bdev_close_dev(struct super_block *sb)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
fsi->bd_opened = false;
return 0;
}
static inline s32 block_device_ejected(struct super_block *sb)
{
struct inode *bd_inode = sb->s_bdev->bd_inode;
struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
return (bdi->dev == NULL);
}
s32 bdev_check_bdi_valid(struct super_block *sb)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
if (block_device_ejected(sb)) {
if (!(fsi->prev_eio & SDFAT_EIO_BDI)) {
fsi->prev_eio |= SDFAT_EIO_BDI;
sdfat_log_msg(sb, KERN_ERR, "%s: block device is "
"eliminated.(bdi:%p)", __func__, sb->s_bdi);
}
return -ENXIO;
}
return 0;
}
#if IS_BUILTIN(CONFIG_SDFAT_FS)
static void __bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
{
u32 sects_per_page = (PAGE_SIZE >> sb->s_blocksize_bits);
struct blk_plug plug;
u64 i;
blk_start_plug(&plug);
for (i = 0; i < num_secs; i++) {
if (i && !(i & (sects_per_page - 1)))
blk_flush_plug(current);
sb_breadahead(sb, (sector_t)(secno + i));
}
blk_finish_plug(&plug);
}
#else
static void __bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
{
u64 i;
for (i = 0; i < num_secs; i++)
sb_breadahead(sb, (sector_t)(secno + i));
}
#endif
/* Make a readahead request */
s32 bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
if (!fsi->bd_opened)
return -EIO;
__bdev_readahead(sb, secno, num_secs);
return 0;
}
s32 bdev_mread(struct super_block *sb, u64 secno, struct buffer_head **bh, u64 num_secs, s32 read)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
u8 blksize_bits = sb->s_blocksize_bits;
#ifdef CONFIG_SDFAT_DBG_IOCTL
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
long flags = sbi->debug_flags;
if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
return -EIO;
#endif /* CONFIG_SDFAT_DBG_IOCTL */
if (!fsi->bd_opened)
return -EIO;
brelse(*bh);
if (read)
*bh = __bread(sb->s_bdev, (sector_t)secno, num_secs << blksize_bits);
else
*bh = __getblk(sb->s_bdev, (sector_t)secno, num_secs << blksize_bits);
/* read successfully */
if (*bh)
return 0;
/*
* patch 1.2.4 : reset ONCE warning message per volume.
*/
if (!(fsi->prev_eio & SDFAT_EIO_READ)) {
fsi->prev_eio |= SDFAT_EIO_READ;
sdfat_log_msg(sb, KERN_ERR, "%s: No bh. I/O error.", __func__);
sdfat_debug_warn_on(1);
}
return -EIO;
}
s32 bdev_mwrite(struct super_block *sb, u64 secno, struct buffer_head *bh, u64 num_secs, s32 sync)
{
u64 count;
struct buffer_head *bh2;
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
#ifdef CONFIG_SDFAT_DBG_IOCTL
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
long flags = sbi->debug_flags;
if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
return -EIO;
#endif /* CONFIG_SDFAT_DBG_IOCTL */
if (!fsi->bd_opened)
return -EIO;
if (secno == bh->b_blocknr) {
set_buffer_uptodate(bh);
mark_buffer_dirty(bh);
if (sync && (sync_dirty_buffer(bh) != 0))
return -EIO;
} else {
count = num_secs << sb->s_blocksize_bits;
bh2 = __getblk(sb->s_bdev, (sector_t)secno, count);
if (!bh2)
goto no_bh;
lock_buffer(bh2);
memcpy(bh2->b_data, bh->b_data, count);
set_buffer_uptodate(bh2);
mark_buffer_dirty(bh2);
unlock_buffer(bh2);
if (sync && (sync_dirty_buffer(bh2) != 0)) {
__brelse(bh2);
goto no_bh;
}
__brelse(bh2);
}
return 0;
no_bh:
/*
* patch 1.2.4 : reset ONCE warning message per volume.
*/
if (!(fsi->prev_eio & SDFAT_EIO_WRITE)) {
fsi->prev_eio |= SDFAT_EIO_WRITE;
sdfat_log_msg(sb, KERN_ERR, "%s: No bh. I/O error.", __func__);
sdfat_debug_warn_on(1);
}
return -EIO;
}
s32 bdev_sync_all(struct super_block *sb)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
#ifdef CONFIG_SDFAT_DBG_IOCTL
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
long flags = sbi->debug_flags;
if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
return -EIO;
#endif /* CONFIG_SDFAT_DBG_IOCTL */
if (!fsi->bd_opened)
return -EIO;
return sync_blockdev(sb->s_bdev);
}
/*
* Sector Read/Write Functions
*/
s32 read_sect(struct super_block *sb, u64 sec, struct buffer_head **bh, s32 read)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
BUG_ON(!bh);
if ((sec >= fsi->num_sectors) && (fsi->num_sectors > 0)) {
sdfat_fs_error_ratelimit(sb,
"%s: out of range (sect:%llu)", __func__, sec);
return -EIO;
}
if (bdev_mread(sb, sec, bh, 1, read)) {
sdfat_fs_error_ratelimit(sb,
"%s: I/O error (sect:%llu)", __func__, sec);
return -EIO;
}
return 0;
}
s32 write_sect(struct super_block *sb, u64 sec, struct buffer_head *bh, s32 sync)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
BUG_ON(!bh);
if ((sec >= fsi->num_sectors) && (fsi->num_sectors > 0)) {
sdfat_fs_error_ratelimit(sb,
"%s: out of range (sect:%llu)", __func__, sec);
return -EIO;
}
if (bdev_mwrite(sb, sec, bh, 1, sync)) {
sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu)",
__func__, sec);
return -EIO;
}
return 0;
}
s32 read_msect(struct super_block *sb, u64 sec, struct buffer_head **bh, u64 num_secs, s32 read)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
BUG_ON(!bh);
if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
__func__, sec, num_secs);
return -EIO;
}
if (bdev_mread(sb, sec, bh, num_secs, read)) {
sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu len:%llu)",
__func__, sec, num_secs);
return -EIO;
}
return 0;
}
s32 write_msect(struct super_block *sb, u64 sec, struct buffer_head *bh, u64 num_secs, s32 sync)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
BUG_ON(!bh);
if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
__func__, sec, num_secs);
return -EIO;
}
if (bdev_mwrite(sb, sec, bh, num_secs, sync)) {
sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu len:%llu)",
__func__, sec, num_secs);
return -EIO;
}
return 0;
}
static inline void __blkdev_write_bhs(struct buffer_head **bhs, s32 nr_bhs)
{
s32 i;
for (i = 0; i < nr_bhs; i++)
write_dirty_buffer(bhs[i], WRITE);
}
static inline s32 __blkdev_sync_bhs(struct buffer_head **bhs, s32 nr_bhs)
{
s32 i, err = 0;
for (i = 0; i < nr_bhs; i++) {
wait_on_buffer(bhs[i]);
if (!err && !buffer_uptodate(bhs[i]))
err = -EIO;
}
return err;
}
static inline s32 __buffer_zeroed(struct super_block *sb, u64 blknr, u64 num_secs)
{
struct buffer_head *bhs[MAX_BUF_PER_PAGE];
s32 nr_bhs = MAX_BUF_PER_PAGE;
u64 last_blknr = blknr + num_secs;
s32 err, i, n;
struct blk_plug plug;
/* Zeroing the unused blocks on this cluster */
n = 0;
blk_start_plug(&plug);
while (blknr < last_blknr) {
bhs[n] = sb_getblk(sb, (sector_t)blknr);
if (!bhs[n]) {
err = -ENOMEM;
blk_finish_plug(&plug);
goto error;
}
memset(bhs[n]->b_data, 0, sb->s_blocksize);
set_buffer_uptodate(bhs[n]);
mark_buffer_dirty(bhs[n]);
n++;
blknr++;
if (blknr == last_blknr)
break;
if (n == nr_bhs) {
__blkdev_write_bhs(bhs, n);
for (i = 0; i < n; i++)
brelse(bhs[i]);
n = 0;
}
}
__blkdev_write_bhs(bhs, n);
blk_finish_plug(&plug);
err = __blkdev_sync_bhs(bhs, n);
if (err)
goto error;
for (i = 0; i < n; i++)
brelse(bhs[i]);
return 0;
error:
EMSG("%s: failed zeroed sect %llu\n", __func__, blknr);
for (i = 0; i < n; i++)
bforget(bhs[i]);
return err;
}
s32 write_msect_zero(struct super_block *sb, u64 sec, u64 num_secs)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
__func__, sec, num_secs);
return -EIO;
}
/* Just return -EAGAIN if it is failed */
if (__buffer_zeroed(sb, sec, num_secs))
return -EAGAIN;
return 0;
} /* end of write_msect_zero */
/* end of blkdev.c */