fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / 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/smp_lock.h>
25 #include <linux/vmalloc.h>
26 #include <linux/slab.h>
27 #include <linux/capability.h>
28 #include <linux/highuid.h>
29 #include <linux/security.h>
30 #include <linux/rcupdate.h>
31 #include <linux/workqueue.h>
32 #include <linux/seq_file.h>
33 #include <linux/proc_fs.h>
34 #include <linux/audit.h>
35 #include <linux/nsproxy.h>
36 #include <linux/vs_base.h>
37 #include <linux/vserver/global.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 #ifdef CONFIG_IPC_NS
57 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
58 {
59         int err;
60         struct ipc_namespace *ns;
61
62         err = -ENOMEM;
63         ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
64         if (ns == NULL)
65                 goto err_mem;
66
67         err = sem_init_ns(ns);
68         if (err)
69                 goto err_sem;
70         err = msg_init_ns(ns);
71         if (err)
72                 goto err_msg;
73         err = shm_init_ns(ns);
74         if (err)
75                 goto err_shm;
76
77         kref_init(&ns->kref);
78         atomic_inc(&vs_global_ipc_ns);
79         return ns;
80
81 err_shm:
82         msg_exit_ns(ns);
83 err_msg:
84         sem_exit_ns(ns);
85 err_sem:
86         kfree(ns);
87 err_mem:
88         return ERR_PTR(err);
89 }
90
91 int unshare_ipcs(unsigned long unshare_flags, struct ipc_namespace **new_ipc)
92 {
93         struct ipc_namespace *new;
94
95         if (unshare_flags & CLONE_NEWIPC) {
96                 if (!capable(CAP_SYS_ADMIN))
97                         return -EPERM;
98
99                 new = clone_ipc_ns(current->nsproxy->ipc_ns);
100                 if (IS_ERR(new))
101                         return PTR_ERR(new);
102
103                 *new_ipc = new;
104         }
105
106         return 0;
107 }
108
109 int copy_ipcs(unsigned long flags, struct task_struct *tsk)
110 {
111         struct ipc_namespace *old_ns = tsk->nsproxy->ipc_ns;
112         struct ipc_namespace *new_ns;
113         int err = 0;
114
115         if (!old_ns)
116                 return 0;
117
118         get_ipc_ns(old_ns);
119
120         if (!(flags & CLONE_NEWIPC))
121                 return 0;
122
123         if (!capable(CAP_SYS_ADMIN)) {
124                 err = -EPERM;
125                 goto out;
126         }
127
128         new_ns = clone_ipc_ns(old_ns);
129         if (!new_ns) {
130                 err = -ENOMEM;
131                 goto out;
132         }
133
134         tsk->nsproxy->ipc_ns = new_ns;
135 out:
136         put_ipc_ns(old_ns);
137         return err;
138 }
139
140 void free_ipc_ns(struct kref *kref)
141 {
142         struct ipc_namespace *ns;
143
144         ns = container_of(kref, struct ipc_namespace, kref);
145         sem_exit_ns(ns);
146         msg_exit_ns(ns);
147         shm_exit_ns(ns);
148         atomic_dec(&vs_global_ipc_ns);
149         kfree(ns);
150 }
151 #endif
152
153 /**
154  *      ipc_init        -       initialise IPC subsystem
155  *
156  *      The various system5 IPC resources (semaphores, messages and shared
157  *      memory are initialised
158  */
159  
160 static int __init ipc_init(void)
161 {
162         sem_init();
163         msg_init();
164         shm_init();
165         return 0;
166 }
167 __initcall(ipc_init);
168
169 /**
170  *      ipc_init_ids            -       initialise IPC identifiers
171  *      @ids: Identifier set
172  *      @size: Number of identifiers
173  *
174  *      Given a size for the ipc identifier range (limited below IPCMNI)
175  *      set up the sequence range to use then allocate and initialise the
176  *      array itself. 
177  */
178  
179 void __ipc_init ipc_init_ids(struct ipc_ids* ids, int size)
180 {
181         int i;
182
183         mutex_init(&ids->mutex);
184
185         if(size > IPCMNI)
186                 size = IPCMNI;
187         ids->in_use = 0;
188         ids->max_id = -1;
189         ids->seq = 0;
190         {
191                 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
192                 if(seq_limit > USHRT_MAX)
193                         ids->seq_max = USHRT_MAX;
194                  else
195                         ids->seq_max = seq_limit;
196         }
197
198         ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
199                                      sizeof(struct ipc_id_ary));
200
201         if(ids->entries == NULL) {
202                 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
203                 size = 0;
204                 ids->entries = &ids->nullentry;
205         }
206         ids->entries->size = size;
207         for(i=0;i<size;i++)
208                 ids->entries->p[i] = NULL;
209 }
210
211 #ifdef CONFIG_PROC_FS
212 static struct file_operations sysvipc_proc_fops;
213 /**
214  *      ipc_init_proc_interface -  Create a proc interface for sysipc types
215  *                                 using a seq_file interface.
216  *      @path: Path in procfs
217  *      @header: Banner to be printed at the beginning of the file.
218  *      @ids: ipc id table to iterate.
219  *      @show: show routine.
220  */
221 void __init ipc_init_proc_interface(const char *path, const char *header,
222                 int ids, int (*show)(struct seq_file *, void *))
223 {
224         struct proc_dir_entry *pde;
225         struct ipc_proc_iface *iface;
226
227         iface = kmalloc(sizeof(*iface), GFP_KERNEL);
228         if (!iface)
229                 return;
230         iface->path     = path;
231         iface->header   = header;
232         iface->ids      = ids;
233         iface->show     = show;
234
235         pde = create_proc_entry(path,
236                                 S_IRUGO,        /* world readable */
237                                 NULL            /* parent dir */);
238         if (pde) {
239                 pde->data = iface;
240                 pde->proc_fops = &sysvipc_proc_fops;
241         } else {
242                 kfree(iface);
243         }
244 }
245 #endif
246
247 /**
248  *      ipc_findkey     -       find a key in an ipc identifier set     
249  *      @ids: Identifier set
250  *      @key: The key to find
251  *      
252  *      Requires ipc_ids.mutex locked.
253  *      Returns the identifier if found or -1 if not.
254  */
255  
256 int ipc_findkey(struct ipc_ids* ids, key_t key)
257 {
258         int id;
259         struct kern_ipc_perm* p;
260         int max_id = ids->max_id;
261
262         /*
263          * rcu_dereference() is not needed here
264          * since ipc_ids.mutex is held
265          */
266         for (id = 0; id <= max_id; id++) {
267                 p = ids->entries->p[id];
268                 if(p==NULL)
269                         continue;
270                 if (key == p->key)
271                         return id;
272         }
273         return -1;
274 }
275
276 /*
277  * Requires ipc_ids.mutex locked
278  */
279 static int grow_ary(struct ipc_ids* ids, int newsize)
280 {
281         struct ipc_id_ary* new;
282         struct ipc_id_ary* old;
283         int i;
284         int size = ids->entries->size;
285
286         if(newsize > IPCMNI)
287                 newsize = IPCMNI;
288         if(newsize <= size)
289                 return newsize;
290
291         new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
292                             sizeof(struct ipc_id_ary));
293         if(new == NULL)
294                 return size;
295         new->size = newsize;
296         memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
297         for(i=size;i<newsize;i++) {
298                 new->p[i] = NULL;
299         }
300         old = ids->entries;
301
302         /*
303          * Use rcu_assign_pointer() to make sure the memcpyed contents
304          * of the new array are visible before the new array becomes visible.
305          */
306         rcu_assign_pointer(ids->entries, new);
307
308         __ipc_fini_ids(ids, old);
309         return newsize;
310 }
311
312 /**
313  *      ipc_addid       -       add an IPC identifier
314  *      @ids: IPC identifier set
315  *      @new: new IPC permission set
316  *      @size: new size limit for the id array
317  *
318  *      Add an entry 'new' to the IPC arrays. The permissions object is
319  *      initialised and the first free entry is set up and the id assigned
320  *      is returned. The list is returned in a locked state on success.
321  *      On failure the list is not locked and -1 is returned.
322  *
323  *      Called with ipc_ids.mutex held.
324  */
325  
326 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
327 {
328         int id;
329
330         size = grow_ary(ids,size);
331
332         /*
333          * rcu_dereference()() is not needed here since
334          * ipc_ids.mutex is held
335          */
336         for (id = 0; id < size; id++) {
337                 if(ids->entries->p[id] == NULL)
338                         goto found;
339         }
340         return -1;
341 found:
342         ids->in_use++;
343         if (id > ids->max_id)
344                 ids->max_id = id;
345
346         new->cuid = new->uid = current->euid;
347         new->gid = new->cgid = current->egid;
348
349         new->seq = ids->seq++;
350         if(ids->seq > ids->seq_max)
351                 ids->seq = 0;
352
353         spin_lock_init(&new->lock);
354         new->deleted = 0;
355         rcu_read_lock();
356         spin_lock(&new->lock);
357         ids->entries->p[id] = new;
358         return id;
359 }
360
361 /**
362  *      ipc_rmid        -       remove an IPC identifier
363  *      @ids: identifier set
364  *      @id: Identifier to remove
365  *
366  *      The identifier must be valid, and in use. The kernel will panic if
367  *      fed an invalid identifier. The entry is removed and internal
368  *      variables recomputed. The object associated with the identifier
369  *      is returned.
370  *      ipc_ids.mutex and the spinlock for this ID is hold before this function
371  *      is called, and remain locked on the exit.
372  */
373  
374 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
375 {
376         struct kern_ipc_perm* p;
377         int lid = id % SEQ_MULTIPLIER;
378         BUG_ON(lid >= ids->entries->size);
379
380         /* 
381          * do not need a rcu_dereference()() here to force ordering
382          * on Alpha, since the ipc_ids.mutex is held.
383          */     
384         p = ids->entries->p[lid];
385         ids->entries->p[lid] = NULL;
386         BUG_ON(p==NULL);
387         ids->in_use--;
388
389         if (lid == ids->max_id) {
390                 do {
391                         lid--;
392                         if(lid == -1)
393                                 break;
394                 } while (ids->entries->p[lid] == NULL);
395                 ids->max_id = lid;
396         }
397         p->deleted = 1;
398         return p;
399 }
400
401 /**
402  *      ipc_alloc       -       allocate ipc space
403  *      @size: size desired
404  *
405  *      Allocate memory from the appropriate pools and return a pointer to it.
406  *      NULL is returned if the allocation fails
407  */
408  
409 void* ipc_alloc(int size)
410 {
411         void* out;
412         if(size > PAGE_SIZE)
413                 out = vmalloc(size);
414         else
415                 out = kmalloc(size, GFP_KERNEL);
416         return out;
417 }
418
419 /**
420  *      ipc_free        -       free ipc space
421  *      @ptr: pointer returned by ipc_alloc
422  *      @size: size of block
423  *
424  *      Free a block created with ipc_alloc. The caller must know the size
425  *      used in the allocation call.
426  */
427
428 void ipc_free(void* ptr, int size)
429 {
430         if(size > PAGE_SIZE)
431                 vfree(ptr);
432         else
433                 kfree(ptr);
434 }
435
436 /*
437  * rcu allocations:
438  * There are three headers that are prepended to the actual allocation:
439  * - during use: ipc_rcu_hdr.
440  * - during the rcu grace period: ipc_rcu_grace.
441  * - [only if vmalloc]: ipc_rcu_sched.
442  * Their lifetime doesn't overlap, thus the headers share the same memory.
443  * Unlike a normal union, they are right-aligned, thus some container_of
444  * forward/backward casting is necessary:
445  */
446 struct ipc_rcu_hdr
447 {
448         int refcount;
449         int is_vmalloc;
450         void *data[0];
451 };
452
453
454 struct ipc_rcu_grace
455 {
456         struct rcu_head rcu;
457         /* "void *" makes sure alignment of following data is sane. */
458         void *data[0];
459 };
460
461 struct ipc_rcu_sched
462 {
463         struct work_struct work;
464         /* "void *" makes sure alignment of following data is sane. */
465         void *data[0];
466 };
467
468 #define HDRLEN_KMALLOC          (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
469                                         sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
470 #define HDRLEN_VMALLOC          (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
471                                         sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
472
473 static inline int rcu_use_vmalloc(int size)
474 {
475         /* Too big for a single page? */
476         if (HDRLEN_KMALLOC + size > PAGE_SIZE)
477                 return 1;
478         return 0;
479 }
480
481 /**
482  *      ipc_rcu_alloc   -       allocate ipc and rcu space 
483  *      @size: size desired
484  *
485  *      Allocate memory for the rcu header structure +  the object.
486  *      Returns the pointer to the object.
487  *      NULL is returned if the allocation fails. 
488  */
489  
490 void* ipc_rcu_alloc(int size)
491 {
492         void* out;
493         /* 
494          * We prepend the allocation with the rcu struct, and
495          * workqueue if necessary (for vmalloc). 
496          */
497         if (rcu_use_vmalloc(size)) {
498                 out = vmalloc(HDRLEN_VMALLOC + size);
499                 if (out) {
500                         out += HDRLEN_VMALLOC;
501                         container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
502                         container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
503                 }
504         } else {
505                 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
506                 if (out) {
507                         out += HDRLEN_KMALLOC;
508                         container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
509                         container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
510                 }
511         }
512
513         return out;
514 }
515
516 void ipc_rcu_getref(void *ptr)
517 {
518         container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
519 }
520
521 static void ipc_do_vfree(struct work_struct *work)
522 {
523         vfree(container_of(work, struct ipc_rcu_sched, work));
524 }
525
526 /**
527  * ipc_schedule_free - free ipc + rcu space
528  * @head: RCU callback structure for queued work
529  * 
530  * Since RCU callback function is called in bh,
531  * we need to defer the vfree to schedule_work
532  */
533 static void ipc_schedule_free(struct rcu_head *head)
534 {
535         struct ipc_rcu_grace *grace =
536                 container_of(head, struct ipc_rcu_grace, rcu);
537         struct ipc_rcu_sched *sched =
538                         container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
539
540         INIT_WORK(&sched->work, ipc_do_vfree);
541         schedule_work(&sched->work);
542 }
543
544 /**
545  * ipc_immediate_free - free ipc + rcu space
546  * @head: RCU callback structure that contains pointer to be freed
547  *
548  * Free from the RCU callback context
549  */
550 static void ipc_immediate_free(struct rcu_head *head)
551 {
552         struct ipc_rcu_grace *free =
553                 container_of(head, struct ipc_rcu_grace, rcu);
554         kfree(free);
555 }
556
557 void ipc_rcu_putref(void *ptr)
558 {
559         if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
560                 return;
561
562         if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
563                 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
564                                 ipc_schedule_free);
565         } else {
566                 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
567                                 ipc_immediate_free);
568         }
569 }
570
571 /**
572  *      ipcperms        -       check IPC permissions
573  *      @ipcp: IPC permission set
574  *      @flag: desired permission set.
575  *
576  *      Check user, group, other permissions for access
577  *      to ipc resources. return 0 if allowed
578  */
579  
580 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
581 {       /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
582         int requested_mode, granted_mode, err;
583
584         if (unlikely((err = audit_ipc_obj(ipcp))))
585                 return err;
586         requested_mode = (flag >> 6) | (flag >> 3) | flag;
587         granted_mode = ipcp->mode;
588         if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
589                 granted_mode >>= 6;
590         else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
591                 granted_mode >>= 3;
592         /* is there some bit set in requested_mode but not in granted_mode? */
593         if ((requested_mode & ~granted_mode & 0007) && 
594             !capable(CAP_IPC_OWNER))
595                 return -1;
596
597         return security_ipc_permission(ipcp, flag);
598 }
599
600 /*
601  * Functions to convert between the kern_ipc_perm structure and the
602  * old/new ipc_perm structures
603  */
604
605 /**
606  *      kernel_to_ipc64_perm    -       convert kernel ipc permissions to user
607  *      @in: kernel permissions
608  *      @out: new style IPC permissions
609  *
610  *      Turn the kernel object 'in' into a set of permissions descriptions
611  *      for returning to userspace (out).
612  */
613  
614
615 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
616 {
617         out->key        = in->key;
618         out->uid        = in->uid;
619         out->gid        = in->gid;
620         out->cuid       = in->cuid;
621         out->cgid       = in->cgid;
622         out->mode       = in->mode;
623         out->seq        = in->seq;
624 }
625
626 /**
627  *      ipc64_perm_to_ipc_perm  -       convert old ipc permissions to new
628  *      @in: new style IPC permissions
629  *      @out: old style IPC permissions
630  *
631  *      Turn the new style permissions object in into a compatibility
632  *      object and store it into the 'out' pointer.
633  */
634  
635 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
636 {
637         out->key        = in->key;
638         SET_UID(out->uid, in->uid);
639         SET_GID(out->gid, in->gid);
640         SET_UID(out->cuid, in->cuid);
641         SET_GID(out->cgid, in->cgid);
642         out->mode       = in->mode;
643         out->seq        = in->seq;
644 }
645
646 /*
647  * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
648  * is called with shm_ids.mutex locked.  Since grow_ary() is also called with
649  * shm_ids.mutex down(for Shared Memory), there is no need to add read
650  * barriers here to gurantee the writes in grow_ary() are seen in order 
651  * here (for Alpha).
652  *
653  * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
654  * if in the future ipc_get() is used by other places without ipc_ids.mutex
655  * down, then ipc_get() needs read memery barriers as ipc_lock() does.
656  */
657 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
658 {
659         struct kern_ipc_perm* out;
660         int lid = id % SEQ_MULTIPLIER;
661         if(lid >= ids->entries->size)
662                 return NULL;
663         out = ids->entries->p[lid];
664         return out;
665 }
666
667 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
668 {
669         struct kern_ipc_perm* out;
670         int lid = id % SEQ_MULTIPLIER;
671         struct ipc_id_ary* entries;
672
673         rcu_read_lock();
674         entries = rcu_dereference(ids->entries);
675         if(lid >= entries->size) {
676                 rcu_read_unlock();
677                 return NULL;
678         }
679         out = entries->p[lid];
680         if(out == NULL) {
681                 rcu_read_unlock();
682                 return NULL;
683         }
684         spin_lock(&out->lock);
685         
686         /* ipc_rmid() may have already freed the ID while ipc_lock
687          * was spinning: here verify that the structure is still valid
688          */
689         if (out->deleted) {
690                 spin_unlock(&out->lock);
691                 rcu_read_unlock();
692                 return NULL;
693         }
694         return out;
695 }
696
697 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
698 {
699         rcu_read_lock();
700         spin_lock(&perm->lock);
701 }
702
703 void ipc_unlock(struct kern_ipc_perm* perm)
704 {
705         spin_unlock(&perm->lock);
706         rcu_read_unlock();
707 }
708
709 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
710 {
711         return SEQ_MULTIPLIER*seq + id;
712 }
713
714 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
715 {
716         if(uid/SEQ_MULTIPLIER != ipcp->seq)
717                 return 1;
718         return 0;
719 }
720
721 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
722
723
724 /**
725  *      ipc_parse_version       -       IPC call version
726  *      @cmd: pointer to command
727  *
728  *      Return IPC_64 for new style IPC and IPC_OLD for old style IPC. 
729  *      The cmd value is turned from an encoding command and version into
730  *      just the command code.
731  */
732  
733 int ipc_parse_version (int *cmd)
734 {
735         if (*cmd & IPC_64) {
736                 *cmd ^= IPC_64;
737                 return IPC_64;
738         } else {
739                 return IPC_OLD;
740         }
741 }
742
743 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
744
745 #ifdef CONFIG_PROC_FS
746 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
747 {
748         struct ipc_proc_iface *iface = s->private;
749         struct kern_ipc_perm *ipc = it;
750         loff_t p;
751         struct ipc_ids *ids;
752
753         ids = current->nsproxy->ipc_ns->ids[iface->ids];
754
755         /* If we had an ipc id locked before, unlock it */
756         if (ipc && ipc != SEQ_START_TOKEN)
757                 ipc_unlock(ipc);
758
759         /*
760          * p = *pos - 1 (because id 0 starts at position 1)
761          *          + 1 (because we increment the position by one)
762          */
763         for (p = *pos; p <= ids->max_id; p++) {
764                 if ((ipc = ipc_lock(ids, p)) != NULL) {
765                         *pos = p + 1;
766                         return ipc;
767                 }
768         }
769
770         /* Out of range - return NULL to terminate iteration */
771         return NULL;
772 }
773
774 /*
775  * File positions: pos 0 -> header, pos n -> ipc id + 1.
776  * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
777  */
778 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
779 {
780         struct ipc_proc_iface *iface = s->private;
781         struct kern_ipc_perm *ipc;
782         loff_t p;
783         struct ipc_ids *ids;
784
785         ids = current->nsproxy->ipc_ns->ids[iface->ids];
786
787         /*
788          * Take the lock - this will be released by the corresponding
789          * call to stop().
790          */
791         mutex_lock(&ids->mutex);
792
793         /* pos < 0 is invalid */
794         if (*pos < 0)
795                 return NULL;
796
797         /* pos == 0 means header */
798         if (*pos == 0)
799                 return SEQ_START_TOKEN;
800
801         /* Find the (pos-1)th ipc */
802         for (p = *pos - 1; p <= ids->max_id; p++) {
803                 if ((ipc = ipc_lock(ids, p)) != NULL) {
804                         *pos = p + 1;
805                         return ipc;
806                 }
807         }
808         return NULL;
809 }
810
811 static void sysvipc_proc_stop(struct seq_file *s, void *it)
812 {
813         struct kern_ipc_perm *ipc = it;
814         struct ipc_proc_iface *iface = s->private;
815         struct ipc_ids *ids;
816
817         /* If we had a locked segment, release it */
818         if (ipc && ipc != SEQ_START_TOKEN)
819                 ipc_unlock(ipc);
820
821         ids = current->nsproxy->ipc_ns->ids[iface->ids];
822         /* Release the lock we took in start() */
823         mutex_unlock(&ids->mutex);
824 }
825
826 static int sysvipc_proc_show(struct seq_file *s, void *it)
827 {
828         struct ipc_proc_iface *iface = s->private;
829
830         if (it == SEQ_START_TOKEN)
831                 return seq_puts(s, iface->header);
832
833         return iface->show(s, it);
834 }
835
836 static struct seq_operations sysvipc_proc_seqops = {
837         .start = sysvipc_proc_start,
838         .stop  = sysvipc_proc_stop,
839         .next  = sysvipc_proc_next,
840         .show  = sysvipc_proc_show,
841 };
842
843 static int sysvipc_proc_open(struct inode *inode, struct file *file) {
844         int ret;
845         struct seq_file *seq;
846
847         ret = seq_open(file, &sysvipc_proc_seqops);
848         if (!ret) {
849                 seq = file->private_data;
850                 seq->private = PDE(inode)->data;
851         }
852         return ret;
853 }
854
855 static struct file_operations sysvipc_proc_fops = {
856         .open    = sysvipc_proc_open,
857         .read    = seq_read,
858         .llseek  = seq_lseek,
859         .release = seq_release,
860 };
861 #endif /* CONFIG_PROC_FS */