ipc: fix wrong comments
[sfrench/cifs-2.6.git] / ipc / util.h
1 /*
2  * linux/ipc/util.h
3  * Copyright (C) 1999 Christoph Rohland
4  *
5  * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
6  * namespaces support.      2006 OpenVZ, SWsoft Inc.
7  *                               Pavel Emelianov <xemul@openvz.org>
8  */
9
10 #ifndef _IPC_UTIL_H
11 #define _IPC_UTIL_H
12
13 #include <linux/idr.h>
14 #include <linux/err.h>
15
16 #define USHRT_MAX 0xffff
17 #define SEQ_MULTIPLIER  (IPCMNI)
18
19 void sem_init (void);
20 void msg_init (void);
21 void shm_init (void);
22
23 int sem_init_ns(struct ipc_namespace *ns);
24 int msg_init_ns(struct ipc_namespace *ns);
25 int shm_init_ns(struct ipc_namespace *ns);
26
27 void sem_exit_ns(struct ipc_namespace *ns);
28 void msg_exit_ns(struct ipc_namespace *ns);
29 void shm_exit_ns(struct ipc_namespace *ns);
30
31 struct ipc_ids {
32         int in_use;
33         unsigned short seq;
34         unsigned short seq_max;
35         struct mutex mutex;
36         struct idr ipcs_idr;
37 };
38
39 /*
40  * Structure that holds the parameters needed by the ipc operations
41  * (see after)
42  */
43 struct 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 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  */
62 struct ipc_ops {
63         int (*getnew) (struct ipc_namespace *, struct ipc_params *);
64         int (*associate) (struct kern_ipc_perm *, int);
65         int (*more_checks) (struct kern_ipc_perm *, struct ipc_params *);
66 };
67
68 struct seq_file;
69
70 void ipc_init_ids(struct ipc_ids *);
71 #ifdef CONFIG_PROC_FS
72 void __init ipc_init_proc_interface(const char *path, const char *header,
73                 int ids, int (*show)(struct seq_file *, void *));
74 #else
75 #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
76 #endif
77
78 #define IPC_SEM_IDS     0
79 #define IPC_MSG_IDS     1
80 #define IPC_SHM_IDS     2
81
82 #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
83
84 /* must be called with ids->mutex acquired.*/
85 int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
86 int ipc_get_maxid(struct ipc_ids *);
87
88 /* must be called with both locks acquired. */
89 void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
90
91 /* must be called with ipcp locked */
92 int ipcperms(struct kern_ipc_perm *ipcp, short flg);
93
94 /* for rare, potentially huge allocations.
95  * both function can sleep
96  */
97 void* ipc_alloc(int size);
98 void ipc_free(void* ptr, int size);
99
100 /*
101  * For allocation that need to be freed by RCU.
102  * Objects are reference counted, they start with reference count 1.
103  * getref increases the refcount, the putref call that reduces the recount
104  * to 0 schedules the rcu destruction. Caller must guarantee locking.
105  */
106 void* ipc_rcu_alloc(int size);
107 void ipc_rcu_getref(void *ptr);
108 void ipc_rcu_putref(void *ptr);
109
110 struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
111
112 void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
113 void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
114
115 #if defined(__ia64__) || defined(__x86_64__) || defined(__hppa__) || defined(__XTENSA__)
116   /* On IA-64, we always use the "64-bit version" of the IPC structures.  */ 
117 # define ipc_parse_version(cmd) IPC_64
118 #else
119 int ipc_parse_version (int *cmd);
120 #endif
121
122 extern void free_msg(struct msg_msg *msg);
123 extern struct msg_msg *load_msg(const void __user *src, int len);
124 extern int store_msg(void __user *dest, struct msg_msg *msg, int len);
125 extern int ipcget_new(struct ipc_namespace *, struct ipc_ids *,
126                         struct ipc_ops *, struct ipc_params *);
127 extern int ipcget_public(struct ipc_namespace *, struct ipc_ids *,
128                         struct ipc_ops *, struct ipc_params *);
129
130 static inline int ipc_buildid(struct ipc_ids *ids, int id, int seq)
131 {
132         return SEQ_MULTIPLIER * seq + id;
133 }
134
135 /*
136  * Must be called with ipcp locked
137  */
138 static inline int ipc_checkid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp,
139                                 int uid)
140 {
141         if (uid / SEQ_MULTIPLIER != ipcp->seq)
142                 return 1;
143         return 0;
144 }
145
146 static inline void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
147 {
148         rcu_read_lock();
149         spin_lock(&perm->lock);
150 }
151
152 static inline void ipc_unlock(struct kern_ipc_perm *perm)
153 {
154         spin_unlock(&perm->lock);
155         rcu_read_unlock();
156 }
157
158 static inline struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids,
159                                                 int id)
160 {
161         struct kern_ipc_perm *out;
162
163         out = ipc_lock(ids, id);
164         if (IS_ERR(out))
165                 return out;
166
167         if (ipc_checkid(ids, out, id)) {
168                 ipc_unlock(out);
169                 return ERR_PTR(-EIDRM);
170         }
171
172         return out;
173 }
174
175 /**
176  * ipcget - Common sys_*get() code
177  * @ns : namsepace
178  * @ids : IPC identifier set
179  * @ops : operations to be called on ipc object creation, permission checks
180  *        and further checks
181  * @params : the parameters needed by the previous operations.
182  *
183  * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
184  */
185 static inline int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
186                         struct ipc_ops *ops, struct ipc_params *params)
187 {
188         if (params->key == IPC_PRIVATE)
189                 return ipcget_new(ns, ids, ops, params);
190         else
191                 return ipcget_public(ns, ids, ops, params);
192 }
193
194 #endif