This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / kernel / nsproxy.c
1 /*
2  *  Copyright (C) 2006 IBM Corporation
3  *
4  *  Author: Serge Hallyn <serue@us.ibm.com>
5  *
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
8  *  published by the Free Software Foundation, version 2 of the
9  *  License.
10  *
11  *  Jun 2006 - namespaces support
12  *             OpenVZ, SWsoft Inc.
13  *             Pavel Emelianov <xemul@openvz.org>
14  */
15
16 #include <linux/module.h>
17 #include <linux/version.h>
18 #include <linux/nsproxy.h>
19 #include <linux/init_task.h>
20 #include <linux/mnt_namespace.h>
21 #include <linux/utsname.h>
22 #include <linux/pid_namespace.h>
23 #include <linux/vserver/global.h>
24
25 struct nsproxy init_nsproxy = INIT_NSPROXY(init_nsproxy);
26
27 void get_task_namespaces(struct task_struct *tsk)
28 {
29         struct nsproxy *ns = tsk->nsproxy;
30         if (ns) {
31                 get_nsproxy(ns);
32         }
33 }
34
35 /*
36  * creates a copy of "orig" with refcount 1.
37  * This does not grab references to the contained namespaces,
38  * so that needs to be done by dup_namespaces.
39  */
40 static inline struct nsproxy *clone_namespaces(struct nsproxy *orig)
41 {
42         struct nsproxy *ns;
43
44         ns = kmemdup(orig, sizeof(struct nsproxy), GFP_KERNEL);
45         if (ns)
46                 atomic_set(&ns->count, 1);
47         atomic_inc(&vs_global_nsproxy);
48         return ns;
49 }
50
51 /*
52  * copies the nsproxy, setting refcount to 1, and grabbing a
53  * reference to all contained namespaces.  Called from
54  * sys_unshare()
55  */
56 struct nsproxy *dup_namespaces(struct nsproxy *orig)
57 {
58         struct nsproxy *ns = clone_namespaces(orig);
59
60         if (ns) {
61                 if (ns->mnt_ns)
62                         get_mnt_ns(ns->mnt_ns);
63                 if (ns->uts_ns)
64                         get_uts_ns(ns->uts_ns);
65                 if (ns->ipc_ns)
66                         get_ipc_ns(ns->ipc_ns);
67                 if (ns->pid_ns)
68                         get_pid_ns(ns->pid_ns);
69         }
70
71         return ns;
72 }
73
74 /*
75  * called from clone.  This now handles copy for nsproxy and all
76  * namespaces therein.
77  */
78 int copy_namespaces(int flags, struct task_struct *tsk)
79 {
80         struct nsproxy *old_ns = tsk->nsproxy;
81         struct nsproxy *new_ns;
82         int err = 0;
83
84         if (!old_ns)
85                 return 0;
86
87         get_nsproxy(old_ns);
88
89         if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
90                 return 0;
91
92         new_ns = clone_namespaces(old_ns);
93         if (!new_ns) {
94                 err = -ENOMEM;
95                 goto out;
96         }
97
98         tsk->nsproxy = new_ns;
99
100         err = copy_mnt_ns(flags, tsk);
101         if (err)
102                 goto out_ns;
103
104         err = copy_utsname(flags, tsk);
105         if (err)
106                 goto out_uts;
107
108         err = copy_ipcs(flags, tsk);
109         if (err)
110                 goto out_ipc;
111
112         err = copy_pid_ns(flags, tsk);
113         if (err)
114                 goto out_pid;
115
116 out:
117         put_nsproxy(old_ns);
118         return err;
119
120 out_pid:
121         if (new_ns->ipc_ns)
122                 put_ipc_ns(new_ns->ipc_ns);
123 out_ipc:
124         if (new_ns->uts_ns)
125                 put_uts_ns(new_ns->uts_ns);
126 out_uts:
127         if (new_ns->mnt_ns)
128                 put_mnt_ns(new_ns->mnt_ns);
129 out_ns:
130         tsk->nsproxy = old_ns;
131         kfree(new_ns);
132         goto out;
133 }
134
135 void free_nsproxy(struct nsproxy *ns)
136 {
137         if (ns->mnt_ns)
138                 put_mnt_ns(ns->mnt_ns);
139         if (ns->uts_ns)
140                 put_uts_ns(ns->uts_ns);
141         if (ns->ipc_ns)
142                 put_ipc_ns(ns->ipc_ns);
143         if (ns->pid_ns)
144                 put_pid_ns(ns->pid_ns);
145         atomic_dec(&vs_global_nsproxy);
146         kfree(ns);
147 }