Commit 4c2a7e72d5937c6a112141c7ff3df0727b3cf3df
Committed by
Linus Torvalds
1 parent
dca4a97960
Exists in
master
and in
4 other branches
utsns: extract creeate_uts_ns()
create_uts_ns() will be used by C/R to create fresh uts_ns. Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Acked-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 1 changed file with 11 additions and 2 deletions Inline Diff
kernel/utsname.c
1 | /* | 1 | /* |
2 | * Copyright (C) 2004 IBM Corporation | 2 | * Copyright (C) 2004 IBM Corporation |
3 | * | 3 | * |
4 | * Author: Serge Hallyn <serue@us.ibm.com> | 4 | * Author: Serge Hallyn <serue@us.ibm.com> |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or | 6 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public License as | 7 | * modify it under the terms of the GNU General Public License as |
8 | * published by the Free Software Foundation, version 2 of the | 8 | * published by the Free Software Foundation, version 2 of the |
9 | * License. | 9 | * License. |
10 | */ | 10 | */ |
11 | 11 | ||
12 | #include <linux/module.h> | 12 | #include <linux/module.h> |
13 | #include <linux/uts.h> | 13 | #include <linux/uts.h> |
14 | #include <linux/utsname.h> | 14 | #include <linux/utsname.h> |
15 | #include <linux/err.h> | 15 | #include <linux/err.h> |
16 | #include <linux/slab.h> | 16 | #include <linux/slab.h> |
17 | 17 | ||
18 | static struct uts_namespace *create_uts_ns(void) | ||
19 | { | ||
20 | struct uts_namespace *uts_ns; | ||
21 | |||
22 | uts_ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL); | ||
23 | if (uts_ns) | ||
24 | kref_init(&uts_ns->kref); | ||
25 | return uts_ns; | ||
26 | } | ||
27 | |||
18 | /* | 28 | /* |
19 | * Clone a new ns copying an original utsname, setting refcount to 1 | 29 | * Clone a new ns copying an original utsname, setting refcount to 1 |
20 | * @old_ns: namespace to clone | 30 | * @old_ns: namespace to clone |
21 | * Return NULL on error (failure to kmalloc), new ns otherwise | 31 | * Return NULL on error (failure to kmalloc), new ns otherwise |
22 | */ | 32 | */ |
23 | static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns) | 33 | static struct uts_namespace *clone_uts_ns(struct uts_namespace *old_ns) |
24 | { | 34 | { |
25 | struct uts_namespace *ns; | 35 | struct uts_namespace *ns; |
26 | 36 | ||
27 | ns = kmalloc(sizeof(struct uts_namespace), GFP_KERNEL); | 37 | ns = create_uts_ns(); |
28 | if (!ns) | 38 | if (!ns) |
29 | return ERR_PTR(-ENOMEM); | 39 | return ERR_PTR(-ENOMEM); |
30 | 40 | ||
31 | down_read(&uts_sem); | 41 | down_read(&uts_sem); |
32 | memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); | 42 | memcpy(&ns->name, &old_ns->name, sizeof(ns->name)); |
33 | up_read(&uts_sem); | 43 | up_read(&uts_sem); |
34 | kref_init(&ns->kref); | ||
35 | return ns; | 44 | return ns; |
36 | } | 45 | } |
37 | 46 | ||
38 | /* | 47 | /* |
39 | * Copy task tsk's utsname namespace, or clone it if flags | 48 | * Copy task tsk's utsname namespace, or clone it if flags |
40 | * specifies CLONE_NEWUTS. In latter case, changes to the | 49 | * specifies CLONE_NEWUTS. In latter case, changes to the |
41 | * utsname of this process won't be seen by parent, and vice | 50 | * utsname of this process won't be seen by parent, and vice |
42 | * versa. | 51 | * versa. |
43 | */ | 52 | */ |
44 | struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns) | 53 | struct uts_namespace *copy_utsname(unsigned long flags, struct uts_namespace *old_ns) |
45 | { | 54 | { |
46 | struct uts_namespace *new_ns; | 55 | struct uts_namespace *new_ns; |
47 | 56 | ||
48 | BUG_ON(!old_ns); | 57 | BUG_ON(!old_ns); |
49 | get_uts_ns(old_ns); | 58 | get_uts_ns(old_ns); |
50 | 59 | ||
51 | if (!(flags & CLONE_NEWUTS)) | 60 | if (!(flags & CLONE_NEWUTS)) |
52 | return old_ns; | 61 | return old_ns; |
53 | 62 | ||
54 | new_ns = clone_uts_ns(old_ns); | 63 | new_ns = clone_uts_ns(old_ns); |
55 | 64 | ||
56 | put_uts_ns(old_ns); | 65 | put_uts_ns(old_ns); |
57 | return new_ns; | 66 | return new_ns; |
58 | } | 67 | } |
59 | 68 | ||
60 | void free_uts_ns(struct kref *kref) | 69 | void free_uts_ns(struct kref *kref) |
61 | { | 70 | { |
62 | struct uts_namespace *ns; | 71 | struct uts_namespace *ns; |
63 | 72 | ||
64 | ns = container_of(kref, struct uts_namespace, kref); | 73 | ns = container_of(kref, struct uts_namespace, kref); |
65 | kfree(ns); | 74 | kfree(ns); |
66 | } | 75 | } |