@@ -135,6 +135,32 @@ static void __blkdev_issue_write_zeroes(struct block_device *bdev,
135
135
* biop = bio ;
136
136
}
137
137
138
+ static int blkdev_issue_write_zeroes (struct block_device * bdev , sector_t sector ,
139
+ sector_t nr_sects , gfp_t gfp , unsigned flags )
140
+ {
141
+ struct bio * bio = NULL ;
142
+ struct blk_plug plug ;
143
+ int ret = 0 ;
144
+
145
+ blk_start_plug (& plug );
146
+ __blkdev_issue_write_zeroes (bdev , sector , nr_sects , gfp , & bio , flags );
147
+ if (bio ) {
148
+ ret = submit_bio_wait (bio );
149
+ bio_put (bio );
150
+ }
151
+ blk_finish_plug (& plug );
152
+
153
+ /*
154
+ * For some devices there is no non-destructive way to verify whether
155
+ * WRITE ZEROES is actually supported. These will clear the capability
156
+ * on an I/O error, in which case we'll turn any error into
157
+ * "not supported" here.
158
+ */
159
+ if (ret && !bdev_write_zeroes_sectors (bdev ))
160
+ return - EOPNOTSUPP ;
161
+ return ret ;
162
+ }
163
+
138
164
/*
139
165
* Convert a number of 512B sectors to a number of pages.
140
166
* The result is limited to a number of pages that can fit into a BIO.
@@ -175,6 +201,27 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev,
175
201
* biop = bio ;
176
202
}
177
203
204
+ static int blkdev_issue_zero_pages (struct block_device * bdev , sector_t sector ,
205
+ sector_t nr_sects , gfp_t gfp , unsigned flags )
206
+ {
207
+ struct bio * bio = NULL ;
208
+ struct blk_plug plug ;
209
+ int ret = 0 ;
210
+
211
+ if (flags & BLKDEV_ZERO_NOFALLBACK )
212
+ return - EOPNOTSUPP ;
213
+
214
+ blk_start_plug (& plug );
215
+ __blkdev_issue_zero_pages (bdev , sector , nr_sects , gfp , & bio );
216
+ if (bio ) {
217
+ ret = submit_bio_wait (bio );
218
+ bio_put (bio );
219
+ }
220
+ blk_finish_plug (& plug );
221
+
222
+ return ret ;
223
+ }
224
+
178
225
/**
179
226
* __blkdev_issue_zeroout - generate number of zero filed write bios
180
227
* @bdev: blockdev to issue
@@ -230,52 +277,21 @@ EXPORT_SYMBOL(__blkdev_issue_zeroout);
230
277
int blkdev_issue_zeroout (struct block_device * bdev , sector_t sector ,
231
278
sector_t nr_sects , gfp_t gfp_mask , unsigned flags )
232
279
{
233
- int ret = 0 ;
234
- sector_t bs_mask ;
235
- struct bio * bio ;
236
- struct blk_plug plug ;
237
- bool try_write_zeroes = !!bdev_write_zeroes_sectors (bdev );
280
+ int ret ;
238
281
239
- bs_mask = (bdev_logical_block_size (bdev ) >> 9 ) - 1 ;
240
- if ((sector | nr_sects ) & bs_mask )
282
+ if ((sector | nr_sects ) & ((bdev_logical_block_size (bdev ) >> 9 ) - 1 ))
241
283
return - EINVAL ;
242
284
if (bdev_read_only (bdev ))
243
285
return - EPERM ;
244
- if ((flags & BLKDEV_ZERO_NOFALLBACK ) && !try_write_zeroes )
245
- return - EOPNOTSUPP ;
246
286
247
- retry :
248
- bio = NULL ;
249
- blk_start_plug (& plug );
250
- if (try_write_zeroes ) {
251
- __blkdev_issue_write_zeroes (bdev , sector , nr_sects , gfp_mask ,
252
- & bio , flags );
253
- } else {
254
- __blkdev_issue_zero_pages (bdev , sector , nr_sects , gfp_mask ,
255
- & bio );
256
- }
257
- if (bio ) {
258
- ret = submit_bio_wait (bio );
259
- bio_put (bio );
260
- }
261
- blk_finish_plug (& plug );
262
- if (ret && try_write_zeroes ) {
263
- if (!(flags & BLKDEV_ZERO_NOFALLBACK )) {
264
- try_write_zeroes = false;
265
- goto retry ;
266
- }
267
- if (!bdev_write_zeroes_sectors (bdev )) {
268
- /*
269
- * Zeroing offload support was indicated, but the
270
- * device reported ILLEGAL REQUEST (for some devices
271
- * there is no non-destructive way to verify whether
272
- * WRITE ZEROES is actually supported).
273
- */
274
- ret = - EOPNOTSUPP ;
275
- }
287
+ if (bdev_write_zeroes_sectors (bdev )) {
288
+ ret = blkdev_issue_write_zeroes (bdev , sector , nr_sects ,
289
+ gfp_mask , flags );
290
+ if (!ret )
291
+ return ret ;
276
292
}
277
293
278
- return ret ;
294
+ return blkdev_issue_zero_pages ( bdev , sector , nr_sects , gfp_mask , flags ) ;
279
295
}
280
296
EXPORT_SYMBOL (blkdev_issue_zeroout );
281
297
0 commit comments