nsproxy: extract create_nsproxy()
[linux-2.6-block.git] / ipc / namespace.c
CommitLineData
ae5e1b22
PE
1/*
2 * linux/ipc/namespace.c
3 * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
4 */
5
6#include <linux/ipc.h>
7#include <linux/msg.h>
8#include <linux/ipc_namespace.h>
9#include <linux/rcupdate.h>
10#include <linux/nsproxy.h>
11#include <linux/slab.h>
7eafd7c7
SH
12#include <linux/fs.h>
13#include <linux/mount.h>
ae5e1b22
PE
14
15#include "util.h"
16
612ce478 17static struct ipc_namespace *create_ipc_ns(void)
ae5e1b22 18{
ae5e1b22 19 struct ipc_namespace *ns;
7eafd7c7 20 int err;
ae5e1b22 21
ae5e1b22
PE
22 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
23 if (ns == NULL)
ed2ddbf8 24 return ERR_PTR(-ENOMEM);
ae5e1b22 25
7eafd7c7
SH
26 atomic_set(&ns->count, 1);
27 err = mq_init_ns(ns);
28 if (err) {
29 kfree(ns);
30 return ERR_PTR(err);
31 }
4d89dc6a
ND
32 atomic_inc(&nr_ipc_ns);
33
ed2ddbf8
PP
34 sem_init_ns(ns);
35 msg_init_ns(ns);
36 shm_init_ns(ns);
ae5e1b22 37
e2c284d8
ND
38 /*
39 * msgmni has already been computed for the new ipc ns.
40 * Thus, do the ipcns creation notification before registering that
41 * new ipcns in the chain.
42 */
43 ipcns_notify(IPCNS_CREATED);
b6b337ad
ND
44 register_ipcns_notifier(ns);
45
ae5e1b22 46 return ns;
ae5e1b22
PE
47}
48
49struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
50{
ae5e1b22 51 if (!(flags & CLONE_NEWIPC))
64424289 52 return get_ipc_ns(ns);
612ce478 53 return create_ipc_ns();
ae5e1b22
PE
54}
55
01b8b07a
PP
56/*
57 * free_ipcs - free all ipcs of one type
58 * @ns: the namespace to remove the ipcs from
59 * @ids: the table of ipcs to free
60 * @free: the function called to free each individual ipc
61 *
62 * Called for each kind of ipc when an ipc_namespace exits.
63 */
64void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
65 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
66{
67 struct kern_ipc_perm *perm;
68 int next_id;
69 int total, in_use;
70
71 down_write(&ids->rw_mutex);
72
73 in_use = ids->in_use;
74
75 for (total = 0, next_id = 0; total < in_use; next_id++) {
76 perm = idr_find(&ids->ipcs_idr, next_id);
77 if (perm == NULL)
78 continue;
79 ipc_lock_by_ptr(perm);
80 free(ns, perm);
81 total++;
82 }
83 up_write(&ids->rw_mutex);
84}
85
7eafd7c7
SH
86/*
87 * put_ipc_ns - drop a reference to an ipc namespace.
88 * @ns: the namespace to put
89 *
90 * If this is the last task in the namespace exiting, and
91 * it is dropping the refcount to 0, then it can race with
92 * a task in another ipc namespace but in a mounts namespace
93 * which has this ipcns's mqueuefs mounted, doing some action
94 * with one of the mqueuefs files. That can raise the refcount.
95 * So dropping the refcount, and raising the refcount when
96 * accessing it through the VFS, are protected with mq_lock.
97 *
98 * (Clearly, a task raising the refcount on its own ipc_ns
99 * needn't take mq_lock since it can't race with the last task
100 * in the ipcns exiting).
101 */
102void put_ipc_ns(struct ipc_namespace *ns)
ae5e1b22 103{
7eafd7c7
SH
104 if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
105 mq_clear_sbinfo(ns);
106 spin_unlock(&mq_lock);
107 mq_put_mnt(ns);
108 free_ipc_ns(ns);
109 }
110}
ae5e1b22 111
7eafd7c7
SH
112void free_ipc_ns(struct ipc_namespace *ns)
113{
b6b337ad
ND
114 /*
115 * Unregistering the hotplug notifier at the beginning guarantees
116 * that the ipc namespace won't be freed while we are inside the
117 * callback routine. Since the blocking_notifier_chain_XXX routines
118 * hold a rw lock on the notifier list, unregister_ipcns_notifier()
119 * won't take the rw lock before blocking_notifier_call_chain() has
120 * released the rd lock.
121 */
122 unregister_ipcns_notifier(ns);
ae5e1b22
PE
123 sem_exit_ns(ns);
124 msg_exit_ns(ns);
125 shm_exit_ns(ns);
126 kfree(ns);
4d89dc6a 127 atomic_dec(&nr_ipc_ns);
e2c284d8
ND
128
129 /*
130 * Do the ipcns removal notification after decrementing nr_ipc_ns in
131 * order to have a correct value when recomputing msgmni.
132 */
133 ipcns_notify(IPCNS_REMOVED);
ae5e1b22 134}