block: add a flag BLK_MQ_F_SG_MERGE to more block drivers

Block drivers such as mtip32xx and null_blk should use
BLK_MQ_F_SG_MERGE, so that they don't crash with multipage bvecs.

Signed-off-by: Dongsu Park <dongsu.park@profitbricks.com>
btrfs: make __btrfs_subio_endio_read calculate bv_len accurately

Make __btrfs_subio_endio_read() calculate bv_len accurately, taking
into account PAGE_CACHE_SIZE. Its goal is to fix the following errors.

   BTRFS info (device vdb3): csum failed ino 268 off 0 csum 558635621 expected csum 2566472073
   BTRFS info (device vdb3): csum failed ino 268 off 65536 csum 558635621 expected csum 2566472073

Now xfstests/generic/113 is passed.

TODO: change bio_for_each_page_all() to set bvec.bv_len = min(bvec.bv_len, PAGE_SIZE)?

Signed-off-by: Ming Lin <mlin@minggr.net>
[dpark: added more description, changed coding style a bit]
Signed-off-by: Dongsu Park <dongsu.park@profitbricks.com>
ext4: fix bvec.bv_len returned from bio_for_each_page_all()

Signed-off-by: Ming Lin <mlin@minggr.net>
block: count physical segments through multipage biovecs

As it's now possible for biovecs to have multiple pages,
__blk_recalc_rq_segments() should also support iterating biovecs
using bio_for_each_page() for each bio. Otherwise kernel could
occasionally crash with the following bug, especially when tested
with virtio-blk.

------------[ cut here ]------------
kernel BUG at drivers/block/virtio_blk.c:172!
CPU: 1 PID: 13523 Comm: mount Not tainted 3.18.0+ #106
Call Trace:
 [<ffffffff814ddb78>] __blk_mq_run_hw_queue+0x1b8/0x310
 [<ffffffff814de53d>] blk_mq_run_hw_queue+0x6d/0x90
 [<ffffffff814df49d>] blk_mq_insert_requests+0xfd/0x2c0
 [<ffffffff814e03bb>] blk_mq_flush_plug_list+0x12b/0x140
 [<ffffffff814d4aa1>] blk_flush_plug_list+0xb1/0x200
 [<ffffffff814d51a8>] blk_finish_plug+0x18/0x50
 [<ffffffff813845de>] _xfs_buf_ioapply+0x2fe/0x3e0
 [<ffffffff813858a6>] ? xfs_bwrite+0x46/0x80
 [<ffffffff81384de2>] xfs_buf_submit_wait+0xb2/0x6d0
 [<ffffffff813858a6>] xfs_bwrite+0x46/0x80
 [<ffffffff813bba44>] xlog_bwrite+0xc4/0x160
 [<ffffffff813bcb5b>] xlog_write_log_records+0x1ab/0x240
 [<ffffffff813bcd00>] xlog_clear_stale_blocks+0x110/0x1e0
 [<ffffffff813bd10b>] xlog_find_tail+0x33b/0x430
 [<ffffffff813c3a7e>] xlog_recover+0x1e/0xf0
 [<ffffffff813af73c>] xfs_log_mount+0x28c/0x300
 [<ffffffff813a4dc5>] xfs_mountfs+0x4a5/0x9c0
 [<ffffffff813a9672>] xfs_fs_fill_super+0x312/0x390
 [<ffffffff812237a4>] mount_bdev+0x1a4/0x1e0
 [<ffffffff813a9360>] ? xfs_finish_flags+0x170/0x170
 [<ffffffff813a7255>] xfs_fs_mount+0x15/0x20
 [<ffffffff81224129>] mount_fs+0x39/0x1b0
 [<ffffffff811c7495>] ? __alloc_percpu+0x15/0x20
 [<ffffffff8124372b>] vfs_kern_mount+0x6b/0x150
 [<ffffffff81246680>] do_mount+0x210/0xb90
 [<ffffffff811d181f>] ? might_fault+0x5f/0xb0
 [<ffffffff8124733b>] SyS_mount+0x8b/0xe0
 [<ffffffff817e3092>] system_call_fastpath+0x12/0x17
RIP [<ffffffff81620c27>] virtio_queue_rq+0x277/0x280
---[ end trace d56c2abcb8ac6962 ]---

Signed-off-by: Dongsu Park <dongsu.park@profitbricks.com>
block: plugging

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
[dpark: fix compile errors]
md: get rid of bio_list from struct task
blk-mq: remove also remaining calls to blk_attempt_plug_merge()
block: fix panic in generic_make_request()

bio should be added to splits.
Fixed below panic.

[    1.112557] BUG: unable to handle kernel NULL pointer dereference at           (null)
[    1.112563] IP: [<ffffffff812bc2d3>] generic_make_request+0x63/0x9d
[    1.112564] PGD 0
[    1.112566] Oops: 0002 [#1] PREEMPT SMP
[    1.112568] Modules linked in:
[    1.112570] CPU: 3 PID: 530 Comm: kworker/u8:2 Not tainted 3.18.0-00033-ge76e0b3 #41
[    1.112571] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
[    1.112575] Workqueue: events_unbound async_run_entry_fn
[    1.112576] task: ffff88001d8eae90 ti: ffff88001dad0000 task.ti: ffff88001dad0000
[    1.112579] RIP: 0010:[<ffffffff812bc2d3>]  [<ffffffff812bc2d3>] generic_make_request+0x63/0x9d
[    1.112579] RSP: 0000:ffff88001dad3798  EFLAGS: 00010246
[    1.112580] RAX: 0000000000000000 RBX: ffff88001d8eae90 RCX: 0000000000000008
[    1.112581] RDX: ffff88001dad3798 RSI: 0000000000000000 RDI: ffff88001dad3798
[    1.112582] RBP: ffff88001dad37d8 R08: ffff88001e2908b8 R09: 000000000000000d
[    1.112583] R10: 000000000000000d R11: 000000000000000c R12: ffff88001dad3798
[    1.112583] R13: 0000000000000000 R14: 0000000000000000 R15: ffffea000078cb80
[    1.112585] FS:  0000000000000000(0000) GS:ffff88001fd80000(0000) knlGS:0000000000000000
[    1.112586] CS:  0010 DS: 0000 ES: 0000 CR0: 000000008005003b
[    1.112588] CR2: 0000000000000000 CR3: 000000000180e000 CR4: 00000000000006e0
[    1.112593] Stack:
[    1.112595]  0000000000000000 0000000000000000 ffff88001dad37a8 ffff88001dad37a8
[    1.112597]  ffff88001dad37b8 ffff88001dad37b8 ffff88001e290840 0000000000000008
[    1.112599]  ffff88001dad3838 ffffffff812bc404 ffff88001dad3808 0000000481059f5e
[    1.112599] Call Trace:
[    1.112603]  [<ffffffff812bc404>] submit_bio+0xf7/0x136
[    1.112607]  [<ffffffff8114a1b9>] _submit_bh+0x104/0x12e
[    1.112610]  [<ffffffff8114a1f3>] submit_bh+0x10/0x12
[    1.112612]  [<ffffffff8114a4be>] block_read_full_page+0x24a/0x266
[    1.112614]  [<ffffffff8114be05>] ? I_BDEV+0xd/0xd
[    1.112618]  [<ffffffff812e8bd2>] ? debug_smp_processor_id+0x17/0x19
[    1.112621]  [<ffffffff810dd817>] ? __lru_cache_add+0x72/0x88
[    1.112623]  [<ffffffff8114c4e8>] ? blkdev_readpages+0x1f/0x1f
[    1.112625]  [<ffffffff8114c500>] blkdev_readpage+0x18/0x1a
[    1.112628]  [<ffffffff810d2f1f>] do_read_cache_page+0x97/0x16f
[    1.112630]  [<ffffffff810d3013>] read_cache_page+0x1c/0x1e
[    1.112633]  [<ffffffff812c98cf>] read_dev_sector+0x2d/0x87
[    1.112636]  [<ffffffff812cc283>] read_lba+0xb1/0x105
[    1.112638]  [<ffffffff812cc6e4>] ? find_valid_gpt+0x80/0x4d4
[    1.112640]  [<ffffffff812cc705>] find_valid_gpt+0xa1/0x4d4
[    1.112642]  [<ffffffff812dd430>] ? string.isra.6+0x3d/0xa2
[    1.112645]  [<ffffffff812ccbb4>] efi_partition+0x7c/0x364
[    1.112647]  [<ffffffff812de3b4>] ? vsnprintf+0x82/0x3a7
[    1.112649]  [<ffffffff812de77f>] ? snprintf+0x34/0x36
[    1.112651]  [<ffffffff812ca773>] check_partition+0x11e/0x1c3
[    1.112653]  [<ffffffff812c9e53>] rescan_partitions+0x98/0x271
[    1.112657]  [<ffffffff814818c8>] ? _raw_spin_lock+0x18/0x37
[    1.112659]  [<ffffffff8114d32b>] __blkdev_get+0x1c7/0x3f6
[    1.112660]  [<ffffffff814818c8>] ? _raw_spin_lock+0x18/0x37
[    1.112663]  [<ffffffff8114d5b8>] blkdev_get+0x5e/0x2a9
[    1.112666]  [<ffffffff8113513b>] ? unlock_new_inode+0x5c/0x61
[    1.112668]  [<ffffffff8114c3cb>] ? bdget+0x120/0x12f
[    1.112670]  [<ffffffff81330a9e>] ? put_device+0x17/0x19
[    1.112672]  [<ffffffff812c7e17>] add_disk+0x286/0x3f9
[    1.112676]  [<ffffffff8136ab9f>] sd_probe_async+0x106/0x19d
[    1.112678]  [<ffffffff81053a9c>] async_run_entry_fn+0x71/0x138
[    1.112681]  [<ffffffff8104d603>] process_one_work+0x1c7/0x36f
[    1.112683]  [<ffffffff8104e090>] worker_thread+0x28b/0x37f
[    1.112685]  [<ffffffff8104de05>] ? rescuer_thread+0x226/0x226
[    1.112687]  [<ffffffff810519a8>] kthread+0xdb/0xe3
[    1.112689]  [<ffffffff81480000>] ? __ww_mutex_lock_slowpath+0xcc/0x1c3
[    1.112691]  [<ffffffff810518cd>] ? kthread_create_on_node+0x17f/0x17f
[    1.112693]  [<ffffffff8148226c>] ret_from_fork+0x7c/0xb0
[    1.112695]  [<ffffffff810518cd>] ? kthread_create_on_node+0x17f/0x17f

Signed-off-by: Ming Lin <mlin@minggr.net>
direct-io: Rewrite based on immutable biovecs

Near total rewrite of fs/direct-io.c. This makes use of our new bio
splitting functionality, and the fact that generic_make_request() will
take arbitrary size bios - we allocate a bio, pin pages to it directly,
then call the getblocks() function to map it wherever the filesystem
tells us - splitting as needed.

Appears to pass xfstests with CONFIG_XFS_DEBUG=y (would appreciate testing from
someone who uses xfstests more than I).

Doesn't quite work with btrfs yet - when running xfstests it eventually get
stuck in an infinite loop somewhere. First thing in the kernel log is a warning
at fs/btrfs/ordered-data.c:288 in btrfs_add_ordered_sum() - if any of the btrfs
people want to take a look that would be a great help.

It _definitely_ needs review/auditing from someone intimately familiar with the
expected behaviour w.r.t. everything related to filesystem semantics and the
handling of the getblocks() call - dio_send_bio() is what primarily implements
that behaviour.

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
dio: use submit_io() for block zeroing
block: kill bio_get_nr_vecs()

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
btrfs: fix compile error on calling btrfs_dio_bio_alloc()
foo

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
more dio rewriting
fs/dio: wait for completion for dio inside the loop

Fix compile errors, by waiting for completion for dio inside loop,
as well as checking dio->async instead of dio->refcount.