Skip to content

Commit b267b17

Browse files
ebiedermDavid S. Miller
authored andcommitted
[NET]: Factor out __dev_alloc_name from dev_alloc_name
When forcibly changing the network namespace of a device I need something that can generate a name for the device in the new namespace without overwriting the old name. __dev_alloc_name provides me that functionality. Signed-off-by: Eric W. Biederman <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 881d966 commit b267b17

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

net/core/dev.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,10 @@ int dev_valid_name(const char *name)
739739
}
740740

741741
/**
742-
* dev_alloc_name - allocate a name for a device
743-
* @dev: device
742+
* __dev_alloc_name - allocate a name for a device
743+
* @net: network namespace to allocate the device name in
744744
* @name: name format string
745+
* @buf: scratch buffer and result name string
745746
*
746747
* Passed a format string - eg "lt%d" it will try and find a suitable
747748
* id. It scans list of devices to build up a free map, then chooses
@@ -752,18 +753,13 @@ int dev_valid_name(const char *name)
752753
* Returns the number of the unit assigned or a negative errno code.
753754
*/
754755

755-
int dev_alloc_name(struct net_device *dev, const char *name)
756+
static int __dev_alloc_name(struct net *net, const char *name, char *buf)
756757
{
757758
int i = 0;
758-
char buf[IFNAMSIZ];
759759
const char *p;
760760
const int max_netdevices = 8*PAGE_SIZE;
761761
long *inuse;
762762
struct net_device *d;
763-
struct net *net;
764-
765-
BUG_ON(!dev->nd_net);
766-
net = dev->nd_net;
767763

768764
p = strnchr(name, IFNAMSIZ-1, '%');
769765
if (p) {
@@ -787,7 +783,7 @@ int dev_alloc_name(struct net_device *dev, const char *name)
787783
continue;
788784

789785
/* avoid cases where sscanf is not exact inverse of printf */
790-
snprintf(buf, sizeof(buf), name, i);
786+
snprintf(buf, IFNAMSIZ, name, i);
791787
if (!strncmp(buf, d->name, IFNAMSIZ))
792788
set_bit(i, inuse);
793789
}
@@ -796,11 +792,9 @@ int dev_alloc_name(struct net_device *dev, const char *name)
796792
free_page((unsigned long) inuse);
797793
}
798794

799-
snprintf(buf, sizeof(buf), name, i);
800-
if (!__dev_get_by_name(net, buf)) {
801-
strlcpy(dev->name, buf, IFNAMSIZ);
795+
snprintf(buf, IFNAMSIZ, name, i);
796+
if (!__dev_get_by_name(net, buf))
802797
return i;
803-
}
804798

805799
/* It is possible to run out of possible slots
806800
* when the name is long and there isn't enough space left
@@ -809,6 +803,34 @@ int dev_alloc_name(struct net_device *dev, const char *name)
809803
return -ENFILE;
810804
}
811805

806+
/**
807+
* dev_alloc_name - allocate a name for a device
808+
* @dev: device
809+
* @name: name format string
810+
*
811+
* Passed a format string - eg "lt%d" it will try and find a suitable
812+
* id. It scans list of devices to build up a free map, then chooses
813+
* the first empty slot. The caller must hold the dev_base or rtnl lock
814+
* while allocating the name and adding the device in order to avoid
815+
* duplicates.
816+
* Limited to bits_per_byte * page size devices (ie 32K on most platforms).
817+
* Returns the number of the unit assigned or a negative errno code.
818+
*/
819+
820+
int dev_alloc_name(struct net_device *dev, const char *name)
821+
{
822+
char buf[IFNAMSIZ];
823+
struct net *net;
824+
int ret;
825+
826+
BUG_ON(!dev->nd_net);
827+
net = dev->nd_net;
828+
ret = __dev_alloc_name(net, name, buf);
829+
if (ret >= 0)
830+
strlcpy(dev->name, buf, IFNAMSIZ);
831+
return ret;
832+
}
833+
812834

813835
/**
814836
* dev_change_name - change name of a device

0 commit comments

Comments
 (0)