@@ -101,44 +101,35 @@ EXPORT_SYMBOL_GPL(blkdev_nr_zones);
101101 * blkdev_report_zones - Get zones information
102102 * @bdev: Target block device
103103 * @sector: Sector from which to report zones
104- * @zones: Array of zone structures where to return the zones information
105- * @nr_zones: Number of zone structures in the zone array
104+ * @nr_zones: Maximum number of zones to report
105+ * @cb: Callback function called for each reported zone
106+ * @data: Private data for the callback
106107 *
107108 * Description:
108- * Get zone information starting from the zone containing @sector.
109- * The number of zone information reported may be less than the number
110- * requested by @nr_zones. The number of zones actually reported is
111- * returned in @nr_zones.
112- * The caller must use memalloc_noXX_save/restore() calls to control
113- * memory allocations done within this function (zone array and command
114- * buffer allocation by the device driver).
109+ * Get zone information starting from the zone containing @sector for at most
110+ * @nr_zones, and call @cb for each zone reported by the device.
111+ * To report all zones in a device starting from @sector, the BLK_ALL_ZONES
112+ * constant can be passed to @nr_zones.
113+ * Returns the number of zones reported by the device, or a negative errno
114+ * value in case of failure.
115+ *
116+ * Note: The caller must use memalloc_noXX_save/restore() calls to control
117+ * memory allocations done within this function.
115118 */
116119int blkdev_report_zones (struct block_device * bdev , sector_t sector ,
117- struct blk_zone * zones , unsigned int * nr_zones )
120+ unsigned int nr_zones , report_zones_cb cb , void * data )
118121{
119- struct request_queue * q = bdev_get_queue (bdev );
120122 struct gendisk * disk = bdev -> bd_disk ;
121123 sector_t capacity = get_capacity (disk );
122124
123- if (!blk_queue_is_zoned (q ))
124- return - EOPNOTSUPP ;
125-
126- /*
127- * A block device that advertized itself as zoned must have a
128- * report_zones method. If it does not have one defined, the device
129- * driver has a bug. So warn about that.
130- */
131- if (WARN_ON_ONCE (!disk -> fops -> report_zones ))
125+ if (!blk_queue_is_zoned (bdev_get_queue (bdev )) ||
126+ WARN_ON_ONCE (!disk -> fops -> report_zones ))
132127 return - EOPNOTSUPP ;
133128
134- if (!* nr_zones || sector >= capacity ) {
135- * nr_zones = 0 ;
129+ if (!nr_zones || sector >= capacity )
136130 return 0 ;
137- }
138131
139- * nr_zones = min (* nr_zones , __blkdev_nr_zones (q , capacity - sector ));
140-
141- return disk -> fops -> report_zones (disk , sector , zones , nr_zones );
132+ return disk -> fops -> report_zones (disk , sector , nr_zones , cb , data );
142133}
143134EXPORT_SYMBOL_GPL (blkdev_report_zones );
144135
@@ -232,6 +223,20 @@ int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op,
232223}
233224EXPORT_SYMBOL_GPL (blkdev_zone_mgmt );
234225
226+ struct zone_report_args {
227+ struct blk_zone __user * zones ;
228+ };
229+
230+ static int blkdev_copy_zone_to_user (struct blk_zone * zone , unsigned int idx ,
231+ void * data )
232+ {
233+ struct zone_report_args * args = data ;
234+
235+ if (copy_to_user (& args -> zones [idx ], zone , sizeof (struct blk_zone )))
236+ return - EFAULT ;
237+ return 0 ;
238+ }
239+
235240/*
236241 * BLKREPORTZONE ioctl processing.
237242 * Called from blkdev_ioctl.
@@ -240,9 +245,9 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
240245 unsigned int cmd , unsigned long arg )
241246{
242247 void __user * argp = (void __user * )arg ;
248+ struct zone_report_args args ;
243249 struct request_queue * q ;
244250 struct blk_zone_report rep ;
245- struct blk_zone * zones ;
246251 int ret ;
247252
248253 if (!argp )
@@ -264,32 +269,16 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
264269 if (!rep .nr_zones )
265270 return - EINVAL ;
266271
267- rep .nr_zones = min (blkdev_nr_zones (bdev ), rep .nr_zones );
268-
269- zones = kvmalloc_array (rep .nr_zones , sizeof (struct blk_zone ),
270- GFP_KERNEL | __GFP_ZERO );
271- if (!zones )
272- return - ENOMEM ;
273-
274- ret = blkdev_report_zones (bdev , rep .sector , zones , & rep .nr_zones );
275- if (ret )
276- goto out ;
277-
278- if (copy_to_user (argp , & rep , sizeof (struct blk_zone_report ))) {
279- ret = - EFAULT ;
280- goto out ;
281- }
282-
283- if (rep .nr_zones ) {
284- if (copy_to_user (argp + sizeof (struct blk_zone_report ), zones ,
285- sizeof (struct blk_zone ) * rep .nr_zones ))
286- ret = - EFAULT ;
287- }
272+ args .zones = argp + sizeof (struct blk_zone_report );
273+ ret = blkdev_report_zones (bdev , rep .sector , rep .nr_zones ,
274+ blkdev_copy_zone_to_user , & args );
275+ if (ret < 0 )
276+ return ret ;
288277
289- out :
290- kvfree ( zones );
291-
292- return ret ;
278+ rep . nr_zones = ret ;
279+ if ( copy_to_user ( argp , & rep , sizeof ( struct blk_zone_report )))
280+ return - EFAULT ;
281+ return 0 ;
293282}
294283
295284/*
@@ -351,31 +340,6 @@ static inline unsigned long *blk_alloc_zone_bitmap(int node,
351340 GFP_NOIO , node );
352341}
353342
354- /*
355- * Allocate an array of struct blk_zone to get nr_zones zone information.
356- * The allocated array may be smaller than nr_zones.
357- */
358- static struct blk_zone * blk_alloc_zones (unsigned int * nr_zones )
359- {
360- struct blk_zone * zones ;
361- size_t nrz = min (* nr_zones , BLK_ZONED_REPORT_MAX_ZONES );
362-
363- /*
364- * GFP_KERNEL here is meaningless as the caller task context has
365- * the PF_MEMALLOC_NOIO flag set in blk_revalidate_disk_zones()
366- * with memalloc_noio_save().
367- */
368- zones = kvcalloc (nrz , sizeof (struct blk_zone ), GFP_KERNEL );
369- if (!zones ) {
370- * nr_zones = 0 ;
371- return NULL ;
372- }
373-
374- * nr_zones = nrz ;
375-
376- return zones ;
377- }
378-
379343void blk_queue_free_zone_bitmaps (struct request_queue * q )
380344{
381345 kfree (q -> seq_zones_bitmap );
@@ -384,12 +348,21 @@ void blk_queue_free_zone_bitmaps(struct request_queue *q)
384348 q -> seq_zones_wlock = NULL ;
385349}
386350
351+ struct blk_revalidate_zone_args {
352+ struct gendisk * disk ;
353+ unsigned long * seq_zones_bitmap ;
354+ unsigned long * seq_zones_wlock ;
355+ sector_t sector ;
356+ };
357+
387358/*
388359 * Helper function to check the validity of zones of a zoned block device.
389360 */
390- static bool blk_zone_valid (struct gendisk * disk , struct blk_zone * zone ,
391- sector_t * sector )
361+ static int blk_revalidate_zone_cb (struct blk_zone * zone , unsigned int idx ,
362+ void * data )
392363{
364+ struct blk_revalidate_zone_args * args = data ;
365+ struct gendisk * disk = args -> disk ;
393366 struct request_queue * q = disk -> queue ;
394367 sector_t zone_sectors = blk_queue_zone_sectors (q );
395368 sector_t capacity = get_capacity (disk );
@@ -409,14 +382,14 @@ static bool blk_zone_valid(struct gendisk *disk, struct blk_zone *zone,
409382 zone -> len > zone_sectors ) {
410383 pr_warn ("%s: Invalid zoned device with larger last zone size\n" ,
411384 disk -> disk_name );
412- return false ;
385+ return - ENODEV ;
413386 }
414387
415388 /* Check for holes in the zone report */
416- if (zone -> start != * sector ) {
389+ if (zone -> start != args -> sector ) {
417390 pr_warn ("%s: Zone gap at sectors %llu..%llu\n" ,
418- disk -> disk_name , * sector , zone -> start );
419- return false ;
391+ disk -> disk_name , args -> sector , zone -> start );
392+ return - ENODEV ;
420393 }
421394
422395 /* Check zone type */
@@ -428,12 +401,38 @@ static bool blk_zone_valid(struct gendisk *disk, struct blk_zone *zone,
428401 default :
429402 pr_warn ("%s: Invalid zone type 0x%x at sectors %llu\n" ,
430403 disk -> disk_name , (int )zone -> type , zone -> start );
431- return false ;
404+ return - ENODEV ;
432405 }
433406
434- * sector += zone -> len ;
407+ if (zone -> type != BLK_ZONE_TYPE_CONVENTIONAL )
408+ set_bit (idx , args -> seq_zones_bitmap );
435409
436- return true;
410+ args -> sector += zone -> len ;
411+ return 0 ;
412+ }
413+
414+ static int blk_update_zone_info (struct gendisk * disk , unsigned int nr_zones ,
415+ struct blk_revalidate_zone_args * args )
416+ {
417+ /*
418+ * Ensure that all memory allocations in this context are done as
419+ * if GFP_NOIO was specified.
420+ */
421+ unsigned int noio_flag = memalloc_noio_save ();
422+ struct request_queue * q = disk -> queue ;
423+ int ret ;
424+
425+ args -> seq_zones_wlock = blk_alloc_zone_bitmap (q -> node , nr_zones );
426+ if (!args -> seq_zones_wlock )
427+ return - ENOMEM ;
428+ args -> seq_zones_bitmap = blk_alloc_zone_bitmap (q -> node , nr_zones );
429+ if (!args -> seq_zones_bitmap )
430+ return - ENOMEM ;
431+
432+ ret = disk -> fops -> report_zones (disk , 0 , nr_zones ,
433+ blk_revalidate_zone_cb , args );
434+ memalloc_noio_restore (noio_flag );
435+ return ret ;
437436}
438437
439438/**
@@ -449,11 +448,7 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
449448{
450449 struct request_queue * q = disk -> queue ;
451450 unsigned int nr_zones = __blkdev_nr_zones (q , get_capacity (disk ));
452- unsigned long * seq_zones_wlock = NULL , * seq_zones_bitmap = NULL ;
453- unsigned int i , rep_nr_zones = 0 , z = 0 , nrz ;
454- struct blk_zone * zones = NULL ;
455- unsigned int noio_flag ;
456- sector_t sector = 0 ;
451+ struct blk_revalidate_zone_args args = { .disk = disk };
457452 int ret = 0 ;
458453
459454 if (WARN_ON_ONCE (!blk_queue_is_zoned (q )))
@@ -468,82 +463,28 @@ int blk_revalidate_disk_zones(struct gendisk *disk)
468463 return 0 ;
469464 }
470465
471- /*
472- * Ensure that all memory allocations in this context are done as
473- * if GFP_NOIO was specified.
474- */
475- noio_flag = memalloc_noio_save ();
476-
477- if (!nr_zones )
478- goto update ;
479-
480- /* Allocate bitmaps */
481- ret = - ENOMEM ;
482- seq_zones_wlock = blk_alloc_zone_bitmap (q -> node , nr_zones );
483- if (!seq_zones_wlock )
484- goto out ;
485- seq_zones_bitmap = blk_alloc_zone_bitmap (q -> node , nr_zones );
486- if (!seq_zones_bitmap )
487- goto out ;
488-
489- /*
490- * Get zone information to check the zones and initialize
491- * seq_zones_bitmap.
492- */
493- rep_nr_zones = nr_zones ;
494- zones = blk_alloc_zones (& rep_nr_zones );
495- if (!zones )
496- goto out ;
497-
498- while (z < nr_zones ) {
499- nrz = min (nr_zones - z , rep_nr_zones );
500- ret = disk -> fops -> report_zones (disk , sector , zones , & nrz );
501- if (ret )
502- goto out ;
503- if (!nrz )
504- break ;
505- for (i = 0 ; i < nrz ; i ++ ) {
506- if (!blk_zone_valid (disk , & zones [i ], & sector )) {
507- ret = - ENODEV ;
508- goto out ;
509- }
510- if (zones [i ].type != BLK_ZONE_TYPE_CONVENTIONAL )
511- set_bit (z , seq_zones_bitmap );
512- z ++ ;
513- }
514- }
515-
516- if (WARN_ON (z != nr_zones )) {
517- ret = - EIO ;
518- goto out ;
519- }
466+ if (nr_zones )
467+ ret = blk_update_zone_info (disk , nr_zones , & args );
520468
521- update :
522469 /*
523470 * Install the new bitmaps, making sure the queue is stopped and
524471 * all I/Os are completed (i.e. a scheduler is not referencing the
525472 * bitmaps).
526473 */
527474 blk_mq_freeze_queue (q );
528- q -> nr_zones = nr_zones ;
529- swap (q -> seq_zones_wlock , seq_zones_wlock );
530- swap (q -> seq_zones_bitmap , seq_zones_bitmap );
531- blk_mq_unfreeze_queue (q );
532-
533- out :
534- memalloc_noio_restore (noio_flag );
535-
536- kvfree (zones );
537- kfree (seq_zones_wlock );
538- kfree (seq_zones_bitmap );
539-
540- if (ret ) {
475+ if (ret >= 0 ) {
476+ q -> nr_zones = nr_zones ;
477+ swap (q -> seq_zones_wlock , args .seq_zones_wlock );
478+ swap (q -> seq_zones_bitmap , args .seq_zones_bitmap );
479+ ret = 0 ;
480+ } else {
541481 pr_warn ("%s: failed to revalidate zones\n" , disk -> disk_name );
542- blk_mq_freeze_queue (q );
543482 blk_queue_free_zone_bitmaps (q );
544- blk_mq_unfreeze_queue (q );
545483 }
484+ blk_mq_unfreeze_queue (q );
546485
486+ kfree (args .seq_zones_wlock );
487+ kfree (args .seq_zones_bitmap );
547488 return ret ;
548489}
549490EXPORT_SYMBOL_GPL (blk_revalidate_disk_zones );
0 commit comments