ipc: inline ipc_buildid()
[linux-2.6-block.git] / ipc / util.h
CommitLineData
1da177e4
LT
1/*
2 * linux/ipc/util.h
3 * Copyright (C) 1999 Christoph Rohland
4 *
624dffcb 5 * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
73ea4130
KK
6 * namespaces support. 2006 OpenVZ, SWsoft Inc.
7 * Pavel Emelianov <xemul@openvz.org>
1da177e4
LT
8 */
9
10#ifndef _IPC_UTIL_H
11#define _IPC_UTIL_H
12
7ca7e564 13#include <linux/idr.h>
023a5355 14#include <linux/err.h>
7ca7e564 15
1da177e4
LT
16#define USHRT_MAX 0xffff
17#define SEQ_MULTIPLIER (IPCMNI)
18
19void sem_init (void);
20void msg_init (void);
21void shm_init (void);
22
73ea4130
KK
23int sem_init_ns(struct ipc_namespace *ns);
24int msg_init_ns(struct ipc_namespace *ns);
25int shm_init_ns(struct ipc_namespace *ns);
26
27void sem_exit_ns(struct ipc_namespace *ns);
28void msg_exit_ns(struct ipc_namespace *ns);
29void shm_exit_ns(struct ipc_namespace *ns);
30
1da177e4
LT
31struct ipc_ids {
32 int in_use;
1da177e4
LT
33 unsigned short seq;
34 unsigned short seq_max;
5f921ae9 35 struct mutex mutex;
7ca7e564 36 struct idr ipcs_idr;
1da177e4
LT
37};
38
7748dbfa
ND
39/*
40 * Structure that holds the parameters needed by the ipc operations
41 * (see after)
42 */
43struct ipc_params {
44 key_t key;
45 int flg;
46 union {
47 size_t size; /* for shared memories */
48 int nsems; /* for semaphores */
49 } u; /* holds the getnew() specific param */
50};
51
52/*
53 * Structure that holds some ipc operations. This structure is used to unify
54 * the calls to sys_msgget(), sys_semget(), sys_shmget()
55 * . routine to call to create a new ipc object. Can be one of newque,
56 * newary, newseg
57 * . routine to call to call to check permissions for a new ipc object.
58 * Can be one of security_msg_associate, security_sem_associate,
59 * security_shm_associate
60 * . routine to call for an extra check if needed
61 */
62struct ipc_ops {
63 int (*getnew) (struct ipc_namespace *, struct ipc_params *);
03f02c76
ND
64 int (*associate) (struct kern_ipc_perm *, int);
65 int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
7748dbfa
ND
66};
67
ae781774 68struct seq_file;
7d69a1f4 69
7ca7e564 70void ipc_init_ids(struct ipc_ids *);
ae781774
MW
71#ifdef CONFIG_PROC_FS
72void __init ipc_init_proc_interface(const char *path, const char *header,
73ea4130 73 int ids, int (*show)(struct seq_file *, void *));
ae781774
MW
74#else
75#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
76#endif
1da177e4 77
73ea4130
KK
78#define IPC_SEM_IDS 0
79#define IPC_MSG_IDS 1
80#define IPC_SHM_IDS 2
81
ce621f5b
ND
82#define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
83
5f921ae9 84/* must be called with ids->mutex acquired.*/
7ca7e564
ND
85int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
86int ipc_get_maxid(struct ipc_ids *);
1da177e4
LT
87
88/* must be called with both locks acquired. */
7ca7e564 89void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
1da177e4
LT
90
91int ipcperms (struct kern_ipc_perm *ipcp, short flg);
92
93/* for rare, potentially huge allocations.
94 * both function can sleep
95 */
96void* ipc_alloc(int size);
97void ipc_free(void* ptr, int size);
98
99/*
100 * For allocation that need to be freed by RCU.
101 * Objects are reference counted, they start with reference count 1.
102 * getref increases the refcount, the putref call that reduces the recount
103 * to 0 schedules the rcu destruction. Caller must guarantee locking.
104 */
105void* ipc_rcu_alloc(int size);
106void ipc_rcu_getref(void *ptr);
107void ipc_rcu_putref(void *ptr);
108
023a5355 109struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
1da177e4
LT
110
111void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
112void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
113
813e6783 114#if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
1da177e4
LT
115 /* On IA-64, we always use the "64-bit version" of the IPC structures. */
116# define ipc_parse_version(cmd) IPC_64
117#else
118int ipc_parse_version (int *cmd);
119#endif
120
121extern void free_msg(struct msg_msg *msg);
122extern struct msg_msg *load_msg(const void __user *src, int len);
123extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
7748dbfa
ND
124extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *,
125 struct ipc_ops *, struct ipc_params *);
126extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *,
127 struct ipc_ops *, struct ipc_params *);
128
28028313
ND
129static inline int ipc_buildid(struct ipc_ids *ids, int id, int seq)
130{
131 return SEQ_MULTIPLIER * seq + id;
132}
133
023a5355
ND
134static inline int ipc_checkid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp,
135 int uid)
136{
137 if (uid / SEQ_MULTIPLIER != ipcp->seq)
138 return 1;
139 return 0;
140}
141
142static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
143{
144 rcu_read_lock();
145 spin_lock(&perm->lock);
146}
147
148static inline void ipc_unlock(struct kern_ipc_perm *perm)
149{
150 spin_unlock(&perm->lock);
151 rcu_read_unlock();
152}
153
154static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids,
155 int id)
156{
157 struct kern_ipc_perm *out;
158
159 out = ipc_lock(ids, id);
160 if (IS_ERR(out))
161 return out;
162
163 if (ipc_checkid(ids, out, id)) {
164 ipc_unlock(out);
165 return ERR_PTR(-EIDRM);
166 }
167
168 return out;
169}
170
7748dbfa
ND
171static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
172 struct ipc_ops *ops, struct ipc_params *params)
173{
174 if (params->key == IPC_PRIVATE)
175 return ipcget_new(ns, ids, ops, params);
176 else
177 return ipcget_public(ns, ids, ops, params);
178}
1da177e4
LT
179
180#endif