Skip to content

Commit 29eaadc

Browse files
josefbacikaxboe
authored andcommitted
nbd: stop using the bdev everywhere
In preparation for the upcoming netlink interface we need to not rely on already having the bdev for the NBD device we are doing operations on. Instead of passing the bdev around, just use it in places where we know we already have the bdev. Signed-off-by: Josef Bacik <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 5ea8d10 commit 29eaadc

File tree

1 file changed

+37
-55
lines changed

1 file changed

+37
-55
lines changed

drivers/block/nbd.c

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ static inline struct device *nbd_to_dev(struct nbd_device *nbd)
120120
return disk_to_dev(nbd->disk);
121121
}
122122

123-
static bool nbd_is_connected(struct nbd_device *nbd)
124-
{
125-
return !!nbd->task_recv;
126-
}
127-
128123
static const char *nbdcmd_to_ascii(int cmd)
129124
{
130125
switch (cmd) {
@@ -160,36 +155,30 @@ static void nbd_mark_nsock_dead(struct nbd_sock *nsock)
160155
nsock->sent = 0;
161156
}
162157

163-
static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
158+
static void nbd_size_clear(struct nbd_device *nbd)
164159
{
165160
if (nbd->config->bytesize) {
166-
if (bdev->bd_openers <= 1)
167-
bd_set_size(bdev, 0);
168161
set_capacity(nbd->disk, 0);
169162
kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
170163
}
171-
172-
return 0;
173164
}
174165

175-
static void nbd_size_update(struct nbd_device *nbd, struct block_device *bdev)
166+
static void nbd_size_update(struct nbd_device *nbd)
176167
{
177168
struct nbd_config *config = nbd->config;
178169
blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
179170
blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
180-
bd_set_size(bdev, config->bytesize);
181171
set_capacity(nbd->disk, config->bytesize >> 9);
182172
kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
183173
}
184174

185-
static void nbd_size_set(struct nbd_device *nbd, struct block_device *bdev,
186-
loff_t blocksize, loff_t nr_blocks)
175+
static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
176+
loff_t nr_blocks)
187177
{
188178
struct nbd_config *config = nbd->config;
189179
config->blksize = blocksize;
190180
config->bytesize = blocksize * nr_blocks;
191-
if (nbd_is_connected(nbd))
192-
nbd_size_update(nbd, bdev);
181+
nbd_size_update(nbd);
193182
}
194183

195184
static void nbd_end_request(struct nbd_cmd *cmd)
@@ -739,8 +728,7 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
739728
return ret;
740729
}
741730

742-
static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
743-
unsigned long arg)
731+
static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg)
744732
{
745733
struct nbd_config *config = nbd->config;
746734
struct socket *sock;
@@ -783,8 +771,6 @@ static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
783771
nsock->sent = 0;
784772
socks[config->num_connections++] = nsock;
785773

786-
if (max_part)
787-
bdev->bd_invalidated = 1;
788774
return 0;
789775
}
790776

@@ -800,19 +786,20 @@ static void nbd_bdev_reset(struct block_device *bdev)
800786
{
801787
if (bdev->bd_openers > 1)
802788
return;
803-
set_device_ro(bdev, false);
804-
bdev->bd_inode->i_size = 0;
789+
bd_set_size(bdev, 0);
805790
if (max_part > 0) {
806791
blkdev_reread_part(bdev);
807792
bdev->bd_invalidated = 1;
808793
}
809794
}
810795

811-
static void nbd_parse_flags(struct nbd_device *nbd, struct block_device *bdev)
796+
static void nbd_parse_flags(struct nbd_device *nbd)
812797
{
813798
struct nbd_config *config = nbd->config;
814799
if (config->flags & NBD_FLAG_READ_ONLY)
815-
set_device_ro(bdev, true);
800+
set_disk_ro(nbd->disk, true);
801+
else
802+
set_disk_ro(nbd->disk, false);
816803
if (config->flags & NBD_FLAG_SEND_TRIM)
817804
queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
818805
if (config->flags & NBD_FLAG_SEND_FLUSH)
@@ -841,52 +828,36 @@ static void send_disconnects(struct nbd_device *nbd)
841828
}
842829
}
843830

844-
static int nbd_disconnect(struct nbd_device *nbd, struct block_device *bdev)
831+
static int nbd_disconnect(struct nbd_device *nbd)
845832
{
846833
struct nbd_config *config = nbd->config;
847834

848835
dev_info(disk_to_dev(nbd->disk), "NBD_DISCONNECT\n");
849-
mutex_unlock(&nbd->config_lock);
850-
fsync_bdev(bdev);
851-
mutex_lock(&nbd->config_lock);
852-
853836
if (!test_and_set_bit(NBD_DISCONNECT_REQUESTED,
854837
&config->runtime_flags))
855838
send_disconnects(nbd);
856839
return 0;
857840
}
858841

859-
static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
842+
static void nbd_clear_sock(struct nbd_device *nbd)
860843
{
861844
sock_shutdown(nbd);
862845
nbd_clear_que(nbd);
863-
864-
__invalidate_device(bdev, true);
865-
nbd_bdev_reset(bdev);
866846
nbd->task_setup = NULL;
867-
return 0;
868847
}
869848

870849
static void nbd_config_put(struct nbd_device *nbd)
871850
{
872851
if (refcount_dec_and_mutex_lock(&nbd->config_refs,
873852
&nbd->config_lock)) {
874-
struct block_device *bdev;
875853
struct nbd_config *config = nbd->config;
876-
877-
bdev = bdget_disk(nbd->disk, 0);
878-
if (!bdev) {
879-
mutex_unlock(&nbd->config_lock);
880-
return;
881-
}
882-
883854
nbd_dev_dbg_close(nbd);
884-
nbd_size_clear(nbd, bdev);
855+
nbd_size_clear(nbd);
885856
if (test_and_clear_bit(NBD_HAS_PID_FILE,
886857
&config->runtime_flags))
887858
device_remove_file(disk_to_dev(nbd->disk), &pid_attr);
888859
nbd->task_recv = NULL;
889-
nbd_clear_sock(nbd, bdev);
860+
nbd_clear_sock(nbd);
890861
if (config->num_connections) {
891862
int i;
892863
for (i = 0; i < config->num_connections; i++) {
@@ -897,7 +868,6 @@ static void nbd_config_put(struct nbd_device *nbd)
897868
}
898869
nbd_reset(nbd);
899870
mutex_unlock(&nbd->config_lock);
900-
bdput(bdev);
901871
module_put(THIS_MODULE);
902872
}
903873
}
@@ -912,27 +882,30 @@ static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev)
912882
return -EBUSY;
913883
if (!config->socks)
914884
return -EINVAL;
915-
916885
if (num_connections > 1 &&
917886
!(config->flags & NBD_FLAG_CAN_MULTI_CONN)) {
918887
dev_err(disk_to_dev(nbd->disk), "server does not support multiple connections per device.\n");
919888
return -EINVAL;
920889
}
921890

891+
if (max_part)
892+
bdev->bd_invalidated = 1;
922893
blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
923894
nbd->task_recv = current;
924895
mutex_unlock(&nbd->config_lock);
925896

926-
nbd_parse_flags(nbd, bdev);
897+
nbd_parse_flags(nbd);
927898

928899
error = device_create_file(disk_to_dev(nbd->disk), &pid_attr);
929900
if (error) {
930901
dev_err(disk_to_dev(nbd->disk), "device_create_file failed!\n");
931902
return error;
932903
}
933-
set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
934904

935-
nbd_size_update(nbd, bdev);
905+
set_bit(NBD_HAS_PID_FILE, &config->runtime_flags);
906+
if (max_part)
907+
bdev->bd_invalidated = 1;
908+
bd_set_size(bdev, config->bytesize);
936909

937910
nbd_dev_dbg_init(nbd);
938911
for (i = 0; i < num_connections; i++) {
@@ -965,6 +938,14 @@ static int nbd_start_device(struct nbd_device *nbd, struct block_device *bdev)
965938
return error;
966939
}
967940

941+
static void nbd_clear_sock_ioctl(struct nbd_device *nbd,
942+
struct block_device *bdev)
943+
{
944+
nbd_clear_sock(nbd);
945+
kill_bdev(bdev);
946+
nbd_bdev_reset(bdev);
947+
}
948+
968949
/* Must be called with config_lock held */
969950
static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
970951
unsigned int cmd, unsigned long arg)
@@ -973,21 +954,22 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
973954

974955
switch (cmd) {
975956
case NBD_DISCONNECT:
976-
return nbd_disconnect(nbd, bdev);
957+
return nbd_disconnect(nbd);
977958
case NBD_CLEAR_SOCK:
978-
return nbd_clear_sock(nbd, bdev);
959+
nbd_clear_sock_ioctl(nbd, bdev);
960+
return 0;
979961
case NBD_SET_SOCK:
980-
return nbd_add_socket(nbd, bdev, arg);
962+
return nbd_add_socket(nbd, arg);
981963
case NBD_SET_BLKSIZE:
982-
nbd_size_set(nbd, bdev, arg,
964+
nbd_size_set(nbd, arg,
983965
div_s64(config->bytesize, arg));
984966
return 0;
985967
case NBD_SET_SIZE:
986-
nbd_size_set(nbd, bdev, config->blksize,
968+
nbd_size_set(nbd, config->blksize,
987969
div_s64(arg, config->blksize));
988970
return 0;
989971
case NBD_SET_SIZE_BLOCKS:
990-
nbd_size_set(nbd, bdev, config->blksize, arg);
972+
nbd_size_set(nbd, config->blksize, arg);
991973
return 0;
992974
case NBD_SET_TIMEOUT:
993975
if (arg) {

0 commit comments

Comments
 (0)