]> bbs.cooldavid.org Git - net-next-2.6.git/blame - ipc/util.c
ipc: recompute msgmni on memory add / remove
[net-next-2.6.git] / ipc / util.c
CommitLineData
1da177e4
LT
1/*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 *
5 * Sep 1997 - Call suser() last after "normal" permission checks so we
6 * get BSD style process accounting right.
7 * Occurs in several places in the IPC code.
8 * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
9 * Nov 1999 - ipc helper functions, unified SMP locking
624dffcb 10 * Manfred Spraul <manfred@colorfullife.com>
1da177e4
LT
11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
073115d6
SG
13 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
73ea4130
KK
15 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
1da177e4
LT
18 */
19
1da177e4
LT
20#include <linux/mm.h>
21#include <linux/shm.h>
22#include <linux/init.h>
23#include <linux/msg.h>
1da177e4
LT
24#include <linux/vmalloc.h>
25#include <linux/slab.h>
c59ede7b 26#include <linux/capability.h>
1da177e4
LT
27#include <linux/highuid.h>
28#include <linux/security.h>
29#include <linux/rcupdate.h>
30#include <linux/workqueue.h>
ae781774
MW
31#include <linux/seq_file.h>
32#include <linux/proc_fs.h>
073115d6 33#include <linux/audit.h>
73ea4130 34#include <linux/nsproxy.h>
3e148c79 35#include <linux/rwsem.h>
b6b337ad 36#include <linux/memory.h>
ae5e1b22 37#include <linux/ipc_namespace.h>
1da177e4
LT
38
39#include <asm/unistd.h>
40
41#include "util.h"
42
ae781774
MW
43struct ipc_proc_iface {
44 const char *path;
45 const char *header;
73ea4130 46 int ids;
ae781774
MW
47 int (*show)(struct seq_file *, void *);
48};
49
73ea4130
KK
50struct ipc_namespace init_ipc_ns = {
51 .kref = {
52 .refcount = ATOMIC_INIT(2),
53 },
54};
55
4d89dc6a
ND
56atomic_t nr_ipc_ns = ATOMIC_INIT(1);
57
58
b6b337ad
ND
59#ifdef CONFIG_MEMORY_HOTPLUG
60
61static int ipc_memory_callback(struct notifier_block *self,
62 unsigned long action, void *arg)
63{
64 switch (action) {
65 case MEM_ONLINE: /* memory successfully brought online */
66 case MEM_OFFLINE: /* or offline: it's time to recompute msgmni */
67 /*
68 * This is done by invoking the ipcns notifier chain with the
69 * IPC_MEMCHANGED event.
70 */
71 ipcns_notify(IPCNS_MEMCHANGED);
72 break;
73 case MEM_GOING_ONLINE:
74 case MEM_GOING_OFFLINE:
75 case MEM_CANCEL_ONLINE:
76 case MEM_CANCEL_OFFLINE:
77 default:
78 break;
79 }
80
81 return NOTIFY_OK;
82}
83
84#endif /* CONFIG_MEMORY_HOTPLUG */
85
1da177e4
LT
86/**
87 * ipc_init - initialise IPC subsystem
88 *
89 * The various system5 IPC resources (semaphores, messages and shared
72fd4a35 90 * memory) are initialised
b6b337ad
ND
91 * A callback routine is registered into the memory hotplug notifier
92 * chain: since msgmni scales to lowmem this callback routine will be
93 * called upon successful memory add / remove to recompute msmgni.
1da177e4
LT
94 */
95
96static int __init ipc_init(void)
97{
98 sem_init();
99 msg_init();
100 shm_init();
b6b337ad
ND
101 hotplug_memory_notifier(ipc_memory_callback, IPC_CALLBACK_PRI);
102 register_ipcns_notifier(&init_ipc_ns);
1da177e4
LT
103 return 0;
104}
105__initcall(ipc_init);
106
107/**
108 * ipc_init_ids - initialise IPC identifiers
109 * @ids: Identifier set
1da177e4 110 *
7ca7e564
ND
111 * Set up the sequence range to use for the ipc identifier range (limited
112 * below IPCMNI) then initialise the ids idr.
1da177e4
LT
113 */
114
7ca7e564 115void ipc_init_ids(struct ipc_ids *ids)
1da177e4 116{
3e148c79 117 init_rwsem(&ids->rw_mutex);
1da177e4 118
1da177e4 119 ids->in_use = 0;
1da177e4
LT
120 ids->seq = 0;
121 {
122 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
123 if(seq_limit > USHRT_MAX)
124 ids->seq_max = USHRT_MAX;
125 else
126 ids->seq_max = seq_limit;
127 }
128
7ca7e564 129 idr_init(&ids->ipcs_idr);
1da177e4
LT
130}
131
ae781774 132#ifdef CONFIG_PROC_FS
9a32144e 133static const struct file_operations sysvipc_proc_fops;
ae781774 134/**
72fd4a35 135 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
ae781774
MW
136 * @path: Path in procfs
137 * @header: Banner to be printed at the beginning of the file.
138 * @ids: ipc id table to iterate.
139 * @show: show routine.
140 */
141void __init ipc_init_proc_interface(const char *path, const char *header,
73ea4130 142 int ids, int (*show)(struct seq_file *, void *))
ae781774
MW
143{
144 struct proc_dir_entry *pde;
145 struct ipc_proc_iface *iface;
146
147 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
148 if (!iface)
149 return;
150 iface->path = path;
151 iface->header = header;
152 iface->ids = ids;
153 iface->show = show;
154
155 pde = create_proc_entry(path,
156 S_IRUGO, /* world readable */
157 NULL /* parent dir */);
158 if (pde) {
159 pde->data = iface;
160 pde->proc_fops = &sysvipc_proc_fops;
161 } else {
162 kfree(iface);
163 }
164}
165#endif
166
1da177e4
LT
167/**
168 * ipc_findkey - find a key in an ipc identifier set
169 * @ids: Identifier set
170 * @key: The key to find
171 *
3e148c79 172 * Requires ipc_ids.rw_mutex locked.
7ca7e564
ND
173 * Returns the LOCKED pointer to the ipc structure if found or NULL
174 * if not.
f4566f04 175 * If key is found ipc points to the owning ipc structure
1da177e4
LT
176 */
177
7748dbfa 178static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
1da177e4 179{
7ca7e564
ND
180 struct kern_ipc_perm *ipc;
181 int next_id;
182 int total;
1da177e4 183
7ca7e564
ND
184 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
185 ipc = idr_find(&ids->ipcs_idr, next_id);
186
187 if (ipc == NULL)
1da177e4 188 continue;
7ca7e564
ND
189
190 if (ipc->key != key) {
191 total++;
192 continue;
193 }
194
195 ipc_lock_by_ptr(ipc);
196 return ipc;
1da177e4 197 }
7ca7e564
ND
198
199 return NULL;
1da177e4
LT
200}
201
7ca7e564
ND
202/**
203 * ipc_get_maxid - get the last assigned id
204 * @ids: IPC identifier set
205 *
3e148c79 206 * Called with ipc_ids.rw_mutex held.
1da177e4 207 */
1da177e4 208
7ca7e564
ND
209int ipc_get_maxid(struct ipc_ids *ids)
210{
211 struct kern_ipc_perm *ipc;
212 int max_id = -1;
213 int total, id;
214
215 if (ids->in_use == 0)
216 return -1;
1da177e4 217
7ca7e564
ND
218 if (ids->in_use == IPCMNI)
219 return IPCMNI - 1;
220
221 /* Look for the last assigned id */
222 total = 0;
223 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
224 ipc = idr_find(&ids->ipcs_idr, id);
225 if (ipc != NULL) {
226 max_id = id;
227 total++;
228 }
229 }
230 return max_id;
1da177e4
LT
231}
232
233/**
234 * ipc_addid - add an IPC identifier
235 * @ids: IPC identifier set
236 * @new: new IPC permission set
7ca7e564 237 * @size: limit for the number of used ids
1da177e4 238 *
f4566f04 239 * Add an entry 'new' to the IPC ids idr. The permissions object is
1da177e4 240 * initialised and the first free entry is set up and the id assigned
f4566f04 241 * is returned. The 'new' entry is returned in a locked state on success.
283bb7fa 242 * On failure the entry is not locked and a negative err-code is returned.
1da177e4 243 *
3e148c79 244 * Called with ipc_ids.rw_mutex held as a writer.
1da177e4
LT
245 */
246
247int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
248{
7ca7e564 249 int id, err;
1da177e4 250
7ca7e564
ND
251 if (size > IPCMNI)
252 size = IPCMNI;
253
254 if (ids->in_use >= size)
283bb7fa 255 return -ENOSPC;
7ca7e564
ND
256
257 err = idr_get_new(&ids->ipcs_idr, new, &id);
258 if (err)
283bb7fa 259 return err;
7ca7e564 260
1da177e4 261 ids->in_use++;
1da177e4
LT
262
263 new->cuid = new->uid = current->euid;
264 new->gid = new->cgid = current->egid;
265
266 new->seq = ids->seq++;
267 if(ids->seq > ids->seq_max)
268 ids->seq = 0;
269
48dea404 270 new->id = ipc_buildid(id, new->seq);
1da177e4
LT
271 spin_lock_init(&new->lock);
272 new->deleted = 0;
273 rcu_read_lock();
274 spin_lock(&new->lock);
1da177e4
LT
275 return id;
276}
277
7748dbfa
ND
278/**
279 * ipcget_new - create a new ipc object
280 * @ns: namespace
f4566f04 281 * @ids: IPC identifer set
7748dbfa
ND
282 * @ops: the actual creation routine to call
283 * @params: its parameters
284 *
f4566f04
ND
285 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
286 * when the key is IPC_PRIVATE.
7748dbfa 287 */
b2d75cdd 288static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
7748dbfa
ND
289 struct ipc_ops *ops, struct ipc_params *params)
290{
291 int err;
283bb7fa 292retry:
7748dbfa
ND
293 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
294
295 if (!err)
296 return -ENOMEM;
297
3e148c79 298 down_write(&ids->rw_mutex);
7748dbfa 299 err = ops->getnew(ns, params);
3e148c79 300 up_write(&ids->rw_mutex);
7748dbfa 301
283bb7fa
PP
302 if (err == -EAGAIN)
303 goto retry;
304
7748dbfa
ND
305 return err;
306}
307
308/**
309 * ipc_check_perms - check security and permissions for an IPC
310 * @ipcp: ipc permission set
7748dbfa
ND
311 * @ops: the actual security routine to call
312 * @params: its parameters
f4566f04
ND
313 *
314 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
315 * when the key is not IPC_PRIVATE and that key already exists in the
316 * ids IDR.
317 *
318 * On success, the IPC id is returned.
319 *
3e148c79 320 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
7748dbfa
ND
321 */
322static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
323 struct ipc_params *params)
324{
325 int err;
326
327 if (ipcperms(ipcp, params->flg))
328 err = -EACCES;
329 else {
330 err = ops->associate(ipcp, params->flg);
331 if (!err)
332 err = ipcp->id;
333 }
334
335 return err;
336}
337
338/**
339 * ipcget_public - get an ipc object or create a new one
340 * @ns: namespace
f4566f04 341 * @ids: IPC identifer set
7748dbfa
ND
342 * @ops: the actual creation routine to call
343 * @params: its parameters
344 *
f4566f04
ND
345 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
346 * when the key is not IPC_PRIVATE.
347 * It adds a new entry if the key is not found and does some permission
348 * / security checkings if the key is found.
349 *
350 * On success, the ipc id is returned.
7748dbfa 351 */
b2d75cdd 352static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
7748dbfa
ND
353 struct ipc_ops *ops, struct ipc_params *params)
354{
355 struct kern_ipc_perm *ipcp;
356 int flg = params->flg;
357 int err;
283bb7fa 358retry:
7748dbfa
ND
359 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
360
3e148c79
ND
361 /*
362 * Take the lock as a writer since we are potentially going to add
363 * a new entry + read locks are not "upgradable"
364 */
365 down_write(&ids->rw_mutex);
7748dbfa
ND
366 ipcp = ipc_findkey(ids, params->key);
367 if (ipcp == NULL) {
368 /* key not used */
369 if (!(flg & IPC_CREAT))
370 err = -ENOENT;
371 else if (!err)
372 err = -ENOMEM;
373 else
374 err = ops->getnew(ns, params);
375 } else {
376 /* ipc object has been locked by ipc_findkey() */
377
378 if (flg & IPC_CREAT && flg & IPC_EXCL)
379 err = -EEXIST;
380 else {
381 err = 0;
382 if (ops->more_checks)
383 err = ops->more_checks(ipcp, params);
384 if (!err)
f4566f04
ND
385 /*
386 * ipc_check_perms returns the IPC id on
387 * success
388 */
7748dbfa
ND
389 err = ipc_check_perms(ipcp, ops, params);
390 }
391 ipc_unlock(ipcp);
392 }
3e148c79 393 up_write(&ids->rw_mutex);
7748dbfa 394
283bb7fa
PP
395 if (err == -EAGAIN)
396 goto retry;
397
7748dbfa
ND
398 return err;
399}
400
401
1da177e4
LT
402/**
403 * ipc_rmid - remove an IPC identifier
f4566f04
ND
404 * @ids: IPC identifier set
405 * @ipcp: ipc perm structure containing the identifier to remove
1da177e4 406 *
3e148c79
ND
407 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
408 * before this function is called, and remain locked on the exit.
1da177e4
LT
409 */
410
7ca7e564 411void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
1da177e4 412{
ce621f5b 413 int lid = ipcid_to_idx(ipcp->id);
7ca7e564
ND
414
415 idr_remove(&ids->ipcs_idr, lid);
1da177e4 416
1da177e4
LT
417 ids->in_use--;
418
7ca7e564
ND
419 ipcp->deleted = 1;
420
421 return;
1da177e4
LT
422}
423
424/**
425 * ipc_alloc - allocate ipc space
426 * @size: size desired
427 *
428 * Allocate memory from the appropriate pools and return a pointer to it.
429 * NULL is returned if the allocation fails
430 */
431
432void* ipc_alloc(int size)
433{
434 void* out;
435 if(size > PAGE_SIZE)
436 out = vmalloc(size);
437 else
438 out = kmalloc(size, GFP_KERNEL);
439 return out;
440}
441
442/**
443 * ipc_free - free ipc space
444 * @ptr: pointer returned by ipc_alloc
445 * @size: size of block
446 *
72fd4a35 447 * Free a block created with ipc_alloc(). The caller must know the size
1da177e4
LT
448 * used in the allocation call.
449 */
450
451void ipc_free(void* ptr, int size)
452{
453 if(size > PAGE_SIZE)
454 vfree(ptr);
455 else
456 kfree(ptr);
457}
458
459/*
460 * rcu allocations:
461 * There are three headers that are prepended to the actual allocation:
462 * - during use: ipc_rcu_hdr.
463 * - during the rcu grace period: ipc_rcu_grace.
464 * - [only if vmalloc]: ipc_rcu_sched.
465 * Their lifetime doesn't overlap, thus the headers share the same memory.
466 * Unlike a normal union, they are right-aligned, thus some container_of
467 * forward/backward casting is necessary:
468 */
469struct ipc_rcu_hdr
470{
471 int refcount;
472 int is_vmalloc;
473 void *data[0];
474};
475
476
477struct ipc_rcu_grace
478{
479 struct rcu_head rcu;
480 /* "void *" makes sure alignment of following data is sane. */
481 void *data[0];
482};
483
484struct ipc_rcu_sched
485{
486 struct work_struct work;
487 /* "void *" makes sure alignment of following data is sane. */
488 void *data[0];
489};
490
491#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
492 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
493#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
494 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
495
496static inline int rcu_use_vmalloc(int size)
497{
498 /* Too big for a single page? */
499 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
500 return 1;
501 return 0;
502}
503
504/**
505 * ipc_rcu_alloc - allocate ipc and rcu space
506 * @size: size desired
507 *
508 * Allocate memory for the rcu header structure + the object.
509 * Returns the pointer to the object.
510 * NULL is returned if the allocation fails.
511 */
512
513void* ipc_rcu_alloc(int size)
514{
515 void* out;
516 /*
517 * We prepend the allocation with the rcu struct, and
518 * workqueue if necessary (for vmalloc).
519 */
520 if (rcu_use_vmalloc(size)) {
521 out = vmalloc(HDRLEN_VMALLOC + size);
522 if (out) {
523 out += HDRLEN_VMALLOC;
524 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
525 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
526 }
527 } else {
528 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
529 if (out) {
530 out += HDRLEN_KMALLOC;
531 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
532 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
533 }
534 }
535
536 return out;
537}
538
539void ipc_rcu_getref(void *ptr)
540{
541 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
542}
543
65f27f38
DH
544static void ipc_do_vfree(struct work_struct *work)
545{
546 vfree(container_of(work, struct ipc_rcu_sched, work));
547}
548
1da177e4 549/**
1e5d5331
RD
550 * ipc_schedule_free - free ipc + rcu space
551 * @head: RCU callback structure for queued work
1da177e4
LT
552 *
553 * Since RCU callback function is called in bh,
72fd4a35 554 * we need to defer the vfree to schedule_work().
1da177e4
LT
555 */
556static void ipc_schedule_free(struct rcu_head *head)
557{
f4566f04
ND
558 struct ipc_rcu_grace *grace;
559 struct ipc_rcu_sched *sched;
560
561 grace = container_of(head, struct ipc_rcu_grace, rcu);
562 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
563 data[0]);
1da177e4 564
65f27f38 565 INIT_WORK(&sched->work, ipc_do_vfree);
1da177e4
LT
566 schedule_work(&sched->work);
567}
568
569/**
1e5d5331
RD
570 * ipc_immediate_free - free ipc + rcu space
571 * @head: RCU callback structure that contains pointer to be freed
1da177e4 572 *
72fd4a35 573 * Free from the RCU callback context.
1da177e4
LT
574 */
575static void ipc_immediate_free(struct rcu_head *head)
576{
577 struct ipc_rcu_grace *free =
578 container_of(head, struct ipc_rcu_grace, rcu);
579 kfree(free);
580}
581
582void ipc_rcu_putref(void *ptr)
583{
584 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
585 return;
586
587 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
588 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
589 ipc_schedule_free);
590 } else {
591 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
592 ipc_immediate_free);
593 }
594}
595
596/**
597 * ipcperms - check IPC permissions
598 * @ipcp: IPC permission set
599 * @flag: desired permission set.
600 *
601 * Check user, group, other permissions for access
602 * to ipc resources. return 0 if allowed
603 */
604
605int ipcperms (struct kern_ipc_perm *ipcp, short flag)
606{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
073115d6 607 int requested_mode, granted_mode, err;
1da177e4 608
073115d6
SG
609 if (unlikely((err = audit_ipc_obj(ipcp))))
610 return err;
1da177e4
LT
611 requested_mode = (flag >> 6) | (flag >> 3) | flag;
612 granted_mode = ipcp->mode;
613 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
614 granted_mode >>= 6;
615 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
616 granted_mode >>= 3;
617 /* is there some bit set in requested_mode but not in granted_mode? */
618 if ((requested_mode & ~granted_mode & 0007) &&
619 !capable(CAP_IPC_OWNER))
620 return -1;
621
622 return security_ipc_permission(ipcp, flag);
623}
624
625/*
626 * Functions to convert between the kern_ipc_perm structure and the
627 * old/new ipc_perm structures
628 */
629
630/**
631 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
632 * @in: kernel permissions
633 * @out: new style IPC permissions
634 *
72fd4a35
RD
635 * Turn the kernel object @in into a set of permissions descriptions
636 * for returning to userspace (@out).
1da177e4
LT
637 */
638
639
640void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
641{
642 out->key = in->key;
643 out->uid = in->uid;
644 out->gid = in->gid;
645 out->cuid = in->cuid;
646 out->cgid = in->cgid;
647 out->mode = in->mode;
648 out->seq = in->seq;
649}
650
651/**
f4566f04 652 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
1da177e4
LT
653 * @in: new style IPC permissions
654 * @out: old style IPC permissions
655 *
72fd4a35
RD
656 * Turn the new style permissions object @in into a compatibility
657 * object and store it into the @out pointer.
1da177e4
LT
658 */
659
660void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
661{
662 out->key = in->key;
663 SET_UID(out->uid, in->uid);
664 SET_GID(out->gid, in->gid);
665 SET_UID(out->cuid, in->cuid);
666 SET_GID(out->cgid, in->cgid);
667 out->mode = in->mode;
668 out->seq = in->seq;
669}
670
f4566f04 671/**
3e148c79 672 * ipc_lock - Lock an ipc structure without rw_mutex held
f4566f04
ND
673 * @ids: IPC identifier set
674 * @id: ipc id to look for
675 *
676 * Look for an id in the ipc ids idr and lock the associated ipc object.
677 *
f4566f04 678 * The ipc object is locked on exit.
3e148c79
ND
679 *
680 * This is the routine that should be called when the rw_mutex is not already
681 * held, i.e. idr tree not protected: it protects the idr tree in read mode
682 * during the idr_find().
f4566f04
ND
683 */
684
7ca7e564 685struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
1da177e4 686{
7ca7e564 687 struct kern_ipc_perm *out;
ce621f5b 688 int lid = ipcid_to_idx(id);
1da177e4 689
3e148c79
ND
690 down_read(&ids->rw_mutex);
691
1da177e4 692 rcu_read_lock();
7ca7e564
ND
693 out = idr_find(&ids->ipcs_idr, lid);
694 if (out == NULL) {
1da177e4 695 rcu_read_unlock();
3e148c79 696 up_read(&ids->rw_mutex);
023a5355 697 return ERR_PTR(-EINVAL);
1da177e4 698 }
7ca7e564 699
3e148c79
ND
700 up_read(&ids->rw_mutex);
701
1da177e4
LT
702 spin_lock(&out->lock);
703
704 /* ipc_rmid() may have already freed the ID while ipc_lock
705 * was spinning: here verify that the structure is still valid
706 */
707 if (out->deleted) {
708 spin_unlock(&out->lock);
709 rcu_read_unlock();
023a5355 710 return ERR_PTR(-EINVAL);
1da177e4 711 }
7ca7e564 712
1da177e4
LT
713 return out;
714}
715
3e148c79
ND
716/**
717 * ipc_lock_down - Lock an ipc structure with rw_sem held
718 * @ids: IPC identifier set
719 * @id: ipc id to look for
720 *
721 * Look for an id in the ipc ids idr and lock the associated ipc object.
722 *
723 * The ipc object is locked on exit.
724 *
725 * This is the routine that should be called when the rw_mutex is already
726 * held, i.e. idr tree protected.
727 */
728
729struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
730{
731 struct kern_ipc_perm *out;
732 int lid = ipcid_to_idx(id);
733
734 rcu_read_lock();
735 out = idr_find(&ids->ipcs_idr, lid);
736 if (out == NULL) {
737 rcu_read_unlock();
738 return ERR_PTR(-EINVAL);
739 }
740
741 spin_lock(&out->lock);
742
743 /*
744 * No need to verify that the structure is still valid since the
745 * rw_mutex is held.
746 */
747 return out;
748}
749
b2d75cdd
PE
750struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
751{
752 struct kern_ipc_perm *out;
753
754 out = ipc_lock_down(ids, id);
755 if (IS_ERR(out))
756 return out;
757
758 if (ipc_checkid(out, id)) {
759 ipc_unlock(out);
760 return ERR_PTR(-EIDRM);
761 }
762
763 return out;
764}
765
766struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
767{
768 struct kern_ipc_perm *out;
769
770 out = ipc_lock(ids, id);
771 if (IS_ERR(out))
772 return out;
773
774 if (ipc_checkid(out, id)) {
775 ipc_unlock(out);
776 return ERR_PTR(-EIDRM);
777 }
778
779 return out;
780}
781
782/**
783 * ipcget - Common sys_*get() code
784 * @ns : namsepace
785 * @ids : IPC identifier set
786 * @ops : operations to be called on ipc object creation, permission checks
787 * and further checks
788 * @params : the parameters needed by the previous operations.
789 *
790 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
791 */
792int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
793 struct ipc_ops *ops, struct ipc_params *params)
794{
795 if (params->key == IPC_PRIVATE)
796 return ipcget_new(ns, ids, ops, params);
797 else
798 return ipcget_public(ns, ids, ops, params);
799}
800
1da177e4
LT
801#ifdef __ARCH_WANT_IPC_PARSE_VERSION
802
803
804/**
805 * ipc_parse_version - IPC call version
806 * @cmd: pointer to command
807 *
808 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
72fd4a35 809 * The @cmd value is turned from an encoding command and version into
1da177e4
LT
810 * just the command code.
811 */
812
813int ipc_parse_version (int *cmd)
814{
815 if (*cmd & IPC_64) {
816 *cmd ^= IPC_64;
817 return IPC_64;
818 } else {
819 return IPC_OLD;
820 }
821}
822
823#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
ae781774
MW
824
825#ifdef CONFIG_PROC_FS
bc1fc6d8
EB
826struct ipc_proc_iter {
827 struct ipc_namespace *ns;
828 struct ipc_proc_iface *iface;
829};
830
7ca7e564
ND
831/*
832 * This routine locks the ipc structure found at least at position pos.
833 */
b524b9ad
AB
834static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
835 loff_t *new_pos)
ae781774 836{
7ca7e564
ND
837 struct kern_ipc_perm *ipc;
838 int total, id;
73ea4130 839
7ca7e564
ND
840 total = 0;
841 for (id = 0; id < pos && total < ids->in_use; id++) {
842 ipc = idr_find(&ids->ipcs_idr, id);
843 if (ipc != NULL)
844 total++;
845 }
ae781774 846
7ca7e564
ND
847 if (total >= ids->in_use)
848 return NULL;
ae781774 849
7ca7e564
ND
850 for ( ; pos < IPCMNI; pos++) {
851 ipc = idr_find(&ids->ipcs_idr, pos);
852 if (ipc != NULL) {
853 *new_pos = pos + 1;
854 ipc_lock_by_ptr(ipc);
ae781774
MW
855 return ipc;
856 }
857 }
858
859 /* Out of range - return NULL to terminate iteration */
860 return NULL;
861}
862
7ca7e564
ND
863static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
864{
865 struct ipc_proc_iter *iter = s->private;
866 struct ipc_proc_iface *iface = iter->iface;
867 struct kern_ipc_perm *ipc = it;
868
869 /* If we had an ipc id locked before, unlock it */
870 if (ipc && ipc != SEQ_START_TOKEN)
871 ipc_unlock(ipc);
872
ed2ddbf8 873 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
7ca7e564
ND
874}
875
ae781774 876/*
f4566f04
ND
877 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
878 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
ae781774
MW
879 */
880static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
881{
bc1fc6d8
EB
882 struct ipc_proc_iter *iter = s->private;
883 struct ipc_proc_iface *iface = iter->iface;
73ea4130
KK
884 struct ipc_ids *ids;
885
ed2ddbf8 886 ids = &iter->ns->ids[iface->ids];
ae781774
MW
887
888 /*
889 * Take the lock - this will be released by the corresponding
890 * call to stop().
891 */
3e148c79 892 down_read(&ids->rw_mutex);
ae781774
MW
893
894 /* pos < 0 is invalid */
895 if (*pos < 0)
896 return NULL;
897
898 /* pos == 0 means header */
899 if (*pos == 0)
900 return SEQ_START_TOKEN;
901
902 /* Find the (pos-1)th ipc */
7ca7e564 903 return sysvipc_find_ipc(ids, *pos - 1, pos);
ae781774
MW
904}
905
906static void sysvipc_proc_stop(struct seq_file *s, void *it)
907{
908 struct kern_ipc_perm *ipc = it;
bc1fc6d8
EB
909 struct ipc_proc_iter *iter = s->private;
910 struct ipc_proc_iface *iface = iter->iface;
73ea4130 911 struct ipc_ids *ids;
ae781774 912
f4566f04 913 /* If we had a locked structure, release it */
ae781774
MW
914 if (ipc && ipc != SEQ_START_TOKEN)
915 ipc_unlock(ipc);
916
ed2ddbf8 917 ids = &iter->ns->ids[iface->ids];
ae781774 918 /* Release the lock we took in start() */
3e148c79 919 up_read(&ids->rw_mutex);
ae781774
MW
920}
921
922static int sysvipc_proc_show(struct seq_file *s, void *it)
923{
bc1fc6d8
EB
924 struct ipc_proc_iter *iter = s->private;
925 struct ipc_proc_iface *iface = iter->iface;
ae781774
MW
926
927 if (it == SEQ_START_TOKEN)
928 return seq_puts(s, iface->header);
929
930 return iface->show(s, it);
931}
932
933static struct seq_operations sysvipc_proc_seqops = {
934 .start = sysvipc_proc_start,
935 .stop = sysvipc_proc_stop,
936 .next = sysvipc_proc_next,
937 .show = sysvipc_proc_show,
938};
939
bc1fc6d8
EB
940static int sysvipc_proc_open(struct inode *inode, struct file *file)
941{
ae781774
MW
942 int ret;
943 struct seq_file *seq;
bc1fc6d8
EB
944 struct ipc_proc_iter *iter;
945
946 ret = -ENOMEM;
947 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
948 if (!iter)
949 goto out;
ae781774
MW
950
951 ret = seq_open(file, &sysvipc_proc_seqops);
bc1fc6d8
EB
952 if (ret)
953 goto out_kfree;
954
955 seq = file->private_data;
956 seq->private = iter;
957
958 iter->iface = PDE(inode)->data;
959 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
960out:
ae781774 961 return ret;
bc1fc6d8
EB
962out_kfree:
963 kfree(iter);
964 goto out;
965}
966
967static int sysvipc_proc_release(struct inode *inode, struct file *file)
968{
969 struct seq_file *seq = file->private_data;
970 struct ipc_proc_iter *iter = seq->private;
971 put_ipc_ns(iter->ns);
972 return seq_release_private(inode, file);
ae781774
MW
973}
974
9a32144e 975static const struct file_operations sysvipc_proc_fops = {
ae781774
MW
976 .open = sysvipc_proc_open,
977 .read = seq_read,
978 .llseek = seq_lseek,
bc1fc6d8 979 .release = sysvipc_proc_release,
ae781774
MW
980};
981#endif /* CONFIG_PROC_FS */