2d545d7144a7b9cfe04edaeaedefd2472314000d
[safe/jmp/linux-2.6] / ipc / util.c
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
10  *            Manfred Spraul <manfred@colorfullife.com>
11  * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12  *            Mingming Cao <cmm@us.ibm.com>
13  * Mar 2006 - support for audit of ipc object properties
14  *            Dustin Kirkland <dustin.kirkland@us.ibm.com>
15  * Jun 2006 - namespaces ssupport
16  *            OpenVZ, SWsoft Inc.
17  *            Pavel Emelianov <xemul@openvz.org>
18  */
19
20 #include <linux/mm.h>
21 #include <linux/shm.h>
22 #include <linux/init.h>
23 #include <linux/msg.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/capability.h>
27 #include <linux/highuid.h>
28 #include <linux/security.h>
29 #include <linux/rcupdate.h>
30 #include <linux/workqueue.h>
31 #include <linux/seq_file.h>
32 #include <linux/proc_fs.h>
33 #include <linux/audit.h>
34 #include <linux/nsproxy.h>
35 #include <linux/rwsem.h>
36 #include <linux/memory.h>
37 #include <linux/ipc_namespace.h>
38
39 #include <asm/unistd.h>
40
41 #include "util.h"
42
43 struct ipc_proc_iface {
44         const char *path;
45         const char *header;
46         int ids;
47         int (*show)(struct seq_file *, void *);
48 };
49
50 struct ipc_namespace init_ipc_ns = {
51         .kref = {
52                 .refcount       = ATOMIC_INIT(2),
53         },
54 };
55
56 atomic_t nr_ipc_ns = ATOMIC_INIT(1);
57
58
59 #ifdef CONFIG_MEMORY_HOTPLUG
60
61 static 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
86 /**
87  *      ipc_init        -       initialise IPC subsystem
88  *
89  *      The various system5 IPC resources (semaphores, messages and shared
90  *      memory) are initialised
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.
94  */
95  
96 static int __init ipc_init(void)
97 {
98         sem_init();
99         msg_init();
100         shm_init();
101         hotplug_memory_notifier(ipc_memory_callback, IPC_CALLBACK_PRI);
102         register_ipcns_notifier(&init_ipc_ns);
103         return 0;
104 }
105 __initcall(ipc_init);
106
107 /**
108  *      ipc_init_ids            -       initialise IPC identifiers
109  *      @ids: Identifier set
110  *
111  *      Set up the sequence range to use for the ipc identifier range (limited
112  *      below IPCMNI) then initialise the ids idr.
113  */
114  
115 void ipc_init_ids(struct ipc_ids *ids)
116 {
117         init_rwsem(&ids->rw_mutex);
118
119         ids->in_use = 0;
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
129         idr_init(&ids->ipcs_idr);
130 }
131
132 #ifdef CONFIG_PROC_FS
133 static const struct file_operations sysvipc_proc_fops;
134 /**
135  *      ipc_init_proc_interface -  Create a proc interface for sysipc types using a seq_file interface.
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  */
141 void __init ipc_init_proc_interface(const char *path, const char *header,
142                 int ids, int (*show)(struct seq_file *, void *))
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
167 /**
168  *      ipc_findkey     -       find a key in an ipc identifier set     
169  *      @ids: Identifier set
170  *      @key: The key to find
171  *      
172  *      Requires ipc_ids.rw_mutex locked.
173  *      Returns the LOCKED pointer to the ipc structure if found or NULL
174  *      if not.
175  *      If key is found ipc points to the owning ipc structure
176  */
177  
178 static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
179 {
180         struct kern_ipc_perm *ipc;
181         int next_id;
182         int total;
183
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)
188                         continue;
189
190                 if (ipc->key != key) {
191                         total++;
192                         continue;
193                 }
194
195                 ipc_lock_by_ptr(ipc);
196                 return ipc;
197         }
198
199         return NULL;
200 }
201
202 /**
203  *      ipc_get_maxid   -       get the last assigned id
204  *      @ids: IPC identifier set
205  *
206  *      Called with ipc_ids.rw_mutex held.
207  */
208
209 int 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;
217
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;
231 }
232
233 /**
234  *      ipc_addid       -       add an IPC identifier
235  *      @ids: IPC identifier set
236  *      @new: new IPC permission set
237  *      @size: limit for the number of used ids
238  *
239  *      Add an entry 'new' to the IPC ids idr. The permissions object is
240  *      initialised and the first free entry is set up and the id assigned
241  *      is returned. The 'new' entry is returned in a locked state on success.
242  *      On failure the entry is not locked and a negative err-code is returned.
243  *
244  *      Called with ipc_ids.rw_mutex held as a writer.
245  */
246  
247 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
248 {
249         int id, err;
250
251         if (size > IPCMNI)
252                 size = IPCMNI;
253
254         if (ids->in_use >= size)
255                 return -ENOSPC;
256
257         err = idr_get_new(&ids->ipcs_idr, new, &id);
258         if (err)
259                 return err;
260
261         ids->in_use++;
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
270         new->id = ipc_buildid(id, new->seq);
271         spin_lock_init(&new->lock);
272         new->deleted = 0;
273         rcu_read_lock();
274         spin_lock(&new->lock);
275         return id;
276 }
277
278 /**
279  *      ipcget_new      -       create a new ipc object
280  *      @ns: namespace
281  *      @ids: IPC identifer set
282  *      @ops: the actual creation routine to call
283  *      @params: its parameters
284  *
285  *      This routine is called by sys_msgget, sys_semget() and sys_shmget()
286  *      when the key is IPC_PRIVATE.
287  */
288 static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
289                 struct ipc_ops *ops, struct ipc_params *params)
290 {
291         int err;
292 retry:
293         err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
294
295         if (!err)
296                 return -ENOMEM;
297
298         down_write(&ids->rw_mutex);
299         err = ops->getnew(ns, params);
300         up_write(&ids->rw_mutex);
301
302         if (err == -EAGAIN)
303                 goto retry;
304
305         return err;
306 }
307
308 /**
309  *      ipc_check_perms -       check security and permissions for an IPC
310  *      @ipcp: ipc permission set
311  *      @ops: the actual security routine to call
312  *      @params: its parameters
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  *
320  *      It is called with ipc_ids.rw_mutex and ipcp->lock held.
321  */
322 static 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
341  *      @ids: IPC identifer set
342  *      @ops: the actual creation routine to call
343  *      @params: its parameters
344  *
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.
351  */
352 static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
353                 struct ipc_ops *ops, struct ipc_params *params)
354 {
355         struct kern_ipc_perm *ipcp;
356         int flg = params->flg;
357         int err;
358 retry:
359         err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
360
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);
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)
385                                 /*
386                                  * ipc_check_perms returns the IPC id on
387                                  * success
388                                  */
389                                 err = ipc_check_perms(ipcp, ops, params);
390                 }
391                 ipc_unlock(ipcp);
392         }
393         up_write(&ids->rw_mutex);
394
395         if (err == -EAGAIN)
396                 goto retry;
397
398         return err;
399 }
400
401
402 /**
403  *      ipc_rmid        -       remove an IPC identifier
404  *      @ids: IPC identifier set
405  *      @ipcp: ipc perm structure containing the identifier to remove
406  *
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.
409  */
410  
411 void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
412 {
413         int lid = ipcid_to_idx(ipcp->id);
414
415         idr_remove(&ids->ipcs_idr, lid);
416
417         ids->in_use--;
418
419         ipcp->deleted = 1;
420
421         return;
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  
432 void* 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  *
447  *      Free a block created with ipc_alloc(). The caller must know the size
448  *      used in the allocation call.
449  */
450
451 void 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  */
469 struct ipc_rcu_hdr
470 {
471         int refcount;
472         int is_vmalloc;
473         void *data[0];
474 };
475
476
477 struct 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
484 struct 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
496 static 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  
513 void* 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
539 void ipc_rcu_getref(void *ptr)
540 {
541         container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
542 }
543
544 static void ipc_do_vfree(struct work_struct *work)
545 {
546         vfree(container_of(work, struct ipc_rcu_sched, work));
547 }
548
549 /**
550  * ipc_schedule_free - free ipc + rcu space
551  * @head: RCU callback structure for queued work
552  * 
553  * Since RCU callback function is called in bh,
554  * we need to defer the vfree to schedule_work().
555  */
556 static void ipc_schedule_free(struct rcu_head *head)
557 {
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]);
564
565         INIT_WORK(&sched->work, ipc_do_vfree);
566         schedule_work(&sched->work);
567 }
568
569 /**
570  * ipc_immediate_free - free ipc + rcu space
571  * @head: RCU callback structure that contains pointer to be freed
572  *
573  * Free from the RCU callback context.
574  */
575 static 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
582 void 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  
605 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
606 {       /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
607         int requested_mode, granted_mode, err;
608
609         if (unlikely((err = audit_ipc_obj(ipcp))))
610                 return err;
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  *
635  *      Turn the kernel object @in into a set of permissions descriptions
636  *      for returning to userspace (@out).
637  */
638  
639
640 void 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 /**
652  *      ipc64_perm_to_ipc_perm  -       convert new ipc permissions to old
653  *      @in: new style IPC permissions
654  *      @out: old style IPC permissions
655  *
656  *      Turn the new style permissions object @in into a compatibility
657  *      object and store it into the @out pointer.
658  */
659  
660 void 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
671 /**
672  * ipc_lock - Lock an ipc structure without rw_mutex held
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  *
678  * The ipc object is locked on exit.
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().
683  */
684
685 struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
686 {
687         struct kern_ipc_perm *out;
688         int lid = ipcid_to_idx(id);
689
690         down_read(&ids->rw_mutex);
691
692         rcu_read_lock();
693         out = idr_find(&ids->ipcs_idr, lid);
694         if (out == NULL) {
695                 rcu_read_unlock();
696                 up_read(&ids->rw_mutex);
697                 return ERR_PTR(-EINVAL);
698         }
699
700         up_read(&ids->rw_mutex);
701
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();
710                 return ERR_PTR(-EINVAL);
711         }
712
713         return out;
714 }
715
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
729 struct 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
750 struct 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
766 struct 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  */
792 int 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
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. 
809  *      The @cmd value is turned from an encoding command and version into
810  *      just the command code.
811  */
812  
813 int 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 */
824
825 #ifdef CONFIG_PROC_FS
826 struct ipc_proc_iter {
827         struct ipc_namespace *ns;
828         struct ipc_proc_iface *iface;
829 };
830
831 /*
832  * This routine locks the ipc structure found at least at position pos.
833  */
834 static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
835                                               loff_t *new_pos)
836 {
837         struct kern_ipc_perm *ipc;
838         int total, id;
839
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         }
846
847         if (total >= ids->in_use)
848                 return NULL;
849
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);
855                         return ipc;
856                 }
857         }
858
859         /* Out of range - return NULL to terminate iteration */
860         return NULL;
861 }
862
863 static 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
873         return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
874 }
875
876 /*
877  * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
878  * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
879  */
880 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
881 {
882         struct ipc_proc_iter *iter = s->private;
883         struct ipc_proc_iface *iface = iter->iface;
884         struct ipc_ids *ids;
885
886         ids = &iter->ns->ids[iface->ids];
887
888         /*
889          * Take the lock - this will be released by the corresponding
890          * call to stop().
891          */
892         down_read(&ids->rw_mutex);
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 */
903         return sysvipc_find_ipc(ids, *pos - 1, pos);
904 }
905
906 static void sysvipc_proc_stop(struct seq_file *s, void *it)
907 {
908         struct kern_ipc_perm *ipc = it;
909         struct ipc_proc_iter *iter = s->private;
910         struct ipc_proc_iface *iface = iter->iface;
911         struct ipc_ids *ids;
912
913         /* If we had a locked structure, release it */
914         if (ipc && ipc != SEQ_START_TOKEN)
915                 ipc_unlock(ipc);
916
917         ids = &iter->ns->ids[iface->ids];
918         /* Release the lock we took in start() */
919         up_read(&ids->rw_mutex);
920 }
921
922 static int sysvipc_proc_show(struct seq_file *s, void *it)
923 {
924         struct ipc_proc_iter *iter = s->private;
925         struct ipc_proc_iface *iface = iter->iface;
926
927         if (it == SEQ_START_TOKEN)
928                 return seq_puts(s, iface->header);
929
930         return iface->show(s, it);
931 }
932
933 static 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
940 static int sysvipc_proc_open(struct inode *inode, struct file *file)
941 {
942         int ret;
943         struct seq_file *seq;
944         struct ipc_proc_iter *iter;
945
946         ret = -ENOMEM;
947         iter = kmalloc(sizeof(*iter), GFP_KERNEL);
948         if (!iter)
949                 goto out;
950
951         ret = seq_open(file, &sysvipc_proc_seqops);
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);
960 out:
961         return ret;
962 out_kfree:
963         kfree(iter);
964         goto out;
965 }
966
967 static 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);
973 }
974
975 static const struct file_operations sysvipc_proc_fops = {
976         .open    = sysvipc_proc_open,
977         .read    = seq_read,
978         .llseek  = seq_lseek,
979         .release = sysvipc_proc_release,
980 };
981 #endif /* CONFIG_PROC_FS */