@@ -2122,15 +2122,15 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
2122
2122
return BLK_QC_T_NONE ;
2123
2123
}
2124
2124
2125
- static void handle_bad_sector (struct bio * bio )
2125
+ static void handle_bad_sector (struct bio * bio , sector_t maxsector )
2126
2126
{
2127
2127
char b [BDEVNAME_SIZE ];
2128
2128
2129
2129
printk (KERN_INFO "attempt to access beyond end of device\n" );
2130
2130
printk (KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n" ,
2131
2131
bio_devname (bio , b ), bio -> bi_opf ,
2132
2132
(unsigned long long )bio_end_sector (bio ),
2133
- (long long )get_capacity ( bio -> bi_disk ) );
2133
+ (long long )maxsector );
2134
2134
}
2135
2135
2136
2136
#ifdef CONFIG_FAIL_MAKE_REQUEST
@@ -2191,68 +2191,59 @@ static noinline int should_fail_bio(struct bio *bio)
2191
2191
}
2192
2192
ALLOW_ERROR_INJECTION (should_fail_bio , ERRNO );
2193
2193
2194
+ /*
2195
+ * Check whether this bio extends beyond the end of the device or partition.
2196
+ * This may well happen - the kernel calls bread() without checking the size of
2197
+ * the device, e.g., when mounting a file system.
2198
+ */
2199
+ static inline int bio_check_eod (struct bio * bio , sector_t maxsector )
2200
+ {
2201
+ unsigned int nr_sectors = bio_sectors (bio );
2202
+
2203
+ if (nr_sectors && maxsector &&
2204
+ (nr_sectors > maxsector ||
2205
+ bio -> bi_iter .bi_sector > maxsector - nr_sectors )) {
2206
+ handle_bad_sector (bio , maxsector );
2207
+ return - EIO ;
2208
+ }
2209
+ return 0 ;
2210
+ }
2211
+
2194
2212
/*
2195
2213
* Remap block n of partition p to block n+start(p) of the disk.
2196
2214
*/
2197
2215
static inline int blk_partition_remap (struct bio * bio )
2198
2216
{
2199
2217
struct hd_struct * p ;
2200
- int ret = 0 ;
2218
+ int ret = - EIO ;
2201
2219
2202
2220
rcu_read_lock ();
2203
2221
p = __disk_get_part (bio -> bi_disk , bio -> bi_partno );
2204
- if (unlikely (!p || should_fail_request (p , bio -> bi_iter .bi_size ) ||
2205
- bio_check_ro (bio , p ))) {
2206
- ret = - EIO ;
2222
+ if (unlikely (!p ))
2223
+ goto out ;
2224
+ if (unlikely (should_fail_request (p , bio -> bi_iter .bi_size )))
2225
+ goto out ;
2226
+ if (unlikely (bio_check_ro (bio , p )))
2207
2227
goto out ;
2208
- }
2209
2228
2210
2229
/*
2211
2230
* Zone reset does not include bi_size so bio_sectors() is always 0.
2212
2231
* Include a test for the reset op code and perform the remap if needed.
2213
2232
*/
2214
- if (!bio_sectors (bio ) && bio_op (bio ) != REQ_OP_ZONE_RESET )
2215
- goto out ;
2216
-
2217
- bio -> bi_iter .bi_sector += p -> start_sect ;
2218
- bio -> bi_partno = 0 ;
2219
- trace_block_bio_remap (bio -> bi_disk -> queue , bio , part_devt (p ),
2220
- bio -> bi_iter .bi_sector - p -> start_sect );
2221
-
2233
+ if (bio_sectors (bio ) || bio_op (bio ) == REQ_OP_ZONE_RESET ) {
2234
+ if (bio_check_eod (bio , part_nr_sects_read (p )))
2235
+ goto out ;
2236
+ bio -> bi_iter .bi_sector += p -> start_sect ;
2237
+ bio -> bi_partno = 0 ;
2238
+ trace_block_bio_remap (bio -> bi_disk -> queue , bio , part_devt (p ),
2239
+ bio -> bi_iter .bi_sector - p -> start_sect );
2240
+ }
2241
+ ret = 0 ;
2222
2242
out :
2223
2243
rcu_read_unlock ();
2224
2244
return ret ;
2225
2245
}
2226
2246
2227
- /*
2228
- * Check whether this bio extends beyond the end of the device.
2229
- */
2230
- static inline int bio_check_eod (struct bio * bio , unsigned int nr_sectors )
2231
- {
2232
- sector_t maxsector ;
2233
-
2234
- if (!nr_sectors )
2235
- return 0 ;
2236
-
2237
- /* Test device or partition size, when known. */
2238
- maxsector = get_capacity (bio -> bi_disk );
2239
- if (maxsector ) {
2240
- sector_t sector = bio -> bi_iter .bi_sector ;
2241
-
2242
- if (maxsector < nr_sectors || maxsector - nr_sectors < sector ) {
2243
- /*
2244
- * This may well happen - the kernel calls bread()
2245
- * without checking the size of the device, e.g., when
2246
- * mounting a device.
2247
- */
2248
- handle_bad_sector (bio );
2249
- return 1 ;
2250
- }
2251
- }
2252
-
2253
- return 0 ;
2254
- }
2255
-
2256
2247
static noinline_for_stack bool
2257
2248
generic_make_request_checks (struct bio * bio )
2258
2249
{
@@ -2263,9 +2254,6 @@ generic_make_request_checks(struct bio *bio)
2263
2254
2264
2255
might_sleep ();
2265
2256
2266
- if (bio_check_eod (bio , nr_sectors ))
2267
- goto end_io ;
2268
-
2269
2257
q = bio -> bi_disk -> queue ;
2270
2258
if (unlikely (!q )) {
2271
2259
printk (KERN_ERR
@@ -2285,17 +2273,16 @@ generic_make_request_checks(struct bio *bio)
2285
2273
if (should_fail_bio (bio ))
2286
2274
goto end_io ;
2287
2275
2288
- if (! bio -> bi_partno ) {
2289
- if (unlikely (bio_check_ro (bio , & bio -> bi_disk -> part0 )))
2276
+ if (bio -> bi_partno ) {
2277
+ if (unlikely (blk_partition_remap (bio )))
2290
2278
goto end_io ;
2291
2279
} else {
2292
- if (blk_partition_remap (bio ))
2280
+ if (unlikely (bio_check_ro (bio , & bio -> bi_disk -> part0 )))
2281
+ goto end_io ;
2282
+ if (unlikely (bio_check_eod (bio , get_capacity (bio -> bi_disk ))))
2293
2283
goto end_io ;
2294
2284
}
2295
2285
2296
- if (bio_check_eod (bio , nr_sectors ))
2297
- goto end_io ;
2298
-
2299
2286
/*
2300
2287
* Filter flush bio's early so that make_request based
2301
2288
* drivers without flush support don't have to worry
0 commit comments