@@ -1530,25 +1530,31 @@ static int nbd_ioctl(struct block_device *bdev, blk_mode_t mode,
1530
1530
return error ;
1531
1531
}
1532
1532
1533
- static struct nbd_config * nbd_alloc_config ( void )
1533
+ static int nbd_alloc_and_init_config ( struct nbd_device * nbd )
1534
1534
{
1535
1535
struct nbd_config * config ;
1536
1536
1537
+ if (WARN_ON (nbd -> config ))
1538
+ return - EINVAL ;
1539
+
1537
1540
if (!try_module_get (THIS_MODULE ))
1538
- return ERR_PTR ( - ENODEV ) ;
1541
+ return - ENODEV ;
1539
1542
1540
1543
config = kzalloc (sizeof (struct nbd_config ), GFP_NOFS );
1541
1544
if (!config ) {
1542
1545
module_put (THIS_MODULE );
1543
- return ERR_PTR ( - ENOMEM ) ;
1546
+ return - ENOMEM ;
1544
1547
}
1545
1548
1546
1549
atomic_set (& config -> recv_threads , 0 );
1547
1550
init_waitqueue_head (& config -> recv_wq );
1548
1551
init_waitqueue_head (& config -> conn_wait );
1549
1552
config -> blksize_bits = NBD_DEF_BLKSIZE_BITS ;
1550
1553
atomic_set (& config -> live_connections , 0 );
1551
- return config ;
1554
+ nbd -> config = config ;
1555
+ refcount_set (& nbd -> config_refs , 1 );
1556
+
1557
+ return 0 ;
1552
1558
}
1553
1559
1554
1560
static int nbd_open (struct gendisk * disk , blk_mode_t mode )
@@ -1567,21 +1573,17 @@ static int nbd_open(struct gendisk *disk, blk_mode_t mode)
1567
1573
goto out ;
1568
1574
}
1569
1575
if (!refcount_inc_not_zero (& nbd -> config_refs )) {
1570
- struct nbd_config * config ;
1571
-
1572
1576
mutex_lock (& nbd -> config_lock );
1573
1577
if (refcount_inc_not_zero (& nbd -> config_refs )) {
1574
1578
mutex_unlock (& nbd -> config_lock );
1575
1579
goto out ;
1576
1580
}
1577
- config = nbd_alloc_config ();
1578
- if (IS_ERR (config )) {
1579
- ret = PTR_ERR (config );
1581
+ ret = nbd_alloc_and_init_config (nbd );
1582
+ if (ret ) {
1580
1583
mutex_unlock (& nbd -> config_lock );
1581
1584
goto out ;
1582
1585
}
1583
- nbd -> config = config ;
1584
- refcount_set (& nbd -> config_refs , 1 );
1586
+
1585
1587
refcount_inc (& nbd -> refs );
1586
1588
mutex_unlock (& nbd -> config_lock );
1587
1589
if (max_part )
@@ -1990,22 +1992,17 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info)
1990
1992
pr_err ("nbd%d already in use\n" , index );
1991
1993
return - EBUSY ;
1992
1994
}
1993
- if (WARN_ON (nbd -> config )) {
1994
- mutex_unlock (& nbd -> config_lock );
1995
- nbd_put (nbd );
1996
- return - EINVAL ;
1997
- }
1998
- config = nbd_alloc_config ();
1999
- if (IS_ERR (config )) {
1995
+
1996
+ ret = nbd_alloc_and_init_config (nbd );
1997
+ if (ret ) {
2000
1998
mutex_unlock (& nbd -> config_lock );
2001
1999
nbd_put (nbd );
2002
2000
pr_err ("couldn't allocate config\n" );
2003
- return PTR_ERR ( config ) ;
2001
+ return ret ;
2004
2002
}
2005
- nbd -> config = config ;
2006
- refcount_set (& nbd -> config_refs , 1 );
2007
- set_bit (NBD_RT_BOUND , & config -> runtime_flags );
2008
2003
2004
+ config = nbd -> config ;
2005
+ set_bit (NBD_RT_BOUND , & config -> runtime_flags );
2009
2006
ret = nbd_genl_size_set (info , nbd );
2010
2007
if (ret )
2011
2008
goto out ;
0 commit comments